remove obsolete frame divider images
[openemr.git] / library / freeb / xmlrpcs.inc
blob1af4fcc25630a5fb10da2fb3aed6b126d7d02ed9
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\r\nContent-length: " . strlen($payload));
303                         print $payload;
304                 }
306                 /*
307                 add a method to the dispatch map
308                 */
309                 function add_to_map($methodname,$function,$sig,$doc)
310                 {
311                         //echo "adding $methodname : $function :<bR />";
312                         $this->dmap[$methodname] = array(
313                                 'function'  => $function,
314                                 'signature' => $sig,
315                                 'docstring' => $doc
316                         );
317                 }
319                 function verifySignature($in, $sig)
320                 {
321                         
322                         //print_r($sig);
323                         //echo "in is: " . print_r($in->getNumParams(),true);
324                         $wanted;$got;$pno;$argcount=false;
325                         for($i=0;$i<sizeof($sig);$i++)
326                         {
327                                 // check each possible signature in turn
328                                 $cursig=$sig[$i];
329                                 //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
330                                 //is that part of the standard?
331                                 if (sizeof($cursig)==$in->getNumParams()+1)
332                                 {
333                                         $argcount=true;
334                                         $itsOK=1;
335                                         for($n=0; $n<$in->getNumParams(); $n++)
336                                         {
337                                                 $p=$in->getParam($n);
338                                                 //print "<!-- $p -->\n";
339                                                 if ($p->kindOf() == 'scalar') {
340                                                         $pt=$p->scalartyp();
341                                                 }
342                                                 else {
343                                                         $pt=$p->kindOf();
344                                                 }
345                                                 // $n+1 as first type of sig is return type
346                                                 if ($pt != $cursig[$n+1]) {
347                                                         $itsOK=0;
348                                                         $pno=$n+1;
349                                                         $wanted=$cursig[$n+1];
350                                                         $got=$pt;
351                                                         break;
352                                                 }
353                                         }
354                                         if ($itsOK)
355                                         {
356                                                 return array(1);
357                                         }
358                                 }
359                                 else {
360                                         $argcount = false;
361                                 }
363                                 if (!$argcount) {
364                                         return array(0,"Signature count off, wanted: " . (count($cursig) -1) ." got: " .$in->getNumParams() . " Method Name: " . $in->method());
365                                 }
366                                 
367                                 return array(0, "Wanted " . $wanted . ", got " . $got . " at param " . $pno  . " Method Name: " . $in->method() . "\nargs " . print_r($in,true));
368                         }
370                         
371                 }
373                 function parseRequest($data='')
374                 {
375                         global $_xh,$HTTP_RAW_POST_DATA;
376                         global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding,
377                         $_xmlrpcs_dmap;
379                         if ($data=='')
380                         {
381                                 $data=$HTTP_RAW_POST_DATA;
382                         }
383                         $parser = xml_parser_create($xmlrpc_defencoding);
385                         $_xh[$parser]=array();
386                         $_xh[$parser]['st']='';
387                         $_xh[$parser]['cm']=0;
388                         $_xh[$parser]['isf']=0;
389                         $_xh[$parser]['params']=array();
390                         $_xh[$parser]['method']='';
392                         // decompose incoming XML into request structure
394                         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
395                         xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
396                         xml_set_character_data_handler($parser, 'xmlrpc_cd');
397                         xml_set_default_handler($parser, 'xmlrpc_dh');
398                         if (!xml_parse($parser, $data, 1))
399                         {
400                                 // return XML error as a faultCode
401                                 $r=new xmlrpcresp(0,
402                                 $xmlrpcerrxml+xml_get_error_code($parser),
403                                 sprintf('XML error: %s at line %d',
404                                         xml_error_string(xml_get_error_code($parser)),
405                                         xml_get_current_line_number($parser)));
406                                 xml_parser_free($parser);
407                         }
408                         else
409                         {
410                                 xml_parser_free($parser);
411                                 $m=new xmlrpcmsg($_xh[$parser]['method']);
412                                 // now add parameters in
413                                 $plist='';
414                                 for($i=0; $i<sizeof($_xh[$parser]['params']); $i++)
415                                 {
416                                         //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
417                                         $plist.="$i - " .  $_xh[$parser]['params'][$i]. " \n";
418                                         eval('$m->addParam(' . $_xh[$parser]['params'][$i]. ');');
419                                 }
420                                 // uncomment this to really see what the server's getting!
421                                  xmlrpc_debugmsg($plist);
423                                 $r = $this->execute($m);
424                         }
426                         return $r;
427                 }
429                 function execute ($m)
430                 {
431                         global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
432                         // now to deal with the method
433                         $methName = $m->method();
434                         
435                         //$f = fopen("log/xmlrpc.log","a");
436                         //fwrite($f,"Got this:\n" . print_r($m,true) . "\n");
437                         
438                         
439                         $sysCall = ereg("^system\.", $methName);
440                         $dmap = $sysCall ? $_xmlrpcs_dmap : $this->dmap;
441                         //print_r($dmap);
442                         if (!isset($dmap[$methName]['function']))
443                         {
444                         //echo "method: '" . $methName ."'<br />";
445                                 // No such method
446                                 return new xmlrpcresp(0,
447                                         $xmlrpcerr['unknown_method'] . " " . $methName,
448                                         $xmlrpcstr['unknown_method']);
449                         }
450                         // Check signature.
451                         if (isset($dmap[$methName]['signature']))
452                         {
455                                 $sig = $dmap[$methName]['signature'];
456                                 list ($ok, $errstr) = $this->verifySignature($m, $sig);
457                                 if (!$ok)
459                                 {
460                                         // Didn't match.
461                                         return new xmlrpcresp(0,
462                                                 $xmlrpcerr['incorrect_params'],
463                                                 $xmlrpcstr['incorrect_params'] . ": ${errstr}");
464                                 }
465                         }
467                         $func = $dmap[$methName]['function'];
468                         
469                         if ($sysCall)
470                         {
471                                 $retval = call_user_func($func, $this, $m);
472                         }
473                         else
474                         {
475                                 //echo "calling user func: $func <br />";
476                                 //print_r($func);
477                                 $retval = call_user_func($func, $m);
478                         }
479                         //fwrite($f,"Returning this:\n" . print_r($retval,true) . "\n");
480                         //fclose($f);
481                         return $retval;
482                 }
484                 function echoInput()
485                 {
486                         global $HTTP_RAW_POST_DATA;
488                         // a debugging routine: just echos back the input
489                         // packet as a string value
491                         $r=new xmlrpcresp;
492                         $r->xv=new xmlrpcval( "'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
493                         print $r->serialize();
494                 }
496                 function registerMethods(&$obj) {
497                         $cvars = $this->getObjectVars(&$obj);
498                         foreach($cvars as $ckey => $cvar) {
499                                 if (is_object($cvar)) {
500                                         $this->registerMethods(&$cvars[$ckey]);
501                                 }
502                         }
504                         $meths = get_class_methods($obj);
506                         foreach($meths as $method) {
507                                 if (! (in_array(strtolower($method),$this->get_ancestors($obj))) && strtolower($method) != strtolower(get_class($obj)) && ! (strpos($method,"_") === 0)) {
508                                         if (array_key_exists($obj->_func_map[$method]['name'],$this->dmap)) {
509                                                 $xmlrpcname = $obj->_func_map[$method]['name'];
510                                                 $entry = $this->dmap[$xmlrpcname];
511                                                 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);
512                                         }
513                                         elseif (is_callable(array(&$obj,$method))) {
514                                                 //echo "registering method: $method as " . $obj->_func_map[$method]['name'] ."\n";// . " sig is: " . print_r($obj->func_map[$method]['sig'],true) . "<br />";
515                                                 $this->add_to_map($obj->_func_map[$method]['name'],array(&$obj,$method),array($obj->_func_map[$method]['sig']),$obj->_func_map[$method]['doc']);
516                                         }
517                                         else {
518                                                 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);
519                                         }
520                                 }
521                                 else {
522                                         //echo "skipping method: $method\n";
523                                 }
524                         }
525                 }
527                 function &getVar($obj, $name)   {
528                         $expr="\$prop=&\$obj->$name;";
529                         eval($expr);
530                         return $prop;
531                 }
533                 function &getObjectVars($obj)   {
534                         $result=array();
535                         $vars=get_object_vars($obj);
536                         foreach ($vars as $var => $value)       {
537                                 $result[$var]=&$this->getVar(&$obj, $var);
538                         }
539                         return $result;
540                 }
542                 function get_ancestors ($class) {
543                         for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = strtolower($class));
544                                 return $classes;
545                 }
547         }