Mercurial > SimpleWebPresenter
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CacheTimeCheck.inc.php Sun Jan 22 19:22:00 2023 +0100 @@ -0,0 +1,86 @@ +<?php +$baseDir = dirname(__FILE__); + +include_once 'common-functions.inc.php'; +include_once 'FileCacheSet.inc.php'; +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->cache_time("${baseDir}/FileCacheSet.inc.php"); + +function toGMTime($time) { + return gmdate('D, d M Y H:i:s', $time) . ' GMT'; +} + +/** + * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is + * newer, set a Last-Modified header, otherwise abort with an 304 + * status code + */ +function CheckHttpModified($cache) +{ + if (DEBUG_LEVEL >= VERBOSITY_DEBUG) + var_dump($_SERVER); + + $gmdate_mod = toGMTime($cache->getNewest()); + + if (array_key_exists('REDIRECT_URL', $_SERVER) && $_SERVER['REDIRECT_URL'] == '/sitemap.xml') { + //print_r($gmdate_mod); + } + + foreach($cache->cacheControl() as $header => $value) { + header ("${header}: ${value}"); + } + if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { + $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; + $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); + + if (strtotime($if_modified_since) >= $cache->getNewest()) { + header("HTTP/1.0 304 Not Modified"); + return false; + } + } + + foreach($cache->cacheControl() as $header => $value) { + header ("${header}: ${value}"); + } + header("Last-Modified: $gmdate_mod"); + return true; +} + + + +/** + * CacheTimeCheck provides a set of functions to enable generating a + * correct time for the latest update for a given file. + * + * @author Tom Fredrik Blenning Klaussen + */ +class CacheTimeCheck extends FileCacheSet +{ + function __construct($filename = False) + { + parent::__construct(); + if ($filename) + $this->cache_time($filename); + $this->cache_time(__FILE__); + $this->max_age=0; + } + + public function addParent($cache) + { + parent::addParent($cache); + } + + /** + * Convenience function to load a file, and add it to the cacheset + * + * @param $path path of the file + * @return the contents of the file + */ + function loadFile($path) + { + $this->cache_time($path); + return loadFile($path); + } + +} +?>
