changeset 36:c3a61615a39c

Branch merge.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 04 Oct 2012 21:22:02 +0200
parents aec57ed6f5f6 (diff) f37be50a70cf (current diff)
children da1726860524
files
diffstat 6 files changed, 182 insertions(+), 123 deletions(-) [+]
line wrap: on
line diff
--- a/Options.inc	Sun Sep 30 17:06:07 2012 +0200
+++ b/Options.inc	Thu Oct 04 21:22:02 2012 +0200
@@ -6,6 +6,8 @@
   private $name;
   private $acceptedLanguages = array();
   private $inputDefaults = array();
+  private $cache;
+  private $urlParams = array();
 
   function getDefaultLang()
   {
@@ -22,6 +24,21 @@
     $this->lang = $lang;
   }
 
+  function setUrlParams($urlParams)
+  {
+    foreach($urlParams as $key) {
+      $value = $_GET[$key];
+      if (!$value)
+	$value = '';
+      $this->urlParams[$key] = $value;
+    }
+  }
+
+  function getUrlParams()
+  {
+    return $this->urlParams;
+  }
+
   function setName($name)
   {
     $this->name = $name;
@@ -32,6 +49,16 @@
     return $this->name;
   }
 
+  function setCache($cache)
+  {
+    $this->cache = $cache;
+  }
+
+  function getCache()
+  {
+    return $this->cache;
+  }
+
   function getAcceptedLanguages()
   {
     return $this->acceptedLanguages;
--- a/cache_check.inc	Sun Sep 30 17:06:07 2012 +0200
+++ b/cache_check.inc	Thu Oct 04 21:22:02 2012 +0200
@@ -1,35 +1,47 @@
 <?php
-function cache_check($mtime)
+class CacheTimeCheck
 {
-  if (DEBUG)
-    var_dump($_SERVER);
+  private $newest;
+  private $files = array();
 
-  $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE'];
-  $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
+  function __construct($minTime = 0, $filename = False)
+  {
+    if ($filename)
+      array_push($this->files, $filename);
+    $this->newest = $minTime;
+    $this->cache_time(__FILE__);
+  }
 
-  $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
+  function CheckHttpModified()
+  {
+    if (DEBUG)
+      var_dump($_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;
+    $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT';
+
+    if ($if_modified_since == $gmdate_mod) {
+      header("HTTP/1.0 304 Not Modified");
+      exit;
+    }
+    header("Last-Modified: $gmdate_mod");
   }
-  header("Last-Modified: $gmdate_mod");
-}
 
-function cache_time($path)
-{
-  global $newest;
-  $mtime = filemtime($path);
-  if ($mtime > $newest) {
-    $newest = $mtime;
+  function cache_time($path)
+  {
+    array_push($this->files, $path);
+    $mtime = filemtime($path);
+    if ($mtime > $this->newest) {
+      $this->newest = $mtime;
+    }
+  }
+
+  function includeOnce($path)
+  {
+    $this->cache_time($path);
+    include_once($path);
   }
 }
-
-function include_with_mtime($path)
-{
-  cache_time($path);
-  include_once($path);
-}
-
-cache_time(__FILE__);
 ?>
\ No newline at end of file
--- a/common-functions.inc	Sun Sep 30 17:06:07 2012 +0200
+++ b/common-functions.inc	Thu Oct 04 21:22:02 2012 +0200
@@ -3,71 +3,71 @@
 
 function loadFile($sFilename, $sCharset = 'UTF-8')
 {
-    if (floatval(phpversion()) >= 4.3) {
-      if (!file_exists($sFilename)) return -3;
-      $sData = file_get_contents($sFilename);
-    } else {
-        if (!file_exists($sFilename)) return -3;
-        $rHandle = fopen($sFilename, 'r');
-        if (!$rHandle) return -2;
+  if (floatval(phpversion()) >= 4.3) {
+    if (!file_exists($sFilename))
+      return -3;
+    $sData = file_get_contents($sFilename);
+  }
+  else {
+    if (!file_exists($sFilename))
+      return -3;
+    $rHandle = fopen($sFilename, 'r');
+    if (!$rHandle)
+      return -2;
 
-        $sData = '';
-        while(!feof($rHandle))
-            $sData .= fread($rHandle, filesize($sFilename));
-        fclose($rHandle);
+    $sData = '';
+    while(!feof($rHandle))
+      $sData .= fread($rHandle, filesize($sFilename));
+    fclose($rHandle);
+  }
+  if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) {
+    if ($sEncoding != 1) {
+      $sData = mb_convert_encoding($sData, $sCharset, $sEncoding);
     }
-    if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) {
-       if ($sEncoding != 1) {
-       	 $sData = mb_convert_encoding($sData, $sCharset, $sEncoding);
-       }
-    }
-    return $sData;
+  }
+  return $sData;
 }
 
-function errorPage($errorText, $errorCode=403)
+function errorPage($errorText, $errorCode = 403)
 {
   header(StatusCodes::httpHeaderFor($errorCode));
   print "<div id=\"page\"><h1>${errorText}</h1></div>";
   exit;
 }
 
-function genUrl($keys=array()) {
-	 $out="?";
-	 $first=1;
-	 $URL_PARAMS=$GLOBALS['URL_PARAMS'];
-	 $new_params=array();
-	 foreach($URL_PARAMS as $param) {
-		 $new_params[$param]=$GLOBALS[$param];
-	 }
-	 foreach($keys as $param => $val) {
-		 $new_params[$param]=$val;
-	 }
-	 foreach($new_params as $param => $val) {
-	         if($first)
-		   $first=0;
-		 else
-		   $out.="&amp;";
-	 	 $out.=urlencode($param).'='.urlencode($val);
-	 }
-	 return $out;
+function genUrl($urlParams, $keys = array()) {
+  $out = "?";
+  $first = 1;
+  $new_params = $urlParams;
+  foreach($keys as $param => $val) {
+    $new_params[$param] = $val;
+  }
+  foreach($new_params as $param => $val) {
+    if($first)
+      $first = 0;
+    else
+      $out .= "&amp;";
+    $out .= urlencode($param) . '=' . urlencode($val);
+  }
+  return $out;
 }
 
 function getElementByTagName($obj, $name) {
-  $elems=$obj->getElementsByTagName($name);
+  $elems = $obj->getElementsByTagName($name);
   if ($elems->length != 1) {
     exit;
   }
-  $elem=$elems->item(0);
+  $elem = $elems->item(0);
   return $elem;
 }
 
 function getXmlContent($node)
 {
-  $text=$node->ownerDocument->saveXml($node);
-  $pattern="/^<".$node->tagName."[^>]*>/is";
-  $text=preg_replace($pattern, '' , $text);
-  $pattern='/<\/'.$node->tagName.'[^>]*>$/is';
-  $text=preg_replace($pattern, '' , $text);
+  $text = $node->ownerDocument->saveXml($node);
+  $pattern = "/^<" . $node->tagName."[^>]*>/is";
+  $text = preg_replace($pattern, '' , $text);
+  $pattern = '/<\/' . $node->tagName . '[^>]*>$/is';
+  $text = preg_replace($pattern, '' , $text);
 
   return $text;
 }
--- a/filters.inc	Sun Sep 30 17:06:07 2012 +0200
+++ b/filters.inc	Thu Oct 04 21:22:02 2012 +0200
@@ -1,52 +1,68 @@
 <?php
-function activeNav($input)
+function activeNav($input, $options)
 {
-	$name=$GLOBALS['name'];
-	$lang=$GLOBALS['lang'];
-	$pattern = "/<li id=\"${name}\"\s?([^>]*)>/is";
-	$replacement = "<li id=\"${name}\" class=\"active\" $1>";
-	$output= preg_replace($pattern, $replacement, $input);
+  $name = $options->getName();
+  $lang = $options->getLang();
+  $pattern = "/<li id=\"${name}\"\s?([^>]*)>/is";
+  $replacement = "<li id=\"${name}\" class=\"active\" $1>";
+  $output = preg_replace($pattern, $replacement, $input);
+
+  $pattern = '/<li id="([^"]+)"\s?([^>]*)>(.*?)<\/li>/is';
+  $replacement = "<li id=\"\$1\" \$2><a href=\"%URL-$1%\">\$3</a></li>";
+
+  $opt = 'array(';
+  $start = True;
 
-	$pattern = '/<li id="([^"]+)"\s?([^>]*)>(.*?)<\/li>/is';
-	$replacement = "<li id=\"\$1\" \$2><a href=\"%URL-$1%\">\$3</a></li>";
-	$output=preg_replace_callback($pattern,
+  foreach($options->getUrlParams() as $param => $value) {
+    if ($start) {
+      $start = False;
+      $opt .= "\"${param}\" => \"${value}\"";
+    }
+    else {
+      $opt .= ", \"${param}\" => \"${value}\"";
+    }
+  }
+  $opt .= ')';
+
+  $output = preg_replace_callback($pattern,
 	create_function(
 	  '$matches',
-	'return "<li id=\"$matches[1]\" $matches[2]><a href=\"".genUrl(array("name" => $matches[1]))."\">$matches[3]</a></li>";'
+	  'return "<li id=\"$matches[1]\" $matches[2]><a href=\"" . genUrl(' . $opt . ', array("name" => $matches[1])) . "\">$matches[3]</a></li>";'
 	),
 	$output);
 
-	return $output;
+  return $output;
 }
 
-function addLangBar($input, $languages) {
-	 $name=$GLOBALS['name'];
-	 $lang=$GLOBALS['lang'];
-//	 $languages = array("no","en");
-	 $langbar='<ul id="language-select">';
+function addLangBar($input, $languages)
+{
+  $name=$GLOBALS['name'];
+  $lang=$GLOBALS['lang'];
+  //	 $languages = array("no","en");
+  $langbar='<ul id="language-select">';
 
-	 foreach($languages as $l) {
-	   $active=($l == $lang)?0:1;
-	     $langbar.= "
+  foreach($languages as $l) {
+    $active = ($l == $lang) ? 0 : 1;
+    $langbar.= "
 	    <li class=\"${l}\">";
-	    if ($active)
-	    $langbar.='<a href="'.genUrl(array( 'lang' => $l)).'">';
+    if ($active)
+      $langbar .= '<a href="'.genUrl(array( 'lang' => $l)) . '">';
 
-	    $langbar.= "
+    $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\"/>";
-	    if ($active)
-	    $langbar.="</a>";
+    if ($active)
+      $langbar .= "</a>";
 
-	      $langbar.= "
+    $langbar .= "
 	    </li>
 ";
 
-	}
-	$langbar.='</ul>';
-	$pattern = '/<ul id="language-select"\/>/';
-	$replacement = $langbar;
-	$output=preg_replace($pattern, $replacement, $input);
+  }
+  $langbar.='</ul>';
+  $pattern = '/<ul id="language-select"\/>/';
+  $replacement = $langbar;
+  $output = preg_replace($pattern, $replacement, $input);
 
-	return $output;
+  return $output;
 }
 ?>
\ No newline at end of file
--- a/index.php	Sun Sep 30 17:06:07 2012 +0200
+++ b/index.php	Thu Oct 04 21:22:02 2012 +0200
@@ -1,7 +1,7 @@
 <?php
-define(DEBUG,0);
-define(MAX_RECURSE,50);
-define(CACHING,1);
+define(DEBUG, 0);
+define(MAX_RECURSE, 50);
+define(CACHING, 1);
 
 /*
 var_dump($_SERVER);
@@ -13,23 +13,25 @@
   ini_set("display_errors", 1);
 }
 
-$SCRIPT_FILENAME=$_SERVER['SCRIPT_FILENAME'];
-$newest = filemtime($SCRIPT_FILENAME);
 $cacheable = true;
 
 include_once 'php/cache_check.inc';
-include_with_mtime('php/Options.inc');
-include_with_mtime('php/accept-language.inc');
-include_with_mtime('php/common-functions.inc');
-include_with_mtime('php/filters.inc');
-include_with_mtime('php/inputParser.inc');
 
-$URL_PARAMS=array('name','lang');
+$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');
 
 $master = new DOMDocument();
 $master->load("master.xml");
 
 $options = new Options($master);
+$options->setCache($cache);
+
+$options->setUrlParams(array('name', 'lang'));
+//$URL_PARAMS = array('name', 'lang');
 
 $lang = $_GET['lang'];
 if($lang) {
@@ -60,7 +62,7 @@
 }
 
 if (CACHING && $cacheable)
-  cache_check($newest);
+  $options->getCache()->CheckHttpModified($newest);
 
 print $master->saveXml($master);
 
--- a/inputParser.inc	Sun Sep 30 17:06:07 2012 +0200
+++ b/inputParser.inc	Thu Oct 04 21:22:02 2012 +0200
@@ -43,7 +43,7 @@
   if (!file_exists($confFile)) {
      errorPage("Resource not available");
   }
-  cache_time($confFile);
+  $options->getCache()->cache_time($confFile);
   $doc = new DOMDocument();
   $doc->load($confFile);
 
@@ -65,7 +65,7 @@
 	$subdoc = new DOMDocument();
 	$subfile = "${lang}/${src}";
 	$subdoc->load("$subfile");
-	cache_time($subfile);
+	$options->getCache()->cache_time($subfile);
 	$parent = $include->parentNode;
 	$xml = getElementByTagName($subdoc,"xml");
 	foreach($xml->childNodes as $child) {
@@ -142,7 +142,7 @@
     else {
       $src = $file->getAttribute("src");
       $fname = "${lang}/${src}";
-      cache_time($fname);
+      $options->getCache()->cache_time($fname);
       $file_content = loadFile($fname);
     }
     if(floatval($file_content)<0) {
@@ -151,20 +151,22 @@
 
     $filters=$file->getElementsByTagName("filter");
     foreach($filters as $filter) {
-      $func=$filter->getAttribute("function");
-      $params=$filter->getElementsByTagName("param");
-      $callString="\$file_content=${func}(\$file_content";
-      $param_values=array();
-      $i=0;
+      $func = $filter->getAttribute("function");
+      $params = $filter->getElementsByTagName("param");
+      $callString = "\$file_content = ${func}(\$file_content, \$options";
+      $param_values = array();
+      $i = 0;
       foreach ($filter->childNodes as $param) {
 	if ($param->nodeType == XML_ELEMENT_NODE)
 	  {
-	    $param_value[$i]=getParam($param);
-	    $callString.=",\$param_value[$i]";
+	    $param_value[$i] = getParam($param);
+	    $callString .= ",\$param_value[$i]";
 	    $i++;
 	  }
       }
-      $callString.=");";
+      $callString .= ");";
+      //print $callString;
+      //exit;
       eval($callString);
     }
     $out.= $file_content;