📄 lib-xmlrpc.inc.php
字号:
{
// check for correct element nesting
// top level element can only be of 2 types
if (count($_xh[$parser]['stack']) == 0)
{
if ($name != 'METHODRESPONSE' && $name != 'METHODCALL')
{
$_xh[$parser]['isf'] = 2;
$_xh[$parser]['isf_reason'] = 'missing top level xmlrpc element';
return;
}
}
else
{
// not top level element: see if parent is OK
if (!in_array($_xh[$parser]['stack'][0], $xmlrpc_valid_parents[$name]))
{
$_xh[$parser]['isf'] = 2;
$_xh[$parser]['isf_reason'] = "xmlrpc element $name cannot be child of {$_xh[$parser]['stack'][0]}";
return;
}
}
switch($name)
{
case 'STRUCT':
case 'ARRAY':
//$_xh[$parser]['st'].='array(';
//$_xh[$parser]['cm']++;
// this last line turns quoting off
// this means if we get an empty array we'll
// simply get a bit of whitespace in the eval
//$_xh[$parser]['qt']=0;
// create an empty array to hold child values, and push it onto appropriate stack
$cur_val = array();
$cur_val['values'] = array();
$cur_val['type'] = $name;
array_unshift($_xh[$parser]['valuestack'], $cur_val);
break;
case 'METHODNAME':
case 'NAME':
//$_xh[$parser]['st'].='"';
$_xh[$parser]['ac']='';
break;
case 'FAULT':
$_xh[$parser]['isf']=1;
break;
case 'PARAM':
//$_xh[$parser]['st']='';
// clear value, so we can check later if no value will passed for this param/member
$_xh[$parser]['value']=null;
break;
case 'VALUE':
//$_xh[$parser]['st'].='new xmlrpcval(';
// look for a value: if this is still true by the
// time we reach the end tag for value then the type is string
// by implication
$_xh[$parser]['vt']='value';
$_xh[$parser]['ac']='';
//$_xh[$parser]['qt']=0;
$_xh[$parser]['lv']=1;
break;
case 'I4':
case 'INT':
case 'STRING':
case 'BOOLEAN':
case 'DOUBLE':
case 'DATETIME.ISO8601':
case 'BASE64':
if ($_xh[$parser]['vt']!='value')
{
//two data elements inside a value: an error occurred!
$_xh[$parser]['isf'] = 2;
$_xh[$parser]['isf_reason'] = "$name element following a {$_xh[$parser]['vt']} element inside a single value";
return;
}
// reset the accumulator
$_xh[$parser]['ac']='';
/*if ($name=='DATETIME.ISO8601' || $name=='STRING')
{
$_xh[$parser]['qt']=1;
if ($name=='DATETIME.ISO8601')
{
$_xh[$parser]['vt']=$xmlrpcDateTime;
}
}
elseif ($name=='BASE64')
{
$_xh[$parser]['qt']=2;
}
else
{
// No quoting is required here -- but
// at the end of the element we must check
// for data format errors.
$_xh[$parser]['qt']=0;
}*/
break;
case 'MEMBER':
//$_xh[$parser]['ac']='';
// avoid warnings later on if no NAME is found before VALUE inside
// a struct member predefining member name as NULL
$_xh[$parser]['valuestack'][0]['name'] = '';
// clear value, so we can check later if no value will passed for this param/member
$_xh[$parser]['value']=null;
break;
case 'DATA':
case 'METHODCALL':
case 'METHODRESPONSE':
case 'PARAMS':
// valid elements that add little to processing
break;
default:
/// INVALID ELEMENT: RAISE ISF so that it is later recognized!!!
$_xh[$parser]['isf'] = 2;
$_xh[$parser]['isf_reason'] = "found not-xmlrpc xml element $name";
break;
}
// Save current element name to stack, to validate nesting
array_unshift($_xh[$parser]['stack'], $name);
if ($name!='VALUE')
{
$_xh[$parser]['lv']=0;
}
}
}
function xmlrpc_ee($parser, $name)
{
global $_xh,$xmlrpcTypes,$xmlrpcString,$xmlrpcDateTime;
if ($_xh[$parser]['isf'] < 2)
{
// push this element name from stack
// NB: if XML validates, correct opening/closing is guaranteed and
// we do not have to check for $name == $curr_elem.
// we also checked for proper nesting at start of elements...
$curr_elem = array_shift($_xh[$parser]['stack']);
switch($name)
{
case 'STRUCT':
case 'ARRAY':
//if ($_xh[$parser]['cm'] && substr($_xh[$parser]['st'], -1) ==',')
//{
// $_xh[$parser]['st']=substr($_xh[$parser]['st'],0,-1);
//}
//$_xh[$parser]['st'].=')';
// fetch out of stack array of values, and promote it to current value
$cur_val = array_shift($_xh[$parser]['valuestack']);
$_xh[$parser]['value'] = $cur_val['values'];
$_xh[$parser]['vt']=strtolower($name);
//$_xh[$parser]['cm']--;
break;
case 'NAME':
//$_xh[$parser]['st'].= $_xh[$parser]['ac'] . '" => ';
$_xh[$parser]['valuestack'][0]['name'] = $_xh[$parser]['ac'];
break;
case 'BOOLEAN':
case 'I4':
case 'INT':
case 'STRING':
case 'DOUBLE':
case 'DATETIME.ISO8601':
case 'BASE64':
$_xh[$parser]['vt']=strtolower($name);
//if ($_xh[$parser]['qt']==1)
if ($name=='STRING')
{
// we use double quotes rather than single so backslashification works OK
//$_xh[$parser]['st'].='"'. $_xh[$parser]['ac'] . '"';
$_xh[$parser]['value']=$_xh[$parser]['ac'];
}
elseif ($name=='DATETIME.ISO8601')
{
$_xh[$parser]['vt']=$xmlrpcDateTime;
$_xh[$parser]['value']=$_xh[$parser]['ac'];
}
elseif ($name=='BASE64')
{
//$_xh[$parser]['st'].='base64_decode("'. $_xh[$parser]['ac'] . '")';
///@todo check for failure of base64 decoding / catch warnings
$_xh[$parser]['value']=base64_decode($_xh[$parser]['ac']);
}
elseif ($name=='BOOLEAN')
{
// special case here: we translate boolean 1 or 0 into PHP
// constants true or false
// NB: this simple checks helps a lot sanitizing input, ie no
// security problems around here
if ($_xh[$parser]['ac']=='1')
{
//$_xh[$parser]['ac']='true';
$_xh[$parser]['value']=true;
}
else
{
//$_xh[$parser]['ac']='false';
// log if receiveing something strange, even though we set the value to false anyway
if ($_xh[$parser]['ac']!='0')
error_log('XML-RPC: invalid value received in BOOLEAN: '.$_xh[$parser]['ac']);
$_xh[$parser]['value']=false;
}
//$_xh[$parser]['st'].=$_xh[$parser]['ac'];
}
elseif ($name=='DOUBLE')
{
// we have a DOUBLE
// we must check that only 0123456789-.<space> are characters here
if (!ereg("^[+-]?[eE0123456789 \\t\\.]+$", $_xh[$parser]['ac']))
{
// TODO: find a better way of throwing an error
// than this!
error_log('XML-RPC: non numeric value received in DOUBLE: '.$_xh[$parser]['ac']);
//$_xh[$parser]['st'].="'ERROR_NON_NUMERIC_FOUND'";
$_xh[$parser]['value']='ERROR_NON_NUMERIC_FOUND';
}
else
{
// it's ok, add it on
//$_xh[$parser]['st'].=(double)$_xh[$parser]['ac'];
$_xh[$parser]['value']=(double)$_xh[$parser]['ac'];
}
}
else
{
// we have an I4/INT
// we must check that only 0123456789-<space> are characters here
if (!ereg("^[+-]?[0123456789 \\t]+$", $_xh[$parser]['ac']))
{
// TODO: find a better way of throwing an error
// than this!
error_log('XML-RPC: non numeric value received in INT: '.$_xh[$parser]['ac']);
//$_xh[$parser]['st'].="'ERROR_NON_NUMERIC_FOUND'";
$_xh[$parser]['value']='ERROR_NON_NUMERIC_FOUND';
}
else
{
// it's ok, add it on
//$_xh[$parser]['st'].=(int)$_xh[$parser]['ac'];
$_xh[$parser]['value']=(int)$_xh[$parser]['ac'];
}
}
$_xh[$parser]['ac']='';
//$_xh[$parser]['qt']=0;
$_xh[$parser]['lv']=3; // indicate we've found a value
break;
case 'VALUE':
// This if() detects if no scalar was inside <VALUE></VALUE>
if ($_xh[$parser]['vt']=='value')
{
$_xh[$parser]['value']=$_xh[$parser]['ac'];
$_xh[$parser]['vt']=$xmlrpcString;
}
/*if (strlen($_xh[$parser]['ac'])>0 &&
$_xh[$parser]['vt']==$xmlrpcString)
{
$_xh[$parser]['st'].='"'. $_xh[$parser]['ac'] . '"';
}
// This if() detects if no scalar was inside <VALUE></VALUE>
// and pads an empty ''.
if($_xh[$parser]['st'][strlen($_xh[$parser]['st'])-1] == '(')
{
$_xh[$parser]['st'].= '""';
}
// G. Giunta 2005/03/12 save some chars in the reconstruction of string vals...
if ($_xh[$parser]['vt'] != $xmlrpcString)
$_xh[$parser]['st'].=", '" . $_xh[$parser]['vt'] . "')";
else
$_xh[$parser]['st'].=")";
if ($_xh[$parser]['cm'])
{
$_xh[$parser]['st'].=',';
}*/
// build the xmlrpc val out of the data received, and substitute it
$temp = new xmlrpcval($_xh[$parser]['value'], $_xh[$parser]['vt']);
// check if we are inside an array or struct:
// if value just built is inside an array, let's move it into array on the stack
if (count($_xh[$parser]['valuestack']) && $_xh[$parser]['valuestack'][0]['type']=='ARRAY')
{
$_xh[$parser]['valuestack'][0]['values'][] = $temp;
} else {
$_xh[$parser]['value'] = $temp;
}
break;
case 'MEMBER':
$_xh[$parser]['ac']='';
//$_xh[$parser]['qt']=0;
// add to array in the stack the last element built
// unless no VALUE was found
if ($_xh[$parser]['value'])
$_xh[$parser]['valuestack'][0]['values'][$_xh[$parser]['valuestack'][0]['name']] = $_xh[$parser]['value'];
else
error_log('XML-RPC: missing VALUE inside STRUCT in received xml');
break;
case 'DATA':
$_xh[$parser]['ac']='';
//$_xh[$parser]['qt']=0;
break;
case 'PARAM':
//$_xh[$parser]['params'][]=$_xh[$parser]['st'];
if ($_xh[$parser]['value'])
$_xh[$parser]['params'][]=$_xh[$parser]['value'];
else
error_log('XML-RPC: missing VALUE inside PARAM in received xml');
break;
case 'METHODNAME':
$_xh[$parser]['method']=ereg_replace("^[\n\r\t ]+", '', $_xh[$parser]['ac']);
break;
case 'PARAMS':
case 'FAULT':
case 'METHODCALL':
case 'METHORESPONSE':
break;
default:
// End of INVALID ELEMENT!
// shall we add an assert here for unreachable code???
break;
}
// if it's a valid type name, set the type
/*if (isset($xmlrpcTypes[strtolower($name)]))
{
$_xh[$parser]['vt']=strtolower($name);
}*/
}
}
function xmlrpc_cd($parser, $data)
{
global $_xh, $xmlrpc_backslash;
//if (ereg("^[\n\r \t]+$", $data)) return;
// print "adding [${data}]\n";
// skip processing if xml fault already detected
if ($_xh[$parser]['isf'] < 2)
{
if ($_xh[$parser]['lv']!=3)
{
// "lookforvalue==3" means that we've found an entire value
// and should discard any further character data
if ($_xh[$parser]['lv']==1)
{
// if we've found text and we're just in a <value> then
// turn quoting on, as this will be a string
//$_xh[$parser]['qt']=1;
// and say we've found a value
$_xh[$parser]['lv']=2;
}
if(!@isset($_xh[$parser]['ac']))
{
$_xh[$parser]['ac'] = '';
}
//$_xh[$parser]['ac'].=str_replace('$', '\$', str_replace('"', '\"', str_replace(chr(92),$xmlrpc_backslash, $data)));
$_xh[$parser]['ac'].=$data;
}
}
}
function xmlrpc_dh($parser, $data)
{
global $_xh, $xmlrpc_backslash;
// skip processing if xml fault already detected
if ($parser[$_xh]['isf'] < 2)
{
if (substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';')
{
if ($_xh[$parser]['lv']==1)
{
//$_xh[$parser]['qt']=1;
$_xh[$parser]['lv']=2;
}
//$_xh[$parser]['ac'].=str_replace('$', '\$', str_replace('"', '\"', str_replace(chr(92),$xmlrpc_backslash, $data)));
$_xh[$parser]['ac'].=$data;
}
}
}
class xmlrpc_client
{
var $path;
var $server;
var $port;
var $errno;
var $errstr;
var $debug=0;
var $username='';
var $password='';
var $cert='';
var $certpass='';
var $verifypeer=1;
var $verifyhost=1;
var $no_multicall=false;
function xmlrpc_client($path, $server, $port=0)
{
$this->port=$port; $this->server=$server; $this->path=$path;
}
function setDebug($in)
{
if ($in)
{
$this->debug=1;
}
else
{
$this->debug=0;
}
}
function setCredentials($u, $p)
{
$this->username=$u;
$this->password=$p;
}
function setCertificate($cert, $certpass)
{
$this->cert = $cert;
$this->certpass = $certpass;
}
function setSSLVerifyPeer($i)
{
$this->verifypeer = $i;
}
function setSSLVerifyHost($i)
{
$this->verifyhost = $i;
}
function send($msg, $timeout=0, $method='http')
{
if (is_array($msg))
{
// $msg is an array of xmlrpcmsg's
return $this->multicall($msg, $timeout, $method);
}
// where msg is an xmlrpcmsg
$msg->debug=$this->debug;
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, $xmlrpcName, $xmlrpcVersion, $xmlrpc_defencoding;
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)
{
if ($timeout>0 && function_exists('stream_set_timeout'))
stream_set_timeout($fp, $timeout);
}
else
{
$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\n" .
"User-Agent: " . $xmlrpcName . " " . $xmlrpcVersion . "\r\n" .
"Host: ". $server . "\r\n" .
$credentials .
"Accept-Charset: " . $xmlrpc_defencoding . "\r\n" .
"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, $xmlrpcVersion, $xmlrpc_internalencoding;
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';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -