diff Options.inc @ 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 bd82b719a0de
children c6d0892f81ff
line wrap: on
line diff
--- 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