+++ /dev/null
-<?php\r
-/**\r
- * Class for performing HTTP requests\r
- *\r
- * PHP versions 4 and 5\r
- *\r
- * LICENSE:\r
- *\r
- * Copyright (c) 2002-2007, Richard Heyes\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * o Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * o Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * o The names of the authors may not be used to endorse or promote\r
- * products derived from this software without specific prior written\r
- * permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request\r
- * @author Richard Heyes <richard@phpguru.org>\r
- * @author Alexey Borzov <avb@php.net>\r
- * @copyright 2002-2007 Richard Heyes\r
- * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
- * @version CVS: $Id: Request.php,v 1.63 2008/10/11 11:07:10 avb Exp $\r
- * @link http://pear.php.net/package/HTTP_Request/\r
- */\r
-\r
-/**\r
- * PEAR and PEAR_Error classes (for error handling)\r
- */\r
-require_once 'PEAR.php';\r
-/**\r
- * Socket class\r
- */\r
-require_once 'Net/Socket.php';\r
-/**\r
- * URL handling class\r
- */\r
-require_once 'Net/URL.php';\r
-\r
-/**#@+\r
- * Constants for HTTP request methods\r
- */\r
-define('HTTP_REQUEST_METHOD_GET', 'GET', true);\r
-define('HTTP_REQUEST_METHOD_HEAD', 'HEAD', true);\r
-define('HTTP_REQUEST_METHOD_POST', 'POST', true);\r
-define('HTTP_REQUEST_METHOD_PUT', 'PUT', true);\r
-define('HTTP_REQUEST_METHOD_DELETE', 'DELETE', true);\r
-define('HTTP_REQUEST_METHOD_OPTIONS', 'OPTIONS', true);\r
-define('HTTP_REQUEST_METHOD_TRACE', 'TRACE', true);\r
-/**#@-*/\r
-\r
-/**#@+\r
- * Constants for HTTP request error codes\r
- */\r
-define('HTTP_REQUEST_ERROR_FILE', 1);\r
-define('HTTP_REQUEST_ERROR_URL', 2);\r
-define('HTTP_REQUEST_ERROR_PROXY', 4);\r
-define('HTTP_REQUEST_ERROR_REDIRECTS', 8);\r
-define('HTTP_REQUEST_ERROR_RESPONSE', 16);\r
-define('HTTP_REQUEST_ERROR_GZIP_METHOD', 32);\r
-define('HTTP_REQUEST_ERROR_GZIP_READ', 64);\r
-define('HTTP_REQUEST_ERROR_GZIP_DATA', 128);\r
-define('HTTP_REQUEST_ERROR_GZIP_CRC', 256);\r
-/**#@-*/\r
-\r
-/**#@+\r
- * Constants for HTTP protocol versions\r
- */\r
-define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true);\r
-define('HTTP_REQUEST_HTTP_VER_1_1', '1.1', true);\r
-/**#@-*/\r
-\r
-if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) {\r
- /**\r
- * Whether string functions are overloaded by their mbstring equivalents\r
- */\r
- define('HTTP_REQUEST_MBSTRING', true);\r
-} else {\r
- /**\r
- * @ignore\r
- */\r
- define('HTTP_REQUEST_MBSTRING', false);\r
-}\r
-\r
-/**\r
- * Class for performing HTTP requests\r
- *\r
- * Simple example (fetches yahoo.com and displays it):\r
- * <code>\r
- * $a = &new HTTP_Request('http://www.yahoo.com/');\r
- * $a->sendRequest();\r
- * echo $a->getResponseBody();\r
- * </code>\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request\r
- * @author Richard Heyes <richard@phpguru.org>\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 1.4.4\r
- */\r
-class HTTP_Request\r
-{\r
- /**#@+\r
- * @access private\r
- */\r
- /**\r
- * Instance of Net_URL\r
- * @var Net_URL\r
- */\r
- var $_url;\r
-\r
- /**\r
- * Type of request\r
- * @var string\r
- */\r
- var $_method;\r
-\r
- /**\r
- * HTTP Version\r
- * @var string\r
- */\r
- var $_http;\r
-\r
- /**\r
- * Request headers\r
- * @var array\r
- */\r
- var $_requestHeaders;\r
-\r
- /**\r
- * Basic Auth Username\r
- * @var string\r
- */\r
- var $_user;\r
-\r
- /**\r
- * Basic Auth Password\r
- * @var string\r
- */\r
- var $_pass;\r
-\r
- /**\r
- * Socket object\r
- * @var Net_Socket\r
- */\r
- var $_sock;\r
-\r
- /**\r
- * Proxy server\r
- * @var string\r
- */\r
- var $_proxy_host;\r
-\r
- /**\r
- * Proxy port\r
- * @var integer\r
- */\r
- var $_proxy_port;\r
-\r
- /**\r
- * Proxy username\r
- * @var string\r
- */\r
- var $_proxy_user;\r
-\r
- /**\r
- * Proxy password\r
- * @var string\r
- */\r
- var $_proxy_pass;\r
-\r
- /**\r
- * Post data\r
- * @var array\r
- */\r
- var $_postData;\r
-\r
- /**\r
- * Request body\r
- * @var string\r
- */\r
- var $_body;\r
-\r
- /**\r
- * A list of methods that MUST NOT have a request body, per RFC 2616\r
- * @var array\r
- */\r
- var $_bodyDisallowed = array('TRACE');\r
-\r
- /**\r
- * Methods having defined semantics for request body\r
- *\r
- * Content-Length header (indicating that the body follows, section 4.3 of\r
- * RFC 2616) will be sent for these methods even if no body was added\r
- *\r
- * @var array\r
- */\r
- var $_bodyRequired = array('POST', 'PUT');\r
-\r
- /**\r
- * Files to post\r
- * @var array\r
- */\r
- var $_postFiles = array();\r
-\r
- /**\r
- * Connection timeout.\r
- * @var float\r
- */\r
- var $_timeout;\r
-\r
- /**\r
- * HTTP_Response object\r
- * @var HTTP_Response\r
- */\r
- var $_response;\r
-\r
- /**\r
- * Whether to allow redirects\r
- * @var boolean\r
- */\r
- var $_allowRedirects;\r
-\r
- /**\r
- * Maximum redirects allowed\r
- * @var integer\r
- */\r
- var $_maxRedirects;\r
-\r
- /**\r
- * Current number of redirects\r
- * @var integer\r
- */\r
- var $_redirects;\r
-\r
- /**\r
- * Whether to append brackets [] to array variables\r
- * @var bool\r
- */\r
- var $_useBrackets = true;\r
-\r
- /**\r
- * Attached listeners\r
- * @var array\r
- */\r
- var $_listeners = array();\r
-\r
- /**\r
- * Whether to save response body in response object property\r
- * @var bool\r
- */\r
- var $_saveBody = true;\r
-\r
- /**\r
- * Timeout for reading from socket (array(seconds, microseconds))\r
- * @var array\r
- */\r
- var $_readTimeout = null;\r
-\r
- /**\r
- * Options to pass to Net_Socket::connect. See stream_context_create\r
- * @var array\r
- */\r
- var $_socketOptions = null;\r
- /**#@-*/\r
-\r
- /**\r
- * Constructor\r
- *\r
- * Sets up the object\r
- * @param string The url to fetch/access\r
- * @param array Associative array of parameters which can have the following keys:\r
- * <ul>\r
- * <li>method - Method to use, GET, POST etc (string)</li>\r
- * <li>http - HTTP Version to use, 1.0 or 1.1 (string)</li>\r
- * <li>user - Basic Auth username (string)</li>\r
- * <li>pass - Basic Auth password (string)</li>\r
- * <li>proxy_host - Proxy server host (string)</li>\r
- * <li>proxy_port - Proxy server port (integer)</li>\r
- * <li>proxy_user - Proxy auth username (string)</li>\r
- * <li>proxy_pass - Proxy auth password (string)</li>\r
- * <li>timeout - Connection timeout in seconds (float)</li>\r
- * <li>allowRedirects - Whether to follow redirects or not (bool)</li>\r
- * <li>maxRedirects - Max number of redirects to follow (integer)</li>\r
- * <li>useBrackets - Whether to append [] to array variable names (bool)</li>\r
- * <li>saveBody - Whether to save response body in response object property (bool)</li>\r
- * <li>readTimeout - Timeout for reading / writing data over the socket (array (seconds, microseconds))</li>\r
- * <li>socketOptions - Options to pass to Net_Socket object (array)</li>\r
- * </ul>\r
- * @access public\r
- */\r
- function HTTP_Request($url = '', $params = array())\r
- {\r
- $this->_method = HTTP_REQUEST_METHOD_GET;\r
- $this->_http = HTTP_REQUEST_HTTP_VER_1_1;\r
- $this->_requestHeaders = array();\r
- $this->_postData = array();\r
- $this->_body = null;\r
-\r
- $this->_user = null;\r
- $this->_pass = null;\r
-\r
- $this->_proxy_host = null;\r
- $this->_proxy_port = null;\r
- $this->_proxy_user = null;\r
- $this->_proxy_pass = null;\r
-\r
- $this->_allowRedirects = false;\r
- $this->_maxRedirects = 3;\r
- $this->_redirects = 0;\r
-\r
- $this->_timeout = null;\r
- $this->_response = null;\r
-\r
- foreach ($params as $key => $value) {\r
- $this->{'_' . $key} = $value;\r
- }\r
-\r
- if (!empty($url)) {\r
- $this->setURL($url);\r
- }\r
-\r
- // Default useragent\r
- $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )');\r
-\r
- // We don't do keep-alives by default\r
- $this->addHeader('Connection', 'close');\r
-\r
- // Basic authentication\r
- if (!empty($this->_user)) {\r
- $this->addHeader('Authorization', 'Basic ' . base64_encode($this->_user . ':' . $this->_pass));\r
- }\r
-\r
- // Proxy authentication (see bug #5913)\r
- if (!empty($this->_proxy_user)) {\r
- $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($this->_proxy_user . ':' . $this->_proxy_pass));\r
- }\r
-\r
- // Use gzip encoding if possible\r
- if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib')) {\r
- $this->addHeader('Accept-Encoding', 'gzip');\r
- }\r
- }\r
-\r
- /**\r
- * Generates a Host header for HTTP/1.1 requests\r
- *\r
- * @access private\r
- * @return string\r
- */\r
- function _generateHostHeader()\r
- {\r
- if ($this->_url->port != 80 AND strcasecmp($this->_url->protocol, 'http') == 0) {\r
- $host = $this->_url->host . ':' . $this->_url->port;\r
-\r
- } elseif ($this->_url->port != 443 AND strcasecmp($this->_url->protocol, 'https') == 0) {\r
- $host = $this->_url->host . ':' . $this->_url->port;\r
-\r
- } elseif ($this->_url->port == 443 AND strcasecmp($this->_url->protocol, 'https') == 0 AND strpos($this->_url->url, ':443') !== false) {\r
- $host = $this->_url->host . ':' . $this->_url->port;\r
-\r
- } else {\r
- $host = $this->_url->host;\r
- }\r
-\r
- return $host;\r
- }\r
-\r
- /**\r
- * Resets the object to its initial state (DEPRECATED).\r
- * Takes the same parameters as the constructor.\r
- *\r
- * @param string $url The url to be requested\r
- * @param array $params Associative array of parameters\r
- * (see constructor for details)\r
- * @access public\r
- * @deprecated deprecated since 1.2, call the constructor if this is necessary\r
- */\r
- function reset($url, $params = array())\r
- {\r
- $this->HTTP_Request($url, $params);\r
- }\r
-\r
- /**\r
- * Sets the URL to be requested\r
- *\r
- * @param string The url to be requested\r
- * @access public\r
- */\r
- function setURL($url)\r
- {\r
- $this->_url = &new Net_URL($url, $this->_useBrackets);\r
-\r
- if (!empty($this->_url->user) || !empty($this->_url->pass)) {\r
- $this->setBasicAuth($this->_url->user, $this->_url->pass);\r
- }\r
-\r
- if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http) {\r
- $this->addHeader('Host', $this->_generateHostHeader());\r
- }\r
-\r
- // set '/' instead of empty path rather than check later (see bug #8662)\r
- if (empty($this->_url->path)) {\r
- $this->_url->path = '/';\r
- }\r
- }\r
-\r
- /**\r
- * Returns the current request URL\r
- *\r
- * @return string Current request URL\r
- * @access public\r
- */\r
- function getUrl()\r
- {\r
- return empty($this->_url)? '': $this->_url->getUrl();\r
- }\r
-\r
- /**\r
- * Sets a proxy to be used\r
- *\r
- * @param string Proxy host\r
- * @param int Proxy port\r
- * @param string Proxy username\r
- * @param string Proxy password\r
- * @access public\r
- */\r
- function setProxy($host, $port = 8080, $user = null, $pass = null)\r
- {\r
- $this->_proxy_host = $host;\r
- $this->_proxy_port = $port;\r
- $this->_proxy_user = $user;\r
- $this->_proxy_pass = $pass;\r
-\r
- if (!empty($user)) {\r
- $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $pass));\r
- }\r
- }\r
-\r
- /**\r
- * Sets basic authentication parameters\r
- *\r
- * @param string Username\r
- * @param string Password\r
- */\r
- function setBasicAuth($user, $pass)\r
- {\r
- $this->_user = $user;\r
- $this->_pass = $pass;\r
-\r
- $this->addHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass));\r
- }\r
-\r
- /**\r
- * Sets the method to be used, GET, POST etc.\r
- *\r
- * @param string Method to use. Use the defined constants for this\r
- * @access public\r
- */\r
- function setMethod($method)\r
- {\r
- $this->_method = $method;\r
- }\r
-\r
- /**\r
- * Sets the HTTP version to use, 1.0 or 1.1\r
- *\r
- * @param string Version to use. Use the defined constants for this\r
- * @access public\r
- */\r
- function setHttpVer($http)\r
- {\r
- $this->_http = $http;\r
- }\r
-\r
- /**\r
- * Adds a request header\r
- *\r
- * @param string Header name\r
- * @param string Header value\r
- * @access public\r
- */\r
- function addHeader($name, $value)\r
- {\r
- $this->_requestHeaders[strtolower($name)] = $value;\r
- }\r
-\r
- /**\r
- * Removes a request header\r
- *\r
- * @param string Header name to remove\r
- * @access public\r
- */\r
- function removeHeader($name)\r
- {\r
- if (isset($this->_requestHeaders[strtolower($name)])) {\r
- unset($this->_requestHeaders[strtolower($name)]);\r
- }\r
- }\r
-\r
- /**\r
- * Adds a querystring parameter\r
- *\r
- * @param string Querystring parameter name\r
- * @param string Querystring parameter value\r
- * @param bool Whether the value is already urlencoded or not, default = not\r
- * @access public\r
- */\r
- function addQueryString($name, $value, $preencoded = false)\r
- {\r
- $this->_url->addQueryString($name, $value, $preencoded);\r
- }\r
-\r
- /**\r
- * Sets the querystring to literally what you supply\r
- *\r
- * @param string The querystring data. Should be of the format foo=bar&x=y etc\r
- * @param bool Whether data is already urlencoded or not, default = already encoded\r
- * @access public\r
- */\r
- function addRawQueryString($querystring, $preencoded = true)\r
- {\r
- $this->_url->addRawQueryString($querystring, $preencoded);\r
- }\r
-\r
- /**\r
- * Adds postdata items\r
- *\r
- * @param string Post data name\r
- * @param string Post data value\r
- * @param bool Whether data is already urlencoded or not, default = not\r
- * @access public\r
- */\r
- function addPostData($name, $value, $preencoded = false)\r
- {\r
- if ($preencoded) {\r
- $this->_postData[$name] = $value;\r
- } else {\r
- $this->_postData[$name] = $this->_arrayMapRecursive('urlencode', $value);\r
- }\r
- }\r
-\r
- /**\r
- * Recursively applies the callback function to the value\r
- *\r
- * @param mixed Callback function\r
- * @param mixed Value to process\r
- * @access private\r
- * @return mixed Processed value\r
- */\r
- function _arrayMapRecursive($callback, $value)\r
- {\r
- if (!is_array($value)) {\r
- return call_user_func($callback, $value);\r
- } else {\r
- $map = array();\r
- foreach ($value as $k => $v) {\r
- $map[$k] = $this->_arrayMapRecursive($callback, $v);\r
- }\r
- return $map;\r
- }\r
- }\r
-\r
- /**\r
- * Adds a file to form-based file upload\r
- *\r
- * Used to emulate file upload via a HTML form. The method also sets\r
- * Content-Type of HTTP request to 'multipart/form-data'.\r
- *\r
- * If you just want to send the contents of a file as the body of HTTP\r
- * request you should use setBody() method.\r
- *\r
- * @access public\r
- * @param string name of file-upload field\r
- * @param mixed file name(s)\r
- * @param mixed content-type(s) of file(s) being uploaded\r
- * @return bool true on success\r
- * @throws PEAR_Error\r
- */\r
- function addFile($inputName, $fileName, $contentType = 'application/octet-stream')\r
- {\r
- if (!is_array($fileName) && !is_readable($fileName)) {\r
- return PEAR::raiseError("File '{$fileName}' is not readable", HTTP_REQUEST_ERROR_FILE);\r
- } elseif (is_array($fileName)) {\r
- foreach ($fileName as $name) {\r
- if (!is_readable($name)) {\r
- return PEAR::raiseError("File '{$name}' is not readable", HTTP_REQUEST_ERROR_FILE);\r
- }\r
- }\r
- }\r
- $this->addHeader('Content-Type', 'multipart/form-data');\r
- $this->_postFiles[$inputName] = array(\r
- 'name' => $fileName,\r
- 'type' => $contentType\r
- );\r
- return true;\r
- }\r
-\r
- /**\r
- * Adds raw postdata (DEPRECATED)\r
- *\r
- * @param string The data\r
- * @param bool Whether data is preencoded or not, default = already encoded\r
- * @access public\r
- * @deprecated deprecated since 1.3.0, method setBody() should be used instead\r
- */\r
- function addRawPostData($postdata, $preencoded = true)\r
- {\r
- $this->_body = $preencoded ? $postdata : urlencode($postdata);\r
- }\r
-\r
- /**\r
- * Sets the request body (for POST, PUT and similar requests)\r
- *\r
- * @param string Request body\r
- * @access public\r
- */\r
- function setBody($body)\r
- {\r
- $this->_body = $body;\r
- }\r
-\r
- /**\r
- * Clears any postdata that has been added (DEPRECATED).\r
- *\r
- * Useful for multiple request scenarios.\r
- *\r
- * @access public\r
- * @deprecated deprecated since 1.2\r
- */\r
- function clearPostData()\r
- {\r
- $this->_postData = null;\r
- }\r
-\r
- /**\r
- * Appends a cookie to "Cookie:" header\r
- *\r
- * @param string $name cookie name\r
- * @param string $value cookie value\r
- * @access public\r
- */\r
- function addCookie($name, $value)\r
- {\r
- $cookies = isset($this->_requestHeaders['cookie']) ? $this->_requestHeaders['cookie']. '; ' : '';\r
- $this->addHeader('Cookie', $cookies . $name . '=' . $value);\r
- }\r
-\r
- /**\r
- * Clears any cookies that have been added (DEPRECATED).\r
- *\r
- * Useful for multiple request scenarios\r
- *\r
- * @access public\r
- * @deprecated deprecated since 1.2\r
- */\r
- function clearCookies()\r
- {\r
- $this->removeHeader('Cookie');\r
- }\r
-\r
- /**\r
- * Sends the request\r
- *\r
- * @access public\r
- * @param bool Whether to store response body in Response object property,\r
- * set this to false if downloading a LARGE file and using a Listener\r
- * @return mixed PEAR error on error, true otherwise\r
- */\r
- function sendRequest($saveBody = true)\r
- {\r
- if (!is_a($this->_url, 'Net_URL')) {\r
- return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL);\r
- }\r
-\r
- $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host;\r
- $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port;\r
-\r
- if (strcasecmp($this->_url->protocol, 'https') == 0) {\r
- // Bug #14127, don't try connecting to HTTPS sites without OpenSSL\r
- if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) {\r
- return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests',\r
- HTTP_REQUEST_ERROR_URL);\r
- } elseif (isset($this->_proxy_host)) {\r
- return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY);\r
- }\r
- $host = 'ssl://' . $host;\r
- }\r
-\r
- // magic quotes may fuck up file uploads and chunked response processing\r
- $magicQuotes = ini_get('magic_quotes_runtime');\r
- ini_set('magic_quotes_runtime', false);\r
-\r
- // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive\r
- // connection token to a proxy server...\r
- if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) &&\r
- 'Keep-Alive' == $this->_requestHeaders['connection'])\r
- {\r
- $this->removeHeader('connection');\r
- }\r
-\r
- $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) ||\r
- (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']);\r
- $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets');\r
- $sockKey = $host . ':' . $port;\r
- unset($this->_sock);\r
-\r
- // There is a connected socket in the "static" property?\r
- if ($keepAlive && !empty($sockets[$sockKey]) &&\r
- !empty($sockets[$sockKey]->fp))\r
- {\r
- $this->_sock =& $sockets[$sockKey];\r
- $err = null;\r
- } else {\r
- $this->_notify('connect');\r
- $this->_sock =& new Net_Socket();\r
- $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions);\r
- }\r
- PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest());\r
-\r
- if (!PEAR::isError($err)) {\r
- if (!empty($this->_readTimeout)) {\r
- $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]);\r
- }\r
-\r
- $this->_notify('sentRequest');\r
-\r
- // Read the response\r
- $this->_response = &new HTTP_Response($this->_sock, $this->_listeners);\r
- $err = $this->_response->process(\r
- $this->_saveBody && $saveBody,\r
- HTTP_REQUEST_METHOD_HEAD != $this->_method\r
- );\r
-\r
- if ($keepAlive) {\r
- $keepAlive = (isset($this->_response->_headers['content-length'])\r
- || (isset($this->_response->_headers['transfer-encoding'])\r
- && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked'));\r
- if ($keepAlive) {\r
- if (isset($this->_response->_headers['connection'])) {\r
- $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive';\r
- } else {\r
- $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol;\r
- }\r
- }\r
- }\r
- }\r
-\r
- ini_set('magic_quotes_runtime', $magicQuotes);\r
-\r
- if (PEAR::isError($err)) {\r
- return $err;\r
- }\r
-\r
- if (!$keepAlive) {\r
- $this->disconnect();\r
- // Store the connected socket in "static" property\r
- } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) {\r
- $sockets[$sockKey] =& $this->_sock;\r
- }\r
-\r
- // Check for redirection\r
- if ( $this->_allowRedirects\r
- AND $this->_redirects <= $this->_maxRedirects\r
- AND $this->getResponseCode() > 300\r
- AND $this->getResponseCode() < 399\r
- AND !empty($this->_response->_headers['location'])) {\r
-\r
-\r
- $redirect = $this->_response->_headers['location'];\r
-\r
- // Absolute URL\r
- if (preg_match('/^https?:\/\//i', $redirect)) {\r
- $this->_url = &new Net_URL($redirect);\r
- $this->addHeader('Host', $this->_generateHostHeader());\r
- // Absolute path\r
- } elseif ($redirect{0} == '/') {\r
- $this->_url->path = $redirect;\r
-\r
- // Relative path\r
- } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') {\r
- if (substr($this->_url->path, -1) == '/') {\r
- $redirect = $this->_url->path . $redirect;\r
- } else {\r
- $redirect = dirname($this->_url->path) . '/' . $redirect;\r
- }\r
- $redirect = Net_URL::resolvePath($redirect);\r
- $this->_url->path = $redirect;\r
-\r
- // Filename, no path\r
- } else {\r
- if (substr($this->_url->path, -1) == '/') {\r
- $redirect = $this->_url->path . $redirect;\r
- } else {\r
- $redirect = dirname($this->_url->path) . '/' . $redirect;\r
- }\r
- $this->_url->path = $redirect;\r
- }\r
-\r
- $this->_redirects++;\r
- return $this->sendRequest($saveBody);\r
-\r
- // Too many redirects\r
- } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) {\r
- return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS);\r
- }\r
-\r
- return true;\r
- }\r
-\r
- /**\r
- * Disconnect the socket, if connected. Only useful if using Keep-Alive.\r
- *\r
- * @access public\r
- */\r
- function disconnect()\r
- {\r
- if (!empty($this->_sock) && !empty($this->_sock->fp)) {\r
- $this->_notify('disconnect');\r
- $this->_sock->disconnect();\r
- }\r
- }\r
-\r
- /**\r
- * Returns the response code\r
- *\r
- * @access public\r
- * @return mixed Response code, false if not set\r
- */\r
- function getResponseCode()\r
- {\r
- return isset($this->_response->_code) ? $this->_response->_code : false;\r
- }\r
-\r
- /**\r
- * Returns the response reason phrase\r
- *\r
- * @access public\r
- * @return mixed Response reason phrase, false if not set\r
- */\r
- function getResponseReason()\r
- {\r
- return isset($this->_response->_reason) ? $this->_response->_reason : false;\r
- }\r
-\r
- /**\r
- * Returns either the named header or all if no name given\r
- *\r
- * @access public\r
- * @param string The header name to return, do not set to get all headers\r
- * @return mixed either the value of $headername (false if header is not present)\r
- * or an array of all headers\r
- */\r
- function getResponseHeader($headername = null)\r
- {\r
- if (!isset($headername)) {\r
- return isset($this->_response->_headers)? $this->_response->_headers: array();\r
- } else {\r
- $headername = strtolower($headername);\r
- return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false;\r
- }\r
- }\r
-\r
- /**\r
- * Returns the body of the response\r
- *\r
- * @access public\r
- * @return mixed response body, false if not set\r
- */\r
- function getResponseBody()\r
- {\r
- return isset($this->_response->_body) ? $this->_response->_body : false;\r
- }\r
-\r
- /**\r
- * Returns cookies set in response\r
- *\r
- * @access public\r
- * @return mixed array of response cookies, false if none are present\r
- */\r
- function getResponseCookies()\r
- {\r
- return isset($this->_response->_cookies) ? $this->_response->_cookies : false;\r
- }\r
-\r
- /**\r
- * Builds the request string\r
- *\r
- * @access private\r
- * @return string The request string\r
- */\r
- function _buildRequest()\r
- {\r
- $separator = ini_get('arg_separator.output');\r
- ini_set('arg_separator.output', '&');\r
- $querystring = ($querystring = $this->_url->getQueryString()) ? '?' . $querystring : '';\r
- ini_set('arg_separator.output', $separator);\r
-\r
- $host = isset($this->_proxy_host) ? $this->_url->protocol . '://' . $this->_url->host : '';\r
- $port = (isset($this->_proxy_host) AND $this->_url->port != 80) ? ':' . $this->_url->port : '';\r
- $path = $this->_url->path . $querystring;\r
- $url = $host . $port . $path;\r
-\r
- if (!strlen($url)) {\r
- $url = '/';\r
- }\r
-\r
- $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n";\r
-\r
- if (in_array($this->_method, $this->_bodyDisallowed) ||\r
- (0 == strlen($this->_body) && (HTTP_REQUEST_METHOD_POST != $this->_method ||\r
- (empty($this->_postData) && empty($this->_postFiles)))))\r
- {\r
- $this->removeHeader('Content-Type');\r
- } else {\r
- if (empty($this->_requestHeaders['content-type'])) {\r
- // Add default content-type\r
- $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');\r
- } elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) {\r
- $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime());\r
- $this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary);\r
- }\r
- }\r
-\r
- // Request Headers\r
- if (!empty($this->_requestHeaders)) {\r
- foreach ($this->_requestHeaders as $name => $value) {\r
- $canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));\r
- $request .= $canonicalName . ': ' . $value . "\r\n";\r
- }\r
- }\r
-\r
- // Method does not allow a body, simply add a final CRLF\r
- if (in_array($this->_method, $this->_bodyDisallowed)) {\r
-\r
- $request .= "\r\n";\r
-\r
- // Post data if it's an array\r
- } elseif (HTTP_REQUEST_METHOD_POST == $this->_method &&\r
- (!empty($this->_postData) || !empty($this->_postFiles))) {\r
-\r
- // "normal" POST request\r
- if (!isset($boundary)) {\r
- $postdata = implode('&', array_map(\r
- create_function('$a', 'return $a[0] . \'=\' . $a[1];'),\r
- $this->_flattenArray('', $this->_postData)\r
- ));\r
-\r
- // multipart request, probably with file uploads\r
- } else {\r
- $postdata = '';\r
- if (!empty($this->_postData)) {\r
- $flatData = $this->_flattenArray('', $this->_postData);\r
- foreach ($flatData as $item) {\r
- $postdata .= '--' . $boundary . "\r\n";\r
- $postdata .= 'Content-Disposition: form-data; name="' . $item[0] . '"';\r
- $postdata .= "\r\n\r\n" . urldecode($item[1]) . "\r\n";\r
- }\r
- }\r
- foreach ($this->_postFiles as $name => $value) {\r
- if (is_array($value['name'])) {\r
- $varname = $name . ($this->_useBrackets? '[]': '');\r
- } else {\r
- $varname = $name;\r
- $value['name'] = array($value['name']);\r
- }\r
- foreach ($value['name'] as $key => $filename) {\r
- $fp = fopen($filename, 'r');\r
- $basename = basename($filename);\r
- $type = is_array($value['type'])? @$value['type'][$key]: $value['type'];\r
-\r
- $postdata .= '--' . $boundary . "\r\n";\r
- $postdata .= 'Content-Disposition: form-data; name="' . $varname . '"; filename="' . $basename . '"';\r
- $postdata .= "\r\nContent-Type: " . $type;\r
- $postdata .= "\r\n\r\n" . fread($fp, filesize($filename)) . "\r\n";\r
- fclose($fp);\r
- }\r
- }\r
- $postdata .= '--' . $boundary . "--\r\n";\r
- }\r
- $request .= 'Content-Length: ' .\r
- (HTTP_REQUEST_MBSTRING? mb_strlen($postdata, 'iso-8859-1'): strlen($postdata)) .\r
- "\r\n\r\n";\r
- $request .= $postdata;\r
-\r
- // Explicitly set request body\r
- } elseif (0 < strlen($this->_body)) {\r
-\r
- $request .= 'Content-Length: ' .\r
- (HTTP_REQUEST_MBSTRING? mb_strlen($this->_body, 'iso-8859-1'): strlen($this->_body)) .\r
- "\r\n\r\n";\r
- $request .= $this->_body;\r
-\r
- // No body: send a Content-Length header nonetheless (request #12900),\r
- // but do that only for methods that require a body (bug #14740)\r
- } else {\r
-\r
- if (in_array($this->_method, $this->_bodyRequired)) {\r
- $request .= "Content-Length: 0\r\n";\r
- }\r
- $request .= "\r\n";\r
- }\r
-\r
- return $request;\r
- }\r
-\r
- /**\r
- * Helper function to change the (probably multidimensional) associative array\r
- * into the simple one.\r
- *\r
- * @param string name for item\r
- * @param mixed item's values\r
- * @return array array with the following items: array('item name', 'item value');\r
- * @access private\r
- */\r
- function _flattenArray($name, $values)\r
- {\r
- if (!is_array($values)) {\r
- return array(array($name, $values));\r
- } else {\r
- $ret = array();\r
- foreach ($values as $k => $v) {\r
- if (empty($name)) {\r
- $newName = $k;\r
- } elseif ($this->_useBrackets) {\r
- $newName = $name . '[' . $k . ']';\r
- } else {\r
- $newName = $name;\r
- }\r
- $ret = array_merge($ret, $this->_flattenArray($newName, $v));\r
- }\r
- return $ret;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Adds a Listener to the list of listeners that are notified of\r
- * the object's events\r
- *\r
- * Events sent by HTTP_Request object\r
- * - 'connect': on connection to server\r
- * - 'sentRequest': after the request was sent\r
- * - 'disconnect': on disconnection from server\r
- *\r
- * Events sent by HTTP_Response object\r
- * - 'gotHeaders': after receiving response headers (headers are passed in $data)\r
- * - 'tick': on receiving a part of response body (the part is passed in $data)\r
- * - 'gzTick': on receiving a gzip-encoded part of response body (ditto)\r
- * - 'gotBody': after receiving the response body (passes the decoded body in $data if it was gzipped)\r
- *\r
- * @param HTTP_Request_Listener listener to attach\r
- * @return boolean whether the listener was successfully attached\r
- * @access public\r
- */\r
- function attach(&$listener)\r
- {\r
- if (!is_a($listener, 'HTTP_Request_Listener')) {\r
- return false;\r
- }\r
- $this->_listeners[$listener->getId()] =& $listener;\r
- return true;\r
- }\r
-\r
-\r
- /**\r
- * Removes a Listener from the list of listeners\r
- *\r
- * @param HTTP_Request_Listener listener to detach\r
- * @return boolean whether the listener was successfully detached\r
- * @access public\r
- */\r
- function detach(&$listener)\r
- {\r
- if (!is_a($listener, 'HTTP_Request_Listener') ||\r
- !isset($this->_listeners[$listener->getId()])) {\r
- return false;\r
- }\r
- unset($this->_listeners[$listener->getId()]);\r
- return true;\r
- }\r
-\r
-\r
- /**\r
- * Notifies all registered listeners of an event.\r
- *\r
- * @param string Event name\r
- * @param mixed Additional data\r
- * @access private\r
- * @see HTTP_Request::attach()\r
- */\r
- function _notify($event, $data = null)\r
- {\r
- foreach (array_keys($this->_listeners) as $id) {\r
- $this->_listeners[$id]->update($this, $event, $data);\r
- }\r
- }\r
-}\r
-\r
-\r
-/**\r
- * Response class to complement the Request class\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request\r
- * @author Richard Heyes <richard@phpguru.org>\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 1.4.4\r
- */\r
-class HTTP_Response\r
-{\r
- /**\r
- * Socket object\r
- * @var Net_Socket\r
- */\r
- var $_sock;\r
-\r
- /**\r
- * Protocol\r
- * @var string\r
- */\r
- var $_protocol;\r
-\r
- /**\r
- * Return code\r
- * @var string\r
- */\r
- var $_code;\r
-\r
- /**\r
- * Response reason phrase\r
- * @var string\r
- */\r
- var $_reason;\r
-\r
- /**\r
- * Response headers\r
- * @var array\r
- */\r
- var $_headers;\r
-\r
- /**\r
- * Cookies set in response\r
- * @var array\r
- */\r
- var $_cookies;\r
-\r
- /**\r
- * Response body\r
- * @var string\r
- */\r
- var $_body = '';\r
-\r
- /**\r
- * Used by _readChunked(): remaining length of the current chunk\r
- * @var string\r
- */\r
- var $_chunkLength = 0;\r
-\r
- /**\r
- * Attached listeners\r
- * @var array\r
- */\r
- var $_listeners = array();\r
-\r
- /**\r
- * Bytes left to read from message-body\r
- * @var null|int\r
- */\r
- var $_toRead;\r
-\r
- /**\r
- * Constructor\r
- *\r
- * @param Net_Socket socket to read the response from\r
- * @param array listeners attached to request\r
- */\r
- function HTTP_Response(&$sock, &$listeners)\r
- {\r
- $this->_sock =& $sock;\r
- $this->_listeners =& $listeners;\r
- }\r
-\r
-\r
- /**\r
- * Processes a HTTP response\r
- *\r
- * This extracts response code, headers, cookies and decodes body if it\r
- * was encoded in some way\r
- *\r
- * @access public\r
- * @param bool Whether to store response body in object property, set\r
- * this to false if downloading a LARGE file and using a Listener.\r
- * This is assumed to be true if body is gzip-encoded.\r
- * @param bool Whether the response can actually have a message-body.\r
- * Will be set to false for HEAD requests.\r
- * @throws PEAR_Error\r
- * @return mixed true on success, PEAR_Error in case of malformed response\r
- */\r
- function process($saveBody = true, $canHaveBody = true)\r
- {\r
- do {\r
- $line = $this->_sock->readLine();\r
- if (!preg_match('!^(HTTP/\d\.\d) (\d{3})(?: (.+))?!', $line, $s)) {\r
- return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE);\r
- } else {\r
- $this->_protocol = $s[1];\r
- $this->_code = intval($s[2]);\r
- $this->_reason = empty($s[3])? null: $s[3];\r
- }\r
- while ('' !== ($header = $this->_sock->readLine())) {\r
- $this->_processHeader($header);\r
- }\r
- } while (100 == $this->_code);\r
-\r
- $this->_notify('gotHeaders', $this->_headers);\r
-\r
- // RFC 2616, section 4.4:\r
- // 1. Any response message which "MUST NOT" include a message-body ...\r
- // is always terminated by the first empty line after the header fields\r
- // 3. ... If a message is received with both a\r
- // Transfer-Encoding header field and a Content-Length header field,\r
- // the latter MUST be ignored.\r
- $canHaveBody = $canHaveBody && $this->_code >= 200 &&\r
- $this->_code != 204 && $this->_code != 304;\r
-\r
- // If response body is present, read it and decode\r
- $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']);\r
- $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']);\r
- $hasBody = false;\r
- if ($canHaveBody && ($chunked || !isset($this->_headers['content-length']) ||\r
- 0 != $this->_headers['content-length']))\r
- {\r
- if ($chunked || !isset($this->_headers['content-length'])) {\r
- $this->_toRead = null;\r
- } else {\r
- $this->_toRead = $this->_headers['content-length'];\r
- }\r
- while (!$this->_sock->eof() && (is_null($this->_toRead) || 0 < $this->_toRead)) {\r
- if ($chunked) {\r
- $data = $this->_readChunked();\r
- } elseif (is_null($this->_toRead)) {\r
- $data = $this->_sock->read(4096);\r
- } else {\r
- $data = $this->_sock->read(min(4096, $this->_toRead));\r
- $this->_toRead -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data);\r
- }\r
- if ('' == $data && (!$this->_chunkLength || $this->_sock->eof())) {\r
- break;\r
- } else {\r
- $hasBody = true;\r
- if ($saveBody || $gzipped) {\r
- $this->_body .= $data;\r
- }\r
- $this->_notify($gzipped? 'gzTick': 'tick', $data);\r
- }\r
- }\r
- }\r
-\r
- if ($hasBody) {\r
- // Uncompress the body if needed\r
- if ($gzipped) {\r
- $body = $this->_decodeGzip($this->_body);\r
- if (PEAR::isError($body)) {\r
- return $body;\r
- }\r
- $this->_body = $body;\r
- $this->_notify('gotBody', $this->_body);\r
- } else {\r
- $this->_notify('gotBody');\r
- }\r
- }\r
- return true;\r
- }\r
-\r
-\r
- /**\r
- * Processes the response header\r
- *\r
- * @access private\r
- * @param string HTTP header\r
- */\r
- function _processHeader($header)\r
- {\r
- if (false === strpos($header, ':')) {\r
- return;\r
- }\r
- list($headername, $headervalue) = explode(':', $header, 2);\r
- $headername = strtolower($headername);\r
- $headervalue = ltrim($headervalue);\r
-\r
- if ('set-cookie' != $headername) {\r
- if (isset($this->_headers[$headername])) {\r
- $this->_headers[$headername] .= ',' . $headervalue;\r
- } else {\r
- $this->_headers[$headername] = $headervalue;\r
- }\r
- } else {\r
- $this->_parseCookie($headervalue);\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Parse a Set-Cookie header to fill $_cookies array\r
- *\r
- * @access private\r
- * @param string value of Set-Cookie header\r
- */\r
- function _parseCookie($headervalue)\r
- {\r
- $cookie = array(\r
- 'expires' => null,\r
- 'domain' => null,\r
- 'path' => null,\r
- 'secure' => false\r
- );\r
-\r
- // Only a name=value pair\r
- if (!strpos($headervalue, ';')) {\r
- $pos = strpos($headervalue, '=');\r
- $cookie['name'] = trim(substr($headervalue, 0, $pos));\r
- $cookie['value'] = trim(substr($headervalue, $pos + 1));\r
-\r
- // Some optional parameters are supplied\r
- } else {\r
- $elements = explode(';', $headervalue);\r
- $pos = strpos($elements[0], '=');\r
- $cookie['name'] = trim(substr($elements[0], 0, $pos));\r
- $cookie['value'] = trim(substr($elements[0], $pos + 1));\r
-\r
- for ($i = 1; $i < count($elements); $i++) {\r
- if (false === strpos($elements[$i], '=')) {\r
- $elName = trim($elements[$i]);\r
- $elValue = null;\r
- } else {\r
- list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i]));\r
- }\r
- $elName = strtolower($elName);\r
- if ('secure' == $elName) {\r
- $cookie['secure'] = true;\r
- } elseif ('expires' == $elName) {\r
- $cookie['expires'] = str_replace('"', '', $elValue);\r
- } elseif ('path' == $elName || 'domain' == $elName) {\r
- $cookie[$elName] = urldecode($elValue);\r
- } else {\r
- $cookie[$elName] = $elValue;\r
- }\r
- }\r
- }\r
- $this->_cookies[] = $cookie;\r
- }\r
-\r
-\r
- /**\r
- * Read a part of response body encoded with chunked Transfer-Encoding\r
- *\r
- * @access private\r
- * @return string\r
- */\r
- function _readChunked()\r
- {\r
- // at start of the next chunk?\r
- if (0 == $this->_chunkLength) {\r
- $line = $this->_sock->readLine();\r
- if (preg_match('/^([0-9a-f]+)/i', $line, $matches)) {\r
- $this->_chunkLength = hexdec($matches[1]);\r
- // Chunk with zero length indicates the end\r
- if (0 == $this->_chunkLength) {\r
- $this->_sock->readLine(); // make this an eof()\r
- return '';\r
- }\r
- } else {\r
- return '';\r
- }\r
- }\r
- $data = $this->_sock->read($this->_chunkLength);\r
- $this->_chunkLength -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data);\r
- if (0 == $this->_chunkLength) {\r
- $this->_sock->readLine(); // Trailing CRLF\r
- }\r
- return $data;\r
- }\r
-\r
-\r
- /**\r
- * Notifies all registered listeners of an event.\r
- *\r
- * @param string Event name\r
- * @param mixed Additional data\r
- * @access private\r
- * @see HTTP_Request::_notify()\r
- */\r
- function _notify($event, $data = null)\r
- {\r
- foreach (array_keys($this->_listeners) as $id) {\r
- $this->_listeners[$id]->update($this, $event, $data);\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Decodes the message-body encoded by gzip\r
- *\r
- * The real decoding work is done by gzinflate() built-in function, this\r
- * method only parses the header and checks data for compliance with\r
- * RFC 1952\r
- *\r
- * @access private\r
- * @param string gzip-encoded data\r
- * @return string decoded data\r
- */\r
- function _decodeGzip($data)\r
- {\r
- if (HTTP_REQUEST_MBSTRING) {\r
- $oldEncoding = mb_internal_encoding();\r
- mb_internal_encoding('iso-8859-1');\r
- }\r
- $length = strlen($data);\r
- // If it doesn't look like gzip-encoded data, don't bother\r
- if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) {\r
- return $data;\r
- }\r
- $method = ord(substr($data, 2, 1));\r
- if (8 != $method) {\r
- return PEAR::raiseError('_decodeGzip(): unknown compression method', HTTP_REQUEST_ERROR_GZIP_METHOD);\r
- }\r
- $flags = ord(substr($data, 3, 1));\r
- if ($flags & 224) {\r
- return PEAR::raiseError('_decodeGzip(): reserved bits are set', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
-\r
- // header is 10 bytes minimum. may be longer, though.\r
- $headerLength = 10;\r
- // extra fields, need to skip 'em\r
- if ($flags & 4) {\r
- if ($length - $headerLength - 2 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $extraLength = unpack('v', substr($data, 10, 2));\r
- if ($length - $headerLength - 2 - $extraLength[1] < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $headerLength += $extraLength[1] + 2;\r
- }\r
- // file name, need to skip that\r
- if ($flags & 8) {\r
- if ($length - $headerLength - 1 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $filenameLength = strpos(substr($data, $headerLength), chr(0));\r
- if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $headerLength += $filenameLength + 1;\r
- }\r
- // comment, need to skip that also\r
- if ($flags & 16) {\r
- if ($length - $headerLength - 1 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $commentLength = strpos(substr($data, $headerLength), chr(0));\r
- if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $headerLength += $commentLength + 1;\r
- }\r
- // have a CRC for header. let's check\r
- if ($flags & 1) {\r
- if ($length - $headerLength - 2 < 8) {\r
- return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA);\r
- }\r
- $crcReal = 0xffff & crc32(substr($data, 0, $headerLength));\r
- $crcStored = unpack('v', substr($data, $headerLength, 2));\r
- if ($crcReal != $crcStored[1]) {\r
- return PEAR::raiseError('_decodeGzip(): header CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC);\r
- }\r
- $headerLength += 2;\r
- }\r
- // unpacked data CRC and size at the end of encoded data\r
- $tmp = unpack('V2', substr($data, -8));\r
- $dataCrc = $tmp[1];\r
- $dataSize = $tmp[2];\r
-\r
- // finally, call the gzinflate() function\r
- // don't pass $dataSize to gzinflate, see bugs #13135, #14370\r
- $unpacked = gzinflate(substr($data, $headerLength, -8));\r
- if (false === $unpacked) {\r
- return PEAR::raiseError('_decodeGzip(): gzinflate() call failed', HTTP_REQUEST_ERROR_GZIP_READ);\r
- } elseif ($dataSize != strlen($unpacked)) {\r
- return PEAR::raiseError('_decodeGzip(): data size check failed', HTTP_REQUEST_ERROR_GZIP_READ);\r
- } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) {\r
- return PEAR::raiseError('_decodeGzip(): data CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC);\r
- }\r
- if (HTTP_REQUEST_MBSTRING) {\r
- mb_internal_encoding($oldEncoding);\r
- }\r
- return $unpacked;\r
- }\r
-} // End class HTTP_Response\r
-?>\r