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.

Create subdomain and FTP user account using PHP

Discussion in 'General Discussion' started by MarkCool, Dec 1, 2008.

  1. MarkCool

    MarkCool Registered

    Joined:
    Dec 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    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 :)
     
    #1 MarkCool, Dec 1, 2008
    Last edited: Dec 1, 2008
Loading...

Share This Page