2 // by Edd Dumbill (C) 1999-2002
4 // $Id: xmlrpcs.inc,v 1.4 2005-07-21 19:25:13 rurban Exp $
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 // print "<!-- ${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_encode_entitites($_xmlrpc_debuginfo) . "\n-->\n";
296 //global $xmlrpc_defencoding;
298 $r=$this->parseRequest();
299 //$payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?' . '>' . "\n"
300 $payload='<?xml version="1.0" ?' . '>' . "\n"
301 . $this->serializeDebug()
303 header('Content-Type: text/xml');
304 header('Content-Length: ' . (int)strlen($payload));
309 add a method to the dispatch map
311 function add_to_map($methodname,$function,$sig,$doc)
313 $this->dmap[$methodname] = array(
314 'function' => $function,
320 function verifySignature($in, $sig)
322 for($i=0; $i<sizeof($sig); $i++)
324 // check each possible signature in turn
326 if (sizeof($cursig)==$in->getNumParams()+1)
329 for($n=0; $n<$in->getNumParams(); $n++)
331 $p=$in->getParam($n);
332 // print "<!-- $p -->\n";
333 if ($p->kindOf() == 'scalar')
341 // $n+1 as first type of sig is return type
342 if ($pt != $cursig[$n+1])
345 $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
356 return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
358 return array(0, "No method signature matches number of parameters");
361 function parseRequest($data='')
363 global $_xh,$HTTP_RAW_POST_DATA;
364 global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
365 $_xmlrpcs_dmap, $xmlrpc_internalencoding;
369 $data=$HTTP_RAW_POST_DATA;
371 // G. Giunta 2005/02/13: we do NOT expect to receive html entities
372 // so we do not try to convert them into xml character entities
373 //$data = xmlrpc_html_entity_xlate($data);
374 $parser = xml_parser_create($xmlrpc_defencoding);
375 // Error: Resource ID#46 used as offset, casting to integer
376 $parser_idx = (int)$parser;
378 $_xh[$parser_idx]=array();
379 $_xh[$parser_idx]['st']='';
380 $_xh[$parser_idx]['cm']=0;
381 $_xh[$parser_idx]['isf']=0;
382 $_xh[$parser_idx]['params']=array();
383 $_xh[$parser_idx]['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);
407 xml_parser_free($parser);
408 $m=new xmlrpcmsg($_xh[$parser_idx]['method']);
409 // now add parameters in
412 for($i=0; $i<sizeof($_xh[$parser_idx]['params']); $i++)
414 //print "<!-- " . $_xh[$parser_idx]['params'][$i]. "-->\n";
415 $plist.="$i - " . $_xh[$parser_idx]['params'][$i]. ";\n";
417 @eval('$m->addParam(' . $_xh[$parser_idx]['params'][$i]. '); $allOK=1;');
423 // uncomment this to really see what the server's getting!
424 // xmlrpc_debugmsg($plist);
427 $r = new xmlrpcresp(0,
428 $xmlrpcerr['incorrect_params'],
429 $xmlrpcstr['incorrect_params'] . ": xml error in param " . $i);
433 $r = $this->execute($m);
439 function execute ($m)
441 global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
442 // now to deal with the method
443 $methName = $m->method();
444 $sysCall = ereg("^system\.", $methName);
445 $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
447 if (!isset($dmap[$methName]['function']))
450 return new xmlrpcresp(0,
451 $xmlrpcerr['unknown_method'],
452 $xmlrpcstr['unknown_method']);
456 if (isset($dmap[$methName]['signature']))
458 $sig = $dmap[$methName]['signature'];
459 list($ok, $errstr) = $this->verifySignature($m, $sig);
463 return new xmlrpcresp(
465 $xmlrpcerr['incorrect_params'],
466 $xmlrpcstr['incorrect_params'] . ": ${errstr}"
471 $func = $dmap[$methName]['function'];
475 return call_user_func($func, $this, $m);
479 return call_user_func($func, $m);
485 global $HTTP_RAW_POST_DATA;
487 // a debugging routine: just echos back the input
488 // packet as a string value
491 $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
492 print $r->serialize();