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.

171 lines
6.3 KiB

25 years ago
25 years ago
24 years ago
26 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 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. // | Author: Stig Bakken <ssb@fast.no> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. //
  21. // HTTP utility functions.
  22. //
  23. if (!empty($GLOBALS['USED_PACKAGES']['HTTP'])) return;
  24. $GLOBALS['USED_PACKAGES']['HTTP'] = true;
  25. class HTTP {
  26. /**
  27. * Format a RFC compliant HTTP header. This function
  28. * honors the "y2k_compliance" php.ini directive.
  29. *
  30. * @param $time int UNIX timestamp
  31. *
  32. * @return HTTP date string, or false for an invalid timestamp.
  33. *
  34. * @author Stig Bakken <ssb@fast.no>
  35. * @author Sterling Hughes <sterling@php.net>
  36. */
  37. function Date($time) {
  38. /* If we're y2k compliant, use the newer, reccomended RFC 822
  39. format */
  40. if (ini_get("y2k_compliance") == true) {
  41. return gmdate("D, d M Y H:i:s \G\M\T", $time);
  42. }
  43. /* Use RFC-850 which supports two character year numbers */
  44. else {
  45. return gmdate("F, d-D-y H:i:s \G\M\T", $time);
  46. }
  47. }
  48. /**
  49. * Negotiate language with the user's browser through the
  50. * Accept-Language HTTP header or the user's host address.
  51. * Language codes are generally in the form "ll" for a language
  52. * spoken in only one country, or "ll-CC" for a language spoken in
  53. * a particular country. For example, U.S. English is "en-US",
  54. * while British English is "en-UK". Portugese as spoken in
  55. * Portugal is "pt-PT", while Brazilian Portugese is "pt-BR".
  56. * Two-letter country codes can be found in the ISO 3166 standard.
  57. *
  58. * Quantities in the Accept-Language: header are supported, for
  59. * example:
  60. *
  61. * Accept-Language: en-UK;q=0.7, en-US;q=0.6, no;q=1.0, dk;q=0.8
  62. *
  63. * @param $supported an associative array indexed by language
  64. * codes (country codes) supported by the application. Values
  65. * must evaluate to true.
  66. *
  67. * @param $default the default language to use if none is found
  68. * during negotiation, defaults to "en-US" for U.S. English
  69. *
  70. * @author Stig Bakken <ssb@fast.no>
  71. */
  72. function negotiateLanguage(&$supported, $default = 'en-US') {
  73. global $HTTP_SERVER_VARS;
  74. /* If the client has sent an Accept-Language: header, see if
  75. * it contains a language we support.
  76. */
  77. if (isset($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE'])) {
  78. $accepted = split(',[[:space:]]*', $HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE']);
  79. for ($i = 0; $i < count($accepted); $i++) {
  80. if (eregi('^([a-z]+);[[:space:]]*q=([0-9\.]+)', $accepted[$i], $arr)) {
  81. $q = (double)$arr[2];
  82. $l = $arr[1];
  83. } else {
  84. $q = 42;
  85. $l = $accepted[$i];
  86. }
  87. if (!empty($supported[$l]) && ($q > 0.0)) {
  88. if ($q == 42) {
  89. return $l;
  90. }
  91. $candidates[$l] = $q;
  92. }
  93. }
  94. if (isset($candidates)) {
  95. arsort($candidates);
  96. reset($candidates);
  97. return key($candidates);
  98. }
  99. }
  100. /* Check for a valid language code in the top-level domain of
  101. * the client's host address.
  102. */
  103. if (isset($HTTP_SERVER_VARS['REMOTE_HOST']) &&
  104. ereg("\.[^\.]+$", $HTTP_SERVER_VARS['REMOTE_HOST'], $arr)) {
  105. $lang = strtolower($arr[1]);
  106. if (!empty($supported[$lang])) {
  107. return $lang;
  108. }
  109. }
  110. return $default;
  111. }
  112. /**
  113. * Sends a "HEAD" HTTP command to a server and returns the headers
  114. * as an associative array. Example output could be:
  115. * Array
  116. * (
  117. * [response_code] => 200 // The HTTP response code
  118. * [response] => HTTP/1.1 200 OK // The full HTTP response string
  119. * [Date] => Fri, 11 Jan 2002 01:41:44 GMT
  120. * [Server] => Apache/1.3.20 (Unix) PHP/4.1.1
  121. * [X-Powered-By] => PHP/4.1.1
  122. * [Connection] => close
  123. * [Content-Type] => text/html
  124. * )
  125. *
  126. * @param string $url A valid url, for ex: http://pear.php.net/credits.php
  127. * @return mixed Assoc array or PEAR error on no conection
  128. *
  129. * @author Tomas V.V.Cox <cox@idecnet.com>
  130. */
  131. function head($url)
  132. {
  133. $purl = parse_url($url);
  134. $port = (isset($purl['port'])) ? $purl['port'] : 80;
  135. $fp = fsockopen($purl['host'], $port, $errno, $errstr, 10);
  136. if (!$fp) {
  137. return PEAR::raiseError("HTTP::head Error $errstr ($erno)");
  138. }
  139. $path = (!empty($purl['path'])) ? $purl['path'] : '/';
  140. fputs($fp, "HEAD $path HTTP/1.0\r\n");
  141. fputs($fp, "Host: " . $purl['host'] . "\r\n\r\n");
  142. $response = rtrim(fgets($fp, 4096));
  143. if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
  144. $headers['response_code'] = $status[1];
  145. }
  146. $headers['response'] = $response;
  147. while ($line = fgets($fp, 4096)) {
  148. if (!trim($line)) {
  149. break;
  150. }
  151. if (($pos = strpos($line, ':')) !== false) {
  152. $header = substr($line, 0, $pos);
  153. $value = trim(substr($line, $pos + 1));
  154. $headers[$header] = $value;
  155. }
  156. }
  157. fclose($fp);
  158. return $headers;
  159. }
  160. }
  161. ?>