changeset 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
files CacheTimeCheck.inc Options.inc common-functions.inc flag.php index.php inputParser.inc
diffstat 6 files changed, 91 insertions(+), 54 deletions(-) [+]
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) {
--- a/Options.inc	Thu Oct 04 22:07:19 2012 +0200
+++ b/Options.inc	Fri Oct 05 00:21:27 2012 +0200
@@ -8,6 +8,7 @@
   private $inputDefaults = array();
   private $cache;
   private $urlParams = array();
+  private $basePath;
 
   function getDefaultLang()
   {
@@ -24,12 +25,20 @@
     $this->lang = $lang;
   }
 
+  function getBasePath()
+  {
+    return $this->basePath;
+  }
+
+  function setBasePath($basePath)
+  {
+    $this->basePath = $basePath;
+  }
+
   function setUrlParams($urlParams)
   {
     foreach($urlParams as $key) {
-      $value = $_GET[$key];
-      if (!$value)
-	$value = '';
+      $value = array_key_exists($key, $_GET) ? $_GET[$key] : '';
       $this->urlParams[$key] = $value;
     }
   }
--- a/common-functions.inc	Thu Oct 04 22:07:19 2012 +0200
+++ b/common-functions.inc	Fri Oct 05 00:21:27 2012 +0200
@@ -1,5 +1,14 @@
 <?php
-include_once 'http-response-status-codes.inc';
+include_once 'CacheTimeCheck.inc';
+
+$cache = CacheTimeCheck::instance(__FILE__);
+$cache->includeOnce('http-response-status-codes.inc');
+
+function basePath()
+{
+  $l = strrpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME']);
+  return substr($_SERVER['SCRIPT_FILENAME'], 0, $l);
+}
 
 function loadFile($sFilename, $sCharset = 'UTF-8')
 {
--- a/flag.php	Thu Oct 04 22:07:19 2012 +0200
+++ b/flag.php	Fri Oct 05 00:21:27 2012 +0200
@@ -3,7 +3,7 @@
 
 include_once 'CacheTimeCheck.inc';
 
-$cache = new CacheTimeCheck(filemtime(__FILE__));
+$cache = CacheTimeCheck::instance(__FILE__);
 $cache->includeOnce('accept-language.inc');
 $cache->includeOnce('common-functions.inc');
 
--- a/index.php	Thu Oct 04 22:07:19 2012 +0200
+++ b/index.php	Fri Oct 05 00:21:27 2012 +0200
@@ -1,49 +1,55 @@
 <?php
-define(DEBUG, 0);
+define(VERBOSITY_NONE, 0);
+define(VERBOSITY_ERROR, 1);
+define(VERBOSITY_WARNING, 10);
+define(VERBOSITY_DEBUG, 100);
+
+define(DEBUG_LEVEL, VERBOSITY_WARNING);
+
+define(DUMP, 0);
 define(MAX_RECURSE, 50);
 define(CACHING, 1);
 
-/*
-var_dump($_SERVER);
-exit;
-*/
 
-if (DEBUG) {
+if (DEBUG_LEVEL >= VERBOSITY_DEBUG) {
+  var_dump($_SERVER);
+}
+
+if (DEBUG_LEVEL >= VERBOSITY_WARNING) {
   error_reporting(E_ALL);
   ini_set("display_errors", 1);
 }
 
 $cacheable = true;
 
-include_once 'php/CacheTimeCheck.inc';
+include_once 'CacheTimeCheck.inc';
 
-$cache = new CacheTimeCheck(filemtime(__FILE__));
-$cache->includeOnce('php/Options.inc');
-$cache->includeOnce('php/accept-language.inc');
-$cache->includeOnce('php/common-functions.inc');
-$cache->includeOnce('php/filters.inc');
-$cache->includeOnce('php/inputParser.inc');
+$cache = CacheTimeCheck::instance(__FILE__);
+$cache->includeOnce('Options.inc');
+$cache->includeOnce('accept-language.inc');
+$cache->includeOnce('common-functions.inc');
+$cache->includeOnce('filters.inc');
+$cache->includeOnce('inputParser.inc');
 
 $master = new DOMDocument();
-$master->load("master.xml");
+$masterName = basePath() . "/master.xml";
+$master->load($masterName);
 
 $options = new Options($master);
 $options->setCache($cache);
+$options->setBasePath(basePath());
 
 $options->setUrlParams(array('name', 'lang'));
-//$URL_PARAMS = array('name', 'lang');
 
-$lang = $_GET['lang'];
-if($lang) {
-  $options->setLang($lang);
+if(array_key_exists('lang', $_GET)) {
+  $options->setLang($_GET['lang']);
 }
 else {
   $options->setLang($options->getDefaultLang());
 }
 
-$name = $_GET['name'];
-if($name) {
-  $options->setName($name);
+if( array_key_exists('name', $_GET)) {
+  $options->setName($_GET['name']);
 }
 
 $params = $master->getElementsByTagName("param");
@@ -62,7 +68,7 @@
 }
 
 if (CACHING && $cacheable)
-  $options->getCache()->CheckHttpModified($newest);
+  $options->getCache()->CheckHttpModified();
 
 print $master->saveXml($master);
 
--- a/inputParser.inc	Thu Oct 04 22:07:19 2012 +0200
+++ b/inputParser.inc	Fri Oct 05 00:21:27 2012 +0200
@@ -39,10 +39,7 @@
   if (!$conf)
     $conf = $param->getAttribute("default");
 
-  $confFile = "${lang}/${conf}.xml";
-  if (!file_exists($confFile)) {
-     errorPage("Resource not available");
-  }
+  $confFile = $options->getBasePath() . "/${lang}/${conf}.xml";
   $options->getCache()->cache_time($confFile);
   $doc = new DOMDocument();
   $doc->load($confFile);
@@ -63,7 +60,7 @@
     foreach ($includes as $include) {
 	$src = $include->getAttribute("src");
 	$subdoc = new DOMDocument();
-	$subfile = "${lang}/${src}";
+	$subfile = $options->getBasePath() . "/${lang}/${src}";
 	$subdoc->load("$subfile");
 	$options->getCache()->cache_time($subfile);
 	$parent = $include->parentNode;
@@ -78,8 +75,8 @@
     $includes = $doc->getElementsByTagName("include");
   }
 
-  $head=getElementByTagName($doc,"head");
-  $title=$head->getAttribute("title");
+  $head = getElementByTagName($doc, "head");
+  $title = $head->getAttribute("title");
 
   if($title) {
     $values=$master->getElementsByTagName("param");
@@ -96,15 +93,15 @@
     }
   }
 
-  $css=getElementByTagName($head,"css");
-  $css=$doc->saveXML($css);
-  $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
+  $css = getElementByTagName($head,"css");
+  $css = $doc->saveXML($css);
+  $css = preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
 
   if($css) {
-    $values=$master->getElementsByTagName("param");
+    $values = $master->getElementsByTagName("param");
     foreach ($values as $value) {
-      if ($value->getAttribute("type")=="input_config") {
-        if ($value->getAttribute("id")=="css") {
+      if ($value->getAttribute("type") == "input_config") {
+        if ($value->getAttribute("id") == "css") {
           $tmp = new DOMDocument();
           $tmp->loadXml("<xml>${css}</xml>");
 	  $parent=$value->parentNode;
@@ -127,7 +124,7 @@
     if ($script) {
       $cacheable = false;
       $src="";
-      $cwd=getcwd();
+      $cwd = getcwd();
 
       $matches=array();
       preg_match('/(.*\/)/', $script, $matches);
@@ -141,7 +138,7 @@
     }
     else {
       $src = $file->getAttribute("src");
-      $fname = "${lang}/${src}";
+      $fname = $options->getBasePath() . "/${lang}/${src}";
       $file_content = $options->getCache()->loadFile($fname);
     }
     if(floatval($file_content)<0) {
@@ -164,8 +161,6 @@
 	  }
       }
       $callString .= ");";
-      //print $callString;
-      //exit;
       eval($callString);
     }
     $out.= $file_content;