You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.9 KiB

26 years ago
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no> |
  17. // | |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id$
  21. //
  22. // HTTP utility functions.
  23. //
  24. if (!empty($GLOBALS['USED_PACKAGES']['HTTP'])) return;
  25. $GLOBALS['USED_PACKAGES']['HTTP'] = true;
  26. class HTTP {
  27. /**
  28. * Format a date according to RFC-XXXX (can't remember the HTTP
  29. * RFC number off-hand anymore, shame on me). This function
  30. * honors the "y2k_compliance" php.ini directive.
  31. *
  32. * @param $time int UNIX timestamp
  33. *
  34. * @return HTTP date string, or false for an invalid timestamp.
  35. *
  36. * @author Stig Bakken <ssb@fast.no>
  37. */
  38. function Date($time) {
  39. $y = ini_get("y2k_compliance") ? "Y" : "y";
  40. return gmdate("D, d M $y H:i:s", $time);
  41. }
  42. /**
  43. * Negotiate language with the user's browser through the
  44. * Accept-Language HTTP header or the user's host address.
  45. * Language codes are generally in the form "ll" for a language
  46. * spoken in only one country, or "ll_CC" for a language spoken in
  47. * a particular country. For example, U.S. English is "en_US",
  48. * while British English is "en_UK". Portugese as spoken in
  49. * Portugal is "pt_PT", while Brazilian Portugese is "pt_BR".
  50. * Two-letter country codes can be found in the ISO 3166 standard.
  51. *
  52. * Quantities in the Accept-Language: header are supported, for
  53. * example:
  54. *
  55. * Accept-Language: en_UK;q=0.7, en_US;q=0.6, no;q=1.0, dk;q=0.8
  56. *
  57. * @param $supported an associative array indexed by language
  58. * codes (country codes) supported by the application. Values
  59. * must evaluate to true.
  60. *
  61. * @param $default the default language to use if none is found
  62. * during negotiation, defaults to "en_US" for U.S. English
  63. *
  64. * @author Stig Bakken <ssb@fast.no>
  65. */
  66. function negotiateLanguage(&$supported, $default = 'en_US') {
  67. global $HTTP_SERVER_VARS;
  68. /* If the client has sent an Accept-Language: header, see if
  69. * it contains a language we support.
  70. */
  71. if (isset($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE'])) {
  72. $accepted = split(',[[:space:]]*', $HTTP_ACCEPT_LANGUAGE);
  73. for ($i = 0; $i < count($accepted); $i++) {
  74. if (eregi('^([a-z]+);[[:space:]]*q=([0-9\.]+)', $accepted[$i], &$arr)) {
  75. $q = (double)$arr[2];
  76. $l = $arr[1];
  77. } else {
  78. $q = 42;
  79. $l = $accepted[$i];
  80. }
  81. if (!empty($supported[$l]) && ($q > 0.0)) {
  82. if ($q == 42) {
  83. return $l;
  84. }
  85. $candidates[$l] = $q;
  86. }
  87. }
  88. if (isset($candidates)) {
  89. arsort($candidates);
  90. reset($candidates);
  91. return key($candidates);
  92. }
  93. }
  94. /* Check for a valid language code in the top-level domain of
  95. * the client's host address.
  96. */
  97. if (ereg("\.[^\.]+$", $HTTP_SERVER_VARS['REMOTE_HOST'], &$arr)) {
  98. $lang = strtolower($arr[1]);
  99. if (!empty($supported[$lang])) {
  100. return $lang;
  101. }
  102. }
  103. return $default;
  104. }
  105. }
  106. ?>