| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 //inspired by http://alexandre.alapetite.net/doc-alex/php-http-304/ 10 11 define('LIMB_HTTP_CACHE_TYPE_PRIVATE', 0); 12 define('LIMB_HTTP_CACHE_TYPE_PUBLIC', 1); 13 14 /** 15 * class lmbHttpCache. 16 * 17 * @package net 18 * @version $Id: lmbHttpCache.class.php 5945 2007-06-06 08:31:43Z pachanga $ 19 */ 20 class lmbHttpCache 21 { 22 protected $etag; 23 protected $last_modified_time; 24 protected $cache_time; 25 protected $cache_type; 26 27 function __construct() 28 { 29 $this->reset(); 30 } 31 32 function reset() 33 { 34 $this->last_modified_time = time(); 35 $this->etag = null; 36 $this->cache_time = 0; 37 $this->cache_type = LIMB_HTTP_CACHE_TYPE_PRIVATE; 38 } 39 40 function checkAndWrite($response) 41 { 42 if($this->is412()) 43 { 44 $this->_write412Response($response); 45 return true; 46 } 47 elseif($this->is304()) 48 { 49 $this->_write304Response($response); 50 return true; 51 } 52 else 53 { 54 $this->_writeCachingResponse($response); 55 56 return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD'; //rfc2616-sec9.html#sec9.4 57 } 58 } 59 60 function is412() 61 { 62 if (isset($_SERVER['HTTP_IF_MATCH'])) //rfc2616-sec14.html#sec14.24 63 { 64 $etag_client = stripslashes($_SERVER['HTTP_IF_MATCH']); 65 return (($etag_client != '*') && (strpos($etag_client, $this->getEtag()) === false)); 66 } 67 68 if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE'])) //http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.28 69 { 70 return (strcasecmp($_SERVER['HTTP_IF_UNMODIFIED_SINCE'], $this->formatLastModifiedTime()) != 0); 71 } 72 73 return false; 74 } 75 76 function is304() 77 { 78 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) //rfc2616-sec14.html#sec14.25 //rfc1945.txt 79 { 80 return ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $this->formatLastModifiedTime()); 81 } 82 83 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) //rfc2616-sec14.html#sec14.26 84 { 85 $etag_client = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); 86 return (($etag_client == $this->getEtag()) || ($etag_client == '*')); 87 } 88 89 return false; 90 } 91 92 protected function _write412Response($response) 93 { 94 $response->header('HTTP/1.1 412 Precondition Failed'); 95 $response->header('Cache-Control: protected, max-age=0, must-revalidate'); 96 $response->header('Content-Type: text/plain'); 97 98 $response->write("HTTP/1.1 Error 412 Precondition Failed: Precondition request failed positive evaluation\n"); 99 } 100 101 protected function _write304Response($response) 102 { 103 $response->header('HTTP/1.0 304 Not Modified'); 104 $response->header('Etag: ' . $this->getEtag()); 105 $response->header('Pragma: '); 106 $response->header('Cache-Control: '); 107 $response->header('Last-Modified: '); 108 $response->header('Expires: '); 109 } 110 111 protected function _writeCachingResponse($response) 112 { 113 $response->header('Cache-Control: ' . $this->_getCacheControl()); //rfc2616-sec14.html#sec14.9 114 $response->header('Last-Modified: ' . $this->formatLastModifiedTime()); 115 $response->header('Etag: ' . $this->getEtag()); 116 $response->header('Pragma: '); 117 $response->header('Expires: '); 118 } 119 120 protected function _getCacheControl() 121 { 122 if ($this->cache_time == 0) 123 $cache = 'protected, must-revalidate, '; 124 elseif ($this->cache_type == LIMB_HTTP_CACHE_TYPE_PRIVATE) 125 $cache = 'protected, '; 126 elseif ($this->cache_type == LIMB_HTTP_CACHE_TYPE_PUBLIC) 127 $cache = 'public, '; 128 else 129 $cache = ''; 130 $cache .= 'max-age=' . floor($this->cache_time); 131 return $cache; 132 } 133 134 function formatLastModifiedTime() 135 { 136 return $this->_formatGmtTime($this->last_modified_time); 137 } 138 139 protected function _formatGmtTime($time) 140 { 141 return gmdate('D, d M Y H:i:s \G\M\T', $time); 142 } 143 144 function setLastModifiedTime($last_modified_time) 145 { 146 $this->last_modified_time = $last_modified_time; 147 } 148 149 function getLastModifiedTime() 150 { 151 return $this->last_modified_time; 152 } 153 154 function setEtag($etag) 155 { 156 $this->etag = $etag; 157 } 158 159 function getEtag() 160 { 161 if($this->etag) 162 return $this->etag; 163 164 //rfc2616-sec14.html#sec14.19 //='"0123456789abcdef0123456789abcdef"' 165 if (isset($_SERVER['QUERY_STRING'])) 166 $query = '?' . $_SERVER['QUERY_STRING']; 167 else 168 $query = ''; 169 170 $this->etag = '"' . md5($this->_getScriptName() . $query . '#' . $this->last_modified_time ) . '"'; 171 172 return $this->etag; 173 } 174 175 protected function _getScriptName() 176 { 177 if (isset($_SERVER['SCRIPT_FILENAME'])) 178 return $_SERVER['SCRIPT_FILENAME']; 179 elseif (isset($_SERVER['PATH_TRANSLATED'])) 180 return $_SERVER['PATH_TRANSLATED']; 181 else 182 return ''; 183 } 184 185 function setCacheTime($cache_time) 186 { 187 $this->cache_time = $cache_time; 188 } 189 190 function getCacheTime() 191 { 192 return $this->cache_time; 193 } 194 195 function setCacheType($cache_type) 196 { 197 $this->cache_type = $cache_type; 198 } 199 200 function getCacheType() 201 { 202 return $this->cache_type; 203 } 204 } 205 206 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Oct 7 05:02:03 2008 | Cross-referenced by PHPXref 0.7 |