The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

FTP accounts creation from my PHP script

Discussion in 'General Discussion' started by maysoft, Mar 27, 2007.

  1. maysoft

    maysoft Well-Known Member

    Joined:
    Nov 10, 2005
    Messages:
    64
    Likes Received:
    0
    Trophy Points:
    6
    I have created a generic user (not a reseller) with unlimited FTP accounts, and I want to be able to create FTP accounts under that username, but not from cpanel but from my own PHP script. I have some positive experience with creating accounts from scripts, but I couldn't find how to proceed with simple FTP accounts :)

    I was also unable to find a corresponding script in /scripts folder (so that I could theoretically build a PHP script that would just use that script)....

    Any help is greatly appreciated.
     
  2. maysoft

    maysoft Well-Known Member

    Joined:
    Nov 10, 2005
    Messages:
    64
    Likes Received:
    0
    Trophy Points:
    6
    Well, I've managed to accomplish this the "hard way". I made a PHP script that acts just like a browser (connecting to the real cpanel account via http, POSTing the data and parsing the answer).
     
  3. serj21

    serj21 Member

    Joined:
    Oct 11, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Looking for a similar solution.

    I basically want a way to either:
    * Have a php script that sets up FTP accounts for me.

    or

    * A way of enabling pureftpd to run with mysql support so user info can be grabbed from a database.

    Does anyone have any advice? Help?
     
  4. maysoft

    maysoft Well-Known Member

    Joined:
    Nov 10, 2005
    Messages:
    64
    Likes Received:
    0
    Trophy Points:
    6
    I think I will answer your PM here since others may be interested in this as well.

    I cannot provide a "complete solution" since I already integrated it too deep into my scripts, so I have rewritten the most important part for this forum:

    PHP:
    $username 'your_cpanel_login';
    $password 'your_cpanel_password';

    $ftp_data = array ();
    $ftp_data 'login' ] = 'login';
    $ftp_data 'password' ] = 'pwd';
    $ftp_data 'quota' ] = '1000'//megabytes
    $ftp_data 'homedir' ] = 'accounts/' $ftp_data 'login' ];

    $host 'server.address.com';

    $uri '/frontend/x2/ftp/doaddftp.html'// if you are not using X2, then you will have to change this

    //
    // ======= yeah baby, lets add the user! =======
    // 

    $reqbody ""
    foreach (
    $ftp_data as $key => $val) { 
        if ( 
    $reqbody$reqbody .= "&"
        
    $reqbody .= $key "=" urlencode ($val) ; 


    $contentlength strlen ($reqbody) ; 

    $reqheader =  "POST $uri HTTP/1.1\r\n";
    $reqheader .= "Host: $host\n""User-Agent: ftp account creator v0.01a\r\n";
    $reqheader .= "Connection: close\r\n";
    $reqheader .=  "Authorization: Basic " base64_encode ("$username:$password") . "\r\n";
    $reqheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $reqheader .= "Content-Length: $contentlength\r\n";
    $reqheader .= "\r\n";
    $reqheader .= "$reqbody\r\n"

    $socket fsockopen ($host$port$errno$errstr); 

    $result '';
    if (!
    $socket) { 
        
    $result .= 'ERROR: ' $errno ' ' $errstr


    fputs ($socket$reqheader); 

    $answer_header '';
    do 
    $answer_header.= fread ($socket1); while (!preg_match ('/\\r\\n\\r\\n$/'$answer_header));

    if (!
    stristr ($answer_header"200 OK")) {
        
    $result .= 'Houston, we have a problem: ' $answer_header
    }

    while (!
    feof ($socket) ) $result .= fgets($socket4096); 
    fclose($socket); 



    //
    // now you just parse the result and see if it is good.
    //
    //
    // example:
    //
    if ( stristr $result'Ignore any messages of success' ) ) 
    {
        echo 
    "crap! it didn't work out!<br> Dumping out the result:<br><hr>" str_replace "\n"'<br>'$result );
    }

    Enjoy! :)
     
    #4 maysoft, Mar 30, 2007
    Last edited: Mar 30, 2007
  5. maysoft

    maysoft Well-Known Member

    Joined:
    Nov 10, 2005
    Messages:
    64
    Likes Received:
    0
    Trophy Points:
    6
    Oops. Just noticed, when there is an error happens, you not only have to assign a value to $error var, but you have to end the script (with something like die or return).
     
    #5 maysoft, Mar 31, 2007
    Last edited: Mar 31, 2007
Loading...

Share This Page