HeaderUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00017     final class HeaderUtils extends StaticFactory
00018     {
00019         private static $headerSent      = false;
00020         private static $redirectSent    = false;
00021         private static $cacheLifeTime   = 3600;
00022         
00023         public static function redirectRaw($url)
00024         {
00025             header("Location: {$url}");
00026 
00027             self::$headerSent = true;
00028             self::$redirectSent = true;
00029         }
00030         
00031         public static function redirectBack()
00032         {
00033             if (isset($_SERVER['HTTP_REFERER'])) {
00034                 header("Location: {$_SERVER['HTTP_REFERER']}");
00035                 self::$headerSent = true;
00036                 self::$redirectSent = true;
00037                 return $_SERVER['HTTP_REFERER'];
00038             } else
00039                 return false;
00040         }
00041         
00042         public static function getParsedURI(/* ... */)
00043         {
00044             if ($num = func_num_args()) {
00045                 $out = self::getURI();
00046                 $uri = null;
00047                 $arr = func_get_args();
00048                 
00049                 for ($i = 0; $i < $num; ++$i)
00050                     unset($out[$arr[$i]]);
00051                 
00052                 foreach ($out as $key => $val) {
00053                     if (is_array($val)) {
00054                         foreach ($val as $k => $v)
00055                             $uri .= "&{$key}[{$k}]={$v}";
00056                     } else
00057                         $uri .= "&{$key}={$val}";
00058                 }
00059 
00060                 return $uri;
00061             }
00062 
00063             return null;
00064         }
00065         
00066         public static function sendCachedHeader()
00067         {
00068             header('Cache-control: private, max-age=3600');
00069             
00070             header(
00071                 'Expires: '
00072                 .date('D, d M Y H:i:s', date('U') + self::$cacheLifeTime)
00073                 .' GMT'
00074             );
00075             
00076             self::$headerSent = true;
00077         }
00078 
00079         public static function sendNotCachedHeader()
00080         {
00081             header('Cache-control: no-cache');
00082             header(
00083                 'Expires: '
00084                 .date('D, d M Y H:i:s', date('U') - self::$cacheLifeTime)
00085                 .' GMT'
00086             );
00087             
00088             self::$headerSent = true;
00089         }
00090         
00091         public static function sendContentLength($length)
00092         {
00093             Assert::isInteger($length);
00094             
00095             header(
00096                 "Content-Length: {$length}"
00097             );
00098             
00099             self::$headerSent = true;
00100         }
00101         
00102         public static function sendHttpStatus(HttpStatus $status)
00103         {
00104             header($status->toString());
00105             
00106             self::$headerSent = true;
00107         }
00108 
00109         public static function isHeaderSent()
00110         {
00111             return self::$headerSent;
00112         }
00113         
00114         public static function forceHeaderSent()
00115         {
00116             self::$headerSent = true;
00117         }
00118         
00119         public static function isRedirectSent()
00120         {
00121             return self::$redirectSent;
00122         }
00123         
00124         public static function setCacheLifeTime($cacheLifeTime)
00125         {
00126             self::$cacheLifeTime = $cacheLifeTime;
00127         }
00128         
00129         public static function getCacheLifeTime()
00130         {
00131             return self::$cacheLifeTime;
00132         }
00133 
00134         private static function getURI()
00135         {
00136             $out = null;
00137             
00138             parse_str($_SERVER['QUERY_STRING'], $out);
00139             
00140             return $out;
00141         }
00142     }
00143 ?>