diff 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
line wrap: on
line diff
--- a/CacheTimeCheck.inc	Thu Oct 04 22:07:19 2012 +0200
+++ b/CacheTimeCheck.inc	Fri Oct 05 00:21:27 2012 +0200
@@ -3,34 +3,52 @@
 {
   private $newest;
   private $files = array();
+  private static $myInstance = 0;
 
-  function __construct($minTime = 0, $filename = False)
+  private function __construct($filename = False)
   {
     if ($filename)
-      array_push($this->files, $filename);
-    $this->newest = $minTime;
+      $this->cache_time($filename);
     $this->cache_time(__FILE__);
   }
 
+  function instance($filename = False)
+  {
+    if (! self::$myInstance)
+      self::$myInstance = new self($filename);
+    elseif ($filename)
+      self::$myInstance->cache_time($filename);
+    return self::$myInstance;
+  }
+
   function CheckHttpModified()
   {
-    if (DEBUG)
+    if (DEBUG_LEVEL >= VERBOSITY_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(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 ($if_modified_since == $gmdate_mod) {
-      header("HTTP/1.0 304 Not Modified");
-      exit;
+      if (strtotime($if_modified_since) >= $this->newest) {
+	header("HTTP/1.0 304 Not Modified");
+	exit;
+      }
     }
+
     header("Last-Modified: $gmdate_mod");
   }
 
   function cache_time($path)
   {
+    if (!file_exists($path)) {
+      if (DEBUG)
+	print "${path} does not exist";
+      errorPage("Resource not available");
+    }
+
     array_push($this->files, $path);
     $mtime = filemtime($path);
     if ($mtime > $this->newest) {