xmlrpc_backslash = chr(92).chr(92);
// Types for info sent back and forth
$this->xmlrpcTypes = array(
$this->xmlrpcI4 => '1',
$this->xmlrpcInt => '1',
$this->xmlrpcBoolean => '1',
$this->xmlrpcString => '1',
$this->xmlrpcDouble => '1',
$this->xmlrpcDateTime => '1',
$this->xmlrpcBase64 => '1',
$this->xmlrpcArray => '2',
$this->xmlrpcStruct => '3'
);
// Array of Valid Parents for Various XML-RPC elements
$this->valid_parents = array('BOOLEAN' => array('VALUE'),
'I4' => array('VALUE'),
'INT' => array('VALUE'),
'STRING' => array('VALUE'),
'DOUBLE' => array('VALUE'),
'DATETIME.ISO8601' => array('VALUE'),
'BASE64' => array('VALUE'),
'ARRAY' => array('VALUE'),
'STRUCT' => array('VALUE'),
'PARAM' => array('PARAMS'),
'METHODNAME' => array('METHODCALL'),
'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
'MEMBER' => array('STRUCT'),
'NAME' => array('MEMBER'),
'DATA' => array('ARRAY'),
'FAULT' => array('METHODRESPONSE'),
'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT')
);
// XML-RPC Responses
$this->xmlrpcerr['unknown_method'] = '1';
$this->xmlrpcstr['unknown_method'] = 'This is not a known method for this XML-RPC Server';
$this->xmlrpcerr['invalid_return'] = '2';
$this->xmlrpcstr['invalid_return'] = 'The XML data received was either invalid or not in the correct form for XML-RPC. Turn on debugging to examine the XML data further.';
$this->xmlrpcerr['incorrect_params'] = '3';
$this->xmlrpcstr['incorrect_params'] = 'Incorrect parameters were passed to method';
$this->xmlrpcerr['introspect_unknown'] = '4';
$this->xmlrpcstr['introspect_unknown'] = 'Cannot inspect signature for request: method unknown';
$this->xmlrpcerr['http_error'] = '5';
$this->xmlrpcstr['http_error'] = "Did not receive a '200 OK' response from remote server.";
$this->xmlrpcerr['no_data'] = '6';
$this->xmlrpcstr['no_data'] = 'No data received from server.';
$this->initialize($config);
log_message('info', 'XML-RPC Class Initialized');
}
// --------------------------------------------------------------------
/**
* Initialize
*
* @param array $config
* @return void
*/
public function initialize($config = array())
{
if (count($config) > 0)
{
foreach ($config as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
// --------------------------------------------------------------------
/**
* Parse server URL
*
* @param string $url
* @param int $port
* @param string $proxy
* @param int $proxy_port
* @return void
*/
public function server($url, $port = 80, $proxy = FALSE, $proxy_port = 8080)
{
if (stripos($url, 'http') !== 0)
{
$url = 'http://'.$url;
}
$parts = parse_url($url);
if (isset($parts['user'], $parts['pass']))
{
$parts['host'] = $parts['user'].':'.$parts['pass'].'@'.$parts['host'];
}
$path = isset($parts['path']) ? $parts['path'] : '/';
if ( ! empty($parts['query']))
{
$path .= '?'.$parts['query'];
}
$this->client = new XML_RPC_Client($path, $parts['host'], $port, $proxy, $proxy_port);
}
// --------------------------------------------------------------------
/**
* Set Timeout
*
* @param int $seconds
* @return void
*/
public function timeout($seconds = 5)
{
if ($this->client !== NULL && is_int($seconds))
{
$this->client->timeout = $seconds;
}
}
// --------------------------------------------------------------------
/**
* Set Methods
*
* @param string $function Method name
* @return void
*/
public function method($function)
{
$this->method = $function;
}
// --------------------------------------------------------------------
/**
* Take Array of Data and Create Objects
*
* @param array $incoming
* @return void
*/
public function request($incoming)
{
if ( ! is_array($incoming))
{
// Send Error
return;
}
$this->data = array();
foreach ($incoming as $key => $value)
{
$this->data[$key] = $this->values_parsing($value);
}
}
// --------------------------------------------------------------------
/**
* Set Debug
*
* @param bool $flag
* @return void
*/
public function set_debug($flag = TRUE)
{
$this->debug = ($flag === TRUE);
}
// --------------------------------------------------------------------
/**
* Values Parsing
*
* @param mixed $value
* @return object
*/
public function values_parsing($value)
{
if (is_array($value) && array_key_exists(0, $value))
{
if ( ! isset($value[1], $this->xmlrpcTypes[$value[1]]))
{
$temp = new XML_RPC_Values($value[0], (is_array($value[0]) ? 'array' : 'string'));
}
else
{
if (is_array($value[0]) && ($value[1] === 'struct' OR $value[1] === 'array'))
{
while (list($k) = each($value[0]))
{
$value[0][$k] = $this->values_parsing($value[0][$k]);
}
}
$temp = new XML_RPC_Values($value[0], $value[1]);
}
}
else
{
$temp = new XML_RPC_Values($value, 'string');
}
return $temp;
}
// --------------------------------------------------------------------
/**
* Sends XML-RPC Request
*
* @return bool
*/
public function send_request()
{
$this->message = new XML_RPC_Message($this->method, $this->data);
$this->message->debug = $this->debug;
if ( ! $this->result = $this->client->send($this->message) OR ! is_object($this->result->val))
{
$this->error = $this->result->errstr;
return FALSE;
}
$this->response = $this->result->decode();
return TRUE;
}
// --------------------------------------------------------------------
/**
* Returns Error
*
* @return string
*/
public function display_error()
{
return $this->error;
}
// --------------------------------------------------------------------
/**
* Returns Remote Server Response
*
* @return string
*/
public function display_response()
{
return $this->response;
}
// --------------------------------------------------------------------
/**
* Sends an Error Message for Server Request
*
* @param int $number
* @param string $message
* @return object
*/
public function send_error_message($number, $message)
{
return new XML_RPC_Response(0, $number, $message);
}
// --------------------------------------------------------------------
/**
* Send Response for Server Request
*
* @param array $response
* @return object
*/
public function send_response($response)
{
// $response should be array of values, which will be parsed
// based on their data and type into a valid group of XML-RPC values
return new XML_RPC_Response($this->values_parsing($response));
}
} // END XML_RPC Class
/**
* XML-RPC Client class
*
* @category XML-RPC
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/libraries/xmlrpc.html
*/
class XML_RPC_Client extends CI_Xmlrpc
{
/**
* Path
*
* @var string
*/
public $path = '';
/**
* Server hostname
*
* @var string
*/
public $server = '';
/**
* Server port
*
* @var int
*/
public $port = 80;
/**
*
* Server username
*
* @var string
*/
public $username;
/**
* Server password
*
* @var string
*/
public $password;
/**
* Proxy hostname
*
* @var string
*/
public $proxy = FALSE;
/**
* Proxy port
*
* @var int
*/
public $proxy_port = 8080;
/**
* Error number
*
* @var string
*/
public $errno = '';
/**
* Error message
*
* @var string
*/
public $errstring = '';
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 5;
/**
* No Multicall flag
*
* @var bool
*/
public $no_multicall = FALSE;
// --------------------------------------------------------------------
/**
* Constructor
*
* @param string $path
* @param object $server
* @param int $port
* @param string $proxy
* @param int $proxy_port
* @return void
*/
public function __construct($path, $server, $port = 80, $proxy = FALSE, $proxy_port = 8080)
{
parent::__construct();
$url = parse_url('http://'.$server);
if (isset($url['user'], $url['pass']))
{
$this->username = $url['user'];
$this->password = $url['pass'];
}
$this->port = $port;
$this->server = $url['host'];
$this->path = $path;
$this->proxy = $proxy;
$this->proxy_port = $proxy_port;
}
// --------------------------------------------------------------------
/**
* Send message
*
* @param mixed $msg
* @return object
*/
public function send($msg)
{
if (is_array($msg))
{
// Multi-call disabled
return new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'], $this->xmlrpcstr['multicall_recursion']);
}
return $this->sendPayload($msg);
}
// --------------------------------------------------------------------
/**
* Send payload
*
* @param object $msg
* @return object
*/
public function sendPayload($msg)
{
if ($this->proxy === FALSE)
{
$server = $this->server;
$port = $this->port;
}
else
{
$server = $this->proxy;
$port = $this->proxy_port;
}
$fp = @fsockopen($server, $port, $this->errno, $this->errstring, $this->timeout);
if ( ! is_resource($fp))
{
error_log($this->xmlrpcstr['http_error']);
return new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
}
if (empty($msg->payload))
{
// $msg = XML_RPC_Messages
$msg->createPayload();
}
$r = "\r\n";
$op = 'POST '.$this->path.' HTTP/1.0'.$r
.'Host: '.$this->server.$r
.'Content-Type: text/xml'.$r
.(isset($this->username, $this->password) ? 'Authorization: Basic '.base64_encode($this->username.':'.$this->password).$r : '')
.'User-Agent: '.$this->xmlrpcName.$r
.'Content-Length: '.strlen($msg->payload).$r.$r
.$msg->payload;
stream_set_timeout($fp, $this->timeout); // set timeout for subsequent operations
for ($written = $timestamp = 0, $length = strlen($op); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($op, $written))) === FALSE)
{
break;
}
// See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
elseif ($result === 0)
{
if ($timestamp === 0)
{
$timestamp = time();
}
elseif ($timestamp < (time() - $this->timeout))
{
$result = FALSE;
break;
}
}
else
{
$timestamp = 0;
}
}
if ($result === FALSE)
{
error_log($this->xmlrpcstr['http_error']);
return new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
}
$resp = $msg->parseResponse($fp);
fclose($fp);
return $resp;
}
} // END XML_RPC_Client Class
/**
* XML-RPC Response class
*
* @category XML-RPC
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/libraries/xmlrpc.html
*/
class XML_RPC_Response
{
/**
* Value
*
* @var mixed
*/
public $val = 0;
/**
* Error number
*
* @var int
*/
public $errno = 0;
/**
* Error message
*
* @var string
*/
public $errstr = '';
/**
* Headers list
*
* @var array
*/
public $headers = array();
/**
* XSS Filter flag
*
* @var bool
*/
public $xss_clean = TRUE;
// --------------------------------------------------------------------
/**
* Constructor
*
* @param mixed $val
* @param int $code
* @param string $fstr
* @return void
*/
public function __construct($val, $code = 0, $fstr = '')
{
if ($code !== 0)
{
// error
$this->errno = $code;
$this->errstr = htmlspecialchars($fstr, ENT_XML1 | ENT_NOQUOTES, 'UTF-8');
}
elseif ( ! is_object($val))
{
// programmer error, not an object
error_log("Invalid type '".gettype($val)."' (value: ".$val.') passed to XML_RPC_Response. Defaulting to empty value.');
$this->val = new XML_RPC_Values();
}
else
{
$this->val = $val;
}
}
// --------------------------------------------------------------------
/**
* Fault code
*
* @return int
*/
public function faultCode()
{
return $this->errno;
}
// --------------------------------------------------------------------
/**
* Fault string
*
* @return string
*/
public function faultString()
{
return $this->errstr;
}
// --------------------------------------------------------------------
/**
* Value
*
* @return mixed
*/
public function value()
{
return $this->val;
}
// --------------------------------------------------------------------
/**
* Prepare response
*
* @return string xml
*/
public function prepare_response()
{
return "
---DATA---\n".htmlspecialchars($data)."\n---END DATA---\n\n"; } // Check for data if ($data === '') { error_log($this->xmlrpcstr['no_data']); return new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']); } // Check for HTTP 200 Response if (strpos($data, 'HTTP') === 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data)) { $errstr = substr($data, 0, strpos($data, "\n")-1); return new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error'].' ('.$errstr.')'); } //------------------------------------- // Create and Set Up XML Parser //------------------------------------- $parser = xml_parser_create($this->xmlrpc_defencoding); $pname = (string) $parser; $this->xh[$pname] = array( 'isf' => 0, 'ac' => '', 'headers' => array(), 'stack' => array(), 'valuestack' => array(), 'isf_reason' => 0 ); xml_set_object($parser, $this); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, TRUE); xml_set_element_handler($parser, 'open_tag', 'closing_tag'); xml_set_character_data_handler($parser, 'character_data'); //xml_set_default_handler($parser, 'default_handler'); // Get headers $lines = explode("\r\n", $data); while (($line = array_shift($lines))) { if (strlen($line) < 1) { break; } $this->xh[$pname]['headers'][] = $line; } $data = implode("\r\n", $lines); // Parse XML data if ( ! xml_parse($parser, $data, count($data))) { $errstr = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)); $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']); xml_parser_free($parser); return $r; } xml_parser_free($parser); // Got ourselves some badness, it seems if ($this->xh[$pname]['isf'] > 1) { if ($this->debug === TRUE) { echo "---Invalid Return---\n".$this->xh[$pname]['isf_reason']."---Invalid Return---\n\n"; } return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$pname]['isf_reason']); } elseif ( ! is_object($this->xh[$pname]['value'])) { return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$pname]['isf_reason']); } // Display XML content for debugging if ($this->debug === TRUE) { echo '
'; if (count($this->xh[$pname]['headers'] > 0)) { echo "---HEADERS---\n"; foreach ($this->xh[$pname]['headers'] as $header) { echo $header."\n"; } echo "---END HEADERS---\n\n"; } echo "---DATA---\n".htmlspecialchars($data)."\n---END DATA---\n\n---PARSED---\n"; var_dump($this->xh[$pname]['value']); echo "\n---END PARSED---"; } // Send response $v = $this->xh[$pname]['value']; if ($this->xh[$pname]['isf']) { $errno_v = $v->me['struct']['faultCode']; $errstr_v = $v->me['struct']['faultString']; $errno = $errno_v->scalarval(); if ($errno === 0) { // FAULT returned, errno needs to reflect that $errno = -1; } $r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval()); } else { $r = new XML_RPC_Response($v); } $r->headers = $this->xh[$pname]['headers']; return $r; } // -------------------------------------------------------------------- // ------------------------------------ // Begin Return Message Parsing section // ------------------------------------ // quick explanation of components: // ac - used to accumulate values // isf - used to indicate a fault // lv - used to indicate "looking for a value": implements // the logic to allow values with no types to be strings // params - used to store parameters in method calls // method - used to store method name // stack - array with parent tree of the xml element, // used to validate the nesting of elements // -------------------------------------------------------------------- /** * Start Element Handler * * @param string * @param string * @return void */ public function open_tag($the_parser, $name) { $the_parser = (string) $the_parser; // If invalid nesting, then return if ($this->xh[$the_parser]['isf'] > 1) return; // Evaluate and check for correct nesting of XML elements if (count($this->xh[$the_parser]['stack']) === 0) { if ($name !== 'METHODRESPONSE' && $name !== 'METHODCALL') { $this->xh[$the_parser]['isf'] = 2; $this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing'; return; } } // not top level element: see if parent is OK elseif ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE)) { $this->xh[$the_parser]['isf'] = 2; $this->xh[$the_parser]['isf_reason'] = 'XML-RPC element '.$name.' cannot be child of '.$this->xh[$the_parser]['stack'][0]; return; } switch ($name) { case 'STRUCT': case 'ARRAY': // Creates array for child elements $cur_val = array('value' => array(), 'type' => $name); array_unshift($this->xh[$the_parser]['valuestack'], $cur_val); break; case 'METHODNAME': case 'NAME': $this->xh[$the_parser]['ac'] = ''; break; case 'FAULT': $this->xh[$the_parser]['isf'] = 1; break; case 'PARAM': $this->xh[$the_parser]['value'] = NULL; break; case 'VALUE': $this->xh[$the_parser]['vt'] = 'value'; $this->xh[$the_parser]['ac'] = ''; $this->xh[$the_parser]['lv'] = 1; break; case 'I4': case 'INT': case 'STRING': case 'BOOLEAN': case 'DOUBLE': case 'DATETIME.ISO8601': case 'BASE64': if ($this->xh[$the_parser]['vt'] !== 'value') { //two data elements inside a value: an error occurred! $this->xh[$the_parser]['isf'] = 2; $this->xh[$the_parser]['isf_reason'] = 'There is a '.$name.' element following a ' .$this->xh[$the_parser]['vt'].' element inside a single value'; return; } $this->xh[$the_parser]['ac'] = ''; break; case 'MEMBER': // Set name of