13 protected $dmap = array();
19 public $functions_parameters_type =
'xmlrpcvals';
26 public $phpvals_encoding_options = array(
'auto_dates');
38 public $exception_handling = 0;
44 public $compress_response =
false;
49 public $accepted_compression = array();
51 public $allow_system_funcs =
true;
57 public $accepted_charset_encodings = array();
67 public $response_charset_encoding =
'';
71 protected $debug_info =
'';
75 public $user_data =
null;
77 protected static $_xmlrpc_debuginfo =
'';
78 protected static $_xmlrpcs_occurred_errors =
'';
79 protected static $_xmlrpcs_prev_ehandler =
'';
85 public function __construct($dispatchMap =
null, $serviceNow =
true)
89 if (function_exists(
'gzinflate')) {
90 $this->accepted_compression = array(
'gzip',
'deflate');
91 $this->compress_response =
true;
95 $this->accepted_charset_encodings = array(
'UTF-8',
'ISO-8859-1',
'US-ASCII');
104 $this->dmap = $dispatchMap;
127 $this->debug = $level;
141 static::$_xmlrpc_debuginfo .= $msg .
"\n";
146 static::$_xmlrpcs_occurred_errors .= $msg .
"\n";
164 if ($this->debug_info !=
'') {
165 $out .=
"<!-- SERVER DEBUG INFO (BASE64 ENCODED):\n" . base64_encode($this->debug_info) .
"\n-->\n";
167 if (static::$_xmlrpc_debuginfo !=
'') {
168 $out .=
"<!-- DEBUG INFO:\n" . Charset::instance()->encodeEntities(str_replace(
'--',
'_-', static::$_xmlrpc_debuginfo), PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) .
"\n-->\n";
187 public function service($data =
null, $returnPayload =
false)
189 if ($data ===
null) {
190 $data = file_get_contents(
'php://input');
195 $this->debug_info =
'';
198 if ($this->debug > 1) {
199 $this->debugmsg(
"+++GOT+++\n" . $data .
"\n+++END+++");
202 $r = $this->parseRequestHeaders($data, $reqCharset, $respCharset, $respEncoding);
205 $r = $this->parseRequest($data, $reqCharset);
209 $r->raw_data = $rawData;
211 if ($this->debug > 2 && static::$_xmlrpcs_occurred_errors) {
212 $this->debugmsg(
"+++PROCESSING ERRORS AND WARNINGS+++\n" .
213 static::$_xmlrpcs_occurred_errors .
"+++END+++");
216 $payload = $this->xml_header($respCharset);
217 if ($this->debug > 0) {
218 $payload = $payload . $this->serializeDebug($respCharset);
223 if (empty($r->payload)) {
224 $r->serialize($respCharset);
226 $payload = $payload . $r->payload;
228 if ($returnPayload) {
234 if (!headers_sent()) {
235 header(
'Content-Type: ' . $r->content_type);
238 header(
"Vary: Accept-Charset");
243 $phpNoSelfCompress = !ini_get(
'zlib.output_compression') && (ini_get(
'output_handler') !=
'ob_gzhandler');
244 if ($this->compress_response && function_exists(
'gzencode') && $respEncoding !=
''
245 && $phpNoSelfCompress
247 if (strpos($respEncoding,
'gzip') !==
false) {
248 $payload = gzencode($payload);
249 header(
"Content-Encoding: gzip");
250 header(
"Vary: Accept-Encoding");
251 } elseif (strpos($respEncoding,
'deflate') !==
false) {
252 $payload = gzcompress($payload);
253 header(
"Content-Encoding: deflate");
254 header(
"Vary: Accept-Encoding");
260 if ($phpNoSelfCompress) {
261 header(
'Content-Length: ' . (
int)strlen($payload));
264 error_log(
'XML-RPC: ' . __METHOD__ .
': http headers already sent before response is fully generated. Check for php warning or error messages');
282 public function add_to_map($methodName, $function, $sig =
null, $doc =
false, $sigDoc =
false)
284 $this->dmap[$methodName] = array(
285 'function' => $function,
289 $this->dmap[$methodName][
'signature'] = $sig;
292 $this->dmap[$methodName][
'signature_docs'] = $sigDoc;
307 if (is_object($in)) {
308 $numParams = $in->getNumParams();
310 $numParams = count($in);
312 foreach ($sigs as $curSig) {
313 if (count($curSig) == $numParams + 1) {
315 for ($n = 0; $n < $numParams; $n++) {
316 if (is_object($in)) {
317 $p = $in->getParam($n);
318 if ($p->kindOf() ==
'scalar') {
319 $pt = $p->scalartyp();
324 $pt = $in[$n] ==
'i4' ?
'int' : strtolower($in[$n]);
328 if ($pt != $curSig[$n + 1] && $curSig[$n + 1] != Value::$xmlrpcValue) {
331 $wanted = $curSig[$n + 1];
341 if (isset($wanted)) {
342 return array(0,
"Wanted ${wanted}, got ${got} at param ${pno}");
344 return array(0,
"No method signature matches number of parameters");
357 if (count($_SERVER) == 0) {
358 error_log(
'XML-RPC: ' . __METHOD__ .
': cannot parse request headers as $_SERVER is not populated');
361 if ($this->debug > 1) {
362 if (function_exists(
'getallheaders')) {
364 foreach (getallheaders() as $name => $val) {
365 $this->debugmsg(
"HEADER: $name: $val");
370 if (isset($_SERVER[
'HTTP_CONTENT_ENCODING'])) {
371 $contentEncoding = str_replace(
'x-',
'', $_SERVER[
'HTTP_CONTENT_ENCODING']);
373 $contentEncoding =
'';
377 if ($contentEncoding !=
'' && strlen($data)) {
378 if ($contentEncoding ==
'deflate' || $contentEncoding ==
'gzip') {
380 if (function_exists(
'gzinflate') && in_array($contentEncoding, $this->accepted_compression)) {
381 if ($contentEncoding ==
'deflate' && $degzdata = @gzuncompress($data)) {
383 if ($this->debug > 1) {
384 $this->debugmsg(
"\n+++INFLATED REQUEST+++[" . strlen($data) .
" chars]+++\n" . $data .
"\n+++END+++");
386 } elseif ($contentEncoding ==
'gzip' && $degzdata = @gzinflate(substr($data, 10))) {
388 if ($this->debug > 1) {
389 $this->debugmsg(
"+++INFLATED REQUEST+++[" . strlen($data) .
" chars]+++\n" . $data .
"\n+++END+++");
392 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'server_decompress_fail'], PhpXmlRpc::$xmlrpcstr[
'server_decompress_fail']);
397 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'server_cannot_decompress'], PhpXmlRpc::$xmlrpcstr[
'server_cannot_decompress']);
406 if ($this->response_charset_encoding ==
'auto') {
408 if (isset($_SERVER[
'HTTP_ACCEPT_CHARSET'])) {
412 $clientAcceptedCharsets = explode(
',', strtoupper($_SERVER[
'HTTP_ACCEPT_CHARSET']));
414 $knownCharsets = array(PhpXmlRpc::$xmlrpc_internalencoding,
'UTF-8',
'ISO-8859-1',
'US-ASCII');
415 foreach ($knownCharsets as $charset) {
416 foreach ($clientAcceptedCharsets as $accepted) {
417 if (strpos($accepted, $charset) === 0) {
418 $respEncoding = $charset;
428 $respEncoding = $this->response_charset_encoding;
431 if (isset($_SERVER[
'HTTP_ACCEPT_ENCODING'])) {
432 $respCompression = $_SERVER[
'HTTP_ACCEPT_ENCODING'];
434 $respCompression =
'';
439 $reqEncoding = XMLParser::guessEncoding(isset($_SERVER[
'CONTENT_TYPE']) ? $_SERVER[
'CONTENT_TYPE'] :
'',
460 if ($reqEncoding !=
'') {
466 if (!in_array($reqEncoding, array(
'UTF-8',
'US-ASCII')) && !XMLParser::hasEncoding($data)) {
467 if ($reqEncoding ==
'ISO-8859-1') {
468 $data = utf8_encode($data);
470 if (extension_loaded(
'mbstring')) {
471 $data = mb_convert_encoding($data,
'UTF-8', $reqEncoding);
473 error_log(
'XML-RPC: ' . __METHOD__ .
': invalid charset encoding of received request: ' . $reqEncoding);
479 $parser = xml_parser_create();
480 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,
true);
487 if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array(
'UTF-8',
'ISO-8859-1',
'US-ASCII'))) {
488 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING,
'UTF-8');
490 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, PhpXmlRpc::$xmlrpc_internalencoding);
494 xml_set_object($parser, $xmlRpcParser);
496 if ($this->functions_parameters_type !=
'xmlrpcvals') {
497 xml_set_element_handler($parser,
'xmlrpc_se',
'xmlrpc_ee_fast');
499 xml_set_element_handler($parser,
'xmlrpc_se',
'xmlrpc_ee');
501 xml_set_character_data_handler($parser,
'xmlrpc_cd');
502 xml_set_default_handler($parser,
'xmlrpc_dh');
503 if (!xml_parse($parser, $data, 1)) {
506 PhpXmlRpc::$xmlrpcerrxml + xml_get_error_code($parser),
507 sprintf(
'XML error: %s at line %d, column %d',
508 xml_error_string(xml_get_error_code($parser)),
509 xml_get_current_line_number($parser), xml_get_current_column_number($parser)));
510 xml_parser_free($parser);
511 } elseif ($xmlRpcParser->_xh[
'isf']) {
512 xml_parser_free($parser);
514 PhpXmlRpc::$xmlrpcerr[
'invalid_request'],
515 PhpXmlRpc::$xmlrpcstr[
'invalid_request'] .
' ' . $xmlRpcParser->_xh[
'isf_reason']);
517 xml_parser_free($parser);
522 if ($this->functions_parameters_type !=
'xmlrpcvals' || (isset($this->dmap[$xmlRpcParser->_xh[
'method']][
'parameters_type']) && ($this->dmap[$xmlRpcParser->_xh[
'method']][
'parameters_type'] ==
'phpvals'))) {
523 if ($this->debug > 1) {
524 $this->debugmsg(
"\n+++PARSED+++\n" . var_export($xmlRpcParser->_xh[
'params'],
true) .
"\n+++END+++");
526 $r = $this->execute($xmlRpcParser->_xh[
'method'], $xmlRpcParser->_xh[
'params'], $xmlRpcParser->_xh[
'pt']);
529 $req =
new Request($xmlRpcParser->_xh[
'method']);
531 for ($i = 0; $i < count($xmlRpcParser->_xh[
'params']); $i++) {
532 $req->addParam($xmlRpcParser->_xh[
'params'][$i]);
535 if ($this->debug > 1) {
536 $this->debugmsg(
"\n+++PARSED+++\n" . var_export($req,
true) .
"\n+++END+++");
538 $r = $this->execute($req);
556 protected function execute($req, $params =
null, $paramTypes =
null)
558 static::$_xmlrpcs_occurred_errors =
'';
559 static::$_xmlrpc_debuginfo =
'';
561 if (is_object($req)) {
562 $methName = $req->method();
566 $sysCall = $this->allow_system_funcs && (strpos($methName,
"system.") === 0);
567 $dmap = $sysCall ? $this->getSystemDispatchMap() : $this->dmap;
569 if (!isset($dmap[$methName][
'function'])) {
572 PhpXmlRpc::$xmlrpcerr[
'unknown_method'],
573 PhpXmlRpc::$xmlrpcstr[
'unknown_method']);
577 if (isset($dmap[$methName][
'signature'])) {
578 $sig = $dmap[$methName][
'signature'];
579 if (is_object($req)) {
580 list($ok, $errStr) = $this->verifySignature($req, $sig);
582 list($ok, $errStr) = $this->verifySignature($paramTypes, $sig);
588 PhpXmlRpc::$xmlrpcerr[
'incorrect_params'],
589 PhpXmlRpc::$xmlrpcstr[
'incorrect_params'] .
": ${errStr}"
594 $func = $dmap[$methName][
'function'];
596 if (is_string($func) && strpos($func,
'::')) {
597 $func = explode(
'::', $func);
600 if (is_array($func)) {
601 if (is_object($func[0])) {
602 $funcName = get_class($func[0]) .
'->' . $func[1];
604 $funcName = implode(
'::', $func);
606 }
else if ($func instanceof \Closure) {
607 $funcName =
'Closure';
613 if (!is_callable($func)) {
614 error_log(
"XML-RPC: " . __METHOD__ .
": function '$funcName' registered as method handler is not callable");
617 PhpXmlRpc::$xmlrpcerr[
'server_error'],
618 PhpXmlRpc::$xmlrpcstr[
'server_error'] .
": no function matches method"
624 if ($this->debug > 2) {
625 self::$_xmlrpcs_prev_ehandler = set_error_handler(array(
'\PhpXmlRpc\Server',
'_xmlrpcs_errorHandler'));
630 if (is_object($req)) {
632 $r = call_user_func($func, $this, $req);
634 $r = call_user_func($func, $req);
636 if (!is_a($r,
'PhpXmlRpc\Response')) {
637 error_log(
"XML-RPC: " . __METHOD__ .
": function '$funcName' registered as method handler does not return an xmlrpc response object but a " . gettype($r));
638 if (is_a($r,
'PhpXmlRpc\Value')) {
643 PhpXmlRpc::$xmlrpcerr[
'server_error'],
644 PhpXmlRpc::$xmlrpcstr[
'server_error'] .
": function does not return xmlrpc response object"
651 array_unshift($params, $this);
652 $r = call_user_func_array($func, $params);
655 if ($this->functions_parameters_type ==
'epivals') {
656 $r = call_user_func_array($func, array($methName, $params, $this->user_data));
659 if (is_array($r) && array_key_exists(
'faultCode', $r) && array_key_exists(
'faultString', $r)) {
660 $r =
new Response(0, (integer)$r[
'faultCode'], (
string)$r[
'faultString']);
664 $r =
new Response(php_xmlrpc_encode($r, array(
'extension_api')));
667 $r = call_user_func_array($func, $params);
671 if (!is_a($r,
'\PhpXmlRpc\Response')) {
674 $r =
new Response(php_xmlrpc_encode($r, $this->phpvals_encoding_options));
677 }
catch (\Exception $e) {
680 switch ($this->exception_handling) {
682 if ($this->debug > 2) {
683 if (self::$_xmlrpcs_prev_ehandler) {
684 set_error_handler(self::$_xmlrpcs_prev_ehandler);
686 restore_error_handler();
692 $r =
new Response(0, $e->getCode(), $e->getMessage());
695 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'server_error'], PhpXmlRpc::$xmlrpcstr[
'server_error']);
698 if ($this->debug > 2) {
701 if (self::$_xmlrpcs_prev_ehandler) {
702 set_error_handler(self::$_xmlrpcs_prev_ehandler);
704 restore_error_handler();
718 $this->debug_info .= $string .
"\n";
727 if ($charsetEncoding !=
'') {
728 return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\"?" .
">\n";
730 return "<?xml version=\"1.0\"?" .
">\n";
742 'system.listMethods' => array(
743 'function' =>
'PhpXmlRpc\Server::_xmlrpcs_listMethods',
746 'signature' => array(array(Value::$xmlrpcArray)),
747 'docstring' =>
'This method lists all the methods that the XML-RPC server knows how to dispatch',
748 'signature_docs' => array(array(
'list of method names')),
750 'system.methodHelp' => array(
751 'function' =>
'PhpXmlRpc\Server::_xmlrpcs_methodHelp',
752 'signature' => array(array(Value::$xmlrpcString, Value::$xmlrpcString)),
753 'docstring' =>
'Returns help text if defined for the method passed, otherwise returns an empty string',
754 'signature_docs' => array(array(
'method description',
'name of the method to be described')),
756 'system.methodSignature' => array(
757 'function' =>
'PhpXmlRpc\Server::_xmlrpcs_methodSignature',
758 'signature' => array(array(Value::$xmlrpcArray, Value::$xmlrpcString)),
759 'docstring' =>
'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)',
760 'signature_docs' => array(array(
'list of known signatures, each sig being an array of xmlrpc type names',
'name of method to be described')),
762 'system.multicall' => array(
763 'function' =>
'PhpXmlRpc\Server::_xmlrpcs_multicall',
764 'signature' => array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)),
765 'docstring' =>
'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details',
766 'signature_docs' => 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"')),
768 'system.getCapabilities' => array(
769 'function' =>
'PhpXmlRpc\Server::_xmlrpcs_getCapabilities',
770 'signature' => array(array(Value::$xmlrpcStruct)),
771 'docstring' =>
'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',
772 'signature_docs' => array(array(
'list of capabilities, described as structs with a version number and url for the spec')),
785 'specUrl' =>
'http://www.xmlrpc.com/spec',
790 'system.multicall' => array(
791 'specUrl' =>
'http://www.xmlrpc.com/discuss/msgReader$1208',
795 'introspection' => array(
796 'specUrl' =>
'http://phpxmlrpc.sourceforge.net/doc-2/ch10.html',
802 if (PhpXmlRpc::$xmlrpc_null_extension) {
803 $outAr[
'nil'] = array(
804 'specUrl' =>
'http://www.ontosys.com/xml-rpc/extensions.php',
815 return new Response($encoder->encode($server->getCapabilities()));
821 foreach ($server->dmap as $key => $val) {
822 $outAr[] =
new Value($key,
'string');
824 if ($server->allow_system_funcs) {
825 foreach ($server->getSystemDispatchMap() as $key => $val) {
826 $outAr[] =
new Value($key,
'string');
836 if (is_object($req)) {
837 $methName = $req->getParam(0);
838 $methName = $methName->scalarval();
842 if (strpos($methName,
"system.") === 0) {
843 $dmap = $server->getSystemDispatchMap();
845 $dmap = $server->dmap;
847 if (isset($dmap[$methName])) {
848 if (isset($dmap[$methName][
'signature'])) {
850 foreach ($dmap[$methName][
'signature'] as $inSig) {
852 foreach ($inSig as $sig) {
853 $curSig[] =
new Value($sig,
'string');
855 $sigs[] =
new Value($curSig,
'array');
864 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'introspect_unknown'], PhpXmlRpc::$xmlrpcstr[
'introspect_unknown']);
873 if (is_object($req)) {
874 $methName = $req->getParam(0);
875 $methName = $methName->scalarval();
879 if (strpos($methName,
"system.") === 0) {
880 $dmap = $server->getSystemDispatchMap();
882 $dmap = $server->dmap;
884 if (isset($dmap[$methName])) {
885 if (isset($dmap[$methName][
'docstring'])) {
886 $r =
new Response(
new Value($dmap[$methName][
'docstring']),
'string');
891 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'introspect_unknown'], PhpXmlRpc::$xmlrpcstr[
'introspect_unknown']);
899 if (is_string($err)) {
900 $str = PhpXmlRpc::$xmlrpcstr[
"multicall_${err}"];
901 $code = PhpXmlRpc::$xmlrpcerr[
"multicall_${err}"];
903 $code = $err->faultCode();
904 $str = $err->faultString();
907 $struct[
'faultCode'] =
new Value($code,
'int');
908 $struct[
'faultString'] =
new Value($str,
'string');
910 return new Value($struct,
'struct');
915 if ($call->kindOf() !=
'struct') {
916 return static::_xmlrpcs_multicall_error(
'notstruct');
918 $methName = @$call[
'methodName'];
920 return static::_xmlrpcs_multicall_error(
'nomethod');
922 if ($methName->kindOf() !=
'scalar' || $methName->scalartyp() !=
'string') {
923 return static::_xmlrpcs_multicall_error(
'notstring');
925 if ($methName->scalarval() ==
'system.multicall') {
926 return static::_xmlrpcs_multicall_error(
'recursion');
929 $params = @$call[
'params'];
931 return static::_xmlrpcs_multicall_error(
'noparams');
933 if ($params->kindOf() !=
'array') {
934 return static::_xmlrpcs_multicall_error(
'notarray');
937 $req =
new Request($methName->scalarval());
938 foreach($params as $i => $param) {
939 if (!$req->addParam($param)) {
941 return static::_xmlrpcs_multicall_error(
new Response(0,
942 PhpXmlRpc::$xmlrpcerr[
'incorrect_params'],
943 PhpXmlRpc::$xmlrpcstr[
'incorrect_params'] .
": probable xml error in param " . $i));
947 $result = $server->execute($req);
949 if ($result->faultCode() != 0) {
950 return static::_xmlrpcs_multicall_error($result);
953 return new Value(array($result->value()),
'array');
958 if (!is_array($call)) {
959 return static::_xmlrpcs_multicall_error(
'notstruct');
961 if (!array_key_exists(
'methodName', $call)) {
962 return static::_xmlrpcs_multicall_error(
'nomethod');
964 if (!is_string($call[
'methodName'])) {
965 return static::_xmlrpcs_multicall_error(
'notstring');
967 if ($call[
'methodName'] ==
'system.multicall') {
968 return static::_xmlrpcs_multicall_error(
'recursion');
970 if (!array_key_exists(
'params', $call)) {
971 return static::_xmlrpcs_multicall_error(
'noparams');
973 if (!is_array($call[
'params'])) {
974 return static::_xmlrpcs_multicall_error(
'notarray');
979 $numParams = count($call[
'params']);
982 foreach ($call[
'params'] as $val) {
983 $pt[] = $wrapper->php2XmlrpcType(gettype($val));
986 $result = $server->execute($call[
'methodName'], $call[
'params'], $pt);
988 if ($result->faultCode() != 0) {
989 return static::_xmlrpcs_multicall_error($result);
992 return new Value(array($result->value()),
'array');
999 if (is_object($req)) {
1000 $calls = $req->getParam(0);
1001 foreach($calls as $call) {
1002 $result[] = static::_xmlrpcs_multicall_do_call($server, $call);
1005 $numCalls = count($req);
1006 for ($i = 0; $i < $numCalls; $i++) {
1007 $result[$i] = static::_xmlrpcs_multicall_do_call_phpvals($server, $req[$i]);
1025 if (error_reporting() == 0) {
1030 if ($errCode != E_STRICT) {
1035 if (self::$_xmlrpcs_prev_ehandler ==
'') {
1038 if (ini_get(
'log_errors') && (intval(ini_get(
'error_reporting')) & $errCode)) {
1039 error_log($errString);
1043 if (self::$_xmlrpcs_prev_ehandler != array(
'\PhpXmlRpc\Server',
'_xmlrpcs_errorHandler')) {
1044 if (is_array(self::$_xmlrpcs_prev_ehandler)) {
1046 call_user_func_array(self::$_xmlrpcs_prev_ehandler, array($errCode, $errString, $filename, $lineNo, $context));
1048 $method = self::$_xmlrpcs_prev_ehandler;
1049 $method($errCode, $errString, $filename, $lineNo, $context);