Results 1 to 1 of 1

Thread: Create subdomain and FTP user account using PHP

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Default Create subdomain and FTP user account using PHP

    Hi Everyone,
    this is my first post here, I have tried all over the net!

    I have a script that creates an subdomain in cpanel and an FTP account for that subdomain, however everytime I run it I get an error;

    in ScriptAlias cgi-bin entry for subdomain in apache config, once added its putting entry as

    ScriptAlias /cgi-bin/ /home/user/public_html/
    cgi-bin/

    But it should be
    ScriptAlias /cgi-bin/ /home/user/public_html/cgi-bin/

    So its splitting the ScriptAlias path in 2 lines.

    And this stops all websites in my shared hosting account from working. Obviously I am not the most favourite person in the eyes of my hosting provider.

    Here is the code that creates the subdomain
    Code:
    <?php
    
    ###############################################################
    # cPanel Subdomains Creator 1.1
    ###############################################################
    # Visit http://www.zubrag.com/scripts/ for updates
    ###############################################################
    #
    # Can be used in 3 ways:
    # 1. just open script in browser and fill the form
    # 2. pass all info via url and form will not appear
    # Sample: cpanel_subdomains.php?cpaneluser=USER&cpanelpass=PASSWORD&domain=DOMAIN&subdomain=SUBDOMAIN
    # 3. list subdomains in file. In this case you must provide all the defaults below
    #
    # Note: you can omit any parameter, except "subdomain".
    # When omitted, default value specified below will be taken
    ###############################################################
    
    // cpanel user
    define('CPANELUSER','username');
    
    // cpanel password
    define('CPANELPASS','password');
    
    // name of the subdomains list file.
    // file format may be 1 column or 2 columns divided with semicilon (;)
    // Example for two columns:
    //   rootdomain1;subdomain1
    //   rootdomain1;subdomain2
    // Example for one columns:
    //   subdomain1
    //   subdomain2
    define('INPUT_FILE','domains.txt');
    
    // cPanel skin (mainly "x")
    // Check http://www.zubrag.com/articles/determine-cpanel-skin.php
    // to know it for sure
    define('CPANEL_SKIN','x3');
    
    // Default domain (subdomains will be created for this domain)
    // Will be used if not passed via parameter and not set in subdomains file
    define('DOMAIN','domain');
    
    
    /////////////// END OF INITIAL SETTINGS ////////////////////////
    ////////////////////////////////////////////////////////////////
    
    function getVar($name, $def = '') {
      if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != ''))
        return $_REQUEST[$name];
      else 
        return $def;
    }
    
    $cpaneluser=getVar('cpaneluser', CPANELUSER);
    $cpanelpass=getVar('cpanelpass', CPANELPASS);
    $cpanel_skin = getVar('cpanelskin', CPANEL_SKIN);
    
    if (isset($_REQUEST["subdomain"])) {
      // get parameters passed via URL or form, emulate string from file 
      $doms = array( getVar('domain', DOMAIN) . ";" . $_REQUEST["subdomain"]);
      if (getVar('domain', DOMAIN) == '') die("You must specify domain name");
    }
    else {
      // open file with domains list
      $doms = @file(INPUT_FILE);
      if (!$doms) {
        // file does not exist, show input form
        echo "
    
    <form method='post'>
      Subdomain:<input name='subdomain'><br>
       FTP pass:<input name='fpass'><br>
      <input type='submit' value='Create Subdomain' style='border:1px solid black'>
    </form>";
        die();
      }
    }
    
    // create subdomain
    function subd($host,$port,$ownername,$passw,$request) {
    
      $sock = fsockopen('localhost',2082);
      if(!$sock) {
        print('Socket error');
        exit();
      }
    
      $authstr = "$ownername:$passw";
      $pass = base64_encode($authstr);
      $in = "GET $request\r\n";
      $in .= "HTTP/1.0\r\n";
      $in .= "Host:$host\r\n";
      $in .= "Authorization: Basic $pass\r\n";
      $in .= "\r\n";
     
      fputs($sock, $in);
      while (!feof($sock)) {
        $result .= fgets ($sock,128);
      }
      fclose( $sock );
    
      return $result;
    }
    
    foreach($doms as $dom) {
      $lines = explode(';',$dom);
      if (count($lines) == 2) {
        // domain and subdomain passed
        $domain = trim($lines[0]);
        $subd = trim($lines[1]);
      }
      else {
        // only subdomain passed
        $domain = getVar('domain', DOMAIN);
        $subd = trim($lines[0]);
      }
      // http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here]
      $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
      $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);
      $show = strip_tags($result);
      echo "domain created!"; 
     include("ftp.php"); 
    }
    
    ?>
    And here is the code that creates the FTP account

    Code:
    <?php
    
    ###############################################################
    # cPanel FTP Account Creator 1.0
    ###############################################################
    # Visit http://www.zubrag.com/scripts/ for updates
    ###############################################################
    # Required parameters:
    # - domain - create ftp account for this domain
    # - fuser - ftp account username
    # - fpass - ftp account password
    # - fquota - ftp account quota
    # - fhomedir - ftp account home directory (home folder)
    #
    # Sample run: cpanel-add-ftp.php?domain=reseller.com&fuser=ftp555&fpass=ftp12345&fquota=50&fhomedir=/
    #
    # This script can also be run from another PHP script. This may
    # be helpful if you have some user interface already in place and 
    # want to automatically create FTP accounts from there.
    # In this case you have to setup following variables instead of
    # passing them via url as parameters:
    # - $domain - new account domain
    # - $fuser - new ftp account username
    # - $fpass - new ftp account password
    # - $fquota - account quota
    # - $fhomedir - user's home directory
    #
    # Feel free to post your questions and comments at http://www.zubrag.com/forum/
    #
    ###############################################################
    
    #####################################################################################
    ##############        START OF SETTINGS. YOU MAY EDIT BELOW    ######################
    #####################################################################################
    
    // Cpanel username and password
    $user = "";
    $pass = "";
    
    // cpanel skin. For more info on what is your skin check 
    // this url      http://www.zubrag.com/articles/determine-cpanel-skin.php 
    $skin = "x3";
    
    #####################################################################################
    ##############          END OF SETTINGS. DO NOT EDIT BELOW    #######################
    #####################################################################################
    
    
    
    // ftp account for domain
    if (!isset($domain)) {
      $domain = getVar('subdomain') . paygwebs.co.uk;
    }
    
    // ftp user
    if (!isset($fuser)) {
      $fuser = getVar('subdomain');
    }
    
    // ftp password
    if (!isset($fpass)) {
      $fpass = getVar('fpass');
    }
    
    // ftp quota
    if (!isset($fquota)) {
      $fquota = "0";
    }
    
    // ftp homedir
    if (!isset($fhomedir)) {
      $fhomedir = "public_html/" . getVar('subdomain');
    }
    
    $url = "http://$user:$pass@$domain:2082/frontend/$skin/ftp/doaddftp.html?";
    $url = $url . "login=$fuser&password=$fpass&homedir=$fhomedir&quota=$fquota";
    $result = @file_get_contents($url);
    if ($result === FALSE) die("ERROR: FTP Account not created. Please make sure you passed correct parameters.");
    echo $result;
    
    ?>
    Can anyone see what is going wrong and why I am getting this error? I appreciate any help you can offer :-)
    Last edited by MarkCool; 12-01-2008 at 12:56 PM.

Similar Threads

  1. Create a user account with PHP (script)?
    By MrLeN in forum cPanel & WHM Discussions
    Replies: 2
    Last Post: 12-19-2010, 12:27 PM
  2. Create subdomain with seperate user account
    By section31 in forum cPanel & WHM Discussions
    Replies: 3
    Last Post: 07-29-2008, 10:14 PM
  3. Create system FTP user but no cPanel account
    By divisionbyzero in forum cPanel & WHM Discussions
    Replies: 2
    Last Post: 05-15-2007, 03:01 PM
  4. Is there any way to create a new FTP account from PHP?
    By arkamax in forum cPanel & WHM Discussions
    Replies: 3
    Last Post: 06-15-2005, 06:07 AM
  5. Create FTP Account - not to /public_html/user
    By justhost in forum cPanel & WHM Discussions
    Replies: 0
    Last Post: 02-04-2004, 02:21 PM