Create cpanel database through php script

Viewed 18364

Im trying to automate the installation of some custom software using cpanel/whm and postwwwacct php script.This requires files to be copied to the users public_html folder then edit the config and set eh file permissions. So far so good, no issues. When trying to create the database im running into some problems.

    $db_create= $opts['user']. '_lol';  
    $db_host="immersion-networks.com";
    include("xmlapi.php");   
    $xmlapi = new xmlapi($db_host);    
    $xmlapi->password_auth("".$opts['user']."","".$opts['pass']."");    
    $xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false  
    $xmlapi->set_output('array');//set this for browser output  
    //create database    
    $createdb = $xmlapi->api1_query($opts['user'], "Mysql", "adddb", array($db_create));   
    //create user 
    $usr = $xmlapi->api1_query($opts['user'], "Mysql", "adduser", array($db_create, $opts['pass']));   
     //add user 
    $addusr = $xmlapi->api1_query($opts['user'], "Mysql", "adduserdb", array($db_create,$db_create, 'all')); 

Rest of the code runs ok but the db isnt being created nor are the users. Any ideas?

6 Answers

I have done modification based on @kenvilar by putting prefix definition at fn: addUserToDb()

<?php

$database_name = "dbname"; //without prefix
$database_user = $database_name; //database name and database username are both similar, change the value if you want
$database_pass = "random_password";
$cpanel_username = "my_cpanel_username";
$cpanel_pass = "my_cpanel_password";
$cpanel_theme = "paper_lantern"; // change this to "x3" if you don't have paper_lantern yet

function createDb($cpanel_theme, $cPanelUser, $cPanelPass, $dbName)
{
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/addb.html?db=" . $dbName;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

function createUser($cpanel_theme, $cPanelUser, $cPanelPass, $userName, $userPass)
{
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/adduser.html?user=" . $userName . "&pass=" . $userPass;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

function addUserToDb($cpanel_theme, $cPanelUser, $cPanelPass, $userName, $dbName, $privileges){
    
    /* Redefine prefix for user and dbname */
    $prefix = substr($cPanelUser,0,8);
    
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/addusertodb.html?user=" . $prefix . "_" . 
            $userName . "&db=" . $prefix . "_" . $dbName . "&privileges=" . $privileges;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

//Create Db
createDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_name);

//Create User
createUser($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_pass);

//Add user to DB - ALL Privileges
addUserToDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_name, 'ALL PRIVILEGES');

//Add user to DB - SELECTED PRIVILEGES
//addUserToDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_name, 'DELETE,UPDATE,CREATE,ALTER');

?>

This one worked for me. I've made some updates from @brunofitas answer

<?php

$database_name = "dbname"; //without prefix
$database_user = $database_name; //database name and database username are both similar, change the value if you want
$database_pass = "random_password";
$cpanel_username = "my_cpanel_username";
$cpanel_pass = "my_cpanel_password";
$cpanel_theme = "paper_lantern"; // change this to "x3" if you don't have paper_lantern yet

function createDb($cpanel_theme, $cPanelUser, $cPanelPass, $dbName)
{
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/addb.html?db=" . $dbName;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

function createUser($cpanel_theme, $cPanelUser, $cPanelPass, $userName, $userPass)
{
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/adduser.html?user=" . $userName . "&pass=" . $userPass;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

function addUserToDb($cpanel_theme, $cPanelUser, $cPanelPass, $userName, $dbName, $privileges)
{
    $buildRequest = "/frontend/" . $cpanel_theme . "/sql/addusertodb.html?user=" . $cPanelUser . "_" . $userName . "&db=" . $cPanelUser . "_" . $dbName . "&privileges=" . $privileges;

    $openSocket = fsockopen('localhost', 2082);
    if (!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders = "GET " . $buildRequest . "\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while (!feof($openSocket)) {
        fgets($openSocket, 128);
    }
    fclose($openSocket);
}

//Create Db
createDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_name);

//Create User
createUser($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_pass);

//Add user to DB - ALL Privileges
addUserToDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_name, 'ALL PRIVILEGES');

//Add user to DB - SELECTED PRIVILEGES
//addUserToDb($cpanel_theme, $cpanel_username, $cpanel_pass, $database_user, $database_name, 'DELETE,UPDATE,CREATE,ALTER');

We can do this using cpanel API, as the above old codes are not working currently with new versions of cpanel. Although the question was asked earlier but i am pasting this for helping others.

include "cpaneluapi.class.php"; //include the class file from here https://github.com/N1ghteyes/cpanel-UAPI-php-class/blob/master/cpaneluapi.class.php
$uapi = new cpanelAPI('cpanelusername', 'cpanelapssword', 'cpanel.domain.com'); 
//instantiate the object

$database = 'dbname';
$databaseuser = 'dbuser';
$databasepass = 'databasepass';

/**
 * Mysql - Create a database and user, then assign the user to that database.
 * For a full list of functions available for the Mysql module, see: 
    https://documentation.cpanel.net/display/SDK/Mysql
 * Mysql requires cPanel 11.44 +
  */


  $uapi->uapi->Mysql->create_database(array('name' => $database)); //Create the 
  database
  $uapi->uapi->Mysql->create_user(array('name' => $databaseuser, 'password' => 
  $databasepass)); //create a user for the new database
  //After you create the user, you must use the set_privileges_on_database 
  //function call to grant access to the

  //add the user, set all privileges - add specific privileges by comma 
  //separation. e.g. 'DELETE,UPDATE,CREATE,ALTER' 
  $uapi->uapi->Mysql->set_privileges_on_database(array('user' => $databaseuser, 
  'database' => $database, 'privileges' => 'ALL'));
Related