Results 1 to 3 of 3

Thread: postwwwact script

  1. #1
    Registered Member
    Join Date
    May 2008
    Posts
    42

    Default postwwwact script

    Hello

    I am following this guide for the creation of a db (and its user) for cpanel accounts:

    Frequently Asked Scripts: "How do I automate XYZ after creating an account?" - cPanel Integration

    I am not a programmer im just trying to do this to make my life at work easier when it comes to create a new account, I havent tested it yet because i want to sort out the fact that it uses the same password for the db and the cpanel account, so ive inserted a little password generator script inside that file (the bold part), this is what i have:

    #!/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);
    What i want to know is if this line:

    $xmlapi->createDbVirtuser($xmlapi->cliargs['user'],$virtusername, $xmlapi->cliargs['pass']);
    If i put the variable $password (of the mentioned function) instead of ['pass'] it should work?

    Any help and advice is appreciated. Thank you

  2. #2
    Technical Product Specialist cPanelDavidG's Avatar
    Join Date
    Nov 2006
    Location
    Houston, TX
    Posts
    11,289
    cPanel Access Level

    Root Administrator

    Default Re: postwwwact script

    You would want to replace $xmlapi->cliargs['pass'] not just ['pass'] - the stuff between the brackets is a command line interfae (CLI) parameter. Since $password is a variable rather than a command line parameter, you would replace the entirety of that "variable."

    To recap, a CLI parameter is something like this:

    myscript.php user=bob pass=~sup3rsecur3!!

    Everything after the .php is a parameter. In this case, the parameters are named user and pass respectively.

  3. #3
    Registered Member
    Join Date
    May 2008
    Posts
    42

    Default Re: postwwwact script

    Im so sorry for my late reply. I will give it a try thank you.

Similar Threads

  1. shell script vs php script .. which is faster and liter?
    By niceboy in forum Workarounds and Optimization
    Replies: 3
    Last Post: 02-24-2013, 08:06 AM
  2. Problem with postwwwact not sending the email! Please help!
    By nettpakke in forum E-mail Discussions
    Replies: 20
    Last Post: 04-29-2011, 08:41 PM
  3. postwwwact set owner
    By jbnmedia in forum cPanel Developers
    Replies: 4
    Last Post: 06-02-2010, 07:57 AM
  4. Replies: 1
    Last Post: 03-02-2007, 07:52 PM
  5. Automating Account Creation: Shell Script? PHP Script?
    By RangerWest in forum General Discussion
    Replies: 2
    Last Post: 07-13-2003, 10:24 AM
bargain