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.

Creating an Addon Domain with PHP functions

Discussion in 'cPanel Developers' started by jason_85, Feb 8, 2011.

  1. jason_85

    jason_85 Member

    Joined:
    Feb 8, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Hi Everyone,

    I'm completely new to this forum and cPanel. I've got a sandbox cPanel shared hosting account with Hostgator, and wanted to create a script that programatically creates new drupal sites. I've got everything set up, except for adding the domain to cPanel.

    I checked out the documentation at docs.cpanel.net, but could only find functions on modifying DNS records, not creating addon domains.

    I was hoping someone could tell me how I could, from a php script, create an addon domain on my account?
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi jason_85,

    You're probably looking for the API2 call AddonDomain::addaddondomain.

    If you haven't see it already, we have a PHP client class(download) for interfacing with our XML-API (which can perform API1 and API2 calls).

    Another way, if you've got privileged access to the filesystem, is you can create .php files within the theme directories that will be pre-parsed by cPanel for API tags (and then sent to the PHP engine). This is call cPanel PHP.

    I'm not sure how you are handling things like the databases, but if you aren't utilizing the cPanel API functions, you should. The entire cPanel interface (though not WHM) is completely controlled by API1 and API2 calls. That gives 3rd-party developers a lot of control and helps ensure compatibility (future-proof) for your scripts that execute within/against a cPanel system.

    Lastly, you might consider cPanel's software deployment system. We call the software packages that it uses cPAddons. The are benefits and drawbacks to this system, but I thought I'd at least draw you attention to it if you haven't seen it before. In the cPanel interface this system is labeled "Site Software".

    I'm sure there's a Drupal cPAddon out there that you can use, or you could roll-your-own. CAVEAT EMPTOR: If your script doesn't live with the cPanel interface, but instead is external of the cPanel process (say you're using the XML-API), then executing the API1 CpAddons::mainpg gets a little more complicated. I suggest reading this forum post http://forums.cpanel.net/f42/help-cant-make-postwwwacct-work-cpaddon-161586.html#post677297. Otherwise, if you're using cPanel tags in a script within a theme directory (this is where the cPanel UI pages live) then you shouldn't have any problems.

    Look around the forum for PHP XML-API and cPanel PHP. If you still have specific questions or are just out right confused, let me know and we'll try and clarify things!

    Best Regards,
    -DavidN
     
  3. jason_85

    jason_85 Member

    Joined:
    Feb 8, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Great, thanks a million David I'll dive into that stuff and get back to you if I have any problems.

    Regarding the database; I'm using Drupal's abstraction layer mostly (drupal has some pretty neat almost-out-of-the-box multisite features that I took advantage of), and I have some php scripts that login to the mySQL database and do some cron-related housekeeping as well, so no I haven't really used cPanel yet, but that's simply because I know nothing about it.
    I Better get reading!

    Jason
     
  4. jason_85

    jason_85 Member

    Joined:
    Feb 8, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    For anyone looking for a Drupal module that provides access to the cPanel API functions: cPanel API (currently only for Drupal 6.x)
     
  5. jason_85

    jason_85 Member

    Joined:
    Feb 8, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    I was quite disappointed with the functionality and cumbersome nature of the way the existing Drupal module worked, so I created my own.

    Here is an adhoc module that plugs the XML API 2 functionality to Drupal 6.x (I've greated a group - pending moderator approval - to discuss turning this into a module: http://groups.drupal.org/cpanel-xml-api-2-integration):

    PHP:
    <?php
    // $Id:

    /**
     * @file
     * Provide Cpanel API functions through the cPanel XML API 2
     */

    function cpanel_xmlapi_menu() {
      
    $items = array();
      
    $items['admin/settings/cpanel_xmlapi'] = array(
        
    'title' => 'cPanel XML-API Integration Settings',
        
    'description' => 'Configure your cPanel login credentials',
        
    'page callback' => 'drupal_get_form',
        
    'page arguments' => array('cpanel_xmlapi_admin'),
        
    'access arguments' => array('access administration pages'),
       );
      return 
    $items;
    }

    /**
     * Admin Form 
     **/
    function cpanel_xmlapi_admin() {
      
    $form['cpanel_xmlapi_host'] = array(
        
    '#type' => 'textfield',
        
    '#title' => t('Host'),
        
    '#default_value' => variable_get('cpanel_xmlapi_host'''),
        
    '#description' => t("This must be a valid domain, such as: example.com ('localost' is NOT valid)"),
      );
      
    $form['cpanel_xmlapi_port'] = array(
        
    '#type' => 'textfield',
        
    '#title' => t('Port'),
        
    '#default_value' => variable_get('cpanel_xmlapi_port''2082'),
        
    '#description' => t("cPanel host port"),
        
    '#size' => 4,
      );
        
    $form['cpanel_xmlapi_user'] = array(
        
    '#type' => 'textfield',
        
    '#title' => t('Username'),
        
    '#default_value' => variable_get('cpanel_xmlapi_user'''),
        
    '#description' => t("cPanel Username"),
        
    '#size' => 20,
      );
      
    $form['cpanel_xmlapi_pass'] = array(
        
    '#type' => 'password',
        
    '#title' => t('Password'),
        
    '#default_value' => variable_get('cpanel_xmlapi_pass'''),
        
    '#description' => t("cPanel Password"),
        
    '#size' => 20,
      );
      return 
    system_settings_form($form);
    }

    /**
     * Submit query to cPanel, return response
     */
    function cpanel_xmlapi_request($module$function, array $parameters = array(), array $credentials = array()) {
      
      
    //set login info for any parameters that weren't defined in the function input. If no query was given AND they haven't been set in the settings, return an obviously default result that the user can identify in the error message.

      
    $host $credentials['host'] ? $credentials['host'] : variable_get('cpanel_xmlapi_host''example.com');
      
    $port $credentials['port'] ? $credentials['port'] : variable_get('cpanel_xmlapi_port''1234');
      
    $user $credentials['user'] ? $credentials['user'] : variable_get('cpanel_xmlapi_user''username');
      
    $pass $credentials['pass'] ? $credentials['pass'] : variable_get('cpanel_xmlapi_pass''pass');

      
    //no user option for this because I don't know if other versions take parameters differently
      
    $cpanel_xmlapi_version 2;

      
    //non-parameters to prepend to the http query:
      
    $pre_query = array(
        
    'user' => $user,
        
    'cpanel_xmlapi_module' => $module,
        
    'cpanel_xmlapi_func' => $function,
        
    'cpanel_xmlapi_version' => $cpanel_xmlapi_version,
      );

      
    $query_concatenated array_merge($pre_query,$parameters);

      
    $url url('http://www.' $host':' $port .'/xml-api/cpanel', array('query' => $query_concatenated));
      
    $headers['Authorization'] = 'Basic ' base64_encode($user ':' $pass);

      
    $result drupal_http_request($url$headers);

      if (
    $result->code != '200') {
        
    watchdog('cpanel xml api''The following error occurred: '$result->code '. Error occured while sending the following HTTP request: ' $url);
        return 
    FALSE;
      }
      
      return 
    $result;
    }
    ?>
    Here is a copy of the module (it adds a menu where you can input the domain and login credentials so you don't have to provide the function with them at every function call: http://www.warmdebate.com/cpanel_xmlapi.zip
     
    #5 jason_85, Feb 9, 2011
    Last edited: Feb 10, 2011
  6. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi jason_85,

    Glad I could help. Your proposed module looks good. I wanted to point out a few things I noticed though:

    1) Where you collect the "pre-query" URL parameters, the array key "user" should be "cpanel_xmlapi_user". Technically, "user" will work...under most cases. If the function that you're calling needs a parameter called "user" the two will collide. (an example is API2 FTP::addftp)

    Also, it's "cpanel_xmlapi_apiversion" not "cpanel_xmlapi_version". If you only make API2 calls, it's like that the incorrect parameter key will work due to some internal defaults.

    The corrected code:
    PHP:
    $pre_query = array(
        
    'cpanel_xmlapi_user' => $user,
        
    'cpanel_xmlapi_module' => $module,
        
    'cpanel_xmlapi_func' => $function,
        
    'cpanel_xmlapi_apiversion' => $cpanel_xmlapi_version,
      );
    Calling API1 and API2 functions via XML-API interface is documented here

    2) The other thing is your design around the version number. cPanel's API1 and API2 are two different APIs. API2 doesn't obsolete API1. There are many overlapping module names and functions. If there is an overlap, we recommend using the API2. If you don't intend to support API1 calls, theb your design will work fine as is.

    If you alter it to work with both cPanel APIs, then you'll need to add some logic concerning the function's arguments in the URL string. API1 takes ordered parameters, as opposed to API2 which takes key/value pairs....basically an ordinal array vs. an associative array. This is the logic we use in our XML-API PHP class. We actually have the two API types broken into their own methods:
    PHP:
    public function api1_query($user$module$function$args = array() ) {
            if ( !isset(
    $module) || !isset($function) || !isset($user) ) {
                
    error_log("api1_query requires that a module and function are passed to it");
                return 
    false;
            }

            if (!
    is_array($args)) {
                
    error_log('api1_query requires that it is passed an array as the 4th parameter');
                return 
    false;
            }

            
    $module_type 'cpanel_xmlapi_module';
            
    $func_type 'cpanel_xmlapi_func';
            
    $api_type 'cpanel_xmlapi_apiversion';
            
    $cpuser 'cpanel_xmlapi_user';

            if ( 
    $this->get_output() == 'json' ) {
                
    $module_type 'cpanel_jsonapi_module';
                
    $func_type 'cpanel_jsonapi_func';
                
    $api_type 'cpanel_jsonapi_apiversion';
                
    $cpuser 'cpanel_jsonapi_user';
            }

            
    $call = array(
            
    $cpuser => $user,
            
    $module_type => $module,
            
    $func_type => $function,
            
    $api_type => '1'
            
    );
            for (
    $int 0$int count($args);  $int++) {
                
    $call['arg-' $int] = $args[$int];
            }
            return 
    $this->xmlapi_query('cpanel'$call);
        }
    Again, you may want to look at Calling API1 and API2 functions via XML-API for the details.

    3) And lastly, I just wanted to be clear that we're using the XML-API interface to perform cPanel API functionality. The XML-API is it's own API; distinct from cPanel's API1 and API2. It has its own version numbers (that are actually incrementing due to revision in the API as opposed to the cPanel's where the number indicates a separate body of work). I mainly wanted to mention this because it is not obvious, not because you're doing something wrong ;) It's not always easy to decipher the model of our interfaces. And that's why I'm here...to help clarify.

    Keep up the good work Jason!

    Best Regards,
    -DavidN
     
Loading...

Share This Page