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.

xml-api script works only sometimes?

Discussion in 'cPanel Developers' started by progamerinc, Apr 19, 2010.

  1. progamerinc

    progamerinc Member

    Joined:
    Apr 10, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hey - So this script works great. Sometimes. About 50% of the time it does not make the database. All the accounts it was tested on are members of the same package

    I am at a loss for what the problem is. I would guess it is something to do with cpanel/whm and whmcs being stupid but, I just want to run this by people who know more about this sort of than than myself.

    Also: Maybe the code can help someone trying to figure out what to do with the xmlapi.php class (Call me slow but I had no idea what to do with it at first)

    NOTE: This is a partial class but, all the stuff relating to the database creation is shown. The rest is just custom stuff relating to my application
    PHP:

    require("xmlapi.php");
    $installer = new installer();
    if (
    $installer->pageCheck() != "1") {
        die(
    "URL not authorized.");
    }

    $installer->makeDatabase();            // Creates the database. -> Disabled for testing so the db info remains constant.
    $installer->writeDatabase();         // Makes the database.
    $installer->makeconfig();            // Writes the config file.

    class installer {
        var 
    $ip "127.0.0.1";
        var 
    $root_pass "*****";
        var 
    $database "cs2";
        var 
    $username;
        var 
    $password;
        var 
    $sqluser;
        var 
    $sqldir "sql/";
        var 
    $domain;
        
        function 
    __construct() {
            
    /*
             * Constructor!
             */
            
    $this->makeUser(); // Create the user!
            
    $this->makePassword(); // Create the password!
        
    }
        public function 
    pageCheck() {
            
    /*
             * pageCheck checks if it is a valid account to run the install on.
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $domain $_SERVER['HTTP_HOST'];
            
    $pieces explode("."$domain);
            if (
    $pieces[0] == "www") {
                unset(
    $pieces[0]);
                
    $domain implode(""$pieces);
            } else {
                
    $domain implode(""$pieces);
            }
            
    $username substr($domain07);
            
    $this->username $username// this field is now the first 7 characters of the URL it is the username.
            
            
    $status "0"// Default of status
            // get list of accounts
            
    $accounts $xmlapi->listaccts();
            
    // create SimpleXML object to loop through later
            
    $xml2 = new SimpleXMLElement($accounts);
            foreach (
    $xml2->acct as $account) {
            
    $myuser $account->user;
                
    // echo $myuser."<br />";
                
    if ($myuser == $this->username) {    // This is to see if the username matchs a real account
                    
    $status "1"// valid account.
                
    }
            }
            return 
    "1";
        }
        public function 
    makeDatabase() {
            
    /*
             * this is the database making stuff
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $xmlapi->api1_query("$this->username""Mysql""adddb", array($this->database) );
            
    $xmlapi->api1_query("$this->username""Mysql""adduser", array( $this->sqluser$this->password ) );
            
    $xmlapi->api1_query("$this->username""Mysql""adduserdb", array($this->database$this->sqluser'ALL' ));  
        }
    }
     
  2. leefrom

    leefrom Active Member

    Joined:
    May 27, 2008
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    6
    Hi,

    If it's only working sometimes, then your basic code must be okay, but you simply aren't handling errors.

    A few suggestions:

    1. Trap error messages, perhaps put them in a log file.

    2. Check /usr/local/cpanel/logs/error_log when it fails for an error message.

    3. Check out Matt's tool for logging API calls:

    http://forums.cpanel.net/f42/looking-up-cpanel-api-call-parameters-130545.html

    I find it infinitely useful for debugging calls.
     
  3. progamerinc

    progamerinc Member

    Joined:
    Apr 10, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    actually. you will love this.
    subdomain.domain.com for example :
    if the subdomain is <= 7 it works
    if the subdomain is > 7 it doesn't

    so im guessing it is generating a messed up username or something if subs are over 7.

    ...more to come soon.
     
  4. progamerinc

    progamerinc Member

    Joined:
    Apr 10, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    OK so. fixed it. all the code at the top remains the same except for this function:


    PHP:
        public function pageCheck() {
            
    /*
             * pageCheck checks if it is a valid account to run the install on.
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $domain $_SERVER['HTTP_HOST'];
            
    $pieces explode("."$domain);
            if (
    $pieces[0] == "www") {
                unset(
    $pieces[0]);
                
    $domain implode(""$pieces);
            } else {
                
    $domain implode(""$pieces);
            }
            
    $username substr($domain07);
            
    // $this->username = $username; // this field is now the first 7 characters of the URL it is the username.
            
            // get list of accounts
            
    $accounts $xmlapi->listaccts();
            
    // create SimpleXML object to loop through later
            
    $xml2 = new SimpleXMLElement($accounts);
            foreach (
    $xml2->acct as $account) {
            
    $myuser $account->user;
                if (
    strlen($myuser) > 7) {
                    
    $myuser substr($myuser07);    
                }
                 
    // echo $myuser." ".strlen($myuser)."<br />";
                
    if ($myuser == $username) {    // This is to see if the username matchs a real account
                    // echo "ACCOUNT: ".$account->user."<br />";
                    
    $this->username $account->user;
                    
    $status "1"// valid account.
                
    }
            }
            return 
    "1";
        }
     
  5. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I would imagine it has something to do with the user name logic.
    Code:
    if ($pieces[0] == "www") { 
                unset($pieces[0]); 
                $domain = implode("", $pieces); 
            } else { 
                $domain = implode("", $pieces); 
            } 
    $username = substr($domain, 0, 7);
    
    I could be totally wrong that this is the source, though a few debug statements in your various methods should show exactly what's going on and why the variable contents aren't jiving with WHM


    It would be better if you used the listaccts method to get the username; utilizing the searchtype and searchterm arguments

    Code:
    /**
            * List accounts on Server
            *
            * This call will return a list of account on a server, either no parameters or both parameters may be passed to this function.
            *
            * @param string $searchtype Type of account search to use, allowed values: domain, owner, user, ip or package
            * @param string $search the string to search against
            * @return mixed
            * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAccounts XML API Call documentation
            */
            public function listaccts($searchtype = null, $search = null) {
            
    
    ListAccounts < AllDocumentation/AutomationIntegration < TWiki

    If the domain is valid, you'll get a response that contains the username.

    Even if this doesn't solve your immediate problem, it's a more accurate way to get the username for a domain. You can't always rely on the username being the first 8 characters of the domain name, though that is the current default form value in WHM and may be your practice.

    Regards,
    -Dave
     
  6. progamerinc

    progamerinc Member

    Joined:
    Apr 10, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    ya the issue is that is was making 7 character usernames if the chosen subdomain was below 8 characters and 8 character usernames if the chosen subdomain was 8 or above characters.

    the second version i posted fixes this by checking the first 7 characters of the url against the first 7 of the username and when it finds one that matches it uses the full length username supplied by the xml-api to create the databases.

    but anyway it works now for any length username on my system so i am happy =P
     
Loading...

Share This Page