view 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 source

<?php
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__);
  }

  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_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");
  }

  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) {
      $this->newest = $mtime;
    }
  }

  function includeOnce($path)
  {
    $this->cache_time($path);
    include_once($path);
  }

  function loadFile($path)
  {
    $this->cache_time($path);
    return loadFile($path);
  }
}
?>