<?php
/**
* cPanel XMLAPI Client Class
*
* This class allows for easy interaction with cPanel's XML-API allow functions within the XML-API to be called
* by calling funcions within this class
*
* LICENSE:
*
* Copyright (c) 2009, cPanel, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the cPanel, Inc. nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Version: 1.0.7
* Last updated: 10 November 2010
*
* Changes
*
* 1.0.7:
* Corrected typo for setrellerlimits where xml_query incorrectly called xml-api's setresellerips
*
* 1.0.6:
* 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
* Relocated exmaple script to Example subdirectory
* Modified example scripts to take remote server IP and root password from environment variables REMOTE_HOST and REMOTE_PASSWORD, respectively
* Created subdirectory Tests for PHPUnit tests
* Add PHPUnit test BasicParseTest.php
*
* 1.0.5:
* fix bug where api1_query and api2_query would not return JSON data
*
* 1.0.4:
* set_port will now convert non-int values to ints
*
* 1.0.3:
* Fixed issue with set_auth_type using incorrect logic for determining acceptable auth types
* Suppress non-UTF8 encoding when using curl
*
* 1.0.2:
* Increased curl buffer size to 128kb from 16kb
* Fix double encoding issue in terminateresellers()
*
* 1.0.1:
* Fixed use of wrong variable name in curl error checking
* adjust park() to use api2 rather than API1
*
* 1.0
* Added in 11.25 functions
* Changed the constructor to allow for either the "DEFINE" config setting method or using parameters
* Removed used of the gui setting
* Added fopen support
* Added auto detection for fopen or curl (uses curl by default)
* Added ability to return in multiple formats: associative array, simplexml, xml, json
* Added PHP Documentor documentation for all necessary functions
* Changed submission from GET to POST
*
*
* @copyright 2009 cPanel, Inc
* @license http://sdk.cpanel.net/license/bsd.html
* @version 1.0.6
* @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
* @since File available since release 0.1
**/
/**
* The base XML-API class
*
* The XML-API class allows for easy execution of cPanel XML-API calls. The goal of this project is to create
* an open source library that can be used for multiple types of applications. This class relies on PHP5 compiled
* with both curl and simplexml support.
*
* Making Calls with this class are done in the following steps:
*
* 1.) Instaniating the class:
* $xmlapi = new xmlapi($host);
*
* 2.) Setting access credentials within the class via either set_password or set_hash:
* $xmlapi->set_hash("username", $accessHash);
* $xmlapi->set_password("username", "password");
*
* 3.) Execute a function
* $xmlapi->listaccts();
*
* @category Cpanel
* @package xmlapi
* @copyright 2009 cPanel, Inc.
* @license http://sdk.cpanel.net/license/bsd.html
* @version Release: 1.0.6
* @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
* @since Class available since release 0.1
**/
## Defines only available in PHP 5, created for PHP4
if(!defined('PHP_URL_SCHEME')) define('PHP_URL_SCHEME', 1);
if(!defined('PHP_URL_HOST')) define('PHP_URL_HOST', 2);
if(!defined('PHP_URL_PORT')) define('PHP_URL_PORT', 3);
if(!defined('PHP_URL_USER')) define('PHP_URL_USER', 4);
if(!defined('PHP_URL_PASS')) define('PHP_URL_PASS', 5);
if(!defined('PHP_URL_PATH')) define('PHP_URL_PATH', 6);
if(!defined('PHP_URL_QUERY')) define('PHP_URL_QUERY', 7);
if(!defined('PHP_URL_FRAGMENT')) define('PHP_URL_FRAGMENT', 8);
class CpHandle_CpanelLibs_Xmlapi
{
private $debug = false;
// The host to connect to
private $host = '127.0.0.1';
// the port to connect to
private $port = '2087';
// should be the literal strings http or https
private $protocol = 'https';
// output that should be given by the xml-api
private $output = 'simplexml';
// literal strings hash or password
private $auth_type = null;
// the actual password or hash
private $auth = null;
// username to authenticate as
private $user = null;
// The HTTP Client to use
private $http_client = 'curl';
// The array error message
private $aError = array();
public function __construct($host=null, $user=null, $password=null)
{
if ( ( $user != null ) && ( strlen( $user ) < 9 ) ) {
$this->user = $user;
}
if ($password != null ) {
$this->set_password($password);
}
if ( $host != null ) {
$this->set_host($host);
}
if ( function_exists('curl_setopt') ) {
$this->http_client = "curl";
} else if (function_exists("socket_create")) {
$this->http_client = "socket_create";
} else if ( ini_get('allow_url_fopen') ) {
$this->http_client = "fopen";
} else {
/// Error: Cannot usage function curl and fopen
return $this->raiseError('allow_url_fopen and curl are neither available in this PHP configuration');
}
}
public function getErrorMessage()
{
if (count($this->aError) > 0) {
return SGL::raiseError(join('<br />', $this->aError));
} else {
return true;
}
}
private function raiseError($message, $filter=false, $aParams=array())
{
return ($filter === false )
? SGL::raiseError(RvsLibs_String::translate($message))
: SGL::raiseError(RvsLibs_String::translate($message, $filter, $aParams));
}
/**
* Get the host being connected to
*
* This function will return the host being connected to
* @return string host
* @see set_host()
*/
public function get_host()
{
return $this->host;
}
/**
* Set the host to query
*
* Setting this will set the host to be queried
* @param string $host The host to query
* @see get_host()
*/
public function set_host($host)
{
$this->host = $host;
}
/**
* Get the port to connect to
*
* This will return which port the class is connecting to
* @return int $port
* @see set_port()
*/
public function get_port()
{
return $this->port;
}
/**
* Set the port to connect to
*
* This will allow a user to define which port needs to be connected to.
* The default port set within the class is 2087 (WHM-SSL) however other ports are optional
* this function will automatically set the protocol to http if the port is equal to:
* - 2082
* - 2086
* - 2095
* - 80
* @param int $port the port to connect to
* @see set_protocol()
* @see get_port()
*/
public function set_port($port)
{
if ( !is_int( $port ) ) {
$port = intval($port);
}
if ( $port < 1 || $port > 65535 ) {
return $this->raiseError('non integer or negative integer passed to set_port');
}
// Account for ports that are non-ssl
if ( $port == '2086' || $port == '2082' || $port == '80' || $port == '2095' ) {
$this->set_protocol('http');
}
$this->port = $port;
}
/**
* Return the protocol being used to query
*
* This will return the protocol being connected to
* @return string
* @see set_protocol()
*/
public function get_protocol()
{
return $this->protocol;
}
/**
* Set the protocol to use to query
*
* This will allow you to set the protocol to query cpsrvd with. The only to acceptable values
* to be passed to this function are 'http' or 'https'. Anything else will cause the class to throw
* an Exception.
* @param string $proto the protocol to use to connect to cpsrvd
* @see get_protocol()
*/
public function set_protocol($proto)
{
if ( $proto != 'https' && $proto != 'http' ) {
return $this->raiseError('https and http are the only protocols that can be passed to set_protocol');
}
$this->protocol = $proto;
return true;
}
/**
* Return what format calls with be returned in
*
* This function will return the currently set output format
* @see set_output()
* @return string
*/
public function get_output()
{
return $this->output;
}
/**
* Set the output format for call functions
*
* This class is capable of returning data in numerous formats including:
* - json
* - xml
* - {@link http://php.net/simplexml SimpleXML}
* - {@link http://us.php.net/manual/en/language.types.array.php Associative Arrays}
*
* These can be set by passing this class any of the following values:
* - json - return JSON string
* - xml - return XML string
* - simplexml - return SimpleXML object
* - array - Return an associative array
*
* Passing any value other than these to this class will cause an Exception to be thrown.
* @param string $output the output type to be set
* @see get_output()
*/
public function set_output($output)
{
if ( $output != 'json' && $output != 'xml' && $output != 'array' && $output != 'simplexml' ) {
return $this->raiseError('json, xml, array and simplexml are the only allowed values for set_output');
}
$this->output = $output;
return true;
}
/**
* Return the auth_type being used
*
* This function will return a string containing the auth type in use
* @return string auth type
* @see set_auth_type()
*/
public function get_auth_type()
{
return $this->auth_type;
}
/**
* Set the auth type
*
* This class is capable of authenticating with both hash auth and password auth
* This function will allow you to manually set which auth_type you are using.
*
* the only accepted parameters for this function are "hash" and "pass" anything else will cuase
* an exception to be thrown
*
* @see set_password()
* @see set_hash()
* @see get_auth_type()
* @param string auth_type the auth type to be set
*/
public function set_auth_type($auth_type)
{
if ( $auth_type != 'hash' && $auth_type != 'pass') {
return $this->raiseError('the only two allowable auth types are hash and path');
}
$this->auth_type = $auth_type;
}
/**
* Set the password to be autenticated with
*
* This will set the password to be authenticated with, the auth_type will be automatically adjusted
* when this function is used
*
* @param string $pass the password to authenticate with
* @see set_hash()
* @see set_auth_type()
* @see set_user()
*/
public function set_password($pass)
{
$this->auth_type = 'pass';
$this->auth = $pass;
}
/**
* Set the hash to authenticate with
*
* This will set the hash to authenticate with, the auth_type will automatically be set when this function
* is used. This function will automatically strip the newlines from the hash.
* @param string $hash the hash to autenticate with
* @see set_password()
* @see set_auth_type()
* @see set_user()
*/
public function set_hash($hash)
{
$this->auth_type = 'hash';
$this->auth = preg_replace("/(\n|\r|\s)/", '', $hash);
}
public function autodetech_set_hashorpass($hassOrPass)
{
if ( $this->auth_type != 'hash' && $this->auth_type != 'pass')
return $this->raiseError('Please set auth_type');
if ($this->auth_type == 'hash') {
$this->set_hash($hassOrPass);
} else {
$this->set_password($hassOrPass);
}
}
/**
* Return the user being used for authtication
*
* This will return the username being authenticated against.
*
* @return string
*/
public function get_user()
{
return $this->user;
}
/**
* Set the user to authenticate against
*
* This will set the user being authenticated against.
* @param string $user username
* @see set_password()
* @see set_hash()
* @see get_user()
*/
public function set_user($user)
{
$this->user = $user;
}
/**
* Set the user and hash to be used for authentication
*
* This function will allow one to set the user AND hash to be authenticated with
*
* @param string $user username
* @param string $hash WHM Access Hash
* @see set_hash()
* @see set_user()
*/
public function hash_auth($user, $hash)
{
$this->set_hash( $hash );
$this->set_user( $user );
}
/**
* Set the user and password to be used for authentication
*
* This function will allow one to set the user AND password to be authenticated with
* @param string $user username
* @param string $pass password
* @see set_pass()
* @see set_user()
*/
public function password_auth($user, $pass)
{
$this->set_password( $pass );
$this->set_user( $user );
}
/**
* Return XML format
*
* this function will cause call functions to return XML format, this is the same as doing:
* set_output('xml')
*
* @see set_output()
*/
public function return_xml()
{
$this->set_output('xml');
}
/**
* Return simplexml format
*
* this function will cause all call functions to return simplexml format, this is the same as doing:
* set_output('simplexml')
*
* @see set_output()
*/
public function return_object()
{
$this->set_output('simplexml');
}
/**
* Set the HTTP client to use
*
* This class is capable of two types of HTTP Clients:
* - curl
* - fopen
*
* When using allow url fopen the class will use get_file_contents to perform the query
* The only two acceptable parameters for this function are 'curl' and 'fopen'.
* This will default to fopen, however if allow_url_fopen is disabled inside of php.ini
* it will switch to curl
*
* @param string client The http client to use
* @see get_http_client()
*/
public function set_http_client($client)
{
if ( ( $client != 'curl' ) && ( $client != 'fopen' ) && $client != 'socket_create' ) {
return $this->raiseError('Only curl and fopen and allowed http clients socket_create');
}
$this->http_client = $client;
}
/**
* Get the HTTP Client in use
*
* This will return a string containing the HTTP client currently in use
*
* @see set_http_client()
* @return string
*/
public function get_http_client()
{
return $this->http_client;
}
/*
* Query Functions
* --
* This is where the actual calling of the XML-API, building API1 & API2 calls happens
*/
/**
* Perform an XML-API Query
*
* This function will perform an XML-API Query and return the specified output format of the call being made
*
* @param string $function The XML-API call to execute
* @param array $vars An associative array of the parameters to be passed to the XML-API Calls
* @return mixed
*/
public function xmlapi_query($function, $vars=array() )
{
// Check to make sure all the data needed to perform the query is in place
if (!$function) {
return $this->raiseError('xmlapi_query() requires a function to be passed to it');
}
if ($this->user == null ) {
return $this->raiseError('no user has been set');
}
if ($this->auth ==null) {
return $this->raiseError('no authentication information has been set');
}
// Build the query:
$query_type = '/xml-api/';
if ( $this->output == 'json' ) {
$query_type = '/json-api/';
}
$cptoken = (isset($_ENV['cp_security_token']) && $_ENV['cp_security_token']) ? $_ENV['cp_security_token'] : '';
$args = http_build_query($vars, '', '&');
$url = $this->protocol . '://' . $this->host . ':' . $this->port . $cptoken . $query_type . $function;
SGL::logMessage("goto " . $url, PEAR_LOG_DEBUG);
// Set the $auth string
$authstr;
if ( $this->auth_type == 'hash' ) {
$authstr = 'Authorization: WHM ' . $this->user . ':' . $this->auth . "\r\n";
} elseif ($this->auth_type == 'pass' ) {
$authstr = 'Authorization: Basic ' . base64_encode($this->user .':'. $this->auth) . "\r\n";
} else {
return $this->raiseError('invalid auth_type set');
}
// Perform the query (or pass the info to the functions that actually do perform the query)
$response;
if ( $this->http_client == 'curl' ) {
$response = $this->curl_query($url, $args, $authstr);
} else if ( $this->http_client == 'socket_create' ) {
$response = $this->socket_create_query($url, $args, $authstr);
} elseif ( $this->http_client == 'fopen' ) {
$response = $this->fopen_query($url, $args, $authstr);
}
// The only time a response should contain <html> is in the case of authentication error
// cPanel 11.25 fixes this issue, but if <html> is in the response, we'll error out.
if (stristr($response, '<html>') == true) {
if (stristr($response, 'Login Attempt Failed') == true) {
return $this->raiseError("Login Attempt Failed");
}
if (stristr($response, 'action="/login/"') == true) {
return $this->raiseError("Authentication Error");
}
return $this->raiseError('');
}
// perform simplexml transformation (array relies on this)
if ( ($this->output == 'simplexml') || $this->output == 'array') {
$response = simplexml_load_string($response, null, LIBXML_NOERROR | LIBXML_NOWARNING);
if (!$response){
SGL::logMessage("cannot have out data", PEAR_LOG_ERR);
return false;
//return $this->raiseError("Some error message here");
}
}
// perform array tranformation
if ($this->output == 'array') {
$response = $this->unserialize_xml($response);
}
return $response;
}
/**
* Call an API1 function
*
* This function allows you to call API1 from within the XML-API, This allowes a user to peform actions
* such as adding ftp accounts, etc
*
* @param string $user The username of the account to perform API1 actions on
* @param string $module The module of the API1 call to use
* @param string $function The function of the API1 call
* @param array $args The arguments for the API1 function, this should be a non-associative array
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiOne API1 Documentation
*/
public function api1_query($user, $module, $function, $args = array() )
{
if ( !isset($module) || !isset($function) || !isset($user) ) {
return $this->raiseError("api1_query requires that a module and function are passed to it");
}
if (!is_array($args)) {
return $this->raiseError('api1_query requires that it is passed an array as the 4th parameter');
}
$cpuser = 'cpanel_xmlapi_user';
$module_type = 'cpanel_xmlapi_module';
$func_type = 'cpanel_xmlapi_func';
$api_type = 'cpanel_xmlapi_apiversion';
if ( $this->get_output() == 'json' ) {
$cpuser = 'cpanel_jsonapi_user';
$module_type = 'cpanel_jsonapi_module';
$func_type = 'cpanel_jsonapi_func';
$api_type = 'cpanel_jsonapi_apiversion';
}
$call = array(
$cpuser => $user,
$module_type => $module,
$func_type => $function,
$api_type => '1'
);
for ($int = 0; $int < count($args); $int++) {
$call['arg-' . $int] = $args[$int];
}
return $this->xmlapi_query('cpanel', $call);
}
/**
* Call an API2 Function
*
* This function allows you to call an API2 function, this is the modern API for cPanel and should be used in preference over
* API1 when possible
*
* @param string $user The username of the account to perform API2 actions on
* @param string $module The module of the API2 call to use
* @param string $function The function of the API2 call
* @param array $args An associative array containing the arguments for the API2 call
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ApiTwo Legacy API2 Documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiTwo API2 Documentation
*/
public function api2_query($user, $module, $function, $args=array())
{
if (!isset($user) || !isset($module) || !isset($function) ) {
return $this->raiseError("api2_query requires that a username, module and function are passed to it");
}
if (!is_array($args)) {
return $this->raiseError("api2_query requires that an array is passed to it as the 4th parameter");
}
$cpuser = 'cpanel_xmlapi_user';
$module_type = 'cpanel_xmlapi_module';
$func_type = 'cpanel_xmlapi_func';
$api_type = 'cpanel_xmlapi_apiversion';
if ( $this->get_output() == 'json' ) {
$cpuser = 'cpanel_jsonapi_user';
$module_type = 'cpanel_jsonapi_module';
$func_type = 'cpanel_jsonapi_func';
$api_type = 'cpanel_jsonapi_apiversion';
}
$args[$cpuser] = $user;
$args[$module_type] = $module;
$args[$func_type] = $function;
$args[$api_type] = '2';
return $this->xmlapi_query('cpanel', $args);
}
##############################################
# Private Function
##############################################
private function parse_url_compat($url, $component=NULL)
{
if(is_null($component) === true) return parse_url($url);
## PHP 5
if(phpversion() >= 5) return parse_url($url, $component);
## PHP 4
$bits = parse_url($url);
switch($component)
{
case PHP_URL_SCHEME: return $bits['scheme'];
case PHP_URL_HOST: return $bits['host'];
case PHP_URL_PORT: return $bits['port'];
case PHP_URL_USER: return $bits['user'];
case PHP_URL_PASS: return $bits['pass'];
case PHP_URL_PATH: return $bits['path'];
case PHP_URL_QUERY: return $bits['query'];
case PHP_URL_FRAGMENT: return $bits['fragment'];
}
}
/**
* CURL query
* @param <string> $url
* @param <string> $postdata
* @param <string> $authstr
* @return <string>
*/
private function curl_query($url, $postdata, $authstr) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
// Return contents of transfer on curl_exec
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
// Set the URL
curl_setopt($curl, CURLOPT_URL, $url);
// Increase buffer size to avoid "funny output" exception
curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072);
// Pass authentication header
$header[0] =$authstr .
"Referer: {$url}\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($postdata) . "\r\n" . "\r\n";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
if ($result == false) {
return $this->raiseError("curl_exec threw error \"" . curl_error($curl) . "\" for " . $url . "?" . $postdata );
}
curl_close($curl);
return $result;
}
private function socket_create_query($url, $postdata, $authstr)
{
$_host = $this->parse_url_compat($url, PHP_URL_HOST);
if ($_host === false)
return $this->raiseError("Cannot pasrse host from URL %URL");
$_port = 2086;
$_request = $this->parse_url_compat($url, PHP_URL_PATH);
$address = gethostbyname($_host);
$referer = "http://$address:{$_port}";
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
return $this->raiseError("socket_create() failed");
}
if (socket_connect($socket, $address, $_port) === false) {
return $this->raiseError("socket_connect() failed");
}
$aSendHeader = array(
"POST {$_request} HTTP/1.0\r\n",
"Referer: {$referer}\r\n",
"Content-Type: application/x-www-form-urlencoded\r\n",
"Content-Length: " . strlen($postdata) . "\r\n",
"Connection: close\r\n",
$authstr,
"\r\n{$postdata}",
);
/// Send data
foreach ($aSendHeader as $v) {
if (socket_write($socket, $v, RvsLibs_String::strlen($v)) === false) {
return $this->raiseError("socket_write() failed");
}
}
/// Read data response
$page = '';
while (($buf = socket_read($socket, 512)) != false) {
if (socket_last_error($socket)) {
$errormsg = socket_strerror(socket_last_error($socket));
return $this->raiseError("socket failed");
}
$page .= $buf;
}
if ($page == '') {
return $this->raiseError("No response");
}
list ($resHeader, $resData) = preg_split("/\n\n|\r\n\r\n/", $page, 2);
return $resData;
}
/**
* Query by fopen
* @param <string> $url
* @param <string> $postdata
* @param <string> $authstr
* @return <string>
*/
private function fopen_query($url, $postdata, $authstr ) {
if ( !(ini_get('allow_url_fopen') ) ) {
return $this->raiseError('fopen_query called on system without allow_url_fopen enabled in php.ini');
}
$opts = array(
'http' => array(
'allow_self_signed' => true,
'method' => 'POST',
'header' => $authstr .
"Referer: {$url}\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($postdata) . "\r\n" .
"\r\n" . $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
/*
* Convert simplexml to associative arrays
*
* This function will convert simplexml to associative arrays.
*/
private function unserialize_xml($input, $callback=null, $recurse = false)
{
// Get input, loading an xml string with simplexml if its the top level of recursion
$data = ( (!$recurse) && is_string($input) ) ? simplexml_load_string($input) : $input;
// Convert SimpleXMLElements to array
if ($data instanceof SimpleXMLElement) {
$data = (array) $data;
}
// Recurse into arrays
if (is_array($data)) {
foreach ($data as &$item) {
$item = $this->unserialize_xml($item, $callback, true);
}
}
// Run callback and return
return (!is_array($data) && is_callable($callback)) ? call_user_func($callback, $data) : $data;
}
/*****************************
* Tmp
*/
public function __query($request)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
if (is_null($this->remoteAccesskey) === true || empty($this->remoteAccesskey) === true || $this->remoteAccesskey == '') {
return $this->raiseError('No authentication information has been set.');
}
$cleanaccesshash = RvsLibs_String::preg_replace("'(\r|\n)'","", $this->remoteAccesskey);
$authstr = $user . ":" . $cleanaccesshash;
$data = array();
$data['PAGES'] = '';
if (RvsLibs_System::function_exists("curl_init")) {
$ch = curl_init();
if ($usessl) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_URL, 'https://' . $host . ':2087' . $request);
$referer = 'https://' . $_SERVER['SERVER_ADDR'] . ':2087';
} else {
curl_setopt($ch, CURLOPT_URL, 'http://' . $host . ':2086' . $request);
$referer = 'http://' . $_SERVER['SERVER_ADDR'] . ':2086';
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$curlheaders[0] = "Authorization: WHM $authstr";
$curlheaders[1] = "Referer: " . $referer;
curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheaders);
$data['PAGES'] = curl_exec ($ch);
curl_close ($ch);
} elseif (RvsLibs_System::function_exists("socket_create")) {
$usessl = false;
if ($usessl) {
$data['ERROR'] = 1;
$data['ERRORMSG'] = "SSL Support requires curl";
return $data;
}
$servicePort = 2086;
$referer = 'http://' . $_SERVER['SERVER_ADDR'] . ':2086';
/**
* gethostbyname('localhost); ๏ฟฝ๏ฟฝ๏ฟฝีปัญ๏ฟฝ๏ฟฝ ๏ฟฝับ ๏ฟฝรณีท๏ฟฝ๏ฟฝ Host ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ้งค๏ฟฝ๏ฟฝ
* localhost ๏ฟฝ๏ฟฝ 127.0.0.1
*
* ๏ฟฝิธ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝยน connect ๏ฟฝ๏ฟฝาน ip
*/
$address = gethostbyname($host);
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
$data['ERROR'] = 1;
$data['ERRORMSG'] = RvsLibs_String::translate('socket_create() failed');
return $data;
}
$result = socket_connect($socket, $address, $servicePort);
if ($result < 0) {
$data['ERROR'] = 1;
$data['ERRORMSG'] = RvsLibs_String::translate('socket_connect() failed');
return $data;
}
$in = "GET $request HTTP/1.0\n";
socket_write($socket,$in,RvsLibs_String::strlen($in));
$in = "Referer: $referer\n";
socket_write($socket, $in, RvsLibs_String::strlen($in));
$in = "Connection: close\n";
socket_write($socket,$in,RvsLibs_String::strlen($in));
$in = "Authorization: WHM $authstr\n\n\n";
socket_write($socket,$in,RvsLibs_String::strlen($in));
$inheader = 1;
$page = '';
while (($buf = socket_read($socket, 512)) != false) {
$page .= $buf;
}
/// FIX PHP 5.3 by Puttipong
//$aPage = split("\n|\r\n", $page);
$aPage = RvsLibs_String::preg_split('/\n|\r\n/', $page);
$aHeader = array();
$aData = array();
foreach ($aPage as $v) {
if (RvsLibs_String::preg_match("#^$#s", $v, $aMatch)) {
$sh = array_shift($aPage);
$data['HEADER'] = join("\n", $aHeader);
$data['PAGES'] = join("\n", $aPage);
break;
} else {
$aHeader[] = $v;
$sh = array_shift($aPage);
}
}
} else {
$data['ERROR'] = 1;
$data['ERRORMSG'] = RvsLibs_String::translate('php not compiled with --enable-sockets OR curl');
return $data;
}
return $data;
}
}
Copyright 2K16 - 2K18 Indonesian Hacker Rulez