Guest User

Untitled

a guest
May 30th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 85.82 KB | None | 0 0
  1. <?php
  2. /**
  3. * cPanel XMLAPI Client Class
  4. *
  5. * This class allows for easy interaction with cPanel's XML-API allow functions within the XML-API to be called
  6. * by calling funcions within this class
  7. *
  8. * LICENSE:
  9. *
  10. * Copyright (c) 2012, cPanel, Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification, are permitted provided
  14. * that the following conditions are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
  17. *   following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  19. *   following disclaimer in the documentation and/or other materials provided with the distribution.
  20. * * Neither the name of the cPanel, Inc. nor the names of its contributors may be used to endorse or promote
  21. *   products derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  25. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  26. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * Version: 1.0.13
  33. * Last updated: 19 November 2012
  34. *
  35. * Changes
  36. *
  37. * 1.0.13:
  38. * Tidy
  39. *
  40. * 1.0.12:
  41. * github#2 - [Bugfix]: typo related to environment variable XMLAPI_USE_SSL
  42. *
  43. * 1.0.11:
  44. * [Feature]: Remove value requirement for park()'s 'topdomain' argument
  45. *  (Case 51116)
  46. *
  47. * 1.0.10:
  48. * github#1 - [Bugfix]: setresellerpackagelimits() does not properly prepare
  49. *  input arguments for query (Case 51076)
  50. *
  51. * 1.0.9:
  52. * added input argument to servicestatus method which allows single service
  53. *  filtering (Case 50804)
  54. *
  55. * 1.0.8:
  56. * correct unpark bug as reported by Randall Kent
  57. *
  58. * 1.0.7:
  59. * Corrected typo for setrellerlimits where xml_query incorrectly called xml-api's setresellerips
  60. *
  61. * 1.0.6:
  62. * Changed 'user' URL parameter for API1/2 calls to 'cpanel_xmlapi_user'/'cpanel_jsonapi_user' to resolve conflicts with API2 functions that use 'user' as a parameter
  63. * Relocated exmaple script to Example subdirectory
  64. * Modified example scripts to take remote server IP and root password from environment variables REMOTE_HOST and REMOTE_PASSWORD, respectively
  65. * Created subdirectory Tests for PHPUnit tests
  66. * Add PHPUnit test BasicParseTest.php
  67. *
  68. * 1.0.5:
  69. * fix bug where api1_query and api2_query would not return JSON data
  70. *
  71. * 1.0.4:
  72. * set_port will now convert non-int values to ints
  73. *
  74. * 1.0.3:
  75. * Fixed issue with set_auth_type using incorrect logic for determining acceptable auth types
  76. * Suppress non-UTF8 encoding when using curl
  77. *
  78. * 1.0.2:
  79. * Increased curl buffer size to 128kb from 16kb
  80. * Fix double encoding issue in terminateresellers()
  81. *
  82. * 1.0.1:
  83. * Fixed use of wrong variable name in curl error checking
  84. * adjust park() to use api2 rather than API1
  85. *
  86. * 1.0
  87. * Added in 11.25 functions
  88. * Changed the constructor to allow for either the "DEFINE" config setting method or using parameters
  89. * Removed used of the gui setting
  90. * Added fopen support
  91. * Added auto detection for fopen or curl (uses curl by default)
  92. * Added ability to return in multiple formats: associative array, simplexml, xml, json
  93. * Added PHP Documentor documentation for all necessary functions
  94. * Changed submission from GET to POST
  95. *
  96. *
  97. * @copyright 2012 cPanel, Inc
  98. * @license http://sdk.cpanel.net/license/bsd.html
  99. * @version 1.0.13
  100. * @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
  101. * @since File available since release 0.1
  102. **/
  103.  
  104. /**
  105. * The base XML-API class
  106. *
  107. * The XML-API class allows for easy execution of cPanel XML-API calls.  The goal of this project is to create
  108. * an open source library that can be used for multiple types of applications.  This class relies on PHP5 compiled
  109. * with both curl and simplexml support.
  110. *
  111. * Making Calls with this class are done in the following steps:
  112. *
  113. * 1.) Instaniating the class:
  114. * $xmlapi = new xmlapi($host);
  115. *
  116. * 2.) Setting access credentials within the class via either set_password or set_hash:
  117. * $xmlapi->set_hash("username", $accessHash);
  118. * $xmlapi->set_password("username", "password");
  119. *
  120. * 3.) Execute a function
  121. * $xmlapi->listaccts();
  122. *
  123. * @category Cpanel
  124. * @package xmlapi
  125. * @copyright 2012 cPanel, Inc.
  126. * @license http://sdk.cpanel.net/license/bsd.html
  127. * @version Release: 1.0.13
  128. * @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
  129. * @since Class available since release 0.1
  130. **/
  131.  
  132. class xmlapi
  133. {
  134.     // should debugging statements be printed?
  135.     private $debug          = false;
  136.  
  137.     // The host to connect to
  138.     private $host               =   '127.0.0.1';
  139.  
  140.     // the port to connect to
  141.     private $port               =   '2087';
  142.  
  143.     // should be the literal strings http or https
  144.     private $protocol       =   'https';
  145.  
  146.     // output that should be given by the xml-api
  147.     private $output     =   'simplexml';
  148.  
  149.     // literal strings hash or password
  150.     private $auth_type  = null;
  151.  
  152.     //  the actual password or hash
  153.     private $auth           = null;
  154.  
  155.     // username to authenticate as
  156.     private $user               = null;
  157.  
  158.     // The HTTP Client to use
  159.  
  160.     private $http_client        = 'curl';
  161.  
  162.     /**
  163.     * Instantiate the XML-API Object
  164.     * All parameters to this function are optional and can be set via the accessor functions or constants
  165.     * This defaults to password auth, however set_hash can be used to use hash authentication
  166.     *
  167.     * @param string $host The host to perform queries on
  168.     * @param string $user The username to authenticate as
  169.     * @param string $password The password to authenticate with
  170.     * @return Xml_Api object
  171.     */
  172.     public function __construct($host = null, $user = null, $password = null )
  173.     {
  174.         // Check if debugging must be enabled
  175.         if ( (defined('XMLAPI_DEBUG')) && (XMLAPI_DEBUG == '1') ) {
  176.              $this->debug = true;
  177.         }
  178.  
  179.         // Check if raw xml output must be enabled
  180.         if ( (defined('XMLAPI_RAW_XML')) && (XMLAPI_RAW_XML == '1') ) {
  181.              $this->raw_xml = true;
  182.         }
  183.  
  184.         /**
  185.         * Authentication
  186.         * This can either be passed at this point or by using the set_hash or set_password functions
  187.         **/
  188.  
  189.         if ( ( defined('XMLAPI_USER') ) && ( strlen(XMLAPI_USER) > 0 ) ) {
  190.             $this->user = XMLAPI_USER;
  191.  
  192.             // set the authtype to pass and place the password in $this->pass
  193.             if ( ( defined('XMLAPI_PASS') ) && ( strlen(XMLAPI_PASS) > 0 ) ) {
  194.                 $this->auth_type = 'pass';
  195.                 $this->auth = XMLAPI_PASS;
  196.             }
  197.  
  198.             // set the authtype to hash and place the hash in $this->auth
  199.             if ( ( defined('XMLAPI_HASH') ) && ( strlen(XMLAPI_HASH) > 0 ) ) {
  200.                 $this->auth_type = 'hash';
  201.                 $this->auth = preg_replace("/(\n|\r|\s)/", '', XMLAPI_HASH);
  202.             }
  203.  
  204.             // Throw warning if XMLAPI_HASH and XMLAPI_PASS are defined
  205.             if ( ( ( defined('XMLAPI_HASH') ) && ( strlen(XMLAPI_HASH) > 0 ) )
  206.                 && ( ( defined('XMLAPI_PASS') ) && ( strlen(XMLAPI_PASS) > 0 ) ) ) {
  207.                 error_log('warning: both XMLAPI_HASH and XMLAPI_PASS are defined, defaulting to XMLAPI_HASH');
  208.             }
  209.  
  210.  
  211.             // Throw a warning if XMLAPI_HASH and XMLAPI_PASS are undefined and XMLAPI_USER is defined
  212.             if ( !(defined('XMLAPI_HASH') ) || !defined('XMLAPI_PASS') ) {
  213.                 error_log('warning: XMLAPI_USER set but neither XMLAPI_HASH or XMLAPI_PASS have not been defined');
  214.             }
  215.  
  216.         }
  217.  
  218.         if ( ( $user != null ) && ( strlen( $user ) < 9 ) ) {
  219.             $this->user = $user;
  220.         }
  221.  
  222.         if ($password != null) {
  223.             $this->set_password($password);
  224.         }
  225.  
  226.         /**
  227.         * Connection
  228.         *
  229.         * $host/XMLAPI_HOST should always be equal to either the IP of the server or it's hostname
  230.         */
  231.  
  232.         // Set the host, error if not defined
  233.         if ($host == null) {
  234.             if ( (defined('XMLAPI_HOST')) && (strlen(XMLAPI_HOST) > 0) ) {
  235.                 $this->host = XMLAPI_HOST;
  236.             } else {
  237.                 throw new Exception("No host defined");
  238.             }
  239.         } else {
  240.             $this->host = $host;
  241.         }
  242.  
  243.         // disabling SSL is probably a bad idea.. just saying.
  244.         if ( defined('XMLAPI_USE_SSL' ) && (XMLAPI_USE_SSL == '0' ) ) {
  245.             $this->protocol = "http";
  246.         }
  247.  
  248.         // Detemine what the default http client should be.
  249.         if ( function_exists('curl_setopt') ) {
  250.             $this->http_client = "curl";
  251.         } elseif ( ini_get('allow_url_fopen') ) {
  252.             $this->http_client = "fopen";
  253.         } else {
  254.             throw new Exception('allow_url_fopen and curl are neither available in this PHP configuration');
  255.         }
  256.  
  257.     }
  258.  
  259.     /**
  260.     * Accessor Functions
  261.     **/
  262.     /**
  263.     * Return whether the debug option is set within the object
  264.     *
  265.     * @return boolean
  266.     * @see set_debug()
  267.     */
  268.     public function get_debug()
  269.     {
  270.         return $this->debug;
  271.     }
  272.  
  273.     /**
  274.     * Turn on debug mode
  275.     *
  276.     * Enabling this option will cause this script to print debug information such as
  277.     * the queries made, the response XML/JSON and other such pertinent information.
  278.     * Calling this function without any parameters will enable debug mode.
  279.     *
  280.     * @param bool $debug turn on or off debug mode
  281.     * @see get_debug()
  282.     */
  283.     public function set_debug( $debug = 1 )
  284.     {
  285.         $this->debug = $debug;
  286.     }
  287.  
  288.     /**
  289.     * Get the host being connected to
  290.     *
  291.     * This function will return the host being connected to
  292.     * @return string host
  293.     * @see set_host()
  294.     */
  295.     public function get_host()
  296.     {
  297.         return $this->host;
  298.     }
  299.  
  300.     /**
  301.     * Set the host to query
  302.     *
  303.     * Setting this will set the host to be queried
  304.     * @param string $host The host to query
  305.     * @see get_host()
  306.     */
  307.     public function set_host( $host )
  308.     {
  309.         $this->host = $host;
  310.     }
  311.  
  312.     /**
  313.     * Get the port to connect to
  314.     *
  315.     * This will return which port the class is connecting to
  316.     * @return int $port
  317.     * @see set_port()
  318.     */
  319.     public function get_port()
  320.     {
  321.         return $this->port;
  322.     }
  323.  
  324.     /**
  325.     * Set the port to connect to
  326.     *
  327.     * This will allow a user to define which port needs to be connected to.
  328.     * The default port set within the class is 2087 (WHM-SSL) however other ports are optional
  329.     * this function will automatically set the protocol to http if the port is equal to:
  330.     *    - 2082
  331.     *    - 2086
  332.     *    - 2095
  333.     *    - 80
  334.     * @param int $port the port to connect to
  335.     * @see set_protocol()
  336.     * @see get_port()
  337.     */
  338.     public function set_port( $port )
  339.     {
  340.         if ( !is_int( $port ) ) {
  341.             $port = intval($port);
  342.         }
  343.  
  344.         if ($port < 1 || $port > 65535) {
  345.             throw new Exception('non integer or negative integer passed to set_port');
  346.         }
  347.  
  348.         // Account for ports that are non-ssl
  349.         if ($port == '2086' || $port == '2082' || $port == '80' || $port == '2095') {
  350.             $this->set_protocol('http');
  351.         }
  352.  
  353.         $this->port = $port;
  354.     }
  355.  
  356.     /**
  357.     * Return the protocol being used to query
  358.     *
  359.     * This will return the protocol being connected to
  360.     * @return string
  361.     * @see set_protocol()
  362.     */
  363.     public function get_protocol()
  364.     {
  365.         return $this->protocol;
  366.     }
  367.  
  368.     /**
  369.     * Set the protocol to use to query
  370.     *
  371.     * This will allow you to set the protocol to query cpsrvd with.  The only to acceptable values
  372.     * to be passed to this function are 'http' or 'https'.  Anything else will cause the class to throw
  373.     * an Exception.
  374.     * @param string $proto the protocol to use to connect to cpsrvd
  375.     * @see get_protocol()
  376.     */
  377.     public function set_protocol( $proto )
  378.     {
  379.         if ($proto != 'https' && $proto != 'http') {
  380.             throw new Exception('https and http are the only protocols that can be passed to set_protocol');
  381.         }
  382.         $this->protocol = $proto;
  383.     }
  384.  
  385.     /**
  386.     * Return what format calls with be returned in
  387.     *
  388.     * This function will return the currently set output format
  389.     * @see set_output()
  390.     * @return string
  391.     */
  392.     public function get_output()
  393.     {
  394.         return $this->output;
  395.     }
  396.  
  397.     /**
  398.     * Set the output format for call functions
  399.     *
  400.     * This class is capable of returning data in numerous formats including:
  401.     *   - json
  402.     *   - xml
  403.     *   - {@link http://php.net/simplexml SimpleXML}
  404.     *   - {@link http://us.php.net/manual/en/language.types.array.php Associative Arrays}
  405.     *
  406.     * These can be set by passing this class any of the following values:
  407.     *   - json - return JSON string
  408.     *   - xml - return XML string
  409.     *   - simplexml - return SimpleXML object
  410.     *   - array - Return an associative array
  411.     *
  412.     * Passing any value other than these to this class will cause an Exception to be thrown.
  413.     * @param string $output the output type to be set
  414.     * @see get_output()
  415.     */
  416.     public function set_output( $output )
  417.     {
  418.         if ($output != 'json' && $output != 'xml' && $output != 'array' && $output != 'simplexml') {
  419.             throw new Exception('json, xml, array and simplexml are the only allowed values for set_output');
  420.         }
  421.         $this->output = $output;
  422.     }
  423.  
  424.     /**
  425.     * Return the auth_type being used
  426.     *
  427.     * This function will return a string containing the auth type in use
  428.     * @return string auth type
  429.     * @see set_auth_type()
  430.     */
  431.     public function get_auth_type()
  432.     {
  433.         return $this->auth_type;
  434.     }
  435.  
  436.     /**
  437.     * Set the auth type
  438.     *
  439.     * This class is capable of authenticating with both hash auth and password auth
  440.     * This function will allow you to manually set which auth_type you are using.
  441.     *
  442.     * the only accepted parameters for this function are "hash" and "pass" anything else will cuase
  443.     * an exception to be thrown
  444.     *
  445.     * @see set_password()
  446.     * @see set_hash()
  447.     * @see get_auth_type()
  448.     * @param string auth_type the auth type to be set
  449.     */
  450.     public function set_auth_type( $auth_type )
  451.     {
  452.         if ($auth_type != 'hash' && $auth_type != 'pass') {
  453.             throw new Exception('the only two allowable auth types arehash and path');
  454.         }
  455.         $this->auth_type = $auth_type;
  456.     }
  457.  
  458.     /**
  459.     * Set the password to be autenticated with
  460.     *
  461.     * This will set the password to be authenticated with, the auth_type will be automatically adjusted
  462.     * when this function is used
  463.     *
  464.     * @param string $pass the password to authenticate with
  465.     * @see set_hash()
  466.     * @see set_auth_type()
  467.     * @see set_user()
  468.     */
  469.     public function set_password( $pass )
  470.     {
  471.         $this->auth_type = 'pass';
  472.         $this->auth = $pass;
  473.     }
  474.  
  475.     /**
  476.     * Set the hash to authenticate with
  477.     *
  478.     * This will set the hash to authenticate with, the auth_type will automatically be set when this function
  479.     * is used.  This function will automatically strip the newlines from the hash.
  480.     * @param string $hash the hash to autenticate with
  481.     * @see set_password()
  482.     * @see set_auth_type()
  483.     * @see set_user()
  484.     */
  485.     public function set_hash( $hash )
  486.     {
  487.         $this->auth_type = 'hash';
  488.         $this->auth = preg_replace("/(\n|\r|\s)/", '', $hash);
  489.     }
  490.  
  491.     /**
  492.     * Return the user being used for authtication
  493.     *
  494.     * This will return the username being authenticated against.
  495.     *
  496.     * @return string
  497.     */
  498.     public function get_user()
  499.     {
  500.         return $this->user;
  501.     }
  502.  
  503.     /**
  504.     * Set the user to authenticate against
  505.     *
  506.     * This will set the user being authenticated against.
  507.     * @param string $user username
  508.     * @see set_password()
  509.     * @see set_hash()
  510.     * @see get_user()
  511.     */
  512.     public function set_user( $user )
  513.     {
  514.         $this->user = $user;
  515.     }
  516.  
  517.     /**
  518.     * Set the user and hash to be used for authentication
  519.     *
  520.     * This function will allow one to set the user AND hash to be authenticated with
  521.     *
  522.     * @param string $user username
  523.     * @param string $hash WHM Access Hash
  524.     * @see set_hash()
  525.     * @see set_user()
  526.     */
  527.     public function hash_auth( $user, $hash )
  528.     {
  529.         $this->set_hash( $hash );
  530.         $this->set_user( $user );
  531.     }
  532.  
  533.     /**
  534.     * Set the user and password to be used for authentication
  535.     *
  536.     * This function will allow one to set the user AND password to be authenticated with
  537.     * @param string $user username
  538.     * @param string $pass password
  539.     * @see set_pass()
  540.     * @see set_user()
  541.     */
  542.     public function password_auth( $user, $pass )
  543.     {
  544.         $this->set_password( $pass );
  545.         $this->set_user( $user );
  546.     }
  547.  
  548.     /**
  549.     * Return XML format
  550.     *
  551.     * this function will cause call functions to return XML format, this is the same as doing:
  552.     *   set_output('xml')
  553.     *
  554.     * @see set_output()
  555.     */
  556.     public function return_xml()
  557.     {
  558.         $this->set_output('xml');
  559.     }
  560.  
  561.     /**
  562.     * Return simplexml format
  563.     *
  564.     * this function will cause all call functions to return simplexml format, this is the same as doing:
  565.     *   set_output('simplexml')
  566.     *
  567.     * @see set_output()
  568.     */
  569.     public function return_object()
  570.     {
  571.         $this->set_output('simplexml');
  572.     }
  573.  
  574.     /**
  575.     * Set the HTTP client to use
  576.     *
  577.     * This class is capable of two types of HTTP Clients:
  578.     *   - curl
  579.     *   - fopen
  580.     *
  581.     * When using allow url fopen the class will use get_file_contents to perform the query
  582.     * The only two acceptable parameters for this function are 'curl' and 'fopen'.
  583.     * This will default to fopen, however if allow_url_fopen is disabled inside of php.ini
  584.     * it will switch to curl
  585.     *
  586.      * @param string client The http client to use
  587.     * @see get_http_client()
  588.     */
  589.  
  590.     public function set_http_client( $client )
  591.     {
  592.         if ( ( $client != 'curl' ) && ( $client != 'fopen' ) ) {
  593.             throw new Exception('only curl and fopen and allowed http clients');
  594.         }
  595.         $this->http_client = $client;
  596.     }
  597.  
  598.     /**
  599.     * Get the HTTP Client in use
  600.     *
  601.     * This will return a string containing the HTTP client currently in use
  602.     *
  603.     * @see set_http_client()
  604.     * @return string
  605.     */
  606.     public function get_http_client()
  607.     {
  608.         return $this->http_client;
  609.     }
  610.  
  611.      /*
  612.     *   Query Functions
  613.     *   --
  614.     *   This is where the actual calling of the XML-API, building API1 & API2 calls happens
  615.     */
  616.  
  617.     /**
  618.     * Perform an XML-API Query
  619.     *
  620.     * This function will perform an XML-API Query and return the specified output format of the call being made
  621.     *
  622.     * @param string $function The XML-API call to execute
  623.     * @param array $vars An associative array of the parameters to be passed to the XML-API Calls
  624.     * @return mixed
  625.     */
  626.     public function xmlapi_query( $function, $vars = array() )
  627.     {
  628.         // Check to make sure all the data needed to perform the query is in place
  629.         if (!$function) {
  630.             throw new Exception('xmlapi_query() requires a function to be passed to it');
  631.         }
  632.  
  633.         if ($this->user == null) {
  634.             throw new Exception('no user has been set');
  635.         }
  636.  
  637.         if ($this->auth ==null) {
  638.             throw new Exception('no authentication information has been set');
  639.         }
  640.  
  641.         // Build the query:
  642.  
  643.         $query_type = '/xml-api/';
  644.  
  645.         if ($this->output == 'json') {
  646.             $query_type = '/json-api/';
  647.         }
  648.  
  649.         $args = http_build_query($vars, '', '&');
  650.         $url =  $this->protocol . '://' . $this->host . ':' . $this->port . $query_type . $function;
  651.  
  652.         if ($this->debug) {
  653.             error_log('URL: ' . $url);
  654.             error_log('DATA: ' . $args);
  655.         }
  656.  
  657.         // Set the $auth string
  658.  
  659.         $authstr = NULL;
  660.         if ($this->auth_type == 'hash') {
  661.             $authstr = 'Authorization: WHM ' . $this->user . ':' . $this->auth . "\r\n";
  662.         } elseif ($this->auth_type == 'pass') {
  663.             $authstr = 'Authorization: Basic ' . base64_encode($this->user .':'. $this->auth) . "\r\n";
  664.         } else {
  665.             throw new Exception('invalid auth_type set');
  666.         }
  667.  
  668.         if ($this->debug) {
  669.             error_log("Authentication Header: " . $authstr ."\n");
  670.         }
  671.  
  672.         // Perform the query (or pass the info to the functions that actually do perform the query)
  673.  
  674.         $response = NULL;
  675.         if ($this->http_client == 'curl') {
  676.             $response = $this->curl_query($url, $args, $authstr);
  677.         } elseif ($this->http_client == 'fopen') {
  678.             $response = $this->fopen_query($url, $args, $authstr);
  679.         }
  680.  
  681.         /*
  682.         *   Post-Query Block
  683.         * Handle response, return proper data types, debug, etc
  684.         */
  685.  
  686.         // print out the response if debug mode is enabled.
  687.         if ($this->debug) {
  688.             error_log("RESPONSE:\n " . $response);
  689.         }
  690.  
  691.         // The only time a response should contain <html> is in the case of authentication error
  692.         // cPanel 11.25 fixes this issue, but if <html> is in the response, we'll error out.
  693.  
  694.         if (stristr($response, '<html>') == true) {
  695.             if (stristr($response, 'Login Attempt Failed') == true) {
  696.                 error_log("Login Attempt Failed");
  697.  
  698.                 return;
  699.             }
  700.             if (stristr($response, 'action="/login/"') == true) {
  701.                 error_log("Authentication Error");
  702.  
  703.                 return;
  704.             }
  705.  
  706.             return;
  707.         }
  708.  
  709.  
  710.         // perform simplexml transformation (array relies on this)
  711.         if ( ($this->output == 'simplexml') || $this->output == 'array') {
  712.             $response = simplexml_load_string($response, null, LIBXML_NOERROR | LIBXML_NOWARNING);
  713.             if (!$response) {
  714.                     error_log("Some error message here");
  715.  
  716.                     return;
  717.             }
  718.             if ($this->debug) {
  719.                 error_log("SimpleXML var_dump:\n" . print_r($response, true));
  720.             }
  721.         }
  722.  
  723.         // perform array tranformation
  724.         if ($this->output == 'array') {
  725.             $response = $this->unserialize_xml($response);
  726.             if ($this->debug) {
  727.                 error_log("Associative Array var_dump:\n" . print_r($response, true));
  728.             }
  729.         }
  730.  
  731.         return $response;
  732.     }
  733.  
  734.     private function curl_query( $url, $postdata, $authstr )
  735.     {
  736.         $curl = curl_init();
  737.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  738.         // Return contents of transfer on curl_exec
  739.          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  740.         // Allow self-signed certs
  741.         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  742.         // Set the URL
  743.         curl_setopt($curl, CURLOPT_URL, $url);
  744.         // Increase buffer size to avoid "funny output" exception
  745.         curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072);
  746.  
  747.         // Pass authentication header
  748.         $header[0] =$authstr .
  749.             "Content-Type: application/x-www-form-urlencoded\r\n" .
  750.             "Content-Length: " . strlen($postdata) . "\r\n" . "\r\n" . $postdata;
  751.  
  752.         curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  753.  
  754.         curl_setopt($curl, CURLOPT_POST, 1);
  755.  
  756.         $result = curl_exec($curl);
  757.         if ($result == false) {
  758.             throw new Exception("curl_exec threw error \"" . curl_error($curl) . "\" for " . $url . "?" . $postdata );
  759.         }
  760.         curl_close($curl);
  761.  
  762.         return $result;
  763.     }
  764.  
  765.     private function fopen_query( $url, $postdata, $authstr )
  766.     {
  767.         if ( !(ini_get('allow_url_fopen') ) ) {
  768.             throw new Exception('fopen_query called on system without allow_url_fopen enabled in php.ini');
  769.         }
  770.  
  771.         $opts = array(
  772.             'http' => array(
  773.                 'allow_self_signed' => true,
  774.                 'method' => 'POST',
  775.                 'header' => $authstr .
  776.                     "Content-Type: application/x-www-form-urlencoded\r\n" .
  777.                     "Content-Length: " . strlen($postdata) . "\r\n" .
  778.                     "\r\n" . $postdata
  779.             )
  780.         );
  781.         $context = stream_context_create($opts);
  782.  
  783.         return file_get_contents($url, false, $context);
  784.     }
  785.  
  786.  
  787.     /*
  788.     * Convert simplexml to associative arrays
  789.     *
  790.     * This function will convert simplexml to associative arrays.
  791.     */
  792.     private function unserialize_xml($input, $callback = null, $recurse = false)
  793.     {
  794.         // Get input, loading an xml string with simplexml if its the top level of recursion
  795.         $data = ( (!$recurse) && is_string($input) ) ? simplexml_load_string($input) : $input;
  796.         // Convert SimpleXMLElements to array
  797.         if ($data instanceof SimpleXMLElement) {
  798.             $data = (array) $data;
  799.         }
  800.         // Recurse into arrays
  801.         if (is_array($data)) {
  802.             foreach ($data as &$item) {
  803.                 $item = $this->unserialize_xml($item, $callback, true);
  804.             }
  805.         }
  806.         // Run callback and return
  807.         return (!is_array($data) && is_callable($callback)) ? call_user_func($callback, $data) : $data;
  808.     }
  809.  
  810.  
  811.     /* TO DO:
  812.       Implement API1 and API2 query functions!!!!!
  813.     */
  814.     /**
  815.     * Call an API1 function
  816.     *
  817.     * This function allows you to call API1 from within the XML-API,  This allowes a user to peform actions
  818.     * such as adding ftp accounts, etc
  819.     *
  820.     * @param string $user The username of the account to perform API1 actions on
  821.     * @param string $module The module of the API1 call to use
  822.     * @param string $function The function of the API1 call
  823.     * @param array $args The arguments for the API1 function, this should be a non-associative array
  824.     * @return mixed
  825.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
  826.     * @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
  827.     * @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiOne API1 Documentation
  828.     */
  829.     public function api1_query($user, $module, $function, $args = array() )
  830.     {
  831.         if ( !isset($module) || !isset($function) || !isset($user) ) {
  832.             error_log("api1_query requires that a module and function are passed to it");
  833.  
  834.             return false;
  835.         }
  836.  
  837.         if (!is_array($args)) {
  838.             error_log('api1_query requires that it is passed an array as the 4th parameter');
  839.  
  840.             return false;
  841.         }
  842.  
  843.         $cpuser = 'cpanel_xmlapi_user';
  844.         $module_type = 'cpanel_xmlapi_module';
  845.         $func_type = 'cpanel_xmlapi_func';
  846.         $api_type = 'cpanel_xmlapi_apiversion';
  847.  
  848.         if ( $this->get_output() == 'json' ) {
  849.             $cpuser = 'cpanel_jsonapi_user';
  850.             $module_type = 'cpanel_jsonapi_module';
  851.             $func_type = 'cpanel_jsonapi_func';
  852.             $api_type = 'cpanel_jsonapi_apiversion';
  853.         }
  854.  
  855.         $call = array(
  856.                 $cpuser => $user,
  857.                 $module_type => $module,
  858.                 $func_type => $function,
  859.                 $api_type => '1'
  860.             );
  861.         for ($int = 0; $int < count($args);  $int++) {
  862.             $call['arg-' . $int] = $args[$int];
  863.         }
  864.  
  865.         return $this->xmlapi_query('cpanel', $call);
  866.     }
  867.  
  868.     /**
  869.     * Call an API2 Function
  870.     *
  871.     * This function allows you to call an API2 function, this is the modern API for cPanel and should be used in preference over
  872.     * API1 when possible
  873.     *
  874.     * @param string $user The username of the account to perform API2 actions on
  875.     * @param string $module The module of the API2 call to use
  876.     * @param string $function The function of the API2 call
  877.     * @param array $args An associative array containing the arguments for the API2 call
  878.     * @return mixed
  879.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
  880.     * @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
  881.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ApiTwo Legacy API2 Documentation
  882.     * @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiTwo API2 Documentation
  883.     */
  884.  
  885.     public function api2_query($user, $module, $function, $args = array())
  886.     {
  887.         if (!isset($user) || !isset($module) || !isset($function) ) {
  888.             error_log("api2_query requires that a username, module and function are passed to it");
  889.  
  890.             return false;
  891.         }
  892.         if (!is_array($args)) {
  893.             error_log("api2_query requires that an array is passed to it as the 4th parameter");
  894.  
  895.             return false;
  896.         }
  897.  
  898.         $cpuser = 'cpanel_xmlapi_user';
  899.         $module_type = 'cpanel_xmlapi_module';
  900.         $func_type = 'cpanel_xmlapi_func';
  901.         $api_type = 'cpanel_xmlapi_apiversion';
  902.  
  903.         if ( $this->get_output() == 'json' ) {
  904.             $cpuser = 'cpanel_jsonapi_user';
  905.             $module_type = 'cpanel_jsonapi_module';
  906.             $func_type = 'cpanel_jsonapi_func';
  907.             $api_type = 'cpanel_jsonapi_apiversion';
  908.         }
  909.  
  910.         $args[$cpuser] = $user;
  911.         $args[$module_type] = $module;
  912.         $args[$func_type] = $function;
  913.         $args[$api_type] = '2';
  914.  
  915.         return $this->xmlapi_query('cpanel', $args);
  916.     }
  917.  
  918.     ####
  919.    #  XML API Functions
  920.    ####
  921.  
  922.     /**
  923.     * Return a list of available XML-API calls
  924.     *
  925.     * This function will return an array containing all applications available within the XML-API
  926.     *
  927.     * @return mixed
  928.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAvailableCalls XML API Call documentation
  929.     */
  930.     public function applist()
  931.     {
  932.         return $this->xmlapi_query('applist');
  933.     }
  934.  
  935.     ####
  936.    # Account functions
  937.    ####
  938.  
  939.     /**
  940.     * Create a cPanel Account
  941.     *
  942.     * This function will allow one to create an account, the $acctconf parameter requires that the follow
  943.     * three associations are defined:
  944.     *   - username
  945.     *   - password
  946.     *   - domain
  947.     *
  948.     * Failure to prive these will cause an error to be logged.  Any other key/value pairs as defined by the createaccount call
  949.     * documentation are allowed parameters for this call.
  950.     *
  951.     * @param array $acctconf
  952.     * @return mixed
  953.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CreateAccount XML API Call documentation
  954.     */
  955.  
  956.     public function createacct($acctconf)
  957.     {
  958.         if (!is_array($acctconf)) {
  959.             error_log("createacct requires that first parameter passed to it is an array");
  960.  
  961.             return false;
  962.         }
  963.         if (!isset($acctconf['username']) || !isset($acctconf['password']) || !isset($acctconf['domain'])) {
  964.             error_log("createacct requires that username, password & domain elements are in the array passed to it");
  965.  
  966.             return false;
  967.         }
  968.  
  969.         return $this->xmlapi_query('createacct', $acctconf);
  970.     }
  971.  
  972.     /**
  973.     * Change a cPanel Account's Password
  974.     *
  975.     * This function will allow you to change the password of a cpanel account
  976.     *
  977.     * @param string $username The username to change the password of
  978.     * @param string $pass The new password for the cPanel Account
  979.     * @return mixed
  980.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ChangePassword XML API Call documentation
  981.     */
  982.     public function passwd($username, $pass)
  983.     {
  984.         if (!isset($username) || !isset($pass)) {
  985.             error_log("passwd requires that an username and password are passed to it");
  986.  
  987.             return false;
  988.         }
  989.  
  990.         return $this->xmlapi_query('passwd', array('user' => $username, 'pass' => $pass));
  991.     }
  992.  
  993.     /**
  994.     * Limit an account's monthly bandwidth usage
  995.     *
  996.     * This function will set an account's bandwidth limit.
  997.     *
  998.     * @param string $username The username of the cPanel account to modify
  999.     * @param int $bwlimit The new bandwidth limit in megabytes
  1000.     * @return mixed
  1001.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LimitBandwidth XML API Call documentation
  1002.     */
  1003.     public function limitbw($username, $bwlimit)
  1004.     {
  1005.         if (!isset($username) || !isset($bwlimit)) {
  1006.             error_log("limitbw requires that an username and bwlimit are passed to it");
  1007.  
  1008.             return false;
  1009.         }
  1010.  
  1011.         return $this->xmlapi_query('limitbw', array('user' => $username, 'bwlimit' => $bwlimit));
  1012.     }
  1013.  
  1014.     /**
  1015.     * List accounts on Server
  1016.     *
  1017.     * This call will return a list of account on a server, either no parameters or both parameters may be passed to this function.
  1018.     *
  1019.     * @param string $searchtype Type of account search to use, allowed values: domain, owner, user, ip or package
  1020.     * @param string $search the string to search against
  1021.     * @return mixed
  1022.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAccounts XML API Call documentation
  1023.     */
  1024.     public function listaccts($searchtype = null, $search = null)
  1025.     {
  1026.         if ($search) {
  1027.             return $this->xmlapi_query('listaccts', array('searchtype' => $searchtype, 'search' => $search ));
  1028.         }
  1029.  
  1030.         return $this->xmlapi_query('listaccts');
  1031.     }
  1032.  
  1033.     /**
  1034.     * Modify a cPanel account
  1035.     *
  1036.     * This call will allow you to change limitations and information about an account.  See the XML API call documentation for a list of
  1037.     * acceptable values for args.
  1038.     *
  1039.     * @param string $username The username to modify
  1040.     * @param array $args the new values for the modified account (see {@link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ModifyAccount modifyacct documentation})
  1041.     * @return mixed
  1042.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ModifyAccount XML API Call documentation
  1043.     */
  1044.     public function modifyacct($username, $args = array())
  1045.     {
  1046.         if (!isset($username)) {
  1047.             error_log("modifyacct requires that username is passed to it");
  1048.  
  1049.             return false;
  1050.         }
  1051.         $args['user'] = $username;
  1052.         if (sizeof($args) < 2) {
  1053.             error_log("modifyacct requires that at least one attribute is passed to it");
  1054.  
  1055.             return false;
  1056.         }
  1057.  
  1058.         return $this->xmlapi_query('modifyacct', $args);
  1059.     }
  1060.  
  1061.     /**
  1062.     * Edit a cPanel Account's Quota
  1063.     *
  1064.     * This call will allow you to change a cPanel account's quota
  1065.     *
  1066.     * @param string $username The username of the account to modify the quota.
  1067.     * @param int $quota the new quota in megabytes
  1068.     * @return mixed
  1069.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/EditQuota XML API Call documentation
  1070.     */
  1071.     public function editquota($username, $quota)
  1072.     {
  1073.         if (!isset($username) || !isset($quota)) {
  1074.             error_log("editquota requires that an username and quota are passed to it");
  1075.  
  1076.             return false;
  1077.         }
  1078.  
  1079.         return $this->xmlapi_query('editquota', array('user' => $username, 'quota' => $quota));
  1080.     }
  1081.  
  1082.     /**
  1083.     * Return a summary of the account's information
  1084.     *
  1085.     * This call will return a brief report of information about an account, such as:
  1086.     *   - Disk Limit
  1087.     *   - Disk Used
  1088.     *   - Domain
  1089.     *   - Account Email
  1090.     *   - Theme
  1091.     *   - Start Data
  1092.     *
  1093.     * Please see the XML API Call documentation for more information on what is returned by this call
  1094.     *
  1095.     * @param string $username The username to retrieve a summary of
  1096.     * @return mixed
  1097.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ShowAccountInformation XML API Call documenation
  1098.     */
  1099.     public function accountsummary($username)
  1100.     {
  1101.         if (!isset($username)) {
  1102.             error_log("accountsummary requires that an username is passed to it");
  1103.  
  1104.             return false;
  1105.         }
  1106.  
  1107.         return $this->xmlapi_query('accountsummary', array('user' => $username));
  1108.     }
  1109.  
  1110.     /**
  1111.     * Suspend a User's Account
  1112.     *
  1113.     * This function will suspend the specified cPanel users account.
  1114.     * The $reason parameter is optional, but can contain a string of any length
  1115.     *
  1116.     * @param string $username The username to suspend
  1117.     * @param string $reason The reason for the suspension
  1118.     * @return mixed
  1119.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SuspendAccount XML API Call documentation
  1120.     */
  1121.     public function suspendacct($username, $reason = null)
  1122.     {
  1123.         if (!isset($username)) {
  1124.             error_log("suspendacct requires that an username is passed to it");
  1125.  
  1126.             return false;
  1127.         }
  1128.         if ($reason) {
  1129.             return $this->xmlapi_query('suspendacct', array('user' => $username, 'reason' => $reason ));
  1130.         }
  1131.  
  1132.         return $this->xmlapi_query('suspendacct', array('user' => $username));
  1133.     }
  1134.  
  1135.     /**
  1136.     * List suspended accounts on a server
  1137.     *
  1138.     * This function will return an array containing all the suspended accounts on a server
  1139.     *
  1140.     * @return mixed
  1141.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListSuspended XML API Call documentation
  1142.     */
  1143.     public function listsuspended()
  1144.     {
  1145.         return $this->xmlapi_query('listsuspended');
  1146.     }
  1147.  
  1148.     /**
  1149.     * Remove an Account
  1150.     *
  1151.     * This XML API call will remove an account on the server
  1152.     * The $keepdns parameter is optional, when enabled this will leave the DNS zone on the server
  1153.     *
  1154.     * @param string $username The usename to delete
  1155.     * @param bool $keepdns When pass a true value, the DNS zone will be retained
  1156.     * @return mixed
  1157.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/TerminateAccount
  1158.     */
  1159.     public function removeacct($username, $keepdns = false)
  1160.     {
  1161.         if (!isset($username)) {
  1162.             error_log("removeacct requires that a username is passed to it");
  1163.  
  1164.             return false;
  1165.         }
  1166.         if ($keepdns) {
  1167.             return $this->xmlapi_query('removeacct', array('user' => $username, 'keepdns' => '1'));
  1168.         }
  1169.  
  1170.         return $this->xmlapi_query('removeacct', array('user' => $username));
  1171.     }
  1172.  
  1173.     /**
  1174.     * Unsuspend an Account
  1175.     *
  1176.     * This XML API call will unsuspend an account
  1177.     *
  1178.     * @param string $username The username to unsuspend
  1179.     * @return mixed
  1180.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/UnsuspendAcount XML API Call documentation
  1181.      */
  1182.     public function unsuspendacct($username)
  1183.     {
  1184.         if (!isset($username)) {
  1185.             error_log("unsuspendacct requires that a username is passed to it");
  1186.  
  1187.             return false;
  1188.         }
  1189.  
  1190.         return $this->xmlapi_query('unsuspendacct', array('user' => $username));
  1191.     }
  1192.  
  1193.     /**
  1194.     * Change an Account's Package
  1195.     *
  1196.     * This XML API will change the package associated account.
  1197.     *
  1198.     * @param string $username the username to change the package of
  1199.     * @param string $pkg The package to change the account to.
  1200.     * @return mixed
  1201.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ChangePackage XML API Call documentation
  1202.     */
  1203.     public function changepackage($username, $pkg)
  1204.     {
  1205.         if (!isset($username) || !isset($pkg)) {
  1206.             error_log("changepackage requires that username and pkg are passed to it");
  1207.  
  1208.             return false;
  1209.         }
  1210.  
  1211.         return $this->xmlapi_query('changepackage', array('user' => $username, 'pkg' => $pkg));
  1212.     }
  1213.  
  1214.     /**
  1215.     * Return the privileges a reseller has in WHM
  1216.     *
  1217.     * This will return a list of the privileges that a reseller has to WHM
  1218.     * @return mixed
  1219.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ViewPrivileges XML API Call documentation
  1220.     */
  1221.     public function myprivs()
  1222.     {
  1223.         return $this->xmlapi_query('myprivs');
  1224.     }
  1225.  
  1226.  
  1227.     /**
  1228.     * Display Data about a Virtual Host
  1229.     *
  1230.     * This function will return information about a specific domain.  This data is essentially a representation of the data
  1231.     * Contained in the httpd.conf VirtualHost for the domain.
  1232.     *
  1233.     * @return mixed
  1234.     * @param string $domain The domain to fetch information for
  1235.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DomainUserData
  1236.     */
  1237.  
  1238.     public function domainuserdata( $domain )
  1239.     {
  1240.         if (!isset( $domain ) ) {
  1241.             error_log("domainuserdata requires that domain is passed to it");
  1242.  
  1243.             return false;
  1244.         }
  1245.  
  1246.         return $this->xmlapi_query("domainuserdata", array( 'domain' => $domain ) );
  1247.     }
  1248.  
  1249.     /**
  1250.     * Change a site's IP Address
  1251.     *
  1252.     * This function will allow you to change the IP address that a domain listens on.
  1253.     * In order to properly call this function Either $user or $domain parameters must be defined
  1254.     * @param string $ip The $ip address to change the account or domain to
  1255.     * @param string $user The username to change the IP of
  1256.     * @param string $domain The domain to change the IP of
  1257.     * @return mixed
  1258.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetSiteIp XML API Call documentation
  1259.     */
  1260.     public function setsiteip ( $ip, $user = null, $domain = null )
  1261.     {
  1262.         if ( !isset($ip) ) {
  1263.             error_log("setsiteip requires that ip is passed to it");
  1264.  
  1265.             return false;
  1266.         }
  1267.  
  1268.         if ($user == null && $domain == null) {
  1269.             error_log("setsiteip requires that either domain or user is passed to it");
  1270.  
  1271.             return false;
  1272.         }
  1273.  
  1274.         if ($user == null) {
  1275.             return $this->xmlapi_query( "setsiteip", array( "ip" => $ip, "domain" => $domain ) );
  1276.         } else {
  1277.             return $this->xmlapi_query( "setsiteip", array( "ip" => $ip, "user" => $user ) );
  1278.         }
  1279.     }
  1280.  
  1281.     ####
  1282.    # DNS Functions
  1283.    ####
  1284.  
  1285.     // This API function lets you create a DNS zone.
  1286.     /**
  1287.     * Add a DNS Zone
  1288.     *
  1289.     * This XML API function will create a DNS Zone.  This will use the "standard" template when
  1290.     * creating the zone.
  1291.     *
  1292.     * @param string $domain The DNS Domain that you wish to create a zone for
  1293.     * @param string $ip The IP you want the domain to resolve to
  1294.     * @return mixed
  1295.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AddDNSZone XML API Call documentation
  1296.     */
  1297.     public function adddns($domain, $ip)
  1298.     {
  1299.         if (!isset($domain) || !isset($ip)) {
  1300.             error_log("adddns require that domain, ip are passed to it");
  1301.  
  1302.             return false;
  1303.         }
  1304.  
  1305.         return $this->xmlapi_query('adddns', array('domain' => $domain, 'ip' => $ip));
  1306.     }
  1307.  
  1308.     /**
  1309.     * Add a record to a zone
  1310.     *
  1311.     * This will append a record to a DNS Zone.  The $args argument to this function
  1312.     * must be an associative array containing information about the DNS zone, please
  1313.     * see the XML API Call documentation for more info
  1314.     *
  1315.     * @param string $zone The DNS zone that you want to add the record to
  1316.     * @param array $args Associative array representing the record to be added
  1317.     * @return mixed
  1318.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AddZoneRecord XML API Call documentation
  1319.     */
  1320.     public function addzonerecord( $zone, $args )
  1321.     {
  1322.         if (!is_array($args)) {
  1323.             error_log("addzonerecord requires that $args passed to it is an array");
  1324.  
  1325.             return;
  1326.         }
  1327.  
  1328.         $args['zone'] = $zone;
  1329.  
  1330.         return $this->xmlapi_query('addzonerecord', $args);
  1331.     }
  1332.  
  1333.     /**
  1334.     * Edit a Zone Record
  1335.     *
  1336.     * This XML API Function will allow you to edit an existing DNS Zone Record.
  1337.     * This works by passing in the line number of the record you wish to edit.
  1338.     * Line numbers can be retrieved with dumpzone()
  1339.     *
  1340.     * @param string $zone The zone to edit
  1341.     * @param int $line The line number of the zone to edit
  1342.     * @param array $args An associative array representing the zone record
  1343.     * @return mixed
  1344.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/EditZoneRecord XML API Call documentation
  1345.     * @see dumpzone()
  1346.     */
  1347.  
  1348.     public function editzonerecord( $zone, $line, $args )
  1349.     {
  1350.         if (!is_array($args)) {
  1351.             error_log("editzone requires that $args passed to it is an array");
  1352.  
  1353.             return;
  1354.         }
  1355.  
  1356.         $args['domain'] = $zone;
  1357.         $args['Line'] = $line;
  1358.  
  1359.         return $this->xmlapi_query('editzonerecord', $args);
  1360.     }
  1361.  
  1362.     /**
  1363.     * Retrieve a DNS Record
  1364.     *
  1365.     * This function will return a data structure representing a DNS record, to
  1366.     * retrieve all lines see dumpzone.
  1367.     * @param string $zone The zone that you want to retrieve a record from
  1368.     * @param string $line The line of the zone that you want to retrieve
  1369.     * @return mixed
  1370.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/GetZoneRecord XML API Call documentation
  1371.     */
  1372.     public function getzonerecord( $zone, $line )
  1373.     {
  1374.         return $this->xmlapi_query('getzonerecord', array( 'domain' => $zone, 'Line' => $line ) );
  1375.     }
  1376.  
  1377.     /**
  1378.     * Remove a DNS Zone
  1379.     *
  1380.     * This function will remove a DNS Zone from the server
  1381.     *
  1382.     * @param string $domain The domain to be remove
  1383.     * @return mixed
  1384.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DeleteDNSZone XML API Call documentation
  1385.     */
  1386.     public function killdns($domain)
  1387.     {
  1388.         if (!isset($domain)) {
  1389.             error_log("killdns requires that domain is passed to it");
  1390.  
  1391.             return false;
  1392.         }
  1393.  
  1394.         return $this->xmlapi_query('killdns', array('domain' => $domain));
  1395.     }
  1396.  
  1397.     /**
  1398.     * Return a List of all DNS Zones on the server
  1399.     *
  1400.     * This XML API function will return an array containing all the DNS Zones on the server
  1401.     *
  1402.     * @return mixed
  1403.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListDNSZone XML API Call documentation
  1404.     */
  1405.     public function listzones()
  1406.     {
  1407.         return $this->xmlapi_query('listzones');
  1408.     }
  1409.  
  1410.     /**
  1411.     * Return all records in a zone
  1412.     *
  1413.     * This function will return all records within a zone.
  1414.     * @param string $domain The domain to return the records from.
  1415.     * @return mixed
  1416.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListOneZone XML API Call documentation
  1417.     * @see editdnsrecord()
  1418.     * @see getdnsrecord()
  1419.     */
  1420.     public function dumpzone($domain)
  1421.     {
  1422.         if (!isset($domain)) {
  1423.             error_log("dumpzone requires that a domain is passed to it");
  1424.  
  1425.             return false;
  1426.         }
  1427.  
  1428.         return $this->xmlapi_query('dumpzone', array('domain' => $domain));
  1429.     }
  1430.  
  1431.     /**
  1432.     * Return a Nameserver's IP
  1433.     *
  1434.     * This function will return a nameserver's IP
  1435.     *
  1436.     * @param string $nameserver The nameserver to lookup
  1437.     * @return mixed
  1438.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LookupIP XML API Call documentation
  1439.     */
  1440.     public function lookupnsip($nameserver)
  1441.     {
  1442.         if (!isset($nameserver)) {
  1443.             error_log("lookupnsip requres that a nameserver is passed to it");
  1444.  
  1445.             return false;
  1446.         }
  1447.  
  1448.         return $this->xmlapi_query('lookupnsip', array('nameserver' => $nameserver));
  1449.     }
  1450.  
  1451.     /**
  1452.     * Remove a line from a zone
  1453.     *
  1454.     * This function will remove the specified line from a zone
  1455.     * @param string $zone The zone to remove a line from
  1456.     * @param int $line The line to remove from the zone
  1457.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/RemoveZone XML API Call documentation
  1458.     */
  1459.     public function removezonerecord($zone, $line)
  1460.     {
  1461.         if ( !isset($zone) || !isset($line) ) {
  1462.             error_log("removezone record requires that a zone and line number is passed to it");
  1463.  
  1464.             return false;
  1465.         }
  1466.  
  1467.         return $this->xmlapi_query('removezonerecord', array('zone' => $zone, 'Line' => $line) );
  1468.     }
  1469.  
  1470.     /**
  1471.     * Reset a zone
  1472.     *
  1473.     * This function will reset a zone removing all custom records.  Subdomain records will be readded by scanning the userdata datastore.
  1474.     * @param string $domain the domain name of the zone to reset
  1475.     * @return mixed
  1476.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ResetZone XML API Call documentation
  1477.     */
  1478.     public function resetzone($domain)
  1479.     {
  1480.         if ( !isset($domain) ) {
  1481.             error_log("resetzone requires that a domain name is passed to it");
  1482.  
  1483.             return false;
  1484.         }
  1485.  
  1486.         return $this->xmlapi_query('resetzone', array('domain' => $domain));
  1487.     }
  1488.  
  1489.     ####
  1490.    # Package Functions
  1491.    ####
  1492.  
  1493.     /**
  1494.     * Add a new package
  1495.     *
  1496.     * This function will allow you to add a new package
  1497.     * This function should be passed an associative array containing elements that define package parameters.
  1498.     * These variables map directly to the parameters for the XML-API Call, please refer to the link below for a complete
  1499.     * list of possible variable.  The "name" element is required.
  1500.     * @param array $pkg an associative array containing package parameters
  1501.     * @return mixed
  1502.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AddPackage XML API Call documentation
  1503.     */
  1504.     public function addpkg($pkg)
  1505.     {
  1506.         if (!isset($pkg['name'])) {
  1507.             error_log("addpkg requires that name is defined in the array passed to it");
  1508.  
  1509.             return false;
  1510.         }
  1511.  
  1512.         return $this->xmlapi_query('addpkg', $pkg);
  1513.     }
  1514.  
  1515.     /**
  1516.     * Remove a package
  1517.     *
  1518.     * This function allow you to delete a package
  1519.     * @param string $pkgname The package you wish to delete
  1520.     * @return mixed
  1521.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DeletePackage XML API Call documentation
  1522.     */
  1523.     public function killpkg($pkgname)
  1524.     {
  1525.         if (!isset($pkgname)) {
  1526.             error_log("killpkg requires that the package name is passed to it");
  1527.  
  1528.             return false;
  1529.         }
  1530.  
  1531.         return $this->xmlapi_query('killpkg', array('pkg' => $pkgname));
  1532.     }
  1533.  
  1534.     /**
  1535.     * Edit a package
  1536.     *
  1537.     * This function allows you to change a package's paremeters.  This is passed an associative array defining
  1538.     * the parameters for the package.  The keys within this array map directly to the XML-API call, please see the link
  1539.     * below for a list of possible keys within this package.  The name element is required.
  1540.     * @param array $pkg An associative array containing new parameters for the package
  1541.     * @return mixed
  1542.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/EditPackage XML API Call documentation
  1543.     */
  1544.     public function editpkg($pkg)
  1545.     {
  1546.         if (!isset($pkg['name'])) {
  1547.             error_log("editpkg requires that name is defined in the array passed to it");
  1548.  
  1549.             return false;
  1550.         }
  1551.  
  1552.         return $this->xmlapi_query('editpkg', $pkg);
  1553.     }
  1554.  
  1555.     /**
  1556.     * List Packages
  1557.     *
  1558.     * This function will list all packages available to the user
  1559.     * @return mixed
  1560.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListPackages XML API Call documentation
  1561.     */
  1562.     public function listpkgs()
  1563.     {
  1564.         return $this->xmlapi_query('listpkgs');
  1565.     }
  1566.  
  1567.     ####
  1568.    # Reseller functions
  1569.    ####
  1570.  
  1571.     /**
  1572.     * Make a user a reseller
  1573.     *
  1574.     * This function will allow you to mark an account as having reseller privileges
  1575.     * @param string $username The username of the account you wish to add reseller privileges to
  1576.     * @param int $makeowner Boolean 1 or 0 defining whether the account should own itself or not
  1577.     * @see setacls()
  1578.     * @see setresellerlimits()
  1579.     * @return mixed
  1580.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AddResellerPrivileges XML API Call documentation
  1581.     */
  1582.     public function setupreseller($username, $makeowner = true)
  1583.     {
  1584.         if (!isset($username)) {
  1585.             error_log("setupreseller requires that username is passed to it");
  1586.  
  1587.             return false;
  1588.         }
  1589.         if ($makeowner) {
  1590.             return $this->xmlapi_query('setupreseller', array('user' => $username, 'makeowner' => '1'));
  1591.         }
  1592.  
  1593.         return $this->xmlapi_query('setupreseller', array('user' => $username, 'makeowner' => '0'));
  1594.     }
  1595.  
  1596.     /**
  1597.     * Create a New ACL List
  1598.     *
  1599.     * This function allows you to create a new privilege set for reseller accounts.  This is passed an
  1600.     * Associative Array containing the configuration information for this variable.  Please see the XML API Call documentation
  1601.     * For more information.  "acllist" is a required element within this array
  1602.     * @param array $acl an associative array describing the parameters for the ACL to be create
  1603.     * @return mixed
  1604.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CreateResellerACLList XML API Call documentation
  1605.     */
  1606.     public function saveacllist($acl)
  1607.     {
  1608.         if (!isset($acl['acllist'])) {
  1609.             error_log("saveacllist requires that acllist is defined in the array passed to it");
  1610.  
  1611.             return false;
  1612.         }
  1613.  
  1614.         return $this->xmlapi_query('saveacllist', $acl);
  1615.     }
  1616.  
  1617.  
  1618.     /**
  1619.     * List available saved ACLs
  1620.     *
  1621.     * This function will return a list of Saved ACLs for reseller accounts
  1622.     * @return mixed
  1623.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListCurrentResellerACLLists XML API Call documentation
  1624.     */
  1625.     public function listacls()
  1626.     {
  1627.         return $this->xmlapi_query('listacls');
  1628.     }
  1629.  
  1630.     /**
  1631.     * List Resellers
  1632.     *
  1633.     * This function will return a list of resellers on the server
  1634.     * @return mixed
  1635.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListResellerAccounts XML API Call documentation
  1636.     */
  1637.     public function listresellers()
  1638.     {
  1639.         return $this->xmlapi_query('listresellers');
  1640.     }
  1641.  
  1642.     /**
  1643.     * Get a reseller's statistics
  1644.     *
  1645.     * This function will return general information on a reseller and all it's account individually such as disk usage and bandwidth usage
  1646.     *
  1647.     * @param string $username The reseller to be checked
  1648.     * @return mixed
  1649.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListResellersAccountsInformation XML API Call documentation
  1650.     */
  1651.     public function resellerstats($username)
  1652.     {
  1653.         if (!isset($username)) {
  1654.             error_log("resellerstats requires that a username is passed to it");
  1655.  
  1656.             return false;
  1657.         }
  1658.  
  1659.         return $this->xmlapi_query('resellerstats', array('reseller' => $username));
  1660.     }
  1661.  
  1662.     /**
  1663.     * Remove Reseller Privileges
  1664.     *
  1665.     * This function will remove an account's reseller privileges, this does not remove the account.
  1666.     *
  1667.     * @param string $username The username to remove reseller privileges from
  1668.     * @return mixed
  1669.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/RemoveResellerPrivileges XML API Call documentation
  1670.     */
  1671.     public function unsetupreseller($username)
  1672.     {
  1673.         if (!isset($username)) {
  1674.             error_log("unsetupreseller requires that a username is passed to it");
  1675.  
  1676.             return false;
  1677.         }
  1678.  
  1679.         return $this->xmlapi_query('unsetupreseller', array('user' => $username));
  1680.     }
  1681.  
  1682.     /**
  1683.     * Set a reseller's privileges
  1684.     *
  1685.     * This function will allow you to set what parts of WHM a reseller has access to.  This is passed an associative array
  1686.     * containing the privleges that this reseller should have access to.  These map directly to the parameters passed to the XML API Call
  1687.     * Please view the XML API Call documentation for more information.  "reseller" is the only required element within this array
  1688.     * @param array $acl An associative array containing all the ACL information for the reseller
  1689.     * @return mixed
  1690.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellersACLList XML API Call documentation
  1691.     */
  1692.     public function setacls($acl)
  1693.     {
  1694.         if (!isset($acl['reseller'])) {
  1695.             error_log("setacls requires that reseller is defined in the array passed to it");
  1696.  
  1697.             return false;
  1698.         }
  1699.  
  1700.         return $this->xmlapi_query('setacls', $acl);
  1701.     }
  1702.  
  1703.     /**
  1704.     * Terminate a Reseller's Account
  1705.     *
  1706.     * This function will terminate a reseller's account and all accounts owned by the reseller
  1707.     *
  1708.     * @param string $reseller the name of the reseller to terminate
  1709.     * @param boolean $terminatereseller Passing this as true will terminate the the reseller's account as well as all the accounts owned by the reseller
  1710.     * @return mixed
  1711.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/TerminateResellerandAccounts XML API Call documentation
  1712.     *
  1713.     **/
  1714.     public function terminatereseller($reseller, $terminatereseller = true)
  1715.     {
  1716.         if (!isset($reseller)) {
  1717.             error_log("terminatereseller requires that username is passed to it");
  1718.  
  1719.             return false;
  1720.         }
  1721.         $verify = 'I understand this will irrevocably remove all the accounts owned by the reseller ' . $reseller;
  1722.         if ($terminatereseller) {
  1723.             return $this->xmlapi_query('terminatereseller', array('reseller' => $reseller, 'terminatereseller' => '1', 'verify' => $verify));
  1724.         }
  1725.  
  1726.         return $this->xmlapi_query('terminatereseller', array('reseller' => $reseller, 'terminatereseller' => '0', 'verify' => $verify));
  1727.     }
  1728.  
  1729.     /**
  1730.     * Set a reseller's dedicated IP addresses
  1731.     *
  1732.     * This function will set a reseller's dedicated IP addresses.  If an IP is not passed to this function,
  1733.     * it will reset the reseller to use the server's main shared IP address.
  1734.     * @param string $user The username of the reseller to change dedicated IPs for
  1735.     * @param string $ip The IP to assign to the  reseller, this can be a comma-seperated list of IPs to allow for multiple IP addresses
  1736.     * @return mixed
  1737.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellerIps XML API Call documentation
  1738.     */
  1739.     public function setresellerips($user, $ip = null)
  1740.     {
  1741.         if (!isset($user) ) {
  1742.             error_log("setresellerips requires that a username is passed to it");
  1743.  
  1744.             return false;
  1745.         }
  1746.         $params = array("user" => $user);
  1747.         if ($ip != null) {
  1748.             $params['ip'] = $ip;
  1749.         }
  1750.  
  1751.         return $this->xmlapi_query('setresellerips',$params);
  1752.     }
  1753.  
  1754.     /**
  1755.     * Set Accounting Limits for a reseller account
  1756.     *
  1757.     * This function allows you to define limits for reseller accounts not included with in access control such as
  1758.     * the number of accounts a reseller is allowed to create, the amount of disk space to use.
  1759.     * This function is passed an associative array defining these limits, these map directly to the parameters for the XML API
  1760.     * Call, please refer to the XML API Call documentation for more information.  The only required parameters is "user"
  1761.     *
  1762.     * @param array $reseller_cfg An associative array containing configuration information for the specified reseller
  1763.     * @return mixed
  1764.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellerLimits XML API Call documentation
  1765.     *
  1766.     */
  1767.     public function setresellerlimits( $reseller_cfg )
  1768.     {
  1769.         if ( !isset($reseller_cfg['user'] ) ) {
  1770.             error_log("setresellerlimits requires that a user is defined in the array passed to it");
  1771.  
  1772.             return false;
  1773.         }
  1774.  
  1775.         return $this->xmlapi_query('setresellerlimits',$reseller_cfg);
  1776.     }
  1777.  
  1778.     /**
  1779.     * Set a reseller's main IP
  1780.     *
  1781.     * This function will allow you to set a reseller's main IP.  By default all accounts created by this reseller
  1782.     * will be created on this IP
  1783.     * @param string $reseller the username of the reseller to change the main IP of
  1784.     * @param string $ip The ip you would like this reseller to create accounts on by default
  1785.     * @return mixed
  1786.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellerMainIp XML API Call documentation
  1787.     */
  1788.     public function setresellermainip($reseller, $ip)
  1789.     {
  1790.         if ( !isset($reseller) || !isset($ip) ) {
  1791.             error_log("setresellermainip requires that an reseller and ip are passed to it");
  1792.  
  1793.             return false;
  1794.         }
  1795.  
  1796.         return $this->xmlapi_query("setresellermainip", array('user' => $reseller, 'ip' => $ip));
  1797.     }
  1798.  
  1799.     /**
  1800.     * Set reseller package limits
  1801.     *
  1802.     * This function allows you to define which packages a reseller has access to use
  1803.     * @param string $user The reseller you wish to define package limits for
  1804.     * @param boolean $no_limit Whether or not you wish this reseller to have packages limits
  1805.     * @param string $package if $no_limit is false, then the package you wish to modify privileges for
  1806.     * @param boolean $allowed if $no_limit is false, then defines if the reseller should have access to the package or not
  1807.     * @param int $number if $no_limit is false, then defines the number of account a reseller can create of a specific package
  1808.     * @return mixed
  1809.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellerPkgLimit XML API Call documentation
  1810.     */
  1811.     public function setresellerpackagelimits($user, $no_limit, $package = null, $allowed = null, $number = null)
  1812.     {
  1813.         if (!isset($user) || !isset($no_limit) ) {
  1814.             error_log("setresellerpackagelimits requires that a username and no_limit are passed to it by default");
  1815.  
  1816.             return false;
  1817.         }
  1818.         if ($no_limit) {
  1819.             return $this->xmlapi_query("setresellerpackagelimits", array( 'user' => $user, "no_limit" => '1') );
  1820.         } else {
  1821.             if ( is_null($package) || is_null($allowed) ) {
  1822.                 error_log('setresellerpackagelimits requires that package and allowed are passed to it if no_limit eq 0');
  1823.  
  1824.                 return false;
  1825.             }
  1826.             $params = array(
  1827.                 'user' => $user,
  1828.                 'no_limit' => '0',
  1829.                 'package' => $package,
  1830.             );
  1831.             if ($allowed) {
  1832.                 $params['allowed'] = 1;
  1833.             } else {
  1834.                 $params['allowed'] = 0;
  1835.             }
  1836.             if ( !is_null($number) ) {
  1837.                 $params['number'] = $number;
  1838.             }
  1839.  
  1840.             return $this->xmlapi_query('setresellerpackagelimits', $params);
  1841.         }
  1842.     }
  1843.  
  1844.     /**
  1845.     * Suspend a reseller and all accounts owned by a reseller
  1846.     *
  1847.     * This function, when called will suspend a reseller account and all account owned by said reseller
  1848.     * @param string $reseller The reseller account to be suspended
  1849.     * @param string $reason (optional) The reason for suspending the reseller account
  1850.     * @return mixed
  1851.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SuspendReseller XML API Call documentation
  1852.     */
  1853.     public function suspendreseller($reseller, $reason = null)
  1854.     {
  1855.         if (!isset($reseller) ) {
  1856.             error_log("suspendreseller requires that the reseller's username is passed to it");
  1857.  
  1858.             return false;
  1859.         }
  1860.         $params = array("user" => $reseller);
  1861.         if ($reason) {
  1862.             $params['reason'] = $reason;
  1863.         }
  1864.  
  1865.         return $this->xmlapi_query('suspendreseller', $params);
  1866.     }
  1867.  
  1868.  
  1869.     /**
  1870.     * Unsuspend a Reseller Account
  1871.     *
  1872.     * This function will unsuspend a reseller account and all accounts owned by the reseller in question
  1873.     * @param string $user The username of the reseller to be unsuspended
  1874.     * @return mixed
  1875.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/UnsuspendReseller XML API Call documentation
  1876.     */
  1877.     public function unsuspendreseller($user)
  1878.     {
  1879.         if (!isset($user) ) {
  1880.             error_log("unsuspendreseller requires that a username is passed to it");
  1881.  
  1882.             return false;
  1883.         }
  1884.  
  1885.         return $this->xmlapi_query('unsuspendreseller', array('user' => $user));
  1886.     }
  1887.  
  1888.     /**
  1889.     * Get the number of accounts owned by a reseller
  1890.     *
  1891.     * This function will return the number of accounts owned by a reseller account, along with information such as the number of active, suspended and accounting limits
  1892.     * @param string $user The username of the reseller to get account information from
  1893.     * @return mixed
  1894.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AcctCounts XML API Call documentation
  1895.     */
  1896.     public function acctcounts($user)
  1897.     {
  1898.         if (!isset($user)) {
  1899.             error_log('acctcounts requires that a username is passed to it');
  1900.  
  1901.             return false;
  1902.         }
  1903.  
  1904.         return $this->xmlapi_query('acctcounts', array('user' => $user) );
  1905.     }
  1906.  
  1907.     /**
  1908.     * Set a reseller's nameservers
  1909.     *
  1910.     * This function allows you to change the nameservers that account created by a specific reseller account will use.
  1911.     * If this function is not passed a $nameservers parameter, it will reset the nameservers for the reseller to the servers's default
  1912.     * @param string $user The username of the reseller account to grab reseller accounts from
  1913.     * @param string $nameservers A comma seperate list of nameservers
  1914.     * @return mixed
  1915.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResellerNameservers XML API Call documentation
  1916.     */
  1917.     public function setresellernameservers($user, $nameservers = null)
  1918.     {
  1919.         if (!isset($user)) {
  1920.             error_log("setresellernameservers requires that a username is passed to it");
  1921.  
  1922.             return false;
  1923.         }
  1924.         $params = array('user' => $user);
  1925.         if ($nameservers) {
  1926.             $params['nameservers'] = $nameservers;
  1927.         }
  1928.  
  1929.         return $this->xmlapi_query('setresellernameservers', $params);
  1930.     }
  1931.  
  1932.     ####
  1933.    # Server information
  1934.    ####
  1935.  
  1936.     /**
  1937.     * Get a server's hostname
  1938.     *
  1939.     * This function will return a server's hostname
  1940.     * @return mixed
  1941.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DisplayServerHostname XML API Call documentation
  1942.     */
  1943.     public function gethostname()
  1944.     {
  1945.         return $this->xmlapi_query('gethostname');
  1946.     }
  1947.  
  1948.     /**
  1949.     * Get the version of cPanel running on the server
  1950.     *
  1951.     * This function will return the version of cPanel/WHM running on the remote system
  1952.     * @return mixed
  1953.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DisplaycPanelWHMVersion XML API Call documentation
  1954.     */
  1955.     public function version()
  1956.     {
  1957.         return $this->xmlapi_query('version');
  1958.     }
  1959.  
  1960.  
  1961.     /**
  1962.     * Get Load Average
  1963.     *
  1964.     * This function will return the loadavg of the remote system
  1965.     *
  1966.     * @return mixed
  1967.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LoadAvg XML API Call documentation
  1968.     */
  1969.     public function loadavg()
  1970.     {
  1971.         return $this->xmlapi_query('loadavg');
  1972.     }
  1973.  
  1974.     /**
  1975.     * Get a list of languages on the remote system
  1976.     *
  1977.     * This function will return a list of available langauges for the cPanel interface
  1978.     * @return mixed
  1979.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/GetLangList XML API Call documentation
  1980.     *
  1981.     */
  1982.     public function getlanglist()
  1983.     {
  1984.         return $this->xmlapi_query('getlanglist');
  1985.     }
  1986.  
  1987.     ####
  1988.    # Server administration
  1989.    ####
  1990.  
  1991.     /**
  1992.     * Reboot server
  1993.     *
  1994.     * This function will reboot the server
  1995.     * @param boolean $force This will determine if the server should be given a graceful or forceful reboot
  1996.     * @return mixed
  1997.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/RebootServer XML API Call documentation
  1998.     */
  1999.     public function reboot($force = false)
  2000.     {
  2001.         if ($force) {
  2002.             return $this->xmlapi_query('reboot', array('force' => '1'));
  2003.         }
  2004.  
  2005.         return $this->xmlapi_query('reboot');
  2006.     }
  2007.  
  2008.     /**
  2009.     * Add an IP to a server
  2010.     *
  2011.     * This function will add an IP alias to your server
  2012.     * @param string $ip The IP to be added
  2013.     * @param string $netmask The netmask of the IP to be added
  2014.     * @return mixed
  2015.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/AddIPAddress XML API Call documentation
  2016.     */
  2017.     public function addip($ip, $netmask)
  2018.     {
  2019.         if (!isset($ip) || !isset($netmask)) {
  2020.             error_log("addip requires that an IP address and Netmask are passed to it");
  2021.  
  2022.             return false;
  2023.         }
  2024.  
  2025.         return $this->xmlapi_query('addip', array('ip' => $ip, 'netmask' => $netmask));
  2026.     }
  2027.  
  2028.     // This function allows you to delete an IP address from your server.
  2029.     /**
  2030.     * Delete an IP from a server
  2031.     *
  2032.     * Remove an IP from the server
  2033.     * @param string $ip The IP to remove
  2034.     * @param string $ethernetdev The ethernet device that the IP is bound to
  2035.     * @param bool $skipifshutdown Whether the function should remove the IP even if the ethernet interface is down
  2036.     * @return mixed
  2037.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DeleteIPAddress XML API Call documentation
  2038.     */
  2039.     public function delip($ip, $ethernetdev = null, $skipifshutdown = false)
  2040.     {
  2041.         $args = array();
  2042.         if (!isset($ip)) {
  2043.             error_log("delip requires that an IP is defined in the array passed to it");
  2044.  
  2045.             return false;
  2046.         }
  2047.         $args['ip'] = $ip;
  2048.         if ($ethernetdev) {
  2049.             $args['ethernetdev'] = $ethernetdev;
  2050.         }
  2051.         $args['skipifshutdown'] = ($skipifshutdown) ? '1' : '0';
  2052.  
  2053.         return $this->xmlapi_query('delip', $args);
  2054.     }
  2055.  
  2056.     /**
  2057.     * List IPs
  2058.     *
  2059.     * This should return a list of IPs on a server
  2060.     * @return mixed
  2061.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/DeleteIPAddress XML API Call documentation
  2062.     */
  2063.     public function listips()
  2064.     {
  2065.         return $this->xmlapi_query('listips');
  2066.     }
  2067.  
  2068.     /**
  2069.     * Set Hostname
  2070.     *
  2071.     * This function will allow you to set the hostname of the server
  2072.     * @param string $hostname the hostname that should be assigned to the serve
  2073.     * @return mixed
  2074.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetHostname XML API Call documentation
  2075.     */
  2076.     public function sethostname($hostname)
  2077.     {
  2078.         if (!isset($hostname)) {
  2079.             error_log("sethostname requires that hostname is passed to it");
  2080.  
  2081.             return false;
  2082.         }
  2083.  
  2084.         return $this->xmlapi_query('sethostname', array('hostname' => $hostname));
  2085.     }
  2086.  
  2087.     /**
  2088.     * Set the resolvers used by the server
  2089.     *
  2090.     * This function will set the resolvers in /etc/resolv.conf for the server
  2091.     * @param string $nameserver1 The IP of the first nameserver to use
  2092.     * @param string $nameserver2 The IP of the second namesever to use
  2093.     * @param string $nameserver3 The IP of the third nameserver to use
  2094.     * @param string $nameserver4 The IP of the forth nameserver to use
  2095.     * @return mixed
  2096.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/SetResolvers XML API Call documentation
  2097.     */
  2098.     public function setresolvers($nameserver1, $nameserver2 = null, $nameserver3 = null)
  2099.     {
  2100.         $args = array();
  2101.         if (!isset($nameserver1)) {
  2102.             error_log("setresolvers requires that nameserver1 is defined in the array passed to it");
  2103.  
  2104.             return false;
  2105.         }
  2106.         $args['nameserver1'] = $nameserver1;
  2107.         if ($nameserver2) {
  2108.             $args['nameserver2'] = $nameserver2;
  2109.         }
  2110.         if ($nameserver3) {
  2111.             $args['nameserver3'] = $nameserver3;
  2112.         }
  2113.  
  2114.         return $this->xmlapi_query('setresolvers', $args);
  2115.     }
  2116.  
  2117.     /**
  2118.     * Display bandwidth Usage
  2119.     *
  2120.     * This function will return all bandwidth usage information for the server,
  2121.     * The arguments for this can be passed in via an associative array, the elements of this array map directly to the
  2122.     * parameters of the call, please see the XML API Call documentation for more information
  2123.     * @param array $args The configuration for what bandwidth information to display
  2124.     * @return mixed
  2125.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ShowBw XML API Call documentation
  2126.     */
  2127.     public function showbw($args = null)
  2128.     {
  2129.         if (is_array($args)) {
  2130.             return $this->xmlapi_query('showbw', $args);
  2131.         }
  2132.  
  2133.         return $this->xmlapi_query('showbw');
  2134.     }
  2135.  
  2136.     public function nvset($key, $value)
  2137.     {
  2138.         if (!isset($key) || !isset($value)) {
  2139.             error_log("nvset requires that key and value are passed to it");
  2140.  
  2141.             return false;
  2142.         }
  2143.  
  2144.         return $this->xmlapi_query('nvset', array('key' => $key, 'value' => $value));
  2145.     }
  2146.  
  2147.     // This function allows you to retrieve and view a non-volatile variable's value.
  2148.     public function nvget($key)
  2149.     {
  2150.         if (!isset($key)) {
  2151.             error_log("nvget requires that key is passed to it");
  2152.  
  2153.             return false;
  2154.         }
  2155.  
  2156.         return $this->xmlapi_query('nvget', array('key' => $key));
  2157.     }
  2158.  
  2159.     ####
  2160.    # Service functions
  2161.    ####
  2162.  
  2163.     /**
  2164.     * Restart a Service
  2165.     *
  2166.     * This function allows you to restart a service on the server
  2167.     * @param string $service the service that you wish to restart please view the XML API Call documentation for acceptable values to this parameters
  2168.     * @return mixed
  2169.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/RestartService XML API Call documentation
  2170.     */
  2171.     public function restartsrv($service)
  2172.     {
  2173.         if (!isset($service)) {
  2174.             error_log("restartsrv requires that service is passed to it");
  2175.  
  2176.             return false;
  2177.         }
  2178.  
  2179.         return $this->xmlapi_query('restartservice', array('service' => $service));
  2180.     }
  2181.  
  2182.     /**
  2183.     * Service Status
  2184.     *
  2185.     * This function will return the status of all services on the and whether they are running or not
  2186.     * @param string $service A single service to filter for.
  2187.     * @return mixed
  2188.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ServiceStatus XML API Call documentation
  2189.     */
  2190.     public function servicestatus($args=array())
  2191.      {
  2192.         if (!empty($args) && !is_array($args)) {
  2193.             $args = array('service'=>$args);
  2194.         } elseif (!is_array($args)) {
  2195.             $args = array();
  2196.         }
  2197.  
  2198.         return $this->xmlapi_query('servicestatus', $args);
  2199.      }
  2200.  
  2201.     /**
  2202.     * Configure A Service
  2203.     *
  2204.     * This function will allow you to enabled or disable services along with their monitoring by chkservd
  2205.     * @param string $service The service to be monitored
  2206.     * @param bool $enabled Whether the service should be enabled or not
  2207.     * @param bool $monitored Whether the service should be monitored or not
  2208.     * @return mixed
  2209.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ConfigureService XML API Call documentation
  2210.     */
  2211.     public function configureservice($service, $enabled = true, $monitored = true)
  2212.     {
  2213.         if (!isset($service)) {
  2214.             error_log("configure service requires that a service is passed to it");
  2215.  
  2216.             return false;
  2217.         }
  2218.         $params = array('service' => $service);
  2219.  
  2220.         if ($enabled) {
  2221.             $params['enabled'] = 1;
  2222.         } else {
  2223.             $params['enabled'] = 0;
  2224.         }
  2225.  
  2226.         if ($monitored) {
  2227.             $params['monitored'] = 1;
  2228.         } else {
  2229.             $params['monitored'] = 0;
  2230.         }
  2231.  
  2232.         return $this->xmlapi_query('configureservice', $params);
  2233.  
  2234.     }
  2235.  
  2236.     ####
  2237.    # SSL functions
  2238.    ####
  2239.  
  2240.     /**
  2241.     * Display information on an SSL host
  2242.     *
  2243.     * This function will return information on an SSL Certificate, CSR, cabundle and SSL key for a specified domain
  2244.     * @param array $args Configuration information for the SSL certificate, please see XML API Call documentation for required values
  2245.     * @return mixed
  2246.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/FetchSSL XML API Call documentation
  2247.     */
  2248.     public function fetchsslinfo($args)
  2249.     {
  2250.         if ( (isset($args['domain']) && isset($args['crtdata'])) || (!isset($args['domain']) && !isset($args['crtdata'])) ) {
  2251.             error_log("fetchsslinfo requires domain OR crtdata is passed to it");
  2252.         }
  2253.         if (isset($args['crtdata'])) {
  2254.             // crtdata must be URL-encoded!
  2255.             $args['crtdata'] = urlencode(trim($args['crtdata']));
  2256.         }
  2257.  
  2258.         return $this->xmlapi_query('fetchsslinfo', $args);
  2259.     }
  2260.  
  2261.     /**
  2262.     * Generate an SSL Certificate
  2263.     *
  2264.     * This function will generate an SSL Certificate, the arguments for this map directly to the call for the XML API call.  Please consult the XML API Call documentation for more information
  2265.     * @param array $args the configuration for the SSL Certificate being generated
  2266.     * @return mixed
  2267.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/GenerateSSL XML API Call documentation
  2268.     */
  2269.     public function generatessl($args)
  2270.     {
  2271.         if (!isset($args['xemail']) || !isset($args['host']) || !isset($args['country']) || !isset($args['state']) || !isset($args['city']) || !isset($args['co']) || !isset($args['cod']) || !isset($args['email']) || !isset($args['pass'])) {
  2272.             error_log("generatessl requires that xemail, host, country, state, city, co, cod, email and pass are defined in the array passed to it");
  2273.  
  2274.             return false;
  2275.         }
  2276.  
  2277.         return $this->xmlapi_query('generatessl', $args);
  2278.     }
  2279.  
  2280.     /**
  2281.     * Install an SSL certificate
  2282.     *
  2283.     * This function will allow you to install an SSL certificate that is uploaded via the $argument parameter to this call.  The arguments for this call map directly to the parameters for the XML API call,
  2284.     * please consult the XML API Call documentation for more information.
  2285.     * @param array $args The configuration for the SSL certificate
  2286.     * @return mixed
  2287.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/InstallSSL XML API Call documentation
  2288.     */
  2289.     public function installssl($args)
  2290.     {
  2291.         if (!isset($args['user']) || !isset($args['domain']) || !isset($args['cert']) || !isset($args['key']) || !isset($args['cab']) || !isset($args['ip'])) {
  2292.             error_log("installssl requires that user, domain, cert, key, cab and ip are defined in the array passed to it");
  2293.  
  2294.             return false;
  2295.         }
  2296.  
  2297.         return $this->xmlapi_query('installssl', $args);
  2298.     }
  2299.  
  2300.     /**
  2301.     * List SSL Certs
  2302.     *
  2303.     * This function will list all SSL certificates installed on the server
  2304.     * @return mixed
  2305.     * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListSSL XML API Call documentation
  2306.     */
  2307.     public function listcrts()
  2308.     {
  2309.         return $this->xmlapi_query('listcrts');
  2310.     }
  2311.  
  2312.     ####
  2313.    # cPanel API1 functions
  2314.    # Note: A cPanel account username is required
  2315.    # Some cPanel features must be enabled to be able to use some function (f.e. park, unpark)
  2316.    ####
  2317.  
  2318.     // This API1 function adds a emailaccount for a specific user.
  2319.     public function addpop($username, $args)
  2320.     {
  2321.         if (!isset($username) || !isset($args)) {
  2322.             error_log("addpop requires that a user and args are passed to it");
  2323.  
  2324.             return false;
  2325.         }
  2326.         if (is_array($args) && (sizeof($args) < 3)) {
  2327.             error_log("addpop requires that args at least contains an email_username, email_password and email_domain");
  2328.  
  2329.             return false;
  2330.         }
  2331.  
  2332.         return $this->api1_query($username, 'Email', 'addpop', $args);
  2333.     }
  2334.  
  2335.     // This API function parks a domain onto this user's account
  2336.     public function park($username, $newdomain, $topdomain)
  2337.     {
  2338.         $args = array();
  2339.         if ( (!isset($username)) && (!isset($newdomain)) ) {
  2340.             error_log("park requires that a username and new domain are passed to it");
  2341.  
  2342.             return false;
  2343.         }
  2344.         $args['domain'] = $newdomain;
  2345.         if ($topdomain) {
  2346.             $args['topdomain'] = $topdomain;
  2347.         }
  2348.  
  2349.         return $this->api2_query($username, 'Park', 'park', $args);
  2350.     }
  2351.  
  2352.     // This API function unparks a domain from this user's account.
  2353.     public function unpark($username, $domain)
  2354.     {
  2355.         $args = array();
  2356.         if ( (!isset($username)) && (!isset($domain)) ) {
  2357.             error_log("unpark requires that a username and domain are passed to it");
  2358.  
  2359.             return false;
  2360.         }
  2361.         $args['domain'] = $domain;
  2362.  
  2363.         return $this->api2_query($username, 'Park', 'unpark', $args);
  2364.     }
  2365.  
  2366.     ####
  2367.    # cPanel API2 functions
  2368.    # Note: A cPanel account username is required
  2369.    # Some cPanel features must be enabled to be able to use some function
  2370.    ####
  2371.  
  2372.     // This API2 function allows you to view the diskusage of a emailaccount.
  2373.     public function getdiskusage($username, $args)
  2374.     {
  2375.         if (!isset($username) || !isset($args)) {
  2376.             error_log("getdiskusage requires that a username and args are passed to it");
  2377.  
  2378.             return false;
  2379.         }
  2380.         if (is_array($args) && (!isset($args['domain']) || !isset($args['login']))) {
  2381.             error_log("getdiskusage requires that args at least contains an email_domain and email_username");
  2382.  
  2383.             return false;
  2384.         }
  2385.  
  2386.         return $this->api2_query($username, 'Email', 'getdiskusage', $args);
  2387.     }
  2388.  
  2389.     // This API2 function allows you to list ftp-users associated with a cPanel account including disk information.
  2390.     public function listftpwithdisk($username)
  2391.     {
  2392.         if (!isset($username)) {
  2393.             error_log("listftpwithdisk requires that user is passed to it");
  2394.  
  2395.             return false;
  2396.         }
  2397.  
  2398.         return $this->api2_query($username, 'Ftp', 'listftpwithdisk');
  2399.     }
  2400.  
  2401.     // This API2 function allows you to list ftp-users associated with a cPanel account.
  2402.     public function listftp($username)
  2403.     {
  2404.         if (!isset($username)) {
  2405.             error_log("listftp requires that user is passed to it");
  2406.  
  2407.             return false;
  2408.         }
  2409.  
  2410.         return $this->api2_query($username, 'Ftp', 'listftp');
  2411.     }
  2412.  
  2413.     // This API function displays a list of all parked domains for a specific user.
  2414.     public function listparkeddomains($username, $domain = null)
  2415.     {
  2416.         $args = array();
  2417.         if (!isset($username)) {
  2418.             error_log("listparkeddomains requires that a user is passed to it");
  2419.  
  2420.             return false;
  2421.         }
  2422.         if (isset($domain)) {
  2423.             $args['regex'] = $domain;
  2424.  
  2425.             return $this->api2_query($username, 'Park', 'listparkeddomains', $args);
  2426.         }
  2427.  
  2428.         return $this->api2_query($username, 'Park', 'listparkeddomains');
  2429.     }
  2430.  
  2431.     // This API function displays a list of all addon domains for a specific user.
  2432.     public function listaddondomains($username, $domain = null)
  2433.     {
  2434.         $args = array();
  2435.         if (!isset($username)) {
  2436.             error_log("listaddondomains requires that a user is passed to it");
  2437.  
  2438.             return false;
  2439.         }
  2440.         if (isset($domain)) {
  2441.             $args['regex'] = $domain;
  2442.  
  2443.             return $this->api2_query($username, 'AddonDomain', 'listaddondomains', $args);
  2444.         }
  2445.  
  2446.         return $this->api2_query($username, 'Park', 'listaddondomains');
  2447.     }
  2448.  
  2449.     // This API function displays a list of all selected stats for a specific user.
  2450.     public function stat($username, $args = null)
  2451.     {
  2452.         if ( (!isset($username)) || (!isset($args)) ) {
  2453.             error_log("stat requires that a username and options are passed to it");
  2454.  
  2455.             return false;
  2456.         }
  2457.         if (is_array($args)) {
  2458.         $display = '';
  2459.             foreach ($args as $key => $value) {
  2460.                 $display .= $value . '|';
  2461.             }
  2462.             $values['display'] = substr($display, 0, -1);
  2463.         } else {
  2464.             $values['display'] = substr($args, 0, -1);
  2465.         }
  2466.  
  2467.         return $this->api2_query($username, 'StatsBar', 'stat', $values);
  2468.     }
  2469.  
  2470. }
Add Comment
Please, Sign In to add comment