X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fxmlrpc.inc;h=41dae8bbfd73e3762e410ebe396a481231648608;hb=f49e4f7f149082ee5f029e4693cb94bf3a244198;hp=5b39a26753dae92a06ded03f3535f59868b26a81;hpb=05102eb7d8c0faad30ae3719e2045c1af72b9e9d;p=plcapi.git
diff --git a/lib/xmlrpc.inc b/lib/xmlrpc.inc
index 5b39a26..41dae8b 100644
--- a/lib/xmlrpc.inc
+++ b/lib/xmlrpc.inc
@@ -208,7 +208,7 @@
$GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
$GLOBALS['xmlrpcName']='XML-RPC for PHP';
- $GLOBALS['xmlrpcVersion']='3.0.0.beta';
+ $GLOBALS['xmlrpcVersion']='3.0.1';
// let user errors start at 800
$GLOBALS['xmlrpcerruser']=800;
@@ -225,7 +225,7 @@
// set to TRUE to enable encoding of php NULL values to instead of
$GLOBALS['xmlrpc_null_apache_encoding']=false;
- $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions';
+ $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions';
// used to store state during parsing
// quick explanation of components:
@@ -850,7 +850,7 @@
* http://curl.haxx.se/docs/faq.html#7.3)
*/
var $xmlrpc_curl_handle = null;
- /// Wheter to use persistent connections for http 1.1 and https
+ /// Whether to use persistent connections for http 1.1 and https
var $keepalive = false;
/// Charset encodings that can be decoded without problems by the client
var $accepted_charset_encodings = array();
@@ -866,13 +866,21 @@
*/
var $user_agent;
+ /**
+ * @deprecated
+ */
+ function xmlrpc_client($path, $server='', $port='', $method='')
+ {
+ self::__construct($path, $server, $port, $method);
+ }
+
/**
* @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php
* @param string $server the server name / ip address
* @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used
* @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed
*/
- function xmlrpc_client($path, $server='', $port='', $method='')
+ function __construct($path, $server='', $port='', $method='')
{
// allow user to specify all params in $path
if($server == '' and $port == '' and $method == '')
@@ -944,7 +952,7 @@
/**
* Enables/disables the echoing to screen of the xmlrpc responses received
- * @param integer $debug values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
+ * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
* @access public
*/
function setDebug($in)
@@ -980,7 +988,7 @@
/**
* Add a CA certificate to verify server with (see man page about
- * CURLOPT_CAINFO for more details
+ * CURLOPT_CAINFO for more details)
* @param string $cacert certificate file name (or dir holding certificates)
* @param bool $is_dir set to true to indicate cacert is a dir. defaults to false
* @access public
@@ -1112,7 +1120,7 @@
/**
* Directly set cURL options, for extra flexibility
* It allows eg. to bind client to a specific IP interface / address
- * @param $options array
+ * @param array $options
*/
function SetCurlOptions( $options )
{
@@ -1399,11 +1407,11 @@
}
else
{
- // reset errno and errstr on succesful socket connection
+ // reset errno and errstr on successful socket connection
$this->errstr = '';
}
// G. Giunta 2005/10/24: close socket before parsing.
- // should yeld slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
+ // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
$ipd='';
do
{
@@ -1541,7 +1549,6 @@
// return the header too
curl_setopt($curl, CURLOPT_HEADER, 1);
- // will only work with PHP >= 5.0
// NB: if we set an empty string, CURL will add http header indicating
// ALL methods it is supporting. This is possibly a better option than
// letting the user tell what curl can / cannot do...
@@ -1622,6 +1629,13 @@
{
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass);
}
+
+ // Upgrade transparently to more stringent check for versions of php which do not support otherwise.
+ // Doing it in constructor would be cleaner; doing it here saves us a couple of function calls
+ if($this->verifyhost == 1 && $info = curl_version() && version_compare($info['version'], '7.28.1') >= 0)
+ {
+ $this->verifyhost = 2;
+ }
// 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);
}
@@ -1701,6 +1715,12 @@
curl_close($curl);
}
$resp =& $msg->parseResponse($result, true, $this->return_type);
+ // if we got back a 302, we can not reuse the curl handle for later calls
+ if($resp->faultCode() == $GLOBALS['xmlrpcerr']['http_error'] && $keepalive)
+ {
+ curl_close($curl);
+ $this->xmlrpc_curl_handle = null;
+ }
}
return $resp;
}
@@ -1723,7 +1743,7 @@
* @param array $msgs an array of xmlrpcmsg objects
* @param integer $timeout connection timeout (in seconds)
* @param string $method the http protocol variant to be used
- * @param boolean fallback When true, upon receiveing an error during multicall, multiple single calls will be attempted
+ * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted
* @return array
* @access public
*/
@@ -1946,6 +1966,14 @@
var $content_type = 'text/xml';
var $raw_data = '';
+ /**
+ * @deprecated
+ */
+ function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='')
+ {
+ self::__construct($val, $fcode, $fstr, $valtyp);
+ }
+
/**
* @param mixed $val either an xmlrpcval obj, a php value or the xml serialization of an xmlrpcval (a string)
* @param integer $fcode set it to anything but 0 to create an error response
@@ -1956,7 +1984,7 @@
* NB: as of now we do not do it, since it might be either an xmlrpcval or a plain
* php val, or a complete xml chunk, depending on usage of xmlrpc_client::send() inside which creator is called...
*/
- function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='')
+ function __construct($val, $fcode = 0, $fstr = '', $valtyp='')
{
if($fcode != 0)
{
@@ -2030,7 +2058,7 @@
* with attributes being e.g. 'expires', 'path', domain'.
* NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past)
* are still present in the array. It is up to the user-defined code to decide
- * how to use the received cookies, and wheter they have to be sent back with the next
+ * how to use the received cookies, and whether they have to be sent back with the next
* request to the server (using xmlrpc_client::setCookie) or not
* @return array array of cookies received from the server
* @access public
@@ -2108,10 +2136,18 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
var $content_type = 'text/xml';
/**
- * @param string $meth the name of the method to invoke
- * @param array $pars array of parameters to be paased to the method (xmlrpcval objects)
+ * @deprecated
*/
function xmlrpcmsg($meth, $pars=0)
+ {
+ self::__construct($meth, $pars);
+ }
+
+ /**
+ * @param string $meth the name of the method to invoke
+ * @param array $pars array of parameters to be passed to the method (xmlrpcval objects)
+ */
+ function __construct($meth, $pars=0)
{
$this->methodname=$meth;
if(is_array($pars) && count($pars)>0)
@@ -2164,7 +2200,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
else
$this->content_type = 'text/xml';
$this->payload=$this->xml_header($charset_encoding);
- $this->payload.='' . $this->methodname . "\n";
+ $this->payload.='' . xmlrpc_encode_entitites($this->methodname, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "\n";
$this->payload.="\n";
for($i=0; $iparams); $i++)
{
@@ -2193,6 +2229,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
/**
* Returns xml representation of the message. XML prologue included
+ * @param string $charset_encoding
* @return string the xml representation of the message, xml prologue included
* @access public
*/
@@ -2246,6 +2283,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
* infinite loop in that case, because we cannot trust the caller
* to give us a valid pointer to an open file...
* @access public
+ * @param resource $fp stream pointer
* @return xmlrpcresp
* @todo add 2nd & 3rd param to be passed to ParseResponse() ???
*/
@@ -2401,7 +2439,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
{
if ($tag != 'value')
{
- $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val;
+ $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val;
}
}
}
@@ -2585,17 +2623,24 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
$GLOBALS['_xh']['isf_reason']='';
$GLOBALS['_xh']['rt']=''; // 'methodcall or 'methodresponse'
- // if response charset encoding is not known / supported, try to use
- // the default encoding and parse the xml anyway, but log a warning...
- if (!in_array($resp_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))
- // the following code might be better for mb_string enabled installs, but
+ // Since parsing will fail if charset is not specified in the xml prologue,
+ // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
+ // The following code might be better for mb_string enabled installs, but
// makes the lib about 200% slower...
- //if (!is_valid_charset($resp_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))
- {
- error_log('XML-RPC: '.__METHOD__.': invalid charset encoding of received response: '.$resp_encoding);
- $resp_encoding = $GLOBALS['xmlrpc_defencoding'];
+ //if (!is_valid_charset($resp_encoding, array('UTF-8')))
+ if (!in_array($resp_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) {
+ if ($resp_encoding == 'ISO-8859-1') {
+ $data = utf8_encode($data);
+ } else {
+ if (extension_loaded('mbstring')) {
+ $data = mb_convert_encoding($data, 'UTF-8', $resp_encoding);
+ } else {
+ error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $resp_encoding);
+ }
+ }
}
- $parser = xml_parser_create($resp_encoding);
+
+ $parser = xml_parser_create();
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.
@@ -2730,11 +2775,19 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
var $mytype=0;
var $_php_class=null;
+ /**
+ * @deprecated
+ */
+ function xmlrpcval($val=-1, $type='')
+ {
+ self::__construct($val, $type);
+ }
+
/**
* @param mixed $val
* @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed
*/
- function xmlrpcval($val=-1, $type='')
+ function __construct($val=-1, $type='')
{
/// @todo: optimization creep - do not call addXX, do it all inline.
/// downside: booleans will not be coerced anymore
@@ -3075,7 +3128,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
}
/**
- * Checks wheter a struct member with a given name is present.
+ * Checks whether a struct member with a given name is present.
* Works only on xmlrpcvals of type struct.
* @param string $m the name of the struct member to be looked up
* @return boolean
@@ -3552,7 +3605,27 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
$GLOBALS['_xh']['isf_reason'] = '';
$GLOBALS['_xh']['method'] = false;
$GLOBALS['_xh']['rt'] = '';
- /// @todo 'guestimate' encoding
+
+ // 'guestimate' encoding
+ $val_encoding = guess_encoding('', $xml_val);
+
+ // Since parsing will fail if charset is not specified in the xml prologue,
+ // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
+ // The following code might be better for mb_string enabled installs, but
+ // makes the lib about 200% slower...
+ //if (!is_valid_charset($val_encoding, array('UTF-8')))
+ if (!in_array($val_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($xml_val)) {
+ if ($val_encoding == 'ISO-8859-1') {
+ $xml_val = utf8_encode($xml_val);
+ } else {
+ if (extension_loaded('mbstring')) {
+ $xml_val = mb_convert_encoding($xml_val, 'UTF-8', $val_encoding);
+ } else {
+ error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $val_encoding);
+ }
+ }
+ }
+
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
// What if internal encoding is not in one of the 3 allowed?
@@ -3674,9 +3747,10 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
* we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of unconforming (legacy?) clients/servers,
* which will be most probably using UTF-8 anyway...
*
- * @param string $httpheaders the http Content-type header
+ * @param string $httpheader the http Content-type header
* @param string $xmlchunk xml content buffer
* @param string $encoding_prefs comma separated list of character encodings to be used as default (when mb extension is enabled)
+ * @return string
*
* @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!!
*/
@@ -3764,11 +3838,49 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
}
}
+ /**
+ * Helper function: checks if an xml chunk as a charset declaration (BOM or in the xml declaration)
+ *
+ * @param string $xmlChunk
+ * @return bool
+ */
+ function has_encoding($xmlChunk)
+ {
+ // scan the first bytes of the data for a UTF-16 (or other) BOM pattern
+ // (source: http://www.w3.org/TR/2000/REC-xml-20001006)
+ if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk))
+ {
+ return true;
+ }
+ elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk))
+ {
+ return true;
+ }
+ elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk))
+ {
+ return true;
+ }
+
+ // test if encoding is specified in the xml declaration
+ // Details:
+ // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+
+ // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]*
+ if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" .
+ '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/",
+ $xmlChunk, $matches))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
/**
* Checks if a given charset encoding is present in a list of encodings or
* if it is a valid subset of any encoding in the list
* @param string $encoding charset to be tested
* @param mixed $validlist comma separated list of valid charsets (or array of charsets)
+ * @return bool
*/
function is_valid_charset($encoding, $validlist)
{
@@ -3789,7 +3901,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
foreach ($validlist as $allowed)
if (in_array($allowed, $charset_supersets[$encoding]))
return true;
- return false;
+ return false;
}
}