Jump to content

Need help with a FTP hook I wrote please


webjive

Recommended Posts

I wrote this hook a while back and sidelined it due to Chrome and Firefox having FTP embedded. On Macs, there was just about no way of not invoking the built in FTP vs using the OS set FTP client.

Now that all of that mess is gone from browsers, I decided to dig back into this hook. 

The reason for the hook is that we build a LOT of sites and having an FTP url lining out to a local FTP client is SUPER convenient vs having to copy/paste credentials into Transmit FTP.

The problem I'm having is with the SQL not picking up on user and password if there's a single account. If they have multiple and I switch to another client account, the server/user/pass all get populated correctly. Also, the FTP credentials are showing twice on the account admin screen. 

Attached is the code and a screenshot of what's happening on a single account (ftp server/user/pass missing) and if I switch accounts, how it fills in and shows in the admin screen.

Any help is TRULY appreciated!

P.S. Can't embed the code, I keep getting a 403 so attaching a file!  UGH!

https://www.dropbox.com/s/duxm8y318xxelzc/My hook code.txt?dl=0

744794018_singleaccount.jpg.3f176e2da0d88bae7dc8497d8a082d9e.jpg56869916_multipleaccountsandafteriswitchtoanotherone.jpg.7b042ad877c5e72ced7a8c15c3fff901.jpg

Link to comment
Share on other sites

Unfortunately there are multiple issues in the code. And it's unsafe, i will quote myself regarding mysql_real_escape_string:

Quote

I want to note: This update query is insecure and makes you vulnerable to SQL injections. <snip> security risk as mysql_real_escape_string does not protect against all kinds of SQL injections. And mysql_real_escape_string has been removed from recent PHP versions. As far i know, WHMCS has rebuilt this function so it still works, but they may remove it in a later version.

I have rewritten this hook for you:

<?php

use WHMCS\Service\Service;


add_hook('AdminClientServicesTabFields', 1, function($vars) {
    try {
        $getService = Service::findOrFail($vars['id']);
    } catch (Exception $e) {
        return;
    }
    
    $getPassword = localAPI('DecryptPassword', ['password2' => $getService['password'] ]);
    
    if ($getPassword['result'] !== 'success') {
        return;
    }
    
    $username = $getService['username'];
    $password = $getPassword['password'];
    $domain = $getService['domain'];
    
    if (empty(trim($domain))) {
        $domain = $getService->serverModel['hostname'];
    }    
    
    echo "
        <script>
            jQuery(document).ready(function() {
                jQuery('#inputPassword').closest('tr').after(`
                    <tr>
                        <td class='fieldlabel' width='20%'>Live Site FTP URL</td>
                        <td class='fieldarea'>{$username} {$password} and so on</td>
                    </tr>
                    <tr>
                        <td class='fieldlabel' width='20%'>Server FTP URL</td>
                        <td class='fieldarea'>{$username} {$password} and so on</td>
                    </tr>
                `);
            });
        </script>
        asdfasdfsafs
    ";
});

You just need to update the output according to your needs 🙂

Edited by string
-
Link to comment
Share on other sites

Thank you SO much. As you can see, my coding skills are uh.. you get the pic.

My final code if someone needs a hook for opening FTP

<?php

use WHMCS\Service\Service;


add_hook('AdminClientServicesTabFields', 1, function($vars) {
    try {
        $getService = Service::findOrFail($vars['id']);
    } catch (Exception $e) {
        return;
    }
    
    $getPassword = localAPI('DecryptPassword', ['password2' => $getService['password'] ]);
    
    if ($getPassword['result'] !== 'success') {
        return;
    }
    
    $username = $getService['username'];
    $password = $getPassword['password'];
    $domain = $getService['domain'];
    
	$ftpurl = '<a href="ftp://'.$username.':'.$password.'@'.$domain.'">Connect</a>';

    if (empty(trim($domain))) {
        $domain = $getService->serverModel['hostname'];
    }    
    
    echo "
        <script>
            jQuery(document).ready(function() {
                jQuery('#inputPassword').closest('tr').after(`
                    <tr>
                        <td class='fieldlabel' width='20%'>Live Site FTP URL</td>
                        <td class='fieldarea'>{$ftpurl}</td>
                    </tr>
                `);
            });
        </script>
        asdfasdfsafs
    ";
});

 

Edited by webjive
Link to comment
Share on other sites

Updated it to have a button instead of a link

<?php

use WHMCS\Service\Service;


add_hook('AdminClientServicesTabFields', 1, function($vars) {
    try {
        $getService = Service::findOrFail($vars['id']);
    } catch (Exception $e) {
        return;
    }
    
    $getPassword = localAPI('DecryptPassword', ['password2' => $getService['password'] ]);
    
    if ($getPassword['result'] !== 'success') {
        return;
    }
    
    $username = $getService['username'];
    $password = $getPassword['password'];
    $domain = $getService['domain'];
    
	$ftpurl = '<a href="ftp://'.$username.':'.$password.'@'.$domain.'"><input type="button" value="Connect"></a>';

    if (empty(trim($domain))) {
        $domain = $getService->serverModel['hostname'];
    }    
    
    echo "
        <script>
            jQuery(document).ready(function() {
                jQuery('#inputPassword').closest('tr').after(`
                    <tr>
                        <td class='fieldlabel' width='20%'>Live Site FTP URL</td>
                        <td class='fieldarea'>{$ftpurl}</td>
                    </tr>
                `);
            });
        </script>
        asdfasdfsafs
    ";
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated