.pdf)
WHM Plugins
Whostmgr::XMLUI::cPanel module allows you to execute cPanel API 1 and API 2 functions natively from within your Perl applications, namely WHM Plugins written in Perl.
Whostmgr::XMLUI::cPanel module was designed for use by WHM and is provided for use in Perl processes spawned by WHM. If your Perl application is spawned from a cPanel process, do not use this module. You should use the LiveAPI system instead.
You can use this module in standalone Perl applications and scripts; however, we strongly discourage you from doing so. If you plan to use this module in a standalone application, 3 stipulations apply: REMOTE_USER environment variable must be defined and set to either: | Description | Executes a cPanel API call. |
|---|---|
| Return | mixed — • When called in a list context, the function will return an array. The first element in the array will be the length of the API call's output. The second element will be the API function's output in string format.
|
| Example | cpanel_exec ($OPTSref, $socket, $has_root, $cfg) |
Input Parameters:
OPTSref(hashref) — The API function to execute and any relevant parameters. This is the only required parameter. This hash must be formatted as though it were wrapped in a JSON or XML API call.
- A username key/value pair must be passed in the
OPTSrefhash reference. The username must be a valid cPanel account or root. The username passed here determines under which user the API 1 or API 2 call will run. The username must be passed using one of the following keys:
cpanel_jsonapi_usercpanel_xmlapi_usersocket(string) deprecated — A file handle. This parameter is deprecated; passundef.has_root(boolean) — Whether the user included in theOPTSrefhash reference has root access.
- A value of
1indicates that the user has root access, allowing access checks to be skipped.- If the
has_rootparameter is set to0(false), security checks are performed. This requires theREMOTE_USERenvironment variable to contain either:
- The username passed in the
OPTSrefhash reference, or- The account owner's username in the
OPTSrefhash referencecfg(hashref) — Output configuration. Accepted values:
json(boolean) — When enabled, the response will be returned in JSON instead of XML.
$has_root input argument $has_root parameter governs how access control is performed in cpanel_exec() prior to invoking the API call. This compares the authenticated user, referenced by the REMOTE_USER environment variable, and the cPanel user being affected (i.e., the value set in cpanel_jsonapi_user or cpanel_xmlapi_user).
1 to $has_root grants the authenticated user access to modify the affected cPanel accounts, without performing any validation.
0, validation is performed to verify that the authenticated user owns the affected cPanel account or is the account in question. 1 or undef should be specified to $has_root.
undef causes the Whostmgr::ACLS module to load. The $has_root parameter is then assigned the appropriate value based on the all ACL for REMOTE_USER.
$has_root with the appropriate level of trust, or simply pass undef. Hardcoding a value of 1 could grant an unreasonably high level of trust to an authenticated user.
#!/usr/bin/perl
# cpanel - example/Whostmgr-XMLUI-cPanel.pl Copyright(c) 2012 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use Test::More tests => 8;
use strict;
use warnings;
use Whostmgr::XMLUI::cPanel ();
use Cpanel::JSON ();
use XML::Simple ();
#The following block executes the API 1 function Cgi::phpmyadminlink and produces XML output.
{
my $form_ref = {
'cpanel_xmlapi_module' => 'Cgi',
'cpanel_xmlapi_func' => 'phpmyadminlink',
'cpanel_xmlapi_apiversion' => 1,
'cpanel_xmlapi_user' => 'cpanel',
};
my ( $serialized_results_length, $serialized_results_ref ) = Whostmgr::XMLUI::cPanel::cpanel_exec( $form_ref, undef, 1 );
ok( $serialized_results_length, "Whostmgr::XMLUI::cPanel::cpanel_exec returns a result" );
my $ref = XML::Simple::XMLin($serialized_results_ref);
is( $ref->{'data'}->{'result'}, '/3rdparty/phpMyAdmin/index.php', "Fast XML API 1 returns a phpmyadmin link" );
}
#The following block executes the API 1 function Cgi::phpmyadminlink and produces JSON output.
{
my $form_ref = {
'cpanel_jsonapi_module' => 'Cgi',
'cpanel_jsonapi_func' => 'phpmyadminlink',
'cpanel_jsonapi_apiversion' => 1,
'cpanel_jsonapi_user' => 'cpanel',
};
my ( $serialized_results_length, $serialized_results_ref ) = Whostmgr::XMLUI::cPanel::cpanel_exec( $form_ref, undef, 1, { 'json' => 1 } );
ok( $serialized_results_length, "Whostmgr::XMLUI::cPanel::cpanel_exec returns a result" );
my $ref = Cpanel::JSON::Load($$serialized_results_ref);
is( $ref->{'data'}->{'result'}, 'cpanel', "Fast JSON API1 returns a phpmyadmin link" );
}
#The following block executes the API 2 function Statsbar::stat and produces XML output.
{
my $form_ref = {
'cpanel_xmlapi_module' => 'StatsBar',
'cpanel_xmlapi_func' => 'stat',
'display' => 'perlversion',
'cpanel_xmlapi_apiversion' => 2,
'cpanel_xmlapi_user' => 'cpanel',
};
my ( $serialized_results_length, $serialized_results_ref ) = Whostmgr::XMLUI::cPanel::cpanel_exec( $form_ref, undef, 1 );
ok( $serialized_results_length, "Whostmgr::XMLUI::cPanel::cpanel_exec returns a result" );
my $ref = XML::Simple::XMLin($serialized_results_ref);
is ($ref->{'data'}->{'id'}, 'perlversion', 'XML response contains proper "id" node');
like( $ref->{'data'}->{'value'}, qr/5[\.\d]*/, 'XML response for "value" node contains the expected Perl version string');
}
#The following block executes the API 2 function Statsbar::stat and produces JSON output.
{
my $form_ref = {
'cpanel_xmlapi_module' => 'StatsBar',
'cpanel_jsonapi_func' => 'stat',
'display' => 'perlversion',
'cpanel_jsonapi_apiversion' => 2,
'cpanel_jsonapi_user' => 'cpanel',
};
my ( $serialized_results_length, $serialized_results_ref ) = Whostmgr::XMLUI::cPanel::cpanel_exec( $form_ref, undef, 1, { 'json' => 1 } );
ok( $serialized_results_length, "Whostmgr::XMLUI::cPanel::cpanel_exec returns a result" );
my $ref = Cpanel::JSON::Load($$serialized_results_ref);
is ($ref->{'data'}->{'id'}, 'perlversion', 'JSON response contains proper "id" node');
like( $ref->{'data'}->{'value'}, qr/5[\.\d]*/, 'JSON response for "value" node contains the expected Perl version string');
}