OSDN Git Service

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