📄 xmlrpcs.inc
字号:
// It might contain a copy of raw data received from client, ie with unknown encoding,
// intermixed with php generated data and user generated data...
return "<!-- DEBUG INFO:\n\n" . xmlrpc_encode_entitites($GLOBALS['_xmlrpc_debuginfo']) . "\n-->\n";
// NB: a better solution MIGHT be to use CDATA, but we need to insert it
// into return payload AFTER the beginning tag
//return "<![CDATA[ DEBUG INFO:\n\n" . str_replace(']]>', ']_]_>', $GLOBALS['_xmlrpc_debuginfo']) . "\n]]>\n";
}
else
{
return '';
}
}
function service()
{
// Play nice to PHP 4.0.x: superglobals were not yet invented...
if(!isset($_SERVER))
{
$_SERVER = $GLOBALS['HTTP_SERVER_VARS'];
}
// Echo back what we received, before parsing it
// NB: we should make sure we have no substring -- in the received data, and
// that it does not end with a '-' char, or it would break
// xml comment encapsulating it
if($this->debug > 1)
{
xmlrpc_debugmsg("+++GOT+++");
if(function_exists('getallheaders'))
{
foreach(getallheaders() as $name => $val)
{
xmlrpc_debugmsg("$name: $val\r");
}
}
// do NOT log back to client the compressed request we received,
// until we find a way to correctly encode it in charset-agnostic xml
if(isset($_SERVER['HTTP_CONTENT_ENCODING']))
{
xmlrpc_debugmsg("\r\n+++END+++");
}
else
{
xmlrpc_debugmsg("\r\n" . $GLOBALS['HTTP_RAW_POST_DATA'] . "\n+++END+++");
}
}
if(isset($_SERVER['HTTP_CONTENT_ENCODING']))
{
$r=$this->parseRequest($GLOBALS['HTTP_RAW_POST_DATA'], $_SERVER['HTTP_CONTENT_ENCODING']);
}
else
{
$r=$this->parseRequest($GLOBALS['HTTP_RAW_POST_DATA']);
}
if($this->debug > 2 && $GLOBALS['_xmlrpcs_occurred_errors'])
{
xmlrpc_debugmsg("+++PROCESSING ERRORS AND WARNINGS+++\n" .
$GLOBALS['_xmlrpcs_occurred_errors'] . "+++END+++");
}
//$payload='<?xml version="1.0" encoding="' . $GLOBALS['xmlrpc_defencoding'] . '"?' . '>' . "\n"
$payload=$this->xml_header;
if($this->debug > 0)
{
//$payload = $payload . "<methodResponse>\n" . $this->serializeDebug();
//$payload = $payload . substr($r->serialize(), 17);
$payload = $payload . $this->serializeDebug();
}
//else
//{
$payload = $payload . $r->serialize();
//}
// if we get a warning/error that has output some text before here, then we cannot
// add a new header. We cannot say we are sending xml, either...
if(!headers_sent())
{
header('Content-Type: '.$r->content_type);
// http compression of output
if($this->compress_response && function_exists('gzencode') && isset($_SERVER['HTTP_ACCEPT_ENCODING']))
{
if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
{
//(strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip')) {
$payload = gzencode($payload);
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
}
elseif (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate'))
{
//(strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'deflate')) {
$payload = gzdeflate($payload);
header("Content-Encoding: deflate");
header("Vary: Accept-Encoding");
}
}
header('Content-Length: ' . (int)strlen($payload));
}
else
{
//print "Internal server error: headers sent before PHP response"
}
print $payload;
}
/**
* add a method to the dispatch map
*/
function add_to_map($methodname,$function,$sig,$doc='')
{
$this->dmap[$methodname] = array(
'function' => $function,
'signature' => $sig,
'docstring' => $doc
);
}
/**
* @access private
*/
function verifySignature($in, $sig)
{
// check each possible signature in turn
foreach($sig as $cursig)
{
if(sizeof($cursig)==$in->getNumParams()+1)
{
$itsOK=1;
for($n=0; $n<$in->getNumParams(); $n++)
{
$p=$in->getParam($n);
// print "<!-- $p -->\n";
if($p->kindOf() == 'scalar')
{
$pt=$p->scalartyp();
}
else
{
$pt=$p->kindOf();
}
// $n+1 as first type of sig is return type
if($pt != $cursig[$n+1] && $cursig[$n+1] != $GLOBALS['xmlrpcValue'])
{
$itsOK=0;
$pno=$n+1;
$wanted=$cursig[$n+1];
$got=$pt;
break;
}
}
if($itsOK)
{
return array(1,'');
}
}
}
if(isset($wanted))
{
return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
}
else
{
return array(0, "No method signature matches number of parameters");
}
}
/**
* @access private
*/
function parseRequest($data, $content_encoding='')
{
// 2005/05/07 commented and moved into caller function code
//if($data=='')
//{
// $data=$GLOBALS['HTTP_RAW_POST_DATA'];
//}
// check if request body has been compressed and decompress it
if($content_encoding != '' && strlen($data))
{
if($content_encoding == 'deflate' || $content_encoding == 'gzip')
{
// if decoding works, use it. else assume data wasn't gzencoded
if(function_exists('gzinflate') && in_array($content_encoding, $this->accepted_compression))
{
if($content_encoding == 'deflate' && $degzdata = @gzinflate($data))
{
$data = $degzdata;
if($this->debug > 1)
{
xmlrpc_debugmsg("+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++");
}
}
elseif($content_encoding == 'gzip' && $degzdata = @gzinflate(substr($data, 10)))
{
$data = $degzdata;
if($this->debug > 1)
xmlrpc_debugmsg("+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++");
}
else
{
$r =& new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_decompress_fail'], $GLOBALS['xmlrpcstr']['server_decompress_fail']);
return $r;
}
}
else
{
//error_log('The server sent deflated data. Your php install must have the Zlib extension compiled in to support this.');
$r =& new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_cannot_decompress'], $GLOBALS['xmlrpcstr']['server_cannot_decompress']);
return $r;
}
}
}
// 'guestimate' request encoding
/// @todo check if mbstring is enabled and automagic input conversion is on: it might mingle with this check???
$req_encoding = guess_encoding(isset($_SERVER['HTTP_CONTENT_TYPE']) ? $_SERVER['HTTP_CONTENT_TYPE'] : '',
$data);
// G. Giunta 2005/02/13: we do NOT expect to receive html entities
// so we do not try to convert them into xml character entities
//$data = xmlrpc_html_entity_xlate($data);
$GLOBALS['_xh']=array();
$GLOBALS['_xh']['isf']=0;
$GLOBALS['_xh']['isf_reason']='';
$GLOBALS['_xh']['params']=array();
$GLOBALS['_xh']['stack']=array();
$GLOBALS['_xh']['valuestack'] = array();
$GLOBALS['_xh']['method']='';
// decompose incoming XML into request structure
if (!in_array($req_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))
// the following code might be better for mb_string enabled installs, but
// makes the lib about 200% slower...
//if (!is_valid_charset($req_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))
{
error_log('XML-RPC: xmlrpc_server::parseRequest: invalid charset encoding of received request: '.$req_encoding);
$req_encoding = $GLOBALS['xmlrpc_defencoding'];
}
$parser = xml_parser_create($req_encoding);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
// G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell
// the xml parser to give us back data in the expected charset
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']);
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
xml_set_character_data_handler($parser, 'xmlrpc_cd');
xml_set_default_handler($parser, 'xmlrpc_dh');
if(!xml_parse($parser, $data, 1))
{
// return XML error as a faultCode
$r=&new xmlrpcresp(0,
$GLOBALS['xmlrpcerrxml']+xml_get_error_code($parser),
sprintf('XML error: %s at line %d',
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
xml_parser_free($parser);
}
elseif ($GLOBALS['_xh']['isf'])
{
xml_parser_free($parser);
$r=&new xmlrpcresp(0,
$GLOBALS['xmlrpcerr']['invalid_request'],
$GLOBALS['xmlrpcstr']['invalid_request'] . ' ' . $GLOBALS['_xh']['isf_reason']);
}
else
{
xml_parser_free($parser);
$m=&new xmlrpcmsg($GLOBALS['_xh']['method']);
// now add parameters in
$plist='';
for($i=0; $i<sizeof($GLOBALS['_xh']['params']); $i++)
{
//print "<!-- " . $GLOBALS['_xh']['params'][$i]. "-->\n";
$plist.="$i - " . $GLOBALS['_xh']['params'][$i]. ";\n";
$m->addParam($GLOBALS['_xh']['params'][$i]);
}
$r = $this->execute($m);
}
return $r;
}
function execute($m)
{
// now to deal with the method
$methName = $m->method();
$sysCall = $this->allow_system_funcs && ereg("^system\.", $methName);
$dmap = $sysCall ? $GLOBALS['_xmlrpcs_dmap'] : $this->dmap;
if(!isset($dmap[$methName]['function']))
{
// No such method
return new xmlrpcresp(0,
$GLOBALS['xmlrpcerr']['unknown_method'],
$GLOBALS['xmlrpcstr']['unknown_method']);
}
// Check signature.
if(isset($dmap[$methName]['signature']))
{
$sig = $dmap[$methName]['signature'];
list($ok, $errstr) = $this->verifySignature($m, $sig);
if(!$ok)
{
// Didn't match.
return new xmlrpcresp(
0,
$GLOBALS['xmlrpcerr']['incorrect_params'],
$GLOBALS['xmlrpcstr']['incorrect_params'] . ": ${errstr}"
);
}
}
$func = $dmap[$methName]['function'];
// check that function defined in dispatch method does in fact exist
if((is_array($func) && !method_exists($func[0], $func[1])) && !function_exists($func))
{
return new xmlrpcresp(
0,
$GLOBALS['xmlrpcerr']['server_error'],
$GLOBALS['xmlrpcstr']['server_error'] . ": no function matches method"
);
}
// If debug level is 3, we should catch all errors generated during
// processing of user function, and log them into response
if($this->debug > 2)
{
$GLOBALS['_xmlrpcs_prev_ehandler'] = set_error_handler('_xmlrpcs_errorHandler');
}
if($sysCall)
{
$r = call_user_func($func, $this, $m);
}
else
{
$r = call_user_func($func, $m);
}
if($this->debug > 2)
{
// note: restore the error handler we found before calling the
// user func, even if it has been changed inside the func itself
if($GLOBALS['_xmlrpcs_prev_ehandler'])
{
set_error_handler($GLOBALS['_xmlrpcs_prev_ehandler']);
}
else
{
restore_error_handler();
}
}
return $r;
}
/**
* A debugging routine: just echoes back the input packet as a string value
* DEPRECATED!
*/
function echoInput()
{
$r=&new xmlrpcresp(new xmlrpcval( "'Aha said I: '" . $GLOBALS['HTTP_RAW_POST_DATA'], 'string'));
print $r->serialize();
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -