* Please look at the Introduction_to_PublicAPI.php for more details.
 */
try {
    require_once realpath(dirname(__FILE__) . '/../Util/Autoload.php');
    /**
     * The follow function prepares an environment that emulates cpaneld that is
     * present when loading *.live.php scripts (e.g., the LivePHP environment).
     * This is ONLY FOR DEMO PURPOSES.  CAVEAT EMPTOR: The provided mock server
     * is for use by this example script only.  It is not supported in any way
     * by the cPanel PHP Library, or the PublicAPI project.
     */
    startExampleMockServer();
    ////////////////////////////////////////////////////////////////////////////
    // Prepare PublicAPI and provision a LivePHP Service object
    $cp = Cpanel_PublicAPI::getInstance();
    $cpanel = Cpanel_PublicAPI::factory('LivePHP');
    /**
     * Note about the LivePHP Service
     * 
     * The LivePHP Service is intended for use on a cPanel system in accordance
     * with the LivePHP environment.  It has not meaning outside that context.
     * 
     * Pleas see the cPanel documentation for more information on LivePHP
     * http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LivePHP
     * 
     * 
     * Technical Detail and Caveat:
     * The LivePHP Service is actually a cPanel Service object with a special
     * backend adapter, Cpanel_Service_Adapter_Liveapi.  This type of
     * inheritance allow for legacy methods of LivePHP to be available while
     * also providing access for the respective PublicAPI interface methods,
Пример #2
0
 /**
  * Verify 'WHM' object is returned
  */
 public function testFactoryCanReturnWHM()
 {
     Cpanel_PublicAPI::resetInstance();
     $objtype = 'WHM';
     $optsArray = $this->getSampleConfig();
     $name = 'myserver1';
     $obj = Cpanel_PublicAPI::factory($objtype, $optsArray, $name);
     $this->assertInstanceOf("Cpanel_Service_{$objtype}", $obj);
 }
  * You can get a specific cPanel & WHM services via the factory method
  * OR you can instantiate the service directly from a service class
  * 
  * In either case you'll want to pass in some initialization data, here's a
  * basic config array
  */
 $whmCfg = array('host' => '10.1.4.191', 'user' => 'root', 'password' => 'rootsecret');
 /**
  * The following lines of code illustrate both ways of obtaining a Service
  * object (mentioned earlier)
  * 
  * The first example illustrates using the PublicAPI factory method as a
  * static call.  Alteratively, you can invoke factory() from the PublicAPI
  * object returned above.
  */
 $xmlapi = Cpanel_PublicAPI::factory('WHM', $whmCfg);
 $xmlapi = $cp->factory('WHM', $whmCfg);
 /**
  * This next example illustrates using the cPanel library directly.
  * 
  * Note: this method does NOT register the Service object in the PublicAPI
  * registry, use the above methods if that is the desired affect
  */
 $xmlapi = new Cpanel_Service_WHM($whmCfg);
 /**
  * If you did not pass a configuration earlier, wish to change on of those
  * values or wish to change a default value for the Service, you can use
  * various accessor methods.  Any value can be set by calling 
  * "setKey(value)".  Additionally,  most of the accessors available in the
  * legacy XML-API client class are available too.
  * 
     * 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";
     *  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";
}