-/**
- * IXR_Value
- *
- * @package IXR
- * @since 1.5.0
- */
-class IXR_Value {
- var $data;
- var $type;
-
- function IXR_Value($data, $type = false)
- {
- $this->data = $data;
- if (!$type) {
- $type = $this->calculateType();
- }
- $this->type = $type;
- if ($type == 'struct') {
- // Turn all the values in the array in to new IXR_Value objects
- foreach ($this->data as $key => $value) {
- $this->data[$key] = new IXR_Value($value);
- }
- }
- if ($type == 'array') {
- for ($i = 0, $j = count($this->data); $i < $j; $i++) {
- $this->data[$i] = new IXR_Value($this->data[$i]);
- }
- }
- }
-
- function calculateType()
- {
- if ($this->data === true || $this->data === false) {
- return 'boolean';
- }
- if (is_integer($this->data)) {
- return 'int';
- }
- if (is_double($this->data)) {
- return 'double';
- }
-
- // Deal with IXR object types base64 and date
- if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
- return 'date';
- }
- if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
- return 'base64';
- }
-
- // If it is a normal PHP object convert it in to a struct
- if (is_object($this->data)) {
- $this->data = get_object_vars($this->data);
- return 'struct';
- }
- if (!is_array($this->data)) {
- return 'string';
- }
-
- // We have an array - is it an array or a struct?
- if ($this->isStruct($this->data)) {
- return 'struct';
- } else {
- return 'array';
- }
- }
-
- function getXml()
- {
- // Return XML for this value
- switch ($this->type) {
- case 'boolean':
- return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>';
- break;
- case 'int':
- return '<int>'.$this->data.'</int>';
- break;
- case 'double':
- return '<double>'.$this->data.'</double>';
- break;
- case 'string':
- return '<string>'.htmlspecialchars($this->data).'</string>';
- break;
- case 'array':
- $return = '<array><data>'."\n";
- foreach ($this->data as $item) {
- $return .= ' <value>'.$item->getXml()."</value>\n";
- }
- $return .= '</data></array>';
- return $return;
- break;
- case 'struct':
- $return = '<struct>'."\n";
- foreach ($this->data as $name => $value) {
- $name = htmlspecialchars($name);
- $return .= " <member><name>$name</name><value>";
- $return .= $value->getXml()."</value></member>\n";
- }
- $return .= '</struct>';
- return $return;
- break;
- case 'date':
- case 'base64':
- return $this->data->getXml();
- break;
- }
- return false;
- }
-
- /**
- * Checks whether or not the supplied array is a struct or not
- *
- * @param array $array
- * @return boolean
- */
- function isStruct($array)
- {
- $expected = 0;
- foreach ($array as $key => $value) {
- if ((string)$key != (string)$expected) {
- return true;
- }
- $expected++;
- }
- return false;
- }
-}
-
-/**
- * IXR_MESSAGE
- *
- * @package IXR
- * @since 1.5.0
- *
- */
-class IXR_Message
-{
- var $message;
- var $messageType; // methodCall / methodResponse / fault
- var $faultCode;
- var $faultString;
- var $methodName;
- var $params;
-
- // Current variable stacks
- 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 $_currentStructName = array(); // A stack as well
- var $_param;
- var $_value;
- var $_currentTag;
- var $_currentTagContents;
- // The XML parser
- var $_parser;
-
- function IXR_Message($message)
- {
- $this->message =& $message;
- }
-
- function parse()
- {
- // first remove the XML declaration
- // merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages
- $header = preg_replace( '/<\?xml.*?\?'.'>/s', '', substr( $this->message, 0, 100 ), 1 );
- $this->message = trim( substr_replace( $this->message, $header, 0, 100 ) );
- if ( '' == $this->message ) {
- return false;
- }
-
- // Then remove the DOCTYPE
- $header = preg_replace( '/^<!DOCTYPE[^>]*+>/i', '', substr( $this->message, 0, 200 ), 1 );
- $this->message = trim( substr_replace( $this->message, $header, 0, 200 ) );
- if ( '' == $this->message ) {
- return false;
- }
-
- // Check that the root tag is valid
- $root_tag = substr( $this->message, 0, strcspn( substr( $this->message, 0, 20 ), "> \t\r\n" ) );
- if ( '<!DOCTYPE' === strtoupper( $root_tag ) ) {
- return false;
- }
- if ( ! in_array( $root_tag, array( '<methodCall', '<methodResponse', '<fault' ) ) ) {
- return false;
- }
-
- // Bail if there are too many elements to parse
- $element_limit = 30000;
- if ( function_exists( 'apply_filters' ) ) {
- /**
- * Filter the number of elements to parse in an XML-RPC response.
- *
- * @since 4.0.0
- *
- * @param int $element_limit Default elements limit.
- */
- $element_limit = apply_filters( 'xmlrpc_element_limit', $element_limit );
- }
- if ( $element_limit && 2 * $element_limit < substr_count( $this->message, '<' ) ) {
- return false;
- }
-
- $this->_parser = xml_parser_create();
- // Set XML parser to take the case of tags in to account
- xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
- // Set XML parser callback functions
- xml_set_object($this->_parser, $this);
- xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
- xml_set_character_data_handler($this->_parser, 'cdata');
- $chunk_size = 262144; // 256Kb, parse in chunks to avoid the RAM usage on very large messages
- $final = false;
- do {
- if (strlen($this->message) <= $chunk_size) {
- $final = true;
- }
- $part = substr($this->message, 0, $chunk_size);
- $this->message = substr($this->message, $chunk_size);
- if (!xml_parse($this->_parser, $part, $final)) {
- return false;
- }
- if ($final) {
- break;
- }
- } while (true);
- xml_parser_free($this->_parser);
-
- // Grab the error messages, if any
- if ($this->messageType == 'fault') {
- $this->faultCode = $this->params[0]['faultCode'];
- $this->faultString = $this->params[0]['faultString'];
- }
- return true;
- }
-
- function tag_open($parser, $tag, $attr)
- {
- $this->_currentTagContents = '';
- $this->currentTag = $tag;
- switch($tag) {
- case 'methodCall':
- case 'methodResponse':
- case 'fault':
- $this->messageType = $tag;
- break;
- /* Deal with stacks of arrays and structs */
- case 'data': // data is to all intents and puposes more interesting than array
- $this->_arraystructstypes[] = 'array';
- $this->_arraystructs[] = array();
- break;
- case 'struct':
- $this->_arraystructstypes[] = 'struct';
- $this->_arraystructs[] = array();
- break;
- }
- }
-
- function cdata($parser, $cdata)
- {
- $this->_currentTagContents .= $cdata;
- }
-
- function tag_close($parser, $tag)
- {
- $valueFlag = false;
- switch($tag) {
- case 'int':
- case 'i4':
- $value = (int)trim($this->_currentTagContents);
- $valueFlag = true;
- break;
- case 'double':
- $value = (double)trim($this->_currentTagContents);
- $valueFlag = true;
- break;
- case 'string':
- $value = (string)trim($this->_currentTagContents);
- $valueFlag = true;
- break;
- case 'dateTime.iso8601':
- $value = new IXR_Date(trim($this->_currentTagContents));
- $valueFlag = true;
- break;
- case 'value':
- // "If no type is indicated, the type is string."
- if (trim($this->_currentTagContents) != '') {
- $value = (string)$this->_currentTagContents;
- $valueFlag = true;
- }
- break;
- case 'boolean':
- $value = (boolean)trim($this->_currentTagContents);
- $valueFlag = true;
- break;
- case 'base64':
- $value = base64_decode($this->_currentTagContents);
- $valueFlag = true;
- break;
- /* Deal with stacks of arrays and structs */
- case 'data':
- case 'struct':
- $value = array_pop($this->_arraystructs);
- array_pop($this->_arraystructstypes);
- $valueFlag = true;
- break;
- case 'member':
- array_pop($this->_currentStructName);
- break;
- case 'name':
- $this->_currentStructName[] = trim($this->_currentTagContents);
- break;
- case 'methodName':
- $this->methodName = trim($this->_currentTagContents);
- break;
- }
-
- if ($valueFlag) {
- if (count($this->_arraystructs) > 0) {
- // Add value to struct or array
- if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
- // Add to struct
- $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value;
- } else {
- // Add to array
- $this->_arraystructs[count($this->_arraystructs)-1][] = $value;
- }
- } else {
- // Just add as a parameter
- $this->params[] = $value;
- }
- }
- $this->_currentTagContents = '';
- }
-}
-
-/**
- * IXR_Server
- *
- * @package IXR
- * @since 1.5.0
- */
-class IXR_Server
-{
- var $data;
- var $callbacks = array();
- var $message;
- var $capabilities;
-
- function IXR_Server($callbacks = false, $data = false, $wait = false)
- {
- $this->setCapabilities();
- if ($callbacks) {
- $this->callbacks = $callbacks;
- }
- $this->setCallbacks();
- if (!$wait) {
- $this->serve($data);
- }
- }
-
- function serve($data = false)
- {
- if (!$data) {
- if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
- header('Content-Type: text/plain'); // merged from WP #9093
- 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;
- }
- }
- $this->message = new IXR_Message($data);
- if (!$this->message->parse()) {
- $this->error(-32700, 'parse error. not well formed');
- }
- if ($this->message->messageType != '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);
-
- // Is the result an error?
- if (is_a($result, 'IXR_Error')) {
- $this->error($result);
- }
-
- // Encode the result
- $r = new IXR_Value($result);
- $resultxml = $r->getXml();
-
- // Create the XML
- $xml = <<<EOD
-<methodResponse>
- <params>
- <param>
- <value>
- $resultxml
- </value>
- </param>
- </params>
-</methodResponse>
-
-EOD;
- // Send it
- $this->output($xml);
- }
-
- function call($methodname, $args)
- {
- if (!$this->hasMethod($methodname)) {
- return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
- }
- $method = $this->callbacks[$methodname];
-
- // Perform the callback and send the response
- if (count($args) == 1) {
- // If only one parameter just send that instead of the whole array
- $args = $args[0];
- }
-
- // Are we dealing with a function or a method?
- if (is_string($method) && substr($method, 0, 5) == 'this:') {
- // It's a class method - check it exists
- $method = substr($method, 5);
- if (!method_exists($this, $method)) {
- return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
- }
-
- //Call the method
- $result = $this->$method($args);
- } else {
- // It's a function - does it exist?
- if (is_array($method)) {
- if (!is_callable(array($method[0], $method[1]))) {
- return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.');
- }
- } else if (!function_exists($method)) {
- return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
- }
-
- // Call the function
- $result = call_user_func($method, $args);
- }
- return $result;
- }
-
- function error($error, $message = false)
- {
- // Accepts either an error object or an error code and message
- if ($message && !is_object($error)) {
- $error = new IXR_Error($error, $message);
- }
- $this->output($error->getXml());
- }
-
- function output($xml)
- {
- $charset = function_exists('get_option') ? get_option('blog_charset') : '';
- if ($charset)
- $xml = '<?xml version="1.0" encoding="'.$charset.'"?>'."\n".$xml;
- else
- $xml = '<?xml version="1.0"?>'."\n".$xml;
- $length = strlen($xml);
- header('Connection: close');
- header('Content-Length: '.$length);
- if ($charset)
- header('Content-Type: text/xml; charset='.$charset);
- else
- header('Content-Type: text/xml');
- header('Date: '.date('r'));
- echo $xml;
- exit;
- }