From: Stig Bakken Date: Sat, 26 Feb 2000 03:38:27 +0000 (+0000) Subject: Added simple HTTP utility class, currently with Date() and X-Git-Tag: PHP-4.0-RC1~404 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4d7150657c6fb4154beecdfa6308e6bec5810c37;p=php Added simple HTTP utility class, currently with Date() and negotiateLanguage(). --- diff --git a/pear/HTTP.php b/pear/HTTP.php new file mode 100644 index 0000000000..3947c06d6d --- /dev/null +++ b/pear/HTTP.php @@ -0,0 +1,110 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ +// +// HTTP utility functions. +// + +class HTTP { + /** + * Format a date according to RFC-XXXX (can't remember the HTTP + * RFC number off-hand anymore, shame on me). This function + * honors the "y2k_compliance" php.ini directive. + * + * @param $time int UNIX timestamp + * + * @return HTTP date string, or false for an invalid timestamp. + * + * @author Stig Bakken + */ + function Date($time) { + $y = ini_get("y2k_compliance") ? "Y" : "y"; + return gmdate("D, d M $y H:i:s", $time); + } + + /** + * 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 language spoken in + * a particular country. For example, U.S. English is "en_US", + * while British English is "en_UK". Portugese as spoken in + * Portugal is "pt_PT", while Brazilian Portugese is "pt_BR". + * Two-letter country codes can be found in the ISO 3166 standard. + * + * Quantities in the Accept-Language: header are supported, for + * example: + * + * Accept-Language: en_UK;q=0.7, en_US;q=0.6, no;q=1.0, dk;q=0.8 + * + * @param $supported an associative array indexed by language + * codes (country codes) supported by the application. Values + * must evaluate to true. + * + * @param $default the default language to use if none is found + * during negotiation, defaults to "en_US" for U.S. English + * + * @author Stig Bakken + */ + function negotiateLanguage(&$supported, $default = 'en_US') { + global $HTTP_ACCEPT_LANGUAGE; + + /* If the client has sent an Accept-Language: header, see if + * it contains a language we support. + */ + if ($HTTP_ACCEPT_LANGUAGE) { + $accepted = split(',[[:space:]]*', $HTTP_ACCEPT_LANGUAGE); + for ($i = 0; $i < count($accepted); $i++) { + if (eregi('^([a-z]+);[[:space:]]*q=([0-9\.]+)', $accepted[$i], &$arr)) { + $q = (double)$arr[2]; + $l = $arr[1]; + } else { + $q = 42; + $l = $accepted[$i]; + } + if ($supported[$l] && $q > 0.0) { + if ($q == 42) { + return $l; + } + $candidates[$l] = $q; + } + } + if (isset($candidates)) { + arsort($candidates); + reset($candidates); + return key($candidates); + } + } + + /* Check for a valid language code in the top-level domain of + * the client's host address. + */ + if (eregi("\.[^\.]+$", $REMOTE_HOST, &$arr)) { + $lang = strtolower($arr[1]); + if ($supported[$lang]) { + return $lang; + } + } + + return $default; + } +} + +?>