blob: ac644d894b9323702ecbd7faa86d6675f7bdf8c0 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +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
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
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
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
44
45 //-------------------------------------
46 // Constructor, more or less
47 //-------------------------------------
48
49 function CI_Xmlrpcs($config=array())
50 {
51 parent::CI_Xmlrpc();
52 $this->set_system_methods();
53
54 if (isset($config['functions']) && is_array($config['functions']))
55 {
paulburdickc45cf3c2008-01-22 23:35:10 +000056 $this->methods = array_merge($this->methods, $config['functions']);
Derek Allardd2df9bc2007-04-15 17:41:17 +000057 }
58
59 log_message('debug', "XML-RPC Server Class Initialized");
60 }
61
62 //-------------------------------------
63 // Initialize Prefs and Serve
64 //-------------------------------------
65
66 function initialize($config=array())
67 {
68 if (isset($config['functions']) && is_array($config['functions']))
69 {
paulburdickc45cf3c2008-01-22 23:35:10 +000070 $this->methods = array_merge($this->methods, $config['functions']);
Derek Allardd2df9bc2007-04-15 17:41:17 +000071 }
72
73 if (isset($config['debug']))
74 {
75 $this->debug = $config['debug'];
76 }
77 }
78
79 //-------------------------------------
80 // Setting of System Methods
81 //-------------------------------------
82
83 function set_system_methods ()
84 {
paulburdickc45cf3c2008-01-22 23:35:10 +000085 $this->methods = array(
86 'system.listMethods' => array(
87 'function' => 'this.listMethods',
88 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
89 'docstring' => 'Returns an array of available methods on this server'),
90 'system.methodHelp' => array(
91 'function' => 'this.methodHelp',
92 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
93 'docstring' => 'Returns a documentation string for the specified method'),
94 'system.methodSignature' => array(
95 'function' => 'this.methodSignature',
96 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
97 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
98 'system.multicall' => array(
99 'function' => 'this.multicall',
100 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
101 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
102 );
Derek Allardd2df9bc2007-04-15 17:41:17 +0000103 }
104
105
106 //-------------------------------------
107 // Main Server Function
108 //-------------------------------------
109
110 function serve()
111 {
112 $r = $this->parseRequest();
113 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n";
114 $payload .= $this->debug_msg;
115 $payload .= $r->prepare_response();
116
117 header("Content-Type: text/xml");
118 header("Content-Length: ".strlen($payload));
119 echo $payload;
120 }
121
122 //-------------------------------------
123 // Add Method to Class
124 //-------------------------------------
125
126 function add_to_map($methodname,$function,$sig,$doc)
127 {
128 $this->methods[$methodname] = array(
129 'function' => $function,
130 'signature' => $sig,
131 'docstring' => $doc
132 );
133 }
134
135
136 //-------------------------------------
137 // Parse Server Request
138 //-------------------------------------
139
140 function parseRequest($data='')
141 {
142 global $HTTP_RAW_POST_DATA;
143
144 //-------------------------------------
145 // Get Data
146 //-------------------------------------
147
148 if ($data == '')
149 {
150 $data = $HTTP_RAW_POST_DATA;
151 }
152
Derek Allardd2df9bc2007-04-15 17:41:17 +0000153 //-------------------------------------
154 // Set up XML Parser
155 //-------------------------------------
156
157 $parser = xml_parser_create($this->xmlrpc_defencoding);
158 $parser_object = new XML_RPC_Message("filler");
159
160 $parser_object->xh[$parser] = array();
161 $parser_object->xh[$parser]['isf'] = 0;
162 $parser_object->xh[$parser]['isf_reason'] = '';
163 $parser_object->xh[$parser]['params'] = array();
164 $parser_object->xh[$parser]['stack'] = array();
165 $parser_object->xh[$parser]['valuestack'] = array();
166 $parser_object->xh[$parser]['method'] = '';
167
168 xml_set_object($parser, $parser_object);
169 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
170 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
171 xml_set_character_data_handler($parser, 'character_data');
172 //xml_set_default_handler($parser, 'default_handler');
173
174
175 //-------------------------------------
176 // PARSE + PROCESS XML DATA
177 //-------------------------------------
178
179 if ( ! xml_parse($parser, $data, 1))
180 {
181 // return XML error as a faultCode
182 $r = new XML_RPC_Response(0,
183 $this->xmlrpcerrxml + xml_get_error_code($parser),
184 sprintf('XML error: %s at line %d',
185 xml_error_string(xml_get_error_code($parser)),
186 xml_get_current_line_number($parser)));
187 xml_parser_free($parser);
188 }
189 elseif($parser_object->xh[$parser]['isf'])
190 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000191 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000192 }
193 else
194 {
195 xml_parser_free($parser);
196
197 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
198 $plist='';
199
200 for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++)
201 {
paulburdick6a8f97c2007-10-10 15:09:27 +0000202 if ($this->debug === TRUE)
203 {
204 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
205 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000206
207 $m->addParam($parser_object->xh[$parser]['params'][$i]);
208 }
209
210 if ($this->debug === TRUE)
211 {
212 echo "<pre>";
213 echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
214 echo "</pre>";
215 }
216
217 $r = $this->_execute($m);
218 }
219
220 //-------------------------------------
221 // SET DEBUGGING MESSAGE
222 //-------------------------------------
223
224 if ($this->debug === TRUE)
225 {
226 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
227 }
228
229 return $r;
230 }
231
232 //-------------------------------------
233 // Executes the Method
234 //-------------------------------------
235
236 function _execute($m)
237 {
238 $methName = $m->method_name;
239
240 // Check to see if it is a system call
paulburdickc45cf3c2008-01-22 23:35:10 +0000241 $system_call = (strncmp($methName, 'system', 5) == 0) ? TRUE : FALSE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000242
243 //-------------------------------------
paulburdickc45cf3c2008-01-22 23:35:10 +0000244 // Valid Method
Derek Allardd2df9bc2007-04-15 17:41:17 +0000245 //-------------------------------------
246
paulburdickc45cf3c2008-01-22 23:35:10 +0000247 if ( ! isset($this->methods[$methName]['function']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000248 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000249 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
250 }
251
252 //-------------------------------------
253 // Check for Method (and Object)
254 //-------------------------------------
255
256 $method_parts = explode(".", $this->methods[$methName]['function']);
257 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? TRUE : FALSE;
258
259 if ($system_call === TRUE)
260 {
261 if (! is_callable(array($this,$method_parts['1'])))
262 {
263 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
264 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000265 }
266 else
267 {
Derek Allardd2df9bc2007-04-15 17:41:17 +0000268 if ($objectCall && !is_callable(array($method_parts['0'],$method_parts['1'])))
269 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000270 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000271 }
paulburdickc45cf3c2008-01-22 23:35:10 +0000272 elseif (!$objectCall && !is_callable($this->methods[$methName]['function']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000273 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000274 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
275 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000276 }
paulburdickc45cf3c2008-01-22 23:35:10 +0000277
Derek Allardd2df9bc2007-04-15 17:41:17 +0000278 //-------------------------------------
279 // Checking Methods Signature
280 //-------------------------------------
281
paulburdickc45cf3c2008-01-22 23:35:10 +0000282 if (isset($this->methods[$methName]['signature']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000283 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000284 $sig = $this->methods[$methName]['signature'];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000285 for($i=0; $i<sizeof($sig); $i++)
286 {
287 $current_sig = $sig[$i];
288
289 if (sizeof($current_sig) == sizeof($m->params)+1)
290 {
291 for($n=0; $n < sizeof($m->params); $n++)
292 {
293 $p = $m->params[$n];
paulburdickc45cf3c2008-01-22 23:35:10 +0000294 $pt = ($p->kindOf() == 'scalar') ? $p->scalarval() : $p->kindOf();
Derek Allardd2df9bc2007-04-15 17:41:17 +0000295
296 if ($pt != $current_sig[$n+1])
297 {
298 $pno = $n+1;
299 $wanted = $current_sig[$n+1];
300
301 return new XML_RPC_Response(0,
302 $this->xmlrpcerr['incorrect_params'],
303 $this->xmlrpcstr['incorrect_params'] .
304 ": Wanted {$wanted}, got {$pt} at param {$pno})");
305 }
306 }
307 }
308 }
309 }
310
311 //-------------------------------------
312 // Calls the Function
313 //-------------------------------------
314
paulburdickc45cf3c2008-01-22 23:35:10 +0000315 if ($objectCall === TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000316 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000317 if ($method_parts[0] == "this" && $system_call == TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000318 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000319 return call_user_func(array($this, $method_parts[1]), $m);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000320 }
321 else
322 {
323 $CI =& get_instance();
324 return $CI->$method_parts['1']($m);
325 //$class = new $method_parts['0'];
326 //return $class->$method_parts['1']($m);
327 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
328 }
329 }
330 else
331 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000332 return call_user_func($this->methods[$methName]['function'], $m);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000333 }
334 }
335
336
337 //-------------------------------------
338 // Server Function: List Methods
339 //-------------------------------------
340
341 function listMethods($m)
342 {
343 $v = new XML_RPC_Values();
344 $output = array();
paulburdickc45cf3c2008-01-22 23:35:10 +0000345
346 foreach($this->methods as $key => $value)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000347 {
348 $output[] = new XML_RPC_Values($key, 'string');
349 }
350
351 foreach($this->system_methods as $key => $value)
352 {
353 $output[]= new XML_RPC_Values($key, 'string');
354 }
355
356 $v->addArray($output);
357 return new XML_RPC_Response($v);
358 }
359
360 //-------------------------------------
361 // Server Function: Return Signature for Method
362 //-------------------------------------
363
364 function methodSignature($m)
365 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000366 $parameters = $m->output_parameters();
367 $method_name = $parameters[0];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000368
paulburdickc45cf3c2008-01-22 23:35:10 +0000369 if (isset($this->methods[$method_name]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000370 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000371 if ($this->methods[$method_name]['signature'])
Derek Allardd2df9bc2007-04-15 17:41:17 +0000372 {
373 $sigs = array();
paulburdickc45cf3c2008-01-22 23:35:10 +0000374 $signature = $this->methods[$method_name]['signature'];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000375
376 for($i=0; $i < sizeof($signature); $i++)
377 {
378 $cursig = array();
379 $inSig = $signature[$i];
380 for($j=0; $j<sizeof($inSig); $j++)
381 {
382 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
383 }
384 $sigs[]= new XML_RPC_Values($cursig, 'array');
385 }
386 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
387 }
388 else
389 {
390 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
391 }
392 }
393 else
394 {
395 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
396 }
397 return $r;
398 }
399
400 //-------------------------------------
401 // Server Function: Doc String for Method
402 //-------------------------------------
403
404 function methodHelp($m)
405 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000406 $parameters = $m->output_parameters();
407 $method_name = $parameters[0];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000408
paulburdickc45cf3c2008-01-22 23:35:10 +0000409 if (isset($this->methods[$method_name]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000410 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000411 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
412
413 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000414 }
415 else
416 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000417 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000418 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000419 }
420
421 //-------------------------------------
422 // Server Function: Multi-call
423 //-------------------------------------
424
425 function multicall($m)
426 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000427 // Disabled
428 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
429
430 $parameters = $m->output_parameters();
431 $calls = $parameters[0];
432
Derek Allardd2df9bc2007-04-15 17:41:17 +0000433 $result = array();
434
paulburdickc45cf3c2008-01-22 23:35:10 +0000435 foreach ($calls as $value)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000436 {
paulburdickc45cf3c2008-01-22 23:35:10 +0000437 //$attempt = $this->_execute(new XML_RPC_Message($value[0], $value[1]));
438
439 $m = new XML_RPC_Message($value[0]);
440 $plist='';
441
442 for($i=0; $i < sizeof($value[1]); $i++)
443 {
444 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
445 }
446
447 $attempt = $this->_execute($m);
448
449 if ($attempt->faultCode() != 0)
450 {
451 return $attempt;
452 }
453
454 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
Derek Allardd2df9bc2007-04-15 17:41:17 +0000455 }
456
457 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
458 }
459
460
461 //-------------------------------------
462 // Multi-call Function: Error Handling
463 //-------------------------------------
464
465 function multicall_error($err)
466 {
467 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
468 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
469
470 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
471 $struct['faultString'] = new XML_RPC_Values($str, 'string');
472
473 return new XML_RPC_Values($struct, 'struct');
474 }
475
476
477 //-------------------------------------
478 // Multi-call Function: Processes method
479 //-------------------------------------
480
481 function do_multicall($call)
482 {
483 if ($call->kindOf() != 'struct')
484 return $this->multicall_error('notstruct');
485 elseif (!$methName = $call->me['struct']['methodName'])
486 return $this->multicall_error('nomethod');
487
488 list($scalar_type,$scalar_value)=each($methName->me);
489 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
490
491 if ($methName->kindOf() != 'scalar' || $scalar_type != 'string')
492 return $this->multicall_error('notstring');
493 elseif ($scalar_value == 'system.multicall')
494 return $this->multicall_error('recursion');
495 elseif (!$params = $call->me['struct']['params'])
496 return $this->multicall_error('noparams');
497 elseif ($params->kindOf() != 'array')
498 return $this->multicall_error('notarray');
499
500 list($a,$b)=each($params->me);
501 $numParams = sizeof($b);
502
503 $msg = new XML_RPC_Message($scalar_value);
504 for ($i = 0; $i < $numParams; $i++)
505 {
506 $msg->params[] = $params->me['array'][$i];
507 }
508
509 $result = $this->_execute($msg);
510
511 if ($result->faultCode() != 0)
512 {
513 return $this->multicall_error($result);
514 }
515
516 return new XML_RPC_Values(array($result->value()), 'array');
517 }
518
519}
520// END XML_RPC_Server class
521
adminb0dd10f2006-08-25 17:25:49 +0000522?>