verzia 1.1, 2005/06/05 12:13:52 |
verzia 1.2, 2005/11/07 20:37:10 |
|
|
<?php
|
<?php |
// +----------------------------------------------------------------------+
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
// | PEAR :: HTTP |
|
|
// +----------------------------------------------------------------------+
|
/** |
// | This source file is subject to version 3.0 of the PHP license, |
|
* HTTP |
// | that is available at http://www.php.net/license/3_0.txt |
|
* |
// | If you did not receive a copy of the PHP license and are unable |
|
* PHP versions 4 and 5 |
// | to obtain it through the world-wide-web, please send a note to |
|
* |
// | license@php.net so we can mail you a copy immediately. |
|
* LICENSE: This source file is subject to version 3.0 of the PHP license |
// +----------------------------------------------------------------------+
|
* that is available through the world-wide-web at the following URI: |
// | Authors: Stig Bakken <ssb@fast.no> |
|
* http://www.php.net/license/3_0.txt. If you did not receive a copy of |
// | Sterling Hughes <sterling@php.net> |
|
* the PHP License and are unable to obtain it through the web, please |
// | Tomas V.V.Cox <cox@idecnet.com> |
|
* send a note to license@php.net so we can mail you a copy immediately. |
// | Richard Heyes <richard@php.net> |
|
* |
// | Philippe Jausions <Philippe.Jausions@11abacus.com> |
|
* @category HTTP |
// | Michael Wallner <mike@php.net> |
|
* @package HTTP |
// +----------------------------------------------------------------------+
|
* @author Stig Bakken <ssb@fast.no> |
//
|
* @author Sterling Hughes <sterling@php.net> |
// $Id$
|
* @author Tomas V.V.Cox <cox@idecnet.com> |
|
* @author Richard Heyes <richard@php.net> |
/**
|
* @author Philippe Jausions <Philippe.Jausions@11abacus.com> |
* HTTP
|
* @author Michael Wallner <mike@php.net> |
*
|
* @copyright 2002-2005 The Authors |
* HTTP utility functions
|
* @license http://www.php.net/license/3_0.txt PHP License 3.0 |
*
|
* @version CVS: $Id$ |
* @package HTTP
|
* @link http://pear.php.net/package/HTTP |
* @category HTTP
|
*/ |
* @license PHP License
|
|
* @access public
|
/** |
* @version $Revision$
|
* Miscellaneous HTTP Utilities |
*/
|
* |
class HTTP
|
* PEAR::HTTP provides static shorthand methods for generating HTTP dates, |
{
|
* issueing HTTP HEAD requests, building absolute URIs, firing redirects and |
/**
|
* negotiating user preferred language. |
* Date
|
* |
*
|
* @package HTTP |
* Format a RFC compliant GMT date HTTP header. This function honors the
|
* @category HTTP |
* "y2k_compliance" php.ini directive and formats the GMT date corresponding
|
* @license PHP License |
* to either RFC850 or RFC822.
|
* @access public |
*
|
* @static |
* @static
|
* @version $Revision$ |
* @access public
|
*/ |
* @return mixed GMT date string, or false for an invalid $time parameter
|
class HTTP |
* @param mixed $time unix timestamp or date (default = current time)
|
{ |
*/
|
/** |
function Date($time = null)
|
* Date |
{
|
* |
if (!isset($time)) {
|
* Format a RFC compliant GMT date HTTP header. This function honors the |
$time = time();
|
* "y2k_compliance" php.ini directive and formats the GMT date corresponding |
} elseif (!is_numeric($time) && (-1 === $time = strtotime($time))) {
|
* to either RFC850 or RFC822. |
return false;
|
* |
}
|
* @static |
|
* @access public |
// RFC822 or RFC850
|
* @return mixed GMT date string, or false for an invalid $time parameter |
$format = ini_get('y2k_compliance') ? 'D, d M Y' : 'l, d-M-y';
|
* @param mixed $time unix timestamp or date (default = current time) |
|
*/ |
return gmdate($format .' H:i:s \G\M\T', $time);
|
function Date($time = null) |
}
|
{ |
|
if (!isset($time)) { |
/**
|
$time = time(); |
* Negotiate Language
|
} elseif (!is_numeric($time) && (-1 === $time = strtotime($time))) { |
*
|
return false; |
* Negotiate language with the user's browser through the Accept-Language
|
} |
* HTTP header or the user's host address. Language codes are generally in
|
|
* the form "ll" for a language spoken in only one country, or "ll-CC" for a
|
// RFC822 or RFC850 |
* language spoken in a particular country. For example, U.S. English is
|
$format = ini_get('y2k_compliance') ? 'D, d M Y' : 'l, d-M-y'; |
* "en-US", while British English is "en-UK". Portugese as spoken in
|
|
* Portugal is "pt-PT", while Brazilian Portugese is "pt-BR".
|
return gmdate($format .' H:i:s \G\M\T', $time); |
*
|
} |
* Quality factors in the Accept-Language: header are supported, e.g.:
|
|
* Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8
|
/** |
*
|
* Negotiate Language |
* <code>
|
* |
* require_once 'HTTP.php';
|
* Negotiate language with the user's browser through the Accept-Language |
* $langs = array(
|
* HTTP header or the user's host address. Language codes are generally in |
* 'en' => 'locales/en',
|
* the form "ll" for a language spoken in only one country, or "ll-CC" for a |
* 'en-US'=> 'locales/en',
|
* language spoken in a particular country. For example, U.S. English is |
* 'en-UK'=> 'locales/en',
|
* "en-US", while British English is "en-UK". Portugese as spoken in |
* 'de' => 'locales/de',
|
* Portugal is "pt-PT", while Brazilian Portugese is "pt-BR". |
* 'de-DE'=> 'locales/de',
|
* |
* 'de-AT'=> 'locales/de',
|
* Quality factors in the Accept-Language: header are supported, e.g.: |
* );
|
* Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8 |
* $neg = HTTP::negotiateLanguage($langs);
|
* |
* $dir = $langs[$neg];
|
* <code> |
* </code>
|
* require_once 'HTTP.php'; |
*
|
* $langs = array( |
* @static
|
* 'en' => 'locales/en', |
* @access public
|
* 'en-US'=> 'locales/en', |
* @return string The negotiated language result or the supplied default.
|
* 'en-UK'=> 'locales/en', |
* @param array $supported An associative array of supported languages,
|
* 'de' => 'locales/de', |
* whose values must evaluate to true.
|
* 'de-DE'=> 'locales/de', |
* @param string $default The default language to use if none is found.
|
* 'de-AT'=> 'locales/de', |
*/
|
* ); |
function negotiateLanguage($supported, $default = 'en-US')
|
* $neg = HTTP::negotiateLanguage($langs); |
{
|
* $dir = $langs[$neg]; |
$supp = array();
|
* </code> |
foreach ($supported as $lang => $isSupported) {
|
* |
if ($isSupported) {
|
* @static |
$supp[strToLower($lang)] = $lang;
|
* @access public |
}
|
* @return string The negotiated language result or the supplied default. |
}
|
* @param array $supported An associative array of supported languages, |
|
* whose values must evaluate to true. |
if (!count($supp)) {
|
* @param string $default The default language to use if none is found. |
return $default;
|
*/ |
}
|
function negotiateLanguage($supported, $default = 'en-US') |
|
{ |
$matches = array();
|
$supp = array(); |
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
foreach ($supported as $lang => $isSupported) { |
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
|
if ($isSupported) { |
$lang = array_map('trim', explode(';', $lang));
|
$supp[strToLower($lang)] = $lang; |
if (isset($lang[1])) {
|
} |
$l = strtolower($lang[0]);
|
} |
$q = (float) str_replace('q=', '', $lang[1]);
|
|
} else {
|
if (!count($supp)) { |
$l = strtolower($lang[0]);
|
return $default; |
$q = null;
|
} |
}
|
|
if (isset($supp[$l])) {
|
$matches = array(); |
$matches[$l] = isset($q) ? $q : 1000 - count($matches);
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
}
|
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { |
}
|
$lang = array_map('trim', explode(';', $lang)); |
}
|
if (isset($lang[1])) { |
|
$l = strtolower($lang[0]); |
if (count($matches)) {
|
$q = (float) str_replace('q=', '', $lang[1]); |
asort($matches, SORT_NUMERIC);
|
} else { |
return $supp[array_pop(array_keys($matches))];
|
$l = strtolower($lang[0]); |
}
|
$q = null; |
|
} |
if (isset($_SERVER['REMOTE_HOST'])) {
|
if (isset($supp[$l])) { |
$lang = strtolower(array_pop(explode('.', $_SERVER['REMOTE_HOST'])));
|
$matches[$l] = isset($q) ? $q : 1000 - count($matches); |
if (isset($supp[$lang])) {
|
} |
return $supp[$lang];
|
} |
}
|
} |
}
|
|
|
if (count($matches)) { |
return $default;
|
asort($matches, SORT_NUMERIC); |
}
|
return $supp[array_pop(array_keys($matches))]; |
|
} |
/**
|
|
* Head
|
if (isset($_SERVER['REMOTE_HOST'])) { |
*
|
$lang = strtolower(array_pop(explode('.', $_SERVER['REMOTE_HOST']))); |
* Sends a "HEAD" HTTP command to a server and returns the headers
|
if (isset($supp[$lang])) { |
* as an associative array. Example output could be:
|
return $supp[$lang]; |
* <code>
|
} |
* Array
|
} |
* (
|
|
* [response_code] => 200 // The HTTP response code
|
return $default; |
* [response] => HTTP/1.1 200 OK // The full HTTP response string
|
} |
* [Date] => Fri, 11 Jan 2002 01:41:44 GMT
|
|
* [Server] => Apache/1.3.20 (Unix) PHP/4.1.1
|
/** |
* [X-Powered-By] => PHP/4.1.1
|
* Head |
* [Connection] => close
|
* |
* [Content-Type] => text/html
|
* Sends a "HEAD" HTTP command to a server and returns the headers |
* )
|
* as an associative array. Example output could be: |
* </code>
|
* <code> |
*
|
* Array |
* @see HTTP_Client::head()
|
* ( |
* @see HTTP_Request
|
* [response_code] => 200 // The HTTP response code |
*
|
* [response] => HTTP/1.1 200 OK // The full HTTP response string |
* @static
|
* [Date] => Fri, 11 Jan 2002 01:41:44 GMT |
* @access public
|
* [Server] => Apache/1.3.20 (Unix) PHP/4.1.1 |
* @return mixed Returns associative array of response headers on success
|
* [X-Powered-By] => PHP/4.1.1 |
* or PEAR error on failure.
|
* [Connection] => close |
* @param string $url A valid URL, e.g.: http://pear.php.net/credits.php
|
* [Content-Type] => text/html |
* @param integer $timeout Timeout in seconds (default = 10)
|
* ) |
*/
|
* </code> |
function head($url, $timeout = 10)
|
* |
{
|
* @see HTTP_Client::head() |
$p = parse_url($url);
|
* @see HTTP_Request |
if (!isset($p['scheme'])) {
|
* |
$p = parse_url(HTTP::absoluteURI($url));
|
* @static |
} elseif ($p['scheme'] != 'http') {
|
* @access public |
return HTTP::raiseError('Unsupported protocol: '. $p['scheme']);
|
* @return mixed Returns associative array of response headers on success |
}
|
* or PEAR error on failure. |
|
* @param string $url A valid URL, e.g.: http://pear.php.net/credits.php |
$port = isset($p['port']) ? $p['port'] : 80;
|
* @param integer $timeout Timeout in seconds (default = 10) |
|
*/ |
if (!$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout)) {
|
function head($url, $timeout = 10) |
return HTTP::raiseError("Connection error: $estr ($eno)");
|
{ |
}
|
$p = parse_url($url); |
|
if (!isset($p['scheme'])) { |
$path = !empty($p['path']) ? $p['path'] : '/';
|
$p = parse_url(HTTP::absoluteURI($url)); |
$path .= !empty($p['query']) ? '?' . $p['query'] : '';
|
} elseif ($p['scheme'] != 'http') { |
|
return HTTP::raiseError('Unsupported protocol: '. $p['scheme']); |
fputs($fp, "HEAD $path HTTP/1.0\r\n");
|
} |
fputs($fp, 'Host: ' . $p['host'] . ':' . $port . "\r\n");
|
|
fputs($fp, "Connection: close\r\n\r\n");
|
$port = isset($p['port']) ? $p['port'] : 80; |
|
|
$response = rtrim(fgets($fp, 4096));
|
if (!$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout)) { |
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
|
return HTTP::raiseError("Connection error: $estr ($eno)"); |
$headers['response_code'] = $status[1];
|
} |
}
|
|
$headers['response'] = $response;
|
$path = !empty($p['path']) ? $p['path'] : '/'; |
|
$path .= !empty($p['query']) ? '?' . $p['query'] : ''; |
while ($line = fgets($fp, 4096)) {
|
|
if (!trim($line)) {
|
fputs($fp, "HEAD $path HTTP/1.0\r\n"); |
break;
|
fputs($fp, 'Host: ' . $p['host'] . ':' . $port . "\r\n"); |
}
|
fputs($fp, "Connection: close\r\n\r\n"); |
if (($pos = strpos($line, ':')) !== false) {
|
|
$header = substr($line, 0, $pos);
|
$response = rtrim(fgets($fp, 4096)); |
$value = trim(substr($line, $pos + 1));
|
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) { |
$headers[$header] = $value;
|
$headers['response_code'] = $status[1]; |
}
|
} |
}
|
$headers['response'] = $response; |
fclose($fp);
|
|
return $headers;
|
while ($line = fgets($fp, 4096)) { |
}
|
if (!trim($line)) { |
|
break; |
/**
|
} |
* Redirect
|
if (($pos = strpos($line, ':')) !== false) { |
*
|
$header = substr($line, 0, $pos); |
* This function redirects the client. This is done by issuing
|
$value = trim(substr($line, $pos + 1)); |
* a "Location" header and exiting if wanted. If you set $rfc2616 to true
|
$headers[$header] = $value; |
* HTTP will output a hypertext note with the location of the redirect.
|
} |
*
|
} |
* @static
|
fclose($fp); |
* @access public
|
return $headers; |
* @return mixed Returns true on succes (or exits) or false if headers
|
} |
* have already been sent.
|
|
* @param string $url URL where the redirect should go to.
|
/** |
* @param bool $exit Whether to exit immediately after redirection.
|
* Redirect |
* @param bool $rfc2616 Wheter to output a hypertext note where we're
|
* |
* redirecting to (Redirecting to <a href="...">...</a>.)
|
* This function redirects the client. This is done by issuing |
*/
|
* a "Location" header and exiting if wanted. If you set $rfc2616 to true |
function redirect($url, $exit = true, $rfc2616 = false)
|
* HTTP will output a hypertext note with the location of the redirect. |
{
|
* |
if (headers_sent()) {
|
* @static |
return false;
|
* @access public |
}
|
* @return mixed Returns true on succes (or exits) or false if headers |
|
* have already been sent. |
$url = HTTP::absoluteURI($url);
|
* @param string $url URL where the redirect should go to. |
header('Location: '. $url);
|
* @param bool $exit Whether to exit immediately after redirection. |
|
* @param bool $rfc2616 Wheter to output a hypertext note where we're |
if ( $rfc2616 && isset($_SERVER['REQUEST_METHOD']) &&
|
* redirecting to (Redirecting to <a href="...">...</a>.) |
$_SERVER['REQUEST_METHOD'] != 'HEAD') {
|
*/ |
printf('Redirecting to: <a href="%s">%s</a>.', $url, $url);
|
function redirect($url, $exit = true, $rfc2616 = false) |
}
|
{ |
if ($exit) {
|
if (headers_sent()) { |
exit;
|
return false; |
}
|
} |
return true;
|
|
}
|
$url = HTTP::absoluteURI($url); |
|
header('Location: '. $url); |
/**
|
|
* Absolute URI
|
if ( $rfc2616 && isset($_SERVER['REQUEST_METHOD']) && |
*
|
$_SERVER['REQUEST_METHOD'] != 'HEAD') { |
* This function returns the absolute URI for the partial URL passed.
|
printf('Redirecting to: <a href="%s">%s</a>.', $url, $url); |
* The current scheme (HTTP/HTTPS), host server, port, current script
|
} |
* location are used if necessary to resolve any relative URLs.
|
if ($exit) { |
*
|
exit; |
* Offsets potentially created by PATH_INFO are taken care of to resolve
|
} |
* relative URLs to the current script.
|
return true; |
*
|
} |
* You can choose a new protocol while resolving the URI. This is
|
|
* particularly useful when redirecting a web browser using relative URIs
|
/** |
* and to switch from HTTP to HTTPS, or vice-versa, at the same time.
|
* Absolute URI |
*
|
* |
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
|
* This function returns the absolute URI for the partial URL passed. |
* @static
|
* The current scheme (HTTP/HTTPS), host server, port, current script |
* @access public
|
* location are used if necessary to resolve any relative URLs. |
* @return string The absolute URI.
|
* |
* @param string $url Absolute or relative URI the redirect should go to.
|
* Offsets potentially created by PATH_INFO are taken care of to resolve |
* @param string $protocol Protocol to use when redirecting URIs.
|
* relative URLs to the current script. |
* @param integer $port A new port number.
|
* |
*/
|
* You can choose a new protocol while resolving the URI. This is |
function absoluteURI($url = null, $protocol = null, $port = null)
|
* particularly useful when redirecting a web browser using relative URIs |
{
|
* and to switch from HTTP to HTTPS, or vice-versa, at the same time. |
// filter CR/LF
|
* |
$url = str_replace(array("\r", "\n"), ' ', $url);
|
* @author Philippe Jausions <Philippe.Jausions@11abacus.com> |
|
* @static |
// Mess around with already absolute URIs
|
* @access public |
if (preg_match('!^([a-z0-9]+)://!i', $url)) {
|
* @return string The absolute URI. |
if (empty($protocol) && empty($port)) {
|
* @param string $url Absolute or relative URI the redirect should go to. |
return $url;
|
* @param string $protocol Protocol to use when redirecting URIs. |
}
|
* @param integer $port A new port number. |
if (!empty($protocol)) {
|
*/ |
$url = $protocol .':'. array_pop(explode(':', $url, 2));
|
function absoluteURI($url = null, $protocol = null, $port = null) |
}
|
{ |
if (!empty($port)) {
|
// filter CR/LF |
$url = preg_replace('!^(([a-z0-9]+)://[^/:]+)(:[\d]+)?!i',
|
$url = str_replace(array("\r", "\n"), ' ', $url); |
'\1:'. $port, $url);
|
|
}
|
// Mess around with already absolute URIs |
return $url;
|
if (preg_match('!^([a-z0-9]+)://!i', $url)) { |
}
|
if (empty($protocol) && empty($port)) { |
|
return $url; |
$host = 'localhost';
|
} |
if (!empty($_SERVER['HTTP_HOST'])) {
|
if (!empty($protocol)) { |
list($host) = explode(':', $_SERVER['HTTP_HOST']);
|
$url = $protocol .':'. array_pop(explode(':', $url, 2)); |
} elseif (!empty($_SERVER['SERVER_NAME'])) {
|
} |
list($host) = explode(':', $_SERVER['SERVER_NAME']);
|
if (!empty($port)) { |
}
|
$url = preg_replace('!^(([a-z0-9]+)://[^/:]+)(:[\d]+)?!i', |
|
'\1:'. $port, $url); |
if (empty($protocol)) {
|
} |
if (isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'], 'on')) {
|
return $url; |
$protocol = 'https';
|
} |
} else {
|
|
$protocol = 'http';
|
$host = 'localhost'; |
}
|
if (!empty($_SERVER['HTTP_HOST'])) { |
if (!isset($port) || $port != intval($port)) {
|
list($host) = explode(':', $_SERVER['HTTP_HOST']); |
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
|
} elseif (!empty($_SERVER['SERVER_NAME'])) { |
}
|
list($host) = explode(':', $_SERVER['SERVER_NAME']); |
}
|
} |
|
|
if ($protocol == 'http' && $port == 80) {
|
if (empty($protocol)) { |
unset($port);
|
if (isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'], 'on')) { |
}
|
$protocol = 'https'; |
if ($protocol == 'https' && $port == 443) {
|
} else { |
unset($port);
|
$protocol = 'http'; |
}
|
} |
|
if (!isset($port) || $port != intval($port)) { |
$server = $protocol .'://'. $host . (isset($port) ? ':'. $port : '');
|
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80; |
|
} |
if (!strlen($url)) {
|
} |
$url = isset($_SERVER['REQUEST_URI']) ?
|
|
$_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
|
if ($protocol == 'http' && $port == 80) { |
}
|
unset($port); |
|
} |
if ($url{0} == '/') {
|
if ($protocol == 'https' && $port == 443) { |
return $server . $url;
|
unset($port); |
}
|
} |
|
|
// Check for PATH_INFO
|
$server = $protocol .'://'. $host . (isset($port) ? ':'. $port : ''); |
if (isset($_SERVER['PATH_INFO']) && $_SERVER['PHP_SELF'] != $_SERVER['PATH_INFO']) {
|
|
$path = dirname(substr($_SERVER['PHP_SELF'], 0, -strlen($_SERVER['PATH_INFO'])));
|
if (!strlen($url)) { |
} else {
|
$url = isset($_SERVER['REQUEST_URI']) ? |
$path = dirname($_SERVER['PHP_SELF']);
|
$_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']; |
}
|
} |
|
|
if (substr($path = strtr($path, '\\', '/'), -1) != '/') {
|
if ($url{0} == '/') { |
$path .= '/';
|
return $server . $url; |
}
|
} |
|
|
return $server . $path . $url;
|
// Check for PATH_INFO |
}
|
if (isset($_SERVER['PATH_INFO']) && strlen($_SERVER['PATH_INFO']) && |
|
$_SERVER['PHP_SELF'] != $_SERVER['PATH_INFO']) { |
/**
|
$path = dirname(substr($_SERVER['PHP_SELF'], 0, -strlen($_SERVER['PATH_INFO']))); |
* Raise Error
|
} else { |
*
|
$path = dirname($_SERVER['PHP_SELF']); |
* Lazy raising of PEAR_Errors.
|
} |
*
|
|
* @static
|
if (substr($path = strtr($path, '\\', '/'), -1) != '/') { |
* @access protected
|
$path .= '/'; |
* @return object PEAR_Error
|
} |
* @param mixed $error
|
|
* @param int $code
|
return $server . $path . $url; |
*/
|
} |
function raiseError($error = null, $code = null)
|
|
{
|
/** |
require_once 'PEAR.php';
|
* Raise Error |
return PEAR::raiseError($error, $code);
|
* |
}
|
* Lazy raising of PEAR_Errors. |
}
|
* |
?>
|
* @static |
|
* @access protected |
|
* @return object PEAR_Error |
|
* @param mixed $error |
|
* @param int $code |
|
*/ |
|
function raiseError($error = null, $code = null) |
|
{ |
|
require_once 'PEAR.php'; |
|
return PEAR::raiseError($error, $code); |
|
} |
|
} |
|
|
|
?> |