* require_once 'HTTP.php';
* $langs = array(
* 'en' => 'locales/en',
* 'en-US' => 'locales/en',
* 'en-UK' => 'locales/en',
* 'de' => 'locales/de',
* 'de-DE' => 'locales/de',
* 'de-AT' => 'locales/de',
* );
* $neg = HTTP::negotiateLanguage($langs);
* $dir = $langs[$neg];
*
*
* @param array $supported An associative array of supported languages,
* whose values must evaluate to true.
* @param string $default The default language to use if none is found.
*
* @return string The negotiated language result or the supplied default.
* @static
* @access public
*/
function negotiateLanguage($supported, $default = 'en-US')
{
$supp = array();
foreach ($supported as $lang => $isSupported) {
if ($isSupported) {
$supp[strtolower($lang)] = $lang;
}
}
if (!count($supp)) {
return $default;
}
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$match = HTTP::_matchAccept($_SERVER['HTTP_ACCEPT_LANGUAGE'],
$supp);
if (!is_null($match)) {
return $match;
}
}
if (isset($_SERVER['REMOTE_HOST'])) {
$lang = strtolower(end($h = explode('.', $_SERVER['REMOTE_HOST'])));
if (isset($supp[$lang])) {
return $supp[$lang];
}
}
return $default;
}
/**
* Negotiates charset with the user's browser through the Accept-Charset
* HTTP header.
*
* Quality factors in the Accept-Charset: header are supported, e.g.:
* Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8
*
*
* require_once 'HTTP.php';
* $charsets = array(
* 'UTF-8',
* 'ISO-8859-1',
* );
* $charset = HTTP::negotiateCharset($charsets);
*
*
* @param array $supported An array of supported charsets
* @param string $default The default charset to use if none is found.
*
* @return string The negotiated language result or the supplied default.
* @static
* @author Philippe Jausions
* require_once 'HTTP.php';
* $contentType = array(
* 'application/xhtml+xml',
* 'application/xml',
* 'text/html',
* 'text/plain',
* );
* $mime = HTTP::negotiateContentType($contentType);
*
*
* @param array $supported An associative array of supported MIME types.
* @param string $default The default type to use if none match.
*
* @return string The negotiated MIME type result or the supplied default.
* @static
* @author Philippe Jausions
* Array
* (
* [response_code] => 200 // The HTTP response code
* [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
* [Connection] => close
* [Content-Type] => text/html
* )
*
*
* @param string $url A valid URL, e.g.: http://pear.php.net/credits.php
* @param integer $timeout Timeout in seconds (default = 10)
*
* @return array Returns associative array of response headers on success
* or PEAR error on failure.
* @static
* @access public
* @see HTTP_Client::head()
* @see HTTP_Request
*/
function head($url, $timeout = 10)
{
$p = parse_url($url);
if (!isset($p['scheme'])) {
$p = parse_url(HTTP::absoluteURI($url));
} elseif ($p['scheme'] != 'http') {
return HTTP::raiseError('Unsupported protocol: '. $p['scheme']);
}
$port = isset($p['port']) ? $p['port'] : 80;
if (!$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout)) {
return HTTP::raiseError("Connection error: $estr ($eno)");
}
$path = !empty($p['path']) ? $p['path'] : '/';
$path .= !empty($p['query']) ? '?' . $p['query'] : '';
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");
$response = rtrim(fgets($fp, 4096));
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
$headers['response_code'] = $status[1];
}
$headers['response'] = $response;
while ($line = fgets($fp, 4096)) {
if (!trim($line)) {
break;
}
if (($pos = strpos($line, ':')) !== false) {
$header = substr($line, 0, $pos);
$value = trim(substr($line, $pos + 1));
$headers[$header] = $value;
}
}
fclose($fp);
return $headers;
}
/**
* This function redirects the client. This is done by issuing
* a "Location" header and exiting if wanted. If you set $rfc2616 to true
* HTTP will output a hypertext note with the location of the redirect.
*
* @param string $url URL where the redirect should go to.
* @param bool $exit Whether to exit immediately after redirection.
* @param bool $rfc2616 Wheter to output a hypertext note where we're
* redirecting to (Redirecting to
* ....)
*
* @return boolean Returns TRUE on succes (or exits) or FALSE if headers
* have already been sent.
* @static
* @access public
*/
function redirect($url, $exit = true, $rfc2616 = false)
{
if (headers_sent()) {
return false;
}
$url = HTTP::absoluteURI($url);
header('Location: '. $url);
if ($rfc2616 && isset($_SERVER['REQUEST_METHOD'])
&& $_SERVER['REQUEST_METHOD'] != 'HEAD') {
echo '
Redirecting to: ' .htmlspecialchars($url).'.
'; } if ($exit) { exit; } return true; } /** * This function returns the absolute URI for the partial URL passed. * The current scheme (HTTP/HTTPS), host server, port, current script * location are used if necessary to resolve any relative URLs. * * Offsets potentially created by PATH_INFO are taken care of to resolve * relative URLs to the current script. * * 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. * * @param string $url Absolute or relative URI the redirect should * go to. * @param string $protocol Protocol to use when redirecting URIs. * @param integer $port A new port number. * * @return string The absolute URI. * @author Philippe Jausions