changeset 47:66382989353f

Extract baseDir only once. Function for generating cacheset. Added logger functionality. Removed hardcoded location for flagScript. BUGFIX: masterdocument was not added to the cacheset. BUGFIX: When two options existed next to eachother, the last was not read.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Mon, 08 Oct 2012 17:35:08 +0200
parents 15879e2aab65
children c6d0892f81ff
files CacheTimeCheck.inc Options.inc constants.inc filters.inc index.php logger.inc
diffstat 6 files changed, 106 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/CacheTimeCheck.inc	Mon Oct 08 15:49:28 2012 +0200
+++ b/CacheTimeCheck.inc	Mon Oct 08 17:35:08 2012 +0200
@@ -12,6 +12,19 @@
     $this->cache_time(__FILE__);
   }
 
+  function cacheSet($humanReadable = False)
+  {
+    $retVal = array();
+    foreach($this->files as $file) {
+      $mtime = filemtime($file);
+      if ($humanReadable)
+	$mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
+
+      $retVal[$file] = $mtime;
+    }
+    return $retVal;
+  }
+
   function instance($filename = False)
   {
     if (! self::$myInstance)
@@ -44,9 +57,7 @@
   function cache_time($path, $basedir = False)
   {
     if (!file_exists($path)) {
-      if (DEBUG_LEVEL >= VERBOSITY_WARNING)
-	print "${path} does not exist";
-      var_dump($_SERVER);
+      warn("${path} does not exist");
       errorPage("Resource not available");
     }
 
--- a/Options.inc	Mon Oct 08 15:49:28 2012 +0200
+++ b/Options.inc	Mon Oct 08 17:35:08 2012 +0200
@@ -9,6 +9,8 @@
   private $cache;
   private $urlParams = array();
   private $basePath;
+  private $flagUrl;
+  private $baseUrl;
 
   function getDefaultLang()
   {
@@ -20,6 +22,44 @@
     return $this->lang;
   }
 
+  function getBaseUrl()
+  {
+    if ($this->baseUrl)
+      return $this->baseUrl;
+
+    $request = $_SERVER['REQUEST_URI'];
+    $l = strpos($request, '?');
+    $base = ($l) ? substr($request, 0 , $l) : $request;
+
+    return "http://" . $_SERVER['HTTP_HOST'] . $base;
+  }
+
+  function replacePlaceholders($value)
+  {
+    $value = preg_replace('/%HOST%/', $_SERVER['HTTP_HOST'], $value);
+    return $value;
+  }
+
+  function setBaseUrl($baseUrl)
+  {
+    $baseUrl = self::replacePlaceholders($baseUrl);
+    $this->baseUrl = $baseUrl;
+  }
+
+  function setFlagUrl($flagUrl)
+  {
+    $flagUrl = self::replacePlaceholders($flagUrl);
+    $this->flagUrl = $flagUrl;
+  }
+
+  function getFlagUrl()
+  {
+    if ($this->flagUrl)
+      return $this->flagUrl;
+
+    return $this->getBaseUrl() . "/flag.php";
+  }
+
   function setLang($lang)
   {
     $this->lang = $lang;
@@ -81,6 +121,7 @@
   function __construct($baseDocument)
   {
     $params = $baseDocument->getElementsByTagName("param");
+    $toRemove = array();
     foreach ($params as $param) {
       if ($param->getAttribute("type") == "option") {
 	$id = $param->getAttribute("id");
@@ -93,11 +134,22 @@
 	    }
 	  }
 	}
-	else {
-	  die("Invalid option : $id");
+	elseif ($id == "baseUrl") {
+	  $value = $param->getAttribute("value");
+	  if($value)
+	    $this->setBaseUrl($value);
 	}
-	$parent = $param->parentNode;
-	$parent->removeChild($param);
+	elseif ($id == "flagUrl") {
+	  $value = $param->getAttribute("value");
+	  if($value)
+	    $this->setFlagUrl($value);
+	}
+	else {
+	  warn("Invalid option: ${id}");
+	}
+	//We need to iterate in the opposite direction when removing,
+	//so best shifting.
+	array_unshift($toRemove, $param);
       }
       elseif ($param->getAttribute("type") == "input") {
 	$id = $param->getAttribute("id");
@@ -105,6 +157,10 @@
 	$this->inputDefaults[$id] = $default;
       }
     }
+    foreach($toRemove as $param) {
+      $parent = $param->parentNode;
+      $parent->removeChild($param);
+    }
   }
 }
 ?>
\ No newline at end of file
--- a/constants.inc	Mon Oct 08 15:49:28 2012 +0200
+++ b/constants.inc	Mon Oct 08 17:35:08 2012 +0200
@@ -10,6 +10,9 @@
 define(MAX_RECURSE, 50);
 define(CACHING, 1);
 
+define(ABORT_ON_LOG, TRUE);
+
+
 if (DEBUG_LEVEL >= VERBOSITY_WARNING) {
   error_reporting(E_ALL);
   ini_set("display_errors", 1);
--- a/filters.inc	Mon Oct 08 15:49:28 2012 +0200
+++ b/filters.inc	Mon Oct 08 17:35:08 2012 +0200
@@ -37,8 +37,11 @@
     if ($active)
       $langbar .= '<a href="'.genUrl($options->getUrlParams(), array( 'lang' => $l), array('lang', 'name') ) . '">';
 
+    //$flagUrl = "http://dev.bfginvest.no/php/flag.php";
+    $flagUrl = $options->getFlagUrl();
+
     $langbar .= "
-	      <img src=\"http://dev.bfginvest.no/php/flag.php?lang=${l}&amp;active=${active}\" width=\"20\" height=\"16\" alt=\"Norsk versjon - inaktiv\" title=\"Norsk\"/>";
+	      <img src=\"${flagUrl}?lang=${l}&amp;active=${active}\" width=\"20\" height=\"16\" alt=\"Norsk versjon - inaktiv\" title=\"Norsk\"/>";
     if ($active)
       $langbar .= "</a>";
 
--- a/index.php	Mon Oct 08 15:49:28 2012 +0200
+++ b/index.php	Mon Oct 08 17:35:08 2012 +0200
@@ -5,12 +5,15 @@
 
 include_once 'CacheTimeCheck.inc';
 
+$baseDir = dirname(__FILE__);
+
 $cache = CacheTimeCheck::instance(__FILE__);
-$cache->includeOnce('Options.inc', dirname(__FILE__));
-$cache->includeOnce('accept-language.inc', dirname(__FILE__));
-$cache->includeOnce('common-functions.inc', dirname(__FILE__));
-$cache->includeOnce('filters.inc', dirname(__FILE__));
-$cache->includeOnce('inputParser.inc', dirname(__FILE__));
+$cache->includeOnce('Options.inc', $baseDir);
+$cache->includeOnce('accept-language.inc', $baseDir);
+$cache->includeOnce('common-functions.inc', $baseDir);
+$cache->includeOnce('filters.inc', $baseDir);
+$cache->includeOnce('inputParser.inc', $baseDir);
+$cache->includeOnce('logger.inc', $baseDir);
 
 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) {
   var_dump($_SERVER);
@@ -18,6 +21,7 @@
 
 $master = new DOMDocument();
 $masterName = basePath() . "/master.xml";
+$cache->cache_time($masterName);
 $master->load($masterName);
 
 $options = new Options($master);
@@ -56,5 +60,6 @@
   $options->getCache()->CheckHttpModified();
 
 print $master->saveXml($master);
+//print_r($cache->cacheSet(1));
 
 ?>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/logger.inc	Mon Oct 08 17:35:08 2012 +0200
@@ -0,0 +1,15 @@
+<?php
+function logMsg($level, $msg)
+{
+  if (DEBUG_LEVEL >= $level) {
+    print $msg;
+    if(ABORT_ON_LOG)
+      exit;
+  }
+}
+
+function warn($msg)
+{
+  logMsg(VERBOSITY_WARNING, $msg);
+}
+?>
\ No newline at end of file