view CacheTimeCheck.inc @ 77:9d766788f0bc

Fix remaining documentation errors.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Fri, 12 Oct 2012 01:59:21 +0200
parents dd4ddedca4c5
children d98e315308cd
line wrap: on
line source

<?php
$baseDir = dirname(__FILE__);

include_once 'common-functions.inc';
include_once 'FileCacheSet.inc';
$cache = ScriptIncludeCache::instance(__FILE__);
$cache->cache_time("${baseDir}/FileCacheSet.inc");

/**
 * CacheTimeCheck provides a set of functions to enable generating a
 * correct time for the latest update for a given file.
 *
 * @author Tom Fredrik Blenning Klaussen
 */
class CacheTimeCheck extends FileCacheSet
{
  function __construct($filename = False)
  {
    parent::__construct();
    if ($filename)
      $this->cache_time($filename);
    $this->cache_time(__FILE__);
  }

  public function addParent($cache)
  {
    parent::addParent($cache);
  }

  /**
   * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is
   * newer, set a Last-Modified header, otherwise abort with an 304
   * status code
   */
  function CheckHttpModified()
  {
    if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
      var_dump($_SERVER);

    $gmdate_mod = gmdate('D, d M Y H:i:s', $this->getNewest()) . ' 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->getNewest()) {
	header("HTTP/1.0 304 Not Modified");
	exit;
      }
    }

    header("Last-Modified: $gmdate_mod");
  }

  /**
   * Convenience function to load a file, and add it to the cacheset
   *
   * @param $path path of the file
   * @return the contents of the file
   */
  function loadFile($path)
  {
    $this->cache_time($path);
    return loadFile($path);
  }
}
?>