X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2FClient.php;h=e29ba20b0c406470be66bd283e42887e9705fc4e;hb=537abd434045577958c4b3e2ce77f71358c3341c;hp=46984785baa9c7523dfc6ebaf28806da53643617;hpb=19d9e6a3f9c89251656c6fb1a1d7478af4656379;p=plcapi.git diff --git a/src/Client.php b/src/Client.php index 4698478..e29ba20 100644 --- a/src/Client.php +++ b/src/Client.php @@ -9,6 +9,10 @@ use PhpXmlRpc\Helper\Logger; */ class Client { + const USE_CURL_NEVER = 0; + const USE_CURL_ALWAYS = 1; + const USE_CURL_AUTO = 2; + /// @todo: do these need to be public? public $method = 'http'; public $server; @@ -41,6 +45,7 @@ class Client public $cookies = array(); public $extracurlopts = array(); + public $use_curl = self::USE_CURL_AUTO; /** * @var bool @@ -56,10 +61,9 @@ class Client * List of http compression methods accepted by the client for responses. * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib. * - * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since - * in those cases it will be up to CURL to decide the compression methods - * it supports. You might check for the presence of 'zlib' in the output of - * curl_version() to determine wheter compression is supported or not + * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since in those cases it will be up to CURL to + * decide the compression methods it supports. You might check for the presence of 'zlib' in the output of + * curl_version() to determine whether compression is supported or not */ public $accepted_compression = array(); @@ -67,11 +71,12 @@ class Client * Name of compression scheme to be used for sending requests. * Either null, gzip or deflate. */ - public $request_compression = ''; + /** * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see: * http://curl.haxx.se/docs/faq.html#7.3). + * @internal */ public $xmlrpc_curl_handle = null; @@ -177,7 +182,7 @@ class Client $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); // Add all charsets which mbstring can handle, but remove junk not found in IANA registry at - // in http://www.iana.org/assignments/character-sets/character-sets.xhtml + // http://www.iana.org/assignments/character-sets/character-sets.xhtml // NB: this is disabled to avoid making all the requests sent huge... mbstring supports more than 80 charsets! /*if (function_exists('mb_list_encodings')) { @@ -201,7 +206,7 @@ class Client * This option can be very useful when debugging servers as it allows you to see exactly what the client sends and * the server returns. * - * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) + * @param integer $level values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) */ public function setDebug($level) { @@ -390,7 +395,7 @@ class Client */ public function setCookie($name, $value = '', $path = '', $domain = '', $port = null) { - $this->cookies[$name]['value'] = urlencode($value); + $this->cookies[$name]['value'] = rawurlencode($value); if ($path || $domain || $port) { $this->cookies[$name]['path'] = $path; $this->cookies[$name]['domain'] = $domain; @@ -413,6 +418,15 @@ class Client $this->extracurlopts = $options; } + /** + * @param int $useCurlMode self::USE_CURL_ALWAYS, self::USE_CURL_AUTO or self::USE_CURL_NEVER + */ + public function setUseCurl($useCurlMode) + { + $this->use_curl = $useCurlMode; + } + + /** * Set user-agent string that will be used by this client instance in http headers sent to the server. * @@ -473,8 +487,9 @@ class Client $req->setDebug($this->debug); /// @todo we could be smarter about this and force usage of curl in scenarios where it is both available and - /// needed, such as digest or ntlm auth - $useCurl = ($method == 'https' || $method == 'http11'); + /// needed, such as digest or ntlm auth. Do not attempt to use it for https if not present + $useCurl = ($this->use_curl == self::USE_CURL_ALWAYS) || ($this->use_curl == self::USE_CURL_AUTO && + ($method == 'https' || $method == 'http11')); if ($useCurl) { $r = $this->sendPayloadCURL( @@ -521,7 +536,6 @@ class Client $this->proxy_pass, $this->proxy_authtype, $method, - $this->keepalive, $this->key, $this->keypass, $this->sslversion @@ -552,8 +566,10 @@ class Client $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method='http') { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + return $this->sendPayloadSocket($req, $server, $port, $timeout, $username, $password, $authType, null, null, - null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType); + null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, $method); } /** @@ -585,6 +601,8 @@ class Client $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '', $sslVersion = 0) { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + return $this->sendPayloadCURL($req, $server, $port, $timeout, $username, $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, 'https', $keepAlive, $key, $keyPass, $sslVersion); @@ -629,6 +647,7 @@ class Client $payload = $req->payload; // Deflate request body and set appropriate request headers + $encodingHdr = ''; if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) { if ($this->request_compression == 'gzip') { $a = @gzencode($payload); @@ -643,8 +662,6 @@ class Client $encodingHdr = "Content-Encoding: deflate\r\n"; } } - } else { - $encodingHdr = ''; } // thanks to Grant Rauscher for this @@ -652,7 +669,7 @@ class Client if ($username != '') { $credentials = 'Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; if ($authType != 1) { - error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0'); + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0'); } } @@ -672,20 +689,18 @@ class Client $uri = 'http://' . $server . ':' . $port . $this->path; if ($proxyUsername != '') { if ($proxyAuthType != 1) { - error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported with HTTP 1.0'); + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported with HTTP 1.0'); } $proxyCredentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyUsername . ':' . $proxyPassword) . "\r\n"; } } else { $connectServer = $server; $connectPort = $port; - /// @todo if supporting https, we should support all its current options as well: peer name verification etc... $transport = ( $method === 'https' ) ? 'tls' : 'tcp'; $uri = $this->path; } - // Cookie generation, as per rfc2965 (version 1 cookies) or - // netscape's rules (version 0 cookies) + // Cookie generation, as per rfc2965 (version 1 cookies) or netscape's rules (version 0 cookies) $cookieHeader = ''; if (count($this->cookies)) { $version = ''; @@ -751,7 +766,7 @@ class Client $contextOptions['ssl']['local_pk'] = $key; } $contextOptions['ssl']['verify_peer'] = $this->verifypeer; - + $contextOptions['ssl']['verify_peer_name'] = $this->verifypeer; } $context = stream_context_create($contextOptions); @@ -761,6 +776,9 @@ class Client $connectTimeout = $timeout; } + $this->errno = 0; + $this->errstr = ''; + $fp = @stream_socket_client("$transport://$connectServer:$connectPort", $this->errno, $this->errstr, $connectTimeout, STREAM_CLIENT_CONNECT, $context); if ($fp) { @@ -768,6 +786,10 @@ class Client stream_set_timeout($fp, $timeout); } } else { + if ($this->errstr == '') { + $err = error_get_last(); + $this->errstr = $err['message']; + } $this->errstr = 'Connect error: ' . $this->errstr; $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')'); @@ -780,12 +802,10 @@ class Client $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr); return $r; - } else { - // reset errno and errstr on successful socket connection - $this->errstr = ''; } - // G. Giunta 2005/10/24: close socket before parsing. - // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects) + + // Close socket before parsing. + // It should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects) $ipd = ''; do { // shall we check for $data === FALSE? @@ -793,6 +813,7 @@ class Client $ipd .= fread($fp, 32768); } while (!feof($fp)); fclose($fp); + $r = $req->parseResponse($ipd, false, $this->return_type); return $r; @@ -912,8 +933,7 @@ class Client curl_setopt($curl, CURLOPT_HEADER, 1); // 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... + // ALL methods it is supporting. This is possibly a better option than letting the user tell what curl can / cannot do... if (is_array($this->accepted_compression) && count($this->accepted_compression)) { //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression)); // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?) @@ -955,7 +975,7 @@ class Client if (defined('CURLOPT_HTTPAUTH')) { curl_setopt($curl, CURLOPT_HTTPAUTH, $authType); } elseif ($authType != 1) { - error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install'); + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install'); } } @@ -985,7 +1005,8 @@ class Client if ($keyPass) { curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keyPass); } - // 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 + // 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); @@ -1002,14 +1023,13 @@ class Client if (defined('CURLOPT_PROXYAUTH')) { curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyAuthType); } elseif ($proxyAuthType != 1) { - error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported by the current PHP/curl install'); + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported by the current PHP/curl install'); } } } // NB: should we build cookie http headers by hand rather than let CURL do it? - // the following code does not honour 'expires', 'path' and 'domain' cookie attributes - // set to client obj the the user... + // the following code does not honour 'expires', 'path' and 'domain' cookie attributes set to client obj the the user... if (count($this->cookies)) { $cookieHeader = ''; foreach ($this->cookies as $name => $cookie) { @@ -1239,10 +1259,12 @@ class Client break; case 'struct': $code = $val['faultCode']; + /** @var Value $code */ if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') { return false; } $str = $val['faultString']; + /** @var Value $str */ if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') { return false; }