changeset 23:814296ea84a9

Move functionality into inputParser.inc
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Wed, 19 Sep 2012 18:23:10 +0200
parents ccfddd7fba1b
children 5c4d7c758fda
files index.php inputParser.inc
diffstat 2 files changed, 182 insertions(+), 179 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Wed Sep 19 16:15:38 2012 +0200
+++ b/index.php	Wed Sep 19 18:23:10 2012 +0200
@@ -21,6 +21,7 @@
 include_with_mtime('php/accept-language.inc');
 include_with_mtime('php/filters.inc');
 include_with_mtime('php/common-functions.inc');
+include_with_mtime('php/inputParser.inc');
 
 $URL_PARAMS=array('name','lang');
 
@@ -45,191 +46,18 @@
   }
 }
 
-function getParam($param)
-{
-  $param_type=$param->getAttribute("type");
-  $param_value;
-  if (!$param_type)
-    $param_type="scalar";
-
-  if($param_type == "scalar") {
-    $param_subst=$param->getAttribute("subst");
-    $param_value=$param->getAttribute("value");
-    if ($param_subst) {
-      /*
-      $param_value=preg_replace("/name/", $name, $param_subst);
-      $param_value=preg_replace('/lang/', $lang, $param_value);
-      */
-    }
-  }
-  elseif($param_type == "array") {
-	  $params=$param->getElementsByTagName("param");
-	  $param_value=array();
-	  foreach ($param->childNodes as $param) {
-	    if ($param->nodeType == XML_ELEMENT_NODE)
-	    {
-	      array_push($param_value, getParam($param));            
-	    }
-	  }
-  }
-  return $param_value;
-}
-
-function getInput($master, $param)
-{
-  $out='';
-
-  $lang=$GLOBALS['lang'];
-  $name=$param->getAttribute("id");
-  $conf=$_GET[$name];
-  $GLOBALS[$name]=$conf;
-  if (!$conf)
-    $conf=$param->getAttribute("default");
-
-  $fname = "${lang}/${conf}.xml";
-  cache_time($fname);
-  $config = loadFile($fname);
-
-  $confFile="${lang}/${conf}.xml";
-  if (!file_exists($confFile)) {
-     errorPage("Resource not available");
-  }
-  $doc = new DOMDocument();
-  $doc->load($confFile);
-  
-  $includes=$doc->getElementsByTagName("include");
-  $recurse=0;
-
-  while($includes->length>0) {
-    if(++$recurse>MAX_RECURSE) {
-      errorPage('Recursion limit exceeded', 500);
-    }
-    foreach ($includes as $include) {
-	$src=$include->getAttribute("src");
-	$subdoc = new DOMDocument();
-	$subdoc->load("${lang}/${src}");
-	$parent=$include->parentNode;
-	$xml=getElementByTagName($subdoc,"xml");
-	foreach($xml->childNodes as $child) {
-		$text=$subdoc->saveXml($child);
-		$clonedChild=$doc->importNode($child,true);
-		$parent->insertBefore($clonedChild,$include);
-	}
-	$parent->removeChild($include);	
-    }
-    $includes=$doc->getElementsByTagName("include");
-  }
-
-  $head=getElementByTagName($doc,"head");
-  $title=$head->getAttribute("title");
-
-  if($title) {
-    $values=$master->getElementsByTagName("param");
-    foreach ($values as $value) {
-      if ($value->getAttribute("type")=="input_config") {
-        if ($value->getAttribute("id")=="title") {
-          $tmp = new DOMDocument();
-          $tmp->loadXml("<xml>${title}</xml>");
-	  $parent=$value->parentNode;
-	  $parent->removeChild($value);
-	  $parent->appendChild(new DOMText($tmp->textContent));
-        }
-      }
-    }
-  }
-
-  $css=getElementByTagName($head,"css");
-  $css=$doc->saveXML($css);
-  $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
-
-  if($css) {
-    $values=$master->getElementsByTagName("param");
-    foreach ($values as $value) {
-      if ($value->getAttribute("type")=="input_config") {
-        if ($value->getAttribute("id")=="css") {
-          $tmp = new DOMDocument();
-          $tmp->loadXml("<xml>${css}</xml>");
-	  $parent=$value->parentNode;
-	  foreach($tmp->firstChild->childNodes as $node) {
-		$clonedChild=$master->importNode($node,true);
-		$parent->insertBefore($clonedChild,$value);
-          }
-	  $parent->removeChild($value);
-        }
-      }
-    }
-  }
-
-
-  $body=getElementByTagName($doc,"body");
-  $files=$body->getElementsByTagName("file");
-
-  foreach ($files as $file) {
-    $script=$file->getAttribute("script");
-    if ($script) {
-      $cacheable = false;
-      $src="";
-      $cwd=getcwd();
-      
-      $matches=array();
-      preg_match('/(.*\/)/', $script, $matches);
-      $dirname=$matches[0];
-      preg_match('/([^\/]*)$/', $script, $matches);
-      $filename=$matches[0];
-      chdir("${lang}/${dirname}");
-      $pipe=popen("php ${filename}","r");
-      $file_content = stream_get_contents($pipe);
-      chdir("${cwd}");
-    }
-    else {
-      $src = $file->getAttribute("src");
-      $fname = "${lang}/${src}";
-      cache_time($fname);
-      $file_content = loadFile($fname);
-    }
-    if(floatval($file_content)<0) {
-      errorPage("Resource not found '${lang}/${src}'");
-    }
-
-    $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;
-      foreach ($filter->childNodes as $param) {
-	if ($param->nodeType == XML_ELEMENT_NODE)
-	  {
-	    $param_value[$i]=getParam($param);
-	    $callString.=",\$param_value[$i]";
-	    $i++;
-	  }
-      }
-      $callString.=");";
-      eval($callString);
-    }
-    $out.= $file_content;
-  }
-
-  $doc = new DOMDocument();
-  $doc->loadXml("<xml>${out}</xml>");
-  
-  return $doc;
-}
-
 $master = new DOMDocument();
 $master->load("master.xml");
 
-$params=$master->getElementsByTagName("param");
+$params = $master->getElementsByTagName("param");
 foreach ($params as $param) {
   if ($param->getAttribute("type") == "input") {
-    $doc=getInput($master,$param);
+    $doc = getInput($master, $param);
 
-    $parent=$param->parentNode;
+    $parent = $param->parentNode;
     foreach ($doc->firstChild->childNodes as $child) {
-      $clonedChild=$master->importNode($child,true);
-      $parent->insertBefore($clonedChild,$param);
+      $clonedChild = $master->importNode($child, true);
+      $parent->insertBefore($clonedChild, $param);
     }
     $parent->removeChild($param);	
 
@@ -241,4 +69,4 @@
 
 print $master->saveXml($master);
 
-?>
+?>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inputParser.inc	Wed Sep 19 18:23:10 2012 +0200
@@ -0,0 +1,175 @@
+<?php
+function getParam($param)
+{
+  $param_type=$param->getAttribute("type");
+  $param_value;
+  if (!$param_type)
+    $param_type="scalar";
+
+  if($param_type == "scalar") {
+    $param_subst=$param->getAttribute("subst");
+    $param_value=$param->getAttribute("value");
+    if ($param_subst) {
+      /*
+      $param_value=preg_replace("/name/", $name, $param_subst);
+      $param_value=preg_replace('/lang/', $lang, $param_value);
+      */
+    }
+  }
+  elseif($param_type == "array") {
+	  $params=$param->getElementsByTagName("param");
+	  $param_value=array();
+	  foreach ($param->childNodes as $param) {
+	    if ($param->nodeType == XML_ELEMENT_NODE)
+	    {
+	      array_push($param_value, getParam($param));
+	    }
+	  }
+  }
+  return $param_value;
+}
+
+function getInput($master, $param)
+{
+  $out='';
+
+  $lang=$GLOBALS['lang'];
+  $name=$param->getAttribute("id");
+  $conf=$_GET[$name];
+  $GLOBALS[$name]=$conf;
+  if (!$conf)
+    $conf=$param->getAttribute("default");
+
+  $fname = "${lang}/${conf}.xml";
+  cache_time($fname);
+  $config = loadFile($fname);
+
+  $confFile="${lang}/${conf}.xml";
+  if (!file_exists($confFile)) {
+     errorPage("Resource not available");
+  }
+  $doc = new DOMDocument();
+  $doc->load($confFile);
+
+  $includes=$doc->getElementsByTagName("include");
+  $recurse=0;
+
+  while($includes->length>0) {
+    if(++$recurse>MAX_RECURSE) {
+      errorPage('Recursion limit exceeded', 500);
+    }
+    foreach ($includes as $include) {
+	$src=$include->getAttribute("src");
+	$subdoc = new DOMDocument();
+	$subdoc->load("${lang}/${src}");
+	$parent=$include->parentNode;
+	$xml=getElementByTagName($subdoc,"xml");
+	foreach($xml->childNodes as $child) {
+		$text=$subdoc->saveXml($child);
+		$clonedChild=$doc->importNode($child,true);
+		$parent->insertBefore($clonedChild,$include);
+	}
+	$parent->removeChild($include);
+    }
+    $includes=$doc->getElementsByTagName("include");
+  }
+
+  $head=getElementByTagName($doc,"head");
+  $title=$head->getAttribute("title");
+
+  if($title) {
+    $values=$master->getElementsByTagName("param");
+    foreach ($values as $value) {
+      if ($value->getAttribute("type")=="input_config") {
+        if ($value->getAttribute("id")=="title") {
+          $tmp = new DOMDocument();
+          $tmp->loadXml("<xml>${title}</xml>");
+	  $parent=$value->parentNode;
+	  $parent->removeChild($value);
+	  $parent->appendChild(new DOMText($tmp->textContent));
+        }
+      }
+    }
+  }
+
+  $css=getElementByTagName($head,"css");
+  $css=$doc->saveXML($css);
+  $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
+
+  if($css) {
+    $values=$master->getElementsByTagName("param");
+    foreach ($values as $value) {
+      if ($value->getAttribute("type")=="input_config") {
+        if ($value->getAttribute("id")=="css") {
+          $tmp = new DOMDocument();
+          $tmp->loadXml("<xml>${css}</xml>");
+	  $parent=$value->parentNode;
+	  foreach($tmp->firstChild->childNodes as $node) {
+		$clonedChild=$master->importNode($node,true);
+		$parent->insertBefore($clonedChild,$value);
+          }
+	  $parent->removeChild($value);
+        }
+      }
+    }
+  }
+
+
+  $body=getElementByTagName($doc,"body");
+  $files=$body->getElementsByTagName("file");
+
+  foreach ($files as $file) {
+    $script=$file->getAttribute("script");
+    if ($script) {
+      $cacheable = false;
+      $src="";
+      $cwd=getcwd();
+
+      $matches=array();
+      preg_match('/(.*\/)/', $script, $matches);
+      $dirname=$matches[0];
+      preg_match('/([^\/]*)$/', $script, $matches);
+      $filename=$matches[0];
+      chdir("${lang}/${dirname}");
+      $pipe=popen("php ${filename}","r");
+      $file_content = stream_get_contents($pipe);
+      chdir("${cwd}");
+    }
+    else {
+      $src = $file->getAttribute("src");
+      $fname = "${lang}/${src}";
+      cache_time($fname);
+      $file_content = loadFile($fname);
+    }
+    if(floatval($file_content)<0) {
+      errorPage("Resource not found '${lang}/${src}'");
+    }
+
+    $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;
+      foreach ($filter->childNodes as $param) {
+	if ($param->nodeType == XML_ELEMENT_NODE)
+	  {
+	    $param_value[$i]=getParam($param);
+	    $callString.=",\$param_value[$i]";
+	    $i++;
+	  }
+      }
+      $callString.=");";
+      eval($callString);
+    }
+    $out.= $file_content;
+  }
+
+  $doc = new DOMDocument();
+  $doc->loadXml("<xml>${out}</xml>");
+
+  return $doc;
+}
+
+?>
\ No newline at end of file