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 DB with PHP

Discussion in 'cPanel Developers' started by jhyland87, May 18, 2009.

  1. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    So I looked around on the API section, didnt see anything that would show me how to create a DB using php, I googled it, and it said that it was disabled for security reasons.

    I am trying to create a script that will do the following when a user signs up..

    Create account (Got this developed),
    Create MySQL DB
    Create FTP account

    Can the last 2 be done via PHP? And this is all on the same server, which I have root access to, but id rather limit the permissions to the reseller limits, not abuse the root access...
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The last two can be done via API1/API2 as described at: http://forums.cpanel.net/showthread.php?t=112153

    You can use Matt's PHP 5 class to do this via PHP 5 if you don't want to directly interact with our APIs. You can find his PHP 5 class at: http://forums.cpanel.net/showthread.php?t=111897
     
  3. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    Right as you posted that, I made some code to do it :P Im just having issues adding users to the Db, but ill get it;-)
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The APIs are just like the x3 interface for cPanel. A common mistake is people add the user_ prefix when adding database users or databases. Don't do that, let the APIs handle that for you.

    However, you do need the user_ prefix when adding users to databases, just like the x3 interface for cPanel.
     
  5. DaveUsedToWorkHere

    DaveUsedToWorkHere Well-Known Member

    Joined:
    Dec 28, 2001
    Messages:
    689
    Likes Received:
    1
    Trophy Points:
    18
    You can use the addusertodb API call for this through the XML API. Here's how we use it in cPanel:


    <cpanel Mysql="adduserdb($db, $user, $ALTER $CREATEROUTINE $CREATETEMPORARYTABLES $CREATE $DELETE $DROP $SELECT $INSERT $UPDATE $REFERENCES $INDEX $LOCKTABLES $ALL)">
     
  6. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    To clarify, the parameters for adding privileges are not binary/switches. Instead, for example, if you want to assign all privileges, you would substitute $ALL with "ALL" (without quotes).

    Looking at the source of /usr/local/cpanel/base/frontend/x3/sql/userrights.html will help you find the values of the other parameters for permissions if you want to use those.
     
  7. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    I actually dont see his PHP class for making a db/user and assigning the DB to the user. The code I have is below.

    PHP:
    <?php



    ###############################################################

    # cPanel Database Creator 1.2

    ###############################################################

    # Visit http://www.zubrag.com/scripts/ for updates

    ############################################################### 



    // cPanel username (you use to login to cPanel)

    $cpanel_user "user";



    // cPanel password (you use to login to cPanel)

    $cpanel_password "password";



    // cPanel domain (example: mysite.com)

    $cpanel_host "";



    // cPanel theme/skin, usually "x"

    // Check http://www.zubrag.com/articles/determine-cpanel-skin.php

    // to know it for sure

    $cpanel_skin "x";



    // Script will add user to database if these values are not empty

    // User wil have ALL permissions

    $db_username '';

    $db_userpass '';



    // Update this only if you are experienced user or if script does not work

    // Path to cURL on your server. Usually /usr/bin/curl

    $curl_path "";



    //////////////////////////////////////

    /* Code below should not be changed */

    //////////////////////////////////////



    function execCommand($command) {

      global 
    $curl_path;



      if (!empty(
    $curl_path)) {

        return 
    exec("$curl_path '$command'");

      }

      else {

        return 
    file_get_contents($command);

      }

    }



    if(isset(
    $_GET['db']) && !empty($_GET['db'])) {

      
    // escape db name

      
    $db_name escapeshellarg($_GET['db']);



      
    // will return empty string on success, error message on error

      
    $result execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addb.html?db=$db_name");



      if(isset(
    $_GET['user']) && !empty($_GET['user'])) {

        
    $db_username $_GET['user'];

        
    $db_userpass $_GET['pass'];

      }



      if (!empty(
    $db_username)) {

        
    // create user

        
    $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adduser.html?user={$db_username}&pass={$db_userpass}");

        
    // assign user to database

        
    $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addusertodb.html?user={$cpanel_user}_{$db_username}&db={$cpanel_user}_{$db_name}&ALL=ALL");

      }



      
    // output result

      
    echo $result;

    }

    else {

      echo 
    "Usage: cpanel_create_db.php?db=databasename&user=username&pass=password";

    }



    ?>
    And I think thats kinda jimmy riggin it... Id rather do it the right way.
     
  8. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The PHP Class isn't for interfacing only with creating a database. It's for interfacing with all of our APIs. I recommend you review the sample code for using an API1 function so you understand how to use the class. Then you can use that class to call the aforementioned API1 function.
     
  9. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    I tried to tweak some of the php code to see if I could get it to make a DB, i failed, but ill try a little more later
     
  10. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    If you can post the line of code that calls the API1 function, I could perhaps assist you with determining why it is not functioning.
     
  11. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    Well, for starters, his cp_xmlapi_php is mostly for controlling WHM, meaning its not doing things in cPanel for users (create databases, create subdomains, etc), but looking at the code in it.. It seems to return similar results like this out of the objects
    PHP:
    return $this->xmlapi_query("setupreseller",array("user" => $user"makeowner" => $makeowner));
    But when I change the 'setupreseller' to 'addb' or 'adddb', and alter the array, it doesnt return anything other than an error, saying adddb and addb doesnt exist.
     
  12. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    That's an XML API query you're executing. That will not work with API1 and API2 via the XML API. Instead, please look at api1_example.php in the zip file Matt provided.

    For the api1_query in that example file you would replace Email with Mysql and change addpop with adduserdb and after that is an array of the parameters to the adduserdb in order.
     
  13. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    Awesome! This is what ive got so far
    PHP:
    include("xmlapi.php.inc");

    $ip "svr1.xxxxxx.com";
    $root_pass "xxxxxx";

    $account "xxxxxx";
    $db_name "xxxxxx";
    $db_user "xxxxxx";
    $db_user_pass "xxxxxx";

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("xxxxxx",$root_pass);

    $xmlapi->set_debug(1);
    print 
    $xmlapi->api1_query($account"Mysql""adddb", array($db_name));
    echo 
    "<hr>";
    print 
    $xmlapi->api1_query($account"Mysql""adduser", array($db_user$db_user_pass));
    echo 
    "<hr>";
    print 
    $xmlapi->api1_query($account"Mysql""adduserdb", array($db_name$db_user));
    But that last line wont add the user to the db, im guessing its because I need to specify the add,drop,insert, etc etc.. how do I specify it?

    UPDATE
    PHP:
    print $xmlapi->api1_query($account"Mysql""adduserdb", array($db_name$db_user'ALL'));
    seems to do the trick, does it matter that it doesnt show ALL options as selected when I go to edit the permissions via cpanel?

    UPDATE 2
    Got it..
    PHP:
    print $xmlapi->api1_query($account"Mysql""adduserdb", array($db_name$db_user'ALTER CREATEROUTINE CREATETEMPORARYTABLES CREATE DELETE DROP SELECT INSERT UPDATE REFERENCES INDEX LOCKTABLES ALL'));
    Any way to silence the output of the results? so I can just return if it was done or not.. I took the print out, but still shows it.. lol
     
    #13 jhyland87, May 20, 2009
    Last edited: May 20, 2009
  14. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    What would the API command be to grab the users bandwidth limits/usages, as well as the accounts disk space usage/limits.

    Im trying to make a site based around cpanel, without giving access :)
     
  15. DaveUsedToWorkHere

    DaveUsedToWorkHere Well-Known Member

    Joined:
    Dec 28, 2001
    Messages:
    689
    Likes Received:
    1
    Trophy Points:
    18

    The function is printing so you'll need to capture or redirect the output if you don't want it to show on STDOUT.

    Here's the PHP page on output streams: http://us3.php.net/wrappers.php
     
  16. DaveUsedToWorkHere

    DaveUsedToWorkHere Well-Known Member

    Joined:
    Dec 28, 2001
    Messages:
    689
    Likes Received:
    1
    Trophy Points:
    18
    Bandwidth usage is listed when calling limitbw:

    http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LimitBandwidth

    Disk usage is listed on listaccts:

    http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAccounts


    Those are WHM XML API calls though. If you're looking to get the same information in cPanel, you'll need to use the XML API in conjunction with the StatsBar function in cPanel:

    http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions

    Here's the API2 call for StatsBar:

    <?cp StatsBar::stat(%/%,count,max) display=diskusage|bandwidthusage,infinitylang=true ?>


    count = disk/bandwidth used
    max = maximum allowed disk / bandwidth
     
  17. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    Back to this.. Whats the command to change the password of the mysql user? Any idea?

    Do you have a list of the commands and the required arguments?
     
  18. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Just like the cPanel interface, just re-add the user but with a different password to change the password of the MySQL user.

    You can find many MySQL-related functions with their required arguments in this thread:

    http://forums.cpanel.net/f42/function-create-ftp-account-email-mysql-database-112153.html
     
  19. lvt

    lvt Well-Known Member

    Joined:
    May 23, 2009
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    6
    cPanel Access Level:
    Reseller Owner
    I wonder why my API call only works with 'ALL' as option, any other options will fail ?

    My code :

    PHP:
    <cpanelaction>
    <
    module>Mysql</module>
    <
    func>adduserdb</func>
    <
    apiversion>1</apiversion>
    <
    args>testdb</args>
    <
    args>user1</args>
    <
    args>SELECT UPDATE INSERT DELETE</args>
    </
    cpanelaction>
     
  20. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The adduserdb function requires the cPanel user prefix for both the username and database name (e.g. user_testdb, user_user1). That could explain why the permissions are not being applied as you desired.
     
Loading...

Share This Page