2 // by Edd Dumbill (C) 1999-2002
4 // $Id: xmlrpcs.inc.php,v 1.7 2007-03-22 09:23:58 kimitake Exp $
5 // $NucleusJP: xmlrpcs.inc.php,v 1.9 2007/02/04 06:28:46 kimitake Exp $
7 // Copyright (c) 1999,2000,2002 Edd Dumbill.
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
14 // * Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 // XML RPC Server class
40 // requires: xmlrpc.inc
42 // listMethods: either a string, or nothing
43 $_xmlrpcs_listMethods_sig=array(array($xmlrpcArray, $xmlrpcString), array($xmlrpcArray));
44 $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';
45 function _xmlrpcs_listMethods($server, $m)
47 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
51 for(reset($dmap); list($key, $val)=each($dmap); )
53 $outAr[]=new xmlrpcval($key, 'string');
56 for(reset($dmap); list($key, $val)=each($dmap); )
58 $outAr[]=new xmlrpcval($key, 'string');
61 return new xmlrpcresp($v);
64 $_xmlrpcs_methodSignature_sig=array(array($xmlrpcArray, $xmlrpcString));
65 $_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)';
66 function _xmlrpcs_methodSignature($server, $m)
68 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
70 $methName=$m->getParam(0);
71 $methName=$methName->scalarval();
72 if (ereg("^system\.", $methName))
74 $dmap=$_xmlrpcs_dmap; $sysCall=1;
78 $dmap=$server->dmap; $sysCall=0;
80 // print "<!-- ${methName} -->\n";
81 if (isset($dmap[$methName]))
83 if ($dmap[$methName]['signature'])
86 $thesigs=$dmap[$methName]['signature'];
87 for($i=0; $i<sizeof($thesigs); $i++)
91 for($j=0; $j<sizeof($inSig); $j++)
93 $cursig[]=new xmlrpcval($inSig[$j], 'string');
95 $sigs[]=new xmlrpcval($cursig, 'array');
97 $r=new xmlrpcresp(new xmlrpcval($sigs, 'array'));
101 $r=new xmlrpcresp(new xmlrpcval('undef', 'string'));
106 $r=new xmlrpcresp(0,$xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
111 $_xmlrpcs_methodHelp_sig=array(array($xmlrpcString, $xmlrpcString));
112 $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';
113 function _xmlrpcs_methodHelp($server, $m)
115 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
117 $methName=$m->getParam(0);
118 $methName=$methName->scalarval();
119 if (ereg("^system\.", $methName))
121 $dmap=$_xmlrpcs_dmap; $sysCall=1;
125 $dmap=$server->dmap; $sysCall=0;
127 // print "<!-- ${methName} -->\n";
128 if (isset($dmap[$methName]))
130 if ($dmap[$methName]['docstring'])
132 $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string');
136 $r=new xmlrpcresp(new xmlrpcval('', 'string'));
141 $r=new xmlrpcresp(0, $xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
146 $_xmlrpcs_multicall_sig = array(array($xmlrpcArray, $xmlrpcArray));
147 $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details';
149 function _xmlrpcs_multicall_error($err)
153 global $xmlrpcerr, $xmlrpcstr;
154 $str = $xmlrpcstr["multicall_${err}"];
155 $code = $xmlrpcerr["multicall_${err}"];
159 $code = $err->faultCode();
160 $str = $err->faultString();
162 $struct['faultCode'] = new xmlrpcval($code, 'int');
163 $struct['faultString'] = new xmlrpcval($str, 'string');
164 return new xmlrpcval($struct, 'struct');
167 function _xmlrpcs_multicall_do_call($server, $call)
169 if ($call->kindOf() != 'struct')
171 return _xmlrpcs_multicall_error('notstruct');
173 $methName = $call->structmem('methodName');
176 return _xmlrpcs_multicall_error('nomethod');
178 if ($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string')
180 return _xmlrpcs_multicall_error('notstring');
182 if ($methName->scalarval() == 'system.multicall')
184 return _xmlrpcs_multicall_error('recursion');
187 $params = $call->structmem('params');
190 return _xmlrpcs_multicall_error('noparams');
192 if ($params->kindOf() != 'array')
194 return _xmlrpcs_multicall_error('notarray');
196 $numParams = $params->arraysize();
198 $msg = new xmlrpcmsg($methName->scalarval());
199 for ($i = 0; $i < $numParams; $i++)
201 $msg->addParam($params->arraymem($i));
204 $result = $server->execute($msg);
206 if ($result->faultCode() != 0)
208 return _xmlrpcs_multicall_error($result); // Method returned fault.
211 return new xmlrpcval(array($result->value()), 'array');
214 function _xmlrpcs_multicall($server, $m)
216 $calls = $m->getParam(0);
217 $numCalls = $calls->arraysize();
220 for ($i = 0; $i < $numCalls; $i++)
222 $call = $calls->arraymem($i);
223 $result[$i] = _xmlrpcs_multicall_do_call($server, $call);
226 return new xmlrpcresp(new xmlrpcval($result, 'array'));
229 $_xmlrpcs_dmap=array(
230 'system.listMethods' => array(
231 'function' => '_xmlrpcs_listMethods',
232 'signature' => $_xmlrpcs_listMethods_sig,
233 'docstring' => $_xmlrpcs_listMethods_doc),
234 'system.methodHelp' => array(
235 'function' => '_xmlrpcs_methodHelp',
236 'signature' => $_xmlrpcs_methodHelp_sig,
237 'docstring' => $_xmlrpcs_methodHelp_doc),
238 'system.methodSignature' => array(
239 'function' => '_xmlrpcs_methodSignature',
240 'signature' => $_xmlrpcs_methodSignature_sig,
241 'docstring' => $_xmlrpcs_methodSignature_doc),
242 'system.multicall' => array(
243 'function' => '_xmlrpcs_multicall',
244 'signature' => $_xmlrpcs_multicall_sig,
245 'docstring' => $_xmlrpcs_multicall_doc
249 $_xmlrpc_debuginfo='';
250 function xmlrpc_debugmsg($m)
252 global $_xmlrpc_debuginfo;
253 $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
260 function xmlrpc_server($dispMap='', $serviceNow=1)
262 global $HTTP_RAW_POST_DATA;
263 // dispMap is a dispatch array of methods
264 // mapped to function names and signatures
266 // doesn't appear in the map then an unknown
267 // method error is generated
268 /* milosch - changed to make passing dispMap optional.
269 * instead, you can use the class add_to_map() function
270 * to add functions manually (borrowed from SOAPX4)
274 $this->dmap = $dispMap;
282 function serializeDebug()
284 global $_xmlrpc_debuginfo;
285 if ($_xmlrpc_debuginfo!='')
287 return "<!-- DEBUG INFO:\n\n" . xmlrpc_encode_entitites($_xmlrpc_debuginfo) . "\n-->\n";
297 //global $xmlrpc_defencoding;
299 $r=$this->parseRequest();
300 //$payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?' . '>' . "\n"
301 $payload='<?xml version="1.0" ?' . '>' . "\n"
302 . $this->serializeDebug()
304 header('Content-Type: text/xml');
305 header('Content-Length: ' . (int)strlen($payload));
310 add a method to the dispatch map
312 function add_to_map($methodname,$function,$sig,$doc)
314 $this->dmap[$methodname] = array(
315 'function' => $function,
321 function verifySignature($in, $sig)
323 for($i=0; $i<sizeof($sig); $i++)
325 // check each possible signature in turn
327 if (sizeof($cursig)==$in->getNumParams()+1)
330 for($n=0; $n<$in->getNumParams(); $n++)
332 $p=$in->getParam($n);
333 // print "<!-- $p -->\n";
334 if ($p->kindOf() == 'scalar')
342 // $n+1 as first type of sig is return type
343 if ($pt != $cursig[$n+1])
346 $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
357 return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
359 return array(0, "No method signature matches number of parameters");
362 function parseRequest($data='')
364 global $_xh,$HTTP_RAW_POST_DATA;
365 global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
366 $_xmlrpcs_dmap, $xmlrpc_internalencoding;
370 $data=$HTTP_RAW_POST_DATA;
372 // G. Giunta 2005/02/13: we do NOT expect to receive html entities
373 // so we do not try to convert them into xml character entities
374 //$data = xmlrpc_html_entity_xlate($data);
375 $parser = xml_parser_create($xmlrpc_defencoding);
377 $_xh[$parser]=array();
378 //$_xh[$parser]['st']='';
379 //$_xh[$parser]['cm']=0;
380 $_xh[$parser]['isf']=0;
381 $_xh[$parser]['isf_reason']='';
382 $_xh[$parser]['params']=array();
383 $_xh[$parser]['stack']=array();
384 $_xh[$parser]['sp'] = 0;
385 $_xh[$parser]['valuestack'] = array();
386 $_xh[$parser]['vsp'] = 0;
387 $_xh[$parser]['method']='';
389 // decompose incoming XML into request structure
391 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
392 // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell
393 // the xml parser to give us back data in the expected charset
394 @xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $xmlrpc_internalencoding);
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);
410 if ($_xh[$parser]['isf'])
412 xml_parser_free($parser);
414 $xmlrpcerr['invalid_request'],
415 $xmlrpcstr['invalid_request'] . ' ' . $_xh[$parser]['isf_reason']);
419 xml_parser_free($parser);
421 $m=new xmlrpcmsg($_xh[$parser]['method']);
422 // now add parameters in
425 for($i=0; $i<sizeof($_xh[$parser]['params']); $i++)
427 //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
428 $plist.="$i - " . $_xh[$parser]['params'][$i]. ";\n";
430 //@eval('$m->addParam(' . $_xh[$parser]['params'][$i]. '); $allOK=1;');
431 @$m->addParam($_xh[$parser]['params'][$i]);
437 // uncomment this to really see what the server's getting!
438 // xmlrpc_debugmsg($plist);
441 // $r = new xmlrpcresp(0,
442 // $xmlrpcerr['incorrect_params'],
443 // $xmlrpcstr['incorrect_params'] . ": xml error in param " . $i);
447 $r = $this->execute($m);
453 function execute ($m)
455 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
456 // now to deal with the method
457 $methName = $m->method();
458 $sysCall = ereg("^system\.", $methName);
459 $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
461 if (!isset($dmap[$methName]['function']))
464 return new xmlrpcresp(0,
465 $xmlrpcerr['unknown_method'],
466 $xmlrpcstr['unknown_method']);
470 if (isset($dmap[$methName]['signature']))
472 $sig = $dmap[$methName]['signature'];
473 list($ok, $errstr) = $this->verifySignature($m, $sig);
477 return new xmlrpcresp(
479 $xmlrpcerr['incorrect_params'],
480 $xmlrpcstr['incorrect_params'] . ": ${errstr}"
485 $func = $dmap[$methName]['function'];
489 return call_user_func($func, $this, $m);
493 return call_user_func($func, $m);
499 global $HTTP_RAW_POST_DATA;
501 // a debugging routine: just echos back the input
502 // packet as a string value
505 $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
506 print $r->serialize();