Update Incutio XML-RPC Library to latest version (1.7.4). Props hakre. Fixes #14703

git-svn-id: http://svn.automattic.com/wordpress/trunk@15612 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-09-13 16:36:08 +00:00
parent 4c7deb41e7
commit ec7490bc41
1 changed files with 292 additions and 137 deletions

View File

@ -1,16 +1,42 @@
<?php <?php
/** /**
* IXR - The Inutio XML-RPC Library * IXR - The Incutio XML-RPC Library
*
* Copyright (c) 2010, Incutio Ltd.
* 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 Incutio Ltd. 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.
* *
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
* *
* @copyright Incutio Ltd 2002-2005 * @copyright Incutio Ltd 2010 (http://www.incutio.com)
* @version 1.7 (beta) 23rd May 2005 * @version 1.7.4 7th September 2010
* @author Simon Willison * @author Simon Willison
* @link http://scripts.incutio.com/xmlrpc/ Site * @link http://scripts.incutio.com/xmlrpc/ Site/manual
* @link http://scripts.incutio.com/xmlrpc/manual.php Manual * @license http://www.opensource.org/licenses/bsd-license.php BSD
* @license BSD License http://www.opensource.org/licenses/bsd-license.php
*/ */
/** /**
@ -23,14 +49,15 @@ class IXR_Value {
var $data; var $data;
var $type; var $type;
function IXR_Value ($data, $type = false) { function IXR_Value($data, $type = false)
{
$this->data = $data; $this->data = $data;
if (!$type) { if (!$type) {
$type = $this->calculateType(); $type = $this->calculateType();
} }
$this->type = $type; $this->type = $type;
if ($type == 'struct') { if ($type == 'struct') {
/* Turn all the values in the array in to new IXR_Value objects */ // Turn all the values in the array in to new IXR_Value objects
foreach ($this->data as $key => $value) { foreach ($this->data as $key => $value) {
$this->data[$key] = new IXR_Value($value); $this->data[$key] = new IXR_Value($value);
} }
@ -42,7 +69,8 @@ class IXR_Value {
} }
} }
function calculateType() { function calculateType()
{
if ($this->data === true || $this->data === false) { if ($this->data === true || $this->data === false) {
return 'boolean'; return 'boolean';
} }
@ -52,6 +80,7 @@ class IXR_Value {
if (is_double($this->data)) { if (is_double($this->data)) {
return 'double'; return 'double';
} }
// Deal with IXR object types base64 and date // Deal with IXR object types base64 and date
if (is_object($this->data) && is_a($this->data, 'IXR_Date')) { if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
return 'date'; return 'date';
@ -59,16 +88,17 @@ class IXR_Value {
if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) { if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
return 'base64'; return 'base64';
} }
// If it is a normal PHP object convert it in to a struct // If it is a normal PHP object convert it in to a struct
if (is_object($this->data)) { if (is_object($this->data)) {
$this->data = get_object_vars($this->data); $this->data = get_object_vars($this->data);
return 'struct'; return 'struct';
} }
if (!is_array($this->data)) { if (!is_array($this->data)) {
return 'string'; return 'string';
} }
/* We have an array - is it an array or a struct ? */
// We have an array - is it an array or a struct?
if ($this->isStruct($this->data)) { if ($this->isStruct($this->data)) {
return 'struct'; return 'struct';
} else { } else {
@ -76,8 +106,9 @@ class IXR_Value {
} }
} }
function getXml() { function getXml()
/* Return XML for this value */ {
// Return XML for this value
switch ($this->type) { switch ($this->type) {
case 'boolean': case 'boolean':
return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>'; return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>';
@ -117,8 +148,14 @@ class IXR_Value {
return false; return false;
} }
function isStruct($array) { /**
/* Nasty function to check if an array is a struct or not */ * Checks whether or not the supplied array is a struct or not
*
* @param unknown_type $array
* @return boolean
*/
function isStruct($array)
{
$expected = 0; $expected = 0;
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if ((string)$key != (string)$expected) { if ((string)$key != (string)$expected) {
@ -131,18 +168,21 @@ class IXR_Value {
} }
/** /**
* IXR_Message * IXR_MESSAGE
* *
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*
*/ */
class IXR_Message { class IXR_Message
{
var $message; var $message;
var $messageType; // methodCall / methodResponse / fault var $messageType; // methodCall / methodResponse / fault
var $faultCode; var $faultCode;
var $faultString; var $faultString;
var $methodName; var $methodName;
var $params; var $params;
// Current variable stacks // Current variable stacks
var $_arraystructs = array(); // The stack used to keep track of the current array/struct var $_arraystructs = array(); // The stack used to keep track of the current array/struct
var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
@ -153,44 +193,54 @@ class IXR_Message {
var $_currentTagContents; var $_currentTagContents;
// The XML parser // The XML parser
var $_parser; var $_parser;
function IXR_Message (&$message) {
$this->message = &$message; function IXR_Message($message)
{
$this->message =& $message;
} }
function parse() {
// first remove the XML declaration function parse()
// this method avoids the RAM usage of preg_replace on very large messages {
$header = preg_replace( '/<\?xml.*?\?'.'>/', '', substr( $this->message, 0, 100 ), 1 ); // first remove the XML declaration
$this->message = substr_replace($this->message, $header, 0, 100); // merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages
$header = preg_replace( '/<\?xml.*?\?'.'>/', '', substr($this->message, 0, 100), 1);
$this->message = substr_replace($this->message, $header, 0, 100);
if (trim($this->message) == '') { if (trim($this->message) == '') {
return false; return false;
} }
$this->_parser = xml_parser_create(); $this->_parser = xml_parser_create();
// Set XML parser to take the case of tags in to account // Set XML parser to take the case of tags in to account
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
// Set XML parser callback functions // Set XML parser callback functions
xml_set_object($this->_parser, $this); xml_set_object($this->_parser, $this);
xml_set_element_handler($this->_parser, 'tag_open', 'tag_close'); xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->_parser, 'cdata'); xml_set_character_data_handler($this->_parser, 'cdata');
$chunk_size = 262144; // 256Kb, parse in chunks to avoid the RAM usage on very large messages $chunk_size = 262144; // 256Kb, parse in chunks to avoid the RAM usage on very large messages
do { do {
if ( strlen($this->message) <= $chunk_size ) if (strlen($this->message) <= $chunk_size) {
$final=true; $final = true;
$part = substr( $this->message, 0, $chunk_size ); }
$this->message = substr( $this->message, $chunk_size ); $part = substr($this->message, 0, $chunk_size);
if ( !xml_parse( $this->_parser, $part, $final ) ) $this->message = substr($this->message, $chunk_size);
return false; if (!xml_parse($this->_parser, $part, $final)) {
if ( $final ) return false;
break; }
} while ( true ); if ($final) {
xml_parser_free($this->_parser); break;
}
} while (true);
xml_parser_free($this->_parser);
// Grab the error messages, if any // Grab the error messages, if any
if ($this->messageType == 'fault') { if ($this->messageType == 'fault') {
$this->faultCode = $this->params[0]['faultCode']; $this->faultCode = $this->params[0]['faultCode'];
$this->faultString = $this->params[0]['faultString']; $this->faultString = $this->params[0]['faultString'];
} }
return true; return true;
} }
function tag_open($parser, $tag, $attr) {
function tag_open($parser, $tag, $attr)
{
$this->_currentTagContents = ''; $this->_currentTagContents = '';
$this->currentTag = $tag; $this->currentTag = $tag;
switch($tag) { switch($tag) {
@ -199,7 +249,7 @@ class IXR_Message {
case 'fault': case 'fault':
$this->messageType = $tag; $this->messageType = $tag;
break; break;
/* Deal with stacks of arrays and structs */ /* Deal with stacks of arrays and structs */
case 'data': // data is to all intents and puposes more interesting than array case 'data': // data is to all intents and puposes more interesting than array
$this->_arraystructstypes[] = 'array'; $this->_arraystructstypes[] = 'array';
$this->_arraystructs[] = array(); $this->_arraystructs[] = array();
@ -210,28 +260,31 @@ class IXR_Message {
break; break;
} }
} }
function cdata($parser, $cdata) {
function cdata($parser, $cdata)
{
$this->_currentTagContents .= $cdata; $this->_currentTagContents .= $cdata;
} }
function tag_close($parser, $tag) {
function tag_close($parser, $tag)
{
$valueFlag = false; $valueFlag = false;
switch($tag) { switch($tag) {
case 'int': case 'int':
case 'i4': case 'i4':
$value = (int) trim($this->_currentTagContents); $value = (int)trim($this->_currentTagContents);
$valueFlag = true; $valueFlag = true;
break; break;
case 'double': case 'double':
$value = (double) trim($this->_currentTagContents); $value = (double)trim($this->_currentTagContents);
$valueFlag = true; $valueFlag = true;
break; break;
case 'string': case 'string':
$value = $this->_currentTagContents; $value = (string)trim($this->_currentTagContents);
$valueFlag = true; $valueFlag = true;
break; break;
case 'dateTime.iso8601': case 'dateTime.iso8601':
$value = new IXR_Date(trim($this->_currentTagContents)); $value = new IXR_Date(trim($this->_currentTagContents));
// $value = $iso->getTimestamp();
$valueFlag = true; $valueFlag = true;
break; break;
case 'value': case 'value':
@ -242,14 +295,14 @@ class IXR_Message {
} }
break; break;
case 'boolean': case 'boolean':
$value = (boolean) trim($this->_currentTagContents); $value = (boolean)trim($this->_currentTagContents);
$valueFlag = true; $valueFlag = true;
break; break;
case 'base64': case 'base64':
$value = base64_decode( trim( $this->_currentTagContents ) ); $value = base64_decode($this->_currentTagContents);
$valueFlag = true; $valueFlag = true;
break; break;
/* Deal with stacks of arrays and structs */ /* Deal with stacks of arrays and structs */
case 'data': case 'data':
case 'struct': case 'struct':
$value = array_pop($this->_arraystructs); $value = array_pop($this->_arraystructs);
@ -266,6 +319,7 @@ class IXR_Message {
$this->methodName = trim($this->_currentTagContents); $this->methodName = trim($this->_currentTagContents);
break; break;
} }
if ($valueFlag) { if ($valueFlag) {
if (count($this->_arraystructs) > 0) { if (count($this->_arraystructs) > 0) {
// Add value to struct or array // Add value to struct or array
@ -291,27 +345,40 @@ class IXR_Message {
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_Server { class IXR_Server
{
var $data; var $data;
var $callbacks = array(); var $callbacks = array();
var $message; var $message;
var $capabilities; var $capabilities;
function IXR_Server($callbacks = false, $data = false) {
function IXR_Server($callbacks = false, $data = false, $wait = false)
{
$this->setCapabilities(); $this->setCapabilities();
if ($callbacks) { if ($callbacks) {
$this->callbacks = $callbacks; $this->callbacks = $callbacks;
} }
$this->setCallbacks(); $this->setCallbacks();
$this->serve($data); if (!$wait) {
$this->serve($data);
}
} }
function serve($data = false) {
function serve($data = false)
{
if (!$data) { if (!$data) {
global $HTTP_RAW_POST_DATA; if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
if (!$HTTP_RAW_POST_DATA) { header('Content-Type: text/plain'); // merged from WP #9093
header( 'Content-Type: text/plain' ); die('XML-RPC server accepts POST requests only.');
die('XML-RPC server accepts POST requests only.'); }
global $HTTP_RAW_POST_DATA;
if (empty($HTTP_RAW_POST_DATA)) {
// workaround for a bug in PHP 5.2.2 - http://bugs.php.net/bug.php?id=41293
$data = file_get_contents('php://input');
} else {
$data =& $HTTP_RAW_POST_DATA;
} }
$data = &$HTTP_RAW_POST_DATA;
} }
$this->message = new IXR_Message($data); $this->message = new IXR_Message($data);
if (!$this->message->parse()) { if (!$this->message->parse()) {
@ -321,75 +388,83 @@ class IXR_Server {
$this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall'); $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
} }
$result = $this->call($this->message->methodName, $this->message->params); $result = $this->call($this->message->methodName, $this->message->params);
// Is the result an error? // Is the result an error?
if (is_a($result, 'IXR_Error')) { if (is_a($result, 'IXR_Error')) {
$this->error($result); $this->error($result);
} }
// Encode the result // Encode the result
$r = new IXR_Value($result); $r = new IXR_Value($result);
$resultxml = $r->getXml(); $resultxml = $r->getXml();
// Create the XML // Create the XML
$xml = <<<EOD $xml = <<<EOD
<methodResponse> <methodResponse>
<params> <params>
<param> <param>
<value> <value>
$resultxml $resultxml
</value> </value>
</param> </param>
</params> </params>
</methodResponse> </methodResponse>
EOD; EOD;
// Send it // Send it
$this->output($xml); $this->output($xml);
} }
function call($methodname, $args) {
function call($methodname, $args)
{
if (!$this->hasMethod($methodname)) { if (!$this->hasMethod($methodname)) {
return new IXR_Error(-32601, 'server error. requested method '. return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
$methodname.' does not exist.');
} }
$method = $this->callbacks[$methodname]; $method = $this->callbacks[$methodname];
// Perform the callback and send the response // Perform the callback and send the response
if (count($args) == 1) { if (count($args) == 1) {
// If only one paramater just send that instead of the whole array // If only one paramater just send that instead of the whole array
$args = $args[0]; $args = $args[0];
} }
// Are we dealing with a function or a method? // Are we dealing with a function or a method?
if ( is_string( $method ) && substr($method, 0, 5) == 'this:' ) { if (is_string($method) && substr($method, 0, 5) == 'this:') {
// It's a class method - check it exists // It's a class method - check it exists
$method = substr($method, 5); $method = substr($method, 5);
if (!method_exists($this, $method)) { if (!method_exists($this, $method)) {
return new IXR_Error(-32601, 'server error. requested class method "'. return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
$method.'" does not exist.');
} }
// Call the method
//Call the method
$result = $this->$method($args); $result = $this->$method($args);
} else { } else {
// It's a function - does it exist? // It's a function - does it exist?
if (is_array($method)) { if (is_array($method)) {
if (!method_exists($method[0], $method[1])) { if (!method_exists($method[0], $method[1])) {
return new IXR_Error(-32601, 'server error. requested object method "'. return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.');
$method[1].'" does not exist.');
} }
} else if (!function_exists($method)) { } else if (!function_exists($method)) {
return new IXR_Error(-32601, 'server error. requested function "'. return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
$method.'" does not exist.');
} }
// Call the function // Call the function
$result = call_user_func($method, $args); $result = call_user_func($method, $args);
} }
return $result; return $result;
} }
function error($error, $message = false) { function error($error, $message = false)
{
// Accepts either an error object or an error code and message // Accepts either an error object or an error code and message
if ($message && !is_object($error)) { if ($message && !is_object($error)) {
$error = new IXR_Error($error, $message); $error = new IXR_Error($error, $message);
} }
$this->output($error->getXml()); $this->output($error->getXml());
} }
function output($xml) {
function output($xml)
{
$xml = '<?xml version="1.0"?>'."\n".$xml; $xml = '<?xml version="1.0"?>'."\n".$xml;
$length = strlen($xml); $length = strlen($xml);
header('Connection: close'); header('Connection: close');
@ -399,40 +474,52 @@ EOD;
echo $xml; echo $xml;
exit; exit;
} }
function hasMethod($method) {
function hasMethod($method)
{
return in_array($method, array_keys($this->callbacks)); return in_array($method, array_keys($this->callbacks));
} }
function setCapabilities() {
function setCapabilities()
{
// Initialises capabilities array // Initialises capabilities array
$this->capabilities = array( $this->capabilities = array(
'xmlrpc' => array( 'xmlrpc' => array(
'specUrl' => 'http://www.xmlrpc.com/spec', 'specUrl' => 'http://www.xmlrpc.com/spec',
'specVersion' => 1 'specVersion' => 1
), ),
'faults_interop' => array( 'faults_interop' => array(
'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php', 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
'specVersion' => 20010516 'specVersion' => 20010516
), ),
'system.multicall' => array( 'system.multicall' => array(
'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
'specVersion' => 1 'specVersion' => 1
), ),
); );
} }
function getCapabilities($args) {
function getCapabilities($args)
{
return $this->capabilities; return $this->capabilities;
} }
function setCallbacks() {
function setCallbacks()
{
$this->callbacks['system.getCapabilities'] = 'this:getCapabilities'; $this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
$this->callbacks['system.listMethods'] = 'this:listMethods'; $this->callbacks['system.listMethods'] = 'this:listMethods';
$this->callbacks['system.multicall'] = 'this:multiCall'; $this->callbacks['system.multicall'] = 'this:multiCall';
} }
function listMethods($args) {
function listMethods($args)
{
// Returns a list of methods - uses array_reverse to ensure user defined // Returns a list of methods - uses array_reverse to ensure user defined
// methods are listed before server defined methods // methods are listed before server defined methods
return array_reverse(array_keys($this->callbacks)); return array_reverse(array_keys($this->callbacks));
} }
function multiCall($methodcalls) {
function multiCall($methodcalls)
{
// See http://www.xmlrpc.com/discuss/msgReader$1208 // See http://www.xmlrpc.com/discuss/msgReader$1208
$return = array(); $return = array();
foreach ($methodcalls as $call) { foreach ($methodcalls as $call) {
@ -462,11 +549,14 @@ EOD;
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_Request { class IXR_Request
{
var $method; var $method;
var $args; var $args;
var $xml; var $xml;
function IXR_Request($method, $args) {
function IXR_Request($method, $args)
{
$this->method = $method; $this->method = $method;
$this->args = $args; $this->args = $args;
$this->xml = <<<EOD $this->xml = <<<EOD
@ -484,10 +574,14 @@ EOD;
} }
$this->xml .= '</params></methodCall>'; $this->xml .= '</params></methodCall>';
} }
function getLength() {
function getLength()
{
return strlen($this->xml); return strlen($this->xml);
} }
function getXml() {
function getXml()
{
return $this->xml; return $this->xml;
} }
} }
@ -497,26 +591,31 @@ EOD;
* *
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*
*/ */
class IXR_Client { class IXR_Client
{
var $server; var $server;
var $port; var $port;
var $path; var $path;
var $useragent; var $useragent;
var $headers;
var $response; var $response;
var $message = false; var $message = false;
var $debug = false; var $debug = false;
var $timeout; var $timeout;
// Storage place for an error message // Storage place for an error message
var $error = false; var $error = false;
function IXR_Client($server, $path = false, $port = 80, $timeout = false) {
function IXR_Client($server, $path = false, $port = 80, $timeout = 15)
{
if (!$path) { if (!$path) {
// Assume we have been given a URL instead // Assume we have been given a URL instead
$bits = parse_url($server); $bits = parse_url($server);
$this->server = $bits['host']; $this->server = $bits['host'];
$this->port = isset($bits['port']) ? $bits['port'] : 80; $this->port = isset($bits['port']) ? $bits['port'] : 80;
$this->path = isset($bits['path']) ? $bits['path'] : '/'; $this->path = isset($bits['path']) ? $bits['path'] : '/';
// Make absolutely sure we have a path // Make absolutely sure we have a path
if (!$this->path) { if (!$this->path) {
$this->path = '/'; $this->path = '/';
@ -529,7 +628,9 @@ class IXR_Client {
$this->useragent = 'The Incutio XML-RPC PHP Library'; $this->useragent = 'The Incutio XML-RPC PHP Library';
$this->timeout = $timeout; $this->timeout = $timeout;
} }
function query() {
function query()
{
$args = func_get_args(); $args = func_get_args();
$method = array_shift($args); $method = array_shift($args);
$request = new IXR_Request($method, $args); $request = new IXR_Request($method, $args);
@ -538,33 +639,36 @@ class IXR_Client {
$r = "\r\n"; $r = "\r\n";
$request = "POST {$this->path} HTTP/1.0$r"; $request = "POST {$this->path} HTTP/1.0$r";
$this->headers['Host'] = $this->server; // Merged from WP #8145 - allow custom headers
$this->headers['Content-Type'] = 'text/xml'; $this->headers['Host'] = $this->server;
$this->headers['User-Agent'] = $this->useragent; $this->headers['Content-Type'] = 'text/xml';
$this->headers['Content-Length']= $length; $this->headers['User-Agent'] = $this->useragent;
$this->headers['Content-Length']= $length;
foreach( $this->headers as $header => $value ) { foreach( $this->headers as $header => $value ) {
$request .= "{$header}: {$value}{$r}"; $request .= "{$header}: {$value}{$r}";
} }
$request .= $r; $request .= $r;
$request .= $xml; $request .= $xml;
// Now send the request // Now send the request
if ($this->debug) { if ($this->debug) {
echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n"; echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
} }
if ($this->timeout) { if ($this->timeout) {
$fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout); $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
} else { } else {
$fp = @fsockopen($this->server, $this->port, $errno, $errstr); $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
} }
if (!$fp) { if (!$fp) {
$this->error = new IXR_Error(-32300, "transport error - could not open socket: $errno $errstr"); $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
return false; return false;
} }
fputs($fp, $request); fputs($fp, $request);
$contents = ''; $contents = '';
$debug_contents = ''; $debugContents = '';
$gotFirstLine = false; $gotFirstLine = false;
$gettingHeaders = true; $gettingHeaders = true;
while (!feof($fp)) { while (!feof($fp)) {
@ -572,7 +676,7 @@ class IXR_Client {
if (!$gotFirstLine) { if (!$gotFirstLine) {
// Check line for '200' // Check line for '200'
if (strstr($line, '200') === false) { if (strstr($line, '200') === false) {
$this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200'); $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
return false; return false;
} }
$gotFirstLine = true; $gotFirstLine = true;
@ -581,16 +685,17 @@ class IXR_Client {
$gettingHeaders = false; $gettingHeaders = false;
} }
if (!$gettingHeaders) { if (!$gettingHeaders) {
// WP#12559 remove trim so as to not strip newlines from received response. // merged from WP #12559 - remove trim
$contents .= $line; $contents .= $line;
} }
if ($this->debug) { if ($this->debug) {
$debug_contents .= $line; $debugContents .= $line;
} }
} }
if ($this->debug) { if ($this->debug) {
echo '<pre class="ixr_response">'.htmlspecialchars($debug_contents)."\n</pre>\n\n"; echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n";
} }
// Now parse what we've got back // Now parse what we've got back
$this->message = new IXR_Message($contents); $this->message = new IXR_Message($contents);
if (!$this->message->parse()) { if (!$this->message->parse()) {
@ -598,44 +703,59 @@ class IXR_Client {
$this->error = new IXR_Error(-32700, 'parse error. not well formed'); $this->error = new IXR_Error(-32700, 'parse error. not well formed');
return false; return false;
} }
// Is the message a fault? // Is the message a fault?
if ($this->message->messageType == 'fault') { if ($this->message->messageType == 'fault') {
$this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
return false; return false;
} }
// Message must be OK // Message must be OK
return true; return true;
} }
function getResponse() {
function getResponse()
{
// methodResponses can only have one param - return that // methodResponses can only have one param - return that
return $this->message->params[0]; return $this->message->params[0];
} }
function isError() {
function isError()
{
return (is_object($this->error)); return (is_object($this->error));
} }
function getErrorCode() {
function getErrorCode()
{
return $this->error->code; return $this->error->code;
} }
function getErrorMessage() {
function getErrorMessage()
{
return $this->error->message; return $this->error->message;
} }
} }
/** /**
* IXR_Error * IXR_Error
* *
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_Error { class IXR_Error
{
var $code; var $code;
var $message; var $message;
function IXR_Error($code, $message) {
function IXR_Error($code, $message)
{
$this->code = $code; $this->code = $code;
// WP adds htmlspecialchars(). See #5666
$this->message = htmlspecialchars($message); $this->message = htmlspecialchars($message);
} }
function getXml() {
function getXml()
{
$xml = <<<EOD $xml = <<<EOD
<methodResponse> <methodResponse>
<fault> <fault>
@ -673,7 +793,9 @@ class IXR_Date {
var $minute; var $minute;
var $second; var $second;
var $timezone; var $timezone;
function IXR_Date($time) {
function IXR_Date($time)
{
// $time can be a PHP timestamp or an ISO one // $time can be a PHP timestamp or an ISO one
if (is_numeric($time)) { if (is_numeric($time)) {
$this->parseTimestamp($time); $this->parseTimestamp($time);
@ -681,34 +803,41 @@ class IXR_Date {
$this->parseIso($time); $this->parseIso($time);
} }
} }
function parseTimestamp($timestamp) {
function parseTimestamp($timestamp)
{
$this->year = date('Y', $timestamp); $this->year = date('Y', $timestamp);
$this->month = date('m', $timestamp); $this->month = date('m', $timestamp);
$this->day = date('d', $timestamp); $this->day = date('d', $timestamp);
$this->hour = date('H', $timestamp); $this->hour = date('H', $timestamp);
$this->minute = date('i', $timestamp); $this->minute = date('i', $timestamp);
$this->second = date('s', $timestamp); $this->second = date('s', $timestamp);
// WP adds timezone. See #2036
$this->timezone = ''; $this->timezone = '';
} }
function parseIso($iso) {
function parseIso($iso)
{
$this->year = substr($iso, 0, 4); $this->year = substr($iso, 0, 4);
$this->month = substr($iso, 4, 2); $this->month = substr($iso, 4, 2);
$this->day = substr($iso, 6, 2); $this->day = substr($iso, 6, 2);
$this->hour = substr($iso, 9, 2); $this->hour = substr($iso, 9, 2);
$this->minute = substr($iso, 12, 2); $this->minute = substr($iso, 12, 2);
$this->second = substr($iso, 15, 2); $this->second = substr($iso, 15, 2);
// WP adds timezone. See #2036
$this->timezone = substr($iso, 17); $this->timezone = substr($iso, 17);
} }
function getIso() {
// WP adds timezone. See #2036 function getIso()
{
return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone; return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone;
} }
function getXml() {
function getXml()
{
return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>'; return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
} }
function getTimestamp() {
function getTimestamp()
{
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year); return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
} }
} }
@ -719,12 +848,17 @@ class IXR_Date {
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_Base64 { class IXR_Base64
{
var $data; var $data;
function IXR_Base64($data) {
function IXR_Base64($data)
{
$this->data = $data; $this->data = $data;
} }
function getXml() {
function getXml()
{
return '<base64>'.base64_encode($this->data).'</base64>'; return '<base64>'.base64_encode($this->data).'</base64>';
} }
} }
@ -735,10 +869,13 @@ class IXR_Base64 {
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_IntrospectionServer extends IXR_Server { class IXR_IntrospectionServer extends IXR_Server
{
var $signatures; var $signatures;
var $help; var $help;
function IXR_IntrospectionServer() {
function IXR_IntrospectionServer()
{
$this->setCallbacks(); $this->setCallbacks();
$this->setCapabilities(); $this->setCapabilities();
$this->capabilities['introspection'] = array( $this->capabilities['introspection'] = array(
@ -770,16 +907,21 @@ class IXR_IntrospectionServer extends IXR_Server {
'Returns a documentation string for the specified method' 'Returns a documentation string for the specified method'
); );
} }
function addCallback($method, $callback, $args, $help) {
function addCallback($method, $callback, $args, $help)
{
$this->callbacks[$method] = $callback; $this->callbacks[$method] = $callback;
$this->signatures[$method] = $args; $this->signatures[$method] = $args;
$this->help[$method] = $help; $this->help[$method] = $help;
} }
function call($methodname, $args) {
function call($methodname, $args)
{
// Make sure it's in an array // Make sure it's in an array
if ($args && !is_array($args)) { if ($args && !is_array($args)) {
$args = array($args); $args = array($args);
} }
// Over-rides default call method, adds signature check // Over-rides default call method, adds signature check
if (!$this->hasMethod($methodname)) { if (!$this->hasMethod($methodname)) {
return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.'); return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.');
@ -787,10 +929,12 @@ class IXR_IntrospectionServer extends IXR_Server {
$method = $this->callbacks[$methodname]; $method = $this->callbacks[$methodname];
$signature = $this->signatures[$methodname]; $signature = $this->signatures[$methodname];
$returnType = array_shift($signature); $returnType = array_shift($signature);
// Check the number of arguments // Check the number of arguments
if (count($args) != count($signature)) { if (count($args) != count($signature)) {
return new IXR_Error(-32602, 'server error. wrong number of method parameters'); return new IXR_Error(-32602, 'server error. wrong number of method parameters');
} }
// Check the argument types // Check the argument types
$ok = true; $ok = true;
$argsbackup = $args; $argsbackup = $args;
@ -835,7 +979,9 @@ class IXR_IntrospectionServer extends IXR_Server {
// It passed the test - run the "real" method call // It passed the test - run the "real" method call
return parent::call($methodname, $argsbackup); return parent::call($methodname, $argsbackup);
} }
function methodSignature($method) {
function methodSignature($method)
{
if (!$this->hasMethod($method)) { if (!$this->hasMethod($method)) {
return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.'); return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
} }
@ -873,7 +1019,9 @@ class IXR_IntrospectionServer extends IXR_Server {
} }
return $return; return $return;
} }
function methodHelp($method) {
function methodHelp($method)
{
return $this->help[$method]; return $this->help[$method];
} }
} }
@ -884,13 +1032,18 @@ class IXR_IntrospectionServer extends IXR_Server {
* @package IXR * @package IXR
* @since 1.5 * @since 1.5
*/ */
class IXR_ClientMulticall extends IXR_Client { class IXR_ClientMulticall extends IXR_Client
{
var $calls = array(); var $calls = array();
function IXR_ClientMulticall($server, $path = false, $port = 80) {
function IXR_ClientMulticall($server, $path = false, $port = 80)
{
parent::IXR_Client($server, $path, $port); parent::IXR_Client($server, $path, $port);
$this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)'; $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
} }
function addCall() {
function addCall()
{
$args = func_get_args(); $args = func_get_args();
$methodName = array_shift($args); $methodName = array_shift($args);
$struct = array( $struct = array(
@ -899,7 +1052,9 @@ class IXR_ClientMulticall extends IXR_Client {
); );
$this->calls[] = $struct; $this->calls[] = $struct;
} }
function query() {
function query()
{
// Prepare multicall, then call the parent::query() method // Prepare multicall, then call the parent::query() method
return parent::query('system.multicall', $this->calls); return parent::query('system.multicall', $this->calls);
} }