blob: 01853021666d200848ea83b4f7e3ac0272cc5c05 [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
8 * @author Rick Ellis, Paul Burdick
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 Paul Burdick
35 * @link http://www.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;
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 {
56 $this->methods = $config['functions'];
57 }
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 {
70 $this->methods = $config['functions'];
71 }
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 {
85 $system_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 );
103 }
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
153
154 //-------------------------------------
155 // Set up XML Parser
156 //-------------------------------------
157
158 $parser = xml_parser_create($this->xmlrpc_defencoding);
159 $parser_object = new XML_RPC_Message("filler");
160
161 $parser_object->xh[$parser] = array();
162 $parser_object->xh[$parser]['isf'] = 0;
163 $parser_object->xh[$parser]['isf_reason'] = '';
164 $parser_object->xh[$parser]['params'] = array();
165 $parser_object->xh[$parser]['stack'] = array();
166 $parser_object->xh[$parser]['valuestack'] = array();
167 $parser_object->xh[$parser]['method'] = '';
168
169 xml_set_object($parser, $parser_object);
170 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
171 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
172 xml_set_character_data_handler($parser, 'character_data');
173 //xml_set_default_handler($parser, 'default_handler');
174
175
176 //-------------------------------------
177 // PARSE + PROCESS XML DATA
178 //-------------------------------------
179
180 if ( ! xml_parse($parser, $data, 1))
181 {
182 // return XML error as a faultCode
183 $r = new XML_RPC_Response(0,
184 $this->xmlrpcerrxml + xml_get_error_code($parser),
185 sprintf('XML error: %s at line %d',
186 xml_error_string(xml_get_error_code($parser)),
187 xml_get_current_line_number($parser)));
188 xml_parser_free($parser);
189 }
190 elseif($parser_object->xh[$parser]['isf'])
191 {
192 return new XML_RPC_Response(0,
193 $this->xmlrpcerr['invalid_return'],
Derek Allard74c46a12007-04-30 01:39:39 +0000194 $this->xmlrpcstr['invalid_return']);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000195 }
196 else
197 {
198 xml_parser_free($parser);
199
200 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
201 $plist='';
202
203 for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++)
204 {
205 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
206
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
241 // If so, load the system_methods
242 $sysCall = ereg("^system\.", $methName);
243 $methods = $sysCall ? $this->system_methods : $this->methods;
244
245 //-------------------------------------
246 // Check for Function
247 //-------------------------------------
248
249 if (!isset($methods[$methName]['function']))
250 {
251 return new XML_RPC_Response(0,
252 $this->xmlrpcerr['unknown_method'],
253 $this->xmlrpcstr['unknown_method']);
254 }
255 else
256 {
257 // See if we are calling function in an object
258
259 $method_parts = explode(".",$methods[$methName]['function']);
260 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? true : false;
261
262 if ($objectCall && !is_callable(array($method_parts['0'],$method_parts['1'])))
263 {
264 return new XML_RPC_Response(0,
265 $this->xmlrpcerr['unknown_method'],
266 $this->xmlrpcstr['unknown_method']);
267 }
268 elseif (!$objectCall && !is_callable($methods[$methName]['function']))
269 {
270 return new XML_RPC_Response(0,
271 $this->xmlrpcerr['unknown_method'],
272 $this->xmlrpcstr['unknown_method']);
273 }
274 }
275
276 //-------------------------------------
277 // Checking Methods Signature
278 //-------------------------------------
279
280 if (isset($methods[$methName]['signature']))
281 {
282 $sig = $methods[$methName]['signature'];
283 for($i=0; $i<sizeof($sig); $i++)
284 {
285 $current_sig = $sig[$i];
286
287 if (sizeof($current_sig) == sizeof($m->params)+1)
288 {
289 for($n=0; $n < sizeof($m->params); $n++)
290 {
291 $p = $m->params[$n];
292 $pt = ($p->kindOf() == 'scalar') ? $p->scalartyp() : $p->kindOf();
293
294 if ($pt != $current_sig[$n+1])
295 {
296 $pno = $n+1;
297 $wanted = $current_sig[$n+1];
298
299 return new XML_RPC_Response(0,
300 $this->xmlrpcerr['incorrect_params'],
301 $this->xmlrpcstr['incorrect_params'] .
302 ": Wanted {$wanted}, got {$pt} at param {$pno})");
303 }
304 }
305 }
306 }
307 }
308
309 //-------------------------------------
310 // Calls the Function
311 //-------------------------------------
312
313 if ($objectCall)
314 {
315 if ($method_parts['1'] == "this")
316 {
317 return call_user_func(array($this, $method_parts['0']), $m);
318 }
319 else
320 {
321 $CI =& get_instance();
322 return $CI->$method_parts['1']($m);
323 //$class = new $method_parts['0'];
324 //return $class->$method_parts['1']($m);
325 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
326 }
327 }
328 else
329 {
330 return call_user_func($methods[$methName]['function'], $m);
331 }
332 }
333
334
335 //-------------------------------------
336 // Server Function: List Methods
337 //-------------------------------------
338
339 function listMethods($m)
340 {
341 $v = new XML_RPC_Values();
342 $output = array();
343 foreach($this->$methods as $key => $value)
344 {
345 $output[] = new XML_RPC_Values($key, 'string');
346 }
347
348 foreach($this->system_methods as $key => $value)
349 {
350 $output[]= new XML_RPC_Values($key, 'string');
351 }
352
353 $v->addArray($output);
354 return new XML_RPC_Response($v);
355 }
356
357 //-------------------------------------
358 // Server Function: Return Signature for Method
359 //-------------------------------------
360
361 function methodSignature($m)
362 {
363 $methName = $m->getParam(0);
364 $method_name = $methName->scalarval();
365
366 $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
367
368 if (isset($methods[$method_name]))
369 {
370 if ($methods[$method_name]['signature'])
371 {
372 $sigs = array();
373 $signature = $methods[$method_name]['signature'];
374
375 for($i=0; $i < sizeof($signature); $i++)
376 {
377 $cursig = array();
378 $inSig = $signature[$i];
379 for($j=0; $j<sizeof($inSig); $j++)
380 {
381 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
382 }
383 $sigs[]= new XML_RPC_Values($cursig, 'array');
384 }
385 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
386 }
387 else
388 {
389 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
390 }
391 }
392 else
393 {
394 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
395 }
396 return $r;
397 }
398
399 //-------------------------------------
400 // Server Function: Doc String for Method
401 //-------------------------------------
402
403 function methodHelp($m)
404 {
405 $methName = $m->getParam(0);
406 $method_name = $methName->scalarval();
407
408 $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
409
410 if (isset($methods[$methName]))
411 {
412 $docstring = isset($methods[$method_name]['docstring']) ? $methods[$method_name]['docstring'] : '';
413 $r = new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
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: Multi-call
424 //-------------------------------------
425
426 function multicall($m)
427 {
428 $calls = $m->getParam(0);
429 list($a,$b)=each($calls->me);
430 $result = array();
431
432 for ($i = 0; $i < sizeof($b); $i++)
433 {
434 $call = $calls->me['array'][$i];
435 $result[$i] = $this->do_multicall($call);
436 }
437
438 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
439 }
440
441
442 //-------------------------------------
443 // Multi-call Function: Error Handling
444 //-------------------------------------
445
446 function multicall_error($err)
447 {
448 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
449 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
450
451 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
452 $struct['faultString'] = new XML_RPC_Values($str, 'string');
453
454 return new XML_RPC_Values($struct, 'struct');
455 }
456
457
458 //-------------------------------------
459 // Multi-call Function: Processes method
460 //-------------------------------------
461
462 function do_multicall($call)
463 {
464 if ($call->kindOf() != 'struct')
465 return $this->multicall_error('notstruct');
466 elseif (!$methName = $call->me['struct']['methodName'])
467 return $this->multicall_error('nomethod');
468
469 list($scalar_type,$scalar_value)=each($methName->me);
470 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
471
472 if ($methName->kindOf() != 'scalar' || $scalar_type != 'string')
473 return $this->multicall_error('notstring');
474 elseif ($scalar_value == 'system.multicall')
475 return $this->multicall_error('recursion');
476 elseif (!$params = $call->me['struct']['params'])
477 return $this->multicall_error('noparams');
478 elseif ($params->kindOf() != 'array')
479 return $this->multicall_error('notarray');
480
481 list($a,$b)=each($params->me);
482 $numParams = sizeof($b);
483
484 $msg = new XML_RPC_Message($scalar_value);
485 for ($i = 0; $i < $numParams; $i++)
486 {
487 $msg->params[] = $params->me['array'][$i];
488 }
489
490 $result = $this->_execute($msg);
491
492 if ($result->faultCode() != 0)
493 {
494 return $this->multicall_error($result);
495 }
496
497 return new XML_RPC_Values(array($result->value()), 'array');
498 }
499
500}
501// END XML_RPC_Server class
502
adminb0dd10f2006-08-25 17:25:49 +0000503?>