3 * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002
6 * @author Simon Willison
8 * @link http://scripts.incutio.com/xmlrpc/
9 * @link http://scripts.incutio.com/xmlrpc/manual.php
10 * @license Artistic License http://www.opensource.org/licenses/artistic-license.php
12 * Modified for DokuWiki
13 * @author Andreas Gohr <andi@splitbrain.org>
20 function IXR_Value ($data, $type = false) {
23 $type = $this->calculateType();
26 if ($type == 'struct') {
27 /* Turn all the values in the array in to new IXR_Value objects */
28 foreach ($this->data
as $key => $value) {
29 $this->data
[$key] = new IXR_Value($value);
32 if ($type == 'array') {
33 for ($i = 0, $j = count($this->data
); $i < $j; $i++
) {
34 $this->data
[$i] = new IXR_Value($this->data
[$i]);
38 function calculateType() {
39 if ($this->data
=== true ||
$this->data
=== false) {
42 if (is_integer($this->data
)) {
45 if (is_double($this->data
)) {
48 // Deal with IXR object types base64 and date
49 if (is_object($this->data
) && is_a($this->data
, 'IXR_Date')) {
52 if (is_object($this->data
) && is_a($this->data
, 'IXR_Base64')) {
55 // If it is a normal PHP object convert it in to a struct
56 if (is_object($this->data
)) {
58 $this->data
= get_object_vars($this->data
);
61 if (!is_array($this->data
)) {
64 /* We have an array - is it an array or a struct ? */
65 if ($this->isStruct($this->data
)) {
72 /* Return XML for this value */
73 switch ($this->type
) {
75 return '<boolean>'.(($this->data
) ?
'1' : '0').'</boolean>';
78 return '<int>'.$this->data
.'</int>';
81 return '<double>'.$this->data
.'</double>';
84 return '<string>'.htmlspecialchars($this->data
).'</string>';
87 $return = '<array><data>'."\n";
88 foreach ($this->data
as $item) {
89 $return .= ' <value>'.$item->getXml()."</value>\n";
91 $return .= '</data></array>';
95 $return = '<struct>'."\n";
96 foreach ($this->data
as $name => $value) {
97 $return .= " <member><name>$name</name><value>";
98 $return .= $value->getXml()."</value></member>\n";
100 $return .= '</struct>';
105 return $this->data
->getXml();
110 function isStruct($array) {
111 /* Nasty function to check if an array is a struct or not */
113 foreach ($array as $key => $value) {
114 if ((string)$key != (string)$expected) {
126 var $messageType; // methodCall / methodResponse / fault
131 // Current variable stacks
132 var $_arraystructs = array(); // The stack used to keep track of the current array/struct
133 var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
134 var $_currentStructName = array(); // A stack as well
138 var $_currentTagContents;
141 function IXR_Message ($message) {
142 $this->message
= $message;
145 // first remove the XML declaration
146 $this->message
= preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message
);
147 // workaround for a bug in PHP/libxml2, see http://bugs.php.net/bug.php?id=45996
148 $this->message
= str_replace('<', '<', $this->message
);
149 $this->message
= str_replace('>', '>', $this->message
);
150 $this->message
= str_replace('&', '&', $this->message
);
151 $this->message
= str_replace(''', ''', $this->message
);
152 $this->message
= str_replace('"', '"', $this->message
);
153 if (trim($this->message
) == '') {
156 $this->_parser
= xml_parser_create();
157 // Set XML parser to take the case of tags in to account
158 xml_parser_set_option($this->_parser
, XML_OPTION_CASE_FOLDING
, false);
159 // Set XML parser callback functions
160 xml_set_object($this->_parser
, $this);
161 xml_set_element_handler($this->_parser
, 'tag_open', 'tag_close');
162 xml_set_character_data_handler($this->_parser
, 'cdata');
163 if (!xml_parse($this->_parser
, $this->message
)) {
164 /* die(sprintf('XML error: %s at line %d',
165 xml_error_string(xml_get_error_code($this->_parser)),
166 xml_get_current_line_number($this->_parser))); */
169 xml_parser_free($this->_parser
);
170 // Grab the error messages, if any
171 if ($this->messageType
== 'fault') {
172 $this->faultCode
= $this->params
[0]['faultCode'];
173 $this->faultString
= $this->params
[0]['faultString'];
177 function tag_open($parser, $tag, $attr) {
178 $this->currentTag
= $tag;
181 case 'methodResponse':
183 $this->messageType
= $tag;
185 /* Deal with stacks of arrays and structs */
186 case 'data': // data is to all intents and puposes more interesting than array
187 $this->_arraystructstypes
[] = 'array';
188 $this->_arraystructs
[] = array();
191 $this->_arraystructstypes
[] = 'struct';
192 $this->_arraystructs
[] = array();
196 function cdata($parser, $cdata) {
197 $this->_currentTagContents
.= $cdata;
199 function tag_close($parser, $tag) {
204 $value = (int)trim($this->_currentTagContents
);
205 $this->_currentTagContents
= '';
209 $value = (double)trim($this->_currentTagContents
);
210 $this->_currentTagContents
= '';
214 $value = (string)trim($this->_currentTagContents
);
215 $this->_currentTagContents
= '';
218 case 'dateTime.iso8601':
219 $value = new IXR_Date(trim($this->_currentTagContents
));
220 // $value = $iso->getTimestamp();
221 $this->_currentTagContents
= '';
225 // "If no type is indicated, the type is string."
226 if (trim($this->_currentTagContents
) != '') {
227 $value = (string)$this->_currentTagContents
;
228 $this->_currentTagContents
= '';
233 $value = (boolean
)trim($this->_currentTagContents
);
234 $this->_currentTagContents
= '';
238 $value = base64_decode($this->_currentTagContents
);
239 $this->_currentTagContents
= '';
242 /* Deal with stacks of arrays and structs */
245 $value = array_pop($this->_arraystructs
);
246 array_pop($this->_arraystructstypes
);
250 array_pop($this->_currentStructName
);
253 $this->_currentStructName
[] = trim($this->_currentTagContents
);
254 $this->_currentTagContents
= '';
257 $this->methodName
= trim($this->_currentTagContents
);
258 $this->_currentTagContents
= '';
263 if (!is_array($value) && !is_object($value)) {
264 $value = trim($value);
267 if (count($this->_arraystructs
) > 0) {
268 // Add value to struct or array
269 if ($this->_arraystructstypes
[count($this->_arraystructstypes
)-1] == 'struct') {
271 $this->_arraystructs
[count($this->_arraystructs
)-1][$this->_currentStructName
[count($this->_currentStructName
)-1]] = $value;
274 $this->_arraystructs
[count($this->_arraystructs
)-1][] = $value;
277 // Just add as a paramater
278 $this->params
[] = $value;
287 var $callbacks = array();
290 function IXR_Server($callbacks = false, $data = false) {
291 $this->setCapabilities();
293 $this->callbacks
= $callbacks;
295 $this->setCallbacks();
298 function serve($data = false) {
300 global $HTTP_RAW_POST_DATA;
301 if (!$HTTP_RAW_POST_DATA) {
302 die('XML-RPC server accepts POST requests only.');
304 $data = $HTTP_RAW_POST_DATA;
306 $this->message
= new IXR_Message($data);
307 if (!$this->message
->parse()) {
308 $this->error(-32700, 'parse error. not well formed');
310 if ($this->message
->messageType
!= 'methodCall') {
311 $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
313 $result = $this->call($this->message
->methodName
, $this->message
->params
);
314 // Is the result an error?
315 if (is_a($result, 'IXR_Error')) {
316 $this->error($result);
319 $r = new IXR_Value($result);
320 $resultxml = $r->getXml();
337 function call($methodname, $args) {
338 if (!$this->hasMethod($methodname)) {
339 return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
341 $method = $this->callbacks
[$methodname];
342 // Perform the callback and send the response
344 # Removed for DokuWiki to have a more consistent interface
345 # if (count($args) == 1) {
346 # // If only one paramater just send that instead of the whole array
351 # Adjusted for DokuWiki to use call_user_func_array
353 // Are we dealing with a function or a method?
354 if (substr($method, 0, 5) == 'this:') {
355 // It's a class method - check it exists
356 $method = substr($method, 5);
357 if (!method_exists($this, $method)) {
358 return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
361 #$result = $this->$method($args);
362 $result = call_user_func_array(array(&$this,$method),$args);
363 } elseif (substr($method, 0, 7) == 'plugin:') {
364 require_once(DOKU_INC
.'inc/pluginutils.php');
365 list($pluginname, $callback) = explode(':', substr($method, 7), 2);
366 if(!plugin_isdisabled($pluginname)) {
367 $plugin = plugin_load('action', $pluginname);
368 return call_user_func_array(array($plugin, $callback), $args);
370 return new IXR_Error(-99999, 'server error');
373 // It's a function - does it exist?
374 if (!function_exists($method)) {
375 return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
378 #$result = $method($args);
379 $result = call_user_func_array($method,$args);
384 function error($error, $message = false) {
385 // Accepts either an error object or an error code and message
386 if ($message && !is_object($error)) {
387 $error = new IXR_Error($error, $message);
389 $this->output($error->getXml());
391 function output($xml) {
392 $xml = '<?xml version="1.0"?>'."\n".$xml;
393 $length = strlen($xml);
394 header('Connection: close');
395 header('Content-Length: '.$length);
396 header('Content-Type: text/xml');
397 header('Date: '.date('r'));
401 function hasMethod($method) {
402 return in_array($method, array_keys($this->callbacks
));
404 function setCapabilities() {
405 // Initialises capabilities array
406 $this->capabilities
= array(
408 'specUrl' => 'http://www.xmlrpc.com/spec',
411 'faults_interop' => array(
412 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
413 'specVersion' => 20010516
415 'system.multicall' => array(
416 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
421 function getCapabilities() {
422 return $this->capabilities
;
424 function setCallbacks() {
425 $this->callbacks
['system.getCapabilities'] = 'this:getCapabilities';
426 $this->callbacks
['system.listMethods'] = 'this:listMethods';
427 $this->callbacks
['system.multicall'] = 'this:multiCall';
429 function listMethods() {
430 // Returns a list of methods - uses array_reverse to ensure user defined
431 // methods are listed before server defined methods
432 return array_reverse(array_keys($this->callbacks
));
434 function multiCall($methodcalls) {
435 // See http://www.xmlrpc.com/discuss/msgReader$1208
437 foreach ($methodcalls as $call) {
438 $method = $call['methodName'];
439 $params = $call['params'];
440 if ($method == 'system.multicall') {
441 $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
443 $result = $this->call($method, $params);
445 if (is_a($result, 'IXR_Error')) {
447 'faultCode' => $result->code
,
448 'faultString' => $result->message
451 $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
);
489 * Changed for DokuWiki to use DokuHTTPClient
491 * This should be compatible to the original class, but uses DokuWiki's
492 * HTTP client library which will respect proxy settings
494 * Because the XMLRPC client is not used in DokuWiki currently this is completely
497 class IXR_Client
extends DokuHTTPClient
{
499 var $message = false;
500 var $xmlerror = false;
502 function IXR_Client($server, $path = false, $port = 80) {
503 $this->DokuHTTPClient();
505 // Assume we have been given a URL instead
506 $this->posturl
= $server;
508 $this->posturl
= 'http://'.$server.':'.$port.$path;
513 $args = func_get_args();
514 $method = array_shift($args);
515 $request = new IXR_Request($method, $args);
516 $xml = $request->getXml();
518 $this->headers
['Content-Type'] = 'text/xml';
519 if(!$this->sendRequest($this->posturl
,$xml,'POST')){
520 $this->xmlerror
= new IXR_Error(-32300, 'transport error - '.$this->error
);
524 // Check HTTP Response code
525 if($this->status
< 200 ||
$this->status
> 206){
526 $this->xmlerror
= new IXR_Error(-32300, 'transport error - HTTP status '.$this->status
);
530 // Now parse what we've got back
531 $this->message
= new IXR_Message($this->resp_body
);
532 if (!$this->message
->parse()) {
534 $this->xmlerror
= new IXR_Error(-32700, 'parse error. not well formed');
537 // Is the message a fault?
538 if ($this->message
->messageType
== 'fault') {
539 $this->xmlerror
= new IXR_Error($this->message
->faultCode
, $this->message
->faultString
);
542 // Message must be OK
545 function getResponse() {
546 // methodResponses can only have one param - return that
547 return $this->message
->params
[0];
550 return (is_object($this->xmlerror
));
552 function getErrorCode() {
553 return $this->xmlerror
->code
;
555 function getErrorMessage() {
556 return $this->xmlerror
->message
;
564 function IXR_Error($code, $message) {
566 $this->message
= $message;
575 <name>faultCode</name>
576 <value><int>{$this->code}</int></value>
579 <name>faultString</name>
580 <value><string>{$this->message}</string></value>
600 function IXR_Date($time) {
601 // $time can be a PHP timestamp or an ISO one
602 if (is_numeric($time)) {
603 $this->parseTimestamp($time);
605 $this->parseIso($time);
608 function parseTimestamp($timestamp) {
609 $this->year
= gmdate('Y', $timestamp);
610 $this->month
= gmdate('m', $timestamp);
611 $this->day
= gmdate('d', $timestamp);
612 $this->hour
= gmdate('H', $timestamp);
613 $this->minute
= gmdate('i', $timestamp);
614 $this->second
= gmdate('s', $timestamp);
616 function parseIso($iso) {
617 $this->year
= substr($iso, 0, 4);
618 $this->month
= substr($iso, 5, 2);
619 $this->day
= substr($iso, 8, 2);
620 $this->hour
= substr($iso, 11, 2);
621 $this->minute
= substr($iso, 14, 2);
622 $this->second
= substr($iso, 17, 2);
625 return $this->year
.'-'.$this->month
.'-'.$this->day
.'T'.$this->hour
.':'.$this->minute
.':'.$this->second
;
628 return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
630 function getTimestamp() {
631 return gmmktime($this->hour
, $this->minute
, $this->second
, $this->month
, $this->day
, $this->year
);
638 function IXR_Base64($data) {
642 return '<base64>'.base64_encode($this->data
).'</base64>';
647 class IXR_IntrospectionServer
extends IXR_Server
{
650 function IXR_IntrospectionServer() {
651 $this->setCallbacks();
652 $this->setCapabilities();
653 $this->capabilities
['introspection'] = array(
654 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html',
658 'system.methodSignature',
659 'this:methodSignature',
660 array('array', 'string'),
661 'Returns an array describing the return type and required parameters of a method'
664 'system.getCapabilities',
665 'this:getCapabilities',
667 'Returns a struct describing the XML-RPC specifications supported by this server'
670 'system.listMethods',
673 'Returns an array of available methods on this server'
678 array('string', 'string'),
679 'Returns a documentation string for the specified method'
682 function addCallback($method, $callback, $args, $help) {
683 $this->callbacks
[$method] = $callback;
684 $this->signatures
[$method] = $args;
685 $this->help
[$method] = $help;
687 function call($methodname, $args) {
688 // Make sure it's in an array
689 if ($args && !is_array($args)) {
690 $args = array($args);
692 // Over-rides default call method, adds signature check
693 if (!$this->hasMethod($methodname)) {
694 return new IXR_Error(-32601, 'server error. requested method "'.$this->message
->methodName
.'" not specified.');
696 $method = $this->callbacks
[$methodname];
697 $signature = $this->signatures
[$methodname];
698 $returnType = array_shift($signature);
699 // Check the number of arguments. Check only, if the minimum count of parameters is specified. More parameters are possible.
700 // This is a hack to allow optional parameters...
701 if (count($args) < count($signature)) {
702 // print 'Num of args: '.count($args).' Num in signature: '.count($signature);
703 return new IXR_Error(-32602, 'server error. wrong number of method parameters');
705 // Check the argument types
708 for ($i = 0, $j = count($args); $i < $j; $i++
) {
709 $arg = array_shift($args);
710 $type = array_shift($signature);
714 if (is_array($arg) ||
!is_int($arg)) {
720 if (!is_string($arg)) {
725 if ($arg !== false && $arg !== true) {
731 if (!is_float($arg)) {
736 case 'dateTime.iso8601':
737 if (!is_a($arg, 'IXR_Date')) {
743 return new IXR_Error(-32602, 'server error. invalid method parameters');
746 // It passed the test - run the "real" method call
747 return parent
::call($methodname, $argsbackup);
749 function methodSignature($method) {
750 if (!$this->hasMethod($method)) {
751 return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
753 // We should be returning an array of types
754 $types = $this->signatures
[$method];
756 foreach ($types as $type) {
759 $return[] = 'string';
768 case 'dateTime.iso8601':
769 $return[] = new IXR_Date(time());
775 $return[] = new IXR_Base64('base64');
778 $return[] = array('array');
781 $return[] = array('struct' => 'struct');
787 function methodHelp($method) {
788 return $this->help
[$method];
793 class IXR_ClientMulticall
extends IXR_Client
{
794 var $calls = array();
795 function IXR_ClientMulticall($server, $path = false, $port = 80) {
796 parent
::IXR_Client($server, $path, $port);
797 //$this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
800 $args = func_get_args();
801 $methodName = array_shift($args);
803 'methodName' => $methodName,
806 $this->calls
[] = $struct;
809 // Prepare multicall, then call the parent::query() method
810 return parent
::query('system.multicall', $this->calls
);