| [ Index ] |
PHP Cross Reference of Nucleus CMS 3.32 |
[Summary view] [Print] [Text view]
1 <?php 2 // by Edd Dumbill (C) 1999-2002 3 // <edd@usefulinc.com> 4 // $Original: xmlrpcs.inc,v 1.66 2006/09/17 21:25:06 ggiunta Exp $ 5 // $Id: xmlrpcs.inc.php 1199 2007-09-07 07:03:41Z kimitake $ 6 7 // Copyright (c) 1999,2000,2002 Edd Dumbill. 8 // All rights reserved. 9 // 10 // Redistribution and use in source and binary forms, with or without 11 // modification, are permitted provided that the following conditions 12 // are met: 13 // 14 // * Redistributions of source code must retain the above copyright 15 // notice, this list of conditions and the following disclaimer. 16 // 17 // * Redistributions in binary form must reproduce the above 18 // copyright notice, this list of conditions and the following 19 // disclaimer in the documentation and/or other materials provided 20 // with the distribution. 21 // 22 // * Neither the name of the "XML-RPC for PHP" nor the names of its 23 // contributors may be used to endorse or promote products derived 24 // from this software without specific prior written permission. 25 // 26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 29 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 30 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 31 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 32 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 33 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 35 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 37 // OF THE POSSIBILITY OF SUCH DAMAGE. 38 39 // XML RPC Server class 40 // requires: xmlrpc.inc 41 42 $GLOBALS['xmlrpcs_capabilities'] = array( 43 // xmlrpc spec: always supported 44 'xmlrpc' => new xmlrpcval(array( 45 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/spec', 'string'), 46 'specVersion' => new xmlrpcval(1, 'int') 47 ), 'struct'), 48 // if we support system.xxx functions, we always support multicall, too... 49 // Note that, as of 2006/09/17, the following URL does not respond anymore 50 'system.multicall' => new xmlrpcval(array( 51 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/discuss/msgReader$1208', 'string'), 52 'specVersion' => new xmlrpcval(1, 'int') 53 ), 'struct'), 54 // introspection: version 2! we support 'mixed', too 55 'introspection' => new xmlrpcval(array( 56 'specUrl' => new xmlrpcval('http://phpxmlrpc.sourceforge.net/doc-2/ch10.html', 'string'), 57 'specVersion' => new xmlrpcval(2, 'int') 58 ), 'struct') 59 ); 60 61 /* Functions that implement system.XXX methods of xmlrpc servers */ 62 $_xmlrpcs_getCapabilities_sig=array(array($GLOBALS['xmlrpcStruct'])); 63 $_xmlrpcs_getCapabilities_doc='This method lists all the capabilites that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to'; 64 $_xmlrpcs_getCapabilities_sdoc=array(array('list of capabilities, described as structs with a version number and url for the spec')); 65 function _xmlrpcs_getCapabilities($server, $m=null) 66 { 67 $outAr = $GLOBALS['xmlrpcs_capabilities']; 68 // NIL extension 69 if ($GLOBALS['xmlrpc_null_extension']) { 70 $outAr['nil'] = new xmlrpcval(array( 71 'specUrl' => new xmlrpcval('http://www.ontosys.com/xml-rpc/extensions.php', 'string'), 72 'specVersion' => new xmlrpcval(1, 'int') 73 ), 'struct'); 74 } 75 return new xmlrpcresp(new xmlrpcval($outAr, 'struct')); 76 } 77 78 // listMethods: signature was either a string, or nothing. 79 // The useless string variant has been removed 80 $_xmlrpcs_listMethods_sig=array(array($GLOBALS['xmlrpcArray'])); 81 $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch'; 82 $_xmlrpcs_listMethods_sdoc=array(array('list of method names')); 83 function _xmlrpcs_listMethods($server, $m=null) // if called in plain php values mode, second param is missing 84 { 85 86 $outAr=array(); 87 foreach($server->dmap as $key => $val) 88 { 89 $outAr[]=&new xmlrpcval($key, 'string'); 90 } 91 if($server->allow_system_funcs) 92 { 93 foreach($GLOBALS['_xmlrpcs_dmap'] as $key => $val) 94 { 95 $outAr[]=&new xmlrpcval($key, 'string'); 96 } 97 } 98 return new xmlrpcresp(new xmlrpcval($outAr, 'array')); 99 } 100 101 $_xmlrpcs_methodSignature_sig=array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcString'])); 102 $_xmlrpcs_methodSignature_doc='Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)'; 103 $_xmlrpcs_methodSignature_sdoc=array(array('list of known signatures, each sig being an array of xmlrpc type names', 'name of method to be described')); 104 function _xmlrpcs_methodSignature($server, $m) 105 { 106 // let accept as parameter both an xmlrpcval or string 107 if (is_object($m)) 108 { 109 $methName=$m->getParam(0); 110 $methName=$methName->scalarval(); 111 } 112 else 113 { 114 $methName=$m; 115 } 116 if(strpos($methName, "system.") === 0) 117 { 118 $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; 119 } 120 else 121 { 122 $dmap=$server->dmap; $sysCall=0; 123 } 124 if(isset($dmap[$methName])) 125 { 126 if(isset($dmap[$methName]['signature'])) 127 { 128 $sigs=array(); 129 foreach($dmap[$methName]['signature'] as $inSig) 130 { 131 $cursig=array(); 132 foreach($inSig as $sig) 133 { 134 $cursig[]=&new xmlrpcval($sig, 'string'); 135 } 136 $sigs[]=&new xmlrpcval($cursig, 'array'); 137 } 138 $r=&new xmlrpcresp(new xmlrpcval($sigs, 'array')); 139 } 140 else 141 { 142 // NB: according to the official docs, we should be returning a 143 // "none-array" here, which means not-an-array 144 $r=&new xmlrpcresp(new xmlrpcval('undef', 'string')); 145 } 146 } 147 else 148 { 149 $r=&new xmlrpcresp(0,$GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); 150 } 151 return $r; 152 } 153 154 $_xmlrpcs_methodHelp_sig=array(array($GLOBALS['xmlrpcString'], $GLOBALS['xmlrpcString'])); 155 $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string'; 156 $_xmlrpcs_methodHelp_sdoc=array(array('method description', 'name of the method to be described')); 157 function _xmlrpcs_methodHelp($server, $m) 158 { 159 // let accept as parameter both an xmlrpcval or string 160 if (is_object($m)) 161 { 162 $methName=$m->getParam(0); 163 $methName=$methName->scalarval(); 164 } 165 else 166 { 167 $methName=$m; 168 } 169 if(strpos($methName, "system.") === 0) 170 { 171 $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; 172 } 173 else 174 { 175 $dmap=$server->dmap; $sysCall=0; 176 } 177 if(isset($dmap[$methName])) 178 { 179 if(isset($dmap[$methName]['docstring'])) 180 { 181 $r=&new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string'); 182 } 183 else 184 { 185 $r=&new xmlrpcresp(new xmlrpcval('', 'string')); 186 } 187 } 188 else 189 { 190 $r=&new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); 191 } 192 return $r; 193 } 194 195 $_xmlrpcs_multicall_sig = array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcArray'])); 196 $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details'; 197 $_xmlrpcs_multicall_sdoc = array(array('list of response structs, where each struct has the usual members', 'list of calls, with each call being represented as a struct, with members "methodname" and "params"')); 198 function _xmlrpcs_multicall_error($err) 199 { 200 if(is_string($err)) 201 { 202 $str = $GLOBALS['xmlrpcstr']["multicall_$err}"]; 203 $code = $GLOBALS['xmlrpcerr']["multicall_$err}"]; 204 } 205 else 206 { 207 $code = $err->faultCode(); 208 $str = $err->faultString(); 209 } 210 $struct = array(); 211 $struct['faultCode'] =& new xmlrpcval($code, 'int'); 212 $struct['faultString'] =& new xmlrpcval($str, 'string'); 213 return new xmlrpcval($struct, 'struct'); 214 } 215 216 function _xmlrpcs_multicall_do_call($server, $call) 217 { 218 if($call->kindOf() != 'struct') 219 { 220 return _xmlrpcs_multicall_error('notstruct'); 221 } 222 $methName = @$call->structmem('methodName'); 223 if(!$methName) 224 { 225 return _xmlrpcs_multicall_error('nomethod'); 226 } 227 if($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string') 228 { 229 return _xmlrpcs_multicall_error('notstring'); 230 } 231 if($methName->scalarval() == 'system.multicall') 232 { 233 return _xmlrpcs_multicall_error('recursion'); 234 } 235 236 $params = @$call->structmem('params'); 237 if(!$params) 238 { 239 return _xmlrpcs_multicall_error('noparams'); 240 } 241 if($params->kindOf() != 'array') 242 { 243 return _xmlrpcs_multicall_error('notarray'); 244 } 245 $numParams = $params->arraysize(); 246 247 $msg =& new xmlrpcmsg($methName->scalarval()); 248 for($i = 0; $i < $numParams; $i++) 249 { 250 if(!$msg->addParam($params->arraymem($i))) 251 { 252 $i++; 253 return _xmlrpcs_multicall_error(new xmlrpcresp(0, 254 $GLOBALS['xmlrpcerr']['incorrect_params'], 255 $GLOBALS['xmlrpcstr']['incorrect_params'] . ": probable xml error in param " . $i)); 256 } 257 } 258 259 $result = $server->execute($msg); 260 261 if($result->faultCode() != 0) 262 { 263 return _xmlrpcs_multicall_error($result); // Method returned fault. 264 } 265 266 return new xmlrpcval(array($result->value()), 'array'); 267 } 268 269 function _xmlrpcs_multicall_do_call_phpvals($server, $call) 270 { 271 if(!is_array($call)) 272 { 273 return _xmlrpcs_multicall_error('notstruct'); 274 } 275 if(!array_key_exists('methodName', $call)) 276 { 277 return _xmlrpcs_multicall_error('nomethod'); 278 } 279 if (!is_string($call['methodName'])) 280 { 281 return _xmlrpcs_multicall_error('notstring'); 282 } 283 if($call['methodName'] == 'system.multicall') 284 { 285 return _xmlrpcs_multicall_error('recursion'); 286 } 287 if(!array_key_exists('params', $call)) 288 { 289 return _xmlrpcs_multicall_error('noparams'); 290 } 291 if(!is_array($call['params'])) 292 { 293 return _xmlrpcs_multicall_error('notarray'); 294 } 295 296 // this is a real dirty and simplistic hack, since we might have received a 297 // base64 or datetime values, but they will be listed as strings here... 298 $numParams = count($call['params']); 299 $pt = array(); 300 foreach($call['params'] as $val) 301 $pt[] = php_2_xmlrpc_type(gettype($val)); 302 303 $result = $server->execute($call['methodName'], $call['params'], $pt); 304 305 if($result->faultCode() != 0) 306 { 307 return _xmlrpcs_multicall_error($result); // Method returned fault. 308 } 309 310 return new xmlrpcval(array($result->value()), 'array'); 311 } 312 313 function _xmlrpcs_multicall($server, $m) 314 { 315 $result = array(); 316 // let accept a plain list of php parameters, beside a single xmlrpc msg object 317 if (is_object($m)) 318 { 319 $calls = $m->getParam(0); 320 $numCalls = $calls->arraysize(); 321 for($i = 0; $i < $numCalls; $i++) 322 { 323 $call = $calls->arraymem($i); 324 $result[$i] = _xmlrpcs_multicall_do_call($server, $call); 325 } 326 } 327 else 328 { 329 $numCalls=count($m); 330 for($i = 0; $i < $numCalls; $i++) 331 { 332 $result[$i] = _xmlrpcs_multicall_do_call_phpvals($server, $m[$i]); 333 } 334 } 335 336 return new xmlrpcresp(new xmlrpcval($result, 'array')); 337 } 338 339 $GLOBALS['_xmlrpcs_dmap']=array( 340 'system.listMethods' => array( 341 'function' => '_xmlrpcs_listMethods', 342 'signature' => $_xmlrpcs_listMethods_sig, 343 'docstring' => $_xmlrpcs_listMethods_doc, 344 'signature_docs' => $_xmlrpcs_listMethods_sdoc), 345 'system.methodHelp' => array( 346 'function' => '_xmlrpcs_methodHelp', 347 'signature' => $_xmlrpcs_methodHelp_sig, 348 'docstring' => $_xmlrpcs_methodHelp_doc, 349 'signature_docs' => $_xmlrpcs_methodHelp_sdoc), 350 'system.methodSignature' => array( 351 'function' => '_xmlrpcs_methodSignature', 352 'signature' => $_xmlrpcs_methodSignature_sig, 353 'docstring' => $_xmlrpcs_methodSignature_doc, 354 'signature_docs' => $_xmlrpcs_methodSignature_sdoc), 355 'system.multicall' => array( 356 'function' => '_xmlrpcs_multicall', 357 'signature' => $_xmlrpcs_multicall_sig, 358 'docstring' => $_xmlrpcs_multicall_doc, 359 'signature_docs' => $_xmlrpcs_multicall_sdoc), 360 'system.getCapabilities' => array( 361 'function' => '_xmlrpcs_getCapabilities', 362 'signature' => $_xmlrpcs_getCapabilities_sig, 363 'docstring' => $_xmlrpcs_getCapabilities_doc, 364 'signature_docs' => $_xmlrpcs_getCapabilities_sdoc) 365 ); 366 367 $GLOBALS['_xmlrpcs_occurred_errors'] = ''; 368 $GLOBALS['_xmlrpcs_prev_ehandler'] = ''; 369 /** 370 * Error handler used to track errors that occur during server-side execution of PHP code. 371 * This allows to report back to the client whether an internal error has occurred or not 372 * using an xmlrpc response object, instead of letting the client deal with the html junk 373 * that a PHP execution error on the server generally entails. 374 * 375 * NB: in fact a user defined error handler can only handle WARNING, NOTICE and USER_* errors. 376 * 377 */ 378 function _xmlrpcs_errorHandler($errcode, $errstring, $filename=null, $lineno=null, $context=null) 379 { 380 // obey the @ protocol 381 if (error_reporting() == 0) 382 return; 383 384 //if($errcode != E_NOTICE && $errcode != E_WARNING && $errcode != E_USER_NOTICE && $errcode != E_USER_WARNING) 385 if($errcode != 2048) // do not use E_STRICT by name, since on PHP 4 it will not be defined 386 { 387 $GLOBALS['_xmlrpcs_occurred_errors'] = $GLOBALS['_xmlrpcs_occurred_errors'] . $errstring . "\n"; 388 } 389 // Try to avoid as much as possible disruption to the previous error handling 390 // mechanism in place 391 if($GLOBALS['_xmlrpcs_prev_ehandler'] == '') 392 { 393 // The previous error handler was the default: all we should do is log error 394 // to the default error log (if level high enough) 395 if(ini_get('log_errors') && (intval(ini_get('error_reporting')) & $errcode)) 396 { 397 error_log($errstring); 398 } 399 } 400 else 401 { 402 // Pass control on to previous error handler, trying to avoid loops... 403 if($GLOBALS['_xmlrpcs_prev_ehandler'] != '_xmlrpcs_errorHandler') 404 { 405 // NB: this code will NOT work on php < 4.0.2: only 2 params were used for error handlers 406 if(is_array($GLOBALS['_xmlrpcs_prev_ehandler'])) 407 { 408 $GLOBALS['_xmlrpcs_prev_ehandler'][0]->$GLOBALS['_xmlrpcs_prev_ehandler'][1]($errcode, $errstring, $filename, $lineno, $context); 409 } 410 else 411 { 412 $GLOBALS['_xmlrpcs_prev_ehandler']($errcode, $errstring, $filename, $lineno, $context); 413 } 414 } 415 } 416 } 417 418 $GLOBALS['_xmlrpc_debuginfo']=''; 419 420 /** 421 * Add a string to the debug info that can be later seralized by the server 422 * as part of the response message. 423 * Note that for best compatbility, the debug string should be encoded using 424 * the $GLOBALS['xmlrpc_internalencoding'] character set. 425 * @param string $m 426 * @access public 427 */ 428 function xmlrpc_debugmsg($m) 429 { 430 $GLOBALS['_xmlrpc_debuginfo'] .= $m . "\n"; 431 } 432 433 class xmlrpc_server 434 { 435 /// array defining php functions exposed as xmlrpc methods by this server 436 var $dmap=array(); 437 /** 438 * Defines how functions in dmap will be invokde: either using an xmlrpc msg object 439 * or plain php values. 440 * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals' 441 */ 442 var $functions_parameters_type='xmlrpcvals'; 443 /// controls wether the server is going to echo debugging messages back to the client as comments in response body. valid values: 0,1,2,3 444 var $debug = 1; 445 /** 446 * When set to true, it will enable HTTP compression of the response, in case 447 * the client has declared its support for compression in the request. 448 */ 449 var $compress_response = false; 450 /** 451 * List of http compression methods accepted by the server for requests. 452 * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib 453 */ 454 var $accepted_compression = array(); 455 /// shall we serve calls to system.* methods? 456 var $allow_system_funcs = true; 457 /// list of charset encodings natively accepted for requests 458 var $accepted_charset_encodings = array(); 459 /** 460 * charset encoding to be used for response. 461 * NB: if we can, we will convert the generated response from internal_encoding to the intended one. 462 * can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled), 463 * null (leave unspecified in response, convert output stream to US_ASCII), 464 * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed), 465 * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway). 466 * NB: pretty dangerous if you accept every charset and do not have mbstring enabled) 467 */ 468 var $response_charset_encoding = ''; 469 /// storage for internal debug info 470 var $debug_info = ''; 471 /// extra data passed at runtime to method handling functions. Used only by EPI layer 472 var $user_data = null; 473 474 /** 475 * @param array $dispmap the dispatch map withd efinition of exposed services 476 * @param boolean $servicenow set to false to prevent the server from runnung upon construction 477 */ 478 function xmlrpc_server($dispMap=null, $serviceNow=true) 479 { 480 // if ZLIB is enabled, let the server by default accept compressed requests, 481 // and compress responses sent to clients that support them 482 if(function_exists('gzinflate')) 483 { 484 $this->accepted_compression = array('gzip', 'deflate'); 485 $this->compress_response = true; 486 } 487 488 // by default the xml parser can support these 3 charset encodings 489 $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); 490 491 // dispMap is a dispatch array of methods 492 // mapped to function names and signatures 493 // if a method 494 // doesn't appear in the map then an unknown 495 // method error is generated 496 /* milosch - changed to make passing dispMap optional. 497 * instead, you can use the class add_to_map() function 498 * to add functions manually (borrowed from SOAPX4) 499 */ 500 if($dispMap) 501 { 502 $this->dmap = $dispMap; 503 if($serviceNow) 504 { 505 $this->service(); 506 } 507 } 508 } 509 510 /** 511 * Set debug level of server. 512 * @param integer $in debug lvl: determines info added to xmlrpc responses (as xml comments) 513 * 0 = no debug info, 514 * 1 = msgs set from user with debugmsg(), 515 * 2 = add complete xmlrpc request (headers and body), 516 * 3 = add also all processing warnings happened during method processing 517 * (NB: this involves setting a custom error handler, and might interfere 518 * with the standard processing of the php function exposed as method. In 519 * particular, triggering an USER_ERROR level error will not halt script 520 * execution anymore, but just end up logged in the xmlrpc response) 521 * Note that info added at elevel 2 and 3 will be base64 encoded 522 * @access public 523 */ 524 function setDebug($in) 525 { 526 $this->debug=$in; 527 } 528 529 /** 530 * Return a string with the serialized representation of all debug info 531 * @param string $charset_encoding the target charset encoding for the serialization 532 * @return string an XML comment (or two) 533 */ 534 function serializeDebug($charset_encoding='') 535 { 536 // Tough encoding problem: which internal charset should we assume for debug info? 537 // It might contain a copy of raw data received from client, ie with unknown encoding, 538 // intermixed with php generated data and user generated data... 539 // so we split it: system debug is base 64 encoded, 540 // user debug info should be encoded by the end user using the INTERNAL_ENCODING 541 $out = ''; 542 if ($this->debug_info != '') 543 { 544 $out .= "<!-- SERVER DEBUG INFO (BASE64 ENCODED):\n".base64_encode($this->debug_info)."\n-->\n"; 545 } 546 if($GLOBALS['_xmlrpc_debuginfo']!='') 547 { 548 549 $out .= "<!-- DEBUG INFO:\n" . xmlrpc_encode_entitites(str_replace('--', '_-', $GLOBALS['_xmlrpc_debuginfo']), $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "\n-->\n"; 550 // NB: a better solution MIGHT be to use CDATA, but we need to insert it 551 // into return payload AFTER the beginning tag 552 //$out .= "<![CDATA[ DEBUG INFO:\n\n" . str_replace(']]>', ']_]_>', $GLOBALS['_xmlrpc_debuginfo']) . "\n]]>\n"; 553 } 554 return $out; 555 } 556 557 /** 558 * Execute the xmlrpc request, printing the response 559 * @param string $data the request body. If null, the http POST request will be examined 560 * @return xmlrpcresp the response object (usually not used by caller...) 561 * @access public 562 */ 563 function service($data=null, $return_payload=false) 564 { 565 if ($data === null) 566 { 567 $data = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; 568 } 569 $raw_data = $data; 570 571 // reset internal debug info 572 $this->