Mercurial > SimpleWebPresenter
comparison CacheTimeCheck.inc.php @ 134:b6b4a58c7625
Using .inc.php rather than just .inc for include files.
| author | Tom Fredrik Blenning <bfg@bfgconsult.no> |
|---|---|
| date | Sun, 22 Jan 2023 19:22:00 +0100 |
| parents | CacheTimeCheck.inc@df158368051e |
| children |
comparison
equal
deleted
inserted
replaced
| 133:00255ca89459 | 134:b6b4a58c7625 |
|---|---|
| 1 <?php | |
| 2 $baseDir = dirname(__FILE__); | |
| 3 | |
| 4 include_once 'common-functions.inc.php'; | |
| 5 include_once 'FileCacheSet.inc.php'; | |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | |
| 7 $cache->cache_time("${baseDir}/FileCacheSet.inc.php"); | |
| 8 | |
| 9 function toGMTime($time) { | |
| 10 return gmdate('D, d M Y H:i:s', $time) . ' GMT'; | |
| 11 } | |
| 12 | |
| 13 /** | |
| 14 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is | |
| 15 * newer, set a Last-Modified header, otherwise abort with an 304 | |
| 16 * status code | |
| 17 */ | |
| 18 function CheckHttpModified($cache) | |
| 19 { | |
| 20 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) | |
| 21 var_dump($_SERVER); | |
| 22 | |
| 23 $gmdate_mod = toGMTime($cache->getNewest()); | |
| 24 | |
| 25 if (array_key_exists('REDIRECT_URL', $_SERVER) && $_SERVER['REDIRECT_URL'] == '/sitemap.xml') { | |
| 26 //print_r($gmdate_mod); | |
| 27 } | |
| 28 | |
| 29 foreach($cache->cacheControl() as $header => $value) { | |
| 30 header ("${header}: ${value}"); | |
| 31 } | |
| 32 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| 33 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; | |
| 34 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); | |
| 35 | |
| 36 if (strtotime($if_modified_since) >= $cache->getNewest()) { | |
| 37 header("HTTP/1.0 304 Not Modified"); | |
| 38 return false; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 foreach($cache->cacheControl() as $header => $value) { | |
| 43 header ("${header}: ${value}"); | |
| 44 } | |
| 45 header("Last-Modified: $gmdate_mod"); | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 | |
| 50 | |
| 51 /** | |
| 52 * CacheTimeCheck provides a set of functions to enable generating a | |
| 53 * correct time for the latest update for a given file. | |
| 54 * | |
| 55 * @author Tom Fredrik Blenning Klaussen | |
| 56 */ | |
| 57 class CacheTimeCheck extends FileCacheSet | |
| 58 { | |
| 59 function __construct($filename = False) | |
| 60 { | |
| 61 parent::__construct(); | |
| 62 if ($filename) | |
| 63 $this->cache_time($filename); | |
| 64 $this->cache_time(__FILE__); | |
| 65 $this->max_age=0; | |
| 66 } | |
| 67 | |
| 68 public function addParent($cache) | |
| 69 { | |
| 70 parent::addParent($cache); | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * Convenience function to load a file, and add it to the cacheset | |
| 75 * | |
| 76 * @param $path path of the file | |
| 77 * @return the contents of the file | |
| 78 */ | |
| 79 function loadFile($path) | |
| 80 { | |
| 81 $this->cache_time($path); | |
| 82 return loadFile($path); | |
| 83 } | |
| 84 | |
| 85 } | |
| 86 ?> |
