OSDN Git Service

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