view CacheTimeCheck.inc.php @ 136:60bc8f62384d default tip

Use internal URL if available to generate Sitemap.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Mon, 23 Jan 2023 00:17:36 +0100
parents b6b4a58c7625
children
line wrap: on
line source

<?php
$baseDir = dirname(__FILE__);

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

function toGMTime($time) {
  return gmdate('D, d M Y H:i:s', $time) . ' GMT';
}

/**
 * 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($cache)
{
  if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
    var_dump($_SERVER);

  $gmdate_mod = toGMTime($cache->getNewest());

  if (array_key_exists('REDIRECT_URL', $_SERVER) && $_SERVER['REDIRECT_URL'] == '/sitemap.xml') {
    //print_r($gmdate_mod);
  }

  foreach($cache->cacheControl() as $header => $value) {
    header ("${header}: ${value}");
  }
  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) >= $cache->getNewest()) {
      header("HTTP/1.0 304 Not Modified");
      return false;
    }
  }

  foreach($cache->cacheControl() as $header => $value) {
    header ("${header}: ${value}");
  }
  header("Last-Modified: $gmdate_mod");
  return true;
}



/**
 * 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__);
    $this->max_age=0;
  }

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

  /**
   * 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);
  }

}
?>