blob: c1fe649f9b6f429df221a0d4db77f3ae6897558e [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16if ( ! function_exists('xml_parser_create'))
17{
18 show_error('Your PHP installation does not support XML');
19}
20
21if ( ! class_exists('CI_Xmlrpc'))
22{
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
34 * @author ExpressionEngine Dev Team
35 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
36 */
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;
Derek Jonesc4c34ee2010-03-02 23:00:28 -060043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 var $object = FALSE;
Derek Jonesc4c34ee2010-03-02 23:00:28 -060045
46
Derek Allard2067d1a2008-11-13 22:59:24 +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 {
58 $this->methods = array_merge($this->methods, $config['functions']);
59 }
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 {
72 $this->methods = array_merge($this->methods, $config['functions']);
73 }
74
75 if (isset($config['debug']))
76 {
77 $this->debug = $config['debug'];
78 }
79
80 if (isset($config['object']) && is_object($config['object']))
81 {
82 $this->object = $config['object'];
83 }
Robin Sowell66a3fc02010-03-18 09:44:55 -040084
85 if (isset($config['xss_clean']))
86 {
87 $this->xss_clean = $config['xss_clean'];
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90
91 //-------------------------------------
92 // Setting of System Methods
93 //-------------------------------------
94
95 function set_system_methods ()
96 {
97 $this->methods = array(
98 'system.listMethods' => array(
99 'function' => 'this.listMethods',
100 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
101 'docstring' => 'Returns an array of available methods on this server'),
102 'system.methodHelp' => array(
103 'function' => 'this.methodHelp',
104 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
105 'docstring' => 'Returns a documentation string for the specified method'),
106 'system.methodSignature' => array(
107 'function' => 'this.methodSignature',
108 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
109 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
110 'system.multicall' => array(
111 'function' => 'this.multicall',
112 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
113 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
114 );
115 }
116
117
118 //-------------------------------------
119 // Main Server Function
120 //-------------------------------------
121
122 function serve()
123 {
124 $r = $this->parseRequest();
125 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n";
126 $payload .= $this->debug_msg;
127 $payload .= $r->prepare_response();
128
129 header("Content-Type: text/xml");
130 header("Content-Length: ".strlen($payload));
Derek Jonesb8d3c3d2009-06-24 15:27:01 +0000131 exit($payload);
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 //-------------------------------------
135 // Add Method to Class
136 //-------------------------------------
137
138 function add_to_map($methodname,$function,$sig,$doc)
139 {
140 $this->methods[$methodname] = array(
141 'function' => $function,
142 'signature' => $sig,
143 'docstring' => $doc
144 );
145 }
146
147
148 //-------------------------------------
149 // Parse Server Request
150 //-------------------------------------
151
152 function parseRequest($data='')
153 {
154 global $HTTP_RAW_POST_DATA;
155
156 //-------------------------------------
157 // Get Data
158 //-------------------------------------
159
160 if ($data == '')
161 {
162 $data = $HTTP_RAW_POST_DATA;
163 }
164
165 //-------------------------------------
166 // Set up XML Parser
167 //-------------------------------------
168
169 $parser = xml_parser_create($this->xmlrpc_defencoding);
170 $parser_object = new XML_RPC_Message("filler");
171
172 $parser_object->xh[$parser] = array();
173 $parser_object->xh[$parser]['isf'] = 0;
174 $parser_object->xh[$parser]['isf_reason'] = '';
175 $parser_object->xh[$parser]['params'] = array();
176 $parser_object->xh[$parser]['stack'] = array();
177 $parser_object->xh[$parser]['valuestack'] = array();
178 $parser_object->xh[$parser]['method'] = '';
179
180 xml_set_object($parser, $parser_object);
181 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
182 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
183 xml_set_character_data_handler($parser, 'character_data');
184 //xml_set_default_handler($parser, 'default_handler');
185
186
187 //-------------------------------------
188 // PARSE + PROCESS XML DATA
189 //-------------------------------------
190
191 if ( ! xml_parse($parser, $data, 1))
192 {
193 // return XML error as a faultCode
194 $r = new XML_RPC_Response(0,
195 $this->xmlrpcerrxml + xml_get_error_code($parser),
196 sprintf('XML error: %s at line %d',
197 xml_error_string(xml_get_error_code($parser)),
198 xml_get_current_line_number($parser)));
199 xml_parser_free($parser);
200 }
201 elseif($parser_object->xh[$parser]['isf'])
202 {
203 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
204 }
205 else
206 {
207 xml_parser_free($parser);
208
209 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
210 $plist='';
211
Derek Jones33559102009-02-02 18:50:38 +0000212 for($i=0; $i < count($parser_object->xh[$parser]['params']); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 if ($this->debug === TRUE)
215 {
216 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
217 }
218
219 $m->addParam($parser_object->xh[$parser]['params'][$i]);
220 }
221
222 if ($this->debug === TRUE)
223 {
224 echo "<pre>";
225 echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
226 echo "</pre>";
227 }
228
229 $r = $this->_execute($m);
230 }
231
232 //-------------------------------------
233 // SET DEBUGGING MESSAGE
234 //-------------------------------------
235
236 if ($this->debug === TRUE)
237 {
238 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
239 }
240
241 return $r;
242 }
243
244 //-------------------------------------
245 // Executes the Method
246 //-------------------------------------
247
248 function _execute($m)
249 {
250 $methName = $m->method_name;
251
252 // Check to see if it is a system call
253 $system_call = (strncmp($methName, 'system', 5) == 0) ? TRUE : FALSE;
254
Robin Sowell66a3fc02010-03-18 09:44:55 -0400255 if ($this->xss_clean == FALSE)
256 {
257 $m->xss_clean = FALSE;
258 }
259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 //-------------------------------------
261 // Valid Method
262 //-------------------------------------
263
264 if ( ! isset($this->methods[$methName]['function']))
265 {
266 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
267 }
268
269 //-------------------------------------
270 // Check for Method (and Object)
271 //-------------------------------------
272
273 $method_parts = explode(".", $this->methods[$methName]['function']);
274 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? TRUE : FALSE;
275
276 if ($system_call === TRUE)
277 {
278 if ( ! is_callable(array($this,$method_parts['1'])))
279 {
280 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
281 }
282 }
283 else
284 {
285 if ($objectCall && ! is_callable(array($method_parts['0'],$method_parts['1'])))
286 {
287 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
288 }
289 elseif ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
290 {
291 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
292 }
293 }
294
295 //-------------------------------------
296 // Checking Methods Signature
297 //-------------------------------------
298
299 if (isset($this->methods[$methName]['signature']))
300 {
301 $sig = $this->methods[$methName]['signature'];
Derek Jones33559102009-02-02 18:50:38 +0000302 for($i=0; $i<count($sig); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $current_sig = $sig[$i];
305
Derek Jones33559102009-02-02 18:50:38 +0000306 if (count($current_sig) == count($m->params)+1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Derek Jones33559102009-02-02 18:50:38 +0000308 for($n=0; $n < count($m->params); $n++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 $p = $m->params[$n];
311 $pt = ($p->kindOf() == 'scalar') ? $p->scalarval() : $p->kindOf();
312
313 if ($pt != $current_sig[$n+1])
314 {
315 $pno = $n+1;
316 $wanted = $current_sig[$n+1];
317
318 return new XML_RPC_Response(0,
319 $this->xmlrpcerr['incorrect_params'],
320 $this->xmlrpcstr['incorrect_params'] .
321 ": Wanted {$wanted}, got {$pt} at param {$pno})");
322 }
323 }
324 }
325 }
326 }
327
328 //-------------------------------------
329 // Calls the Function
330 //-------------------------------------
331
332 if ($objectCall === TRUE)
333 {
334 if ($method_parts[0] == "this" && $system_call == TRUE)
335 {
336 return call_user_func(array($this, $method_parts[1]), $m);
337 }
338 else
339 {
340 if ($this->object === FALSE)
341 {
342 $CI =& get_instance();
343 return $CI->$method_parts['1']($m);
344 }
345 else
346 {
347 return $this->object->$method_parts['1']($m);
348 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
349 }
350 }
351 }
352 else
353 {
354 return call_user_func($this->methods[$methName]['function'], $m);
355 }
356 }
357
358
359 //-------------------------------------
360 // Server Function: List Methods
361 //-------------------------------------
362
363 function listMethods($m)
364 {
365 $v = new XML_RPC_Values();
366 $output = array();
367
368 foreach($this->methods as $key => $value)
369 {
370 $output[] = new XML_RPC_Values($key, 'string');
371 }
372
373 foreach($this->system_methods as $key => $value)
374 {
375 $output[]= new XML_RPC_Values($key, 'string');
376 }
377
378 $v->addArray($output);
379 return new XML_RPC_Response($v);
380 }
381
382 //-------------------------------------
383 // Server Function: Return Signature for Method
384 //-------------------------------------
385
386 function methodSignature($m)
387 {
388 $parameters = $m->output_parameters();
389 $method_name = $parameters[0];
390
391 if (isset($this->methods[$method_name]))
392 {
393 if ($this->methods[$method_name]['signature'])
394 {
395 $sigs = array();
396 $signature = $this->methods[$method_name]['signature'];
397
Derek Jones33559102009-02-02 18:50:38 +0000398 for($i=0; $i < count($signature); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 $cursig = array();
401 $inSig = $signature[$i];
Derek Jones33559102009-02-02 18:50:38 +0000402 for($j=0; $j<count($inSig); $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
405 }
406 $sigs[]= new XML_RPC_Values($cursig, 'array');
407 }
408 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
409 }
410 else
411 {
412 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
413 }
414 }
415 else
416 {
417 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
418 }
419 return $r;
420 }
421
422 //-------------------------------------
423 // Server Function: Doc String for Method
424 //-------------------------------------
425
426 function methodHelp($m)
427 {
428 $parameters = $m->output_parameters();
429 $method_name = $parameters[0];
430
431 if (isset($this->methods[$method_name]))
432 {
433 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
434
435 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
436 }
437 else
438 {
439 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
440 }
441 }
442
443 //-------------------------------------
444 // Server Function: Multi-call
445 //-------------------------------------
446
447 function multicall($m)
448 {
449 // Disabled
450 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
451
452 $parameters = $m->output_parameters();
453 $calls = $parameters[0];
454
455 $result = array();
456
457 foreach ($calls as $value)
458 {
459 //$attempt = $this->_execute(new XML_RPC_Message($value[0], $value[1]));
460
461 $m = new XML_RPC_Message($value[0]);
462 $plist='';
463
Derek Jones33559102009-02-02 18:50:38 +0000464 for($i=0; $i < count($value[1]); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
467 }
468
469 $attempt = $this->_execute($m);
470
471 if ($attempt->faultCode() != 0)
472 {
473 return $attempt;
474 }
475
476 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
477 }
478
479 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
480 }
481
482
483 //-------------------------------------
484 // Multi-call Function: Error Handling
485 //-------------------------------------
486
487 function multicall_error($err)
488 {
489 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
490 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
491
492 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
493 $struct['faultString'] = new XML_RPC_Values($str, 'string');
494
495 return new XML_RPC_Values($struct, 'struct');
496 }
497
498
499 //-------------------------------------
500 // Multi-call Function: Processes method
501 //-------------------------------------
502
503 function do_multicall($call)
504 {
505 if ($call->kindOf() != 'struct')
506 return $this->multicall_error('notstruct');
507 elseif ( ! $methName = $call->me['struct']['methodName'])
508 return $this->multicall_error('nomethod');
509
510 list($scalar_type,$scalar_value)=each($methName->me);
511 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
512
513 if ($methName->kindOf() != 'scalar' OR $scalar_type != 'string')
514 return $this->multicall_error('notstring');
515 elseif ($scalar_value == 'system.multicall')
516 return $this->multicall_error('recursion');
517 elseif ( ! $params = $call->me['struct']['params'])
518 return $this->multicall_error('noparams');
519 elseif ($params->kindOf() != 'array')
520 return $this->multicall_error('notarray');
521
522 list($a,$b)=each($params->me);
Derek Jones33559102009-02-02 18:50:38 +0000523 $numParams = count($b);
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525 $msg = new XML_RPC_Message($scalar_value);
526 for ($i = 0; $i < $numParams; $i++)
527 {
528 $msg->params[] = $params->me['array'][$i];
529 }
530
531 $result = $this->_execute($msg);
532
533 if ($result->faultCode() != 0)
534 {
535 return $this->multicall_error($result);
536 }
537
538 return new XML_RPC_Values(array($result->value()), 'array');
539 }
540
541}
542// END XML_RPC_Server class
543
544
545/* End of file Xmlrpcs.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000546/* Location: ./system/libraries/Xmlrpcs.php */