comparison InputParser.inc @ 63:92c3e52c12d4

Moved inputParser to InputParser
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 20:26:27 +0200
parents inputParser.inc@b7efe2ecbc11
children 74f7b64bdb78
comparison
equal deleted inserted replaced
62:b7efe2ecbc11 63:92c3e52c12d4
1 <?php
2 /**
3 * @file
4 * Functionality for translating an XML document into a webpage
5 */
6 class InputParser {
7 private $options;
8 private $master;
9
10 function __construct($name, $masterCache) {
11 $this->master = new DOMDocument();
12 $cache = $masterCache;
13 $cache->cache_time($name);
14 $this->master->load($name);
15
16 $this->options = new Options($this->master);
17 $this->options->setCache($cache);
18 $this->options->setBasePath(basePath());
19 $this->options->setCacheable(true);
20
21 $this->options->setUrlParams(array('name', 'lang'));
22
23 if(array_key_exists('lang', $_GET) && $_GET['lang']) {
24 $this->options->setLang($_GET['lang']);
25 }
26 else {
27 $this->options->setLang($this->options->getDefaultLang());
28 }
29
30 if(array_key_exists('name', $_GET) && $_GET['name']) {
31 $this->options->setName($_GET['name']);
32 }
33
34 $params = $this->master->getElementsByTagName("param");
35
36 foreach ($params as $param) {
37 if ($param->getAttribute("type") == "input") {
38 $doc = self::getInput($this->master, $param, $this->options);
39
40 $parent = $param->parentNode;
41 foreach ($doc->firstChild->childNodes as $child) {
42 $clonedChild = $this->master->importNode($child, true);
43 $parent->insertBefore($clonedChild, $param);
44 }
45 $parent->removeChild($param);
46 }
47 }
48 $this->master = self::getFiles($this->master, $this->options);
49
50 }
51
52 function genPage()
53 {
54 if (CACHING && $this->options->getCacheable())
55 $this->options->getCache()->CheckHttpModified();
56
57 print $this->master->saveXml($this->master);
58 //print_r($cache->cacheSet(1));
59 }
60
61
62 function getParam($param)
63 {
64 $param_type=$param->getAttribute("type");
65 $param_value;
66 if (!$param_type)
67 $param_type="scalar";
68
69 if($param_type == "scalar") {
70 $param_subst=$param->getAttribute("subst");
71 $param_value=$param->getAttribute("value");
72 if ($param_subst) {
73 /*
74 $param_value=preg_replace("/name/", $name, $param_subst);
75 $param_value=preg_replace('/lang/', $lang, $param_value);
76 */
77 }
78 }
79 elseif($param_type == "array") {
80 $params=$param->getElementsByTagName("param");
81 $param_value=array();
82 foreach ($param->childNodes as $param) {
83 if ($param->nodeType == XML_ELEMENT_NODE)
84 {
85 array_push($param_value, self::getParam($param));
86 }
87 }
88 }
89 return $param_value;
90 }
91
92 function getFiles($doc, $options) {
93 $lang = $options->getLang();
94 $conf = $options->getName();
95
96 $toRemove = array();
97
98 $topLevelTags = $doc->getElementsByTagName("toplevel");
99 foreach ($topLevelTags as $topLevel) {
100 $topLevel->parentNode->removeChild($topLevel);
101 }
102
103 $valueDict = array();
104 $fragments = array();
105 $setters = $doc->getElementsByTagName("set");
106 foreach ($setters as $setTag) {
107 $key = $setTag->getAttribute("id");
108 $type = $setTag->getAttribute("type");
109 if ($type == "fragment") {
110 $fragments[$key] = $setTag;
111 }
112 else {
113 $value = $setTag->getAttribute("value");
114 if ($key && $value) {
115 $valueDict[$key] = $value;
116 }
117 }
118 //We need to iterate in the opposite direction when removing,
119 //so best shifting.
120 array_unshift($toRemove, $setTag);
121 }
122
123 $params = $doc->getElementsByTagName("param");
124 foreach ($params as $param) {
125 if ($param->getAttribute("type")=="input_config") {
126 $id = $param->getAttribute("id");
127 if (array_key_exists($id, $valueDict)) {
128 $value = $valueDict[$id];
129 $tmp = new DOMDocument();
130
131 $tmp->loadXml("<xml>${value}</xml>");
132 $parent = $param->parentNode;
133 $parent->insertBefore(new DOMText($tmp->textContent), $param);
134 //We need to iterate in the opposite direction when removing,
135 //so best shifting.
136 array_unshift($toRemove, $param);
137 }
138 elseif (array_key_exists($id, $fragments)) {
139 $fragment = $fragments[$id];
140
141 $cloneFragment = $fragment->cloneNode(true);
142 $insNode = $param;
143 for ($i = $cloneFragment->childNodes->length - 1; $i >= 0; $i--) {
144 $child = $cloneFragment->childNodes->item($i);
145 $child = $child->parentNode->removeChild($child);
146 $insNode = $insNode->parentNode->insertBefore($child, $insNode);
147 }
148
149 //We need to iterate in the opposite direction when removing,
150 //so best shifting.
151 array_unshift($toRemove, $param);
152 }
153 }
154 }
155
156 foreach($toRemove as $param) {
157 $parent = $param->parentNode;
158 $parent->removeChild($param);
159 }
160
161 $body = getElementByTagName($doc,"body");
162 $files = $body->getElementsByTagName("file");
163
164 $toRemove = array();
165
166 foreach ($files as $file) {
167 $script = $file->getAttribute("script");
168 if ($script) {
169 $options->setCacheable(false);
170 $src="";
171 $cwd = getcwd();
172
173 $matches=array();
174 preg_match('/(.*\/)/', $script, $matches);
175 $dirname=$matches[0];
176 preg_match('/([^\/]*)$/', $script, $matches);
177 $filename=$matches[0];
178 chdir("${lang}/${dirname}");
179 $pipe=popen("php ${filename}","r");
180 $file_content = stream_get_contents($pipe);
181 chdir("${cwd}");
182 }
183 else {
184 $src = $file->getAttribute("src");
185 $fname = $options->getBasePath() . "/${lang}/${src}";
186 $file_content = $options->getCache()->loadFile($fname);
187 }
188 if(floatval($file_content)<0) {
189 errorPage("Resource not found '${lang}/${src}'");
190 }
191
192 $filters = $file->getElementsByTagName("filter");
193 foreach($filters as $filter) {
194 $func = $filter->getAttribute("function");
195 $params = $filter->getElementsByTagName("param");
196 $callString = "\$file_content = ${func}(\$file_content, \$options";
197 $param_values = array();
198 $i = 0;
199 foreach ($filter->childNodes as $param) {
200 if ($param->nodeType == XML_ELEMENT_NODE)
201 {
202 $param_value[$i] = self::getParam($param);
203 $callString .= ",\$param_value[$i]";
204 $i++;
205 }
206 }
207 $callString .= ");";
208 eval($callString);
209 }
210 $ndoc = new DOMDocument();
211
212 $ndoc->loadXml("<xml>${file_content}</xml>");
213
214 $parent = $file->parentNode;
215 foreach ($ndoc->firstChild->childNodes as $child) {
216 $clonedChild = $doc->importNode($child, true);
217 $parent->insertBefore($clonedChild, $file);
218 }
219 //We need to iterate in the opposite direction when removing,
220 //so best shifting.
221 array_unshift($toRemove, $file);
222 }
223 foreach($toRemove as $param) {
224 $parent = $param->parentNode;
225 $parent->removeChild($param);
226 }
227
228 return $doc;
229 }
230
231 function getInput($master, $param, $options)
232 {
233 $lang = $options->getLang();
234 $name = $param->getAttribute("id");
235 $conf = $options->getName();
236 if (!$conf)
237 $conf = $param->getAttribute("default");
238
239 $confFile = $options->getBasePath() . "/${lang}/${conf}.xml";
240 $options->getCache()->cache_time($confFile);
241 $doc = new DOMDocument();
242 $doc->load($confFile);
243
244 $toplevel = $doc->getElementsByTagName("toplevel");
245
246 if(! $toplevel->length) {
247 errorPage("Resource '${conf}' is not available", 500);
248 }
249
250 $includes = $doc->getElementsByTagName("include");
251 $recurse = 0;
252
253 while($includes->length > 0) {
254 if(++$recurse > MAX_RECURSE) {
255 errorPage('Recursion limit exceeded', 500);
256 }
257 foreach ($includes as $include) {
258 $src = $include->getAttribute("src");
259 $subdoc = new DOMDocument();
260 $subfile = $options->getBasePath() . "/${lang}/${src}";
261 $subdoc->load("$subfile");
262 $options->getCache()->cache_time($subfile);
263 $parent = $include->parentNode;
264 $xml = getElementByTagName($subdoc,"xml");
265 foreach($xml->childNodes as $child) {
266 $text = $subdoc->saveXml($child);
267 $clonedChild = $doc->importNode($child,true);
268 $parent->insertBefore($clonedChild,$include);
269 }
270 $parent->removeChild($include);
271 }
272 $includes = $doc->getElementsByTagName("include");
273 }
274
275 return $doc;
276 }
277 }
278 ?>