admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?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 |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 8 | * @author Rick Ellis, Paul Burdick |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | */ |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 15 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 16 | if ( ! function_exists('xml_parser_create')) |
| 17 | { |
| 18 | show_error('Your PHP installation does not support XML'); |
| 19 | } |
| 20 | |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 21 | |
| 22 | // INITIALIZE THE CLASS --------------------------------------------------- |
| 23 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 24 | require_once(BASEPATH.'libraries/Xmlrpc'.EXT); |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 25 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 26 | // The initialization code is at the bottom of this file. It seems to |
| 27 | // cause an error to have it at the top |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 28 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 29 | // ------------------------------------------------------------------------ |
| 30 | |
| 31 | /** |
| 32 | * XML-RPC server class |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category XML-RPC |
| 37 | * @author Paul Burdick |
| 38 | * @link http://www.codeigniter.com/user_guide/libraries/xmlrpc.html |
| 39 | */ |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 40 | class CI_Xmlrpcs extends CI_Xmlrpc |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 41 | { |
| 42 | var $methods = array(); //array of methods mapped to function names and signatures |
| 43 | var $debug_msg = ''; // Debug Message |
| 44 | var $system_methods = array(); // XML RPC Server methods |
| 45 | var $controller_obj; |
| 46 | |
| 47 | |
| 48 | //------------------------------------- |
| 49 | // Constructor, more or less |
| 50 | //------------------------------------- |
| 51 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 52 | function CI_Xmlrpcs($config=array()) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 53 | { |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 54 | parent::CI_Xmlrpc(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 55 | $this->set_system_methods(); |
| 56 | |
| 57 | if (isset($config['functions']) && is_array($config['functions'])) |
| 58 | { |
| 59 | $this->methods = $config['functions']; |
| 60 | } |
| 61 | |
| 62 | log_message('debug', "XML-RPC Server Class Initialized"); |
| 63 | } |
| 64 | |
| 65 | //------------------------------------- |
| 66 | // Initialize Prefs and Serve |
| 67 | //------------------------------------- |
| 68 | |
| 69 | function initialize($config=array()) |
| 70 | { |
| 71 | if (isset($config['functions']) && is_array($config['functions'])) |
| 72 | { |
| 73 | $this->methods = $config['functions']; |
| 74 | } |
| 75 | |
| 76 | if (isset($config['debug'])) |
| 77 | { |
| 78 | $this->debug = $config['debug']; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | //------------------------------------- |
| 83 | // Setting of System Methods |
| 84 | //------------------------------------- |
| 85 | |
| 86 | function set_system_methods () |
| 87 | { |
| 88 | $system_methods = array( |
| 89 | 'system.listMethods' => array( |
| 90 | 'function' => 'this.listMethods', |
| 91 | 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)), |
| 92 | 'docstring' => 'Returns an array of available methods on this server'), |
| 93 | 'system.methodHelp' => array( |
| 94 | 'function' => 'this.methodHelp', |
| 95 | 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)), |
| 96 | 'docstring' => 'Returns a documentation string for the specified method'), |
| 97 | 'system.methodSignature' => array( |
| 98 | 'function' => 'this.methodSignature', |
| 99 | 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)), |
| 100 | 'docstring' => 'Returns an array describing the return type and required parameters of a method'), |
| 101 | 'system.multicall' => array( |
| 102 | 'function' => 'this.multicall', |
| 103 | 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)), |
| 104 | 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details') |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | //------------------------------------- |
| 110 | // Main Server Function |
| 111 | //------------------------------------- |
| 112 | |
| 113 | function serve() |
| 114 | { |
| 115 | $r = $this->parseRequest(); |
| 116 | $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n"; |
| 117 | $payload .= $this->debug_msg; |
| 118 | $payload .= $r->prepare_response(); |
| 119 | |
| 120 | header("Content-Type: text/xml"); |
| 121 | header("Content-Length: ".strlen($payload)); |
| 122 | echo $payload; |
| 123 | } |
| 124 | |
| 125 | //------------------------------------- |
| 126 | // Add Method to Class |
| 127 | //------------------------------------- |
| 128 | |
| 129 | function add_to_map($methodname,$function,$sig,$doc) |
| 130 | { |
| 131 | $this->methods[$methodname] = array( |
| 132 | 'function' => $function, |
| 133 | 'signature' => $sig, |
| 134 | 'docstring' => $doc |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | //------------------------------------- |
| 140 | // Parse Server Request |
| 141 | //------------------------------------- |
| 142 | |
| 143 | function parseRequest($data='') |
| 144 | { |
| 145 | global $HTTP_RAW_POST_DATA; |
| 146 | |
| 147 | //------------------------------------- |
| 148 | // Get Data |
| 149 | //------------------------------------- |
| 150 | |
| 151 | if ($data == '') |
| 152 | { |
| 153 | $data = $HTTP_RAW_POST_DATA; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | //------------------------------------- |
| 158 | // Set up XML Parser |
| 159 | //------------------------------------- |
| 160 | |
| 161 | $parser = xml_parser_create($this->xmlrpc_defencoding); |
| 162 | $parser_object = new XML_RPC_Message("filler"); |
| 163 | |
| 164 | $parser_object->xh[$parser] = array(); |
| 165 | $parser_object->xh[$parser]['isf'] = 0; |
| 166 | $parser_object->xh[$parser]['isf_reason'] = ''; |
| 167 | $parser_object->xh[$parser]['params'] = array(); |
| 168 | $parser_object->xh[$parser]['stack'] = array(); |
| 169 | $parser_object->xh[$parser]['valuestack'] = array(); |
| 170 | $parser_object->xh[$parser]['method'] = ''; |
| 171 | |
| 172 | xml_set_object($parser, $parser_object); |
| 173 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
| 174 | xml_set_element_handler($parser, 'open_tag', 'closing_tag'); |
| 175 | xml_set_character_data_handler($parser, 'character_data'); |
| 176 | //xml_set_default_handler($parser, 'default_handler'); |
| 177 | |
| 178 | |
| 179 | //------------------------------------- |
| 180 | // PARSE + PROCESS XML DATA |
| 181 | //------------------------------------- |
| 182 | |
| 183 | if ( ! xml_parse($parser, $data, 1)) |
| 184 | { |
| 185 | // return XML error as a faultCode |
| 186 | $r = new XML_RPC_Response(0, |
| 187 | $this->xmlrpcerrxml + xml_get_error_code($parser), |
| 188 | sprintf('XML error: %s at line %d', |
| 189 | xml_error_string(xml_get_error_code($parser)), |
| 190 | xml_get_current_line_number($parser))); |
| 191 | xml_parser_free($parser); |
| 192 | } |
| 193 | elseif($parser_object->xh[$parser]['isf']) |
| 194 | { |
| 195 | return new XML_RPC_Response(0, |
| 196 | $this->xmlrpcerr['invalid_return'], |
| 197 | $this->xmlrpcstr['invalid_retrun']); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | xml_parser_free($parser); |
| 202 | |
| 203 | $m = new XML_RPC_Message($parser_object->xh[$parser]['method']); |
| 204 | $plist=''; |
| 205 | |
| 206 | for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++) |
| 207 | { |
| 208 | $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n"; |
| 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 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 220 | $r = $this->_execute($m); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | |
admin | e885d78 | 2006-09-23 20:25:05 +0000 | [diff] [blame] | 239 | function _execute($m) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | $obj =& get_instance(); |
| 325 | return $obj->$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 | |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 493 | $result = $this->_execute($msg); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 505 | |
| 506 | |
| 507 | // INITIALIZE THE CLASS --------------------------------------------------- |
| 508 | |
| 509 | $obj =& get_instance(); |
| 510 | $obj->init_class('CI_Xmlrpc'); |
| 511 | $obj->init_class('CI_Xmlrpcs'); |
| 512 | |
| 513 | // ------------------------------------------------------------------------ |
| 514 | |
| 515 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 516 | ?> |