/** * Obliterate all internals * * Useful for enforcing a known state. It is extremely rare, outside of unit * testing, that this method is used. * * @return void */ public static function resetInstance() { if (self::$_instance !== null) { self::$_instance = null; } }
* the cPanel system). In both cases, the raw response from the cPanel * system should be well structured and parsed cleanly (i.e., can be * determined by analysing the data in the 'response' container). * * Future implementation of error handling and the Cpanel_Query_Object * may see some programmatic improvement for this, like throwing a special * Exception, however, for now it is up to the programmer to either 1) check * the response container for such failures, or 2) extend the parser library * with such logic. * * Other convenience methods are described below */ $configOpts = array('username' => 'root', 'password' => 'rootsecret'); $services = array('whm' => array('host' => '10.1.4.102', 'user' => 'root', 'password' => 'rootsecret')); $masterConfig = array('config' => $configOpts, 'service' => $services); $cp = Cpanel_PublicAPI::getInstance($masterConfig); $response = $cp->whm_api('listaccts'); /** * A quick check to see that the server response parsed cleanly. */ if (!$response->validResponse()) { $errors = $response->getResponseErrors(); foreach ($errors as $err) { // do something like log or throw Exception } return; } /** * If authentication failed, there's likely to be a data->reason parsed * response */
/** * Please look at the Introduction_to_PublicAPI.php for more details. */ try { require_once realpath(dirname(__FILE__) . '/../Util/Autoload.php'); /** * PublicAPI style */ $cpCfg = array('cpanel' => array('service' => array('whm' => array('host' => '10.1.4.191', 'user' => 'root', 'password' => 'rootsecret')))); $cp = Cpanel_PublicAPI::getInstance($cpCfg); $response = $cp->whm_api('version'); echo "WHM Version: {$response->version}\n"; /** * One alternative style */ $cp = Cpanel_PublicAPI::getInstance(); $whm = $cp->factory('WHM'); $whm->setUser('root')->setPassword('rootsecret')->setHost('10.1.4.191'); $response = $whm->xmlapi_query('version'); echo "WHM Version: {$response->version}\n"; /** * Another alternative is to pass the config to factory() method */ $config = array('host' => '10.1.4.191', 'user' => 'root', 'password' => 'rootsecret'); $whm = $cp->factory('WHM'); $response = $whm->xmlapi_query('version'); echo "WHM Version: {$response->version}\n"; /** * Using direct library */ $config = array('host' => '10.1.4.191', 'user' => 'root', 'password' => 'rootsecret');
/** * Verify exception is throw when no method is passed for indirect * invocation * * @depends testFactoryCanReturnWHMFromSimpleConfig * @expectedException Exception * @outputBuffering disabled */ public function testCallerThrowExceptionWhenNoArgsOnDispatchedApiRequest() { $arr = array('service' => array('whm' => array('config' => array('host' => '1.1.1.1')))); Cpanel_PublicAPI::resetInstance(); $cp = Cpanel_PublicAPI::getInstance($arr); $response = $cp->whm_api(); }
* be stored into the PublicAPI registry (if the registry was not previously * disabled). This Service object will be stored with the name 'default' if * a name was not provided (in the third parameter of the factory() call) */ $cp = Cpanel_PublicAPI::getInstance($masterConfig); /** * Retrieve the Whostmgr Service named 'myserver1', instantiating it as * necesssary */ $my1_whm = Cpanel_PublicAPI::factory('WHM', '', 'myserver1'); $response = $my1_whm->version(); echo "WHM Version for 'myserver1' via named config: {$response->version}\n\n"; $my2_whm = Cpanel_PublicAPI::factory('WHM', '', 'myserver2'); $response = $my2_whm->version(); echo "WHM Version for 'myserver2' via named config: {$response->version}\n\n"; $default_whm = Cpanel_PublicAPI::factory('WHM'); $response = $default_whm->version(); echo "WHM Version for unnamed config data associated with WHM Services: {$response->version}\n\n"; /** * Just as a side note and example. Any Service level config can be passed * to the constructor of a Service_* library class. This method of * instantiation will not store the new object into a PublicAPI registry. */ $whmCfg = $services['whm']['myserver2']; $xmlapi2 = new Cpanel_Service_WHM($whmCfg); $response = $xmlapi2->version(); echo "WHM Version for 'myserver2' via direct init: {$response->version}\n\n"; } catch (Exception $e) { echo $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; } echo "EOF: You've successfully ended the " . basename(__FILE__) . " script.\n";
* class. However, the PublicAPI client is designed to honor to the * conventions of it's sibling Perl client Cpanel::PublicAPI which offers * a specific, new set of query methods. * * This methods are: * * whm_api($function $func_args) * - For WHM (native XML-API) functions against the WHM Service * * * cpanel_api1_query($service, $mod_func_user_args, $func_args) * - For API1 queries against cPanel or WHM Service * * * cpanel_api2_query($service, $mod_func_user_args, $func_args) * - For API2 queries against cPanel or WHM Service * * api_request($service, $url, $HTTP_REQUEST_TYPE, $formdata, $customHeaders ); * - For making a direct URL query against a cPane or WHM Service UI * * All of these methods are supported directly in the PublicAPI client. * * An example (in it's entirety, and shortest notation) of the previous * "version" request is as follows: */ require_once realpath(dirname(__FILE__) . '/../Util/Autoload.php'); $cpCfg = array('cpanel' => array('service' => array('whm' => array('host' => '10.1.4.191', 'user' => 'root', 'password' => 'rootsecret')))); $cp = Cpanel_PublicAPI::getInstance($cpCfg); $response = $cp->whm_api('version'); echo "WHM Version: {$response->version}\n"; } catch (Exception $e) { echo $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; } echo "EOF: You've successfully ended the " . basename(__FILE__) . " script.\n";
* since a master config will not change during the execution of any given * script using the PublicAPI client. * -- DO NOT use the following static function in production code! -- */ Cpanel_PublicAPI::resetInstance(); /** * Getting a cPanel Service object and invoking the api2_request method on * the cPanel Service object itself. The preferred method is to use the * cpanel_api{n}_request() method available in the PublicAPI interface, but * this is here for demonstration purposes of the Service available in the * cPanel library. * * We use the cPanel Service's accessor methods to set initialization * variables. Authenticating as the user. */ $cpanel = Cpanel_PublicAPI::factory('cPanel'); $cpanel->setUser('dave')->setPassword('dsecret!')->setHost('10.1.4.191'); $service = 'cpanel'; $queryMF = array('module' => 'PHPINI', 'function' => 'getoptions', 'user' => 'dave'); $queryArgs = array('dirlist' => 'allow_url_fopen'); $response = $cpanel->api2_request($service, $queryMF, $queryArgs); echo "API2 response for {$queryMF['module']}::{$queryMF['function']} 'dirlist={$queryArgs['dirlist']}'\n"; foreach ($response->cpanelresult->data as $dataset) { foreach ($dataset as $key => $value) { echo "\t{$key}: {$value}\n"; } } echo "\n"; } catch (Exception $e) { echo $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; }