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