X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2FClient.php;h=cd1491c327aef00fb80e8bb8a1fcd090327eccfe;hb=e01e1ef7d893dde2dd3aa1f314fdab67770d8908;hp=6fc8950c981de24f62d21b93c4e66f9074262b4f;hpb=320bc7777a384926f194839611a1994afa8bf222;p=plcapi.git diff --git a/src/Client.php b/src/Client.php index 6fc8950..cd1491c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,7 +3,7 @@ namespace PhpXmlRpc; use PhpXmlRpc\Helper\Logger; - +use PhpXmlRpc\Helper\XMLParser; /** * Used to represent a client of an XML-RPC server. */ @@ -13,6 +13,8 @@ class Client const USE_CURL_ALWAYS = 1; const USE_CURL_AUTO = 2; + protected static $logger; + /// @todo: do these need to be public? public $method = 'http'; public $server; @@ -61,10 +63,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(); @@ -72,12 +73,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; @@ -89,10 +90,11 @@ class Client /** * The charset encoding that will be used for serializing request sent by the client. - * It defaults to NULL, which means using US-ASCII and encoding all characters outside of the ASCII range using - * their xml character entity representation (this has the benefit that line end characters will not be mangled in - * the transfer, a CR-LF will be preserved as well as a singe LF). - * Valid values are 'US-ASCII', 'UTF-8' and 'ISO-8859-1' + * It defaults to NULL, which means using US-ASCII and encoding all characters outside of the ASCII printable range + * using their xml character entity representation (this has the benefit that line end characters will not be mangled + * in the transfer, a CR-LF will be preserved as well as a singe LF). + * Valid values are 'US-ASCII', 'UTF-8' and 'ISO-8859-1'. + * For the fastest mode of operation, set your both your app internal encoding as well as this to UTF-8. */ public $request_charset_encoding = ''; @@ -109,13 +111,26 @@ class Client * response will be lost. It will be e.g. impossible to tell whether a particular php string value was sent by the * server as an xmlrpc string or base64 value. */ - public $return_type = 'xmlrpcvals'; + public $return_type = XMLParser::RETURN_XMLRPCVALS; /** * Sent to servers in http headers. */ public $user_agent; + public function getLogger() + { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + return self::$logger; + } + + public static function setLogger($logger) + { + self::$logger = $logger; + } + /** * @param string $path either the PATH part of the xmlrpc server URL, or complete server URL (in which case you * should use and empty string for all other parameters) @@ -169,7 +184,7 @@ class Client // if ZLIB is enabled, let the client by default accept compressed responses if (function_exists('gzinflate') || ( - function_exists('curl_init') && (($info = curl_version()) && + function_exists('curl_version') && (($info = curl_version()) && ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version']))) ) ) { @@ -310,7 +325,7 @@ class Client } /** - * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value ): let cURL decide + * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value): let cURL decide * * @param int $i */ @@ -396,7 +411,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; @@ -464,6 +479,7 @@ class Client * chosen during creation of the object will be used. * * @return Response|Response[] Note that the client will always return a Response object, even if the call fails + * @todo allow throwing exceptions instead of returning responses in case of failed calls and/or Fault responses */ public function send($req, $timeout = 0, $method = '') { @@ -537,7 +553,6 @@ class Client $this->proxy_pass, $this->proxy_authtype, $method, - $this->keepalive, $this->key, $this->keypass, $this->sslversion @@ -568,8 +583,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); } /** @@ -601,6 +618,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); @@ -640,11 +659,12 @@ class Client // Only create the payload if it was not created previously if (empty($req->payload)) { - $req->createPayload($this->request_charset_encoding); + $req->serialize($this->request_charset_encoding); } $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); @@ -659,8 +679,6 @@ class Client $encodingHdr = "Content-Encoding: deflate\r\n"; } } - } else { - $encodingHdr = ''; } // thanks to Grant Rauscher for this @@ -668,7 +686,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'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0'); } } @@ -688,7 +706,7 @@ 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'); + $this->getLogger()->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"; } @@ -699,8 +717,7 @@ class Client $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 = ''; @@ -745,7 +762,7 @@ class Client $payload; if ($this->debug > 1) { - Logger::instance()->debugMessage("---SENDING---\n$op\n---END---"); + $this->getLogger()->debugMessage("---SENDING---\n$op\n---END---"); } $contextOptions = array(); @@ -766,8 +783,9 @@ 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); if ($timeout <= 0) { @@ -776,6 +794,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) { @@ -783,6 +804,11 @@ 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 . ')'); @@ -795,12 +821,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? @@ -808,6 +832,7 @@ class Client $ipd .= fread($fp, 32768); } while (!feof($fp)); fclose($fp); + $r = $req->parseResponse($ipd, false, $this->return_type); return $r; @@ -869,7 +894,7 @@ class Client // Only create the payload if it was not created previously if (empty($req->payload)) { - $req->createPayload($this->request_charset_encoding); + $req->serialize($this->request_charset_encoding); } // Deflate request body and set appropriate request headers @@ -893,7 +918,7 @@ class Client } if ($this->debug > 1) { - Logger::instance()->debugMessage("---SENDING---\n$payload\n---END---"); + $this->getLogger()->debugMessage("---SENDING---\n$payload\n---END---"); } if (!$keepAlive || !$this->xmlrpc_curl_handle) { @@ -927,8 +952,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 ?) @@ -970,7 +994,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'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install'); } } @@ -1000,7 +1024,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); @@ -1017,14 +1042,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'); + $this->getLogger()->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) { @@ -1048,7 +1072,7 @@ class Client $message .= $name . ': ' . $val . "\n"; } $message .= '---END---'; - Logger::instance()->debugMessage($message); + $this->getLogger()->debugMessage($message); } if (!$result) { @@ -1254,10 +1278,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; }