blob: a9cf19d2cd79ba2792a04e9228f4a3dc598cea3d [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
Derek Jones0b59f272008-05-13 04:22:33 +000016if ( ! function_exists('xml_parser_create'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000017{
18 show_error('Your PHP installation does not support XML');
19}
20
Derek Jones0b59f272008-05-13 04:22:33 +000021if ( ! class_exists('CI_Xmlrpc'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000022{
23 show_error('You must load the Xmlrpc class before loading the Xmlrpcs class in order to create a server.');
24}
25
26// ------------------------------------------------------------------------
27
28/**
29 * XML-RPC server class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category XML-RPC
Derek Allard3d879d52008-01-18 19:41:32 +000034 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000035 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000036 */
37class CI_Xmlrpcs extends CI_Xmlrpc
38{
39 var $methods = array(); //array of methods mapped to function names and signatures
40 var $debug_msg = ''; // Debug Message
41 var $system_methods = array(); // XML RPC Server methods
42 var $controller_obj;
43
Derek Jones044379d2008-05-08 18:57:58 +000044 var $object = FALSE;
45
46
Derek Allardd2df9bc2007-04-15 17:41:17 +000047 //-------------------------------------
48 // Constructor, more or less
49 //-------------------------------------
50
51 function CI_Xmlrpcs($config=array())
52 {
53 parent::CI_Xmlrpc();
54 $this->set_system_methods();
55
56 if (isset($config['functions']) && is_array($config['functions']))
57 {
paulburdickc45cf3c2008-01-22 23:35:10 +000058 $this->methods = array_merge($this->methods, $config['functions']);
Derek Allardd2df9bc2007-04-15 17:41:17 +000059 }
60
61 log_message('debug', "XML-RPC Server Class Initialized");
62 }
63
64 //-------------------------------------
65 // Initialize Prefs and Serve
66 //-------------------------------------
67
68 function initialize($config=array())
69 {
70 if (isset($config['functions']) && is_array($config['functions']))
71 {
paulburdickc45cf3c2008-01-22 23:35:10 +000072 $this->methods = array_merge($this->methods, $config['functions']);
Derek Allardd2df9bc2007-04-15 17:41:17 +000073 }
74
75 if (isset($config['debug']))
76 {
77 $this->debug = $config['debug'];
78 }
Derek Jones044379d2008-05-08 18:57:58 +000079
80 if (isset($config['object']) && is_object($config['object']))
81 {
82 $this->object = $config['object'];
83 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000084 }
85
86 //-------------------------------------
87 // Setting of System Methods
88 //-------------------------------------
89
90 function set_system_methods ()
91 {
paulburdickc45cf3c2008-01-22 23:35:10 +000092 $this->methods = array(
93 'system.listMethods' => array(
94 'function' => 'this.listMethods',
95 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
96 'docstring' => 'Returns an array of available methods on this server'),
97 'system.methodHelp' => array(
98 'function' => 'this.methodHelp',
99 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
100 'docstring' => 'Returns a documentation string for the specified method'),
101 'system.methodSignature' => array(
102 'function' => 'this.methodSignature',
103 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
104 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
105 'system.multicall' => array(
106 'function' => 'this.multicall',
107 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
108 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
109 );
Derek Allardd2df9bc2007-04-15 17:41:17 +0000110 }
111
112
113 //-------------------------------------
114 // Main Server Function
115 //-------------------------------------
116
117 function serve()
118 {
119 $r = $this->parseRequest();
120 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n";
121 $payload .= $this->debug_msg;
122 $payload .= $r->prepare_response();
123
124 header("Content-Type: text/xml");
125 header("Content-Length: ".strlen($payload));
126 echo $payload;
127 }
128
129 //-------------------------------------
130 // Add Method to Class
131 //-------------------------------------
132
133 function add_to_map($methodname,$function,$sig,$doc)
134 {
135 $this->methods[$methodname] = array(
136 'function' => $function,
137 'signature' => $sig,
138 'docstring' => $doc
139 );
140 }
141
142
143 //-------------------------------------
144 // Parse Server Request
145 //-------------------------------------
146
147 function parseRequest($data='')
148 {
149 global $HTTP_RAW_POST_DATA;
150
151 //-------------------------------------
152 // Get Data
153 //-------------------------------------
154
155 if ($data == '')
156 {
157 $data = $HTTP_RAW_POST_DATA;
158 }
159
Derek Allardd2df9bc2007-04-15 17:41:17 +0000160 //-------------------------------------
161 // Set up XML Parser
162 //-------------------------------------
163
164 $parser = xml_parser_create($this->xmlrpc_defencoding);
165 $parser_object = new XML_RPC_Message("filler");
166
167 $parser_object->xh[$parser] = array();
168 $parser_object->xh[$parser]['isf'] = 0;
169 $parser_object->xh[$parser]['isf_reason'] = '';
170 $parser_object->xh[$parser]['params'] = array();
171 $parser_object->xh[$parser]['stack'] = array();
172 $parser_object->xh[$parser]['valuestack'] = array();
173 $parser_object->xh[$parser]['method'] = '';
174
175 xml_set_object($parser, $parser_object);
176 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
177 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
178 xml_set_character_data_handler($parser, 'character_data');
179 //xml_set_default_handler($parser, 'default_handler');
180
181
182 //-------------------------------------
183 // PARSE + PROCESS XML DATA
184 //-------------------------------------
185
Derek Jones0b59f272008-05-13 04:22:33 +0000186 if ( ! xml_parse($parser, $data, 1))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000187 {
188 // return XML error as a faultCode
189 $r = new XML_RPC_Response(0,
190 $this->xmlrpcerrxml + xml_get_error_code($parser),
191 sprintf('XML error: %s at line %d',
192 xml_error_string(xml_get_error_code($parser)),
193 xml_get_current_line_number($parser)));
194 xml_parser_free($parser);
195 }
196 elseif($parser_object->xh[$parser]['isf'])
197 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000198 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000199 }
200 else
201 {
202 xml_parser_free($parser);
203
204 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
205 $plist='';
206
207 for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++)
208 {
paulburdick6a8f97c2007-10-10 15:09:27 +0000209 if ($this->debug === TRUE)
210 {
211 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
212 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000213
214 $m->addParam($parser_object->xh[$parser]['params'][$i]);
215 }
216
217 if ($this->debug === TRUE)
218 {
219 echo "<pre>";
220 echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
221 echo "</pre>";
222 }
223
224 $r = $this->_execute($m);
225 }
226
227 //-------------------------------------
228 // SET DEBUGGING MESSAGE
229 //-------------------------------------
230
231 if ($this->debug === TRUE)
232 {
233 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
234 }
235
236 return $r;
237 }
238
239 //-------------------------------------
240 // Executes the Method
241 //-------------------------------------
242
243 function _execute($m)
244 {
245 $methName = $m->method_name;
246
247 // Check to see if it is a system call
paulburdickc45cf3c2008-01-22 23:35:10 +0000248 $system_call = (strncmp($methName, 'system', 5) == 0) ? TRUE : FALSE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000249
250 //-------------------------------------
paulburdickc45cf3c2008-01-22 23:35:10 +0000251 // Valid Method
Derek Allardd2df9bc2007-04-15 17:41:17 +0000252 //-------------------------------------
253
Derek Jones0b59f272008-05-13 04:22:33 +0000254 if ( ! isset($this->methods[$methName]['function']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000255 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000256 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
257 }
258
259 //-------------------------------------
260 // Check for Method (and Object)
261 //-------------------------------------
262
263 $method_parts = explode(".", $this->methods[$methName]['function']);
264 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? TRUE : FALSE;
265
266 if ($system_call === TRUE)
267 {
Derek Jones0b59f272008-05-13 04:22:33 +0000268 if ( ! is_callable(array($this,$method_parts['1'])))
paulburdickc45cf3c2008-01-22 23:35:10 +0000269 {
270 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
271 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000272 }
273 else
274 {
Derek Jones0b59f272008-05-13 04:22:33 +0000275 if ($objectCall && ! is_callable(array($method_parts['0'],$method_parts['1'])))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000276 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000277 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000278 }
Derek Jones0b59f272008-05-13 04:22:33 +0000279 elseif ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000280 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000281 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
282 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000283 }
paulburdickc45cf3c2008-01-22 23:35:10 +0000284
Derek Allardd2df9bc2007-04-15 17:41:17 +0000285 //-------------------------------------
286 // Checking Methods Signature
287 //-------------------------------------
288
paulburdickc45cf3c2008-01-22 23:35:10 +0000289 if (isset($this->methods[$methName]['signature']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000290 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000291 $sig = $this->methods[$methName]['signature'];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000292 for($i=0; $i<sizeof($sig); $i++)
293 {
294 $current_sig = $sig[$i];
295
296 if (sizeof($current_sig) == sizeof($m->params)+1)
297 {
298 for($n=0; $n < sizeof($m->params); $n++)
299 {
300 $p = $m->params[$n];
paulburdickc45cf3c2008-01-22 23:35:10 +0000301 $pt = ($p->kindOf() == 'scalar') ? $p->scalarval() : $p->kindOf();
Derek Allardd2df9bc2007-04-15 17:41:17 +0000302
303 if ($pt != $current_sig[$n+1])
304 {
305 $pno = $n+1;
306 $wanted = $current_sig[$n+1];
307
308 return new XML_RPC_Response(0,
309 $this->xmlrpcerr['incorrect_params'],
310 $this->xmlrpcstr['incorrect_params'] .
311 ": Wanted {$wanted}, got {$pt} at param {$pno})");
312 }
313 }
314 }
315 }
316 }
317
318 //-------------------------------------
319 // Calls the Function
320 //-------------------------------------
321
paulburdickc45cf3c2008-01-22 23:35:10 +0000322 if ($objectCall === TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000323 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000324 if ($method_parts[0] == "this" && $system_call == TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000325 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000326 return call_user_func(array($this, $method_parts[1]), $m);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000327 }
328 else
329 {
Derek Jones044379d2008-05-08 18:57:58 +0000330 if ($this->object === FALSE)
331 {
332 $CI =& get_instance();
333 return $CI->$method_parts['1']($m);
334 }
335 else
336 {
337 return $this->object->$method_parts['1']($m);
338 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
339 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000340 }
341 }
342 else
343 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000344 return call_user_func($this->methods[$methName]['function'], $m);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000345 }
346 }
347
348
349 //-------------------------------------
350 // Server Function: List Methods
351 //-------------------------------------
352
353 function listMethods($m)
354 {
355 $v = new XML_RPC_Values();
356 $output = array();
paulburdickc45cf3c2008-01-22 23:35:10 +0000357
358 foreach($this->methods as $key => $value)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000359 {
360 $output[] = new XML_RPC_Values($key, 'string');
361 }
362
363 foreach($this->system_methods as $key => $value)
364 {
365 $output[]= new XML_RPC_Values($key, 'string');
366 }
367
368 $v->addArray($output);
369 return new XML_RPC_Response($v);
370 }
371
372 //-------------------------------------
373 // Server Function: Return Signature for Method
374 //-------------------------------------
375
376 function methodSignature($m)
377 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000378 $parameters = $m->output_parameters();
379 $method_name = $parameters[0];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000380
paulburdickc45cf3c2008-01-22 23:35:10 +0000381 if (isset($this->methods[$method_name]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000382 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000383 if ($this->methods[$method_name]['signature'])
Derek Allardd2df9bc2007-04-15 17:41:17 +0000384 {
385 $sigs = array();
paulburdickc45cf3c2008-01-22 23:35:10 +0000386 $signature = $this->methods[$method_name]['signature'];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000387
388 for($i=0; $i < sizeof($signature); $i++)
389 {
390 $cursig = array();
391 $inSig = $signature[$i];
392 for($j=0; $j<sizeof($inSig); $j++)
393 {
394 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
395 }
396 $sigs[]= new XML_RPC_Values($cursig, 'array');
397 }
398 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
399 }
400 else
401 {
402 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
403 }
404 }
405 else
406 {
407 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
408 }
409 return $r;
410 }
411
412 //-------------------------------------
413 // Server Function: Doc String for Method
414 //-------------------------------------
415
416 function methodHelp($m)
417 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000418 $parameters = $m->output_parameters();
419 $method_name = $parameters[0];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000420
paulburdickc45cf3c2008-01-22 23:35:10 +0000421 if (isset($this->methods[$method_name]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000422 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000423 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
424
425 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000426 }
427 else
428 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000429 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000430 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000431 }
432
433 //-------------------------------------
434 // Server Function: Multi-call
435 //-------------------------------------
436
437 function multicall($m)
438 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000439 // Disabled
440 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
441
442 $parameters = $m->output_parameters();
443 $calls = $parameters[0];
444
Derek Allardd2df9bc2007-04-15 17:41:17 +0000445 $result = array();
446
paulburdickc45cf3c2008-01-22 23:35:10 +0000447 foreach ($calls as $value)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000448 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000449 //$attempt = $this->_execute(new XML_RPC_Message($value[0], $value[1]));
450
451 $m = new XML_RPC_Message($value[0]);
452 $plist='';
453
454 for($i=0; $i < sizeof($value[1]); $i++)
455 {
456 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
457 }
458
459 $attempt = $this->_execute($m);
460
461 if ($attempt->faultCode() != 0)
462 {
463 return $attempt;
464 }
465
466 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
Derek Allardd2df9bc2007-04-15 17:41:17 +0000467 }
468
469 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
470 }
471
472
473 //-------------------------------------
474 // Multi-call Function: Error Handling
475 //-------------------------------------
476
477 function multicall_error($err)
478 {
479 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
480 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
481
482 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
483 $struct['faultString'] = new XML_RPC_Values($str, 'string');
484
485 return new XML_RPC_Values($struct, 'struct');
486 }
487
488
489 //-------------------------------------
490 // Multi-call Function: Processes method
491 //-------------------------------------
492
493 function do_multicall($call)
494 {
495 if ($call->kindOf() != 'struct')
496 return $this->multicall_error('notstruct');
Derek Jones0b59f272008-05-13 04:22:33 +0000497 elseif ( ! $methName = $call->me['struct']['methodName'])
Derek Allardd2df9bc2007-04-15 17:41:17 +0000498 return $this->multicall_error('nomethod');
499
500 list($scalar_type,$scalar_value)=each($methName->me);
501 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
502
Derek Jones0b59f272008-05-13 04:22:33 +0000503 if ($methName->kindOf() != 'scalar' OR $scalar_type != 'string')
Derek Allardd2df9bc2007-04-15 17:41:17 +0000504 return $this->multicall_error('notstring');
505 elseif ($scalar_value == 'system.multicall')
506 return $this->multicall_error('recursion');
Derek Jones0b59f272008-05-13 04:22:33 +0000507 elseif ( ! $params = $call->me['struct']['params'])
Derek Allardd2df9bc2007-04-15 17:41:17 +0000508 return $this->multicall_error('noparams');
509 elseif ($params->kindOf() != 'array')
510 return $this->multicall_error('notarray');
511
512 list($a,$b)=each($params->me);
513 $numParams = sizeof($b);
514
515 $msg = new XML_RPC_Message($scalar_value);
516 for ($i = 0; $i < $numParams; $i++)
517 {
518 $msg->params[] = $params->me['array'][$i];
519 }
520
521 $result = $this->_execute($msg);
522
523 if ($result->faultCode() != 0)
524 {
525 return $this->multicall_error($result);
526 }
527
528 return new XML_RPC_Values(array($result->value()), 'array');
529 }
530
531}
532// END XML_RPC_Server class
533
Derek Jones0b59f272008-05-13 04:22:33 +0000534
535/* End of file Xmlrpcs.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000536/* Location: ./system/libraries/Xmlrpcs.php */