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.18 2004/06/09 18:08:48 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. @package      HTTP_Header
  104. @category     HTTP
  105. @access       public
  106. @version      $Revision: 1.18 $
  107. */
  108. class HTTP_Header extends HTTP
  109. {
  110.     /**
  111.     * Default Headers
  112.     * 
  113.     * The values that are set as default, are the same as PHP sends by default.
  114.     * 
  115.     * @var      array 
  116.     * @access   private
  117.     */
  118.     var $_headers = array(
  119.         'content-type'  =>  'text/html',
  120.         'pragma'        =>  'no-cache',
  121.         'cache-control' =>  'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  122.     );
  123.  
  124.     /**
  125.     * HTTP version
  126.     * 
  127.     * @var      string 
  128.     * @access   private
  129.     */
  130.     var $_httpVersion '1.0';
  131.  
  132.     /**
  133.     * Constructor
  134.     *
  135.     * Sets HTTP version.
  136.     * 
  137.     * @access   public
  138.     * @return   object  HTTP_Header 
  139.     */
  140.     function HTTP_Header()
  141.     {
  142.         if (isset($_SERVER['SERVER_PROTOCOL'])) {
  143.             $this->setHttpVersion(substr($_SERVER['SERVER_PROTOCOL']-3));
  144.         }
  145.     }
  146.     
  147.     /**
  148.     * Set HTTP version
  149.     *
  150.     * @access   public
  151.     * @return   bool    Returns true on success or false if version doesn't
  152.     *                    match 1.0 or 1.1 (note: 1 will result in 1.0)
  153.     * @param    mixed   $version HTTP version, either 1.0 or 1.1
  154.     */
  155.     function setHttpVersion($version)
  156.     {
  157.         $version round((float) $version1);
  158.         if ($version < 1.0 || $version > 1.1{
  159.             return false;
  160.         }
  161.         $this->_httpVersion sprintf('%0.1f'$version);
  162.         return true;
  163.     }
  164.     
  165.     /**
  166.     * Get HTTP version
  167.     *
  168.     * @access   public
  169.     * @return   string 
  170.     */
  171.     function getHttpVersion()
  172.     {
  173.         return $this->_httpVersion;
  174.     }
  175.     
  176.     /**
  177.     * Set Header
  178.     * 
  179.     * The default value for the Last-Modified header will be current
  180.     * date and atime if $value is omitted.
  181.     * 
  182.     * @access   public
  183.     * @return   bool    Returns true on success or false if $key was empty or
  184.     *                    $value was not of an scalar type.
  185.     * @param    string  $key The name of the header
  186.     * @param    string  $value The value of the header
  187.     */
  188.     function setHeader($key$value = null)
  189.     {
  190.         if (empty($key|| (isset($value&& !is_scalar($value))) {
  191.             return false;
  192.         }
  193.         
  194.         $key = strToLower($key);
  195.         
  196.         if ($key == 'last-modified'{
  197.             if (!isset($value)) {
  198.                 $value = HTTP::Date(time());
  199.             elseif (is_numeric($value)) {
  200.                 $value = HTTP::Date($value);
  201.             }
  202.         }
  203.         
  204.         $this->_headers[$key$value;
  205.         return true;
  206.     }
  207.  
  208.     /**
  209.     * Get Header
  210.     * 
  211.     * If $key is omitted, all stored headers will be returned.
  212.     * 
  213.     * @access   public
  214.     * @return   mixed   Returns string value of the requested header,
  215.     *                    array values of all headers or false if header $key
  216.     *                    is not set.
  217.     * @param    string  $key    The name of the header to fetch.
  218.     */
  219.     function getHeader($key = null)
  220.     {
  221.         if (!isset($key)) {
  222.             return $this->_headers;
  223.         }
  224.         
  225.         $key = strToLower($key);
  226.         
  227.         if (!isset($this->_headers[$key])) {
  228.             return false;
  229.         }
  230.         
  231.         return $this->_headers[$key];
  232.     }
  233.  
  234.     /**
  235.     * Send Headers
  236.     * 
  237.     * Send out the header that you set via setHeader().
  238.     * 
  239.     * @access   public
  240.     * @return   bool    Returns true on success or false if headers are already
  241.     *                    sent.
  242.     * @param    array   $keys Headers to (not) send, see $include.
  243.     * @param    array   $include If true only $keys matching headers will be
  244.     *                    sent, if false only header not matching $keys will be
  245.     *                    sent.
  246.     */
  247.     function sendHeaders($keys = array()$include = true)
  248.     {
  249.         if (headers_sent()) {
  250.             return false;
  251.         }
  252.         
  253.         if (count($keys)) {
  254.             array_change_key_case($keysCASE_LOWER);
  255.             foreach ($this->_headers as $key => $value{
  256.                 if ($include in_array($key$keys!in_array($key$keys)) {
  257.                     header($key .': '$value);
  258.                 }
  259.             }
  260.         else {
  261.             foreach ($this->_headers as $header => $value{
  262.                 header($header .': '$value);
  263.             }
  264.         }
  265.         return true;
  266.     }
  267.  
  268.     /**
  269.     * Send Satus Code
  270.     * 
  271.     * Send out the given HTTP-Status code. Use this for example when you
  272.     * want to tell the client this page is cached, then you would call
  273.     * sendStatusCode(304).
  274.     *
  275.     * @see HTTP_Header_Cache::exitIfCached()
  276.     * 
  277.     * @access   public
  278.     * @return   bool    Returns true on success or false if headers are already
  279.     *                    sent.
  280.     * @param    int     $code The status code to send, i.e. 404, 304, 200, etc.
  281.     */
  282.     function sendStatusCode($code)
  283.     {
  284.         if (headers_sent()) {
  285.             return false;
  286.         }
  287.         
  288.         if ($code == (int) $code && defined('HTTP_HEADER_STATUS_'$code)) {
  289.             $code constant('HTTP_HEADER_STATUS_'$code);
  290.         }
  291.         
  292.         header('HTTP/'$this->_httpVersion .' '$code);
  293.         return true;
  294.     }
  295.  
  296.     /**
  297.     * Date to Timestamp
  298.     * 
  299.     * Converts dates like
  300.     *       Mon, 31 Mar 2003 15:26:34 GMT
  301.     *       Tue, 15 Nov 1994 12:45:26 GMT
  302.     * into a timestamp, strtotime() didn't do it in older versions.
  303.     *
  304.     * @deprecated
  305.     * @access   public
  306.     * @return   mixed   Returns int unix timestamp or false if the date doesn't
  307.     *                    seem to be a valid GMT date.
  308.     * @param    string  $d The GMT date.
  309.     */
  310.     function dateToTimestamp($d)
  311.     {
  312.         static $monts = array(
  313.             null => 0'Jan' => 1'Feb' => 2'Mar' => 3'Apr' => 4,
  314.             'May' => 5'Jun' => 6'Jul' => 7'Aug' => 8'Sep' => 9,
  315.             'Oct' => 10'Nov' => 11'Dec' => 12
  316.         );
  317.         
  318.         if (-1 < $timestamp = strToTime($d)) {
  319.             return $timestamp;
  320.         }
  321.         
  322.         if (!preg_match('~[^,]*,\s(\d+)\s(\w+)\s(\d+)\s(\d+):(\d+):(\d+).*~'$d$m)) {
  323.             return false;
  324.         }
  325.         
  326.         // [0] => Mon, 31 Mar 2003 15:42:55 GMT
  327.         // [1] => 31 [2] => Mar [3] => 2003 [4] => 15 [5] => 42 [6] => 55
  328.         return mktime($d[4]$d[5]$d[6]$months[$d[2]]$d[1]$d[3]);
  329.     }
  330.  
  331.     /**
  332.     * Redirect
  333.     * 
  334.     * This function redirects the client. This is done by issuing a Location
  335.     * header and exiting.  Additionally to HTTP::redirect() you can also add
  336.     * parameters to the url.
  337.     * 
  338.     * If you dont need parameters to be added, simply use HTTP::redirect()
  339.     * otherwise use HTTP_Header::redirect().
  340.     *
  341.     * @see      HTTP::redirect()
  342.     * @author   Wolfram Kriesing <wk@visionp.de>
  343.     * @access   public
  344.     * @return   void 
  345.     * @param    string  $url The URL to redirect to, if none is given it
  346.     *                    redirects to the current page.
  347.     * @param    mixed   $param Possible values:
  348.     *                        o null (default) - only the session-id will be
  349.     *                          added, but only when trans_sid is enabled.
  350.     *                        o false - no parameters to add
  351.     *                        o true - add the session-id
  352.     *                        o array - of parameter names, if the key is a string
  353.     *                          it's assumed to be name => value, otherwise the
  354.     *                          value is retreived using $GLOBALS['paraName'].
  355.     */
  356.     function redirect($url = null$param = null)
  357.     {
  358.         if (!isset($url)) {
  359.             $url $_SERVER['PHP_SELF'];
  360.         }
  361.         
  362.         $qs = array();
  363.  
  364.         if ($param || (!isset($param&& ini_get('session.use_trans_sid'))) {
  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.         $purl parse_url($url);
  381.         $url .= (isset($purl['query']'&' '?'implode('&'$qs);
  382.         
  383.         parent::redirect($url);
  384.     }
  385.  
  386.     /**#@+
  387.      * @author Davey Shafik <davey@php.net>
  388.      * @param int $http_code HTTP Code to check
  389.      */
  390.  
  391.     /**
  392.      * Return HTTP Status Code Type
  393.      *
  394.      * @return int|false
  395.      */
  396.     function getStatusType($http_code
  397.     {
  398.         if(is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code|| defined($http_code)) {
  399.             $type substr($http_code,0,1);
  400.             switch ($type{
  401.                 case HTTP_HEADER_STATUS_INFORMATIONAL:
  402.                 case HTTP_HEADER_STATUS_SUCCESSFUL:
  403.                 case HTTP_HEADER_STATUS_REDIRECT:
  404.                 case HTTP_HEADER_STATUS_CLIENT_ERROR:
  405.                 case HTTP_HEADER_STATUS_SERVER_ERROR:
  406.                     return $type;
  407.                     break;
  408.                 default:
  409.                     return false;
  410.                     break;
  411.             }
  412.         else {
  413.             return false;
  414.         }
  415.     }
  416.  
  417.     /**
  418.      * Return Status Code Message
  419.      *
  420.      * @return string|false
  421.      */
  422.     function getStatusText($http_code
  423.     {
  424.         if ($this->getStatusType($http_code)) {
  425.             if (is_int($http_code&& defined('HTTP_HEADER_STATUS_' .$http_code)) {
  426.                 return substr(constant('HTTP_HEADER_STATUS_' .$http_code),4);
  427.             else {
  428.                 return substr($http_code,4);
  429.             }
  430.         else {
  431.             return false;
  432.         }
  433.     }
  434.  
  435.     /**
  436.      * Checks if HTTP Status code is Information (1xx)
  437.      *
  438.      * @return boolean 
  439.      */
  440.     function isInformational($http_code
  441.     {
  442.         if ($status_type $this->getStatusType($http_code)) {
  443.             return $status_type{0== HTTP_HEADER_STATUS_INFORMATIONAL;
  444.         else {
  445.             return false;
  446.         }
  447.     }
  448.  
  449.     /**
  450.      * Checks if HTTP Status code is Successful (2xx)
  451.      *
  452.      * @return boolean 
  453.      */
  454.     function isSuccessful($http_code
  455.     {
  456.         if ($status_type $this->getStatusType($http_code)) {
  457.             return $status_type{0== HTTP_HEADER_STATUS_SUCCESSFUL;
  458.         else {
  459.             return false;
  460.         }
  461.     }
  462.  
  463.     /**
  464.      * Checks if HTTP Status code is a Redirect (3xx)
  465.      *
  466.      * @return boolean 
  467.      */
  468.     function isRedirect($http_code
  469.     {
  470.         if ($status_type $this->getStatusType($http_code)) {
  471.             return $status_type{0== HTTP_HEADER_STATUS_REDIRECT;
  472.         else {
  473.             return false;
  474.         }
  475.     }
  476.  
  477.     /**
  478.      * Checks if HTTP Status code is a Client Error (4xx)
  479.      *
  480.      * @return boolean 
  481.      */
  482.     function isClientError($http_code
  483.     {
  484.         if ($status_type $this->getStatusType($http_code)) {
  485.             return $status_type{0== HTTP_HEADER_STATUS_CLIENT_ERROR;
  486.         else {
  487.             return false;
  488.         }
  489.     }
  490.  
  491.     /**
  492.      * Checks if HTTP Status code is Server Error (5xx)
  493.      *
  494.      * @return boolean 
  495.      */
  496.     function isServerError($http_code
  497.     {
  498.         if ($status_type $this->getStatusType($http_code)) {
  499.             return $status_type{0== HTTP_HEADER_STATUS_SERVER_ERROR;
  500.         else {
  501.             return false;
  502.         }
  503.     }
  504.  
  505.     /**
  506.      * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
  507.      *
  508.      * @return boolean 
  509.      */
  510.     function isError($http_code
  511.     {
  512.         if ($status_type $this->getStatusType($http_code)) {
  513.             return (($status_type == HTTP_HEADER_STATUS_CLIENT_ERROR|| ($status_type == HTTP_HEADER_STATUS_SERVER_ERROR)) ? true : false;
  514.         else {
  515.             return false;
  516.         }
  517.     }
  518.     /**#@-*/
  519. }
  520. ?>

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