view cache_check.inc @ 36:c3a61615a39c

Branch merge.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 04 Oct 2012 21:22:02 +0200
parents 7b19be62ea94
children
line wrap: on
line source

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