diff options
author | gareth | 2007-02-27 23:31:14 +0000 |
---|---|---|
committer | gareth | 2007-02-27 23:31:14 +0000 |
commit | ed4dc30dc2b00628a3d6c298b6b2d6ec7f74ab56 (patch) | |
tree | c96e74cc05e4c6d6e97716c0c509fb0ec1693fc6 /ogs/common/xmlrpc.inc.php | |
parent | Brought in code from branches/gareth (diff) | |
download | opensim-SC_OLD-ed4dc30dc2b00628a3d6c298b6b2d6ec7f74ab56.zip opensim-SC_OLD-ed4dc30dc2b00628a3d6c298b6b2d6ec7f74ab56.tar.gz opensim-SC_OLD-ed4dc30dc2b00628a3d6c298b6b2d6ec7f74ab56.tar.bz2 opensim-SC_OLD-ed4dc30dc2b00628a3d6c298b6b2d6ec7f74ab56.tar.xz |
Brought in other OGS server components and the all-important caffeine script
Diffstat (limited to 'ogs/common/xmlrpc.inc.php')
-rw-r--r-- | ogs/common/xmlrpc.inc.php | 828 |
1 files changed, 828 insertions, 0 deletions
diff --git a/ogs/common/xmlrpc.inc.php b/ogs/common/xmlrpc.inc.php new file mode 100644 index 0000000..03c1ee7 --- /dev/null +++ b/ogs/common/xmlrpc.inc.php | |||
@@ -0,0 +1,828 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 | ||
5 | Version 1.61 - Simon Willison, 11th July 2003 (htmlentities -> htmlspecialchars) | ||
6 | Site: http://scripts.incutio.com/xmlrpc/ | ||
7 | Manual: http://scripts.incutio.com/xmlrpc/manual.php | ||
8 | Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php | ||
9 | */ | ||
10 | |||
11 | |||
12 | class IXR_Value { | ||
13 | var $data; | ||
14 | var $type; | ||
15 | function IXR_Value ($data, $type = false) { | ||
16 | $this->data = $data; | ||
17 | if (!$type) { | ||
18 | $type = $this->calculateType(); | ||
19 | } | ||
20 | $this->type = $type; | ||
21 | if ($type == 'struct') { | ||
22 | /* Turn all the values in the array in to new IXR_Value objects */ | ||
23 | foreach ($this->data as $key => $value) { | ||
24 | $this->data[$key] = new IXR_Value($value); | ||
25 | } | ||
26 | } | ||
27 | if ($type == 'array') { | ||
28 | for ($i = 0, $j = count($this->data); $i < $j; $i++) { | ||
29 | $this->data[$i] = new IXR_Value($this->data[$i]); | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | function calculateType() { | ||
34 | if ($this->data === true || $this->data === false) { | ||
35 | return 'boolean'; | ||
36 | } | ||
37 | if (is_integer($this->data)) { | ||
38 | return 'i4'; | ||
39 | } | ||
40 | if (is_double($this->data)) { | ||
41 | return 'double'; | ||
42 | } | ||
43 | // Deal with IXR object types base64 and date | ||
44 | if (is_object($this->data) && is_a($this->data, 'IXR_Date')) { | ||
45 | return 'date'; | ||
46 | } | ||
47 | if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) { | ||
48 | return 'base64'; | ||
49 | } | ||
50 | if (is_object($this->data)) { | ||
51 | return 'object'; | ||
52 | } | ||
53 | if (!is_array($this->data)) { | ||
54 | return 'string'; | ||
55 | } | ||
56 | /* We have an array - is it an array or a struct ? */ | ||
57 | if ($this->isStruct($this->data)) { | ||
58 | return 'struct'; | ||
59 | } else { | ||
60 | return 'array'; | ||
61 | } | ||
62 | } | ||
63 | function getXml() { | ||
64 | /* Return XML for this value */ | ||
65 | switch ($this->type) { | ||
66 | case 'boolean': | ||
67 | return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>'; | ||
68 | break; | ||
69 | case 'i4': | ||
70 | return '<i4>'.$this->data.'</i4>'; | ||
71 | break; | ||
72 | case 'double': | ||
73 | return '<double>'.$this->data.'</double>'; | ||
74 | break; | ||
75 | case 'string': | ||
76 | return '<string>'.htmlspecialchars($this->data).'</string>'; | ||
77 | break; | ||
78 | case 'array': | ||
79 | $return = '<array><data>'.""; | ||
80 | foreach ($this->data as $item) { | ||
81 | $return .= ' <value>'.$item->getXml()."</value>"; | ||
82 | } | ||
83 | $return .= '</data></array>'; | ||
84 | return $return; | ||
85 | break; | ||
86 | case 'struct': | ||
87 | $return = '<struct>'.""; | ||
88 | foreach ($this->data as $name => $value) { | ||
89 | $return .= "<member><name>$name</name><value>"; | ||
90 | $return .= $value->getXml()."</value></member>"; | ||
91 | } | ||
92 | $return .= '</struct>'; | ||
93 | return $return; | ||
94 | break; | ||
95 | case 'date': | ||
96 | case 'base64': | ||
97 | return $this->data->getXml(); | ||
98 | break; | ||
99 | case 'object': | ||
100 | return $this->data->getXml(); | ||
101 | break; | ||
102 | } | ||
103 | return false; | ||
104 | } | ||
105 | function isStruct($array) { | ||
106 | /* Nasty function to check if an array is a struct or not */ | ||
107 | $expected = 0; | ||
108 | foreach ($array as $key => $value) { | ||
109 | if ((string)$key != (string)$expected) { | ||
110 | return true; | ||
111 | } | ||
112 | $expected++; | ||
113 | } | ||
114 | return false; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | |||
119 | class IXR_Message { | ||
120 | var $message; | ||
121 | var $messageType; // methodCall / methodResponse / fault | ||
122 | var $faultCode; | ||
123 | var $faultString; | ||
124 | var $methodName; | ||
125 | var $params; | ||
126 | // Current variable stacks | ||
127 | var $_arraystructs = array(); // The stack used to keep track of the current array/struct | ||
128 | var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array | ||
129 | var $_currentStructName = array(); // A stack as well | ||
130 | var $_param; | ||
131 | var $_value; | ||
132 | var $_currentTag; | ||
133 | var $_currentTagContents; | ||
134 | // The XML parser | ||
135 | var $_parser; | ||
136 | function IXR_Message ($message) { | ||
137 | $this->message = $message; | ||
138 | } | ||
139 | function parse() { | ||
140 | // first remove the XML declaration | ||
141 | $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message); | ||
142 | if (trim($this->message) == '') { | ||
143 | return false; | ||
144 | } | ||
145 | $this->_parser = xml_parser_create(); | ||
146 | // Set XML parser to take the case of tags in to account | ||
147 | xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); | ||
148 | // Set XML parser callback functions | ||
149 | xml_set_object($this->_parser, $this); | ||
150 | xml_set_element_handler($this->_parser, 'tag_open', 'tag_close'); | ||
151 | xml_set_character_data_handler($this->_parser, 'cdata'); | ||
152 | if (!xml_parse($this->_parser, $this->message)) { | ||
153 | /* die(sprintf('XML error: %s at line %d', | ||
154 | xml_error_string(xml_get_error_code($this->_parser)), | ||
155 | xml_get_current_line_number($this->_parser))); */ | ||
156 | return false; | ||
157 | } | ||
158 | xml_parser_free($this->_parser); | ||
159 | // Grab the error messages, if any | ||
160 | if ($this->messageType == 'fault') { | ||
161 | $this->faultCode = $this->params[0]['faultCode']; | ||
162 | $this->faultString = $this->params[0]['faultString']; | ||
163 | } | ||
164 | return true; | ||
165 | } | ||
166 | function tag_open($parser, $tag, $attr) { | ||
167 | $this->currentTag = $tag; | ||
168 | switch($tag) { | ||
169 | case 'methodCall': | ||
170 | case 'methodResponse': | ||
171 | case 'fault': | ||
172 | $this->messageType = $tag; | ||
173 | break; | ||
174 | /* Deal with stacks of arrays and structs */ | ||
175 | case 'data': // data is to all intents and puposes more interesting than array | ||
176 | $this->_arraystructstypes[] = 'array'; | ||
177 | $this->_arraystructs[] = array(); | ||
178 | break; | ||
179 | case 'struct': | ||
180 | $this->_arraystructstypes[] = 'struct'; | ||
181 | $this->_arraystructs[] = array(); | ||
182 | break; | ||
183 | } | ||
184 | } | ||
185 | function cdata($parser, $cdata) { | ||
186 | $this->_currentTagContents .= $cdata; | ||
187 | } | ||
188 | function tag_close($parser, $tag) { | ||
189 | $valueFlag = false; | ||
190 | switch($tag) { | ||
191 | case 'int': | ||
192 | case 'i4': | ||
193 | $value = (int)trim($this->_currentTagContents); | ||
194 | $this->_currentTagContents = ''; | ||
195 | $valueFlag = true; | ||
196 | break; | ||
197 | case 'double': | ||
198 | $value = (double)trim($this->_currentTagContents); | ||
199 | $this->_currentTagContents = ''; | ||
200 | $valueFlag = true; | ||
201 | break; | ||
202 | case 'string': | ||
203 | $value = (string)trim($this->_currentTagContents); | ||
204 | $this->_currentTagContents = ''; | ||
205 | $valueFlag = true; | ||
206 | break; | ||
207 | case 'dateTime.iso8601': | ||
208 | $value = new IXR_Date(trim($this->_currentTagContents)); | ||
209 | // $value = $iso->getTimestamp(); | ||
210 | $this->_currentTagContents = ''; | ||
211 | $valueFlag = true; | ||
212 | break; | ||
213 | case 'value': | ||
214 | // "If no type is indicated, the type is string." | ||
215 | if (trim($this->_currentTagContents) != '') { | ||
216 | $value = (string)$this->_currentTagContents; | ||
217 | $this->_currentTagContents = ''; | ||
218 | $valueFlag = true; | ||
219 | } | ||
220 | break; | ||
221 | case 'boolean': | ||
222 | $value = (boolean)trim($this->_currentTagContents); | ||
223 | $this->_currentTagContents = ''; | ||
224 | $valueFlag = true; | ||
225 | break; | ||
226 | case 'base64': | ||
227 | $value = base64_decode($this->_currentTagContents); | ||
228 | $this->_currentTagContents = ''; | ||
229 | $valueFlag = true; | ||
230 | break; | ||
231 | /* Deal with stacks of arrays and structs */ | ||
232 | case 'data': | ||
233 | case 'struct': | ||
234 | $value = array_pop($this->_arraystructs); | ||
235 | array_pop($this->_arraystructstypes); | ||
236 | $valueFlag = true; | ||
237 | break; | ||
238 | case 'member': | ||
239 | array_pop($this->_currentStructName); | ||
240 | break; | ||
241 | case 'name': | ||
242 | $this->_currentStructName[] = trim($this->_currentTagContents); | ||
243 | $this->_currentTagContents = ''; | ||
244 | break; | ||
245 | case 'methodName': | ||
246 | $this->methodName = trim($this->_currentTagContents); | ||
247 | $this->_currentTagContents = ''; | ||
248 | break; | ||
249 | } | ||
250 | if ($valueFlag) { | ||
251 | /* | ||
252 | if (!is_array($value) && !is_object($value)) { | ||
253 | $value = trim($value); | ||
254 | } | ||
255 | */ | ||
256 | if (count($this->_arraystructs) > 0) { | ||
257 | // Add value to struct or array | ||
258 | if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') { | ||
259 | // Add to struct | ||
260 | $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value; | ||
261 | } else { | ||
262 | // Add to array | ||
263 | $this->_arraystructs[count($this->_arraystructs)-1][] = $value; | ||
264 | } | ||
265 | } else { | ||
266 | // Just add as a paramater | ||
267 | $this->params[] = $value; | ||
268 | } | ||
269 | } | ||
270 | } | ||
271 | } | ||
272 | |||
273 | |||
274 | class IXR_Server { | ||
275 | var $data; | ||
276 | var $callbacks = array(); | ||
277 | var $message; | ||
278 | var $capabilities; | ||
279 | function IXR_Server($callbacks = false, $data = false) { | ||
280 | $this->setCapabilities(); | ||
281 | if ($callbacks) { | ||
282 | $this->callbacks = $callbacks; | ||
283 | } | ||
284 | $this->setCallbacks(); | ||
285 | $this->serve($data); | ||
286 | } | ||
287 | function serve($data = false) { | ||
288 | if (!$data) { | ||
289 | global $HTTP_RAW_POST_DATA; | ||
290 | if (!$HTTP_RAW_POST_DATA) { | ||
291 | die('XML-RPC server accepts POST requests only.'); | ||
292 | } | ||
293 | $data = $HTTP_RAW_POST_DATA; | ||
294 | } | ||
295 | $this->message = new IXR_Message($data); | ||
296 | if (!$this->message->parse()) { | ||
297 | $this->error(-32700, 'parse error. not well formed'); | ||
298 | } | ||
299 | if ($this->message->messageType != 'methodCall') { | ||
300 | $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall'); | ||
301 | } | ||
302 | $result = $this->call($this->message->methodName, $this->message->params); | ||
303 | // Is the result an error? | ||
304 | if (is_a($result, 'IXR_Error')) { | ||
305 | $this->error($result); | ||
306 | } | ||
307 | // Encode the result | ||
308 | $r = new IXR_Value($result); | ||
309 | $resultxml = $r->getXml(); | ||
310 | // Create the XML | ||
311 | $xml = <<<EOD | ||
312 | <methodResponse><params><param><value>$resultxml</value></param></params></methodResponse> | ||
313 | EOD; | ||
314 | // Send it | ||
315 | $this->output($xml); | ||
316 | } | ||
317 | function call($methodname, $args) { | ||
318 | if (!$this->hasMethod($methodname)) { | ||
319 | return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.'); | ||
320 | } | ||
321 | $method = $this->callbacks[$methodname]; | ||
322 | // Perform the callback and send the response | ||
323 | if (count($args) == 1) { | ||
324 | // If only one paramater just send that instead of the whole array | ||
325 | $args = $args[0]; | ||
326 | } | ||
327 | // Are we dealing with a function or a method? | ||
328 | if (substr($method, 0, 5) == 'this:') { | ||
329 | // It's a class method - check it exists | ||
330 | $method = substr($method, 5); | ||
331 | if (!method_exists($this, $method)) { | ||
332 | return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.'); | ||
333 | } | ||
334 | // Call the method | ||
335 | $result = $this->$method($args); | ||
336 | } else { | ||
337 | // It's a function - does it exist? | ||
338 | if (!function_exists($method)) { | ||
339 | return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.'); | ||
340 | } | ||
341 | // Call the function | ||
342 | $result = $method($args); | ||
343 | } | ||
344 | return $result; | ||
345 | } | ||
346 | |||
347 | function error($error, $message = false) { | ||
348 | // Accepts either an error object or an error code and message | ||
349 | if ($message && !is_object($error)) { | ||
350 | $error = new IXR_Error($error, $message); | ||
351 | } | ||
352 | $this->output($error->getXml()); | ||
353 | } | ||
354 | function output($xml) { | ||
355 | $xml = '<?xml version="1.0" encoding="utf-8"?>'."".$xml; | ||
356 | $length = strlen($xml); | ||
357 | header('Connection: close'); | ||
358 | header('Content-Length: '.$length); | ||
359 | header('Content-Type: text/xml'); | ||
360 | header('Date: '.date('r')); | ||
361 | echo $xml; | ||
362 | exit; | ||
363 | } | ||
364 | function hasMethod($method) { | ||
365 | return in_array($method, array_keys($this->callbacks)); | ||
366 | } | ||
367 | function setCapabilities() { | ||
368 | // Initialises capabilities array | ||
369 | $this->capabilities = array( | ||
370 | 'xmlrpc' => array( | ||
371 | 'specUrl' => 'http://www.xmlrpc.com/spec', | ||
372 | 'specVersion' => 1 | ||
373 | ), | ||
374 | 'faults_interop' => array( | ||
375 | 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php', | ||
376 | 'specVersion' => 20010516 | ||
377 | ), | ||
378 | 'system.multicall' => array( | ||
379 | 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', | ||
380 | 'specVersion' => 1 | ||
381 | ), | ||
382 | ); | ||
383 | } | ||
384 | function getCapabilities($args) { | ||
385 | return $this->capabilities; | ||
386 | } | ||
387 | function setCallbacks() { | ||
388 | $this->callbacks['system.getCapabilities'] = 'this:getCapabilities'; | ||
389 | $this->callbacks['system.listMethods'] = 'this:listMethods'; | ||
390 | $this->callbacks['system.multicall'] = 'this:multiCall'; | ||
391 | } | ||
392 | function listMethods($args) { | ||
393 | // Returns a list of methods - uses array_reverse to ensure user defined | ||
394 | // methods are listed before server defined methods | ||
395 | return array_reverse(array_keys($this->callbacks)); | ||
396 | } | ||
397 | function multiCall($methodcalls) { | ||
398 | // See http://www.xmlrpc.com/discuss/msgReader$1208 | ||
399 | $return = array(); | ||
400 | foreach ($methodcalls as $call) { | ||
401 | $method = $call['methodName']; | ||
402 | $params = $call['params']; | ||
403 | if ($method == 'system.multicall') { | ||
404 | $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden'); | ||
405 | } else { | ||
406 | $result = $this->call($method, $params); | ||
407 | } | ||
408 | if (is_a($result, 'IXR_Error')) { | ||
409 | $return[] = array( | ||
410 | 'faultCode' => $result->code, | ||
411 | 'faultString' => $result->message | ||
412 | ); | ||
413 | } else { | ||
414 | $return[] = array($result); | ||
415 | } | ||
416 | } | ||
417 | return $return; | ||
418 | } | ||
419 | } | ||
420 | |||
421 | class IXR_Request { | ||
422 | var $method; | ||
423 | var $args; | ||
424 | var $xml; | ||
425 | function IXR_Request($method, $args) { | ||
426 | $this->method = $method; | ||
427 | $this->args = $args; | ||
428 | $this->xml = <<<EOD | ||
429 | <?xml version="1.0"?> | ||
430 | <methodCall> | ||
431 | <methodName>{$this->method}</methodName> | ||
432 | <params> | ||
433 | |||
434 | EOD; | ||
435 | foreach ($this->args as $arg) { | ||
436 | $this->xml .= '<param><value>'; | ||
437 | $v = new IXR_Value($arg); | ||
438 | $this->xml .= $v->getXml(); | ||
439 | $this->xml .= "</value></param>"; | ||
440 | } | ||
441 | $this->xml .= '</params></methodCall>'; | ||
442 | } | ||
443 | function getLength() { | ||
444 | return strlen($this->xml); | ||
445 | } | ||
446 | function getXml() { | ||
447 | return $this->xml; | ||
448 | } | ||
449 | } | ||
450 | |||
451 | |||
452 | class IXR_Client { | ||
453 | var $server; | ||
454 | var $port; | ||
455 | var $path; | ||
456 | var $useragent; | ||
457 | var $response; | ||
458 | var $message = false; | ||
459 | var $debug = false; | ||
460 | // Storage place for an error message | ||
461 | var $error = false; | ||
462 | function IXR_Client($server, $path = false, $port = 80) { | ||
463 | if (!$path) { | ||
464 | // Assume we have been given a URL instead | ||
465 | $bits = parse_url($server); | ||
466 | $this->server = $bits['host']; | ||
467 | $this->port = isset($bits['port']) ? $bits['port'] : 80; | ||
468 | $this->path = isset($bits['path']) ? $bits['path'] : '/'; | ||
469 | // Make absolutely sure we have a path | ||
470 | if (!$this->path) { | ||
471 | $this->path = '/'; | ||
472 | } | ||
473 | } else { | ||
474 | $this->server = $server; | ||
475 | $this->path = $path; | ||
476 | $this->port = $port; | ||
477 | } | ||
478 | $this->useragent = 'The Incutio XML-RPC PHP Library'; | ||
479 | } | ||
480 | function query() { | ||
481 | $args = func_get_args(); | ||
482 | $method = array_shift($args); | ||
483 | $request = new IXR_Request($method, $args); | ||
484 | $length = $request->getLength(); | ||
485 | $xml = $request->getXml(); | ||
486 | $r = "\r\n"; | ||
487 | $request = "POST {$this->path} HTTP/1.0$r"; | ||
488 | $request .= "Host: {$this->server}$r"; | ||
489 | $request .= "Content-Type: text/xml$r"; | ||
490 | $request .= "User-Agent: {$this->useragent}$r"; | ||
491 | $request .= "Content-length: {$length}$r$r"; | ||
492 | $request .= $xml; | ||
493 | // Now send the request | ||
494 | if ($this->debug) { | ||
495 | echo '<pre>'.htmlspecialchars($request)."\n</pre>\n\n"; | ||
496 | } | ||
497 | $fp = @fsockopen($this->server, $this->port); | ||
498 | if (!$fp) { | ||
499 | $this->error = new IXR_Error(-32300, 'transport error - could not open socket'); | ||
500 | return false; | ||
501 | } | ||
502 | fputs($fp, $request); | ||
503 | $contents = ''; | ||
504 | $gotFirstLine = false; | ||
505 | $gettingHeaders = true; | ||
506 | while (!feof($fp)) { | ||
507 | $line = fgets($fp, 4096); | ||
508 | if (!$gotFirstLine) { | ||
509 | // Check line for '200' | ||
510 | if (strstr($line, '200') === false) { | ||
511 | $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); | ||
512 | return false; | ||
513 | } | ||
514 | $gotFirstLine = true; | ||
515 | } | ||
516 | if (trim($line) == '') { | ||
517 | $gettingHeaders = false; | ||
518 | } | ||
519 | if (!$gettingHeaders) { | ||
520 | $contents .= trim($line)."\n"; | ||
521 | } | ||
522 | } | ||
523 | if ($this->debug) { | ||
524 | echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n"; | ||
525 | } | ||
526 | // Now parse what we've got back | ||
527 | $this->message = new IXR_Message($contents); | ||
528 | if (!$this->message->parse()) { | ||
529 | // XML error | ||
530 | $this->error = new IXR_Error(-32700, 'parse error. not well formed'); | ||
531 | return false; | ||
532 | } | ||
533 | // Is the message a fault? | ||
534 | if ($this->message->messageType == 'fault') { | ||
535 | $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); | ||
536 | return false; | ||
537 | } | ||
538 | // Message must be OK | ||
539 | return true; | ||
540 | } | ||
541 | function getResponse() { | ||
542 | // methodResponses can only have one param - return that | ||
543 | return $this->message->params[0]; | ||
544 | } | ||
545 | function isError() { | ||
546 | return (is_object($this->error)); | ||
547 | } | ||
548 | function getErrorCode() { | ||
549 | return $this->error->code; | ||
550 | } | ||
551 | function getErrorMessage() { | ||
552 | return $this->error->message; | ||
553 | } | ||
554 | } | ||
555 | |||
556 | |||
557 | class IXR_Error { | ||
558 | var $code; | ||
559 | var $message; | ||
560 | function IXR_Error($code, $message) { | ||
561 | $this->code = $code; | ||
562 | $this->message = $message; | ||
563 | } | ||
564 | function getXml() { | ||
565 | $xml = <<<EOD | ||
566 | <methodResponse> | ||
567 | <fault> | ||
568 | <value> | ||
569 | <struct> | ||
570 | <member> | ||
571 | <name>faultCode</name> | ||
572 | <value><int>{$this->code}</int></value> | ||
573 | </member> | ||
574 | <member> | ||
575 | <name>faultString</name> | ||
576 | <value><string>{$this->message}</string></value> | ||
577 | </member> | ||
578 | </struct> | ||
579 | </value> | ||
580 | </fault> | ||
581 | </methodResponse> | ||
582 | |||
583 | EOD; | ||
584 | return $xml; | ||
585 | } | ||
586 | } | ||
587 | |||
588 | |||
589 | class IXR_Date { | ||
590 | var $year; | ||
591 | var $month; | ||
592 | var $day; | ||
593 | var $hour; | ||
594 | var $minute; | ||
595 | var $second; | ||
596 | function IXR_Date($time) { | ||
597 | // $time can be a PHP timestamp or an ISO one | ||
598 | if (is_numeric($time)) { | ||
599 | $this->parseTimestamp($time); | ||
600 | } else { | ||
601 | $this->parseIso($time); | ||
602 | } | ||
603 | } | ||
604 | function parseTimestamp($timestamp) { | ||
605 | $this->year = date('Y', $timestamp); | ||
606 | $this->month = date('Y', $timestamp); | ||
607 | $this->day = date('Y', $timestamp); | ||
608 | $this->hour = date('H', $timestamp); | ||
609 | $this->minute = date('i', $timestamp); | ||
610 | $this->second = date('s', $timestamp); | ||
611 | } | ||
612 | function parseIso($iso) { | ||
613 | $this->year = substr($iso, 0, 4); | ||
614 | $this->month = substr($iso, 4, 2); | ||
615 | $this->day = substr($iso, 6, 2); | ||
616 | $this->hour = substr($iso, 9, 2); | ||
617 | $this->minute = substr($iso, 12, 2); | ||
618 | $this->second = substr($iso, 15, 2); | ||
619 | } | ||
620 | function getIso() { | ||
621 | return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second; | ||
622 | } | ||
623 | function getXml() { | ||
624 | return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>'; | ||
625 | } | ||
626 | function getTimestamp() { | ||
627 | return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year); | ||
628 | } | ||
629 | } | ||
630 | |||
631 | class LLBlock { | ||
632 | var $data; | ||
633 | var $xml; | ||
634 | |||
635 | function LLBlock($data) { | ||
636 | $this->data = $data; | ||
637 | } | ||
638 | |||
639 | function getXml() { | ||
640 | if(count($this->data)>0) { | ||
641 | $this->xml="<array><data><value><struct>"; | ||
642 | foreach($this->data as $name => $value) { | ||
643 | $this->xml=$this->xml."<member><name>".$name."</name><value><string>".$value."</string></value></member>"; | ||
644 | } | ||
645 | $this->xml=$this->xml."</struct></value></data></array>"; | ||
646 | return $this->xml; | ||
647 | } | ||
648 | } | ||
649 | } | ||
650 | |||
651 | class IXR_Base64 { | ||
652 | var $data; | ||
653 | function IXR_Base64($data) { | ||
654 | $this->data = $data; | ||
655 | } | ||
656 | function getXml() { | ||
657 | return '<base64>'.base64_encode($this->data).'</base64>'; | ||
658 | } | ||
659 | } | ||
660 | |||
661 | |||
662 | class IXR_IntrospectionServer extends IXR_Server { | ||
663 | var $signatures; | ||
664 | var $help; | ||
665 | function IXR_IntrospectionServer() { | ||
666 | $this->setCallbacks(); | ||
667 | $this->setCapabilities(); | ||
668 | $this->capabilities['introspection'] = array( | ||
669 | 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html', | ||
670 | 'specVersion' => 1 | ||
671 | ); | ||
672 | $this->addCallback( | ||
673 | 'system.methodSignature', | ||
674 | 'this:methodSignature', | ||
675 | array('array', 'string'), | ||
676 | 'Returns an array describing the return type and required parameters of a method' | ||
677 | ); | ||
678 | $this->addCallback( | ||
679 | 'system.getCapabilities', | ||
680 | 'this:getCapabilities', | ||
681 | array('struct'), | ||
682 | 'Returns a struct describing the XML-RPC specifications supported by this server' | ||
683 | ); | ||
684 | $this->addCallback( | ||
685 | 'system.listMethods', | ||
686 | 'this:listMethods', | ||
687 | array('array'), | ||
688 | 'Returns an array of available methods on this server' | ||
689 | ); | ||
690 | $this->addCallback( | ||
691 | 'system.methodHelp', | ||
692 | 'this:methodHelp', | ||
693 | array('string', 'string'), | ||
694 | 'Returns a documentation string for the specified method' | ||
695 | ); | ||
696 | } | ||
697 | function addCallback($method, $callback, $args, $help) { | ||
698 | $this->callbacks[$method] = $callback; | ||
699 | $this->signatures[$method] = $args; | ||
700 | $this->help[$method] = $help; | ||
701 | } | ||
702 | function call($methodname, $args) { | ||
703 | // Make sure it's in an array | ||
704 | if ($args && !is_array($args)) { | ||
705 | $args = array($args); | ||
706 | } | ||
707 | // Over-rides default call method, adds signature check | ||
708 | if (!$this->hasMethod($methodname)) { | ||
709 | return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.'); | ||
710 | } | ||
711 | $method = $this->callbacks[$methodname]; | ||
712 | $signature = $this->signatures[$methodname]; | ||
713 | $returnType = array_shift($signature); | ||
714 | // Check the number of arguments | ||
715 | if (count($args) != count($signature)) { | ||
716 | // print 'Num of args: '.count($args).' Num in signature: '.count($signature); | ||
717 | return new IXR_Error(-32602, 'server error. wrong number of method parameters'); | ||
718 | } | ||
719 | // Check the argument types | ||
720 | $ok = true; | ||
721 | $argsbackup = $args; | ||
722 | for ($i = 0, $j = count($args); $i < $j; $i++) { | ||
723 | $arg = array_shift($args); | ||
724 | $type = array_shift($signature); | ||
725 | switch ($type) { | ||
726 | case 'int': | ||
727 | case 'i4': | ||
728 | if (is_array($arg) || !is_int($arg)) { | ||
729 | $ok = false; | ||
730 | } | ||
731 | break; | ||
732 | case 'base64': | ||
733 | case 'string': | ||
734 | if (!is_string($arg)) { | ||
735 | $ok = false; | ||
736 | } | ||
737 | break; | ||
738 | case 'boolean': | ||
739 | if ($arg !== false && $arg !== true) { | ||
740 | $ok = false; | ||
741 | } | ||
742 | break; | ||
743 | case 'float': | ||
744 | case 'double': | ||
745 | if (!is_float($arg)) { | ||
746 | $ok = false; | ||
747 | } | ||
748 | break; | ||
749 | case 'date': | ||
750 | case 'dateTime.iso8601': | ||
751 | if (!is_a($arg, 'IXR_Date')) { | ||
752 | $ok = false; | ||
753 | } | ||
754 | break; | ||
755 | } | ||
756 | if (!$ok) { | ||
757 | return new IXR_Error(-32602, 'server error. invalid method parameters'); | ||
758 | } | ||
759 | } | ||
760 | // It passed the test - run the "real" method call | ||
761 | return parent::call($methodname, $argsbackup); | ||
762 | } | ||
763 | function methodSignature($method) { | ||
764 | if (!$this->hasMethod($method)) { | ||
765 | return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.'); | ||
766 | } | ||
767 | // We should be returning an array of types | ||
768 | $types = $this->signatures[$method]; | ||
769 | $return = array(); | ||
770 | foreach ($types as $type) { | ||
771 | switch ($type) { | ||
772 | case 'string': | ||
773 | $return[] = 'string'; | ||
774 | break; | ||
775 | case 'int': | ||
776 | case 'i4': | ||
777 | $return[] = 42; | ||
778 | break; | ||
779 | case 'double': | ||
780 | $return[] = 3.1415; | ||
781 | break; | ||
782 | case 'dateTime.iso8601': | ||
783 | $return[] = new IXR_Date(time()); | ||
784 | break; | ||
785 | case 'boolean': | ||
786 | $return[] = true; | ||
787 | break; | ||
788 | case 'base64': | ||
789 | $return[] = new IXR_Base64('base64'); | ||
790 | break; | ||
791 | case 'array': | ||
792 | $return[] = array('array'); | ||
793 | break; | ||
794 | case 'struct': | ||
795 | $return[] = array('struct' => 'struct'); | ||
796 | break; | ||
797 | } | ||
798 | } | ||
799 | return $return; | ||
800 | } | ||
801 | function methodHelp($method) { | ||
802 | return $this->help[$method]; | ||
803 | } | ||
804 | } | ||
805 | |||
806 | |||
807 | class IXR_ClientMulticall extends IXR_Client { | ||
808 | var $calls = array(); | ||
809 | function IXR_ClientMulticall($server, $path = false, $port = 80) { | ||
810 | parent::IXR_Client($server, $path, $port); | ||
811 | $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)'; | ||
812 | } | ||
813 | function addCall() { | ||
814 | $args = func_get_args(); | ||
815 | $methodName = array_shift($args); | ||
816 | $struct = array( | ||
817 | 'methodName' => $methodName, | ||
818 | 'params' => $args | ||
819 | ); | ||
820 | $this->calls[] = $struct; | ||
821 | } | ||
822 | function query() { | ||
823 | // Prepare multicall, then call the parent::query() method | ||
824 | return parent::query('system.multicall', $this->calls); | ||
825 | } | ||
826 | } | ||
827 | |||
828 | ?> | ||