view index.php @ 10:1ac66bef193a

Updated to new xml format.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Sat, 21 May 2011 12:46:02 +0200
parents 350a8c63bd14
children ba6f0818018b
line wrap: on
line source

<?php
define(DEBUG,0);
define(MAX_RECURSE,50);
define(CACHING,0);

if (DEBUG) {
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
}


if(CACHING) {
include 'php/cache_check.inc';
}
include 'php/accept-language.inc';
include 'php/filters.inc';
include 'php/common-functions.inc';

$URL_PARAMS=array('name','lang');

#Globals
$name = $_GET['name'];
$lang = $_GET['lang'];

if(!$name) {
  $name="home";
}

if(!$lang) {
  $lang="no";
  $langs=acceptedLanguages();
  foreach ($langs as $l => $val) {
    if (file_exists($l)) {
      $lang=$l;
      break;
    }
  }
}

$title="Dummy title";

$config=loadFile("${lang}/${name}.xml");

$confFile="${lang}/${name}.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");

$css=getElementByTagName($head,"css");
$css=$doc->saveXML($css);
$css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);

$body=getElementByTagName($doc,"body");
$files=$body->getElementsByTagName("file");


$out= '<?xml version="1.0" encoding="UTF-8"?>';

$out.= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>';

$out.= "$title";
$out.= '
	</title>
';
$out.= "$css";
$out.= '
    </head>
    <body>
      <div id="container">
';

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;
}

foreach ($files as $file) {
	$src=$file->getAttribute("src");
	$file_content=loadFile("${lang}/${src}");
	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;
}

$out.='
      </div>
    </body>
</html>
';

print $out;
?>