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('debug', 'XML-RPC Class Initialized');
}
// --------------------------------------------------------------------
/**
* Initialize
*
* @param array
* @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
* @return void
*/
public function server($url, $port = 80, $proxy = FALSE, $proxy_port = 8080)
{
if (strpos($url, 'http') !== 0)
{
$url = 'http://'.$url;
}
$parts = parse_url($url);
$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 ( ! is_null($this->client) && is_int($seconds))
{
$this->client->timeout = $seconds;
}
}
// --------------------------------------------------------------------
/**
* Set Methods
*
* @param string method name
* @return void
*/
public function method($function)
{
$this->method = $function;
}
// --------------------------------------------------------------------
/**
* Take Array of Data and Create Objects
*
* @param array
* @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
* @return void
*/
public function set_debug($flag = TRUE)
{
$this->debug = ($flag === TRUE);
}
// --------------------------------------------------------------------
/**
* Values Parsing
*
* @param mixed
* @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], TRUE);
}
}
$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
* @param string
* @return object
*/
public function send_error_message($number, $message)
{
return new XML_RPC_Response(0, $number, $message);
}
// --------------------------------------------------------------------
/**
* Send Response for Server Request
*
* @param array
* @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 http://codeigniter.com/user_guide/libraries/xmlrpc.html
*/
class XML_RPC_Client extends CI_Xmlrpc
{
public $path = '';
public $server = '';
public $port = 80;
public $proxy = FALSE;
public $proxy_port = 8080;
public $errno = '';
public $errstring = '';
public $timeout = 5;
public $no_multicall = FALSE;
/**
* Constructor
*
* @param string
* @param object
* @param int
* @return void
*/
public function __construct($path, $server, $port = 80, $proxy = FALSE, $proxy_port = 8080)
{
parent::__construct();
$this->port = $port;
$this->server = $server;
$this->path = $path;
$this->proxy = $proxy;
$this->proxy_port = $proxy_port;
}
// --------------------------------------------------------------------
/**
* Send message
*
* @param mixed
* @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
* @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
.'User-Agent: '.$this->xmlrpcName.$r
.'Content-Length: '.strlen($msg->payload).$r.$r
.$msg->payload;
if ( ! fwrite($fp, $op, strlen($op)))
{
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 http://codeigniter.com/user_guide/libraries/xmlrpc.html
*/
class XML_RPC_Response
{
public $val = 0;
public $errno = 0;
public $errstr = '';
public $headers = array();
public $xss_clean = TRUE;
/**
* Constructor
*
* @param mixed
* @param int
* @param string
* @return void
*/
public function __construct($val, $code = 0, $fstr = '')
{
if ($code !== 0)
{
// error
$this->errno = $code;
$this->errstr = htmlspecialchars($fstr,
(is_php('5.4') ? ENT_XML1 | ENT_NOQUOTES : 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); $this->xh[$parser] = 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[$parser]['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)); //error_log($errstr); $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[$parser]['isf'] > 1) { if ($this->debug === TRUE) { echo "---Invalid Return---\n".$this->xh[$parser]['isf_reason']."---Invalid Return---\n\n"; } return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']); } elseif ( ! is_object($this->xh[$parser]['value'])) { return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']); } // Display XML content for debugging if ($this->debug === TRUE) { echo '
'; if (count($this->xh[$parser]['headers'] > 0)) { echo "---HEADERS---\n"; foreach ($this->xh[$parser]['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[$parser]['value']); echo "\n---END PARSED---"; } // Send response $v = $this->xh[$parser]['value']; if ($this->xh[$parser]['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[$parser]['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) { // 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'] = "'Twas 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