remove one more warning with php 7.2
[plcapi.git] / lib / xmlrpcs.inc
1 <?php\r
2 // by Edd Dumbill (C) 1999-2002\r
3 // <edd@usefulinc.com>\r
4 \r
5 // Copyright (c) 1999,2000,2002 Edd Dumbill.\r
6 // All rights reserved.\r
7 //\r
8 // Redistribution and use in source and binary forms, with or without\r
9 // modification, are permitted provided that the following conditions\r
10 // are met:\r
11 //\r
12 //    * Redistributions of source code must retain the above copyright\r
13 //      notice, this list of conditions and the following disclaimer.\r
14 //\r
15 //    * Redistributions in binary form must reproduce the above\r
16 //      copyright notice, this list of conditions and the following\r
17 //      disclaimer in the documentation and/or other materials provided\r
18 //      with the distribution.\r
19 //\r
20 //    * Neither the name of the "XML-RPC for PHP" nor the names of its\r
21 //      contributors may be used to endorse or promote products derived\r
22 //      from this software without specific prior written permission.\r
23 //\r
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
28 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
30 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
31 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
32 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
33 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
34 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r
35 // OF THE POSSIBILITY OF SUCH DAMAGE.\r
36 \r
37         // XML RPC Server class\r
38         // requires: xmlrpc.inc\r
39 \r
40         $GLOBALS['xmlrpcs_capabilities'] = array(\r
41                 // xmlrpc spec: always supported\r
42                 'xmlrpc' => new xmlrpcval(array(\r
43                         'specUrl' => new xmlrpcval('http://www.xmlrpc.com/spec', 'string'),\r
44                         'specVersion' => new xmlrpcval(1, 'int')\r
45                 ), 'struct'),\r
46                 // if we support system.xxx functions, we always support multicall, too...\r
47                 // Note that, as of 2006/09/17, the following URL does not respond anymore\r
48                 'system.multicall' => new xmlrpcval(array(\r
49                         'specUrl' => new xmlrpcval('http://www.xmlrpc.com/discuss/msgReader$1208', 'string'),\r
50                         'specVersion' => new xmlrpcval(1, 'int')\r
51                 ), 'struct'),\r
52                 // introspection: version 2! we support 'mixed', too\r
53                 'introspection' => new xmlrpcval(array(\r
54                         'specUrl' => new xmlrpcval('http://phpxmlrpc.sourceforge.net/doc-2/ch10.html', 'string'),\r
55                         'specVersion' => new xmlrpcval(2, 'int')\r
56                 ), 'struct')\r
57         );\r
58 \r
59         /* Functions that implement system.XXX methods of xmlrpc servers */\r
60         $_xmlrpcs_getCapabilities_sig=array(array($GLOBALS['xmlrpcStruct']));\r
61         $_xmlrpcs_getCapabilities_doc='This method lists all the capabilites that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to';\r
62         $_xmlrpcs_getCapabilities_sdoc=array(array('list of capabilities, described as structs with a version number and url for the spec'));\r
63         function _xmlrpcs_getCapabilities($server, $m=null)\r
64         {\r
65                 $outAr = $GLOBALS['xmlrpcs_capabilities'];\r
66                 // NIL extension\r
67                 if ($GLOBALS['xmlrpc_null_extension']) {\r
68                         $outAr['nil'] = new xmlrpcval(array(\r
69                                 'specUrl' => new xmlrpcval('http://www.ontosys.com/xml-rpc/extensions.php', 'string'),\r
70                                 'specVersion' => new xmlrpcval(1, 'int')\r
71                         ), 'struct');\r
72                 }\r
73                 return new xmlrpcresp(new xmlrpcval($outAr, 'struct'));\r
74         }\r
75 \r
76         // listMethods: signature was either a string, or nothing.\r
77         // The useless string variant has been removed\r
78         $_xmlrpcs_listMethods_sig=array(array($GLOBALS['xmlrpcArray']));\r
79         $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';\r
80         $_xmlrpcs_listMethods_sdoc=array(array('list of method names'));\r
81         function _xmlrpcs_listMethods($server, $m=null) // if called in plain php values mode, second param is missing\r
82         {\r
83 \r
84                 $outAr=array();\r
85                 foreach($server->dmap as $key => $val)\r
86                 {\r
87                         $outAr[]=new xmlrpcval($key, 'string');\r
88                 }\r
89                 if($server->allow_system_funcs)\r
90                 {\r
91                         foreach($GLOBALS['_xmlrpcs_dmap'] as $key => $val)\r
92                         {\r
93                                 $outAr[]=new xmlrpcval($key, 'string');\r
94                         }\r
95                 }\r
96                 return new xmlrpcresp(new xmlrpcval($outAr, 'array'));\r
97         }\r
98 \r
99         $_xmlrpcs_methodSignature_sig=array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcString']));\r
100         $_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)';\r
101         $_xmlrpcs_methodSignature_sdoc=array(array('list of known signatures, each sig being an array of xmlrpc type names', 'name of method to be described'));\r
102         function _xmlrpcs_methodSignature($server, $m)\r
103         {\r
104                 // let accept as parameter both an xmlrpcval or string\r
105                 if (is_object($m))\r
106                 {\r
107                         $methName=$m->getParam(0);\r
108                         $methName=$methName->scalarval();\r
109                 }\r
110                 else\r
111                 {\r
112                         $methName=$m;\r
113                 }\r
114                 if(strpos($methName, "system.") === 0)\r
115                 {\r
116                         $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1;\r
117                 }\r
118                 else\r
119                 {\r
120                         $dmap=$server->dmap; $sysCall=0;\r
121                 }\r
122                 if(isset($dmap[$methName]))\r
123                 {\r
124                         if(isset($dmap[$methName]['signature']))\r
125                         {\r
126                                 $sigs=array();\r
127                                 foreach($dmap[$methName]['signature'] as $inSig)\r
128                                 {\r
129                                         $cursig=array();\r
130                                         foreach($inSig as $sig)\r
131                                         {\r
132                                                 $cursig[]=new xmlrpcval($sig, 'string');\r
133                                         }\r
134                                         $sigs[]=new xmlrpcval($cursig, 'array');\r
135                                 }\r
136                                 $r=new xmlrpcresp(new xmlrpcval($sigs, 'array'));\r
137                         }\r
138                         else\r
139                         {\r
140                                 // NB: according to the official docs, we should be returning a\r
141                                 // "none-array" here, which means not-an-array\r
142                                 $r=new xmlrpcresp(new xmlrpcval('undef', 'string'));\r
143                         }\r
144                 }\r
145                 else\r
146                 {\r
147                         $r=new xmlrpcresp(0,$GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']);\r
148                 }\r
149                 return $r;\r
150         }\r
151 \r
152         $_xmlrpcs_methodHelp_sig=array(array($GLOBALS['xmlrpcString'], $GLOBALS['xmlrpcString']));\r
153         $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';\r
154         $_xmlrpcs_methodHelp_sdoc=array(array('method description', 'name of the method to be described'));\r
155         function _xmlrpcs_methodHelp($server, $m)\r
156         {\r
157                 // let accept as parameter both an xmlrpcval or string\r
158                 if (is_object($m))\r
159                 {\r
160                         $methName=$m->getParam(0);\r
161                         $methName=$methName->scalarval();\r
162                 }\r
163                 else\r
164                 {\r
165                         $methName=$m;\r
166                 }\r
167                 if(strpos($methName, "system.") === 0)\r
168                 {\r
169                         $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1;\r
170                 }\r
171                 else\r
172                 {\r
173                         $dmap=$server->dmap; $sysCall=0;\r
174                 }\r
175                 if(isset($dmap[$methName]))\r
176                 {\r
177                         if(isset($dmap[$methName]['docstring']))\r
178                         {\r
179                                 $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string');\r
180                         }\r
181                         else\r
182                         {\r
183                                 $r=new xmlrpcresp(new xmlrpcval('', 'string'));\r
184                         }\r
185                 }\r
186                 else\r
187                 {\r
188                         $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']);\r
189                 }\r
190                 return $r;\r
191         }\r
192 \r
193         $_xmlrpcs_multicall_sig = array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcArray']));\r
194         $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details';\r
195         $_xmlrpcs_multicall_sdoc = array(array('list of response structs, where each struct has the usual members', 'list of calls, with each call being represented as a struct, with members "methodname" and "params"'));\r
196         function _xmlrpcs_multicall_error($err)\r
197         {\r
198                 if(is_string($err))\r
199                 {\r
200                         $str = $GLOBALS['xmlrpcstr']["multicall_${err}"];\r
201                         $code = $GLOBALS['xmlrpcerr']["multicall_${err}"];\r
202                 }\r
203                 else\r
204                 {\r
205                         $code = $err->faultCode();\r
206                         $str = $err->faultString();\r
207                 }\r
208                 $struct = array();\r
209                 $struct['faultCode'] = new xmlrpcval($code, 'int');\r
210                 $struct['faultString'] = new xmlrpcval($str, 'string');\r
211                 return new xmlrpcval($struct, 'struct');\r
212         }\r
213 \r
214         function _xmlrpcs_multicall_do_call($server, $call)\r
215         {\r
216                 if($call->kindOf() != 'struct')\r
217                 {\r
218                         return _xmlrpcs_multicall_error('notstruct');\r
219                 }\r
220                 $methName = @$call->structmem('methodName');\r
221                 if(!$methName)\r
222                 {\r
223                         return _xmlrpcs_multicall_error('nomethod');\r
224                 }\r
225                 if($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string')\r
226                 {\r
227                         return _xmlrpcs_multicall_error('notstring');\r
228                 }\r
229                 if($methName->scalarval() == 'system.multicall')\r
230                 {\r
231                         return _xmlrpcs_multicall_error('recursion');\r
232                 }\r
233 \r
234                 $params = @$call->structmem('params');\r
235                 if(!$params)\r
236                 {\r
237                         return _xmlrpcs_multicall_error('noparams');\r
238                 }\r
239                 if($params->kindOf() != 'array')\r
240                 {\r
241                         return _xmlrpcs_multicall_error('notarray');\r
242                 }\r
243                 $numParams = $params->arraysize();\r
244 \r
245                 $msg = new xmlrpcmsg($methName->scalarval());\r
246                 for($i = 0; $i < $numParams; $i++)\r
247                 {\r
248                         if(!$msg->addParam($params->arraymem($i)))\r
249                         {\r
250                                 $i++;\r
251                                 return _xmlrpcs_multicall_error(new xmlrpcresp(0,\r
252                                         $GLOBALS['xmlrpcerr']['incorrect_params'],\r
253                                         $GLOBALS['xmlrpcstr']['incorrect_params'] . ": probable xml error in param " . $i));\r
254                         }\r
255                 }\r
256 \r
257                 $result = $server->execute($msg);\r
258 \r
259                 if($result->faultCode() != 0)\r
260                 {\r
261                         return _xmlrpcs_multicall_error($result);               // Method returned fault.\r
262                 }\r
263 \r
264                 return new xmlrpcval(array($result->value()), 'array');\r
265         }\r
266 \r
267         function _xmlrpcs_multicall_do_call_phpvals($server, $call)\r
268         {\r
269                 if(!is_array($call))\r
270                 {\r
271                         return _xmlrpcs_multicall_error('notstruct');\r
272                 }\r
273                 if(!array_key_exists('methodName', $call))\r
274                 {\r
275                         return _xmlrpcs_multicall_error('nomethod');\r
276                 }\r
277                 if (!is_string($call['methodName']))\r
278                 {\r
279                         return _xmlrpcs_multicall_error('notstring');\r
280                 }\r
281                 if($call['methodName'] == 'system.multicall')\r
282                 {\r
283                         return _xmlrpcs_multicall_error('recursion');\r
284                 }\r
285                 if(!array_key_exists('params', $call))\r
286                 {\r
287                         return _xmlrpcs_multicall_error('noparams');\r
288                 }\r
289                 if(!is_array($call['params']))\r
290                 {\r
291                         return _xmlrpcs_multicall_error('notarray');\r
292                 }\r
293 \r
294                 // this is a real dirty and simplistic hack, since we might have received a\r
295                 // base64 or datetime values, but they will be listed as strings here...\r
296                 $numParams = count($call['params']);\r
297                 $pt = array();\r
298                 foreach($call['params'] as $val)\r
299                         $pt[] = php_2_xmlrpc_type(gettype($val));\r
300 \r
301                 $result = $server->execute($call['methodName'], $call['params'], $pt);\r
302 \r
303                 if($result->faultCode() != 0)\r
304                 {\r
305                         return _xmlrpcs_multicall_error($result);               // Method returned fault.\r
306                 }\r
307 \r
308                 return new xmlrpcval(array($result->value()), 'array');\r
309         }\r
310 \r
311         function _xmlrpcs_multicall($server, $m)\r
312         {\r
313                 $result = array();\r
314                 // let accept a plain list of php parameters, beside a single xmlrpc msg object\r
315                 if (is_object($m))\r
316                 {\r
317                         $calls = $m->getParam(0);\r
318                         $numCalls = $calls->arraysize();\r
319                         for($i = 0; $i < $numCalls; $i++)\r
320                         {\r
321                                 $call = $calls->arraymem($i);\r
322                                 $result[$i] = _xmlrpcs_multicall_do_call($server, $call);\r
323                         }\r
324                 }\r
325                 else\r
326                 {\r
327                         $numCalls=count($m);\r
328                         for($i = 0; $i < $numCalls; $i++)\r
329                         {\r
330                                 $result[$i] = _xmlrpcs_multicall_do_call_phpvals($server, $m[$i]);\r
331                         }\r
332                 }\r
333 \r
334                 return new xmlrpcresp(new xmlrpcval($result, 'array'));\r
335         }\r
336 \r
337         $GLOBALS['_xmlrpcs_dmap']=array(\r
338                 'system.listMethods' => array(\r
339                         'function' => '_xmlrpcs_listMethods',\r
340                         'signature' => $_xmlrpcs_listMethods_sig,\r
341                         'docstring' => $_xmlrpcs_listMethods_doc,\r
342                         'signature_docs' => $_xmlrpcs_listMethods_sdoc),\r
343                 'system.methodHelp' => array(\r
344                         'function' => '_xmlrpcs_methodHelp',\r
345                         'signature' => $_xmlrpcs_methodHelp_sig,\r
346                         'docstring' => $_xmlrpcs_methodHelp_doc,\r
347                         'signature_docs' => $_xmlrpcs_methodHelp_sdoc),\r
348                 'system.methodSignature' => array(\r
349                         'function' => '_xmlrpcs_methodSignature',\r
350                         'signature' => $_xmlrpcs_methodSignature_sig,\r
351                         'docstring' => $_xmlrpcs_methodSignature_doc,\r
352                         'signature_docs' => $_xmlrpcs_methodSignature_sdoc),\r
353                 'system.multicall' => array(\r
354                         'function' => '_xmlrpcs_multicall',\r
355                         'signature' => $_xmlrpcs_multicall_sig,\r
356                         'docstring' => $_xmlrpcs_multicall_doc,\r
357                         'signature_docs' => $_xmlrpcs_multicall_sdoc),\r
358                 'system.getCapabilities' => array(\r
359                         'function' => '_xmlrpcs_getCapabilities',\r
360                         'signature' => $_xmlrpcs_getCapabilities_sig,\r
361                         'docstring' => $_xmlrpcs_getCapabilities_doc,\r
362                         'signature_docs' => $_xmlrpcs_getCapabilities_sdoc)\r
363         );\r
364 \r
365         $GLOBALS['_xmlrpcs_occurred_errors'] = '';\r
366         $GLOBALS['_xmlrpcs_prev_ehandler'] = '';\r
367 \r
368         /**\r
369         * Error handler used to track errors that occur during server-side execution of PHP code.\r
370         * This allows to report back to the client whether an internal error has occurred or not\r
371         * using an xmlrpc response object, instead of letting the client deal with the html junk\r
372         * that a PHP execution error on the server generally entails.\r
373         *\r
374         * NB: in fact a user defined error handler can only handle WARNING, NOTICE and USER_* errors.\r
375         *\r
376         */\r
377         function _xmlrpcs_errorHandler($errcode, $errstring, $filename=null, $lineno=null, $context=null)\r
378         {\r
379                 // obey the @ protocol\r
380                 if (error_reporting() == 0)\r
381                         return;\r
382 \r
383                 //if($errcode != E_NOTICE && $errcode != E_WARNING && $errcode != E_USER_NOTICE && $errcode != E_USER_WARNING)\r
384                 if($errcode != E_STRICT)\r
385                 {\r
386                         $GLOBALS['_xmlrpcs_occurred_errors'] = $GLOBALS['_xmlrpcs_occurred_errors'] . $errstring . "\n";\r
387                 }\r
388                 // Try to avoid as much as possible disruption to the previous error handling\r
389                 // mechanism in place\r
390                 if($GLOBALS['_xmlrpcs_prev_ehandler'] == '')\r
391                 {\r
392                         // The previous error handler was the default: all we should do is log error\r
393                         // to the default error log (if level high enough)\r
394                         if(ini_get('log_errors') && (intval(ini_get('error_reporting')) & $errcode))\r
395                         {\r
396                                 error_log($errstring);\r
397                         }\r
398                 }\r
399                 else\r
400                 {\r
401                         // Pass control on to previous error handler, trying to avoid loops...\r
402                         if($GLOBALS['_xmlrpcs_prev_ehandler'] != '_xmlrpcs_errorHandler')\r
403                         {\r
404                                 // NB: this code will NOT work on php < 4.0.2: only 2 params were used for error handlers\r
405                                 if(is_array($GLOBALS['_xmlrpcs_prev_ehandler']))\r
406                                 {\r
407                                         // the following works both with static class methods and plain object methods as error handler\r
408                                         call_user_func_array($GLOBALS['_xmlrpcs_prev_ehandler'], array($errcode, $errstring, $filename, $lineno, $context));\r
409                                 }\r
410                                 else\r
411                                 {\r
412                                         $GLOBALS['_xmlrpcs_prev_ehandler']($errcode, $errstring, $filename, $lineno, $context);\r
413                                 }\r
414                         }\r
415                 }\r
416         }\r
417 \r
418         $GLOBALS['_xmlrpc_debuginfo']='';\r
419 \r
420         /**\r
421         * Add a string to the debug info that can be later seralized by the server\r
422         * as part of the response message.\r
423         * Note that for best compatibility, the debug string should be encoded using\r
424         * the $GLOBALS['xmlrpc_internalencoding'] character set.\r
425         * @param string $m\r
426         * @access public\r
427         */\r
428         function xmlrpc_debugmsg($m)\r
429         {\r
430                 $GLOBALS['_xmlrpc_debuginfo'] .= $m . "\n";\r
431         }\r
432 \r
433         class xmlrpc_server\r
434         {\r
435                 /**\r
436                 * Array defining php functions exposed as xmlrpc methods by this server\r
437                 * @access private\r
438                 */\r
439                 var $dmap=array();\r
440                 /**\r
441                 * Defines how functions in dmap will be invoked: either using an xmlrpc msg object\r
442                 * or plain php values.\r
443                 * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals'\r
444                 */\r
445                 var $functions_parameters_type='xmlrpcvals';\r
446                 /**\r
447                 * Option used for fine-tuning the encoding the php values returned from\r
448                 * functions registered in the dispatch map when the functions_parameters_types\r
449                 * member is set to 'phpvals'\r
450                 * @see php_xmlrpc_encode for a list of values\r
451                 */\r
452                 var $phpvals_encoding_options = array( 'auto_dates' );\r
453                 /// controls whether the server is going to echo debugging messages back to the client as comments in response body. valid values: 0,1,2,3\r
454                 var $debug = 1;\r
455                 /**\r
456                 * Controls behaviour of server when invoked user function throws an exception:\r
457                 * 0 = catch it and return an 'internal error' xmlrpc response (default)\r
458                 * 1 = catch it and return an xmlrpc response with the error corresponding to the exception\r
459                 * 2 = allow the exception to float to the upper layers\r
460                 */\r
461                 var $exception_handling = 0;\r
462                 /**\r
463                 * When set to true, it will enable HTTP compression of the response, in case\r
464                 * the client has declared its support for compression in the request.\r
465                 */\r
466                 var $compress_response = false;\r
467                 /**\r
468                 * List of http compression methods accepted by the server for requests.\r
469                 * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib\r
470                 */\r
471                 var $accepted_compression = array();\r
472                 /// shall we serve calls to system.* methods?\r
473                 var $allow_system_funcs = true;\r
474                 /// list of charset encodings natively accepted for requests\r
475                 var $accepted_charset_encodings = array();\r
476                 /**\r
477                 * charset encoding to be used for response.\r
478                 * NB: if we can, we will convert the generated response from internal_encoding to the intended one.\r
479                 * can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled),\r
480                 * null (leave unspecified in response, convert output stream to US_ASCII),\r
481                 * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed),\r
482                 * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway).\r
483                 * NB: pretty dangerous if you accept every charset and do not have mbstring enabled)\r
484                 */\r
485                 var $response_charset_encoding = '';\r
486                 /**\r
487                 * Storage for internal debug info\r
488                 * @access private\r
489                 */\r
490                 var $debug_info = '';\r
491                 /**\r
492                 * Extra data passed at runtime to method handling functions. Used only by EPI layer\r
493                 */\r
494                 var $user_data = null;\r
495 \r
496                 /**\r
497                 * @param array $dispmap the dispatch map with definition of exposed services\r
498                 * @param boolean $servicenow set to false to prevent the server from running upon construction\r
499                 */\r
500                 function __construct($dispMap=null, $serviceNow=true)\r
501                 {\r
502                         // if ZLIB is enabled, let the server by default accept compressed requests,\r
503                         // and compress responses sent to clients that support them\r
504                         if(function_exists('gzinflate'))\r
505                         {\r
506                                 $this->accepted_compression = array('gzip', 'deflate');\r
507                                 $this->compress_response = true;\r
508                         }\r
509 \r
510                         // by default the xml parser can support these 3 charset encodings\r
511                         $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');\r
512 \r
513                         // dispMap is a dispatch array of methods\r
514                         // mapped to function names and signatures\r
515                         // if a method\r
516                         // doesn't appear in the map then an unknown\r
517                         // method error is generated\r
518                         /* milosch - changed to make passing dispMap optional.\r
519                          * instead, you can use the class add_to_map() function\r
520                          * to add functions manually (borrowed from SOAPX4)\r
521                          */\r
522                         if($dispMap)\r
523                         {\r
524                                 $this->dmap = $dispMap;\r
525                                 if($serviceNow)\r
526                                 {\r
527                                         $this->service();\r
528                                 }\r
529                         }\r
530                 }\r
531 \r
532                 /**\r
533                 * @deprecated\r
534                 */\r
535                 function xmlrpc_client($dispMap=null, $serviceNow=true)\r
536                 {\r
537                         self::__construct($dispMap, $serviceNow);\r
538                 }\r
539 \r
540                 /**\r
541                 * Set debug level of server.\r
542                 * @param integer $in debug lvl: determines info added to xmlrpc responses (as xml comments)\r
543                 * 0 = no debug info,\r
544                 * 1 = msgs set from user with debugmsg(),\r
545                 * 2 = add complete xmlrpc request (headers and body),\r
546                 * 3 = add also all processing warnings happened during method processing\r
547                 * (NB: this involves setting a custom error handler, and might interfere\r
548                 * with the standard processing of the php function exposed as method. In\r
549                 * particular, triggering an USER_ERROR level error will not halt script\r
550                 * execution anymore, but just end up logged in the xmlrpc response)\r
551                 * Note that info added at level 2 and 3 will be base64 encoded\r
552                 * @access public\r
553                 */\r
554                 function setDebug($in)\r
555                 {\r
556                         $this->debug=$in;\r
557                 }\r
558 \r
559                 /**\r
560                 * Return a string with the serialized representation of all debug info\r
561                 * @param string $charset_encoding the target charset encoding for the serialization\r
562                 * @return string an XML comment (or two)\r
563                 */\r
564                 function serializeDebug($charset_encoding='')\r
565                 {\r
566                         // Tough encoding problem: which internal charset should we assume for debug info?\r
567                         // It might contain a copy of raw data received from client, ie with unknown encoding,\r
568                         // intermixed with php generated data and user generated data...\r
569                         // so we split it: system debug is base 64 encoded,\r
570                         // user debug info should be encoded by the end user using the INTERNAL_ENCODING\r
571                         $out = '';\r
572                         if ($this->debug_info != '')\r
573                         {\r
574                                 $out .= "<!-- SERVER DEBUG INFO (BASE64 ENCODED):\n".base64_encode($this->debug_info)."\n-->\n";\r
575                         }\r
576                         if($GLOBALS['_xmlrpc_debuginfo']!='')\r
577                         {\r
578 \r
579                                 $out .= "<!-- DEBUG INFO:\n" . xmlrpc_encode_entitites(str_replace('--', '_-', $GLOBALS['_xmlrpc_debuginfo']), $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "\n-->\n";\r
580                                 // NB: a better solution MIGHT be to use CDATA, but we need to insert it\r
581                                 // into return payload AFTER the beginning tag\r
582                                 //$out .= "<![CDATA[ DEBUG INFO:\n\n" . str_replace(']]>', ']_]_>', $GLOBALS['_xmlrpc_debuginfo']) . "\n]]>\n";\r
583                         }\r
584                         return $out;\r
585                 }\r
586 \r
587                 /**\r
588                 * Execute the xmlrpc request, printing the response\r
589                 * @param string $data the request body. If null, the http POST request will be examined\r
590                 * @return xmlrpcresp the response object (usually not used by caller...)\r
591                 * @access public\r
592                 */\r
593                 function service($data=null, $return_payload=false)\r
594                 {\r
595                         if ($data === null)\r
596                         {\r
597                                 // workaround for a known bug in php ver. 5.2.2 that broke $HTTP_RAW_POST_DATA\r
598                                 $data = file_get_contents('php://input');\r
599                         }\r
600                         $raw_data = $data;\r
601 \r
602                         // reset internal debug info\r
603                         $this->debug_info = '';\r
604 \r
605                         // Echo back what we received, before parsing it\r
606                         if($this->debug > 1)\r
607                         {\r
608                                 $this->debugmsg("+++GOT+++\n" . $data . "\n+++END+++");\r
609                         }\r
610 \r
611                         $r = $this->parseRequestHeaders($data, $req_charset, $resp_charset, $resp_encoding);\r
612                         if (!$r)\r
613                         {\r
614                                 $r=$this->parseRequest($data, $req_charset);\r
615                         }\r
616 \r
617                         // save full body of request into response, for more debugging usages\r
618                         $r->raw_data = $raw_data;\r
619 \r
620                         if($this->debug > 2 && $GLOBALS['_xmlrpcs_occurred_errors'])\r
621                         {\r
622                                 $this->debugmsg("+++PROCESSING ERRORS AND WARNINGS+++\n" .\r
623                                         $GLOBALS['_xmlrpcs_occurred_errors'] . "+++END+++");\r
624                         }\r
625 \r
626                         $payload=$this->xml_header($resp_charset);\r
627                         if($this->debug > 0)\r
628                         {\r
629                                 $payload = $payload . $this->serializeDebug($resp_charset);\r
630                         }\r
631 \r
632                         // G. Giunta 2006-01-27: do not create response serialization if it has\r
633                         // already happened. Helps building json magic\r
634                         if (empty($r->payload))\r
635                         {\r
636                                 $r->serialize($resp_charset);\r
637                         }\r
638                         $payload = $payload . $r->payload;\r
639 \r
640                         if ($return_payload)\r
641                         {\r
642                                 return $payload;\r
643                         }\r
644 \r
645                         // if we get a warning/error that has output some text before here, then we cannot\r
646                         // add a new header. We cannot say we are sending xml, either...\r
647                         if(!headers_sent())\r
648                         {\r
649                                 header('Content-Type: '.$r->content_type);\r
650                                 // we do not know if client actually told us an accepted charset, but if he did\r
651                                 // we have to tell him what we did\r
652                                 header("Vary: Accept-Charset");\r
653 \r
654                                 // http compression of output: only\r
655                                 // if we can do it, and we want to do it, and client asked us to,\r
656                                 // and php ini settings do not force it already\r
657                                 $php_no_self_compress = !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler');\r
658                                 if($this->compress_response && function_exists('gzencode') && $resp_encoding != ''\r
659                                         && $php_no_self_compress)\r
660                                 {\r
661                                         if(strpos($resp_encoding, 'gzip') !== false)\r
662                                         {\r
663                                                 $payload = gzencode($payload);\r
664                                                 header("Content-Encoding: gzip");\r
665                                                 header("Vary: Accept-Encoding");\r
666                                         }\r
667                                         elseif (strpos($resp_encoding, 'deflate') !== false)\r
668                                         {\r
669                                                 $payload = gzcompress($payload);\r
670                                                 header("Content-Encoding: deflate");\r
671                                                 header("Vary: Accept-Encoding");\r
672                                         }\r
673                                 }\r
674 \r
675                                 // do not ouput content-length header if php is compressing output for us:\r
676                                 // it will mess up measurements\r
677                                 if($php_no_self_compress)\r
678                                 {\r
679                                         header('Content-Length: ' . (int)strlen($payload));\r
680                                 }\r
681                         }\r
682                         else\r
683                         {\r
684                                 error_log('XML-RPC: '.__METHOD__.': http headers already sent before response is fully generated. Check for php warning or error messages');\r
685                         }\r
686 \r
687                         print $payload;\r
688 \r
689                         // return request, in case subclasses want it\r
690                         return $r;\r
691                 }\r
692 \r
693                 /**\r
694                 * Add a method to the dispatch map\r
695                 * @param string $methodname the name with which the method will be made available\r
696                 * @param string $function the php function that will get invoked\r
697                 * @param array $sig the array of valid method signatures\r
698                 * @param string $doc method documentation\r
699                 * @param array $sigdoc the array of valid method signatures docs (one string per param, one for return type)\r
700                 * @access public\r
701                 */\r
702                 function add_to_map($methodname,$function,$sig=null,$doc=false,$sigdoc=false)\r
703                 {\r
704                         $this->dmap[$methodname] = array(\r
705                                 'function'      => $function,\r
706                                 'docstring' => $doc\r
707                         );\r
708                         if ($sig)\r
709                         {\r
710                                 $this->dmap[$methodname]['signature'] = $sig;\r
711                         }\r
712                         if ($sigdoc)\r
713                         {\r
714                                 $this->dmap[$methodname]['signature_docs'] = $sigdoc;\r
715                         }\r
716                 }\r
717 \r
718                 /**\r
719                 * Verify type and number of parameters received against a list of known signatures\r
720                 * @param array $in array of either xmlrpcval objects or xmlrpc type definitions\r
721                 * @param array $sig array of known signatures to match against\r
722                 * @return array\r
723                 * @access private\r
724                 */\r
725                 function verifySignature($in, $sig)\r
726                 {\r
727                         // check each possible signature in turn\r
728                         if (is_object($in))\r
729                         {\r
730                                 $numParams = $in->getNumParams();\r
731                         }\r
732                         else\r
733                         {\r
734                                 $numParams = count($in);\r
735                         }\r
736                         foreach($sig as $cursig)\r
737                         {\r
738                                 if(count($cursig)==$numParams+1)\r
739                                 {\r
740                                         $itsOK=1;\r
741                                         for($n=0; $n<$numParams; $n++)\r
742                                         {\r
743                                                 if (is_object($in))\r
744                                                 {\r
745                                                         $p=$in->getParam($n);\r
746                                                         if($p->kindOf() == 'scalar')\r
747                                                         {\r
748                                                                 $pt=$p->scalartyp();\r
749                                                         }\r
750                                                         else\r
751                                                         {\r
752                                                                 $pt=$p->kindOf();\r
753                                                         }\r
754                                                 }\r
755                                                 else\r
756                                                 {\r
757                                                         $pt= $in[$n] == 'i4' ? 'int' : strtolower($in[$n]); // dispatch maps never use i4...\r
758                                                 }\r
759 \r
760                                                 // param index is $n+1, as first member of sig is return type\r
761                                                 if($pt != $cursig[$n+1] && $cursig[$n+1] != $GLOBALS['xmlrpcValue'])\r
762                                                 {\r
763                                                         $itsOK=0;\r
764                                                         $pno=$n+1;\r
765                                                         $wanted=$cursig[$n+1];\r
766                                                         $got=$pt;\r
767                                                         break;\r
768                                                 }\r
769                                         }\r
770                                         if($itsOK)\r
771                                         {\r
772                                                 return array(1,'');\r
773                                         }\r
774                                 }\r
775                         }\r
776                         if(isset($wanted))\r
777                         {\r
778                                 return array(0, "Wanted ${wanted}, got ${got} at param ${pno}");\r
779                         }\r
780                         else\r
781                         {\r
782                                 return array(0, "No method signature matches number of parameters");\r
783                         }\r
784                 }\r
785 \r
786                 /**\r
787                 * Parse http headers received along with xmlrpc request. If needed, inflate request\r
788                 * @return mixed null on success or an xmlrpcresp\r
789                 * @access private\r
790                 */\r
791                 function parseRequestHeaders(&$data, &$req_encoding, &$resp_encoding, &$resp_compression)\r
792                 {\r
793                         // check if $_SERVER is populated: it might have been disabled via ini file\r
794                         // (this is true even when in CLI mode)\r
795                         if (count($_SERVER) == 0)\r
796                         {\r
797                                 error_log('XML-RPC: '.__METHOD__.': cannot parse request headers as $_SERVER is not populated');\r
798                         }\r
799 \r
800                         if($this->debug > 1)\r
801                         {\r
802                                 if(function_exists('getallheaders'))\r
803                                 {\r
804                                         $this->debugmsg(''); // empty line\r
805                                         foreach(getallheaders() as $name => $val)\r
806                                         {\r
807                                                 $this->debugmsg("HEADER: $name: $val");\r
808                                         }\r
809                                 }\r
810 \r
811                         }\r
812 \r
813                         if(isset($_SERVER['HTTP_CONTENT_ENCODING']))\r
814                         {\r
815                                 $content_encoding = str_replace('x-', '', $_SERVER['HTTP_CONTENT_ENCODING']);\r
816                         }\r
817                         else\r
818                         {\r
819                                 $content_encoding = '';\r
820                         }\r
821 \r
822                         // check if request body has been compressed and decompress it\r
823                         if($content_encoding != '' && strlen($data))\r
824                         {\r
825                                 if($content_encoding == 'deflate' || $content_encoding == 'gzip')\r
826                                 {\r
827                                         // if decoding works, use it. else assume data wasn't gzencoded\r
828                                         if(function_exists('gzinflate') && in_array($content_encoding, $this->accepted_compression))\r
829                                         {\r
830                                                 if($content_encoding == 'deflate' && $degzdata = @gzuncompress($data))\r
831                                                 {\r
832                                                         $data = $degzdata;\r
833                                                         if($this->debug > 1)\r
834                                                         {\r
835                                                                 $this->debugmsg("\n+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++");\r
836                                                         }\r
837                                                 }\r
838                                                 elseif($content_encoding == 'gzip' && $degzdata = @gzinflate(substr($data, 10)))\r
839                                                 {\r
840                                                         $data = $degzdata;\r
841                                                         if($this->debug > 1)\r
842                                                                 $this->debugmsg("+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++");\r
843                                                 }\r
844                                                 else\r
845                                                 {\r
846                                                         $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_decompress_fail'], $GLOBALS['xmlrpcstr']['server_decompress_fail']);\r
847                                                         return $r;\r
848                                                 }\r
849                                         }\r
850                                         else\r
851                                         {\r
852                                                 //error_log('The server sent deflated data. Your php install must have the Zlib extension compiled in to support this.');\r
853                                                 $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_cannot_decompress'], $GLOBALS['xmlrpcstr']['server_cannot_decompress']);\r
854                                                 return $r;\r
855                                         }\r
856                                 }\r
857                         }\r
858 \r
859                         // check if client specified accepted charsets, and if we know how to fulfill\r
860                         // the request\r
861                         if ($this->response_charset_encoding == 'auto')\r
862                         {\r
863                                 $resp_encoding = '';\r
864                                 if (isset($_SERVER['HTTP_ACCEPT_CHARSET']))\r
865                                 {\r
866                                         // here we should check if we can match the client-requested encoding\r
867                                         // with the encodings we know we can generate.\r
868                                         /// @todo we should parse q=0.x preferences instead of getting first charset specified...\r
869                                         $client_accepted_charsets = explode(',', strtoupper($_SERVER['HTTP_ACCEPT_CHARSET']));\r
870                                         // Give preference to internal encoding\r
871                                         $known_charsets = array($GLOBALS['xmlrpc_internalencoding'], 'UTF-8', 'ISO-8859-1', 'US-ASCII');\r
872                                         foreach ($known_charsets as $charset)\r
873                                         {\r
874                                                 foreach ($client_accepted_charsets as $accepted)\r
875                                                         if (strpos($accepted, $charset) === 0)\r
876                                                         {\r
877                                                                 $resp_encoding = $charset;\r
878                                                                 break;\r
879                                                         }\r
880                                                 if ($resp_encoding)\r
881                                                         break;\r
882                                         }\r
883                                 }\r
884                         }\r
885                         else\r
886                         {\r
887                                 $resp_encoding = $this->response_charset_encoding;\r
888                         }\r
889 \r
890                         if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))\r
891                         {\r
892                                 $resp_compression = $_SERVER['HTTP_ACCEPT_ENCODING'];\r
893                         }\r
894                         else\r
895                         {\r
896                                 $resp_compression = '';\r
897                         }\r
898 \r
899                         // 'guestimate' request encoding\r
900                         /// @todo check if mbstring is enabled and automagic input conversion is on: it might mingle with this check???\r
901                         $req_encoding = guess_encoding(isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '',\r
902                                 $data);\r
903 \r
904                         return null;\r
905                 }\r
906 \r
907                 /**\r
908                 * Parse an xml chunk containing an xmlrpc request and execute the corresponding\r
909                 * php function registered with the server\r
910                 * @param string $data the xml request\r
911                 * @param string $req_encoding (optional) the charset encoding of the xml request\r
912                 * @return xmlrpcresp\r
913                 * @access private\r
914                 */\r
915                 function parseRequest($data, $req_encoding='')\r
916                 {\r
917                         // 2005/05/07 commented and moved into caller function code\r
918                         //if($data=='')\r
919                         //{\r
920                         //      $data=$GLOBALS['HTTP_RAW_POST_DATA'];\r
921                         //}\r
922 \r
923                         // G. Giunta 2005/02/13: we do NOT expect to receive html entities\r
924                         // so we do not try to convert them into xml character entities\r
925                         //$data = xmlrpc_html_entity_xlate($data);\r
926 \r
927                         $GLOBALS['_xh']=array();\r
928                         $GLOBALS['_xh']['ac']='';\r
929                         $GLOBALS['_xh']['stack']=array();\r
930                         $GLOBALS['_xh']['valuestack'] = array();\r
931                         $GLOBALS['_xh']['params']=array();\r
932                         $GLOBALS['_xh']['pt']=array();\r
933                         $GLOBALS['_xh']['isf']=0;\r
934                         $GLOBALS['_xh']['isf_reason']='';\r
935                         $GLOBALS['_xh']['method']=false; // so we can check later if we got a methodname or not\r
936                         $GLOBALS['_xh']['rt']='';\r
937 \r
938                         // decompose incoming XML into request structure\r
939 \r
940                         if ($req_encoding != '')\r
941                         {\r
942                                 // Since parsing will fail if charset is not specified in the xml prologue,\r
943                                 // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...\r
944                                 // The following code might be better for mb_string enabled installs, but\r
945                                 // makes the lib about 200% slower...\r
946                                 //if (!is_valid_charset($req_encoding, array('UTF-8')))\r
947                                 if (!in_array($req_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) {\r
948                                         if ($req_encoding == 'ISO-8859-1') {\r
949                                                 $data = utf8_encode($data);\r
950                                         } else {\r
951                                                 if (extension_loaded('mbstring')) {\r
952                                                         $data = mb_convert_encoding($data, 'UTF-8', $req_encoding);\r
953                                                 } else {\r
954                                                         error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $req_encoding);\r
955                                                 }\r
956                                         }\r
957                                 }\r
958                         }\r
959 \r
960                         $parser = xml_parser_create();\r
961                         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);\r
962                         // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell\r
963                         // the xml parser to give us back data in the expected charset\r
964                         // What if internal encoding is not in one of the 3 allowed?\r
965                         // we use the broadest one, ie. utf8\r
966                         // This allows to send data which is native in various charset,\r
967                         // by extending xmlrpc_encode_entitites() and setting xmlrpc_internalencoding\r
968                         if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII')))\r
969                         {\r
970                                 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');\r
971                         }\r
972                         else\r
973                         {\r
974                                 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']);\r
975                         }\r
976 \r
977                         if ($this->functions_parameters_type != 'xmlrpcvals')\r
978                                 xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast');\r
979                         else\r
980                                 xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');\r
981                         xml_set_character_data_handler($parser, 'xmlrpc_cd');\r
982                         xml_set_default_handler($parser, 'xmlrpc_dh');\r
983                         if(!xml_parse($parser, $data, 1))\r
984                         {\r
985                                 // return XML error as a faultCode\r
986                                 $r=new xmlrpcresp(0,\r
987                                 $GLOBALS['xmlrpcerrxml']+xml_get_error_code($parser),\r
988                                 sprintf('XML error: %s at line %d, column %d',\r
989                                         xml_error_string(xml_get_error_code($parser)),\r
990                                         xml_get_current_line_number($parser), xml_get_current_column_number($parser)));\r
991                                 xml_parser_free($parser);\r
992                         }\r
993                         elseif ($GLOBALS['_xh']['isf'])\r
994                         {\r
995                                 xml_parser_free($parser);\r
996                                 $r=new xmlrpcresp(0,\r
997                                         $GLOBALS['xmlrpcerr']['invalid_request'],\r
998                                         $GLOBALS['xmlrpcstr']['invalid_request'] . ' ' . $GLOBALS['_xh']['isf_reason']);\r
999                         }\r
1000                         else\r
1001                         {\r
1002                                 xml_parser_free($parser);\r
1003                                 // small layering violation in favor of speed and memory usage:\r
1004                                 // we should allow the 'execute' method handle this, but in the\r
1005                                 // most common scenario (xmlrpcvals type server with some methods\r
1006                                 // registered as phpvals) that would mean a useless encode+decode pass\r
1007                                 if ($this->functions_parameters_type != 'xmlrpcvals' || (isset($this->dmap[$GLOBALS['_xh']['method']]['parameters_type']) && ($this->dmap[$GLOBALS['_xh']['method']]['parameters_type'] == 'phpvals')))\r
1008                                 {\r
1009                                         if($this->debug > 1)\r
1010                                         {\r
1011                                                 $this->debugmsg("\n+++PARSED+++\n".var_export($GLOBALS['_xh']['params'], true)."\n+++END+++");\r
1012                                         }\r
1013                                         $r = $this->execute($GLOBALS['_xh']['method'], $GLOBALS['_xh']['params'], $GLOBALS['_xh']['pt']);\r
1014                                 }\r
1015                                 else\r
1016                                 {\r
1017                                         // build an xmlrpcmsg object with data parsed from xml\r
1018                                         $m=new xmlrpcmsg($GLOBALS['_xh']['method']);\r
1019                                         // now add parameters in\r
1020                                         for($i=0; $i<count($GLOBALS['_xh']['params']); $i++)\r
1021                                         {\r
1022                                                 $m->addParam($GLOBALS['_xh']['params'][$i]);\r
1023                                         }\r
1024 \r
1025                                         if($this->debug > 1)\r
1026                                         {\r
1027                                                 $this->debugmsg("\n+++PARSED+++\n".var_export($m, true)."\n+++END+++");\r
1028                                         }\r
1029                                         $r = $this->execute($m);\r
1030                                 }\r
1031                         }\r
1032                         return $r;\r
1033                 }\r
1034 \r
1035                 /**\r
1036                 * Execute a method invoked by the client, checking parameters used\r
1037                 * @param mixed $m either an xmlrpcmsg obj or a method name\r
1038                 * @param array $params array with method parameters as php types (if m is method name only)\r
1039                 * @param array $paramtypes array with xmlrpc types of method parameters (if m is method name only)\r
1040                 * @return xmlrpcresp\r
1041                 * @access private\r
1042                 */\r
1043                 function execute($m, $params=null, $paramtypes=null)\r
1044                 {\r
1045                         if (is_object($m))\r
1046                         {\r
1047                                 $methName = $m->method();\r
1048                         }\r
1049                         else\r
1050                         {\r
1051                                 $methName = $m;\r
1052                         }\r
1053                         $sysCall = $this->allow_system_funcs && (strpos($methName, "system.") === 0);\r
1054                         $dmap = $sysCall ? $GLOBALS['_xmlrpcs_dmap'] : $this->dmap;\r
1055 \r
1056                         if(!isset($dmap[$methName]['function']))\r
1057                         {\r
1058                                 // No such method\r
1059                                 return new xmlrpcresp(0,\r
1060                                         $GLOBALS['xmlrpcerr']['unknown_method'],\r
1061                                         $GLOBALS['xmlrpcstr']['unknown_method']);\r
1062                         }\r
1063 \r
1064                         // Check signature\r
1065                         if(isset($dmap[$methName]['signature']))\r
1066                         {\r
1067                                 $sig = $dmap[$methName]['signature'];\r
1068                                 if (is_object($m))\r
1069                                 {\r
1070                                         list($ok, $errstr) = $this->verifySignature($m, $sig);\r
1071                                 }\r
1072                                 else\r
1073                                 {\r
1074                                         list($ok, $errstr) = $this->verifySignature($paramtypes, $sig);\r
1075                                 }\r
1076                                 if(!$ok)\r
1077                                 {\r
1078                                         // Didn't match.\r
1079                                         return new xmlrpcresp(\r
1080                                                 0,\r
1081                                                 $GLOBALS['xmlrpcerr']['incorrect_params'],\r
1082                                                 $GLOBALS['xmlrpcstr']['incorrect_params'] . ": ${errstr}"\r
1083                                         );\r
1084                                 }\r
1085                         }\r
1086 \r
1087                         $func = $dmap[$methName]['function'];\r
1088                         // let the 'class::function' syntax be accepted in dispatch maps\r
1089                         if(is_string($func) && strpos($func, '::'))\r
1090                         {\r
1091                                 $func = explode('::', $func);\r
1092                         }\r
1093                         // verify that function to be invoked is in fact callable\r
1094                         if(!is_callable($func))\r
1095                         {\r
1096                                 error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler is not callable");\r
1097                                 return new xmlrpcresp(\r
1098                                         0,\r
1099                                         $GLOBALS['xmlrpcerr']['server_error'],\r
1100                                         $GLOBALS['xmlrpcstr']['server_error'] . ": no function matches method"\r
1101                                 );\r
1102                         }\r
1103 \r
1104                         // If debug level is 3, we should catch all errors generated during\r
1105                         // processing of user function, and log them as part of response\r
1106                         if($this->debug > 2)\r
1107                         {\r
1108                                 $GLOBALS['_xmlrpcs_prev_ehandler'] = set_error_handler('_xmlrpcs_errorHandler');\r
1109                         }\r
1110                         try\r
1111                         {\r
1112                                 // Allow mixed-convention servers\r
1113                                 if (is_object($m))\r
1114                                 {\r
1115                                         if($sysCall)\r
1116                                         {\r
1117                                                 $r = call_user_func($func, $this, $m);\r
1118                                         }\r
1119                                         else\r
1120                                         {\r
1121                                                 $r = call_user_func($func, $m);\r
1122                                         }\r
1123                                         if (!is_a($r, 'xmlrpcresp'))\r
1124                                         {\r
1125                                                 error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler does not return an xmlrpcresp object");\r
1126                                                 if (is_a($r, 'xmlrpcval'))\r
1127                                                 {\r
1128                                                         $r = new xmlrpcresp($r);\r
1129                                                 }\r
1130                                                 else\r
1131                                                 {\r
1132                                                         $r = new xmlrpcresp(\r
1133                                                                 0,\r
1134                                                                 $GLOBALS['xmlrpcerr']['server_error'],\r
1135                                                                 $GLOBALS['xmlrpcstr']['server_error'] . ": function does not return xmlrpcresp object"\r
1136                                                         );\r
1137                                                 }\r
1138                                         }\r
1139                                 }\r
1140                                 else\r
1141                                 {\r
1142                                         // call a 'plain php' function\r
1143                                         if($sysCall)\r
1144                                         {\r
1145                                                 array_unshift($params, $this);\r
1146                                                 $r = call_user_func_array($func, $params);\r
1147                                         }\r
1148                                         else\r
1149                                         {\r
1150                                                 // 3rd API convention for method-handling functions: EPI-style\r
1151                                                 if ($this->functions_parameters_type == 'epivals')\r
1152                                                 {\r
1153                                                         $r = call_user_func_array($func, array($methName, $params, $this->user_data));\r
1154                                                         // mimic EPI behaviour: if we get an array that looks like an error, make it\r
1155                                                         // an eror response\r
1156                                                         if (is_array($r) && array_key_exists('faultCode', $r) && array_key_exists('faultString', $r))\r
1157                                                         {\r
1158                                                                 $r = new xmlrpcresp(0, (integer)$r['faultCode'], (string)$r['faultString']);\r
1159                                                         }\r
1160                                                         else\r
1161                                                         {\r
1162                                                                 // functions using EPI api should NOT return resp objects,\r
1163                                                                 // so make sure we encode the return type correctly\r
1164                                                                 $r = new xmlrpcresp(php_xmlrpc_encode($r, array('extension_api')));\r
1165                                                         }\r
1166                                                 }\r
1167                                                 else\r
1168                                                 {\r
1169                                                         $r = call_user_func_array($func, $params);\r
1170                                                 }\r
1171                                         }\r
1172                                         // the return type can be either an xmlrpcresp object or a plain php value...\r
1173                                         if (!is_a($r, 'xmlrpcresp'))\r
1174                                         {\r
1175                                                 // what should we assume here about automatic encoding of datetimes\r
1176                                                 // and php classes instances???\r
1177                                                 $r = new xmlrpcresp(php_xmlrpc_encode($r, $this->phpvals_encoding_options));\r
1178                                         }\r
1179                                 }\r
1180                         }\r
1181                         catch(Exception $e)\r
1182                         {\r
1183                                 // (barring errors in the lib) an uncatched exception happened\r
1184                                 // in the called function, we wrap it in a proper error-response\r
1185                                 switch($this->exception_handling)\r
1186                                 {\r
1187                                         case 2:\r
1188                                                 throw $e;\r
1189                                                 break;\r
1190                                         case 1:\r
1191                                                 $r = new xmlrpcresp(0, $e->getCode(), $e->getMessage());\r
1192                                                 break;\r
1193                                         default:\r
1194                                                 $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_error'], $GLOBALS['xmlrpcstr']['server_error']);\r
1195                                 }\r
1196                         }\r
1197                         if($this->debug > 2)\r
1198                         {\r
1199                                 // note: restore the error handler we found before calling the\r
1200                                 // user func, even if it has been changed inside the func itself\r
1201                                 if($GLOBALS['_xmlrpcs_prev_ehandler'])\r
1202                                 {\r
1203                                         set_error_handler($GLOBALS['_xmlrpcs_prev_ehandler']);\r
1204                                 }\r
1205                                 else\r
1206                                 {\r
1207                                         restore_error_handler();\r
1208                                 }\r
1209                         }\r
1210                         return $r;\r
1211                 }\r
1212 \r
1213                 /**\r
1214                 * add a string to the 'internal debug message' (separate from 'user debug message')\r
1215                 * @param string $string\r
1216                 * @access private\r
1217                 */\r
1218                 function debugmsg($string)\r
1219                 {\r
1220                         $this->debug_info .= $string."\n";\r
1221                 }\r
1222 \r
1223                 /**\r
1224                 * @access private\r
1225                 */\r
1226                 function xml_header($charset_encoding='')\r
1227                 {\r
1228                         if ($charset_encoding != '')\r
1229                         {\r
1230                                 return "<?xml version=\"1.0\" encoding=\"$charset_encoding\"?" . ">\n";\r
1231                         }\r
1232                         else\r
1233                         {\r
1234                                 return "<?xml version=\"1.0\"?" . ">\n";\r
1235                         }\r
1236                 }\r
1237 \r
1238                 /**\r
1239                 * A debugging routine: just echoes back the input packet as a string value\r
1240                 * @deprecated\r
1241                 */\r
1242                 function echoInput()\r
1243                 {\r
1244                         $r=new xmlrpcresp(new xmlrpcval( "'Aha said I: '" . $GLOBALS['HTTP_RAW_POST_DATA'], 'string'));\r
1245                         print $r->serialize();\r
1246                 }\r
1247         }\r
1248 ?>