blob: 4d50dfb965b0b4d053223e7123bac3883b6fc5cf [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, 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 */
admine79dc712006-09-26 03:52:45 +000015
16
17// INITIALIZE THE CLASS ---------------------------------------------------
18
19$config = array();
20if (file_exists(APPPATH.'config/xmlrpcs'.EXT))
21{
22 include_once(APPPATH.'config/xmlrpcs'.EXT);
23}
24
25if ( ! class_exists('CI_XML_RPC'))
26{
27 if ( ! file_exists(BASEPATH.'libraries/Xmlrpc'.EXT))
28 {
29 if ( ! file_exists(APPPATH.'libraries/Xmlrpc'.EXT))
30 {
31 show_error('Unable to locate the Xmlrpc class');
32 }
33 else
34 {
35 require_once(APPPATH.'libraries/Xmlrpc'.EXT);
36 }
37 }
38 else
39 {
40 require_once(BASEPATH.'libraries/Xmlrpc'.EXT);
41 }
42}
43
44$obj =& get_instance();
45$obj->xmlrpcs = new CI_XML_RPC_Server($config);
46
adminb0dd10f2006-08-25 17:25:49 +000047// ------------------------------------------------------------------------
48
49/**
50 * XML-RPC server class
51 *
52 * @package CodeIgniter
53 * @subpackage Libraries
54 * @category XML-RPC
55 * @author Paul Burdick
56 * @link http://www.codeigniter.com/user_guide/libraries/xmlrpc.html
57 */
58class CI_XML_RPC_Server extends CI_XML_RPC
59{
60 var $methods = array(); //array of methods mapped to function names and signatures
61 var $debug_msg = ''; // Debug Message
62 var $system_methods = array(); // XML RPC Server methods
63 var $controller_obj;
64
65
66 //-------------------------------------
67 // Constructor, more or less
68 //-------------------------------------
69
70 function CI_XML_RPC_Server($config=array())
71 {
72 parent::CI_XML_RPC();
73 $this->set_system_methods();
74
75 if (isset($config['functions']) && is_array($config['functions']))
76 {
77 $this->methods = $config['functions'];
78 }
79
80 log_message('debug', "XML-RPC Server Class Initialized");
81 }
82
83 //-------------------------------------
84 // Initialize Prefs and Serve
85 //-------------------------------------
86
87 function initialize($config=array())
88 {
89 if (isset($config['functions']) && is_array($config['functions']))
90 {
91 $this->methods = $config['functions'];
92 }
93
94 if (isset($config['debug']))
95 {
96 $this->debug = $config['debug'];
97 }
98 }
99
100 //-------------------------------------
101 // Setting of System Methods
102 //-------------------------------------
103
104 function set_system_methods ()
105 {
106 $system_methods = array(
107 'system.listMethods' => array(
108 'function' => 'this.listMethods',
109 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
110 'docstring' => 'Returns an array of available methods on this server'),
111 'system.methodHelp' => array(
112 'function' => 'this.methodHelp',
113 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
114 'docstring' => 'Returns a documentation string for the specified method'),
115 'system.methodSignature' => array(
116 'function' => 'this.methodSignature',
117 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
118 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
119 'system.multicall' => array(
120 'function' => 'this.multicall',
121 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
122 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
123 );
124 }
125
126
127 //-------------------------------------
128 // Main Server Function
129 //-------------------------------------
130
131 function serve()
132 {
133 $r = $this->parseRequest();
134 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n";
135 $payload .= $this->debug_msg;
136 $payload .= $r->prepare_response();
137
138 header("Content-Type: text/xml");
139 header("Content-Length: ".strlen($payload));
140 echo $payload;
141 }
142
143 //-------------------------------------
144 // Add Method to Class
145 //-------------------------------------
146
147 function add_to_map($methodname,$function,$sig,$doc)
148 {
149 $this->methods[$methodname] = array(
150 'function' => $function,
151 'signature' => $sig,
152 'docstring' => $doc
153 );
154 }
155
156
157 //-------------------------------------
158 // Parse Server Request
159 //-------------------------------------
160
161 function parseRequest($data='')
162 {
163 global $HTTP_RAW_POST_DATA;
164
165 //-------------------------------------
166 // Get Data
167 //-------------------------------------
168
169 if ($data == '')
170 {
171 $data = $HTTP_RAW_POST_DATA;
172 }
173
174
175 //-------------------------------------
176 // Set up XML Parser
177 //-------------------------------------
178
179 $parser = xml_parser_create($this->xmlrpc_defencoding);
180 $parser_object = new XML_RPC_Message("filler");
181
182 $parser_object->xh[$parser] = array();
183 $parser_object->xh[$parser]['isf'] = 0;
184 $parser_object->xh[$parser]['isf_reason'] = '';
185 $parser_object->xh[$parser]['params'] = array();
186 $parser_object->xh[$parser]['stack'] = array();
187 $parser_object->xh[$parser]['valuestack'] = array();
188 $parser_object->xh[$parser]['method'] = '';
189
190 xml_set_object($parser, $parser_object);
191 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
192 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
193 xml_set_character_data_handler($parser, 'character_data');
194 //xml_set_default_handler($parser, 'default_handler');
195
196
197 //-------------------------------------
198 // PARSE + PROCESS XML DATA
199 //-------------------------------------
200
201 if ( ! xml_parse($parser, $data, 1))
202 {
203 // return XML error as a faultCode
204 $r = new XML_RPC_Response(0,
205 $this->xmlrpcerrxml + xml_get_error_code($parser),
206 sprintf('XML error: %s at line %d',
207 xml_error_string(xml_get_error_code($parser)),
208 xml_get_current_line_number($parser)));
209 xml_parser_free($parser);
210 }
211 elseif($parser_object->xh[$parser]['isf'])
212 {
213 return new XML_RPC_Response(0,
214 $this->xmlrpcerr['invalid_return'],
215 $this->xmlrpcstr['invalid_retrun']);
216 }
217 else
218 {
219 xml_parser_free($parser);
220
221 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
222 $plist='';
223
224 for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++)
225 {
226 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
227
228 $m->addParam($parser_object->xh[$parser]['params'][$i]);
229 }
230
231 if ($this->debug === TRUE)
232 {
233 echo "<pre>";
234 echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
235 echo "</pre>";
236 }
237
238 $r = $this->execute($m);
239 }
240
241 //-------------------------------------
242 // SET DEBUGGING MESSAGE
243 //-------------------------------------
244
245 if ($this->debug === TRUE)
246 {
247 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
248 }
249
250 return $r;
251 }
252
253 //-------------------------------------
254 // Executes the Method
255 //-------------------------------------
256
admine885d782006-09-23 20:25:05 +0000257 function _execute($m)
adminb0dd10f2006-08-25 17:25:49 +0000258 {
259 $methName = $m->method_name;
260
261 // Check to see if it is a system call
262 // If so, load the system_methods
263 $sysCall = ereg("^system\.", $methName);
264 $methods = $sysCall ? $this->system_methods : $this->methods;
265
266 //-------------------------------------
267 // Check for Function
268 //-------------------------------------
269
270 if (!isset($methods[$methName]['function']))
271 {
272 return new XML_RPC_Response(0,
273 $this->xmlrpcerr['unknown_method'],
274 $this->xmlrpcstr['unknown_method']);
275 }
276 else
277 {
278 // See if we are calling function in an object
279
280 $method_parts = explode(".",$methods[$methName]['function']);
281 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? true : false;
282
283 if ($objectCall && !is_callable(array($method_parts['0'],$method_parts['1'])))
284 {
285 return new XML_RPC_Response(0,
286 $this->xmlrpcerr['unknown_method'],
287 $this->xmlrpcstr['unknown_method']);
288 }
289 elseif (!$objectCall && !is_callable($methods[$methName]['function']))
290 {
291 return new XML_RPC_Response(0,
292 $this->xmlrpcerr['unknown_method'],
293 $this->xmlrpcstr['unknown_method']);
294 }
295 }
296
297 //-------------------------------------
298 // Checking Methods Signature
299 //-------------------------------------
300
301 if (isset($methods[$methName]['signature']))
302 {
303 $sig = $methods[$methName]['signature'];
304 for($i=0; $i<sizeof($sig); $i++)
305 {
306 $current_sig = $sig[$i];
307
308 if (sizeof($current_sig) == sizeof($m->params)+1)
309 {
310 for($n=0; $n < sizeof($m->params); $n++)
311 {
312 $p = $m->params[$n];
313 $pt = ($p->kindOf() == 'scalar') ? $p->scalartyp() : $p->kindOf();
314
315 if ($pt != $current_sig[$n+1])
316 {
317 $pno = $n+1;
318 $wanted = $current_sig[$n+1];
319
320 return new XML_RPC_Response(0,
321 $this->xmlrpcerr['incorrect_params'],
322 $this->xmlrpcstr['incorrect_params'] .
323 ": Wanted {$wanted}, got {$pt} at param {$pno})");
324 }
325 }
326 }
327 }
328 }
329
330 //-------------------------------------
331 // Calls the Function
332 //-------------------------------------
333
334 if ($objectCall)
335 {
336 if ($method_parts['1'] == "this")
337 {
338 return call_user_func(array($this, $method_parts['0']), $m);
339 }
340 else
341 {
342 $obj =& get_instance();
343 return $obj->$method_parts['1']($m);
344 //$class = new $method_parts['0'];
345 //return $class->$method_parts['1']($m);
346 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
347 }
348 }
349 else
350 {
351 return call_user_func($methods[$methName]['function'], $m);
352 }
353 }
354
355
356 //-------------------------------------
357 // Server Function: List Methods
358 //-------------------------------------
359
360 function listMethods($m)
361 {
362 $v = new XML_RPC_Values();
363 $output = array();
364 foreach($this->$methods as $key => $value)
365 {
366 $output[] = new XML_RPC_Values($key, 'string');
367 }
368
369 foreach($this->system_methods as $key => $value)
370 {
371 $output[]= new XML_RPC_Values($key, 'string');
372 }
373
374 $v->addArray($output);
375 return new XML_RPC_Response($v);
376 }
377
378 //-------------------------------------
379 // Server Function: Return Signature for Method
380 //-------------------------------------
381
382 function methodSignature($m)
383 {
384 $methName = $m->getParam(0);
385 $method_name = $methName->scalarval();
386
387 $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
388
389 if (isset($methods[$method_name]))
390 {
391 if ($methods[$method_name]['signature'])
392 {
393 $sigs = array();
394 $signature = $methods[$method_name]['signature'];
395
396 for($i=0; $i < sizeof($signature); $i++)
397 {
398 $cursig = array();
399 $inSig = $signature[$i];
400 for($j=0; $j<sizeof($inSig); $j++)
401 {
402 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
403 }
404 $sigs[]= new XML_RPC_Values($cursig, 'array');
405 }
406 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
407 }
408 else
409 {
410 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
411 }
412 }
413 else
414 {
415 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
416 }
417 return $r;
418 }
419
420 //-------------------------------------
421 // Server Function: Doc String for Method
422 //-------------------------------------
423
424 function methodHelp($m)
425 {
426 $methName = $m->getParam(0);
427 $method_name = $methName->scalarval();
428
429 $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
430
431 if (isset($methods[$methName]))
432 {
433 $docstring = isset($methods[$method_name]['docstring']) ? $methods[$method_name]['docstring'] : '';
434 $r = new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
435 }
436 else
437 {
438 $r = new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
439 }
440 return $r;
441 }
442
443 //-------------------------------------
444 // Server Function: Multi-call
445 //-------------------------------------
446
447 function multicall($m)
448 {
449 $calls = $m->getParam(0);
450 list($a,$b)=each($calls->me);
451 $result = array();
452
453 for ($i = 0; $i < sizeof($b); $i++)
454 {
455 $call = $calls->me['array'][$i];
456 $result[$i] = $this->do_multicall($call);
457 }
458
459 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
460 }
461
462
463 //-------------------------------------
464 // Multi-call Function: Error Handling
465 //-------------------------------------
466
467 function multicall_error($err)
468 {
469 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
470 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
471
472 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
473 $struct['faultString'] = new XML_RPC_Values($str, 'string');
474
475 return new XML_RPC_Values($struct, 'struct');
476 }
477
478
479 //-------------------------------------
480 // Multi-call Function: Processes method
481 //-------------------------------------
482
483 function do_multicall($call)
484 {
485 if ($call->kindOf() != 'struct')
486 return $this->multicall_error('notstruct');
487 elseif (!$methName = $call->me['struct']['methodName'])
488 return $this->multicall_error('nomethod');
489
490 list($scalar_type,$scalar_value)=each($methName->me);
491 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
492
493 if ($methName->kindOf() != 'scalar' || $scalar_type != 'string')
494 return $this->multicall_error('notstring');
495 elseif ($scalar_value == 'system.multicall')
496 return $this->multicall_error('recursion');
497 elseif (!$params = $call->me['struct']['params'])
498 return $this->multicall_error('noparams');
499 elseif ($params->kindOf() != 'array')
500 return $this->multicall_error('notarray');
501
502 list($a,$b)=each($params->me);
503 $numParams = sizeof($b);
504
505 $msg = new XML_RPC_Message($scalar_value);
506 for ($i = 0; $i < $numParams; $i++)
507 {
508 $msg->params[] = $params->me['array'][$i];
509 }
510
511 $result = $this->execute($msg);
512
513 if ($result->faultCode() != 0)
514 {
515 return $this->multicall_error($result);
516 }
517
518 return new XML_RPC_Values(array($result->value()), 'array');
519 }
520
521}
522// END XML_RPC_Server class
523?>