Jump to content

A new problem with hooks and FTP {{{HELP!}}}


thebizbuilder

Recommended Posts

Hi all,

 

I'm looking for some quick help on this problem that I'm having.

What I want is a hook that transfers certain files to a newly created hosting package once it has been created. This is the code that I have so far and there is some additional code that I have used to test the ftp functions I have, these work fine on their own, however when I use them in the hook they fail to transfer the files over ftp to the newly created hosting package.

 

Please take a look and see if you can spot anything and let me know...

My appolagies for the messy code I have been changing and mixing things up trying to get it work and it has left it in a right state!

 

<?php

/*
********************************************
* Hook file that will detect when a client *
* has paid and activated their account and *
* uploads their selected site to their     *
* hosting.								   *
********************************************
*/

function install_package($params)
{
       #sleep has been put in because maybe this script was running too quickly after creation of module
sleep(10);
#THIS FUNCTION INSTALL THE PACKAGE THAT WAS REQUESTED AT TIME OF ORDERING.
#FIRST GET THE PACKAGE THAT IS NEEDING INSTALLING

$dir = getSiteDir($params['params']["customfields"]);

#TRANSFER ALL FILES TO NEW DOMAIN
   // set the various variables
   $ftproot = "/httpdocs/";
   $srcroot = "/var/www/vhosts/********/httpdocs/sites/".$dir;       
   $srcrela = "/";

   // connect to the destination FTP & enter appropriate directories both locally and remotely
   $ftpc = ftp_connect($params['params']['serverip']);
   $ftpr = ftp_login($ftpc,$params['params']['username'],$params['params']['password']);

   if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
   if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
   if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }

   // start ftp'ing over the directory recursively
   ftpRec ($srcrela);

   // close the FTP connection
   ftp_close($ftpc);

#CREATE AND UPLOAD CONFIG FILE FOR USERNAME AND PASSWORD FOR CONTROL PANEL ETC

}


add_hook("AfterModuleCreate",1,"install_package");
// --------------------------------------------------------------------
// THE ACTUAL FUNCTION
// --------------------------------------------------------------------
function ftpRec ($srcrela)
{
   global $srcroot;
   global $ftproot;
   global $ftpc;
   global $ftpr;

   // enter the local directory to be recursed through
   chdir($srcroot.$srcrela);

   // check if the directory exists & change to it on the destination
   if (!ftp_chdir($ftpc,$ftproot.$srcrela))
   {
       // remote directory doesn't exist so create & enter it
          ftp_mkdir    ($ftpc,$ftproot.$srcrela);
          ftp_chdir    ($ftpc,$ftproot.$srcrela);
   }

   if ($handle = opendir("."))
   {
      while (false !== ($fil = readdir($handle)))
      {
          if ($fil != "." && $fil != "..")   
          {
              // check if it's a file or directory
              if (!is_dir($fil))
              {   
                  // it's a file so upload it
                  $res .= ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
               }
               else
               {
                   // it's a directory so recurse through it
                   if ($fil == "templates")
                   {
                           // I want the script to ignore any directories named "templates"
                           // and therefore, not recurse through them and upload their contents
                   }
                   else
                   {
                       ftpRec ($srcrela.$fil."/");
                       chdir ("../");
                   }
               }
         }
     }
     closedir($handle);

   }   
}

function getSiteDir($customfields)
{
#FUNCTION TO GET THE DIRECTORY OF THE SELECTED SITE AND RETURN IT

#FOR TESTING PURPOSES THIS WILL BE SET TO RETURN TESTSITE
$dir = 'testsite';
return $dir;
}
?>

Link to comment
Share on other sites

I was reading through, looked ok but...

 

global $srcroot; 
global $ftproot; 
global $ftpc; 
global $ftpr;

Needs to be in both of the functions. install_package and the one it's in now... ftpRec i think.

 

That's the first thing I saw and posted this. I briefly read the rest but I think that's your issue.

 

So turn this:

 

function install_package($params) 
{ 
    #sleep has been put in because maybe this script was running too quickly after creation of module 
   sleep(10); 
   #THIS FUNCTION INSTALL THE PACKAGE THAT WAS REQUESTED AT TIME OF ORDERING. 
   #FIRST GET THE PACKAGE THAT IS NEEDING INSTALLING 

   $dir = getSiteDir($params['params']["customfields"]); 

To this:

 

function install_package($params) 
{ 

   // Declare globals
   global $srcroot; 
   global $ftproot; 
   global $ftpc; 
   global $ftpr;

    #sleep has been put in because maybe this script was running too quickly after creation of module 
   sleep(10); 
   #THIS FUNCTION INSTALL THE PACKAGE THAT WAS REQUESTED AT TIME OF ORDERING. 
   #FIRST GET THE PACKAGE THAT IS NEEDING INSTALLING 

   $dir = getSiteDir($params['params']["customfields"]); 

You shouldn't need to sleep it either...

 

Let me know if this sorts it out for you :)

Link to comment
Share on other sites

Hi,

Thank you so much for your reply. That seems to have worked however there is another problem and again I have no idea why its doing this!

 

My file structure inside the '"/var/www/vhosts/********/httpdocs/sites/".$dir'

is like:

/testsite/

index.html

thankyou.html

/css/style.css

/css/style2.css

/images/blah.jpg

/images/blah.jpg

/images/blah.jpg

/images/blah.jpg

 

The script seems to be creating and uploading everything in the CSS directory perfectly but doesn't do anything else :?

 

However its heading in the right direction. I don't get any syntax errors or anything but I get a lot of php WARNING errors, so many that it crashes my browser lol! I'm trying to see if they are stored in the log on the server to put them here!

 

OOOoo here are the warnings I am getting...well some of them, there are LOADS!!!

 

[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: chdir() [<a href='function.chdir'>function.chdir</a>]: open_basedir restriction in effect. File(../) is not within the allowed path(s): (/var/www/vhosts/******/httpdocs:.:/php:/usr:/tmp:/etc:/var) in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 99, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: chdir() [<a href='function.chdir'>function.chdir</a>]: open_basedir restriction in effect. File(../) is not within the allowed path(s): (/var/www/vhosts/******/httpdocs:.:/php:/usr:/tmp:/etc:/var) in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 99, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: ftp_put() expects parameter 1 to be resource, null given in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 86, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate
[Fri Dec 10 19:45:54 2010] [error] [client 81.97.91.187] PHP Warning: chdir() [<a href='function.chdir'>function.chdir</a>]: open_basedir restriction in effect. File(../) is not within the allowed path(s): (/var/www/vhosts/******/httpdocs:.:/php:/usr:/tmp:/etc:/var) in /var/www/vhosts/******/httpdocs/whmcs/includes/hooks/uploadpackage.php on line 99, referer: http://www.******/whmcs/admin/clientshosting.php?userid=4&id=69&modop=terminate

 

I hope you can help me on this.

Thank you so much again, you are the first person to help me :D

Link to comment
Share on other sites

LMAO, sorry in the in the frustration of it not working and me being an idiot and changing all different things to get it to work I had deleted the GLOBALS from the ftpRec function, I just put them back in and now its working!!! :D:D:D:D this is GREEAT thank you for all your help bdigitalstudios ur a star! :D:D:D:D

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