comparison CacheTimeCheck.inc @ 39:bd82b719a0de

Make CacheTimeCheck a singleton. Robustify if_modified_since check. Quiet warnings. Set debug levels. Fix basepath references.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Fri, 05 Oct 2012 00:21:27 +0200
parents 42533600214b
children fbbb82ced6de
comparison
equal deleted inserted replaced
38:42533600214b 39:bd82b719a0de
1 <?php 1 <?php
2 class CacheTimeCheck 2 class CacheTimeCheck
3 { 3 {
4 private $newest; 4 private $newest;
5 private $files = array(); 5 private $files = array();
6 private static $myInstance = 0;
6 7
7 function __construct($minTime = 0, $filename = False) 8 private function __construct($filename = False)
8 { 9 {
9 if ($filename) 10 if ($filename)
10 array_push($this->files, $filename); 11 $this->cache_time($filename);
11 $this->newest = $minTime;
12 $this->cache_time(__FILE__); 12 $this->cache_time(__FILE__);
13 }
14
15 function instance($filename = False)
16 {
17 if (! self::$myInstance)
18 self::$myInstance = new self($filename);
19 elseif ($filename)
20 self::$myInstance->cache_time($filename);
21 return self::$myInstance;
13 } 22 }
14 23
15 function CheckHttpModified() 24 function CheckHttpModified()
16 { 25 {
17 if (DEBUG) 26 if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
18 var_dump($_SERVER); 27 var_dump($_SERVER);
19 28
20 $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; 29 $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT';
21 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); 30
31 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
32 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
33 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
22 34
23 $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; 35 if (strtotime($if_modified_since) >= $this->newest) {
36 header("HTTP/1.0 304 Not Modified");
37 exit;
38 }
39 }
24 40
25 if ($if_modified_since == $gmdate_mod) {
26 header("HTTP/1.0 304 Not Modified");
27 exit;
28 }
29 header("Last-Modified: $gmdate_mod"); 41 header("Last-Modified: $gmdate_mod");
30 } 42 }
31 43
32 function cache_time($path) 44 function cache_time($path)
33 { 45 {
46 if (!file_exists($path)) {
47 if (DEBUG)
48 print "${path} does not exist";
49 errorPage("Resource not available");
50 }
51
34 array_push($this->files, $path); 52 array_push($this->files, $path);
35 $mtime = filemtime($path); 53 $mtime = filemtime($path);
36 if ($mtime > $this->newest) { 54 if ($mtime > $this->newest) {
37 $this->newest = $mtime; 55 $this->newest = $mtime;
38 } 56 }