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

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