# HG changeset patch # User Tom Fredrik "BFG" Klaussen # Date 1349381239 -7200 # Node ID 42533600214b6a57826ae20071c0d29a8702e13a # Parent da1726860524309968adfee33ac132b8232106ac Rename cache_check.inc to CacheTimeCheck.inc. Proper caching in flag.php. diff -r da1726860524 -r 42533600214b CacheTimeCheck.inc --- /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 @@ +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 diff -r da1726860524 -r 42533600214b cache_check.inc --- 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 @@ -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 diff -r da1726860524 -r 42533600214b flag.php --- 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 @@ = 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 '

Resource not found

'; +$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 diff -r da1726860524 -r 42533600214b index.php --- 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'); diff -r da1726860524 -r 42533600214b inputParser.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}'");