Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?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.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com
|
| 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | if ( ! function_exists('xml_parser_create'))
|
| 17 | {
|
| 18 | show_error('Your PHP installation does not support XML');
|
| 19 | }
|
| 20 |
|
| 21 | if ( ! 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 | */
|
| 37 | class 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 Allard | 74c46a1 | 2007-04-30 01:39:39 +0000 | [diff] [blame] | 194 | $this->xmlrpcstr['invalid_return']);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 195 | }
|
| 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 | {
|
paulburdick | 6a8f97c | 2007-10-10 15:09:27 +0000 | [diff] [blame] | 205 | if ($this->debug === TRUE)
|
| 206 | {
|
| 207 | $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
|
| 208 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 209 |
|
| 210 | $m->addParam($parser_object->xh[$parser]['params'][$i]);
|
| 211 | }
|
| 212 |
|
| 213 | if ($this->debug === TRUE)
|
| 214 | {
|
| 215 | echo "<pre>";
|
| 216 | echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
|
| 217 | echo "</pre>";
|
| 218 | }
|
| 219 |
|
| 220 | $r = $this->_execute($m);
|
| 221 | }
|
| 222 |
|
| 223 | //-------------------------------------
|
| 224 | // SET DEBUGGING MESSAGE
|
| 225 | //-------------------------------------
|
| 226 |
|
| 227 | if ($this->debug === TRUE)
|
| 228 | {
|
| 229 | $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
|
| 230 | }
|
| 231 |
|
| 232 | return $r;
|
| 233 | }
|
| 234 |
|
| 235 | //-------------------------------------
|
| 236 | // Executes the Method
|
| 237 | //-------------------------------------
|
| 238 |
|
| 239 | function _execute($m)
|
| 240 | {
|
| 241 | $methName = $m->method_name;
|
| 242 |
|
| 243 | // Check to see if it is a system call
|
| 244 | // If so, load the system_methods
|
| 245 | $sysCall = ereg("^system\.", $methName);
|
| 246 | $methods = $sysCall ? $this->system_methods : $this->methods;
|
| 247 |
|
| 248 | //-------------------------------------
|
| 249 | // Check for Function
|
| 250 | //-------------------------------------
|
| 251 |
|
| 252 | if (!isset($methods[$methName]['function']))
|
| 253 | {
|
| 254 | return new XML_RPC_Response(0,
|
| 255 | $this->xmlrpcerr['unknown_method'],
|
| 256 | $this->xmlrpcstr['unknown_method']);
|
| 257 | }
|
| 258 | else
|
| 259 | {
|
| 260 | // See if we are calling function in an object
|
| 261 |
|
| 262 | $method_parts = explode(".",$methods[$methName]['function']);
|
| 263 | $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? true : false;
|
| 264 |
|
| 265 | if ($objectCall && !is_callable(array($method_parts['0'],$method_parts['1'])))
|
| 266 | {
|
| 267 | return new XML_RPC_Response(0,
|
| 268 | $this->xmlrpcerr['unknown_method'],
|
| 269 | $this->xmlrpcstr['unknown_method']);
|
| 270 | }
|
| 271 | elseif (!$objectCall && !is_callable($methods[$methName]['function']))
|
| 272 | {
|
| 273 | return new XML_RPC_Response(0,
|
| 274 | $this->xmlrpcerr['unknown_method'],
|
| 275 | $this->xmlrpcstr['unknown_method']);
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | //-------------------------------------
|
| 280 | // Checking Methods Signature
|
| 281 | //-------------------------------------
|
| 282 |
|
| 283 | if (isset($methods[$methName]['signature']))
|
| 284 | {
|
| 285 | $sig = $methods[$methName]['signature'];
|
| 286 | for($i=0; $i<sizeof($sig); $i++)
|
| 287 | {
|
| 288 | $current_sig = $sig[$i];
|
| 289 |
|
| 290 | if (sizeof($current_sig) == sizeof($m->params)+1)
|
| 291 | {
|
| 292 | for($n=0; $n < sizeof($m->params); $n++)
|
| 293 | {
|
| 294 | $p = $m->params[$n];
|
| 295 | $pt = ($p->kindOf() == 'scalar') ? $p->scalartyp() : $p->kindOf();
|
| 296 |
|
| 297 | if ($pt != $current_sig[$n+1])
|
| 298 | {
|
| 299 | $pno = $n+1;
|
| 300 | $wanted = $current_sig[$n+1];
|
| 301 |
|
| 302 | return new XML_RPC_Response(0,
|
| 303 | $this->xmlrpcerr['incorrect_params'],
|
| 304 | $this->xmlrpcstr['incorrect_params'] .
|
| 305 | ": Wanted {$wanted}, got {$pt} at param {$pno})");
|
| 306 | }
|
| 307 | }
|
| 308 | }
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | //-------------------------------------
|
| 313 | // Calls the Function
|
| 314 | //-------------------------------------
|
| 315 |
|
| 316 | if ($objectCall)
|
| 317 | {
|
| 318 | if ($method_parts['1'] == "this")
|
| 319 | {
|
| 320 | return call_user_func(array($this, $method_parts['0']), $m);
|
| 321 | }
|
| 322 | else
|
| 323 | {
|
| 324 | $CI =& get_instance();
|
| 325 | return $CI->$method_parts['1']($m);
|
| 326 | //$class = new $method_parts['0'];
|
| 327 | //return $class->$method_parts['1']($m);
|
| 328 | //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
|
| 329 | }
|
| 330 | }
|
| 331 | else
|
| 332 | {
|
| 333 | return call_user_func($methods[$methName]['function'], $m);
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 |
|
| 338 | //-------------------------------------
|
| 339 | // Server Function: List Methods
|
| 340 | //-------------------------------------
|
| 341 |
|
| 342 | function listMethods($m)
|
| 343 | {
|
| 344 | $v = new XML_RPC_Values();
|
| 345 | $output = array();
|
| 346 | foreach($this->$methods as $key => $value)
|
| 347 | {
|
| 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 | {
|
| 366 | $methName = $m->getParam(0);
|
| 367 | $method_name = $methName->scalarval();
|
| 368 |
|
| 369 | $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
|
| 370 |
|
| 371 | if (isset($methods[$method_name]))
|
| 372 | {
|
| 373 | if ($methods[$method_name]['signature'])
|
| 374 | {
|
| 375 | $sigs = array();
|
| 376 | $signature = $methods[$method_name]['signature'];
|
| 377 |
|
| 378 | for($i=0; $i < sizeof($signature); $i++)
|
| 379 | {
|
| 380 | $cursig = array();
|
| 381 | $inSig = $signature[$i];
|
| 382 | for($j=0; $j<sizeof($inSig); $j++)
|
| 383 | {
|
| 384 | $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
|
| 385 | }
|
| 386 | $sigs[]= new XML_RPC_Values($cursig, 'array');
|
| 387 | }
|
| 388 | $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
|
| 389 | }
|
| 390 | else
|
| 391 | {
|
| 392 | $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
|
| 393 | }
|
| 394 | }
|
| 395 | else
|
| 396 | {
|
| 397 | $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
|
| 398 | }
|
| 399 | return $r;
|
| 400 | }
|
| 401 |
|
| 402 | //-------------------------------------
|
| 403 | // Server Function: Doc String for Method
|
| 404 | //-------------------------------------
|
| 405 |
|
| 406 | function methodHelp($m)
|
| 407 | {
|
| 408 | $methName = $m->getParam(0);
|
| 409 | $method_name = $methName->scalarval();
|
| 410 |
|
| 411 | $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
|
| 412 |
|
| 413 | if (isset($methods[$methName]))
|
| 414 | {
|
| 415 | $docstring = isset($methods[$method_name]['docstring']) ? $methods[$method_name]['docstring'] : '';
|
| 416 | $r = new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
|
| 417 | }
|
| 418 | else
|
| 419 | {
|
| 420 | $r = new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
|
| 421 | }
|
| 422 | return $r;
|
| 423 | }
|
| 424 |
|
| 425 | //-------------------------------------
|
| 426 | // Server Function: Multi-call
|
| 427 | //-------------------------------------
|
| 428 |
|
| 429 | function multicall($m)
|
| 430 | {
|
| 431 | $calls = $m->getParam(0);
|
| 432 | list($a,$b)=each($calls->me);
|
| 433 | $result = array();
|
| 434 |
|
| 435 | for ($i = 0; $i < sizeof($b); $i++)
|
| 436 | {
|
| 437 | $call = $calls->me['array'][$i];
|
| 438 | $result[$i] = $this->do_multicall($call);
|
| 439 | }
|
| 440 |
|
| 441 | return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
|
| 442 | }
|
| 443 |
|
| 444 |
|
| 445 | //-------------------------------------
|
| 446 | // Multi-call Function: Error Handling
|
| 447 | //-------------------------------------
|
| 448 |
|
| 449 | function multicall_error($err)
|
| 450 | {
|
| 451 | $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
|
| 452 | $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
|
| 453 |
|
| 454 | $struct['faultCode'] = new XML_RPC_Values($code, 'int');
|
| 455 | $struct['faultString'] = new XML_RPC_Values($str, 'string');
|
| 456 |
|
| 457 | return new XML_RPC_Values($struct, 'struct');
|
| 458 | }
|
| 459 |
|
| 460 |
|
| 461 | //-------------------------------------
|
| 462 | // Multi-call Function: Processes method
|
| 463 | //-------------------------------------
|
| 464 |
|
| 465 | function do_multicall($call)
|
| 466 | {
|
| 467 | if ($call->kindOf() != 'struct')
|
| 468 | return $this->multicall_error('notstruct');
|
| 469 | elseif (!$methName = $call->me['struct']['methodName'])
|
| 470 | return $this->multicall_error('nomethod');
|
| 471 |
|
| 472 | list($scalar_type,$scalar_value)=each($methName->me);
|
| 473 | $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
|
| 474 |
|
| 475 | if ($methName->kindOf() != 'scalar' || $scalar_type != 'string')
|
| 476 | return $this->multicall_error('notstring');
|
| 477 | elseif ($scalar_value == 'system.multicall')
|
| 478 | return $this->multicall_error('recursion');
|
| 479 | elseif (!$params = $call->me['struct']['params'])
|
| 480 | return $this->multicall_error('noparams');
|
| 481 | elseif ($params->kindOf() != 'array')
|
| 482 | return $this->multicall_error('notarray');
|
| 483 |
|
| 484 | list($a,$b)=each($params->me);
|
| 485 | $numParams = sizeof($b);
|
| 486 |
|
| 487 | $msg = new XML_RPC_Message($scalar_value);
|
| 488 | for ($i = 0; $i < $numParams; $i++)
|
| 489 | {
|
| 490 | $msg->params[] = $params->me['array'][$i];
|
| 491 | }
|
| 492 |
|
| 493 | $result = $this->_execute($msg);
|
| 494 |
|
| 495 | if ($result->faultCode() != 0)
|
| 496 | {
|
| 497 | return $this->multicall_error($result);
|
| 498 | }
|
| 499 |
|
| 500 | return new XML_RPC_Values(array($result->value()), 'array');
|
| 501 | }
|
| 502 |
|
| 503 | }
|
| 504 | // END XML_RPC_Server class
|
| 505 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 506 | ?> |