comparison index.php @ 11:ba6f0818018b

Made recursive input evaluation into a function.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Mon, 28 May 2012 22:57:30 +0200
parents 1ac66bef193a
children a20bb1b51aad
comparison
equal deleted inserted replaced
10:1ac66bef193a 11:ba6f0818018b
1 <?php 1 <?php
2 define(DEBUG,0); 2 define(DEBUG,0);
3 define(MAX_RECURSE,50); 3 define(MAX_RECURSE,50);
4 define(CACHING,0); 4 define(CACHING,0);
5 5
6 /*
7 var_dump($_SERVER);
8 exit;
9 */
10
6 if (DEBUG) { 11 if (DEBUG) {
7 error_reporting(E_ALL); 12 error_reporting(E_ALL);
8 ini_set("display_errors", 1); 13 ini_set("display_errors", 1);
9 } 14 }
10 15
27 } 32 }
28 33
29 if(!$lang) { 34 if(!$lang) {
30 $lang="no"; 35 $lang="no";
31 $langs=acceptedLanguages(); 36 $langs=acceptedLanguages();
32 foreach ($langs as $l => $val) { 37 if ($langs) {
33 if (file_exists($l)) { 38 foreach ($langs as $l => $val) {
34 $lang=$l; 39 if (file_exists($l)) {
35 break; 40 $lang=$l;
36 } 41 break;
37 } 42 }
38 } 43 }
39 44 }
40 $title="Dummy title"; 45 }
41 46
42 $config=loadFile("${lang}/${name}.xml"); 47 function getParam($param) {
43 48 $param_type=$param->getAttribute("type");
44 $confFile="${lang}/${name}.xml"; 49 $param_value;
45 if (!file_exists($confFile)) { 50 if (!$param_type)
46 errorPage("Resource not available"); 51 $param_type="scalar";
47 } 52
48 $doc = new DOMDocument(); 53 if($param_type == "scalar") {
49 $doc->load($confFile); 54 $param_subst=$param->getAttribute("subst");
55 $param_value=$param->getAttribute("value");
56 if ($param_subst) {
57 /*
58 $param_value=preg_replace("/name/", $name, $param_subst);
59 $param_value=preg_replace('/lang/', $lang, $param_value);
60 */
61 }
62 }
63 elseif($param_type == "array") {
64 $params=$param->getElementsByTagName("param");
65 $param_value=array();
66 foreach ($param->childNodes as $param) {
67 if ($param->nodeType == XML_ELEMENT_NODE)
68 {
69 array_push($param_value, getParam($param));
70 }
71 }
72 }
73 return $param_value;
74 }
75
76 function getInput($master, $param) {
77 $out='';
78
79 $lang=$GLOBALS['lang'];
80 $name=$param->getAttribute("id");
81 $conf=$_GET[$name];
82 $GLOBALS[$name]=$conf;
83 if (!$conf)
84 $conf=$param->getAttribute("default");
85 $config=loadFile("${lang}/${conf}.xml");
86
87 $confFile="${lang}/${conf}.xml";
88 if (!file_exists($confFile)) {
89 errorPage("Resource not available");
90 }
91 $doc = new DOMDocument();
92 $doc->load($confFile);
50 93
51 $includes=$doc->getElementsByTagName("include"); 94 $includes=$doc->getElementsByTagName("include");
52 $recurse=0; 95 $recurse=0;
53 96
54 while($includes->length>0) { 97 while($includes->length>0) {
55 if(++$recurse>MAX_RECURSE) { 98 if(++$recurse>MAX_RECURSE) {
56 errorPage('Recursion limit exceeded', 500); 99 errorPage('Recursion limit exceeded', 500);
57 } 100 }
58 foreach ($includes as $include) { 101 foreach ($includes as $include) {
59 $src=$include->getAttribute("src"); 102 $src=$include->getAttribute("src");
60 $subdoc = new DOMDocument(); 103 $subdoc = new DOMDocument();
61 $subdoc->load("${lang}/${src}"); 104 $subdoc->load("${lang}/${src}");
62 $parent=$include->parentNode; 105 $parent=$include->parentNode;
63 $xml=getElementByTagName($subdoc,"xml"); 106 $xml=getElementByTagName($subdoc,"xml");
65 $text=$subdoc->saveXml($child); 108 $text=$subdoc->saveXml($child);
66 $clonedChild=$doc->importNode($child,true); 109 $clonedChild=$doc->importNode($child,true);
67 $parent->insertBefore($clonedChild,$include); 110 $parent->insertBefore($clonedChild,$include);
68 } 111 }
69 $parent->removeChild($include); 112 $parent->removeChild($include);
70 } 113 }
71 $includes=$doc->getElementsByTagName("include"); 114 $includes=$doc->getElementsByTagName("include");
72 } 115 }
73 116
74 $head=getElementByTagName($doc,"head"); 117 $head=getElementByTagName($doc,"head");
75 $title=$head->getAttribute("title"); 118 $title=$head->getAttribute("title");
76 119
77 $css=getElementByTagName($head,"css"); 120 if($title) {
78 $css=$doc->saveXML($css); 121 $values=$master->getElementsByTagName("param");
79 $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css); 122 foreach ($values as $value) {
80 123 if ($value->getAttribute("type")=="input_config") {
81 $body=getElementByTagName($doc,"body"); 124 if ($value->getAttribute("id")=="title") {
82 $files=$body->getElementsByTagName("file"); 125 $tmp = new DOMDocument();
83 126 $tmp->loadXml("<xml>${title}</xml>");
84 127 $parent=$value->parentNode;
85 $out= '<?xml version="1.0" encoding="UTF-8"?>'; 128 $parent->removeChild($value);
86 129 $parent->appendChild(new DOMText($tmp->textContent));
87 $out.= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 130 }
88 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 131 }
89 <html xmlns="http://www.w3.org/1999/xhtml"> 132 }
90 <head> 133 }
91 <title>'; 134
92 135 $css=getElementByTagName($head,"css");
93 $out.= "$title"; 136 $css=$doc->saveXML($css);
94 $out.= ' 137 $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
95 </title> 138
96 '; 139 if($css) {
97 $out.= "$css"; 140 $values=$master->getElementsByTagName("param");
98 $out.= ' 141 foreach ($values as $value) {
99 </head> 142 if ($value->getAttribute("type")=="input_config") {
100 <body> 143 if ($value->getAttribute("id")=="css") {
101 <div id="container"> 144 $tmp = new DOMDocument();
102 '; 145 $tmp->loadXml("<xml>${css}</xml>");
103 146 $parent=$value->parentNode;
104 function getParam($param) { 147 foreach($tmp->firstChild->childNodes as $node) {
105 $param_type=$param->getAttribute("type"); 148 $clonedChild=$master->importNode($node,true);
106 $param_value; 149 $parent->insertBefore($clonedChild,$value);
107 if (!$param_type) 150 }
108 $param_type="scalar"; 151 $parent->removeChild($value);
109 152 }
110 if($param_type == "scalar") { 153 }
111 $param_subst=$param->getAttribute("subst"); 154 }
112 $param_value=$param->getAttribute("value"); 155 }
113 if ($param_subst) { 156
114 /* 157
115 $param_value=preg_replace("/name/", $name, $param_subst); 158 $body=getElementByTagName($doc,"body");
116 $param_value=preg_replace('/lang/', $lang, $param_value); 159 $files=$body->getElementsByTagName("file");
117 */
118 }
119 }
120 elseif($param_type == "array") {
121 $params=$param->getElementsByTagName("param");
122 $param_value=array();
123 foreach ($param->childNodes as $param) {
124 if ($param->nodeType == XML_ELEMENT_NODE)
125 {
126 array_push($param_value, getParam($param));
127 }
128 }
129 }
130 return $param_value;
131 }
132 160
133 foreach ($files as $file) { 161 foreach ($files as $file) {
134 $src=$file->getAttribute("src"); 162 $src=$file->getAttribute("src");
135 $file_content=loadFile("${lang}/${src}"); 163 $file_content=loadFile("${lang}/${src}");
136 if(floatval($file_content)<0) { 164 if(floatval($file_content)<0) {
156 eval($callString); 184 eval($callString);
157 } 185 }
158 $out.= $file_content; 186 $out.= $file_content;
159 } 187 }
160 188
161 $out.=' 189 $doc = new DOMDocument();
162 </div> 190 $doc->loadXml("<xml>${out}</xml>");
163 </body> 191
164 </html> 192 return $doc;
165 '; 193 }
166 194
167 print $out; 195 $master = new DOMDocument();
196 $master->load("master.xml");
197
198 $params=$master->getElementsByTagName("param");
199 foreach ($params as $param) {
200 if ($param->getAttribute("type") == "input") {
201 $doc=getInput($master,$param);
202
203 $parent=$param->parentNode;
204 foreach ($doc->firstChild->childNodes as $child) {
205 $clonedChild=$master->importNode($child,true);
206 $parent->insertBefore($clonedChild,$param);
207 }
208 $parent->removeChild($param);
209
210 }
211 }
212
213 print $master->saveXml($master);
214
168 ?> 215 ?>