2 // by Edd Dumbill (C) 1999-2002
6 // Copyright (c) 1999,2000,2002 Edd Dumbill.
7 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following
18 // disclaimer in the documentation and/or other materials provided
19 // with the distribution.
21 // * Neither the name of the "XML-RPC for PHP" nor the names of its
22 // contributors may be used to endorse or promote products derived
23 // from this software without specific prior written permission.
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 // OF THE POSSIBILITY OF SUCH DAMAGE.
38 // XML RPC Server class
39 // requires: xmlrpc.inc
41 // listMethods: either a string, or nothing
42 $_xmlrpcs_listMethods_sig=array(array($xmlrpcArray, $xmlrpcString), array($xmlrpcArray));
43 $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';
44 function _xmlrpcs_listMethods($server, $m)
46 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
50 for(reset($dmap); list($key, $val)=each($dmap); )
52 $outAr[]=new xmlrpcval($key, 'string');
55 for(reset($dmap); list($key, $val)=each($dmap); )
57 $outAr[]=new xmlrpcval($key, 'string');
60 return new xmlrpcresp($v);
63 $_xmlrpcs_methodSignature_sig=array(array($xmlrpcArray, $xmlrpcString));
64 $_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)';
65 function _xmlrpcs_methodSignature($server, $m)
67 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
69 $methName=$m->getParam(0);
70 $methName=$methName->scalarval();
71 if (ereg("^system\.", $methName))
73 $dmap=$_xmlrpcs_dmap; $sysCall=1;
77 $dmap=$server->dmap; $sysCall=0;
79 //echo "<!-- ${methName} -->\n";
80 if (isset($dmap[$methName]))
82 if ($dmap[$methName]['signature'])
85 $thesigs=$dmap[$methName]['signature'];
86 for($i=0; $i<sizeof($thesigs); $i++)
90 for($j=0; $j<sizeof($inSig); $j++)
92 $cursig[]=new xmlrpcval($inSig[$j], 'string');
94 $sigs[]=new xmlrpcval($cursig, 'array');
96 $r=new xmlrpcresp(new xmlrpcval($sigs, 'array'));
100 $r=new xmlrpcresp(new xmlrpcval('undef', 'string'));
105 $r=new xmlrpcresp(0,$xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
110 $_xmlrpcs_methodHelp_sig=array(array($xmlrpcString, $xmlrpcString));
111 $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';
112 function _xmlrpcs_methodHelp($server, $m)
114 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
116 $methName=$m->getParam(0);
117 $methName=$methName->scalarval();
118 if (ereg("^system\.", $methName))
120 $dmap=$_xmlrpcs_dmap; $sysCall=1;
124 $dmap=$server->dmap; $sysCall=0;
126 // print "<!-- ${methName} -->\n";
127 if (isset($dmap[$methName]))
129 if ($dmap[$methName]['docstring'])
131 $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string');
135 $r=new xmlrpcresp(new xmlrpcval('', 'string'));
140 $r=new xmlrpcresp(0, $xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
145 $_xmlrpcs_multicall_sig = array(array($xmlrpcArray, $xmlrpcArray));
146 $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details';
148 function _xmlrpcs_multicall_error($err)
152 global $xmlrpcerr, $xmlrpcstr;
153 $str = $xmlrpcstr["multicall_${err}"];
154 $code = $xmlrpcerr["multicall_${err}"];
158 $code = $err->faultCode();
159 $str = $err->faultString();
161 $struct['faultCode'] = new xmlrpcval($code, 'int');
162 $struct['faultString'] = new xmlrpcval($str, 'string');
163 return new xmlrpcval($struct, 'struct');
166 function _xmlrpcs_multicall_do_call($server, $call)
168 if ($call->kindOf() != 'struct')
170 return _xmlrpcs_multicall_error('notstruct');
172 $methName = $call->structmem('methodName');
175 return _xmlrpcs_multicall_error('nomethod');
177 if ($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string')
179 return _xmlrpcs_multicall_error('notstring');
181 if ($methName->scalarval() == 'system.multicall')
183 return _xmlrpcs_multicall_error('recursion');
186 $params = $call->structmem('params');
189 return _xmlrpcs_multicall_error('noparams');
191 if ($params->kindOf() != 'array')
193 return _xmlrpcs_multicall_error('notarray');
195 $numParams = $params->arraysize();
197 $msg = new xmlrpcmsg($methName->scalarval());
198 for ($i = 0; $i < $numParams; $i++)
200 $msg->addParam($params->arraymem($i));
203 $result = $server->execute($msg);
205 if ($result->faultCode() != 0)
207 return _xmlrpcs_multicall_error($result); // Method returned fault.
210 return new xmlrpcval(array($result->value()), 'array');
213 function _xmlrpcs_multicall($server, $m)
215 $calls = $m->getParam(0);
216 $numCalls = $calls->arraysize();
219 for ($i = 0; $i < $numCalls; $i++)
221 $call = $calls->arraymem($i);
222 $result[$i] = _xmlrpcs_multicall_do_call($server, $call);
225 return new xmlrpcresp(new xmlrpcval($result, 'array'));
228 $_xmlrpcs_dmap=array(
229 'system.listMethods' => array(
230 'function' => '_xmlrpcs_listMethods',
231 'signature' => $_xmlrpcs_listMethods_sig,
232 'docstring' => $_xmlrpcs_listMethods_doc),
233 'system.methodHelp' => array(
234 'function' => '_xmlrpcs_methodHelp',
235 'signature' => $_xmlrpcs_methodHelp_sig,
236 'docstring' => $_xmlrpcs_methodHelp_doc),
237 'system.methodSignature' => array(
238 'function' => '_xmlrpcs_methodSignature',
239 'signature' => $_xmlrpcs_methodSignature_sig,
240 'docstring' => $_xmlrpcs_methodSignature_doc),
241 'system.multicall' => array(
242 'function' => '_xmlrpcs_multicall',
243 'signature' => $_xmlrpcs_multicall_sig,
244 'docstring' => $_xmlrpcs_multicall_doc
248 $_xmlrpc_debuginfo='';
249 function xmlrpc_debugmsg($m)
251 global $_xmlrpc_debuginfo;
252 $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
259 function xmlrpc_server($dispMap='', $serviceNow=1)
261 global $HTTP_RAW_POST_DATA;
262 // dispMap is a dispatch array of methods
263 // mapped to function names and signatures
265 // doesn't appear in the map then an unknown
266 // method error is generated
267 /* milosch - changed to make passing dispMap optional.
268 * instead, you can use the class add_to_map() function
269 * to add functions manually (borrowed from SOAPX4)
273 $this->dmap = $dispMap;
281 function serializeDebug()
283 global $_xmlrpc_debuginfo;
284 if ($_xmlrpc_debuginfo!='')
286 return "<!-- DEBUG INFO:\n\n" . $_xmlrpc_debuginfo . "\n-->\n";
296 global $xmlrpc_defencoding;
298 $r=$this->parseRequest();
299 $payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?>' . "\n"
300 . $this->serializeDebug()
302 Header("Content-type: text/xml");
303 Header("Content-length: " . strlen($payload));
308 add a method to the dispatch map
310 function add_to_map($methodname,$function,$sig,$doc)
312 //echo "adding $methodname : $function :<bR />";
313 $this->dmap[$methodname] = array(
314 'function' => $function,
320 function verifySignature($in, $sig)
324 //echo "in is: " . print_r($in->getNumParams(),true);
325 $wanted;$got;$pno;$argcount=false;
326 for($i=0;$i<sizeof($sig);$i++)
328 // check each possible signature in turn
330 //what is the plus one for, all signatures must include at least a string, even if it is not supplied by the client's call
331 //is that part of the standard?
332 if (sizeof($cursig)==$in->getNumParams()+1)
336 for($n=0; $n<$in->getNumParams(); $n++)
338 $p=$in->getParam($n);
339 //print "<!-- $p -->\n";
340 if ($p->kindOf() == 'scalar') {
346 // $n+1 as first type of sig is return type
347 if ($pt != $cursig[$n+1]) {
350 $wanted=$cursig[$n+1];
365 return array(0,"Signature count off, wanted: " . (count($cursig) -1) ." got: " .$in->getNumParams() . " Method Name: " . $in->method());
368 return array(0, "Wanted " . $wanted . ", got " . $got . " at param " . $pno . " Method Name: " . $in->method() . "\nargs " . print_r($in,true));
374 function parseRequest($data='')
376 global $_xh,$HTTP_RAW_POST_DATA;
377 global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
382 $data=$HTTP_RAW_POST_DATA;
384 $parser = xml_parser_create($xmlrpc_defencoding);
386 $_xh[$parser]=array();
387 $_xh[$parser]['st']='';
388 $_xh[$parser]['cm']=0;
389 $_xh[$parser]['isf']=0;
390 $_xh[$parser]['params']=array();
391 $_xh[$parser]['method']='';
393 // decompose incoming XML into request structure
395 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
396 xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
397 xml_set_character_data_handler($parser, 'xmlrpc_cd');
398 xml_set_default_handler($parser, 'xmlrpc_dh');
399 if (!xml_parse($parser, $data, 1))
401 // return XML error as a faultCode
403 $xmlrpcerrxml+xml_get_error_code($parser),
404 sprintf('XML error: %s at line %d',
405 xml_error_string(xml_get_error_code($parser)),
406 xml_get_current_line_number($parser)));
407 xml_parser_free($parser);
411 xml_parser_free($parser);
412 $m=new xmlrpcmsg($_xh[$parser]['method']);
413 // now add parameters in
415 for($i=0; $i<sizeof($_xh[$parser]['params']); $i++)
417 //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
418 $plist.="$i - " . $_xh[$parser]['params'][$i]. " \n";
419 eval('$m->addParam(' . $_xh[$parser]['params'][$i]. ');');
421 // uncomment this to really see what the server's getting!
422 xmlrpc_debugmsg($plist);
424 $r = $this->execute($m);
430 function execute ($m)
432 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
433 // now to deal with the method
434 $methName = $m->method();
436 //$f = fopen("log/xmlrpc.log","a");
437 //fwrite($f,"Got this:\n" . print_r($m,true) . "\n");
440 $sysCall = ereg("^system\.", $methName);
441 $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
443 if (!isset($dmap[$methName]['function']))
445 //echo "method: '" . $methName ."'<br />";
447 return new xmlrpcresp(0,
448 $xmlrpcerr['unknown_method'] . " " . $methName,
449 $xmlrpcstr['unknown_method']);
452 if (isset($dmap[$methName]['signature']))
456 $sig = $dmap[$methName]['signature'];
457 list ($ok, $errstr) = $this->verifySignature($m, $sig);
462 return new xmlrpcresp(0,
463 $xmlrpcerr['incorrect_params'],
464 $xmlrpcstr['incorrect_params'] . ": ${errstr}");
468 $func = $dmap[$methName]['function'];
472 $retval = call_user_func($func, $this, $m);
476 //echo "calling user func: $func <br />";
478 $retval = call_user_func($func, $m);
480 //fwrite($f,"Returning this:\n" . print_r($retval,true) . "\n");
487 global $HTTP_RAW_POST_DATA;
489 // a debugging routine: just echos back the input
490 // packet as a string value
493 $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
494 print $r->serialize();
497 function registerMethods(&$obj) {
498 $cvars = $this->getObjectVars($obj);
499 foreach($cvars as $ckey => $cvar) {
500 if (is_object($cvar)) {
501 $this->registerMethods($cvars[$ckey]);
505 $meths = get_class_methods($obj);
507 foreach($meths as $method) {
508 if (! (in_array(strtolower($method),$this->get_ancestors($obj))) && strtolower($method) != strtolower(get_class($obj)) && ! (strpos($method,"_") === 0)) {
509 if (array_key_exists($obj->_func_map[$method]['name'],$this->dmap)) {
510 $xmlrpcname = $obj->_func_map[$method]['name'];
511 $entry = $this->dmap[$xmlrpcname];
512 trigger_error("Cannot register a function <b>$method as '" . $obj->_func_map[$method]['name'] . "'</b> in class '" . get_class($obj) . "' to XMLRPC server, that XMLRPC name is already in use by another entry in class '" . get_class($entry['function'][0]) ."'" , E_USER_ERROR);
514 elseif (is_callable(array(&$obj,$method))) {
515 //echo "registering method: $method as " . $obj->_func_map[$method]['name'] ."\n";// . " sig is: " . print_r($obj->func_map[$method]['sig'],true) . "<br />";
516 $this->add_to_map($obj->_func_map[$method]['name'],array(&$obj,$method),array($obj->_func_map[$method]['sig']),$obj->_func_map[$method]['doc']);
519 trigger_error("Default Error: Cannot register a function <b>$method in class " . get_class($obj) ."</b> to XMLRPC server which is not callable, check addFunc calls to make sure names map correctly.", E_USER_ERROR);
523 //echo "skipping method: $method\n";
528 function &getVar($obj, $name) {
529 $expr="\$prop=&\$obj->$name;";
534 function &getObjectVars(&$obj) {
536 $vars=get_object_vars($obj);
537 foreach ($vars as $var => $value) {
538 $result[$var]=&$this->getVar($obj, $var);
543 function get_ancestors ($class) {
544 for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = strtolower($class));