Mercurial > SimpleWebPresenter
changeset 38:42533600214b
Rename cache_check.inc to CacheTimeCheck.inc.
Proper caching in flag.php.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 04 Oct 2012 22:07:19 +0200 |
| parents | da1726860524 |
| children | bd82b719a0de |
| files | CacheTimeCheck.inc cache_check.inc flag.php index.php inputParser.inc |
| diffstat | 5 files changed, 72 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CacheTimeCheck.inc Thu Oct 04 22:07:19 2012 +0200 @@ -0,0 +1,53 @@ +<?php +class CacheTimeCheck +{ + private $newest; + private $files = array(); + + function __construct($minTime = 0, $filename = False) + { + if ($filename) + array_push($this->files, $filename); + $this->newest = $minTime; + $this->cache_time(__FILE__); + } + + function CheckHttpModified() + { + if (DEBUG) + var_dump($_SERVER); + + $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; + $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); + + $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; + + if ($if_modified_since == $gmdate_mod) { + header("HTTP/1.0 304 Not Modified"); + exit; + } + header("Last-Modified: $gmdate_mod"); + } + + function cache_time($path) + { + array_push($this->files, $path); + $mtime = filemtime($path); + if ($mtime > $this->newest) { + $this->newest = $mtime; + } + } + + function includeOnce($path) + { + $this->cache_time($path); + include_once($path); + } + + function loadFile($path) + { + $this->cache_time($path); + return loadFile($path); + } +} +?> \ No newline at end of file
--- a/cache_check.inc Thu Oct 04 21:30:57 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -<?php -class CacheTimeCheck -{ - private $newest; - private $files = array(); - - function __construct($minTime = 0, $filename = False) - { - if ($filename) - array_push($this->files, $filename); - $this->newest = $minTime; - $this->cache_time(__FILE__); - } - - function CheckHttpModified() - { - if (DEBUG) - var_dump($_SERVER); - - $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; - $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); - - $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; - - if ($if_modified_since == $gmdate_mod) { - header("HTTP/1.0 304 Not Modified"); - exit; - } - header("Last-Modified: $gmdate_mod"); - } - - function cache_time($path) - { - array_push($this->files, $path); - $mtime = filemtime($path); - if ($mtime > $this->newest) { - $this->newest = $mtime; - } - } - - function includeOnce($path) - { - $this->cache_time($path); - include_once($path); - } -} -?> \ No newline at end of file
--- a/flag.php Thu Oct 04 21:30:57 2012 +0200 +++ b/flag.php Thu Oct 04 22:07:19 2012 +0200 @@ -1,62 +1,40 @@ <?php define(DEBUG,0); -include 'cache_check.inc'; -include 'accept-language.inc'; - -function loadFile($sFilename, $sCharset = 'UTF-8') -{ - if (floatval(phpversion()) >= 4.3) { - if (!file_exists($sFilename)) return -3; - $sData = file_get_contents($sFilename); - } else { - if (!file_exists($sFilename)) return -3; - $rHandle = fopen($sFilename, 'r'); - if (!$rHandle) return -2; +include_once 'CacheTimeCheck.inc'; - $sData = ''; - while(!feof($rHandle)) - $sData .= fread($rHandle, filesize($sFilename)); - fclose($rHandle); - } - if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) { - if ($sEncoding != 1) { - $sData = mb_convert_encoding($sData, $sCharset, $sEncoding); - } - } - return $sData; -} - +$cache = new CacheTimeCheck(filemtime(__FILE__)); +$cache->includeOnce('accept-language.inc'); +$cache->includeOnce('common-functions.inc'); $active = $_GET['active']; $lang = $_GET['lang']; -if(!$name) { - $name="home"; -} if(!$lang) { - $lang="no"; - $langs=acceptedLanguages(); + $lang = "no"; + $langs = acceptedLanguages(); foreach ($langs as $l => $val) { if (file_exists($l)) { - $lang=$l; + $lang = $l; break; } } } -$name="../img/flag-${lang}"; +$name = "../img/flag-${lang}"; if ($active) - $name.="-active"; -$name.=".png"; + $name .= "-active"; +$name .= ".png"; -$flag=loadFile("${name}"); +$cache->cache_time($name); +$cache->CheckHttpModified(); -if (floatval($flag)<0) { - header('HTTP/1.0 404 Not Found'); - print '<div id="page"><h1>Resource not found</h1></div>'; +$flag = loadFile($name); + +if (floatval($flag) < 0) { + errorPage('Resource not found', 404); } else { header("Content-Type: image/png"); print $flag; -} +} \ No newline at end of file
--- a/index.php Thu Oct 04 21:30:57 2012 +0200 +++ b/index.php Thu Oct 04 22:07:19 2012 +0200 @@ -15,7 +15,7 @@ $cacheable = true; -include_once 'php/cache_check.inc'; +include_once 'php/CacheTimeCheck.inc'; $cache = new CacheTimeCheck(filemtime(__FILE__)); $cache->includeOnce('php/Options.inc');
--- a/inputParser.inc Thu Oct 04 21:30:57 2012 +0200 +++ b/inputParser.inc Thu Oct 04 22:07:19 2012 +0200 @@ -142,8 +142,7 @@ else { $src = $file->getAttribute("src"); $fname = "${lang}/${src}"; - $options->getCache()->cache_time($fname); - $file_content = loadFile($fname); + $file_content = $options->getCache()->loadFile($fname); } if(floatval($file_content)<0) { errorPage("Resource not found '${lang}/${src}'");
