HTTP_Header
[ class tree: HTTP_Header ] [ index: HTTP_Header ] [ all elements ]

Source for file Header.php

Documentation is available at Header.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: HTTP :: Header                                               |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Authors: Wolfram Kriesing <wk@visionp.de>                            |
  12. // |          Davey Shafik <davey@php.net>                                |
  13. // |          Michael Wallner <mike@php.net>                              |
  14. // +----------------------------------------------------------------------+
  15. //
  16. // $Id: Header.php,v 1.24 2004/07/28 14:26:30 mike Exp $
  17.  
  18. require_once 'HTTP.php';
  19.  
  20. /**#@+
  21.  * Information Codes
  22.  */
  23. define('HTTP_HEADER_STATUS_100''100 Continue');
  24. define('HTTP_HEADER_STATUS_101''101 Switching Protocols');
  25. define('HTTP_HEADER_STATUS_102''102 Processing');
  26. define('HTTP_HEADER_STATUS_INFORMATIONAL',1);
  27. /**#@-*/
  28.  
  29. /**#+
  30.  * Success Codes
  31.  */
  32. define('HTTP_HEADER_STATUS_200''200 OK');
  33. define('HTTP_HEADER_STATUS_201''201 Created');
  34. define('HTTP_HEADER_STATUS_202''202 Accepted');
  35. define('HTTP_HEADER_STATUS_203''203 Non-Authoriative Information');
  36. define('HTTP_HEADER_STATUS_204''204 No Content');
  37. define('HTTP_HEADER_STATUS_205''205 Reset Content');
  38. define('HTTP_HEADER_STATUS_206''206 Partial Content');
  39. define('HTTP_HEADER_STATUS_207''207 Multi-Status');
  40. define('HTTP_HEADER_STATUS_SUCCESSFUL',2);
  41. /**#@-*/
  42.  
  43. /**#@+
  44.  * Redirection Codes
  45.  */
  46. define('HTTP_HEADER_STATUS_300''300 Multiple Choices');
  47. define('HTTP_HEADER_STATUS_301''301 Moved Permanently');
  48. define('HTTP_HEADER_STATUS_302''302 Found');
  49. define('HTTP_HEADER_STATUS_303''303 See Other');
  50. define('HTTP_HEADER_STATUS_304''304 Not Modified');
  51. define('HTTP_HEADER_STATUS_305''305 Use Proxy');
  52. define('HTTP_HEADER_STATUS_306''306 (Unused)');
  53. define('HTTP_HEADER_STATUS_307''307 Temporary Redirect');
  54. define('HTTP_HEADER_STATUS_REDIRECT',3);
  55. /**#@-*/
  56.  
  57. /**#@+
  58.  * Error Codes
  59.  */
  60. define('HTTP_HEADER_STATUS_400''400 Bad Request');
  61. define('HTTP_HEADER_STATUS_401''401 Unauthorized');
  62. define('HTTP_HEADER_STATUS_402''402 Payment Granted');
  63. define('HTTP_HEADER_STATUS_403''403 Forbidden');
  64. define('HTTP_HEADER_STATUS_404''404 File Not Found');
  65. define('HTTP_HEADER_STATUS_405''405 Method Not Allowed');
  66. define('HTTP_HEADER_STATUS_406''406 Not Acceptable');
  67. define('HTTP_HEADER_STATUS_407''407 Proxy Authentication Required');
  68. define('HTTP_HEADER_STATUS_408''408 Request Time-out');
  69. define('HTTP_HEADER_STATUS_409''409 Conflict');
  70. define('HTTP_HEADER_STATUS_410''410 Gone');
  71. define('HTTP_HEADER_STATUS_411''411 Length Required');
  72. define('HTTP_HEADER_STATUS_412''412 Precondition Failed');
  73. define('HTTP_HEADER_STATUS_413''413 Request Entity Too Large');
  74. define('HTTP_HEADER_STATUS_414''414 Request-URI Too Large');
  75. define('HTTP_HEADER_STATUS_415''415 Unsupported Media Type');
  76. define('HTTP_HEADER_STATUS_416''416 Requested range not satisfiable');
  77. define('HTTP_HEADER_STATUS_417''417 Expectation Failed');
  78. define('HTTP_HEADER_STATUS_422''422 Unprocessable Entity');
  79. define('HTTP_HEADER_STATUS_423''423 Locked');
  80. define('HTTP_HEADER_STATUS_424''424 Failed Dependency');
  81. define('HTTP_HEADER_STATUS_CLIENT_ERROR',4);
  82. /**#@-*/
  83.  
  84. /**#@+
  85.  * Server Errors
  86.  */
  87. define('HTTP_HEADER_STATUS_500''500 Internal Server Error');
  88. define('HTTP_HEADER_STATUS_501''501 Not Implemented');
  89. define('HTTP_HEADER_STATUS_502''502 Bad Gateway');
  90. define('HTTP_HEADER_STATUS_503''503 Service Unavailable');
  91. define('HTTP_HEADER_STATUS_504''504 Gateway Timeout');
  92. define('HTTP_HEADER_STATUS_505''505 HTTP Version Not Supported');
  93. define('HTTP_HEADER_STATUS_507''507 Insufficient Storage');
  94. define('HTTP_HEADER_STATUS_SERVER_ERROR',5);
  95. /**#@-*/
  96.  
  97. /**
  98.  * HTTP_Header
  99.  * 
  100.  * @package     HTTP_Header
  101.  * @category    HTTP
  102.  * @license     PHP License
  103.  * @access      public
  104.  * @version     $Revision: 1.24 $
  105.  */
  106. class HTTP_Header extends HTTP
  107. {
  108.     /**
  109.      * Default Headers
  110.      * 
  111.      * The values that are set as default, are the same as PHP sends by default.
  112.      * 
  113.      * @var     array 
  114.      * @access  private
  115.      */
  116.     var $_headers = array(
  117.         'content-type'  =>  'text/html',
  118.         'pragma'        =>  'no-cache',
  119.         'cache-control' =>  'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  120.     );
  121.  
  122.     /**
  123.      * HTTP version
  124.      * 
  125.      * @var     string 
  126.      * @access  private
  127.      */
  128.     var $_httpVersion '1.0';
  129.  
  130.     /**
  131.      * Constructor
  132.      *
  133.      * Sets HTTP version.
  134.      * 
  135.      * @access  public
  136.      * @return  object  HTTP_Header 
  137.      */
  138.     function HTTP_Header()
  139.     {
  140.         if (isset($_SERVER['SERVER_PROTOCOL'])) {
  141.             $this->setHttpVersion(substr($_SERVER['SERVER_PROTOCOL']-3));
  142.         }
  143.     }
  144.     
  145.     /**
  146.      * Set HTTP version
  147.      *
  148.      * @access  public
  149.      * @return  bool    Returns true on success or false if version doesn't
  150.      *                   match 1.0 or 1.1 (note: 1 will result in 1.0)
  151.      * @param   mixed   $version HTTP version, either 1.0 or 1.1
  152.      */
  153.     function setHttpVersion($version)
  154.     {
  155.         $version round((float) $version1);
  156.         if ($version < 1.0 || $version > 1.1{
  157.             return false;
  158.         }
  159.         $this->_httpVersion sprintf('%0.1f'$version);
  160.         return true;
  161.     }
  162.     
  163.     /**
  164.      * Get HTTP version
  165.      *
  166.      * @access  public
  167.      * @return  string 
  168.      */
  169.     function getHttpVersion()
  170.     {
  171.         return $this->_httpVersion;
  172.     }
  173.     
  174.     /**
  175.      * Set Header
  176.      * 
  177.      * The default value for the Last-Modified header will be current
  178.      * date and atime if $value is omitted.
  179.      * 
  180.      * @access  public
  181.      * @return  bool    Returns true on success or false if $key was empty or
  182.      *                   $value was not of an scalar type.
  183.      * @param   string  $key The name of the header.
  184.      * @param   string  $value The value of the header.
  185.      */
  186.     function setHeader($key$value = null)
  187.     {
  188.         if (empty($key|| (isset($value&& !is_scalar($value))) {
  189.             return false;
  190.         }
  191.         
  192.         $key = strToLower($key);
  193.         
  194.         if ($key == 'last-modified'{
  195.             if (!isset($value)) {
  196.                 $value = HTTP::Date(time());
  197.             elseif (is_numeric($value)) {
  198.                 $value = HTTP::Date($value);
  199.             }
  200.         }
  201.         
  202.         $this->_headers[$key$value;
  203.         return true;
  204.     }
  205.  
  206.     /**
  207.      * Get Header
  208.      * 
  209.      * If $key is omitted, all stored headers will be returned.
  210.      * 
  211.      * @access  public
  212.      * @return  mixed   Returns string value of the requested header,
  213.      *                   array values of all headers or false if header $key
  214.      *                   is not set.
  215.      * @param   string  $key    The name of the header to fetch.
  216.      */
  217.     function getHeader($key = null)
  218.     {
  219.         if (!isset($key)) {
  220.             return $this->_headers;
  221.         }
  222.         
  223.         $key = strToLower($key);
  224.         
  225.         if (!isset($this->_headers[$key])) {
  226.             return false;
  227.         }
  228.         
  229.         return $this->_headers[$key];
  230.     }
  231.  
  232.     /**
  233.      * Send Headers
  234.      * 
  235.      * Send out the header that you set via setHeader().
  236.      * 
  237.      * @access  public
  238.      * @return  bool    Returns true on success or false if headers are already
  239.      *                   sent.
  240.      * @param   array   $keys Headers to (not) send, see $include.
  241.      * @param   array   $include If true only $keys matching headers will be
  242.      *                   sent, if false only header not matching $keys will be
  243.      *                   sent.
  244.      */
  245.     function sendHeaders($keys = array()$include = true)
  246.     {
  247.         if (headers_sent()) {
  248.             return false;
  249.         }
  250.         
  251.         if (count($keys)) {
  252.             array_change_key_case($keysCASE_LOWER);
  253.             foreach ($this->_headers as $key => $value{
  254.                 if ($include in_array($key$keys!in_array($key$keys)) {
  255.                     header($key .': '$value);
  256.                 }
  257.             }
  258.         else {
  259.             foreach ($this->_headers as $header => $value{
  260.                 header($header .': '$value);
  261.             }
  262.         }
  263.         return true;
  264.     }
  265.  
  266.     /**
  267.      * Send Satus Code
  268.      * 
  269.      * Send out the given HTTP-Status code. Use this for example when you
  270.      * want to tell the client this page is cached, then you would call
  271.      * sendStatusCode(304).
  272.      *
  273.      * @see HTTP_Header_Cache::exitIfCached()
  274.      * 
  275.      * @access  public
  276.      * @return  bool    Returns true on success or false if headers are already
  277.      *                   sent.
  278.      * @param   int     $code The status code to send, i.e. 404, 304, 200, etc.
  279.      */
  280.     function sendStatusCode($code)
  281.     {
  282.         if (headers_sent()) {
  283.             return false;
  284.         }
  285.         
  286.         if ($code == (int) $code && defined('HTTP_HEADER_STATUS_'$code)) {
  287.             $code constant('HTTP_HEADER_STATUS_'$code);
  288.         }
  289.         
  290.         header('HTTP/'$this->_httpVersion .' '$code);
  291.         return true;
  292.     }
  293.  
  294.     /**
  295.      * Date to Timestamp
  296.      * 
  297.      * Converts dates like
  298.      *      Mon, 31 Mar 2003 15:26:34 GMT
  299.      *      Tue, 15 Nov 1994 12:45:26 GMT
  300.      * into a timestamp, strtotime() didn't do it in older versions.
  301.      *
  302.      * @deprecated      Use PHPs strtotime() instead.
  303.      * @access  public
  304.      * @return  mixed   Returns int unix timestamp or false if the date doesn't
  305.      *                   seem to be a valid GMT date.
  306.      * @param   string  $date The GMT date.
  307.      */
  308.     function dateToTimestamp($date)
  309.     {
  310.         static $monts = array(
  311.             null => 0'Jan' => 1'Feb' => 2'Mar' => 3'Apr' => 4,
  312.             'May' => 5'Jun' => 6'Jul' => 7'Aug' => 8'Sep' => 9,
  313.             'Oct' => 10'Nov' => 11'Dec' => 12
  314.         );
  315.         
  316.         if (-1 < $timestamp = strToTime($date)) {
  317.             return $timestamp;
  318.         }
  319.         
  320.         if (!preg_match('~[^,]*,\s(\d+)\s(\w+)\s(\d+)\s(\d+):(\d+):(\d+).*~',
  321.             $date$m)) {
  322.             return false;
  323.         }
  324.         
  325.         // [0] => Mon, 31 Mar 2003 15:42:55 GMT
  326.         // [1] => 31 [2] => Mar [3] => 2003 [4] => 15 [5] => 42 [6] => 55
  327.         return mktime($m[4]$m[5]$m[6]$months[$m[2]]$m[1]$m[3]);
  328.     }
  329.  
  330.     /**
  331.      * Redirect
  332.      * 
  333.      * This function redirects the client. This is done by issuing a Location
  334.      * header and exiting.  Additionally to HTTP::redirect() you can also add
  335.      * parameters to the url.
  336.      * 
  337.      * If you dont need parameters to be added, simply use HTTP::redirect()
  338.      * otherwise use HTTP_Header::redirect().
  339.      *
  340.      * @see     HTTP::redirect()
  341.      * @author  Wolfram Kriesing <wk@visionp.de>
  342.      * @access  public
  343.      * @return  void 
  344.      * @param   string  $url The URL to redirect to, if none is given it
  345.      *                   redirects to the current page.
  346.      * @param   array   $param Array of query string parameters to add; usually
  347.      *                   a set of key => value pairs; if an array entry consists
  348.      *                   only of an value it is used as key and the respective
  349.      *                   value is fetched from $GLOBALS[$value]
  350.      * @param   bool    $session Whether the session name/id should be added
  351.      */
  352.     function redirect($url = null$param = array()$session = false)
  353.     {
  354.         if (!isset($url)) {
  355.             $url $_SERVER['PHP_SELF'];
  356.         }
  357.         
  358.         $qs = array();
  359.  
  360.         if ($session{
  361.             $qs[session_name(.'='session_id();
  362.         }
  363.  
  364.         if (is_array($param&& count($param)) {
  365.             if (count($param)) {
  366.                 foreach ($param as $key => $val{
  367.                     if (is_string($key)) {
  368.                         $qs[urlencode($key.'='urlencode($val);
  369.                     else {
  370.                         $qs[urlencode($val.'='urlencode(@$GLOBALS[$val]);
  371.                     }
  372.                 }
  373.             }
  374.         }
  375.         
  376.         if ($qstr implode('&'$qs)) {
  377.             $purl parse_url($url);
  378.             $url .= (isset($purl['query']'&' '?'$qstr;
  379.         }
  380.  
  381.         parent::redirect($url);
  382.     }
  383.  
  384.     /**#@+
  385.      * @author  Davey Shafik <davey@php.net>
  386.      * @param   int $http_code HTTP Code to check
  387.      * @access  public
  388.      */
  389.  
  390.     /**
  391.      * Return HTTP Status Code Type
  392.      *
  393.      * @return int|false
  394.      */
  395.     function getStatusType($http_code
  396.     {
  397.         if(is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code|| defined($http_code)) {
  398.             $type substr($http_code,0,1);
  399.             switch ($type{
  400.                 case HTTP_HEADER_STATUS_INFORMATIONAL:
  401.                 case HTTP_HEADER_STATUS_SUCCESSFUL:
  402.                 case HTTP_HEADER_STATUS_REDIRECT:
  403.                 case HTTP_HEADER_STATUS_CLIENT_ERROR:
  404.                 case HTTP_HEADER_STATUS_SERVER_ERROR:
  405.                     return $type;
  406.                     break;
  407.                 default:
  408.                     return false;
  409.                     break;
  410.             }
  411.         else {
  412.             return false;
  413.         }
  414.     }
  415.  
  416.     /**
  417.      * Return Status Code Message
  418.      *
  419.      * @return string|false
  420.      */
  421.     function getStatusText($http_code
  422.     {
  423.         if ($this->getStatusType($http_code)) {
  424.             if (is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code)) {
  425.                 return substr(constant('HTTP_HEADER_STATUS_' .$http_code),4);
  426.             else {
  427.                 return substr($http_code,4);
  428.             }
  429.         else {
  430.             return false;
  431.         }
  432.     }
  433.  
  434.     /**
  435.      * Checks if HTTP Status code is Information (1xx)
  436.      *
  437.      * @return boolean 
  438.      */
  439.     function isInformational($http_code
  440.     {
  441.         if ($status_type $this->getStatusType($http_code)) {
  442.             return $status_type{0== HTTP_HEADER_STATUS_INFORMATIONAL;
  443.         else {
  444.             return false;
  445.         }
  446.     }
  447.  
  448.     /**
  449.      * Checks if HTTP Status code is Successful (2xx)
  450.      *
  451.      * @return boolean 
  452.      */
  453.     function isSuccessful($http_code
  454.     {
  455.         if ($status_type $this->getStatusType($http_code)) {
  456.             return $status_type{0== HTTP_HEADER_STATUS_SUCCESSFUL;
  457.         else {
  458.             return false;
  459.         }
  460.     }
  461.  
  462.     /**
  463.      * Checks if HTTP Status code is a Redirect (3xx)
  464.      *
  465.      * @return boolean 
  466.      */
  467.     function isRedirect($http_code
  468.     {
  469.         if ($status_type $this->getStatusType($http_code)) {
  470.             return $status_type{0== HTTP_HEADER_STATUS_REDIRECT;
  471.         else {
  472.             return false;
  473.         }
  474.     }
  475.  
  476.     /**
  477.      * Checks if HTTP Status code is a Client Error (4xx)
  478.      *
  479.      * @return boolean 
  480.      */
  481.     function isClientError($http_code
  482.     {
  483.         if ($status_type $this->getStatusType($http_code)) {
  484.             return $status_type{0== HTTP_HEADER_STATUS_CLIENT_ERROR;
  485.         else {
  486.             return false;
  487.         }
  488.     }
  489.  
  490.     /**
  491.      * Checks if HTTP Status code is Server Error (5xx)
  492.      *
  493.      * @return boolean 
  494.      */
  495.     function isServerError($http_code
  496.     {
  497.         if ($status_type $this->getStatusType($http_code)) {
  498.             return $status_type{0== HTTP_HEADER_STATUS_SERVER_ERROR;
  499.         else {
  500.             return false;
  501.         }
  502.     }
  503.  
  504.     /**
  505.      * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
  506.      *
  507.      * @return boolean 
  508.      */
  509.     function isError($http_code
  510.     {
  511.         if ($status_type $this->getStatusType($http_code)) {
  512.             return (($status_type == HTTP_HEADER_STATUS_CLIENT_ERROR|| ($status_type == HTTP_HEADER_STATUS_SERVER_ERROR)) ? true : false;
  513.         else {
  514.             return false;
  515.         }
  516.     }
  517.     /**#@-*/
  518. }
  519. ?>

Documentation generated on Mon, 11 Mar 2019 13:54:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.