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 die('XML-RPC server accepts POST requests only.');
306 $data = $HTTP_RAW_POST_DATA;
308 $this->message = new IXR_Message($data);
309 if (!$this->message->parse()) {
310 $this->error(-32700, 'parse error. not well formed');
312 if ($this->message->messageType != 'methodCall') {
313 $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
315 $result = $this->call($this->message->methodName, $this->message->params);
316 // Is the result an error?
317 if (is_a($result, 'IXR_Error')) {
318 $this->error($result);
321 $r = new IXR_Value($result);
322 $resultxml = $r->getXml();
339 function call($methodname, $args) {
340 if (!$this->hasMethod($methodname)) {
341 return new IXR_Error(-32601, 'server error. requested method '.
342 $methodname.' does not exist.');
344 $method = $this->callbacks[$methodname];
345 // Perform the callback and send the response
346 if (count($args) == 1) {
347 // If only one paramater just send that instead of the whole array
350 // Are we dealing with a function or a method?
351 if (substr($method, 0, 5) == 'this:') {
352 // It's a class method - check it exists
353 $method = substr($method, 5);
354 if (!method_exists($this, $method)) {
355 return new IXR_Error(-32601, 'server error. requested class method "'.
356 $method.'" does not exist.');
359 $result = $this->$method($args);
361 // It's a function - does it exist?
362 if (is_array($method)) {
363 if (!method_exists($method[0], $method[1])) {
364 return new IXR_Error(-32601, 'server error. requested object method "'.
365 $method[1].'" does not exist.');
367 } else if (!function_exists($method)) {
368 return new IXR_Error(-32601, 'server error. requested function "'.
369 $method.'" does not exist.');
372 $result = call_user_func($method, $args);
377 function error($error, $message = false) {
378 // Accepts either an error object or an error code and message
379 if ($message && !is_object($error)) {
380 $error = new IXR_Error($error, $message);
382 $this->output($error->getXml());
384 function output($xml) {
385 $xml = '<?xml version="1.0"?>'."\n".$xml;
386 $length = strlen($xml);
387 header('Connection: close');
388 header('Content-Length: '.$length);
389 header('Content-Type: text/xml');
390 header('Date: '.date('r'));
394 function hasMethod($method) {
395 return in_array($method, array_keys($this->callbacks));
397 function setCapabilities() {
398 // Initialises capabilities array
399 $this->capabilities = array(
401 'specUrl' => 'http://www.xmlrpc.com/spec',
404 'faults_interop' => array(
405 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
406 'specVersion' => 20010516
408 'system.multicall' => array(
409 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
414 function getCapabilities($args) {
415 return $this->capabilities;
417 function setCallbacks() {
418 $this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
419 $this->callbacks['system.listMethods'] = 'this:listMethods';
420 $this->callbacks['system.multicall'] = 'this:multiCall';
422 function listMethods($args) {
423 // Returns a list of methods - uses array_reverse to ensure user defined
424 // methods are listed before server defined methods
425 return array_reverse(array_keys($this->callbacks));
427 function multiCall($methodcalls) {
428 // See http://www.xmlrpc.com/discuss/msgReader$1208
430 foreach ($methodcalls as $call) {
431 $method = $call['methodName'];
432 $params = $call['params'];
433 if ($method == 'system.multicall') {
434 $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
436 $result = $this->call($method, $params);
438 if (is_a($result, 'IXR_Error')) {
440 'faultCode' => $result->code,
441 'faultString' => $result->message
444 $return[] = array($result);
461 function IXR_Request($method, $args) {
462 $this->method = $method;
465 <?xml version="1.0"?>
467 <methodName>{$this->method}</methodName>
471 foreach ($this->args as $arg) {
472 $this->xml .= '<param><value>';
473 $v = new IXR_Value($arg);
474 $this->xml .= $v->getXml();
475 $this->xml .= "</value></param>\n";
477 $this->xml .= '</params></methodCall>';
479 function getLength() {
480 return strlen($this->xml);
499 var $message = false;
502 // Storage place for an error message
504 function IXR_Client($server, $path = false, $port = 80, $timeout = false) {
506 // Assume we have been given a URL instead
507 $bits = parse_url($server);
508 $this->server = $bits['host'];
509 $this->port = isset($bits['port']) ? $bits['port'] : 80;
510 $this->path = isset($bits['path']) ? $bits['path'] : '/';
511 // Make absolutely sure we have a path
516 $this->server = $server;
520 $this->useragent = 'The Incutio XML-RPC PHP Library';
521 $this->timeout = $timeout;
524 $args = func_get_args();
525 $method = array_shift($args);
526 $request = new IXR_Request($method, $args);
527 $length = $request->getLength();
528 $xml = $request->getXml();
530 $request = "POST {$this->path} HTTP/1.0$r";
531 $request .= "Host: {$this->server}$r";
532 $request .= "Content-Type: text/xml$r";
533 $request .= "User-Agent: {$this->useragent}$r";
534 $request .= "Content-length: {$length}$r$r";
536 // Now send the request
538 echo '<pre>'.htmlspecialchars($request)."\n</pre>\n\n";
540 if ($this->timeout) {
541 $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
543 $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
546 $this->error = new IXR_Error(-32300, "transport error - could not open socket: $errno $errstr");
549 fputs($fp, $request);
551 $gotFirstLine = false;
552 $gettingHeaders = true;
554 $line = fgets($fp, 4096);
555 if (!$gotFirstLine) {
556 // Check line for '200'
557 if (strstr($line, '200') === false) {
558 $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
561 $gotFirstLine = true;
563 if (trim($line) == '') {
564 $gettingHeaders = false;
566 if (!$gettingHeaders) {
567 $contents .= trim($line);
571 echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n";
573 // Now parse what we've got back
574 $this->message = new IXR_Message($contents);
575 if (!$this->message->parse()) {
577 $this->error = new IXR_Error(-32700, 'parse error. not well formed');
580 // Is the message a fault?
581 if ($this->message->messageType == 'fault') {
582 $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
585 // Message must be OK
588 function getResponse() {
589 // methodResponses can only have one param - return that
590 return $this->message->params[0];
593 return (is_object($this->error));
595 function getErrorCode() {
596 return $this->error->code;
598 function getErrorMessage() {
599 return $this->error->message;
612 function IXR_Error($code, $message) {
614 // WP adds htmlspecialchars(). See #5666
615 $this->message = htmlspecialchars($message);
624 <name>faultCode</name>
625 <value><int>{$this->code}</int></value>
628 <name>faultString</name>
629 <value><string>{$this->message}</string></value>
655 function IXR_Date($time) {
656 // $time can be a PHP timestamp or an ISO one
657 if (is_numeric($time)) {
658 $this->parseTimestamp($time);
660 $this->parseIso($time);
663 function parseTimestamp($timestamp) {
664 $this->year = date('Y', $timestamp);
665 $this->month = date('m', $timestamp);
666 $this->day = date('d', $timestamp);
667 $this->hour = date('H', $timestamp);
668 $this->minute = date('i', $timestamp);
669 $this->second = date('s', $timestamp);
670 // WP adds timezone. See #2036
671 $this->timezone = '';
673 function parseIso($iso) {
674 $this->year = substr($iso, 0, 4);
675 $this->month = substr($iso, 4, 2);
676 $this->day = substr($iso, 6, 2);
677 $this->hour = substr($iso, 9, 2);
678 $this->minute = substr($iso, 12, 2);
679 $this->second = substr($iso, 15, 2);
680 // WP adds timezone. See #2036
681 $this->timezone = substr($iso, 17);
684 // WP adds timezone. See #2036
685 return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second.$this->timezone;
688 return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
690 function getTimestamp() {
691 return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
703 function IXR_Base64($data) {
707 return '<base64>'.base64_encode($this->data).'</base64>';
712 * IXR_IntrospectionServer
717 class IXR_IntrospectionServer extends IXR_Server {
720 function IXR_IntrospectionServer() {
721 $this->setCallbacks();
722 $this->setCapabilities();
723 $this->capabilities['introspection'] = array(
724 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html',
728 'system.methodSignature',
729 'this:methodSignature',
730 array('array', 'string'),
731 'Returns an array describing the return type and required parameters of a method'
734 'system.getCapabilities',
735 'this:getCapabilities',
737 'Returns a struct describing the XML-RPC specifications supported by this server'
740 'system.listMethods',
743 'Returns an array of available methods on this server'
748 array('string', 'string'),
749 'Returns a documentation string for the specified method'
752 function addCallback($method, $callback, $args, $help) {
753 $this->callbacks[$method] = $callback;
754 $this->signatures[$method] = $args;
755 $this->help[$method] = $help;
757 function call($methodname, $args) {
758 // Make sure it's in an array
759 if ($args && !is_array($args)) {
760 $args = array($args);
762 // Over-rides default call method, adds signature check
763 if (!$this->hasMethod($methodname)) {
764 return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.');
766 $method = $this->callbacks[$methodname];
767 $signature = $this->signatures[$methodname];
768 $returnType = array_shift($signature);
769 // Check the number of arguments
770 if (count($args) != count($signature)) {
771 return new IXR_Error(-32602, 'server error. wrong number of method parameters');
773 // Check the argument types
776 for ($i = 0, $j = count($args); $i < $j; $i++) {
777 $arg = array_shift($args);
778 $type = array_shift($signature);
782 if (is_array($arg) || !is_int($arg)) {
788 if (!is_string($arg)) {
793 if ($arg !== false && $arg !== true) {
799 if (!is_float($arg)) {
804 case 'dateTime.iso8601':
805 if (!is_a($arg, 'IXR_Date')) {
811 return new IXR_Error(-32602, 'server error. invalid method parameters');
814 // It passed the test - run the "real" method call
815 return parent::call($methodname, $argsbackup);
817 function methodSignature($method) {
818 if (!$this->hasMethod($method)) {
819 return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
821 // We should be returning an array of types
822 $types = $this->signatures[$method];
824 foreach ($types as $type) {
827 $return[] = 'string';
836 case 'dateTime.iso8601':
837 $return[] = new IXR_Date(time());
843 $return[] = new IXR_Base64('base64');
846 $return[] = array('array');
849 $return[] = array('struct' => 'struct');
855 function methodHelp($method) {
856 return $this->help[$method];
861 * IXR_ClientMulticall
866 class IXR_ClientMulticall extends IXR_Client {
867 var $calls = array();
868 function IXR_ClientMulticall($server, $path = false, $port = 80) {
869 parent::IXR_Client($server, $path, $port);
870 $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
873 $args = func_get_args();
874 $methodName = array_shift($args);
876 'methodName' => $methodName,
879 $this->calls[] = $struct;
882 // Prepare multicall, then call the parent::query() method
883 return parent::query('system.multicall', $this->calls);