removed call time *pass by reference* and made sure pass by reference specified in...
[openemr.git] / library / freeb / xmlrpcs.inc
blobcb191cd191972a09a8f35ae0db99fbb9212ae119
1 <?php
2 // by Edd Dumbill (C) 1999-2002
3 // <edd@usefulinc.com>
4 // $Id$
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:
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)
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         }
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;
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                 //echo "<!-- ${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         }
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;
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         }
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)
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         }
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                 }
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();
197                 $msg = new xmlrpcmsg($methName->scalarval());
198                 for ($i = 0; $i < $numParams; $i++)
199                 {
200                         $msg->addParam($params->arraymem($i));
201                 }
203                 $result = $server->execute($msg);
205                 if ($result->faultCode() != 0)
206                 {
207                         return _xmlrpcs_multicall_error($result);    // Method returned fault.
208                 }
210                 return new xmlrpcval(array($result->value()), 'array');
211         }
213         function _xmlrpcs_multicall($server, $m)
214         {
215                 $calls = $m->getParam(0);
216                 $numCalls = $calls->arraysize();
217                 $result = array();
219                 for ($i = 0; $i < $numCalls; $i++)
220                 {
221                         $call = $calls->arraymem($i);
222                         $result[$i] = _xmlrpcs_multicall_do_call($server, $call);
223                 }
225                 return new xmlrpcresp(new xmlrpcval($result, 'array'));
226         }
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         );
248         $_xmlrpc_debuginfo='';
249         function xmlrpc_debugmsg($m)
250         {
251                 global $_xmlrpc_debuginfo;
252                 $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
253         }
255         class xmlrpc_server
256         {
257                 var $dmap=array();
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                 }
281                 function serializeDebug()
282                 {
283                         global $_xmlrpc_debuginfo;
284                         if ($_xmlrpc_debuginfo!='')
285                         {
286                                 return "<!-- DEBUG INFO:\n\n" . $_xmlrpc_debuginfo . "\n-->\n";
287                         }
288                         else
289                         {
290                                 return '';
291                         }
292                 }
294                 function service()
295                 {
296                         global $xmlrpc_defencoding;
298                         $r=$this->parseRequest();
299                         $payload='<?xml version="1.0" encoding="' . $xmlrpc_defencoding . '"?>' . "\n"
300                                 . $this->serializeDebug()
301                                 . $r->serialize();
302                         Header("Content-type: text/xml");
303                         Header("Content-length: " . strlen($payload));
304                         print $payload;
305                 }
307                 /*
308                 add a method to the dispatch map
309                 */
310                 function add_to_map($methodname,$function,$sig,$doc)
311                 {
312                         //echo "adding $methodname : $function :<bR />";
313                         $this->dmap[$methodname] = array(
314                                 'function'  => $function,
315                                 'signature' => $sig,
316                                 'docstring' => $doc
317                         );
318                 }
320                 function verifySignature($in, $sig)
321                 {
322                         
323                         //print_r($sig);
324                         //echo "in is: " . print_r($in->getNumParams(),true);
325                         $wanted;$got;$pno;$argcount=false;
326                         for($i=0;$i<sizeof($sig);$i++)
327                         {
328                                 // check each possible signature in turn
329                                 $cursig=$sig[$i];
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)
333                                 {
334                                         $argcount=true;
335                                         $itsOK=1;
336                                         for($n=0; $n<$in->getNumParams(); $n++)
337                                         {
338                                                 $p=$in->getParam($n);
339                                                 //print "<!-- $p -->\n";
340                                                 if ($p->kindOf() == 'scalar') {
341                                                         $pt=$p->scalartyp();
342                                                 }
343                                                 else {
344                                                         $pt=$p->kindOf();
345                                                 }
346                                                 // $n+1 as first type of sig is return type
347                                                 if ($pt != $cursig[$n+1]) {
348                                                         $itsOK=0;
349                                                         $pno=$n+1;
350                                                         $wanted=$cursig[$n+1];
351                                                         $got=$pt;
352                                                         break;
353                                                 }
354                                         }
355                                         if ($itsOK)
356                                         {
357                                                 return array(1);
358                                         }
359                                 }
360                                 else {
361                                         $argcount = false;
362                                 }
364                                 if (!$argcount) {
365                                         return array(0,"Signature count off, wanted: " . (count($cursig) -1) ." got: " .$in->getNumParams() . " Method Name: " . $in->method());
366                                 }
367                                 
368                                 return array(0, "Wanted " . $wanted . ", got " . $got . " at param " . $pno  . " Method Name: " . $in->method() . "\nargs " . print_r($in,true));
369                         }
371                         
372                 }
374                 function parseRequest($data='')
375                 {
376                         global $_xh,$HTTP_RAW_POST_DATA;
377                         global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
378                         $_xmlrpcs_dmap;
380                         if ($data=='')
381                         {
382                                 $data=$HTTP_RAW_POST_DATA;
383                         }
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))
400                         {
401                                 // return XML error as a faultCode
402                                 $r=new xmlrpcresp(0,
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);
408                         }
409                         else
410                         {
411                                 xml_parser_free($parser);
412                                 $m=new xmlrpcmsg($_xh[$parser]['method']);
413                                 // now add parameters in
414                                 $plist='';
415                                 for($i=0; $i<sizeof($_xh[$parser]['params']); $i++)
416                                 {
417                                         //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
418                                         $plist.="$i - " .  $_xh[$parser]['params'][$i]. " \n";
419                                         eval('$m->addParam(' . $_xh[$parser]['params'][$i]. ');');
420                                 }
421                                 // uncomment this to really see what the server's getting!
422                                  xmlrpc_debugmsg($plist);
424                                 $r = $this->execute($m);
425                         }
427                         return $r;
428                 }
430                 function execute ($m)
431                 {
432                         global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
433                         // now to deal with the method
434                         $methName = $m->method();
435                         
436                         //$f = fopen("log/xmlrpc.log","a");
437                         //fwrite($f,"Got this:\n" . print_r($m,true) . "\n");
438                         
439                         
440                         $sysCall = ereg("^system\.", $methName);
441                         $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
442                         //print_r($dmap);
443                         if (!isset($dmap[$methName]['function']))
444                         {
445                         //echo "method: '" . $methName ."'<br />";
446                                 // No such method
447                                 return new xmlrpcresp(0,
448                                         $xmlrpcerr['unknown_method'] . " " . $methName,
449                                         $xmlrpcstr['unknown_method']);
450                         }
451                         // Check signature.
452                         if (isset($dmap[$methName]['signature']))
453                         {
456                                 $sig = $dmap[$methName]['signature'];
457                                 list ($ok, $errstr) = $this->verifySignature($m, $sig);
458                                 if (!$ok)
460                                 {
461                                         // Didn't match.
462                                         return new xmlrpcresp(0,
463                                                 $xmlrpcerr['incorrect_params'],
464                                                 $xmlrpcstr['incorrect_params'] . ": ${errstr}");
465                                 }
466                         }
468                         $func = $dmap[$methName]['function'];
469                         
470                         if ($sysCall)
471                         {
472                                 $retval = call_user_func($func, $this, $m);
473                         }
474                         else
475                         {
476                                 //echo "calling user func: $func <br />";
477                                 //print_r($func);
478                                 $retval = call_user_func($func, $m);
479                         }
480                         //fwrite($f,"Returning this:\n" . print_r($retval,true) . "\n");
481                         //fclose($f);
482                         return $retval;
483                 }
485                 function echoInput()
486                 {
487                         global $HTTP_RAW_POST_DATA;
489                         // a debugging routine: just echos back the input
490                         // packet as a string value
492                         $r=new xmlrpcresp;
493                         $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
494                         print $r->serialize();
495                 }
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]);
502                                 }
503                         }
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);
513                                         }
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']);
517                                         }
518                                         else {
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);
520                                         }
521                                 }
522                                 else {
523                                         //echo "skipping method: $method\n";
524                                 }
525                         }
526                 }
528                 function &getVar($obj, $name)   {
529                         $expr="\$prop=&\$obj->$name;";
530                         eval($expr);
531                         return $prop;
532                 }
534                 function &getObjectVars(&$obj)  {
535                         $result=array();
536                         $vars=get_object_vars($obj);
537                         foreach ($vars as $var => $value)       {
538                                 $result[$var]=&$this->getVar($obj, $var);
539                         }
540                         return $result;
541                 }
543                 function get_ancestors ($class) {
544                         for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = strtolower($class));
545                                 return $classes;
546                 }
548         }