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