Mercurial > SimpleWebPresenter
view CacheTimeCheck.inc @ 52:3898353ed1d8
Include fixes.
sitemap don't print before everything has been processed.
Make a class of accept-language.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 11 Oct 2012 00:48:46 +0200 |
| parents | 2d0cda52f43f |
| children | 164268b4e0d9 |
line wrap: on
line source
<?php $baseDir = dirname(__FILE__); include_once 'logger.inc'; include_once 'common-functions.inc'; $cache = CacheTimeCheck::instance(__FILE__); $cache->cache_time("${baseDir}/logger.inc"); $cache->cache_time("${baseDir}/common-functions.inc"); /** * 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 { private $newest; private $files = array(); private static $myInstance = 0; private function __construct($filename = False) { if ($filename) $this->cache_time($filename); $this->cache_time(__FILE__); } /** * List a set of files which contributes to this pages cacheset. * * @param $humanReadable If the timestamp should be humand readable. * * @return an associative array of file, and timestamp */ function cacheSet($humanReadable = False) { $retVal = array(); foreach($this->files as $file) { $mtime = filemtime($file); if ($humanReadable) $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; $retVal[$file] = $mtime; } return $retVal; } /** * Generates a singleton instance of this CacheTimeCheck * * @param $filename an optional file to include in the cacheset * * @return a CacheTimeCheck object */ function instance($filename = False) { if (! self::$myInstance) self::$myInstance = new self($filename); elseif ($filename) self::$myInstance->cache_time($filename); return self::$myInstance; } /** * 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() { if (DEBUG_LEVEL >= VERBOSITY_DEBUG) var_dump($_SERVER); $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; 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) >= $this->newest) { header("HTTP/1.0 304 Not Modified"); exit; } } header("Last-Modified: $gmdate_mod"); } /** * Include a file in the cacheset * * @param $path the path of the file */ function cache_time($path) { if (!file_exists($path)) { warn("${path} does not exist"); errorPage("Resource is not available"); } array_push($this->files, $path); $mtime = filemtime($path); if ($mtime > $this->newest) { $this->newest = $mtime; } } /** * Convenience function to include a file, and add it to the cacheset. * * @param $path path of the file * @param $basedir a directory to prepend to the path */ function includeOnce($path, $basedir = false) { if ($basedir) $path = $basedir . "/" . $path; $this->cache_time($path); include_once($path); } /** * 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); } } ?>
