# HG changeset patch # User Tom Fredrik "BFG" Klaussen # Date 1348957018 -7200 # Node ID d8c7b328899ea39c360b08e677db4675d6aee327 # Parent da86ec2814e23112230099199f09c0db7109b325 Removed globals, and introduced Options object for passing values around. Introduced tag into master.xml diff -r da86ec2814e2 -r d8c7b328899e Options.inc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Options.inc Sun Sep 30 00:16:58 2012 +0200 @@ -0,0 +1,58 @@ +defaultLang; + } + + function getLang() + { + return $this->lang; + } + + function setLang($lang) + { + $this->lang = $lang; + } + + function setName($name) + { + $this->name = $name; + } + + function getName() + { + return $this->name; + } + + function __construct($baseDocument) + { + $params = $baseDocument->getElementsByTagName("param"); + foreach ($params as $param) { + if ($param->getAttribute("type") == "option") { + $id = $param->getAttribute("id"); + if ($id == "lang") { + $this->defaultLang = $param->getAttribute("default"); + $accepts = $param->getElementsByTagName("accept_value"); + foreach($accepts as $accept) { + foreach($accept->childNodes as $child) { + array_push($this->acceptedLanguages, $child->nodeValue); + } + } + } + else { + die("Invalid option : $id"); + } + $parent = $param->parentNode; + $parent->removeChild($param); + } + } + } +} +?> \ No newline at end of file diff -r da86ec2814e2 -r d8c7b328899e index.php --- a/index.php Wed Sep 19 21:50:20 2012 +0200 +++ b/index.php Sun Sep 30 00:16:58 2012 +0200 @@ -18,37 +18,37 @@ $cacheable = true; include '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/common-functions.inc'); include_with_mtime('php/inputParser.inc'); $URL_PARAMS=array('name','lang'); -#Globals -$name = $_GET['name']; -$lang = $_GET['lang']; - $master = new DOMDocument(); $master->load("master.xml"); -$params = $master->getElementsByTagName("param"); -foreach ($params as $param) { - if ($param->getAttribute("type") == "option") { - if ($param->getAttribute("id") == "lang") { - $defaultLang = $param->getAttribute("default"); - } - } +$options = new Options($master); + +$lang = $_GET['lang']; +if($lang) { + $options->setLang($lang); +} +else { + $options->setLang($options->getDefaultLang()); } -if (!$lang) { - $lang = preferLanguage($defaultLang); +$name = $_GET['name']; +if($name) { + $options->setName($name); } +$params = $master->getElementsByTagName("param"); foreach ($params as $param) { if ($param->getAttribute("type") == "input") { - $doc = getInput($master, $param); + $doc = getInput($master, $param, $options); $parent = $param->parentNode; foreach ($doc->firstChild->childNodes as $child) { @@ -56,7 +56,6 @@ $parent->insertBefore($clonedChild, $param); } $parent->removeChild($param); - } } diff -r da86ec2814e2 -r d8c7b328899e inputParser.inc --- a/inputParser.inc Wed Sep 19 21:50:20 2012 +0200 +++ b/inputParser.inc Sun Sep 30 00:16:58 2012 +0200 @@ -29,16 +29,15 @@ return $param_value; } -function getInput($master, $param) +function getInput($master, $param, $options) { $out=''; - $lang=$GLOBALS['lang']; + $lang = $options->getLang(); $name=$param->getAttribute("id"); - $conf=$_GET[$name]; - $GLOBALS[$name]=$conf; + $conf = $options->getName(); if (!$conf) - $conf=$param->getAttribute("default"); + $conf = $param->getAttribute("default"); $fname = "${lang}/${conf}.xml"; cache_time($fname); @@ -65,9 +64,9 @@ $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); + $text=$subdoc->saveXml($child); + $clonedChild=$doc->importNode($child,true); + $parent->insertBefore($clonedChild,$include); } $parent->removeChild($include); }