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