1 | 1 | simandl | <?php |
2 | | | // WebSVN - Subversion repository viewing via the web using PHP |
3 | | | // Copyright (C) 2004-2006 Tim Armes |
4 | | | // |
5 | | | // This program is free software; you can redistribute it and/or modify |
6 | | | // it under the terms of the GNU General Public License as published by |
7 | | | // the Free Software Foundation; either version 2 of the License, or |
8 | | | // (at your option) any later version. |
9 | | | // |
10 | | | // This program is distributed in the hope that it will be useful, |
11 | | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | | // GNU General Public License for more details. |
14 | | | // |
15 | | | // You should have received a copy of the GNU General Public License |
16 | | | // along with this program; if not, write to the Free Software |
17 | | | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | | | // |
19 | | | // -- |
20 | | | // |
21 | | | // templates.php |
22 | | | // |
23 | | | // Templating system to allow advanced page customisation |
24 | | | |
25 | | | $ignore = false; |
26 | | | |
27 | | | // Stack of previous test results |
28 | | | $ignorestack = array(); |
29 | | | |
30 | | | // Number of test levels currently ignored |
31 | | | $ignorelevel = 0; |
32 | | | |
33 | | | // parseCommand |
34 | | | // |
35 | | | // Parse a special command |
36 | | | |
37 | | | function parseCommand($line, $vars, $handle) { |
38 | 3 | simandl | global $ignore, $ignorestack, $ignorelevel, $config, $listing, $vars; |
39 | 1 | simandl | |
40 | 3 | simandl | // process content of included file |
41 | | | if (strncmp(trim($line), "[websvn-include:", 16) == 0) { |
42 | | | if (!$ignore) { |
43 | | | $line = trim($line); |
44 | | | $file = substr($line, 16, -1); |
45 | | | parseTemplate($config->templatePath.$file, $vars, $listing); |
46 | | | } |
47 | | | return true; |
48 | | | } |
49 | | | |
50 | | | |
51 | 1 | simandl | // Check for test conditions |
52 | | | if (strncmp(trim($line), "[websvn-test:", 13) == 0) { |
53 | | | if (!$ignore) { |
54 | | | $line = trim($line); |
55 | | | $var = substr($line, 13, -1); |
56 | 3 | simandl | $neg = ($var{0} == '!'); |
57 | | | if ($neg) $var = substr($var, 1); |
58 | | | if (empty($vars[$var]) xor $neg) { |
59 | 1 | simandl | array_push($ignorestack, $ignore); |
60 | | | $ignore = true; |
61 | | | } |
62 | | | } else { |
63 | | | $ignorelevel++; |
64 | | | } |
65 | | | |
66 | | | return true; |
67 | | | } |
68 | | | |
69 | | | if (strncmp(trim($line), "[websvn-else]", 13) == 0) { |
70 | | | if ($ignorelevel == 0) { |
71 | | | $ignore = !$ignore; |
72 | | | } |
73 | | | |
74 | | | return true; |
75 | | | } |
76 | | | |
77 | | | if (strncmp(trim($line), "[websvn-endtest]", 16) == 0) { |
78 | | | if ($ignorelevel > 0) { |
79 | | | $ignorelevel--; |
80 | | | } else { |
81 | | | $ignore = array_pop($ignorestack); |
82 | | | } |
83 | | | |
84 | | | return true; |
85 | | | } |
86 | | | |
87 | | | if (strncmp(trim($line), "[websvn-getlisting]", 19) == 0) { |
88 | | | global $path, $rev, $svnrep; |
89 | | | |
90 | | | if (!$ignore) { |
91 | | | $svnrep->listFileContents($path, $rev); |
92 | | | } |
93 | | | |
94 | | | return true; |
95 | | | } |
96 | | | |
97 | | | if (strncmp(trim($line), "[websvn-defineicons]", 19) == 0) { |
98 | | | global $icons; |
99 | | | |
100 | | | if (!isset($icons)) { |
101 | | | $icons = array(); |
102 | | | } |
103 | | | |
104 | | | // Read all the lines until we reach the end of the definition, storing |
105 | | | // each one... |
106 | | | |
107 | | | if (!$ignore) { |
108 | | | while (!feof($handle)) { |
109 | | | $line = trim(fgets($handle)); |
110 | | | |
111 | | | if (strncmp($line, "[websvn-enddefineicons]", 22) == 0) { |
112 | | | return true; |
113 | | | } |
114 | | | |
115 | | | $eqsign = strpos($line, "="); |
116 | | | |
117 | | | $match = substr($line, 0, $eqsign); |
118 | | | $def = substr($line, $eqsign + 1); |
119 | | | |
120 | | | $icons[$match] = $def; |
121 | | | } |
122 | | | } |
123 | | | |
124 | | | return true; |
125 | | | } |
126 | | | |
127 | | | if (strncmp(trim($line), "[websvn-icon]", 13) == 0) { |
128 | | | global $icons, $vars; |
129 | | | |
130 | | | if (!$ignore) { |
131 | | | // The current filetype should be defined my $vars["filetype"] |
132 | | | |
133 | | | if (!empty($icons[$vars["filetype"]])) { |
134 | | | echo parseTags($icons[$vars["filetype"]], $vars); |
135 | | | } else if (!empty($icons["*"])) { |
136 | | | echo parseTags($icons["*"], $vars); |
137 | | | } |
138 | | | } |
139 | | | |
140 | | | return true; |
141 | | | } |
142 | | | |
143 | | | if (strncmp(trim($line), "[websvn-treenode]", 17) == 0) { |
144 | | | global $icons, $vars; |
145 | | | |
146 | | | if (!$ignore) { |
147 | | | if ((!empty($icons["i-node"])) && (!empty($icons["t-node"])) && (!empty($icons["l-node"]))) { |
148 | | | for ($n = 1; $n < $vars["level"]; $n++) { |
149 | | | if ($vars["last_i_node"][$n]) { |
150 | | | echo parseTags($icons["e-node"], $vars); |
151 | | | } else { |
152 | | | echo parseTags($icons["i-node"], $vars); |
153 | | | } |
154 | | | } |
155 | | | |
156 | | | if ($vars["level"] != 0) { |
157 | | | if ($vars["node"] == 0) { |
158 | | | echo parseTags($icons["t-node"], $vars); |
159 | | | } else { |
160 | | | echo parseTags($icons["l-node"], $vars); |
161 | | | $vars["last_i_node"][$vars["level"]] = TRUE; |
162 | | | } |
163 | | | } |
164 | | | } |
165 | | | } |
166 | | | |
167 | | | return true; |
168 | | | } |
169 | | | |
170 | | | return false; |
171 | | | } |
172 | | | |
173 | | | // parseTemplate |
174 | | | // |
175 | | | // Parse the given template, replacing the variables with the values passed |
176 | | | |
177 | | | function parseTemplate($template, $vars, $listing) { |
178 | | | global $ignore, $vars; |
179 | | | |
180 | | | if (!is_file($template)) { |
181 | | | print"No template file found ($template)"; |
182 | | | exit; |
183 | | | } |
184 | | | |
185 | | | $handle = fopen($template, "r"); |
186 | | | $inListing = false; |
187 | | | $ignore = false; |
188 | | | $listLines = array(); |
189 | | | |
190 | | | while (!feof($handle)) { |
191 | | | $line = fgets($handle); |
192 | | | |
193 | | | // Check for the end of the file list |
194 | | | if ($inListing) { |
195 | | | if (strcmp(trim($line), "[websvn-endlisting]") == 0) { |
196 | | | $inListing = false; |
197 | | | |
198 | | | // For each item in the list |
199 | | | foreach ($listing as $listvars) { |
200 | | | // Copy the value for this list item into the $vars array |
201 | | | foreach ($listvars as $id => $value) { |
202 | | | $vars[$id] = $value; |
203 | | | } |
204 | | | |
205 | | | // Output the list item |
206 | | | foreach ($listLines as $line) { |
207 | | | if (!parseCommand($line, $vars, $handle)) { |
208 | | | if (!$ignore) { |
209 | | | print parseTags($line, $vars); |
210 | | | } |
211 | | | } |
212 | | | } |
213 | | | } |
214 | | | } else { |
215 | | | if ($ignore == false) { |
216 | | | $listLines[] = $line; |
217 | | | } |
218 | | | } |
219 | | | } else if (parseCommand($line, $vars, $handle)) { |
220 | | | continue; |
221 | | | } else { |
222 | | | // Check for the start of the file list |
223 | | | if (strncmp(trim($line), "[websvn-startlisting]", 21) == 0) { |
224 | | | $inListing = true; |
225 | | | } else { |
226 | | | if ($ignore == false) { |
227 | | | print parseTags($line, $vars); |
228 | | | } |
229 | | | } |
230 | | | } |
231 | | | } |
232 | | | |
233 | | | fclose($handle); |
234 | | | } |
235 | | | |
236 | | | // parseTags |
237 | | | // |
238 | | | // Replace all occurences of [websvn:varname] with the give variable |
239 | | | |
240 | | | function parseTags($line, $vars) { |
241 | | | global $lang; |
242 | | | |
243 | | | $l = ''; |
244 | | | // Replace the websvn variables |
245 | 3 | simandl | while (preg_match('|\[websvn:([a-zA-Z0-9_]+)\]|', $line, $matches)) { |
246 | 1 | simandl | // Find beginning |
247 | | | $p = strpos($line, $matches[0]); |
248 | | | |
249 | | | // add everything up to beginning |
250 | | | if ($p > 0) { |
251 | | | $l .= substr($line, 0, $p); |
252 | | | } |
253 | | | |
254 | | | // Replace variable (special token, if not exists) |
255 | | | $l .= isset($vars[$matches[1]]) ? $vars[$matches[1]]: ('?'.$matches[1].'?'); |
256 | | | |
257 | | | // Remove allready processed part of line |
258 | | | $line = substr($line, $p + strlen($matches[0])); |
259 | | | } |
260 | | | |
261 | | | // Rebuild line, add remaining part of line |
262 | | | $line = $l.$line; |
263 | | | |
264 | | | // Replace the language strings |
265 | 3 | simandl | while (preg_match('|\[lang:([a-zA-Z0-9_]+)\]|', $line, $matches)) { |
266 | 1 | simandl | // Make sure that the variable exists |
267 | | | if (!isset($lang[$matches[1]])) { |
268 | | | $lang[$matches[1]] = "?${matches[1]}?"; |
269 | | | } |
270 | | | |
271 | | | $line = str_replace($matches[0], $lang[$matches[1]], $line); |
272 | | | } |
273 | | | |
274 | | | // Return the results |
275 | | | return $line; |
276 | | | } |