]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/XMLRPC/xmlrpcs.inc
reformat, fix Resource ID#46 used as offset, casting to integer
[SourceForge/phpwiki.git] / lib / XMLRPC / xmlrpcs.inc
1 <?php
2 // by Edd Dumbill (C) 1999-2002
3 // <edd@usefulinc.com>
4 // $Id: xmlrpcs.inc,v 1.4 2005-07-21 19:25:13 rurban Exp $
5
6 // Copyright (c) 1999,2000,2002 Edd Dumbill.
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 //
13 //    * Redistributions of source code must retain the above copyright
14 //      notice, this list of conditions and the following disclaimer.
15 //
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.
20 //
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.
24 //
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.
37
38   // XML RPC Server class
39   // requires: xmlrpc.inc
40
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)
45 {
46     global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
47     $v=new xmlrpcval();
48     $dmap=$server->dmap;
49     $outAr=array();
50     for(reset($dmap); list($key, $val)=each($dmap); )
51         {
52             $outAr[]=new xmlrpcval($key, 'string');
53         }
54     $dmap=$_xmlrpcs_dmap;
55     for(reset($dmap); list($key, $val)=each($dmap); )
56         {
57             $outAr[]=new xmlrpcval($key, 'string');
58         }
59     $v->addArray($outAr);
60     return new xmlrpcresp($v);
61 }
62
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)
66 {
67     global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
68
69     $methName=$m->getParam(0);
70     $methName=$methName->scalarval();
71     if (ereg("^system\.", $methName))
72         {
73             $dmap=$_xmlrpcs_dmap; $sysCall=1;
74         }
75     else
76         {
77             $dmap=$server->dmap; $sysCall=0;
78         }
79     //  print "<!-- ${methName} -->\n";
80     if (isset($dmap[$methName]))
81         {
82             if ($dmap[$methName]['signature'])
83                 {
84                     $sigs=array();
85                     $thesigs=$dmap[$methName]['signature'];
86                     for($i=0; $i<sizeof($thesigs); $i++)
87                         {
88                             $cursig=array();
89                             $inSig=$thesigs[$i];
90                             for($j=0; $j<sizeof($inSig); $j++)
91                                 {
92                                     $cursig[]=new xmlrpcval($inSig[$j], 'string');
93                                 }
94                             $sigs[]=new xmlrpcval($cursig, 'array');
95                         }
96                     $r=new xmlrpcresp(new xmlrpcval($sigs, 'array'));
97                 }
98             else
99                 {
100                     $r=new xmlrpcresp(new xmlrpcval('undef', 'string'));
101                 }
102         }
103     else
104         {
105             $r=new xmlrpcresp(0,$xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
106         }
107     return $r;
108 }
109
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)
113 {
114     global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
115
116     $methName=$m->getParam(0);
117     $methName=$methName->scalarval();
118     if (ereg("^system\.", $methName))
119         {
120             $dmap=$_xmlrpcs_dmap; $sysCall=1;
121         }
122     else
123         {
124             $dmap=$server->dmap; $sysCall=0;
125         }
126     // print "<!-- ${methName} -->\n";
127     if (isset($dmap[$methName]))
128         {
129             if ($dmap[$methName]['docstring'])
130                 {
131                     $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string');
132                 }
133             else
134                 {
135                     $r=new xmlrpcresp(new xmlrpcval('', 'string'));
136                 }
137         }
138     else
139         {
140             $r=new xmlrpcresp(0, $xmlrpcerr['introspect_unknown'], $xmlrpcstr['introspect_unknown']);
141         }
142     return $r;
143 }
144
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';
147
148 function _xmlrpcs_multicall_error($err)
149 {
150     if (is_string($err))
151         {
152             global $xmlrpcerr, $xmlrpcstr;
153             $str  = $xmlrpcstr["multicall_${err}"];
154             $code = $xmlrpcerr["multicall_${err}"];
155         }
156     else
157         {
158             $code = $err->faultCode();
159             $str = $err->faultString();
160         }
161     $struct['faultCode'] = new xmlrpcval($code, 'int');
162     $struct['faultString'] = new xmlrpcval($str, 'string');
163     return new xmlrpcval($struct, 'struct');
164 }
165
166 function _xmlrpcs_multicall_do_call($server, $call)
167 {
168     if ($call->kindOf() != 'struct')
169         {
170             return _xmlrpcs_multicall_error('notstruct');
171         }
172     $methName = $call->structmem('methodName');
173     if (!$methName)
174         {
175             return _xmlrpcs_multicall_error('nomethod');
176         }
177     if ($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string')
178         {
179             return _xmlrpcs_multicall_error('notstring');
180         }
181     if ($methName->scalarval() == 'system.multicall')
182         {
183             return _xmlrpcs_multicall_error('recursion');
184         }
185
186     $params = $call->structmem('params');
187     if (!$params)
188         {
189             return _xmlrpcs_multicall_error('noparams');
190         }
191     if ($params->kindOf() != 'array')
192         {
193             return _xmlrpcs_multicall_error('notarray');
194         }
195     $numParams = $params->arraysize();
196
197     $msg = new xmlrpcmsg($methName->scalarval());
198     for ($i = 0; $i < $numParams; $i++)
199         {
200             $msg->addParam($params->arraymem($i));
201         }
202
203     $result = $server->execute($msg);
204
205     if ($result->faultCode() != 0)
206         {
207             return _xmlrpcs_multicall_error($result);    // Method returned fault.
208         }
209
210     return new xmlrpcval(array($result->value()), 'array');
211 }
212
213 function _xmlrpcs_multicall($server, $m)
214 {
215     $calls = $m->getParam(0);
216     $numCalls = $calls->arraysize();
217     $result = array();
218
219     for ($i = 0; $i < $numCalls; $i++)
220         {
221             $call = $calls->arraymem($i);
222             $result[$i] = _xmlrpcs_multicall_do_call($server, $call);
223         }
224
225     return new xmlrpcresp(new xmlrpcval($result, 'array'));
226 }
227
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
245                                                  )
246                      );
247
248 $_xmlrpc_debuginfo='';
249 function xmlrpc_debugmsg($m)
250 {
251     global $_xmlrpc_debuginfo;
252     $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
253 }
254
255 class xmlrpc_server
256 {
257     var $dmap=array();
258
259     function xmlrpc_server($dispMap='', $serviceNow=1)
260     {
261         global $HTTP_RAW_POST_DATA;
262         // dispMap is a dispatch array of methods
263         // mapped to function names and signatures
264         // if a method
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)
270          */
271         if($dispMap)
272             {
273                 $this->dmap = $dispMap;
274                 if($serviceNow)
275                     {
276                         $this->service();
277                     }
278             }
279     }
280
281     function serializeDebug()
282     {
283         global $_xmlrpc_debuginfo;
284         if ($_xmlrpc_debuginfo!='')
285             {
286                 return "<!-- DEBUG INFO:\n\n" . xmlrpc_encode_entitites($_xmlrpc_debuginfo) . "\n-->\n";
287             }
288         else
289             {
290                 return '';
291             }
292     }
293
294     function service()
295     {
296         //global $xmlrpc_defencoding;
297
298         $r=$this->parseRequest();
299         //$payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?' . '>' . "\n"
300         $payload='<?xml version="1.0" ?' . '>' . "\n"
301             . $this->serializeDebug()
302             . $r->serialize();
303         header('Content-Type: text/xml');
304         header('Content-Length: ' . (int)strlen($payload));
305         print $payload;
306     }
307
308     /*
309      add a method to the dispatch map
310     */
311     function add_to_map($methodname,$function,$sig,$doc)
312     {
313         $this->dmap[$methodname] = array(
314                                          'function'  => $function,
315                                          'signature' => $sig,
316                                          'docstring' => $doc
317                                          );
318     }
319
320     function verifySignature($in, $sig)
321     {
322         for($i=0; $i<sizeof($sig); $i++)
323             {
324                 // check each possible signature in turn
325                 $cursig=$sig[$i];
326                 if (sizeof($cursig)==$in->getNumParams()+1)
327                     {
328                         $itsOK=1;
329                         for($n=0; $n<$in->getNumParams(); $n++)
330                             {
331                                 $p=$in->getParam($n);
332                                 // print "<!-- $p -->\n";
333                                 if ($p->kindOf() == 'scalar')
334                                     {
335                                         $pt=$p->scalartyp();
336                                     }
337                                 else
338                                     {
339                                         $pt=$p->kindOf();
340                                     }
341                                 // $n+1 as first type of sig is return type
342                                 if ($pt != $cursig[$n+1])
343                                     {
344                                         $itsOK=0;
345                                         $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
346                                         break;
347                                     }
348                             }
349                         if ($itsOK)
350                             {
351                                 return array(1,'');
352                             }
353                     }
354             }
355         if (isset($wanted))
356             return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
357         else
358             return array(0, "No method signature matches number of parameters");
359     }
360
361     function parseRequest($data='')
362     {
363         global $_xh,$HTTP_RAW_POST_DATA;
364         global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
365             $_xmlrpcs_dmap, $xmlrpc_internalencoding;
366
367         if ($data=='')
368             {
369                 $data=$HTTP_RAW_POST_DATA;
370             }
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;
377
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']='';
384
385         // decompose incoming XML into request structure
386
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);
391
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))
396             {
397                 // return XML error as a faultCode
398                 $r=new xmlrpcresp(0,
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);
404             }
405         else
406             {
407                 xml_parser_free($parser);
408                 $m=new xmlrpcmsg($_xh[$parser_idx]['method']);
409                 // now add parameters in
410                 $plist='';
411                 $allOK = 1;
412                 for($i=0; $i<sizeof($_xh[$parser_idx]['params']); $i++)
413                     {
414                         //print "<!-- " . $_xh[$parser_idx]['params'][$i]. "-->\n";
415                         $plist.="$i - " .  $_xh[$parser_idx]['params'][$i]. ";\n";
416                         $allOK = 0;
417                         @eval('$m->addParam(' . $_xh[$parser_idx]['params'][$i]. '); $allOK=1;');
418                         if (!$allOK)
419                             {
420                                 break;
421                             }
422                     }
423                 // uncomment this to really see what the server's getting!
424                 // xmlrpc_debugmsg($plist);
425                 if (!$allOK)
426                     {
427                         $r = new xmlrpcresp(0,
428                                             $xmlrpcerr['incorrect_params'],
429                                             $xmlrpcstr['incorrect_params'] . ": xml error in param " . $i);
430                     }
431                 else
432                     {
433                         $r = $this->execute($m);
434                     }
435             }
436         return $r;
437     }
438
439     function execute ($m)
440     {
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;
446
447         if (!isset($dmap[$methName]['function']))
448             {
449                 // No such method
450                 return new xmlrpcresp(0,
451                                       $xmlrpcerr['unknown_method'],
452                                       $xmlrpcstr['unknown_method']);
453             }
454
455         // Check signature.
456         if (isset($dmap[$methName]['signature']))
457             {
458                 $sig = $dmap[$methName]['signature'];
459                 list($ok, $errstr) = $this->verifySignature($m, $sig);
460                 if(!$ok)
461                     {
462                         // Didn't match.
463                         return new xmlrpcresp(
464                                               0,
465                                               $xmlrpcerr['incorrect_params'],
466                                               $xmlrpcstr['incorrect_params'] . ": ${errstr}"
467                                               );
468                     }
469             }
470
471         $func = $dmap[$methName]['function'];
472
473         if ($sysCall)
474             {
475                 return call_user_func($func, $this, $m);
476             }
477         else
478             {
479                 return call_user_func($func, $m);
480             }
481     }
482
483     function echoInput()
484     {
485         global $HTTP_RAW_POST_DATA;
486
487         // a debugging routine: just echos back the input
488         // packet as a string value
489
490         $r=new xmlrpcresp;
491         $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
492         print $r->serialize();
493     }
494 }
495 ?>