#!/usr/bin/php
# Scripts hook to create database and db virtuser; pair the two
<?php
//set error handling so any warnings are logged
ini_set('error_log','/usr/local/cpanel/logs/error_log');
ini_set('display_errors',0);
//include basic xmlapi client class
include('/home/cpanelcustom/xmlapi.php')
/**
* extend the basic xmlapi class
* add the method for getting args
*/
Class cpScriptsXmlApi extends XMLAPI
{
}
public $cliargs = array();
/**
* Simple method to store args into an array
*
*@params array $argv shell array to be parsed
*@return array
*/
public function argv2array ($argv) {
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
return $opts;
}
/**
* constructor
*
*@param array $scriptargs cli $argv that will be parsed
*@param string $host
*@param string $user
*@param string $password
*@return cpScriptsXmlApi
*/
public function __construct($scriptargs = array(), $host = null, $user = null, $password = null)
{
parent::__construct($host,$user,$password);
$this->cliargs = $this->argv2array($scriptargs);
return $this;
}
/**
* Create a database
*/
public function createUserDb(){}
/**
* Create a db virtuser
*/
public function createDbVirtuser(){}
/**
* Assign db privs
*/
public function assignUserPrivs(){}
/**
* Creating some different password
*/
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$password = createRandomPassword();
/**
* Create a database
*
*@param string $user cpanel user to create db as
*@param string $dbname name for database
*/
public function createUserDb($user,$dbname){
$args = array($dbname);
return $this->api1_query($user,'Mysql','adddb',$args);
}
/**
* Create a db virtuser
*
*@param string $user cpanel user to create virtuser as
*@param string $virtusername name for db virtuser
*@param string $password password for new db virtuser
*/
public function createDbVirtuser($user,$virtusername,$password){
$args = array($virtusername,$password);
return $this->api1_query($user,'Mysql','adduser',$args);
}
/**
* Assign user privs
*
*@param string $user cpanel user to work on behalf of
*@param string $dbname name of database
*@param string $virtusername receiver of privs
*@param array $privs array of privileges to assign.
*/
public function assignUserPrivs($user,$dbname,$virtusername,$privs = array()){
$privs = (empty($privs))? array('all'): $privs; //not the best, you can change the default if you wish
$priv_str = implode(',',$privs);
$args = array($dbname, $virtusername, $priv_str);
return $this->api1_query($user,'Mysql','adduserdb',$args);
}
//create xmlapi object and set it's params
$xmlapi = new cpScriptsXmlApi($argv,'127.0.0.1');
//root auth hash
$hash = file_get_contents('/root/.accesshash');
$xmlapi->set_user('root');
$xmlapi->set_hash($hash);
$xmlapi->set_port('2087');
$dbtoattach = ((int)$prefixing)? $xmlapi->cliargs['user'].'_'.$dbname : $dbname;
$usertoattach = ((int)$prefixing)? $xmlapi->cliargs['user'].'_'.$virtusername : $virusername;
$xmlapi->assignUserPrivs($xmlapi->cliargs['user'], $dbtoattach, $usertoattach, $privs);
$xmlapi->createUserDb($xmlapi->cliargs['user'], $dbname);
$xmlapi->createDbVirtuser($xmlapi->cliargs['user'],$virtusername, $xmlapi->cliargs['pass']); //setting passwd same is not wise, but is done per commission request
$xmlapi->assignUserPrivs($xmlapi->cliargs['user'], $dbprefix.'_'.$dbname, $dbprefix.'_'.$virtusername, $privs);