2 // by Edd Dumbill (C) 1999-2002
5 // Copyright (c) 1999,2000,2002 Edd Dumbill.
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
20 // * Neither the name of the "XML-RPC for PHP" nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35 // OF THE POSSIBILITY OF SUCH DAMAGE.
37 // XML RPC Server class
38 // requires: xmlrpc.inc
40 // listMethods: either a string, or nothing
41 $_xmlrpcs_listMethods_sig=array(array($xmlrpcArray, $xmlrpcString), array($xmlrpcArray));
42 $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';
43 function _xmlrpcs_listMethods($server, $m)
45 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
49 for(reset($dmap); list($key, $val)=each($dmap); )
51 $outAr[]=new xmlrpcval($key, 'string');
54 for(reset($dmap); list($key, $val)=each($dmap); )
56 $outAr[]=new xmlrpcval($key, 'string');
59 return new xmlrpcresp($v);
62 $_xmlrpcs_methodSignature_sig=array(array($xmlrpcArray, $xmlrpcString));
63 $_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)';
64 function _xmlrpcs_methodSignature($server, $m)
66 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
68 $methName=$m->getParam(0);
69 $methName=$methName->scalarval();
70 if (ereg("^system\.", $methName))
72 $dmap=$_xmlrpcs_dmap; $sysCall=1;
76 $dmap=$server->dmap; $sysCall=0;
78 // print "<!-- ${methName} -->\n";
79 if (isset($dmap[$methName]))
81 if ($dmap[$methName]['signature'])
84 $thesigs=$dmap[$methName]['signature'];
85 for($i=0; $i<sizeof($thesigs); $i++)
89 for($j=0; $j<sizeof($inSig); $j++)
91 $cursig[]=new xmlrpcval($inSig[$j], 'string');
93 $sigs[]=new xmlrpcval($cursig, 'array');
95 $r=new xmlrpcresp(new xmlrpcval($sigs, 'array'));
99 $r=new xmlrpcresp(new xmlrpcval('undef', 'string'));
104 $r=new xmlrpcresp(0,$xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
109 $_xmlrpcs_methodHelp_sig=array(array($xmlrpcString, $xmlrpcString));
110 $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';
111 function _xmlrpcs_methodHelp($server, $m)
113 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
115 $methName=$m->getParam(0);
116 $methName=$methName->scalarval();
117 if (ereg("^system\.", $methName))
119 $dmap=$_xmlrpcs_dmap; $sysCall=1;
123 $dmap=$server->dmap; $sysCall=0;
125 // print "<!-- ${methName} -->\n";
126 if (isset($dmap[$methName]))
128 if ($dmap[$methName]['docstring'])
130 $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string');
134 $r=new xmlrpcresp(new xmlrpcval('', 'string'));
139 $r=new xmlrpcresp(0, $xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
144 $_xmlrpcs_multicall_sig = array(array($xmlrpcArray, $xmlrpcArray));
145 $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details';
147 function _xmlrpcs_multicall_error($err)
151 global $xmlrpcerr, $xmlrpcstr;
152 $str = $xmlrpcstr["multicall_${err}"];
153 $code = $xmlrpcerr["multicall_${err}"];
157 $code = $err->faultCode();
158 $str = $err->faultString();
160 $struct['faultCode'] = new xmlrpcval($code, 'int');
161 $struct['faultString'] = new xmlrpcval($str, 'string');
162 return new xmlrpcval($struct, 'struct');
165 function _xmlrpcs_multicall_do_call($server, $call)
167 if ($call->kindOf() != 'struct')
169 return _xmlrpcs_multicall_error('notstruct');
171 $methName = $call->structmem('methodName');
174 return _xmlrpcs_multicall_error('nomethod');
176 if ($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string')
178 return _xmlrpcs_multicall_error('notstring');
180 if ($methName->scalarval() == 'system.multicall')
182 return _xmlrpcs_multicall_error('recursion');
185 $params = $call->structmem('params');
188 return _xmlrpcs_multicall_error('noparams');
190 if ($params->kindOf() != 'array')
192 return _xmlrpcs_multicall_error('notarray');
194 $numParams = $params->arraysize();
196 $msg = new xmlrpcmsg($methName->scalarval());
197 for ($i = 0; $i < $numParams; $i++)
199 $msg->addParam($params->arraymem($i));
202 $result = $server->execute($msg);
204 if ($result->faultCode() != 0)
206 return _xmlrpcs_multicall_error($result); // Method returned fault.
209 return new xmlrpcval(array($result->value()), 'array');
212 function _xmlrpcs_multicall($server, $m)
214 $calls = $m->getParam(0);
215 $numCalls = $calls->arraysize();
218 for ($i = 0; $i < $numCalls; $i++)
220 $call = $calls->arraymem($i);
221 $result[$i] = _xmlrpcs_multicall_do_call($server, $call);
224 return new xmlrpcresp(new xmlrpcval($result, 'array'));
227 $_xmlrpcs_dmap=array(
228 'system.listMethods' => array(
229 'function' => '_xmlrpcs_listMethods',
230 'signature' => $_xmlrpcs_listMethods_sig,
231 'docstring' => $_xmlrpcs_listMethods_doc),
232 'system.methodHelp' => array(
233 'function' => '_xmlrpcs_methodHelp',
234 'signature' => $_xmlrpcs_methodHelp_sig,
235 'docstring' => $_xmlrpcs_methodHelp_doc),
236 'system.methodSignature' => array(
237 'function' => '_xmlrpcs_methodSignature',
238 'signature' => $_xmlrpcs_methodSignature_sig,
239 'docstring' => $_xmlrpcs_methodSignature_doc),
240 'system.multicall' => array(
241 'function' => '_xmlrpcs_multicall',
242 'signature' => $_xmlrpcs_multicall_sig,
243 'docstring' => $_xmlrpcs_multicall_doc
247 $_xmlrpc_debuginfo='';
248 function xmlrpc_debugmsg($m)
250 global $_xmlrpc_debuginfo;
251 $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
258 function xmlrpc_server($dispMap='', $serviceNow=1)
260 global $HTTP_RAW_POST_DATA;
261 // dispMap is a dispatch array of methods
262 // mapped to function names and signatures
264 // doesn't appear in the map then an unknown
265 // method error is generated
266 /* milosch - changed to make passing dispMap optional.
267 * instead, you can use the class add_to_map() function
268 * to add functions manually (borrowed from SOAPX4)
272 $this->dmap = $dispMap;
280 function serializeDebug()
282 global $_xmlrpc_debuginfo;
283 if ($_xmlrpc_debuginfo!='')
285 return "<!-- DEBUG INFO:\n\n" . xmlrpc_encode_entitites($_xmlrpc_debuginfo) . "\n-->\n";
295 //global $xmlrpc_defencoding;
297 $r=$this->parseRequest();
298 //$payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?' . '>' . "\n"
299 $payload='<?xml version="1.0" ?' . '>' . "\n"
300 . $this->serializeDebug()
302 header('Content-Type: text/xml');
303 header('Content-Length: ' . (int)strlen($payload));
308 add a method to the dispatch map
310 function add_to_map($methodname,$function,$sig,$doc)
312 $this->dmap[$methodname] = array(
313 'function' => $function,
319 function verifySignature($in, $sig)
321 for($i=0; $i<sizeof($sig); $i++)
323 // check each possible signature in turn
325 if (sizeof($cursig)==$in->getNumParams()+1)
328 for($n=0; $n<$in->getNumParams(); $n++)
330 $p=$in->getParam($n);
331 // print "<!-- $p -->\n";
332 if ($p->kindOf() == 'scalar')
340 // $n+1 as first type of sig is return type
341 if ($pt != $cursig[$n+1])
344 $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
355 return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
357 return array(0, "No method signature matches number of parameters");
360 function parseRequest($data='')
362 global $_xh,$HTTP_RAW_POST_DATA;
363 global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
364 $_xmlrpcs_dmap, $xmlrpc_internalencoding;
368 $data=$HTTP_RAW_POST_DATA;
370 // G. Giunta 2005/02/13: we do NOT expect to receive html entities
371 // so we do not try to convert them into xml character entities
372 //$data = xmlrpc_html_entity_xlate($data);
373 $parser = xml_parser_create($xmlrpc_defencoding);
375 $_xh[$parser]=array();
376 //$_xh[$parser]['st']='';
377 //$_xh[$parser]['cm']=0;
378 $_xh[$parser]['isf']=0;
379 $_xh[$parser]['isf_reason']='';
380 $_xh[$parser]['params']=array();
381 $_xh[$parser]['stack']=array();
382 $_xh[$parser]['valuestack'] = array();
383 $_xh[$parser]['method']='';
385 // decompose incoming XML into request structure
387 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
388 // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell
389 // the xml parser to give us back data in the expected charset
390 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $xmlrpc_internalencoding);
392 xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
393 xml_set_character_data_handler($parser, 'xmlrpc_cd');
394 xml_set_default_handler($parser, 'xmlrpc_dh');
395 if (!xml_parse($parser, $data, 1))
397 // return XML error as a faultCode
399 $xmlrpcerrxml+xml_get_error_code($parser),
400 sprintf('XML error: %s at line %d',
401 xml_error_string(xml_get_error_code($parser)),
402 xml_get_current_line_number($parser)));
403 xml_parser_free($parser);
406 if ($_xh[$parser]['isf'])
408 xml_parser_free($parser);
410 $xmlrpcerr['invalid_request'],
411 $xmlrpcstr['invalid_request'] . ' ' . $_xh[$parser]['isf_reason']);
415 xml_parser_free($parser);
417 $m=new xmlrpcmsg($_xh[$parser]['method']);
418 // now add parameters in
421 for($i=0; $i<sizeof($_xh[$parser]['params']); $i++)
423 //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
424 $plist.="$i - " . $_xh[$parser]['params'][$i]. ";\n";
426 //@eval('$m->addParam(' . $_xh[$parser]['params'][$i]. '); $allOK=1;');
427 @$m->addParam($_xh[$parser]['params'][$i]);
433 // uncomment this to really see what the server's getting!
434 // xmlrpc_debugmsg($plist);
437 // $r = new xmlrpcresp(0,
438 // $xmlrpcerr['incorrect_params'],
439 // $xmlrpcstr['incorrect_params'] . ": xml error in param " . $i);
443 $r = $this->execute($m);
449 function execute ($m)
451 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
452 // now to deal with the method
453 $methName = $m->method();
454 $sysCall = ereg("^system\.", $methName);
455 $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
457 if (!isset($dmap[$methName]['function']))
460 return new xmlrpcresp(0,
461 $xmlrpcerr['unknown_method'],
462 $xmlrpcstr['unknown_method']);
466 if (isset($dmap[$methName]['signature']))
468 $sig = $dmap[$methName]['signature'];
469 list($ok, $errstr) = $this->verifySignature($m, $sig);
473 return new xmlrpcresp(
475 $xmlrpcerr['incorrect_params'],
476 $xmlrpcstr['incorrect_params'] . ": ${errstr}"
481 $func = $dmap[$methName]['function'];
485 return call_user_func($func, $this, $m);
489 return call_user_func($func, $m);
495 global $HTTP_RAW_POST_DATA;
497 // a debugging routine: just echos back the input
498 // packet as a string value
501 $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
502 print $r->serialize();