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.

Create Mysql Database postwwwacct

Discussion in 'cPanel Developers' started by act2281, Aug 21, 2014.

  1. act2281

    act2281 Registered

    Joined:
    Aug 21, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I am trying to use this script to create a mysqldatabase, but for some reason the database is not appearing in the useraccount matching the account name.

    I did look at xmlapi but every version i found of that did not work and had errors.

    Here is my code
    PHP:
    [PHP]#!/usr/bin/php-cli

    <?

     #!/usr/bin/php
    # Scripts hook to create database and db virtuser; pair the two

    //set error handling so any warnings are logged
    /**
    * extend the basic xmlapi class
    * add the method for getting args

    */
    $dbusername = $virtualusername;
    //dbname from above

    echo 'the username is'.$dbusername.' the pass is'.$password;
    $drupalUsername = 'admin';
    $drupalPassword = 'xxxxxxxx;
    $dbIP = "localhost";
    $dbport = "3306";

    // ----------------------------------------

    // Required to fix print
    echo '<br />'; 

    // Setup opts array
    $opts = array();
    $argv0 = array_shift($argv);
    while(count($argv)) {
        $key = array_shift($argv);
        $value = array_shift($argv);
        $opts[$key] = $value;
    }

    echo '<pre>';
    print_r ($opts);
    echo '</pre>';
    if ($opts['plan'] == 'drupal'){
        // Setup user
        
        //$username = $opts["user"];
        //$r_password = $opts["pass"];
        
        $username = "root";
        $r_password = xxxxxx";
        echo "Username ".$username."the password is ".$r_password.'<br/>';
        
        $dbname = $opts['user']."_db";
        $dbusername = $opts['user'];
        $pass = createPassword(8);

        // Remove folder to prevent 
        shell_exec('rm -r /home/' . $opts['user'] . '/public_html');

        // Execute the drush make script
        shell_exec('drush make /drupal.make /home/' . $opts['user'] . '/public_html');
        
        // Create the database
        $con = mysqli_connect($dbIP,$username,$r_password)or die ('error'.mysqli_error($con));
        // Check connection
        if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $sql = "CREATE DATABASE ".$dbname;
        mysqli_query($con,$sql)or die('could not create'.mysqli_error($con));
        mysqli_query($con,"CREATE USER ".$dbusername."@'' IDENTIFIED BY '".$pass."'") or die('failed user'.mysqli_error($con));
        mysqli_query($con,"GRANT ALL ON ".$dbname.".* to ".$dbusername."@'localhost'") or die('no permissions'.mysqli_error($con));    
        mysqli_query($con,"GRANT DROP ON ".$dbname.".* to ".$dbusername."@'localhost'") or die('could not grand drop'.mysqli_error($con));      
        // Change our working directory
        
        chdir('/home/'.$opts['user'].'/public_html');
        
        // Execute our drush install
         //shell_exec('drush site-install --db-url=mysqli://'.$dbusername.':'.$pass.'@localhost:3306/'.$dbname.' --account-name='.$drupalUsername.' --account-pass='.$drupalPassword.' -y');
         shell_exec('drush site-install --db-url=mysqli://'.$dbusername.':'.$pass.'@localhost:3306/'.$dbname.' --account-name='.$drupalUsername.' --account-pass='.$drupalPassword.' -y');

        // Update permissions
        shell_exec('chown -R '.$opts['user'].' *');
        shell_exec('chgrp -R '.$opts['user'].' *');
        
        include("/drush_enable.php");

        // Display our info to the user
        echo 'Drupal Install...Done<br />';
        echo 'mysqli Pass:'.$pass.'<br />';
    }

    function createPassword($length) {
        $chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $i = 0;
        $password = "";
        while ($i <= $length) {
            $password .= $chars{mt_rand(0,strlen($chars))};
            $i++;
        }
        return $password;
    }

    ?>
    [/PHP]
     
Loading...

Share This Page