Search code examples
phpmysqlwordpresscpanelsoftaculous

How does Softaculous create a MySQL database, user, and user->db mapping in cPanel?


Currently, with PHP code, I'm creating MySQL databases in cPanel, then MySQL users, and then mapping them to the database, using these 4 PHP routines. The trouble is that every once in a blue moon, at a time when we think it's related to server load, this script says it's created the MySQL, user, and user->db mapping, but we can't see it in cPanel. When we go to MySQL command line, we see it there. And WordPress can't deploy to it -- says it can't reach the database. We have to go to command line MySQL to delete the database. When we manually run these routines under lower server load, the problem goes away. The problem is that I'm usually called back on the issue a day later than it actually occurred -- so I can't see what happened. And my logs just show me that WordPress couldn't deploy to the MySQL database. When I go looking, I don't see it in cPanel, but see it at command line in MySQL.

So, since these routines are problematic, I was wondering how Softaculous resolves this problem. Because even when my script fails, Softaculous still works just fine.

<?php

class cPanel {

public static function cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds = 0) {
    $sURL = ($sCpanelPort == 2083) ? 'https://' : 'http://';
    $sURL .= $sRootDomain . ':' . $sCpanelPort . '/xml-api/cpanel';
    $asHeader[0] = 'Authorization: Basic ' . base64_encode($sCpanelUser . ':' . $sCpanelPass) . "\r\n" . 
        "Content-Type: application/x-www-form-urlencoded\r\n" .
        "Content-Length: " . strlen($sPostData) . "\r\n" . "\r\n" . $sPostData;

    $hCurl = curl_init (); 
    curl_setopt($hCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
    curl_setopt($hCurl, CURLOPT_REFERER, 'https://www.google.com/?gws_rd=ssl#q=' . $sURL);
    curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($hCurl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($hCurl, CURLOPT_URL, $sURL);
    curl_setopt($hCurl, CURLOPT_HTTPHEADER, $asHeader);
    curl_setopt($hCurl, CURLOPT_POST, 1);
    if ($nTimeoutSeconds > 0) {
        @ curl_setopt($hCurl, CURLOPT_TIMEOUT, $nTimeoutSeconds); 
        @ curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
    }
    $s = curl_exec($hCurl);
    return $s;
}

public static function cp_createDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName) {
global $gsCpanel_Last_Result;

    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName;
    $nTimeoutSeconds = 60;
    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds);
    $gsCpanel_Last_Result = "$s";
    if (strpos($s, '<result>1</result>') > 0) {
        $sDBName = $sCpanelUser . '_' . $sUnPrefixedDBName;
        return $sDBName;
    } else {
        return FALSE;
    }
}

public static function cp_createDBUser($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBUser, $sDBPass) {
global $gsCpanel_Last_Result;

    $sTest = $sCpanelUser . '_' . $sUnPrefixedDBUser;
    if (strlen($sTest)>16) {
        $sUnPrefixedDBUser = substr($sUnPrefixedDBUser, 0, (strlen($sUnPrefixedDBUser) - (strlen($sTest) - 16)));
    }
    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBUser . '&arg-1='. $sDBPass;
    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
    $gsCpanel_Last_Result = "$s";
    if (strpos($s, '<result>1</result>') > 0) {
        $sDBUser = $sCpanelUser . '_' . $sUnPrefixedDBUser;
        return $sDBUser;
    } else {
        return FALSE;
    }
}

public static function cp_addUserToDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName, $sUnPrefixedDBUser) {
global $gsCpanel_Last_Result;

    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName . '&arg-1=' . $sUnPrefixedDBUser . '&arg-2=all';

    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
    $gsCpanel_Last_Result = "$s";
    return (strpos($s, '<result>1</result>') > 0);
}

} // end class

Solution

  • Softaculous has a glorious SDK that's free here:

    http://www.softaculous.com/docs/File:Softaculous_Development_Kit.zip

    Then, this code should help you install a blog using it. It's blazingly fast -- much faster than cPanel APIs.

    <?php        
    
        // SOURCE: https://gist.github.com/mickaelandrieu/7271939
        require_once('sdk.php');
    
        $new = new Softaculous_API();
        $new->login = 'https://user:[email protected]:2083/frontend/x3/softaculous/index.live.php';
    
        // Domain Name
        $data['softdomain'] = 'root-or-addon-domain.com'; // OPTIONAL - By Default the primary domain will be used
    
        // The directory is relative to your domain and should not exist. e.g. To install at http://mydomain/dir/ just type dir. To install only in http://mydomain/ leave this empty.
        $data['softdirectory'] = ''; // OPTIONAL - By default it will be installed in the /public_html folder
    
        // Admin Username
        $data['admin_username'] = 'admin';
    
        // Admin Pass
        $data['admin_pass'] = 'pass';
    
        // Admin Email
        $data['admin_email'] = '[email protected]';
    
        // Database
        $data['softdb'] = 'wp887'; // make something up that MySQL likes and won't cause a collision
    
        //Database User Name
        $data['dbusername'] = 'wp887'; // ditto
    
        // DB User Pass 
        $data['dbuserpass'] = 'wp887'; // ditto
    
        // Language
        $data['language'] = 'en';
    
        // Site Name
        $data['site_name'] = 'Wordpess wp887';
    
        // Site Description
        $data['site_desc'] = 'WordPress API Test';
    
        // Response
        $res = $new->install(26, $data); // Will install WordPress(26 is its script ID)
    
        // Unserialize
        $res = unserialize($res);
    
        // Done/Error
        if(!empty($res['done'])){
            echo 'Installed';
        }else{
            echo 'Installation Failed<br/>';
            if(!empty($res['error'])){
                print_r($res['error']);
            }
        }
    

    SOURCE: https://gist.github.com/mickaelandrieu/7271939