3
IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002
4
Version 1.61 - Simon Willison, 11th July 2003 (htmlentities -> htmlspecialchars)
5
Site: http://scripts.incutio.com/xmlrpc/
6
Manual: http://scripts.incutio.com/xmlrpc/manual.php
7
Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php
13
function IXR_Value ($data, $type = false) {
16
$type = $this->calculateType();
19
if ($type == 'struct') {
20
/* Turn all the values in the array in to new IXR_Value objects */
21
foreach ($this->data as $key => $value) {
22
$this->data[$key] = new IXR_Value($value);
25
if ($type == 'array') {
26
for ($i = 0, $j = count($this->data); $i < $j; $i++) {
27
$this->data[$i] = new IXR_Value($this->data[$i]);
31
function calculateType() {
32
if ($this->data === true || $this->data === false) {
35
if (is_integer($this->data)) {
38
if (is_double($this->data)) {
41
// Deal with IXR object types base64 and date
42
if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
45
if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
48
// If it is a normal PHP object convert it in to a struct
49
if (is_object($this->data)) {
51
$this->data = get_object_vars($this->data);
54
if (!is_array($this->data)) {
57
/* We have an array - is it an array or a struct ? */
58
if ($this->isStruct($this->data)) {
65
/* Return XML for this value */
66
switch ($this->type) {
68
return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>';
71
return '<int>'.$this->data.'</int>';
74
return '<double>'.$this->data.'</double>';
77
return '<string>'.htmlspecialchars($this->data).'</string>';
80
$return = '<array><data>'."\n";
81
foreach ($this->data as $item) {
82
$return .= ' <value>'.$item->getXml()."</value>\n";
84
$return .= '</data></array>';
88
$return = '<struct>'."\n";
89
foreach ($this->data as $name => $value) {
90
$return .= " <member><name>$name</name><value>";
91
$return .= $value->getXml()."</value></member>\n";
93
$return .= '</struct>';
98
return $this->data->getXml();
103
function isStruct($array) {
104
/* Nasty function to check if an array is a struct or not */
106
foreach ($array as $key => $value) {
107
if ((string)$key != (string)$expected) {
119
var $messageType; // methodCall / methodResponse / fault
124
// Current variable stacks
125
var $_arraystructs = array(); // The stack used to keep track of the current array/struct
126
var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
127
var $_currentStructName = array(); // A stack as well
131
var $_currentTagContents;
134
function IXR_Message ($message) {
135
$this->message = $message;
138
// first remove the XML declaration
139
$this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message);
140
if (trim($this->message) == '') {
143
$this->_parser = xml_parser_create();
144
// Set XML parser to take the case of tags in to account
145
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
146
// Set XML parser callback functions
147
xml_set_object($this->_parser, $this);
148
xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
149
xml_set_character_data_handler($this->_parser, 'cdata');
150
if (!xml_parse($this->_parser, $this->message)) {
151
/* die(sprintf('XML error: %s at line %d',
152
xml_error_string(xml_get_error_code($this->_parser)),
153
xml_get_current_line_number($this->_parser))); */
156
xml_parser_free($this->_parser);
157
// Grab the error messages, if any
158
if ($this->messageType == 'fault') {
159
$this->faultCode = $this->params[0]['faultCode'];
160
$this->faultString = $this->params[0]['faultString'];
164
function tag_open($parser, $tag, $attr) {
165
$this->currentTag = $tag;
168
case 'methodResponse':
170
$this->messageType = $tag;
172
/* Deal with stacks of arrays and structs */
173
case 'data': // data is to all intents and puposes more interesting than array
174
$this->_arraystructstypes[] = 'array';
175
$this->_arraystructs[] = array();
178
$this->_arraystructstypes[] = 'struct';
179
$this->_arraystructs[] = array();
183
function cdata($parser, $cdata) {
184
$this->_currentTagContents .= $cdata;
186
function tag_close($parser, $tag) {
191
$value = (int)trim($this->_currentTagContents);
192
$this->_currentTagContents = '';
196
$value = (double)trim($this->_currentTagContents);
197
$this->_currentTagContents = '';
201
$value = (string)trim($this->_currentTagContents);
202
$this->_currentTagContents = '';
205
case 'dateTime.iso8601':
206
$value = new IXR_Date(trim($this->_currentTagContents));
207
// $value = $iso->getTimestamp();
208
$this->_currentTagContents = '';
212
// "If no type is indicated, the type is string."
213
if (trim($this->_currentTagContents) != '') {
214
$value = (string)$this->_currentTagContents;
215
$this->_currentTagContents = '';
220
$value = (boolean)trim($this->_currentTagContents);
221
$this->_currentTagContents = '';
225
$value = base64_decode($this->_currentTagContents);
226
$this->_currentTagContents = '';
229
/* Deal with stacks of arrays and structs */
232
$value = array_pop($this->_arraystructs);
233
array_pop($this->_arraystructstypes);
237
array_pop($this->_currentStructName);
240
$this->_currentStructName[] = trim($this->_currentTagContents);
241
$this->_currentTagContents = '';
244
$this->methodName = trim($this->_currentTagContents);
245
$this->_currentTagContents = '';
250
if (!is_array($value) && !is_object($value)) {
251
$value = trim($value);
254
if (count($this->_arraystructs) > 0) {
255
// Add value to struct or array
256
if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
259
$this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]]
263
$this->_arraystructs[count($this->_arraystructs)-1][] = $value;
266
// Just add as a paramater
267
$this->params[] = $value;
276
var $callbacks = array();
279
function IXR_Server($callbacks = false, $data = false) {
280
$this->setCapabilities();
282
$this->callbacks = $callbacks;
284
$this->setCallbacks();
287
function serve($data = false) {
289
global $HTTP_RAW_POST_DATA;
290
if (!$HTTP_RAW_POST_DATA) {
291
die('XML-RPC server accepts POST requests only.');
293
$data = $HTTP_RAW_POST_DATA;
295
$this->message = new IXR_Message($data);
296
if (!$this->message->parse()) {
297
$this->error(-32700, 'parse error. not well formed');
299
if ($this->message->messageType != 'methodCall') {
300
$this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a
303
$result = $this->call($this->message->methodName, $this->message->params);
304
// Is the result an error?
305
if (is_a($result, 'IXR_Error')) {
306
$this->error($result);
309
$r = new IXR_Value($result);
310
$resultxml = $r->getXml();
327
function call($methodname, $args) {
328
if (!$this->hasMethod($methodname)) {
329
return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
331
$method = $this->callbacks[$methodname];
332
// Perform the callback and send the response
333
if (count($args) == 1) {
334
// If only one paramater just send that instead of the whole array
337
// Are we dealing with a function or a method?
338
if (substr($method, 0, 5) == 'this:') {
339
// It's a class method - check it exists
340
$method = substr($method, 5);
341
if (!method_exists($this, $method)) {
342
return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not
346
$result = $this->$method($args);
348
// It's a function - does it exist?
349
if (!function_exists($method)) {
350
return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
353
$result = $method($args);
358
function error($error, $message = false) {
359
// Accepts either an error object or an error code and message
360
if ($message && !is_object($error)) {
361
$error = new IXR_Error($error, $message);
363
$this->output($error->getXml());
365
function output($xml) {
366
$xml = '<?xml version="1.0"?>'."\n".$xml;
367
$length = strlen($xml);
368
header('Connection: close');
369
header('Content-Length: '.$length);
370
header('Content-Type: text/xml');
371
header('Date: '.date('r'));
375
function hasMethod($method) {
376
return in_array($method, array_keys($this->callbacks));
378
function setCapabilities() {
379
// Initialises capabilities array
380
$this->capabilities = array(
382
'specUrl' => 'http://www.xmlrpc.com/spec',
385
'faults_interop' => array(
386
'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
387
'specVersion' => 20010516
389
'system.multicall' => array(
390
'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
395
function getCapabilities($args) {
396
return $this->capabilities;
398
function setCallbacks() {
399
$this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
400
$this->callbacks['system.listMethods'] = 'this:listMethods';
401
$this->callbacks['system.multicall'] = 'this:multiCall';
403
function listMethods($args) {
404
// Returns a list of methods - uses array_reverse to ensure user defined
405
// methods are listed before server defined methods
406
return array_reverse(array_keys($this->callbacks));
408
function multiCall($methodcalls) {
409
// See http://www.xmlrpc.com/discuss/msgReader$1208
411
foreach ($methodcalls as $call) {
412
$method = $call['methodName'];
413
$params = $call['params'];
414
if ($method == 'system.multicall') {
415
$result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
417
$result = $this->call($method, $params);
419
if (is_a($result, 'IXR_Error')) {
421
'faultCode' => $result->code,
422
'faultString' => $result->message
425
$return[] = array($result);
436
function IXR_Request($method, $args) {
437
$this->method = $method;
440
<?xml version="1.0"?>
442
<methodName>{$this->method}</methodName>
446
foreach ($this->args as $arg) {
447
$this->xml .= '<param><value>';
448
$v = new IXR_Value($arg);
449
$this->xml .= $v->getXml();
450
$this->xml .= "</value></param>\n";
452
$this->xml .= '</params></methodCall>';
454
function getLength() {
455
return strlen($this->xml);
469
var $message = false;
471
// Storage place for an error message
473
function IXR_Client($server, $path = false, $port = 80) {
475
// Assume we have been given a URL instead
476
$bits = parse_url($server);
477
$this->server = $bits['host'];
478
$this->port = isset($bits['port']) ? $bits['port'] : 80;
479
$this->path = isset($bits['path']) ? $bits['path'] : '/';
480
// Make absolutely sure we have a path
485
$this->server = $server;
489
$this->useragent = 'The Incutio XML-RPC PHP Library';
492
$args = func_get_args();
493
$method = array_shift($args);
494
$request = new IXR_Request($method, $args);
495
$length = $request->getLength();
496
$xml = $request->getXml();
498
$request = "POST {$this->path} HTTP/1.0$r";
499
$request .= "Host: {$this->server}$r";
500
$request .= "Content-Type: text/xml$r";
501
$request .= "User-Agent: {$this->useragent}$r";
502
$request .= "Content-length: {$length}$r$r";
504
// Now send the request
506
echo '<pre>'.htmlspecialchars($request)."\n</pre>\n\n";
508
$fp = @fsockopen($this->server, $this->port);
510
$this->error = new IXR_Error(-32300, 'transport error - could not open socket');
513
fputs($fp, $request);
515
$gotFirstLine = false;
516
$gettingHeaders = true;
518
$line = fgets($fp, 4096);
519
if (!$gotFirstLine) {
520
// Check line for '200'
521
if (strstr($line, '200') === false) {
522
$this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
525
$gotFirstLine = true;
527
if (trim($line) == '') {
528
$gettingHeaders = false;
530
if (!$gettingHeaders) {
531
$contents .= trim($line)."\n";
535
echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n";
537
// Now parse what we've got back
538
$this->message = new IXR_Message($contents);
539
if (!$this->message->parse()) {
541
$this->error = new IXR_Error(-32700, 'parse error. not well formed');
544
// Is the message a fault?
545
if ($this->message->messageType == 'fault') {
546
$this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
549
// Message must be OK
552
function getResponse() {
553
// methodResponses can only have one param - return that
554
return $this->message->params[0];
557
return (is_object($this->error));
559
function getErrorCode() {
560
return $this->error->code;
562
function getErrorMessage() {
563
return $this->error->message;
571
function IXR_Error($code, $message) {
573
$this->message = $message;
582
<name>faultCode</name>
583
<value><int>{$this->code}</int></value>
586
<name>faultString</name>
587
<value><string>{$this->message}</string></value>
607
function IXR_Date($time) {
608
// $time can be a PHP timestamp or an ISO one
609
if (is_numeric($time)) {
610
$this->parseTimestamp($time);
612
$this->parseIso($time);
615
function parseTimestamp($timestamp) {
616
$this->year = date('Y', $timestamp);
617
$this->month = date('Y', $timestamp);
618
$this->day = date('Y', $timestamp);
619
$this->hour = date('H', $timestamp);
620
$this->minute = date('i', $timestamp);
621
$this->second = date('s', $timestamp);
623
function parseIso($iso) {
624
$this->year = substr($iso, 0, 4);
625
$this->month = substr($iso, 4, 2);
626
$this->day = substr($iso, 6, 2);
627
$this->hour = substr($iso, 9, 2);
628
$this->minute = substr($iso, 12, 2);
629
$this->second = substr($iso, 15, 2);
632
return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second;
635
return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>';
637
function getTimestamp() {
638
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
645
function IXR_Base64($data) {
649
return '<base64>'.base64_encode($this->data).'</base64>';
654
class IXR_IntrospectionServer extends IXR_Server {
657
function IXR_IntrospectionServer() {
658
$this->setCallbacks();
659
$this->setCapabilities();
660
$this->capabilities['introspection'] = array(
661
'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html',
665
'system.methodSignature',
666
'this:methodSignature',
667
array('array', 'string'),
668
'Returns an array describing the return type and required parameters of a method'
671
'system.getCapabilities',
672
'this:getCapabilities',
674
'Returns a struct describing the XML-RPC specifications supported by this server'
677
'system.listMethods',
680
'Returns an array of available methods on this server'
685
array('string', 'string'),
686
'Returns a documentation string for the specified method'
689
function addCallback($method, $callback, $args, $help) {
690
$this->callbacks[$method] = $callback;
691
$this->signatures[$method] = $args;
692
$this->help[$method] = $help;
694
function call($methodname, $args) {
695
// Make sure it's in an array
696
if ($args && !is_array($args)) {
697
$args = array($args);
699
// Over-rides default call method, adds signature check
700
if (!$this->hasMethod($methodname)) {
701
return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not
704
$method = $this->callbacks[$methodname];
705
$signature = $this->signatures[$methodname];
706
$returnType = array_shift($signature);
707
// Check the number of arguments
708
if (count($args) != count($signature)) {
709
// print 'Num of args: '.count($args).' Num in signature: '.count($signature);
710
return new IXR_Error(-32602, 'server error. wrong number of method parameters');
712
// Check the argument types
715
for ($i = 0, $j = count($args); $i < $j; $i++) {
716
$arg = array_shift($args);
717
$type = array_shift($signature);
721
if (is_array($arg) || !is_int($arg)) {
727
if (!is_string($arg)) {
732
if ($arg !== false && $arg !== true) {
738
if (!is_float($arg)) {
743
case 'dateTime.iso8601':
744
if (!is_a($arg, 'IXR_Date')) {
750
return new IXR_Error(-32602, 'server error. invalid method parameters');
753
// It passed the test - run the "real" method call
754
return parent::call($methodname, $argsbackup);
756
function methodSignature($method) {
757
if (!$this->hasMethod($method)) {
758
return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
760
// We should be returning an array of types
761
$types = $this->signatures[$method];
763
foreach ($types as $type) {
766
$return[] = 'string';
775
case 'dateTime.iso8601':
776
$return[] = new IXR_Date(time());
782
$return[] = new IXR_Base64('base64');
785
$return[] = array('array');
788
$return[] = array('struct' => 'struct');
794
function methodHelp($method) {
795
return $this->help[$method];
800
class IXR_ClientMulticall extends IXR_Client {
801
var $calls = array();
802
function IXR_ClientMulticall($server, $path = false, $port = 80) {
803
parent::IXR_Client($server, $path, $port);
804
$this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
807
$args = func_get_args();
808
$methodName = array_shift($args);
810
'methodName' => $methodName,
813
$this->calls[] = $struct;
816
// Prepare multicall, then call the parent::query() method
817
return parent::query('system.multicall', $this->calls);
b'\\ No newline at end of file'