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