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.26 2004/12/13 17:34:26 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.26 $
  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.         if (strncasecmp(PHP_SAPI'cgi'3)) {
  291.             header('HTTP/'$this->_httpVersion .' '$code);
  292.         else {
  293.             header('Status: '$code);
  294.         }
  295.         return true;
  296.     }
  297.  
  298.     /**
  299.      * Date to Timestamp
  300.      * 
  301.      * Converts dates like
  302.      *      Mon, 31 Mar 2003 15:26:34 GMT
  303.      *      Tue, 15 Nov 1994 12:45:26 GMT
  304.      * into a timestamp, strtotime() didn't do it in older versions.
  305.      *
  306.      * @deprecated      Use PHPs strtotime() instead.
  307.      * @access  public
  308.      * @return  mixed   Returns int unix timestamp or false if the date doesn't
  309.      *                   seem to be a valid GMT date.
  310.      * @param   string  $date The GMT date.
  311.      */
  312.     function dateToTimestamp($date)
  313.     {
  314.         static $months = array(
  315.             null => 0'Jan' => 1'Feb' => 2'Mar' => 3'Apr' => 4,
  316.             'May' => 5'Jun' => 6'Jul' => 7'Aug' => 8'Sep' => 9,
  317.             'Oct' => 10'Nov' => 11'Dec' => 12
  318.         );
  319.         
  320.         if (-1 < $timestamp = strToTime($date)) {
  321.             return $timestamp;
  322.         }
  323.         
  324.         if (!preg_match('~[^,]*,\s(\d+)\s(\w+)\s(\d+)\s(\d+):(\d+):(\d+).*~',
  325.             $date$m)) {
  326.             return false;
  327.         }
  328.         
  329.         // [0] => Mon, 31 Mar 2003 15:42:55 GMT
  330.         // [1] => 31 [2] => Mar [3] => 2003 [4] => 15 [5] => 42 [6] => 55
  331.         return mktime($m[4]$m[5]$m[6]$months[$m[2]]$m[1]$m[3]);
  332.     }
  333.  
  334.     /**
  335.      * Redirect
  336.      * 
  337.      * This function redirects the client. This is done by issuing a Location
  338.      * header and exiting.  Additionally to HTTP::redirect() you can also add
  339.      * parameters to the url.
  340.      * 
  341.      * If you dont need parameters to be added, simply use HTTP::redirect()
  342.      * otherwise use HTTP_Header::redirect().
  343.      *
  344.      * @see     HTTP::redirect()
  345.      * @author  Wolfram Kriesing <wk@visionp.de>
  346.      * @access  public
  347.      * @return  void 
  348.      * @param   string  $url The URL to redirect to, if none is given it
  349.      *                   redirects to the current page.
  350.      * @param   array   $param Array of query string parameters to add; usually
  351.      *                   a set of key => value pairs; if an array entry consists
  352.      *                   only of an value it is used as key and the respective
  353.      *                   value is fetched from $GLOBALS[$value]
  354.      * @param   bool    $session Whether the session name/id should be added
  355.      */
  356.     function redirect($url = null$param = array()$session = false)
  357.     {
  358.         if (!isset($url)) {
  359.             $url $_SERVER['PHP_SELF'];
  360.         }
  361.         
  362.         $qs = array();
  363.  
  364.         if ($session{
  365.             $qs[session_name(.'='session_id();
  366.         }
  367.  
  368.         if (is_array($param&& count($param)) {
  369.             if (count($param)) {
  370.                 foreach ($param as $key => $val{
  371.                     if (is_string($key)) {
  372.                         $qs[urlencode($key.'='urlencode($val);
  373.                     else {
  374.                         $qs[urlencode($val.'='urlencode(@$GLOBALS[$val]);
  375.                     }
  376.                 }
  377.             }
  378.         }
  379.         
  380.         if ($qstr implode('&'$qs)) {
  381.             $purl parse_url($url);
  382.             $url .= (isset($purl['query']'&' '?'$qstr;
  383.         }
  384.  
  385.         parent::redirect($url);
  386.     }
  387.  
  388.     /**#@+
  389.      * @author  Davey Shafik <davey@php.net>
  390.      * @param   int $http_code HTTP Code to check
  391.      * @access  public
  392.      */
  393.  
  394.     /**
  395.      * Return HTTP Status Code Type
  396.      *
  397.      * @return int|false
  398.      */
  399.     function getStatusType($http_code
  400.     {
  401.         if(is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code|| defined($http_code)) {
  402.             $type substr($http_code,0,1);
  403.             switch ($type{
  404.                 case HTTP_HEADER_STATUS_INFORMATIONAL:
  405.                 case HTTP_HEADER_STATUS_SUCCESSFUL:
  406.                 case HTTP_HEADER_STATUS_REDIRECT:
  407.                 case HTTP_HEADER_STATUS_CLIENT_ERROR:
  408.                 case HTTP_HEADER_STATUS_SERVER_ERROR:
  409.                     return $type;
  410.                     break;
  411.                 default:
  412.                     return false;
  413.                     break;
  414.             }
  415.         else {
  416.             return false;
  417.         }
  418.     }
  419.  
  420.     /**
  421.      * Return Status Code Message
  422.      *
  423.      * @return string|false
  424.      */
  425.     function getStatusText($http_code
  426.     {
  427.         if ($this->getStatusType($http_code)) {
  428.             if (is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code)) {
  429.                 return substr(constant('HTTP_HEADER_STATUS_' .$http_code),4);
  430.             else {
  431.                 return substr($http_code,4);
  432.             }
  433.         else {
  434.             return false;
  435.         }
  436.     }
  437.  
  438.     /**
  439.      * Checks if HTTP Status code is Information (1xx)
  440.      *
  441.      * @return boolean 
  442.      */
  443.     function isInformational($http_code
  444.     {
  445.         if ($status_type $this->getStatusType($http_code)) {
  446.             return $status_type{0== HTTP_HEADER_STATUS_INFORMATIONAL;
  447.         else {
  448.             return false;
  449.         }
  450.     }
  451.  
  452.     /**
  453.      * Checks if HTTP Status code is Successful (2xx)
  454.      *
  455.      * @return boolean 
  456.      */
  457.     function isSuccessful($http_code
  458.     {
  459.         if ($status_type $this->getStatusType($http_code)) {
  460.             return $status_type{0== HTTP_HEADER_STATUS_SUCCESSFUL;
  461.         else {
  462.             return false;
  463.         }
  464.     }
  465.  
  466.     /**
  467.      * Checks if HTTP Status code is a Redirect (3xx)
  468.      *
  469.      * @return boolean 
  470.      */
  471.     function isRedirect($http_code
  472.     {
  473.         if ($status_type $this->getStatusType($http_code)) {
  474.             return $status_type{0== HTTP_HEADER_STATUS_REDIRECT;
  475.         else {
  476.             return false;
  477.         }
  478.     }
  479.  
  480.     /**
  481.      * Checks if HTTP Status code is a Client Error (4xx)
  482.      *
  483.      * @return boolean 
  484.      */
  485.     function isClientError($http_code
  486.     {
  487.         if ($status_type $this->getStatusType($http_code)) {
  488.             return $status_type{0== HTTP_HEADER_STATUS_CLIENT_ERROR;
  489.         else {
  490.             return false;
  491.         }
  492.     }
  493.  
  494.     /**
  495.      * Checks if HTTP Status code is Server Error (5xx)
  496.      *
  497.      * @return boolean 
  498.      */
  499.     function isServerError($http_code
  500.     {
  501.         if ($status_type $this->getStatusType($http_code)) {
  502.             return $status_type{0== HTTP_HEADER_STATUS_SERVER_ERROR;
  503.         else {
  504.             return false;
  505.         }
  506.     }
  507.  
  508.     /**
  509.      * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
  510.      *
  511.      * @return boolean 
  512.      */
  513.     function isError($http_code
  514.     {
  515.         if ($status_type $this->getStatusType($http_code)) {
  516.             return (($status_type == HTTP_HEADER_STATUS_CLIENT_ERROR|| ($status_type == HTTP_HEADER_STATUS_SERVER_ERROR)) ? true : false;
  517.         else {
  518.             return false;
  519.         }
  520.     }
  521.     /**#@-*/
  522. }
  523. ?>

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