Difference: LivePHP (13 vs. 14)

Revision 1413 Jan 2012 - Main.JustinSchaefer

Line: 1 to 1
Changed:
<
<
META TOPICPARENT name="Sandbox.WebHome"
>
>
META TOPICPARENT name="LiveAPI"
 

The LiveAPI PHP Class

Changed:
<
<
The LiveAPI PHP Class is a PHP environment you can utilize within cPanel. This PHP environment provides PHP scripts with a local socket to the cPanel binary, cpsrvd. This allows your script to make API1 and API2 calls to the local machine via the socket.
>
>
For cPanel & WHM 11.30
 
Changed:
<
<
/usr/local/cpanel/php/cpanel.php is an out-of-the-box PHP class that provides common elements for this integration . API calls are executed by the system's user and may fail if asked to perform a task for which the user has insufficient permissions. The same rules apply to this method as when using API tags or API calls made via our XML API
>
>

Introduction

The LiveAPI PHP class is a PHP environment you can utilize within cPanel. This PHP environment provides PHP scripts with a local socket to the cPanel binary, cpsrvd.

This method prevents you from having to:

  • Embed cPanel tags.
  • Use cpphp or phpcp tags.
  • Make remote API calls via XML or JSON APIs, as these calls require greater network overhead.
 
Changed:
<
<
To begin, you must ensure that you meet the following 3 criteria:
  1. You must include /usr/local/cpanel/php/cpanel.php and instantiate the CPANEL object.
>
>
/usr/local/cpanel/php/cpanel.php is a PHP class that provides common elements for this integration.

API calls are executed by the system's user and will fail if the user has insufficient permissions. The same rules apply to LiveAPI as apply to API tags or API calls made through the XML or JSON APIs.

To begin, you must ensure that your application meets the following 3 criteria:

  1. You must include usr/local/cpanel/php/cpanel.php and instantiate the CPANEL object.
 
  1. Your PHP scripts' file names must end in either .livephp or .live.php
Changed:
<
<
  1. You must place your PHP script in /usr/local/cpanel/base/frontend/$THEME You may also use a symlink.
    • note Note: You will need to replace $THEME in the example above with your theme. (e.g. x3 in most cases)
>
>
  1. You must place your PHP script in /usr/local/cpanel/base/frontend/$THEME. Symlinks are permitted.
    • note Note: You will need to replace $THEME in the example above with the name of your theme (e.g., x3 in most cases).
 
Changed:
<
<
>
>
This document is a transcription of the PHP DocBlock comments within the cpanel.php class. PHP developers will find more exact information within the comment blocks.

Synopsis

<-- SyntaxHighlightingPlugin -->
require_once "/usr/local/cpanel/php/cpanel.php";
$cpanel = new CPANEL();
$live_api_result_array = $cpanel->api2( $module, $function, array( 'key1' => 'value1', 'key2' => 'value2') );
<-- end SyntaxHighlightingPlugin -->
 

The CPANEL PHP Class

Line: 21 to 44
 
Class Description Class Variables Class Methods
Changed:
<
<
This class provides an interface to the local system's cPanel binary. It allows you to make API calls and use cPanel tags as described in Developer Resources. Your can make both API1 and API2 calls using this class.
>
>
This class provides an interface to the local system's cPanel binary. It allows you to make API calls from a PHP object and avoid the use of cPanel tag markup. Both API 1 and API 2 are available through this class.
 
Changed:
<
<
  • Version: 0.1 — You can find this class at /usr/local/cpanel/php/cpanel.php
>
>
  • Version: 2.1 — You can find this class at /usr/local/cpanel/php/cpanel.php
 
  • Copyright: cPanel, Inc.
  • License: Modified BSD
Changed:
<
<
  • noteNote: This class depends on /usr/local/cpanel/php/xml.php.
>
>
  • noteNote: Old versions of this class, as well as versions of cPanel prior to 11.28, depend on /usr/local/cpanel/php/xml.php for backward compatibility.
 

Class Variable Summary

Class Description Class Variables Class Methods
Added:
>
>
Environment Variables
  • There are two environment variables that can affect the behavior of this class at object instantiation:
    • LIVEPHP_DEBUG_LEVEL — Enable or disable debugging (can be overridden by accessor methods set_debug()).
    • LIVEPHP_DEBUG_LOG — The absolute path and filename for a debug log. By default, a random log will be created in user's home directory (e.g., ~/.cpanel/livephp.log.$rand ) .
 Public Variables
Changed:
<
<
  • n/a
>
>
  • N/A
 
Deleted:
<
<
 
Changed:
<
<
Private Methods
  • Private methods are not itemized here. See cpanel.php
>
>
Private Variables
  • Private variables are not itemized here. See cpanel.php
 
Deleted:
<
<
note Note: You may pass $TODO private variables here.
 

Class Method Summary

Line: 50 to 76
 
Public Methods
Changed:
<
<
  • void CPANEL ()
  • array api ( string $reqtype, string $version, string $module, string $func, array $args)
>
>
  • object __construct ()
  • array api ( string $reqtype, string $version, string $module, string $func, mixed $args)
 
  • array api1 (string $module, string $func, array $args)
  • array api2 (string $module, string $func, array $args)
Changed:
<
<
>
>
 
Changed:
<
<
  • array exec (string $code)
  • array fetch (string $code)
>
>
 

Line: 72 to 102
 

Class Method Details

Changed:
<
<

  CPANEL =(line 36)

>
>

__construct()

 
Changed:
<
<
Description Get user scope socket handle to cPanel binary. (e.g. Constructor)
Return Nothing.
Access Public
Example CPANEL ()
>
>
Description Instantiates the CPANEL object. Responsible for establishing communication with the cPanel API engine.
Return object — An object to perform API queries.
Access Public
Example new CPANEL ()
 
Changed:
<
<

  api =(line 99)

>
>

api()

 
Changed:
<
<
Description Queries the cpsrvd binary for information.
Return A PHP array representation of an XML response.
Access Public
Output Type Array
Example api ($reqtype, $version, $module, $func, $args)
>
>
note Note: The api1(), api2(), and cpanel*() methods are preferred, as they call this method internally.

Description Executes a cPanel API call.
Return array — Returned response in an array data structure.
Access Public
Example api ($reqtype, $version, $module, $func, $args)
 
Parameters:
  • $reqtype (string) — The type of request. This value is usually exec.
  • $version (integer) — The API version. This value is either 1 or 2.
Changed:
<
<
  • $module (string) — The name of the module you wish to use.
  • $func (string) — The name of the function you wish to use.
  • $args (array) — Any arguments you wish to pass to the API function. API 1 arguments are in ordered array keys. API 2 arguments are passed as literal array keys.
>
>
  • $module (string) — The API 1 or API 2 module to execute.
  • $func (string) — The API 1 or API 2 function to execute.
  • $args (mixed) — Any arguments you wish to pass to the API function. API 1 arguments must be in an ordered array, while API 2 arguments are an associative array; string for non exec $reqtypes.
 

Changed:
<
<

  api1 =(line 75)

>
>

api1()

 
Description Executes an API1 query.
Changed:
<
<
Return A PHP array representation of an XML response
Access Public
Output Type Array
Example api1 ($module, $func, $args)
>
>
Return array — Returned response from the API1 function.
Access Public
Example api1 ($module, $func, $args)
 
Parameters:
Changed:
<
<
  • $module (string) — The module's namespace. (e.g. SSL)
  • $func (string) — The function you wish to call. (e.g. gencrt)
  • $args (array) — The arguments you wish to pass to the function. Keys must be ordered when using an API 1 function. (e.g. array(0=>'funcArg1', 1=>'funcArg2'))
>
>
  • $module (string) — The module's namespace (e.g., SSL).
  • $func (string) — The function you wish to call (e.g., showcrt).
  • $args (array) — The arguments you wish to pass to the function (e.g., array(0=>'foo.tld', 1=>0) ).
    • note Note: Arguments should be passed in an ordinal array (e.g., array(0=>'funcArg1', 1=>'funcArg2')).
 

Changed:
<
<

  api2 =(line 79)

>
>

api2()

 
Description Executes an API2 query.
Changed:
<
<
Return A PHP array representation of an XML response.
Access Public
Output Type Array
Example api2 ($module, $func, $args)
>
>
Return array — Returned response from the API2 function.
Access Public
Example api2 ($module, $func, $args)
 
Parameters:
Changed:
<
<
  • $module (string) — The module's namespace. (e.g. SSL)
  • $func (string) — The function you wish to call. (e.g. gencrt)
  • $args (array) — The arguments you wish to pass to the function.
    • note Note: Array keys must be literal.
>
>
  • $module (string) — The module's namespace (e.g., SSL).
  • $func (string) — The function you wish to call (e.g., listsslitems).
  • $args (array) — The arguments you wish to pass to the function (e.g., array('domains'=>'foo.tld|bar.tld', 'items'=>'key|crt') ).
    • note Note: Arguments should be passed in an associative array.
 

Changed:
<
<

  cpanelfeature =(line 87)

>
>

cpanelfeature()

 
Changed:
<
<
Description Functions as a wrapper for cPanel feature tags.
Return A PHP array representation of an XML response.
Access Public
Output Type Boolean
Example cpanelfeature ($expression)
>
>
Description This method functions as a wrapper for cPanel feature tags.
Return boolean — Whether the current cPanel account has access to the feature.
Access Public
Example cpanelfeature ($feature)
 
Parameters:
Changed:
<
<
  • $expression (string) — A logic expression. For more information visit our documentation here.
>
>
  • $feature (string) — A feature name or logical expression related to a cPanel feature. For more information, visit our documentation about using cPanel feature tags.
 

Changed:
<
<

  cpanelif =(line 83)

>
>

cpanelif()

 
Changed:
<
<
Description This functions as a wrapper for the cpanelif tag.
Return The value of the XML response's result node.
Access Public
Output Type Boolean
Example cpanelif ($expression)
>
>
Description This method functions as a wrapper for the cpanelif tag.
Return boolean — Whether the $code expression evaluates as true or false.
Access Public
Example cpanelif ($code)
 
Parameters:
Changed:
<
<
  • $expression (string) — A logic expression. For more information visit our documentation here.
>
>
  • $code (string) — A cPvar or logical test condition. For more information, please visit our documentation about conditionals.
 

Changed:
<
<

  cpanellangprint =(line 95)

>
>

cpanellangprint()

 
Changed:
<
<
Description This functions as a wrapper for the cPanel langprint function.
Return The value of an XML response's result node.
Access Public
Output Type Boolean
Example cpanellangprint ($textkey)
>
>
Description This method functions as a wrapper for the cPanel langprint function.
Return string — Translated version of the requested language key.
Access Public
Example cpanellangprint ($key)
 
Parameters:
Changed:
<
<
  • $textkey (string) — A reference to a locale (language).
>
>
  • $key (string) — A language key.
 

Changed:
<
<

  cpanelprint =(line 91)

>
>

cpanelprint()

 
Changed:
<
<
Description This functions as a wrapper for the cPanel print function.
Return The value of an XML response's result node.
Access Public
Output Type Boolean
Example cpanelprint ($expression)
>
>
Description This method functions as a wrapper for the cPanel print function.
Return string — The value of the queried ExpVar or text.
Access Public
Example cpanelprint ()$var)
 
Parameters:
Changed:
<
<
  • $expression (string) — A logic expression. For more information visit our documentation here.
>
>
  • $var (string) — An expression that may contain a ExpVar or text. For more information, please visit our ExpVar reference document.
 

Changed:
<
<

  end =(line 121)

>
>

end()

 
Description Deconstructs the class object.
Changed:
<
<
Access Public
Output Type Void
Example end ()
>
>
Note void — This method's use is not required. We provided this method for backwards compatibility with older LivePHP scripts.
Access Public
Example end ()
 
Changed:
<
<

  exec =(line 59)

>
>

exec()

 
Description Executes a cPanel tag. Calling exec directly is discouraged.
Changed:
<
<
Return A PHP array representation of an XML response.
Access Public
Output Type Array
Example exec ($code)
>
>
Return array — Returned response in an array data structure.
Access Public
Example exec ($code [,$skip_return])
 
Parameters:
  • $code (string) — A cPanel tag that contains an API, ExpVar, or a Dynamic UI construct.
Added:
>
>
  • $skip_return (boolean) — (optional) If set to true, this method will return nothing.
 

Changed:
<
<

  fetch =(line 52)

>
>

fetch()

 
Changed:
<
<
Description This functions as a wrapper for the cPanel API print function. This returns the entire XML response as a PHP array. If you only want the data result, use cpanelprint.
Return A PHP array representation of an XML response.
Access Public
Output Type Array
Example fetch ($code)
>
>
Description This method functions as a wrapper for the cPanel API print function. It returns the entire response as a PHP array. If you only want the data result, use cpanelprint.
Return array — An array response containing the evaluated value of $var.
Access Public
Example fetch ($var)
 
Parameters:
Changed:
<
<
  • $code (string) — A cPanel tag that contains an API, ExpVar, or a Dynamic UI construct.
>
>
  • $var (string) — An expression that may contain an ExpVar or text. For more information, please visit our ExpVar reference document.
 
Added:
>
>

get_debug_log()

Description Gets the filename of the debug log currently in use.
Return string — The filename of the LivePHP debug log.
Access Public
Example get_debug_log ()

get_debug_level()

Description Gets the current debug level.
Return integer — The current debug level.
Access Public
Example get_debug_level ()

set_debug()

Description Sets debugging mode to a specific level.
Return void
Access Public
Example set_debug ($debug_level)

Parameters:
  • $debug_level (integer) — Desired debug level.
    • 0 — disable logging. 0 is the default value.
    • 1 — Write socket transactions to the log file.

get_result()

Description A convenience method for fetching the data result node of the last call.
Return mixed — The contents within the ['cpanelresult']['data']['result'] node of the last returned response. If the previous call was an API 1 function, a string is returned. If the last call was an API 2 function, an array of associative arrays is returned.
Access Public
Example get_result ()

Licensing Information

Version 2.1
Copyright cPanel, Inc.
License Modified BSD