comparison index.php @ 4:74196528fc64

Refac to use xml as input, and remove filters and helper-functions from main script.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 19 May 2011 18:04:33 +0200
parents 238c5127b78c
children 18aafb1a8986
comparison
equal deleted inserted replaced
3:238c5127b78c 4:74196528fc64
1 <?php 1 <?php
2 include 'php/cache_check.inc'; 2 include 'php/cache_check.inc';
3 include 'php/accept-language.inc'; 3 include 'php/accept-language.inc';
4 4 include 'php/filters.inc';
5 function loadFile($sFilename, $sCharset = 'UTF-8') 5 include 'php/common-functions.inc';
6 {
7 if (floatval(phpversion()) >= 4.3) {
8 if (!file_exists($sFilename)) return -3;
9 $sData = file_get_contents($sFilename);
10 } else {
11 if (!file_exists($sFilename)) return -3;
12 $rHandle = fopen($sFilename, 'r');
13 if (!$rHandle) return -2;
14
15 $sData = '';
16 while(!feof($rHandle))
17 $sData .= fread($rHandle, filesize($sFilename));
18 fclose($rHandle);
19 }
20 if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) {
21 if ($sEncoding != 1) {
22 $sData = mb_convert_encoding($sData, $sCharset, $sEncoding);
23 }
24 }
25 return $sData;
26 }
27
28 6
29 $name = $_GET['name']; 7 $name = $_GET['name'];
30 $lang = $_GET['lang']; 8 $lang = $_GET['lang'];
31 9
32 if(!$name) { 10 if(!$name) {
42 } 20 }
43 } 21 }
44 } 22 }
45 23
46 $title="Dummy title"; 24 $title="Dummy title";
47 $header="header.html";
48 $footer="footer.html";
49 $body="body.html";
50 25
51 $config=loadFile("${lang}/${name}.cfg"); 26 $config=loadFile("${lang}/${name}.xml");
52 27
53 eval($config); 28 function getElementByTagName($obj, $name) {
54 $body_content=loadFile("${lang}/${body}"); 29 $elems=$obj->getElementsByTagName($name);
55 if(floatval($body_content)<0) { 30 if ($elems->length != 1) {
56 header('HTTP/1.0 404 Not Found'); 31 exit;
57 $body_content='<div id="page"><h1>Resource not found</h1></div>'; 32 }
33 $elem=$elems->item(0);
34 return $elem;
58 } 35 }
59 if (!file_exists($lang)) { 36
60 $lang=no; 37 $doc = new DOMDocument();
61 header('HTTP/1.0 404 Not Found'); 38 $doc->load("${lang}/${name}.xml");
62 $body_content='<div id="page"><h1>Language is not available</h1></div>'; 39
63 } 40 $head=getElementByTagName($doc,"head");
41 $title=$head->getAttribute("title");
42
43 $css=getElementByTagName($head,"css");
44 $css=$doc->saveXML($css);
45 $css=preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
46
47 $body=getElementByTagName($doc,"body");
48 $files=$body->getElementsByTagName("file");
64 49
65 print '<?xml version="1.0" encoding="UTF-8"?>'; 50 print '<?xml version="1.0" encoding="UTF-8"?>';
66 ?> 51 ?>
67 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 52 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
68 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 53 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
79 </head> 64 </head>
80 <body> 65 <body>
81 <div id="container"> 66 <div id="container">
82 67
83 <?php 68 <?php
84 $header_content=loadFile("${lang}/${header}"); 69 foreach ( $files as $file) {
85 $footer_content=loadFile("${lang}/${footer}"); 70 $src=$file->getAttribute("src");
86 71 $file_content=loadFile("${lang}/${src}");
87 $pattern = "/<li id=\"${name}\"\s?([^>]*)>/is"; 72 // print $file_content;
88 $replacement = "<li id=\"${name}\" class=\"active\" $1>"; 73 $filters=$file->getElementsByTagName("filter");
89 $header_content= preg_replace($pattern, $replacement, $header_content); 74 foreach($filters as $filter) {
90 75 $func=$filter->getAttribute("function");
91 $pattern = '/<li id="([^"]+)"\s?([^>]*)>(.*?)<\/li>/is'; 76 $params=$filter->getElementsByTagName("param");
92 $replacement = "<li id=\"\$1\" \$2><a href=\"?name=$1&lang=${lang}\">\$3</a></li>"; 77 $callString="\$file_content=${func}(\$file_content";
93 $header_content=preg_replace($pattern, $replacement, $header_content); 78 foreach ($params as $param) {
94 79 $param_subst=$param->getAttribute("subst");
95 $languages = array("no","en"); 80 $param_value=$param->getAttribute("value");
96 $langbar='<ul id="language-select">'; 81 if ($param_subst) {
97 82 $param_value=preg_replace("/name/", $name, $param_subst);
98 foreach($languages as $l) { 83 $param_value=preg_replace('/lang/', $lang, $param_value);
99 $active=($l == $lang)?1:0; 84 }
100 $langbar.= " 85 $callString.=",\"${param_value}\"";
101 <li class=\"norwegian\"> 86 }
102 <img src=\"http://dev.bfginvest.no/php/flag.php?lang=${l}&active=${active}\" width=\"20\" height=\"16\" alt=\"Norsk versjon - inaktiv\" title=\"Norsk\"/> 87 $callString.=");";
103 </li> 88 eval($callString);
104 "; 89 }
105 90 print $file_content;
106 } 91 }
107 $langbar.='</ul>'; 92
108 $pattern = '/<ul id="language-select"\/>/'; 93 /*
109 $replacement = $langbar; 94 $body_content=loadFile("${lang}/${body}");
110 $header_content=preg_replace($pattern, $replacement, $header_content); 95 if(floatval($body_content)<0) {
111 96 header('HTTP/1.0 404 Not Found');
112 print $header_content; 97 $body_content='<div id="page"><h1>Resource not found</h1></div>';
113 print $body_content; 98 }
114 print $footer_content; 99 if (!file_exists($lang)) {
115 100 $lang=no;
101 header('HTTP/1.0 404 Not Found');
102 $body_content='<div id="page"><h1>Language is not available</h1></div>';
103 }
104 */
116 105
117 ?> 106 ?>
118 107
119 </div> 108 </div>
120 </body> 109 </body>