X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fxmlrpc.inc;h=13bcdfcca7a287f9c364f83eb5e7f70c6c474bff;hb=29fddbc6b33b742c79fc142c4743c00d15ec9465;hp=ea775f82e6daeba0f8a8799f8d4e5ba3401716d9;hpb=aafee68e34ad3b334b55dc5f22f90db985b53c6a;p=plcapi.git diff --git a/lib/xmlrpc.inc b/lib/xmlrpc.inc index ea775f8..13bcdfc 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'; + $GLOBALS['xmlrpcVersion']='3.1.2'; // 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: @@ -821,6 +821,7 @@ var $keypass=''; var $verifypeer=true; var $verifyhost=1; + var $sslversion=0; // corresponds to CURL_SSLVERSION_DEFAULT var $no_multicall=false; var $proxy=''; var $proxyport=0; @@ -872,7 +873,7 @@ * @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 == '') @@ -942,6 +943,14 @@ $this->user_agent = $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion']; } + /** + * @deprecated + */ + function xmlrpc_client($path, $server='', $port='', $method='') + { + self::__construct($path, $server, $port, $method); + } + /** * Enables/disables the echoing to screen of the xmlrpc responses received * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) @@ -1031,6 +1040,16 @@ $this->verifyhost = $i; } + /** + * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value ): let cURL decide + * + * @param int $i + */ + public function setSSLVersion($i) + { + $this->sslversion = $i; + } + /** * Set proxy info * @param string $proxyhost @@ -1182,7 +1201,8 @@ $this->proxy_authtype, $this->keepalive, $this->key, - $this->keypass + $this->keypass, + $this->sslversion ); } elseif($method == 'http11') @@ -1423,11 +1443,11 @@ function &sendPayloadHTTPS($msg, $server, $port, $timeout=0, $username='', $password='', $authtype=1, $cert='',$certpass='', $cacert='', $cacertdir='', $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, - $keepalive=false, $key='', $keypass='') + $keepalive=false, $key='', $keypass='', $sslVersion = 0) { $r =& $this->sendPayloadCURL($msg, $server, $port, $timeout, $username, $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport, - $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass); + $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass, $sslVersion); return $r; } @@ -1440,7 +1460,7 @@ function &sendPayloadCURL($msg, $server, $port, $timeout=0, $username='', $password='', $authtype=1, $cert='', $certpass='', $cacert='', $cacertdir='', $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, $method='https', - $keepalive=false, $key='', $keypass='') + $keepalive=false, $key='', $keypass='', $sslVersion = 0) { if(!function_exists('curl_init')) { @@ -1621,8 +1641,17 @@ { 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); + // allow usage of different SSL versions + curl_setopt($curl, CURLOPT_SSLVERSION, $sslVersion); } // proxy info @@ -1700,12 +1729,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; - } + // 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; } @@ -1961,7 +1990,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) { @@ -1999,6 +2028,14 @@ } } + /** + * @deprecated + */ + function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='') + { + self::__construct($val, $fcode, $fstr, $valtyp); + } + /** * Returns the error code of the response. * @return integer the error code of this response (0 for not-error responses) @@ -2116,7 +2153,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha * @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 xmlrpcmsg($meth, $pars=0) + function __construct($meth, $pars=0) { $this->methodname=$meth; if(is_array($pars) && count($pars)>0) @@ -2128,6 +2165,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha } } + /** + * @deprecated + */ + function xmlrpcmsg($meth, $pars=0) + { + self::__construct($meth, $pars); + } + /** * @access private */ @@ -2169,7 +2214,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++) { @@ -2357,7 +2402,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha } // be tolerant to line endings, and extra empty lines $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); - while(list(,$line) = @each($ar)) + foreach($ar as $line) { // take care of multi-line headers and cookies $arr = explode(':',$line,2); @@ -2408,7 +2453,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; } } } @@ -2592,17 +2637,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. @@ -2632,7 +2684,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha xml_set_default_handler($parser, 'xmlrpc_dh'); // first error check: xml not well formed - if(!xml_parse($parser, $data, count($data))) + if(!xml_parse($parser, $data, 1)) { // thanks to Peter Kocks if((xml_get_current_line_number($parser)) == 1) @@ -2741,7 +2793,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha * @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 @@ -2795,6 +2847,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha } } + /** + * @deprecated + */ + function xmlrpcval($val=-1, $type='') + { + self::__construct($val, $type); + } + /** * Add a single php value to an (unitialized) xmlrpcval * @param mixed $val @@ -2908,7 +2968,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha } // poor man's version of print_r ??? - // DEPRECATED! + /// @deprecated function dump($ar) { foreach($ar as $key => $val) @@ -2916,7 +2976,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha echo "$key => $val
"; if($key == 'array') { - while(list($key2, $val2) = each($val)) + foreach($val as $key2 => $val2) { echo "-- $key2 => $val2
"; } @@ -3062,21 +3122,20 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) //{ - reset($this->me); - list($typ, $val) = each($this->me); + $val = reset($this->me); + $typ = key($this->me); return '' . $this->serializedata($typ, $val, $charset_encoding) . "\n"; //} } - // DEPRECATED + /// @deprecated function serializeval($o) { // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) //{ - $ar=$o->me; - reset($ar); - list($typ, $val) = each($ar); + $val = reset($o->me); + $typ = key($o->me); return '' . $this->serializedata($typ, $val) . "\n"; //} } @@ -3118,19 +3177,20 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha * Return next member element for xmlrpcvals of type struct. * @return xmlrpcval * @access public + * @deprecated this will be removed in future versions */ function structeach() { - return each($this->me['struct']); + return @each($this->me['struct']); } - // DEPRECATED! this code looks like it is very fragile and has not been fixed + // this code looks like it is very fragile and has not been fixed // for a long long time. Shall we remove it for 2.0? + /// @deprecated function getval() { // UNSTABLE - reset($this->me); - list($a,$b)=each($this->me); + $b = reset($this->me); // contributed by I Sofer, 2001-03-24 // add support for nested arrays to scalarval // i've created a new method here, so as to @@ -3138,8 +3198,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha if(is_array($b)) { - @reset($b); - while(list($id,$cont) = @each($b)) + foreach($b as $id => $cont) { $b[$id] = $cont->scalarval(); } @@ -3149,13 +3208,11 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha if(is_object($b)) { $t = get_object_vars($b); - @reset($t); - while(list($id,$cont) = @each($t)) + foreach($t as $id => $cont) { $t[$id] = $cont->scalarval(); } - @reset($t); - while(list($id,$cont) = @each($t)) + foreach($t as $id => $cont) { @$b->$id = $cont; } @@ -3171,8 +3228,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha */ function scalarval() { - reset($this->me); - list(,$b)=each($this->me); + $b = reset($this->me); return $b; } @@ -3185,7 +3241,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha function scalartyp() { reset($this->me); - list($a,)=each($this->me); + $a = key($this->me); if($a==$GLOBALS['xmlrpcI4']) { $a=$GLOBALS['xmlrpcInt']; @@ -3318,8 +3374,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha case 'scalar': if (in_array('extension_api', $options)) { - reset($xmlrpc_val->me); - list($typ,$val) = each($xmlrpc_val->me); + $val = reset($xmlrpc_val->me); + $typ = key($xmlrpc_val->me); switch ($typ) { case 'dateTime.iso8601': @@ -3492,8 +3548,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha else { $arr = array(); - reset($php_val); - while(list($k,$v) = each($php_val)) + foreach($php_val as $k => $v) { $arr[$k] = php_xmlrpc_encode($v, $options); } @@ -3559,7 +3614,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? @@ -3772,6 +3847,43 @@ 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 @@ -3802,4 +3914,4 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha } } -?> \ No newline at end of file +?>