1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 if ( ! function_exists('xml_parser_create'))
18 show_error('Your PHP installation does not support XML');
21 if ( ! class_exists('CI_Xmlrpc'))
23 show_error('You must load the Xmlrpc class before loading the Xmlrpcs class in order to create a server.');
26 // ------------------------------------------------------------------------
29 * XML-RPC server class
31 * @package CodeIgniter
32 * @subpackage Libraries
34 * @author ExpressionEngine Dev Team
35 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
37 class CI_Xmlrpcs extends CI_Xmlrpc
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
49 public function __construct($config=array())
51 parent::__construct();
52 $this->set_system_methods();
54 if (isset($config['functions']) && is_array($config['functions']))
56 $this->methods = array_merge($this->methods, $config['functions']);
59 log_message('debug', "XML-RPC Server Class Initialized");
62 // --------------------------------------------------------------------
65 * Initialize Prefs and Serve
71 function initialize($config=array())
73 if (isset($config['functions']) && is_array($config['functions']))
75 $this->methods = array_merge($this->methods, $config['functions']);
78 if (isset($config['debug']))
80 $this->debug = $config['debug'];
83 if (isset($config['object']) && is_object($config['object']))
85 $this->object = $config['object'];
88 if (isset($config['xss_clean']))
90 $this->xss_clean = $config['xss_clean'];
94 // --------------------------------------------------------------------
97 * Setting of System Methods
102 function set_system_methods()
104 $this->methods = array(
105 'system.listMethods' => array(
106 'function' => 'this.listMethods',
107 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
108 'docstring' => 'Returns an array of available methods on this server'),
109 'system.methodHelp' => array(
110 'function' => 'this.methodHelp',
111 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
112 'docstring' => 'Returns a documentation string for the specified method'),
113 'system.methodSignature' => array(
114 'function' => 'this.methodSignature',
115 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
116 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
117 'system.multicall' => array(
118 'function' => 'this.multicall',
119 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
120 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
124 // --------------------------------------------------------------------
127 * Main Server Function
134 $r = $this->parseRequest();
135 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n";
136 $payload .= $this->debug_msg;
137 $payload .= $r->prepare_response();
139 header("Content-Type: text/xml");
140 header("Content-Length: ".strlen($payload));
144 // --------------------------------------------------------------------
147 * Add Method to Class
150 * @param string method name
151 * @param string function
152 * @param string signature
153 * @param string docstring
156 function add_to_map($methodname, $function, $sig, $doc)
158 $this->methods[$methodname] = array(
159 'function' => $function,
165 // --------------------------------------------------------------------
168 * Parse Server Request
172 * @return object xmlrpc response
174 function parseRequest($data='')
176 global $HTTP_RAW_POST_DATA;
178 //-------------------------------------
180 //-------------------------------------
184 $data = $HTTP_RAW_POST_DATA;
187 //-------------------------------------
189 //-------------------------------------
191 $parser = xml_parser_create($this->xmlrpc_defencoding);
192 $parser_object = new XML_RPC_Message("filler");
194 $parser_object->xh[$parser] = array();
195 $parser_object->xh[$parser]['isf'] = 0;
196 $parser_object->xh[$parser]['isf_reason'] = '';
197 $parser_object->xh[$parser]['params'] = array();
198 $parser_object->xh[$parser]['stack'] = array();
199 $parser_object->xh[$parser]['valuestack'] = array();
200 $parser_object->xh[$parser]['method'] = '';
202 xml_set_object($parser, $parser_object);
203 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
204 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
205 xml_set_character_data_handler($parser, 'character_data');
206 //xml_set_default_handler($parser, 'default_handler');
209 //-------------------------------------
210 // PARSE + PROCESS XML DATA
211 //-------------------------------------
213 if ( ! xml_parse($parser, $data, 1))
215 // return XML error as a faultCode
216 $r = new XML_RPC_Response(0,
217 $this->xmlrpcerrxml + xml_get_error_code($parser),
218 sprintf('XML error: %s at line %d',
219 xml_error_string(xml_get_error_code($parser)),
220 xml_get_current_line_number($parser)));
221 xml_parser_free($parser);
223 elseif ($parser_object->xh[$parser]['isf'])
225 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
229 xml_parser_free($parser);
231 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
234 for ($i=0; $i < count($parser_object->xh[$parser]['params']); $i++)
236 if ($this->debug === TRUE)
238 $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
241 $m->addParam($parser_object->xh[$parser]['params'][$i]);
244 if ($this->debug === TRUE)
247 echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
251 $r = $this->_execute($m);
254 //-------------------------------------
255 // SET DEBUGGING MESSAGE
256 //-------------------------------------
258 if ($this->debug === TRUE)
260 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
266 // --------------------------------------------------------------------
269 * Executes the Method
275 function _execute($m)
277 $methName = $m->method_name;
279 // Check to see if it is a system call
280 $system_call = (strncmp($methName, 'system', 5) == 0) ? TRUE : FALSE;
282 if ($this->xss_clean == FALSE)
284 $m->xss_clean = FALSE;
287 //-------------------------------------
289 //-------------------------------------
291 if ( ! isset($this->methods[$methName]['function']))
293 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
296 //-------------------------------------
297 // Check for Method (and Object)
298 //-------------------------------------
300 $method_parts = explode(".", $this->methods[$methName]['function']);
301 $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? TRUE : FALSE;
303 if ($system_call === TRUE)
305 if ( ! is_callable(array($this,$method_parts['1'])))
307 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
312 if ($objectCall && ! is_callable(array($method_parts['0'],$method_parts['1'])))
314 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
316 elseif ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
318 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
322 //-------------------------------------
323 // Checking Methods Signature
324 //-------------------------------------
326 if (isset($this->methods[$methName]['signature']))
328 $sig = $this->methods[$methName]['signature'];
329 for ($i=0; $i<count($sig); $i++)
331 $current_sig = $sig[$i];
333 if (count($current_sig) == count($m->params)+1)
335 for ($n=0; $n < count($m->params); $n++)
338 $pt = ($p->kindOf() == 'scalar') ? $p->scalarval() : $p->kindOf();
340 if ($pt != $current_sig[$n+1])
343 $wanted = $current_sig[$n+1];
345 return new XML_RPC_Response(0,
346 $this->xmlrpcerr['incorrect_params'],
347 $this->xmlrpcstr['incorrect_params'] .
348 ": Wanted {$wanted}, got {$pt} at param {$pno})");
355 //-------------------------------------
356 // Calls the Function
357 //-------------------------------------
359 if ($objectCall === TRUE)
361 if ($method_parts[0] == "this" && $system_call == TRUE)
363 return call_user_func(array($this, $method_parts[1]), $m);
367 if ($this->object === FALSE)
369 $CI =& get_instance();
370 return $CI->$method_parts['1']($m);
374 return $this->object->$method_parts['1']($m);
375 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
381 return call_user_func($this->methods[$methName]['function'], $m);
385 // --------------------------------------------------------------------
388 * Server Function: List Methods
394 function listMethods($m)
396 $v = new XML_RPC_Values();
399 foreach ($this->methods as $key => $value)
401 $output[] = new XML_RPC_Values($key, 'string');
404 foreach ($this->system_methods as $key => $value)
406 $output[]= new XML_RPC_Values($key, 'string');
409 $v->addArray($output);
410 return new XML_RPC_Response($v);
413 // --------------------------------------------------------------------
416 * Server Function: Return Signature for Method
422 function methodSignature($m)
424 $parameters = $m->output_parameters();
425 $method_name = $parameters[0];
427 if (isset($this->methods[$method_name]))
429 if ($this->methods[$method_name]['signature'])
432 $signature = $this->methods[$method_name]['signature'];
434 for ($i=0; $i < count($signature); $i++)
437 $inSig = $signature[$i];
438 for ($j=0; $j<count($inSig); $j++)
440 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
442 $sigs[]= new XML_RPC_Values($cursig, 'array');
444 $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
448 $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
453 $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
458 // --------------------------------------------------------------------
461 * Server Function: Doc String for Method
467 function methodHelp($m)
469 $parameters = $m->output_parameters();
470 $method_name = $parameters[0];
472 if (isset($this->methods[$method_name]))
474 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
476 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
480 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
484 // --------------------------------------------------------------------
487 * Server Function: Multi-call
493 function multicall($m)
496 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
498 $parameters = $m->output_parameters();
499 $calls = $parameters[0];
503 foreach ($calls as $value)
505 //$attempt = $this->_execute(new XML_RPC_Message($value[0], $value[1]));
507 $m = new XML_RPC_Message($value[0]);
510 for ($i=0; $i < count($value[1]); $i++)
512 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
515 $attempt = $this->_execute($m);
517 if ($attempt->faultCode() != 0)
522 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
525 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
528 // --------------------------------------------------------------------
531 * Multi-call Function: Error Handling
537 function multicall_error($err)
539 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
540 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
542 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
543 $struct['faultString'] = new XML_RPC_Values($str, 'string');
545 return new XML_RPC_Values($struct, 'struct');
548 // --------------------------------------------------------------------
551 * Multi-call Function: Processes method
557 function do_multicall($call)
559 if ($call->kindOf() != 'struct')
561 return $this->multicall_error('notstruct');
563 elseif ( ! $methName = $call->me['struct']['methodName'])
565 return $this->multicall_error('nomethod');
568 list($scalar_type,$scalar_value)=each($methName->me);
569 $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
571 if ($methName->kindOf() != 'scalar' OR $scalar_type != 'string')
573 return $this->multicall_error('notstring');
575 elseif ($scalar_value == 'system.multicall')
577 return $this->multicall_error('recursion');
579 elseif ( ! $params = $call->me['struct']['params'])
581 return $this->multicall_error('noparams');
583 elseif ($params->kindOf() != 'array')
585 return $this->multicall_error('notarray');
588 list($a,$b)=each($params->me);
589 $numParams = count($b);
591 $msg = new XML_RPC_Message($scalar_value);
592 for ($i = 0; $i < $numParams; $i++)
594 $msg->params[] = $params->me['array'][$i];
597 $result = $this->_execute($msg);
599 if ($result->faultCode() != 0)
601 return $this->multicall_error($result);
604 return new XML_RPC_Values(array($result->value()), 'array');
608 // END XML_RPC_Server class
611 /* End of file Xmlrpcs.php */
612 /* Location: ./system/libraries/Xmlrpcs.php */