view index.php @ 5:18aafb1a8986

Better handling of errors and globals.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Fri, 20 May 2011 13:25:53 +0200
parents 74196528fc64
children 6c0162497d56
line wrap: on
line source

<?php
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");

function getElementByTagName($obj, $name) {
  $elems=$obj->getElementsByTagName($name);
  if ($elems->length != 1) {
    exit;
  }
  $elem=$elems->item(0);
  return $elem;
}

$confFile="${lang}/${name}.xml";
if (!file_exists($confFile)) {
   errorPage("Resource not available");
}
$doc = new DOMDocument();
$doc->load($confFile);
  
$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">
';

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";
	  foreach ($params as $param) {
	  	  $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);
                  }
		  $callString.=",\"${param_value}\"";
	  }
	  $callString.=");";
	  eval($callString);
        }
        $out.= $file_content;
}

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

print $out;
?>