3 * IXR - The Inutio XML-RPC Library
8 * @copyright Incutio Ltd 2002-2005
9 * @version 1.7 (beta) 23rd May 2005
10 * @author Simon Willison
11 * @link http://scripts.incutio.com/xmlrpc/ Site
12 * @link http://scripts.incutio.com/xmlrpc/manual.php Manual
13 * @license BSD License http://www.opensource.org/licenses/bsd-license.php
26 function IXR_Value ($data, $type = false) {
29 $type = $this->calculateType();
32 if ($type == 'struct') {
33 /* Turn all the values in the array in to new IXR_Value objects */
34 foreach ($this->data as $key => $value) {
35 $this->data[$key] = new IXR_Value($value);
38 if ($type == 'array') {
39 for ($i = 0, $j = count($this->data); $i < $j; $i++) {
40 $this->data[$i] = new IXR_Value($this->data[$i]);
45 function calculateType() {
46 if ($this->data === true || $this->data === false) {
49 if (is_integer($this->data)) {
52 if (is_double($this->data)) {
55 // Deal with IXR object types base64 and date
56 if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
59 if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
62 // If it is a normal PHP object convert it in to a struct
63 if (is_object($this->data)) {
65 $this->data = get_object_vars($this->data);
68 if (!is_array($this->data)) {
71 /* We have an array - is it an array or a struct ? */
72 if ($this->isStruct($this->data)) {
80 /* Return XML for this value */
81 switch ($this->type) {
83 return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>';
86 return '<int>'.$this->data.'</int>';
89 return '<double>'.$this->data.'</double>';
92 return '<string>'.htmlspecialchars($this->data).'</string>';
95 $return = '<array><data>'."\n";
96 foreach ($this->data as $item) {
97 $return .= ' <value>'.$item->getXml()."</value>\n";
99 $return .= '</data></array>';
103 $return = '<struct>'."\n";
104 foreach ($this->data as $name => $value) {
105 $name = htmlspecialchars($name);
106 $return .= " <member><name>$name</name><value>";
107 $return .= $value->getXml()."</value></member>\n";
109 $return .= '</struct>';
114 return $this->data->getXml();
120 function isStruct($array) {
121 /* Nasty function to check if an array is a struct or not */
123 foreach ($array as $key => $value) {
124 if ((string)$key != (string)$expected) {
141 var $messageType; // methodCall / methodResponse / fault
146 // Current variable stacks
147 var $_arraystructs = array(); // The stack used to keep track of the current array/struct
148 var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
149 var $_currentStructName = array(); // A stack as well
153 var $_currentTagContents;
156 function IXR_Message ($message) {
157 $this->message = $message;
160 // first remove the XML declaration
161 $this->message = preg_replace('/<\?xml.*?\?'.'>/', '', $this->message);
162 if (trim($this->message) == '') {
165 $this->_parser = xml_parser_create();
166 // Set XML parser to take the case of tags in to account
167 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
168 // Set XML parser callback functions
169 xml_set_object($this->_parser, $this);
170 xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
171 xml_set_character_data_handler($this->_parser, 'cdata');
172 if (!xml_parse($this->_parser, $this->message)) {
173 /* die(sprintf('XML error: %s at line %d',
174 xml_error_string(xml_get_error_code($this->_parser)),
175 xml_get_current_line_number($this->_parser))); */
178 xml_parser_free($this->_parser);
179 // Grab the error messages, if any
180 if ($this->messageType == 'fault') {
181 $this->faultCode = $this->params[0]['faultCode'];
182 $this->faultString = $this->params[0]['faultString'];
186 function tag_open($parser, $tag, $attr) {
187 $this->_currentTagContents = '';
188 $this->currentTag = $tag;
191 case 'methodResponse':
193 $this->messageType = $tag;
195 /* Deal with stacks of arrays and structs */
196 case 'data': // data is to all intents and puposes more interesting than array
197 $this->_arraystructstypes[] = 'array';
198 $this->_arraystructs[] = array();
201 $this->_arraystructstypes[] = 'struct';
202 $this->_arraystructs[] = array();
206 function cdata($parser, $cdata) {
207 $this->_currentTagContents .= $cdata;
209 function tag_close($parser, $tag) {
214 $value = (int) trim($this->_currentTagContents);
218 $value = (double) trim($this->_currentTagContents);
222 $value = $this->_currentTagContents;
225 case 'dateTime.iso8601':
226 $value = new IXR_Date(trim($this->_currentTagContents));
227 // $value = $iso->getTimestamp();
231 // "If no type is indicated, the type is string."
232 if (trim($this->_currentTagContents) != '') {
233 $value = (string)$this->_currentTagContents;
238 $value = (boolean) trim($this->_currentTagContents);
242 $value = base64_decode( trim( $this->_currentTagContents ) );
245 /* Deal with stacks of arrays and structs */
248 $value = array_pop($this->_arraystructs);
249 array_pop($this->_arraystructstypes);
253 array_pop($this->_currentStructName);
256 $this->_currentStructName[] = trim($this->_currentTagContents);
259 $this->methodName = trim($this->_currentTagContents);
263 if (count($this->_arraystructs) > 0) {
264 // Add value to struct or array
265 if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
267 $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value;
270 $this->_arraystructs[count($this->_arraystructs)-1][] = $value;
273 // Just add as a paramater
274 $this->params[] = $value;
277 $this->_currentTagContents = '';
289 var $callbacks = array();
292 function IXR_Server($callbacks = false, $data = false) {
293 $this->setCapabilities();
295 $this->callbacks = $callbacks;
297 $this->setCallbacks();
300 function serve($data = false) {
302 global $HTTP_RAW_POST_DATA;
303 if (!$HTTP_RAW_POST_DATA) {
304 header( 'Content-Type: text/plain' );
305 die('XML-RPC server accepts POST requests only.');
307 $data = $HTTP_RAW_POST_DATA;
309 $this->message = new IXR_Message($data);
310 if (!$this->message->parse()) {
311 $this->error(-32700, 'parse error. not well formed');
313 if ($this->message->messageType != 'methodCall') {
314 $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
316 $result = $this->call($this->message->methodName, $this->message->params);
317 // Is the result an error?
318 if (is_a($result, 'IXR_Error')) {
319 $this->error($result);
322 $r = new IXR_Value($result);
323 $resultxml = $r->getXml();
340 function call($methodname, $args) {
341 if (!$this->hasMethod($methodname)) {
342 return new IXR_Error(-32601, 'server error. requested method '.
343 $methodname.' does not exist.');
345 $method = $this->callbacks[$methodname];
346 // Perform the callback and send the response
347 if (count($args) == 1) {
348 // If only one paramater just send that instead of the whole array
351 // Are we dealing with a function or a method?
352 if (substr($method, 0, 5) == 'this:') {
353 // It's a class method - check it exists
354 $method = substr($method, 5);
355 if (!method_exists($this, $method)) {
356 return new IXR_Error(-32601, 'server error. requested class method "'.
357 $method.'" does not exist.');
360 $result = $this->$method($args);
362 // It's a function - does it exist?
363 if (is_array($method)) {
364 if (!method_exists($method[0], $method[1])) {
365 return new IXR_Error(-32601, 'server error. requested object method "'.
366 $method[1].'" does not exist.');
368 } else if (!function_exists($method)) {
369 return new IXR_Error(-32601, 'server error. requested function "'.
370 $method.'" does not exist.');
373 $result = call_user_func($method, $args);
378 function error($error, $message = false) {
379 // Accepts either an error object or an error code and message
380 if ($message && !is_object($error)) {
381 $error = new IXR_Error($error, $message);
383 $this->output($error->getXml());
385 function output($xml) {
386 $xml = '<?xml version="1.0"?>'."\n".$xml;
387 $length = strlen($xml);
388 header('Connection: close');
389 header('Content-Length: '.$length);
390 header('Content-Type: text/xml');
391 header('Date: '.date('r'));
395 function hasMethod($method) {
396 return in_array($method, array_keys($this->callbacks));
398 function setCapabilities() {
399 // Initialises capabilities array
400 $this->capabilities = array(
402 'specUrl' => 'http://www.xmlrpc.com/spec',
405 'faults_interop' => array(
406 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
407 'specVersion' => 20010516
409 'system.multicall' => array(
410 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
415 function getCapabilities($args) {
416 return $this->capabilities;
418 function setCallbacks() {
419 $this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
420 $this->callbacks['system.listMethods'] = 'this:listMethods';
421 $this->callbacks['system.multicall'] = 'this:multiCall';
423 function listMethods($args) {
424 // Returns a list of methods - uses array_reverse to ensure user defined
425 // methods are listed before server defined methods
426 return array_reverse(array_keys($this->callbacks));
428 function multiCall($methodcalls) {
429 // See http://www.xmlrpc.com/discuss/msgReader$1208
431 foreach ($methodcalls as $call) {
432 $method = $call['methodName'];
433 $params = $call['params'];
434 if ($method == 'system.multicall') {
435 $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
437 $result = $this->call($method, $params);
439 if (is_a($result, 'IXR_Error')) {
441 'faultCode' => $result->code,
442 'faultString' => $result->message
445 $return[] = array($result);
462 function IXR_Request($method, $args) {
463 $this->method = $method;
466 <?xml version="1.0"?>
468 <methodName>{$this->method}</methodName>
472 foreach ($this->args as $arg) {
473 $this->xml .= '<param><value>';
474 $v = new IXR_Value($arg);
475 $this->xml .= $v->getXml();
476 $this->xml .= "</value></param>\n";
478 $this->xml .= '</params></methodCall>';
480 function getLength() {
481 return strlen($this->xml);
501 var $message = false;
504 // Storage place for an error message
506 function IXR_Client($server, $path = false, $port = 80, $timeout = false) {
508 // Assume we have been given a URL instead
509 $bits = parse_url($server);
510 $this->server = $bits['host'];
511 $this->port = isset($bits['port']) ? $bits['port'] : 80;
512 $this->path = isset($bits['path']) ? $bits['path'] : '/';
513 // Make absolutely sure we have a path
518 $this->server = $server;
522 $this->useragent = 'The Incutio XML-RPC PHP Library';
523 $this->timeout = $timeout;
526 $args = func_get_args();
527 $method = array_shift($args);
528 $request = new IXR_Request($method, $args);
529 $length = $request->getLength();
530 $xml = $request->getXml();
532 $request = "POST {$this->path} HTTP/1.0$r";
534 $this->headers['Host'] = $this->server;
535 $this->headers['Content-Type'] = 'text/xml';
536 $this->headers['User-Agent'] = $this->useragent;
537 $this->headers['Content-Length']= $length;
539 foreach( $this->headers as $header => $value ) {
540 $request .= "{$header}: {$value}{$r}";
545 // Now send the request
547 echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
549 if ($this->timeout) {
550 $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
552 $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
555 $this->error = new IXR_Error(-32300, "transport error - could not open socket: $errno $errstr");
558 fputs($fp, $request);
560 $debug_contents = '';
561 $gotFirstLine = false;
562 $gettingHeaders = true;
564 $line = fgets($fp, 4096);
565 if (!$gotFirstLine) {
566 // Check line for '200'
567 if (strstr($line, '200') === false) {
568 $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200');
571 $gotFirstLine = true;
573 if (trim($line) == '') {
574 $gettingHeaders = false;
576 if (!$gettingHeaders) {
577 $contents .= trim($line);
580 $debug_contents .= $line;
584 echo '<pre class="ixr_response">'.htmlspecialchars($debug_contents)."\n</pre>\n\n";
586 // Now parse what we've got back
587 $this->message = new IXR_Message($contents);
588 if (!$this->message->parse()) {
590 $this->error = new IXR_Error(-32700, 'parse error. not well formed');
593 // Is the message a fault?
594 if ($this->message->messageType == 'fault') {
595 $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
598 // Message must be OK
601 function getResponse() {
602 // methodResponses can only have one param - return that
603 return $this->message->params[0];
606 return (is_object($this->error));
608 function getErrorCode() {
609 return $this->error->code;
611 function getErrorMessage() {
612 return $this->error->message;
625 function IXR_Error($code, $message) {
627 // WP adds htmlspecialchars(). See #5666
628 $this->message = htmlspecialchars($message);
637 <name>faultCode</name>
638 <value><int>{$this->code}</int></value>
641 <name>faultString</name>
642 <value><string>{$this->message}</string></value>
668 function IXR_Date($time) {
669 // $time can be a PHP timestamp or an ISO one
670 if (is_numeric($time)) {
671 $this->parseTimestamp($time);
673 $this->parseIso($time);
676 function parseTimestamp($timestamp) {
677 $this->year = date('Y', $timestamp);
678 $this->month = date('m', $timestamp);
679 $this->day = date('d', $timestamp);
680 $this->hour = date('H', $timestamp);
681 $this->minute = date('i', $timestamp);
682 $this->second = date('s', $timestamp);
683 // WP adds timezone. See #2036
684 $this->timezone = '';
686 function parseIso($iso) {
687 $this->year = substr($iso, 0, 4);
688 $this->month = substr($iso, 4, 2);
689 $this->day = substr($iso, 6, 2);
690 $this->hour = substr($iso, 9, 2);
691 $this->minute = substr($iso, 12, 2);
692 $this->second = substr($iso, 15, 2);
693 // WP adds timezone. See #2036
694 $this->timezone = substr($iso, 17);
697 // WP adds timezone. See #2036
698 return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone;
701 return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
703 function getTimestamp() {
704 return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
716 function IXR_Base64($data) {
720 return '<base64>'.base64_encode($this->data).'</base64>';
725 * IXR_IntrospectionServer
730 class IXR_IntrospectionServer extends IXR_Server {
733 function IXR_IntrospectionServer() {
734 $this->setCallbacks();
735 $this->setCapabilities();
736 $this->capabilities['introspection'] = array(
737 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html',
741 'system.methodSignature',
742 'this:methodSignature',
743 array('array', 'string'),
744 'Returns an array describing the return type and required parameters of a method'
747 'system.getCapabilities',
748 'this:getCapabilities',
750 'Returns a struct describing the XML-RPC specifications supported by this server'
753 'system.listMethods',
756 'Returns an array of available methods on this server'
761 array('string', 'string'),
762 'Returns a documentation string for the specified method'
765 function addCallback($method, $callback, $args, $help) {
766 $this->callbacks[$method] = $callback;
767 $this->signatures[$method] = $args;
768 $this->help[$method] = $help;
770 function call($methodname, $args) {
771 // Make sure it's in an array
772 if ($args && !is_array($args)) {
773 $args = array($args);
775 // Over-rides default call method, adds signature check
776 if (!$this->hasMethod($methodname)) {
777 return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.');
779 $method = $this->callbacks[$methodname];
780 $signature = $this->signatures[$methodname];
781 $returnType = array_shift($signature);
782 // Check the number of arguments
783 if (count($args) != count($signature)) {
784 return new IXR_Error(-32602, 'server error. wrong number of method parameters');
786 // Check the argument types
789 for ($i = 0, $j = count($args); $i < $j; $i++) {
790 $arg = array_shift($args);
791 $type = array_shift($signature);
795 if (is_array($arg) || !is_int($arg)) {
801 if (!is_string($arg)) {
806 if ($arg !== false && $arg !== true) {
812 if (!is_float($arg)) {
817 case 'dateTime.iso8601':
818 if (!is_a($arg, 'IXR_Date')) {
824 return new IXR_Error(-32602, 'server error. invalid method parameters');
827 // It passed the test - run the "real" method call
828 return parent::call($methodname, $argsbackup);
830 function methodSignature($method) {
831 if (!$this->hasMethod($method)) {
832 return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
834 // We should be returning an array of types
835 $types = $this->signatures[$method];
837 foreach ($types as $type) {
840 $return[] = 'string';
849 case 'dateTime.iso8601':
850 $return[] = new IXR_Date(time());
856 $return[] = new IXR_Base64('base64');
859 $return[] = array('array');
862 $return[] = array('struct' => 'struct');
868 function methodHelp($method) {
869 return $this->help[$method];
874 * IXR_ClientMulticall
879 class IXR_ClientMulticall extends IXR_Client {
880 var $calls = array();
881 function IXR_ClientMulticall($server, $path = false, $port = 80) {
882 parent::IXR_Client($server, $path, $port);
883 $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
886 $args = func_get_args();
887 $methodName = array_shift($args);
889 'methodName' => $methodName,
892 $this->calls[] = $struct;
895 // Prepare multicall, then call the parent::query() method
896 return parent::query('system.multicall', $this->calls);