xmlrpc.inc
来自「BLOG HOSTER---PHP & MYSQL Create Blogs 」· INC 代码 · 共 1,473 行 · 第 1/3 页
INC
1,473 行
if ($method == 'https')
{
return $this->sendPayloadHTTPS($msg,
$this->server,
$this->port, $timeout,
$this->username, $this->password,
$this->cert,
$this->certpass);
}
else
{
return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
$timeout, $this->username,
$this->password);
}
}
function sendPayloadHTTP10($msg, $server, $port, $timeout=0,$username='', $password='')
{
global $xmlrpcerr, $xmlrpcstr;
if ($port==0)
{
$port=80;
}
if($timeout>0)
{
$fp=fsockopen($server, $port,$this->errno, $this->errstr, $timeout);
}
else
{
$fp=fsockopen($server, $port,$this->errno, $this->errstr);
}
if (!$fp)
{
$this->errstr='Connect error';
$r=new xmlrpcresp(0, $xmlrpcerr['http_error'],$xmlrpcstr['http_error']);
return $r;
}
// Only create the payload if it was not created previously
if(empty($msg->payload))
{
$msg->createPayload();
}
// thanks to Grant Rauscher <grant7@firstworld.net>
// for this
$credentials='';
if ($username!='')
{
$credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n";
}
$op= "POST " . $this->path. " HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\n" .
"Host: ". $this->server . "\r\n" .
$credentials .
"Content-Type: text/xml\r\nContent-Length: " .
strlen($msg->payload) . "\r\n\r\n" .
$msg->payload;
if (!fputs($fp, $op, strlen($op)))
{
$this->errstr='Write error';
$r=new xmlrpcresp(0, $xmlrpcerr['http_error'], $xmlrpcstr['http_error']);
return $r;
}
$resp=$msg->parseResponseFile($fp);
fclose($fp);
return $resp;
}
// contributed by Justin Miller <justin@voxel.net>
// requires curl to be built into PHP
function sendPayloadHTTPS($msg, $server, $port, $timeout=0,$username='', $password='', $cert='',$certpass='')
{
global $xmlrpcerr, $xmlrpcstr;
if ($port == 0)
{
$port = 443;
}
// Only create the payload if it was not created previously
if(empty($msg->payload))
{
$msg->createPayload();
}
if (!function_exists('curl_init'))
{
$this->errstr='SSL unavailable on this install';
$r=new xmlrpcresp(0, $xmlrpcerr['no_ssl'], $xmlrpcstr['no_ssl']);
return $r;
}
$curl = curl_init('https://' . $server . ':' . $port . $this->path);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// results into variable
if ($this->debug)
{
curl_setopt($curl, CURLOPT_VERBOSE, 1);
}
curl_setopt($curl, CURLOPT_USERAGENT, 'PHP XMLRPC 1.0');
// required for XMLRPC
curl_setopt($curl, CURLOPT_POST, 1);
// post the data
curl_setopt($curl, CURLOPT_POSTFIELDS, $msg->payload);
// the data
curl_setopt($curl, CURLOPT_HEADER, 1);
// return the header too
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
// whether to verify remote host's cert
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer);
// whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost);
// required for XMLRPC
if ($timeout)
{
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1);
}
// timeout is borked
if ($username && $password)
{
curl_setopt($curl, CURLOPT_USERPWD,"$username:$password");
}
// set auth stuff
if ($cert)
{
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
}
// set cert file
if ($certpass)
{
curl_setopt($curl, CURLOPT_SSLCERTPASSWD,$certpass);
}
// set cert password
$result = curl_exec($curl);
if (!$result)
{
$this->errstr='no response';
$resp=new xmlrpcresp(0, $xmlrpcerr['curl_fail'], $xmlrpcstr['curl_fail']. ': '. curl_error($curl));
}
else
{
$resp = $msg->parseResponse($result);
}
curl_close($curl);
return $resp;
}
function multicall($msgs, $timeout=0, $method='http')
{
$results = false;
if (! $this->no_multicall)
{
$results = $this->_try_multicall($msgs, $timeout, $method);
/* TODO - this is not php3-friendly */
// if($results !== false)
if($results != false)
{
// Either the system.multicall succeeded, or the send
// failed (e.g. due to HTTP timeout). In either case,
// we're done for now.
return $results;
}
else
{
// system.multicall unsupported by server,
// don't try it next time...
$this->no_multicall = true;
}
}
// system.multicall is unupported by server:
// Emulate multicall via multiple requests
$results = array();
//foreach($msgs as $msg)
@reset($msgs);
while(list(,$msg) = @each($msgs))
{
$results[] = $this->send($msg, $timeout, $method);
}
return $results;
}
// Attempt to boxcar $msgs via system.multicall.
function _try_multicall($msgs, $timeout, $method)
{
// Construct multicall message
$calls = array();
//foreach($msgs as $msg)
@reset($msgs);
while(list(,$msg) = @each($msgs))
{
$call['methodName'] = new xmlrpcval($msg->method(),'string');
$numParams = $msg->getNumParams();
$params = array();
for ($i = 0; $i < $numParams; $i++)
{
$params[$i] = $msg->getParam($i);
}
$call['params'] = new xmlrpcval($params, 'array');
$calls[] = new xmlrpcval($call, 'struct');
}
$multicall = new xmlrpcmsg('system.multicall');
$multicall->addParam(new xmlrpcval($calls, 'array'));
// Attempt RPC call
$result = $this->send($multicall, $timeout, $method);
if (!is_object($result))
return ($result || 0); // transport failed
if ($result->faultCode() != 0)
return false; // system.multicall failed
// Unpack responses.
$rets = $result->value();
if ($rets->kindOf() != 'array')
return false; // bad return type from system.multicall
$numRets = $rets->arraysize();
if ($numRets != count($msgs))
return false; // wrong number of return values.
$response = array();
for ($i = 0; $i < $numRets; $i++)
{
$val = $rets->arraymem($i);
switch ($val->kindOf())
{
case 'array':
if ($val->arraysize() != 1)
return false; // Bad value
// Normal return value
$response[$i] = new xmlrpcresp($val->arraymem(0));
break;
case 'struct':
$code = $val->structmem('faultCode');
if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int')
return false;
$str = $val->structmem('faultString');
if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string')
return false;
$response[$i] = new xmlrpcresp(0, $code->scalarval(), $str->scalarval());
break;
default:
return false;
}
}
return $response;
}
} // end class xmlrpc_client
class xmlrpcresp
{
var $val = 0;
var $errno = 0;
var $errstr = '';
var $hdrs = array();
function xmlrpcresp($val, $fcode = 0, $fstr = '')
{
if ($fcode != 0)
{
// error
$this->errno = $fcode;
$this->errstr = htmlspecialchars($fstr); // XXX: encoding probably shouldn't be done here; fix later.
}
else if (!is_object($val))
{
// programmer error
error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to xmlrpcresp. Defaulting to empty value.");
$this->val = new xmlrpcval();
}
else
{
// success
$this->val = $val;
}
}
function faultCode()
{
return $this->errno;
}
function faultString()
{
return $this->errstr;
}
function value()
{
return $this->val;
}
function serialize()
{
$result = "<methodResponse>\n";
if ($this->errno)
{
$result .= '<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>' . $this->errno . '</int></value>
</member>
<member>
<name>faultString</name>
<value><string>' . $this->errstr . '</string></value>
</member>
</struct>
</value>
</fault>';
}
else
{
$result .= "<params>\n<param>\n" .
$this->val->serialize() .
"</param>\n</params>";
}
$result .= "\n</methodResponse>";
return $result;
}
}
class xmlrpcmsg
{
var $payload;
var $methodname;
var $params=array();
var $debug=0;
function xmlrpcmsg($meth, $pars=0)
{
$this->methodname=$meth;
if (is_array($pars) && sizeof($pars)>0)
{
for($i=0; $i<sizeof($pars); $i++)
{
$this->addParam($pars[$i]);
}
}
}
function xml_header()
{
return "<?xml version=\"1.0\"?>\n<methodCall>\n";
}
function xml_footer()
{
return "</methodCall>\n";
}
function createPayload()
{
$this->payload=$this->xml_header();
$this->payload.='<methodName>' . $this->methodname . "</methodName>\n";
// if (sizeof($this->params)) {
$this->payload.="<params>\n";
for($i=0; $i<sizeof($this->params); $i++)
{
$p=$this->params[$i];
$this->payload.="<param>\n" . $p->serialize() .
"</param>\n";
}
$this->payload.="</params>\n";
// }
$this->payload.=$this->xml_footer();
$this->payload=str_replace("\n", "\r\n", $this->payload);
}
function method($meth='')
{
if ($meth!='')
{
$this->methodname=$meth;
}
return $this->methodname;
}
function serialize()
{
$this->createPayload();
return $this->payload;
}
function addParam($par) { $this->params[]=$par; }
function getParam($i) { return $this->params[$i]; }
function getNumParams() { return sizeof($this->params); }
function parseResponseFile($fp)
{
$ipd='';
while($data=fread($fp, 32768))
{
$ipd.=$data;
}
return $this->parseResponse($ipd);
}
function parseResponse($data='')
{
global $_xh,$xmlrpcerr,$xmlrpcstr;
global $xmlrpc_defencoding;
$parser = xml_parser_create($xmlrpc_defencoding);
$_xh[$parser]=array();
$_xh[$parser]['st']='';
$_xh[$parser]['cm']=0;
$_xh[$parser]['isf']=0;
$_xh[$parser]['ac']='';
$_xh[$parser]['qt']='';
$_xh[$parser]['headers'] = array();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
xml_set_character_data_handler($parser, 'xmlrpc_cd');
xml_set_default_handler($parser, 'xmlrpc_dh');
$xmlrpc_value=new xmlrpcval;
$hdrfnd = 0;
if($this->debug)
{
//by maHo, replaced htmlspecialchars with htmlentities
print "<PRE>---GOT---\n" . htmlentities($data) . "\n---END---\n</PRE>";
}
if($data == '')
{
error_log('No response received from server.');
$r = new xmlrpcresp(0, $xmlrpcerr['no_data'], $xmlrpcstr['no_data']);
xml_parser_free($parser);
return $r;
}
// see if we got an HTTP 200 OK, else bomb
// but only do this if we're using the HTTP protocol.
if(ereg("^HTTP",$data) && !ereg("^HTTP/[0-9\.]+ 200 ", $data))
{
$errstr= substr($data, 0, strpos($data, "\n")-1);
error_log('HTTP error, got response: ' .$errstr);
$r=new xmlrpcresp(0, $xmlrpcerr['http_error'], $xmlrpcstr['http_error']. ' (' . $errstr . ')');
xml_parser_free($parser);
return $r;
}
// separate HTTP headers from data
if (ereg("^HTTP", $data))
{
$ar = split("\r\n", $data);
while (($line = array_shift($ar)))
{
if (strlen($line) < 1)
{
break;
}
$_xh[$parser]['headers'][] = $line;
}
$data = join("\r\n", $ar);
}
if ($this->debug && count($_xh[$parser]['headers']))
{
print "<PRE>";
foreach ($_xh[$parser]['headers'] as $header)
{
print "HEADER: $header\n";
}
print "</PRE>\n";
}
if (!xml_parse($parser, $data, sizeof($data)))
{
// thanks to Peter Kocks <peter.kocks@baygate.com>
if((xml_get_current_line_number($parser)) == 1)
{
$errstr = 'XML error at line 1, check URL';
}
else
{
$errstr = sprintf('XML error: %s at line %d',
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser));
error_log($errstr);
$r=new xmlrpcresp(0, $xmlrpcerr['invalid_return'], $xmlrpcstr['invalid_return']);
xml_parser_free($parser);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?