comparison inputParser.inc @ 23:814296ea84a9

Move functionality into inputParser.inc
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Wed, 19 Sep 2012 18:23:10 +0200
parents
children d8c7b328899e
comparison
equal deleted inserted replaced
22:ccfddd7fba1b 23:814296ea84a9
1 <?php
2 function getParam($param)
3 {
4 $param_type=$param->getAttribute("type");
5 $param_value;
6 if (!$param_type)
7 $param_type="scalar";
8
9 if($param_type == "scalar") {
10 $param_subst=$param->getAttribute("subst");
11 $param_value=$param->getAttribute("value");
12 if ($param_subst) {
13 /*
14 $param_value=preg_replace("/name/", $name, $param_subst);
15 $param_value=preg_replace('/lang/', $lang, $param_value);
16 */
17 }
18 }
19 elseif($param_type == "array") {
20 $params=$param->getElementsByTagName("param");
21 $param_value=array();
22 foreach ($param->childNodes as $param) {
23 if ($param->nodeType == XML_ELEMENT_NODE)
24 {
25 array_push($param_value, getParam($param));
26 }
27 }
28 }
29 return $param_value;
30 }
31
32 function getInput($master, $param)
33 {
34 $out='';
35
36 $lang=$GLOBALS['lang'];
37 $name=$param->getAttribute("id");
38 $conf=$_GET[$name];
39 $GLOBALS[$name]=$conf;
40 if (!$conf)
41 $conf=$param->getAttribute("default");
42
43 $fname = "${lang}/${conf}.xml";
44 cache_time($fname);
45 $config = loadFile($fname);
46
47 $confFile="${lang}/${conf}.xml";
48 if (!file_exists($confFile)) {
49 errorPage("Resource not available");
50 }
51 $doc = new DOMDocument();
52 $doc->load($confFile);
53
54 $includes=$doc->getElementsByTagName("include");
55 $recurse=0;
56
57 while($includes->length>0) {
58 if(++$recurse>MAX_RECURSE) {
59 errorPage('Recursion limit exceeded', 500);
60 }
61 foreach ($includes as $include) {
62 $src=$include->getAttribute("src");
63 $subdoc = new DOMDocument();
64 $subdoc->load("${lang}/${src}");
65 $parent=$include->parentNode;
66 $xml=getElementByTagName($subdoc,"xml");
67 foreach($xml->childNodes as $child) {
68 $text=$subdoc->saveXml($child);
69 $clonedChild=$doc->importNode($child,true);
70 $parent->insertBefore($clonedChild,$include);
71 }
72 $parent->removeChild($include);
73 }
74 $includes=$doc->getElementsByTagName("include");
75 }
76
77 $head=getElementByTagName($doc,"head");
78 $title=$head->getAttribute("title");
79
80 if($title) {
81 $values=$master->getElementsByTagName("param");
82 foreach ($values as $value) {
83 if ($value->getAttribute("type")=="input_config") {
84 if ($value->getAttribute("id")=="title") {
85 $tmp = new DOMDocument();
86 $tmp->loadXml("<xml>${title}</xml>");
87 $parent=$value->parentNode;
88 $parent->removeChild($value);
89 $parent->appendChild(new DOMText($tmp->textContent));
90 }
91 }
92 }
93 }
94
95 $css=getElementByTagName($head,"css");
96 $css=$doc->saveXML($css);
97 $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
98
99 if($css) {
100 $values=$master->getElementsByTagName("param");
101 foreach ($values as $value) {
102 if ($value->getAttribute("type")=="input_config") {
103 if ($value->getAttribute("id")=="css") {
104 $tmp = new DOMDocument();
105 $tmp->loadXml("<xml>${css}</xml>");
106 $parent=$value->parentNode;
107 foreach($tmp->firstChild->childNodes as $node) {
108 $clonedChild=$master->importNode($node,true);
109 $parent->insertBefore($clonedChild,$value);
110 }
111 $parent->removeChild($value);
112 }
113 }
114 }
115 }
116
117
118 $body=getElementByTagName($doc,"body");
119 $files=$body->getElementsByTagName("file");
120
121 foreach ($files as $file) {
122 $script=$file->getAttribute("script");
123 if ($script) {
124 $cacheable = false;
125 $src="";
126 $cwd=getcwd();
127
128 $matches=array();
129 preg_match('/(.*\/)/', $script, $matches);
130 $dirname=$matches[0];
131 preg_match('/([^\/]*)$/', $script, $matches);
132 $filename=$matches[0];
133 chdir("${lang}/${dirname}");
134 $pipe=popen("php ${filename}","r");
135 $file_content = stream_get_contents($pipe);
136 chdir("${cwd}");
137 }
138 else {
139 $src = $file->getAttribute("src");
140 $fname = "${lang}/${src}";
141 cache_time($fname);
142 $file_content = loadFile($fname);
143 }
144 if(floatval($file_content)<0) {
145 errorPage("Resource not found '${lang}/${src}'");
146 }
147
148 $filters=$file->getElementsByTagName("filter");
149 foreach($filters as $filter) {
150 $func=$filter->getAttribute("function");
151 $params=$filter->getElementsByTagName("param");
152 $callString="\$file_content=${func}(\$file_content";
153 $param_values=array();
154 $i=0;
155 foreach ($filter->childNodes as $param) {
156 if ($param->nodeType == XML_ELEMENT_NODE)
157 {
158 $param_value[$i]=getParam($param);
159 $callString.=",\$param_value[$i]";
160 $i++;
161 }
162 }
163 $callString.=");";
164 eval($callString);
165 }
166 $out.= $file_content;
167 }
168
169 $doc = new DOMDocument();
170 $doc->loadXml("<xml>${out}</xml>");
171
172 return $doc;
173 }
174
175 ?>