Rev Author Line No. Line
4988 kaklik 1 <?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 $vars['version'] = $version; // Set WebSVN version for all template files
26 $vars['currentyear'] = date('Y');
27  
28 $ignore = false;
29  
30 // Stack of previous test results
31 $ignorestack = array();
32  
33 // Number of test levels currently ignored
34 $ignorelevel = 0;
35  
36 // parseCommand
37 //
38 // Parse a special command
39  
40 function parseCommand($line, $vars, $handle) {
41 global $ignore, $ignorestack, $ignorelevel, $config, $listing, $vars;
42  
43 // process content of included file
44 if (strncmp(trim($line), '[websvn-include:', 16) == 0) {
45 if (!$ignore) {
46 $line = trim($line);
47 $file = substr($line, 16, -1);
48 parseTemplate($file);
49 }
50 return true;
51 }
52  
53  
54 // Check for test conditions
55 if (strncmp(trim($line), '[websvn-test:', 13) == 0) {
56 if (!$ignore) {
57 $line = trim($line);
58 $var = substr($line, 13, -1);
59 $neg = ($var[0] == '!');
60 if ($neg) $var = substr($var, 1);
61 if (empty($vars[$var]) ^ $neg) {
62 array_push($ignorestack, $ignore);
63 $ignore = true;
64 }
65 } else {
66 $ignorelevel++;
67 }
68 return true;
69 }
70  
71 if (strncmp(trim($line), '[websvn-else]', 13) == 0) {
72 if ($ignorelevel == 0) {
73 $ignore = !$ignore;
74 }
75 return true;
76 }
77  
78 if (strncmp(trim($line), '[websvn-endtest]', 16) == 0) {
79 if ($ignorelevel > 0) {
80 $ignorelevel--;
81 } else {
82 $ignore = array_pop($ignorestack);
83 }
84 return true;
85 }
86  
87 if (strncmp(trim($line), '[websvn-getlisting]', 19) == 0) {
88 global $svnrep, $path, $rev, $peg;
89  
90 if (!$ignore) {
91 $svnrep->listFileContents($path, $rev, $peg);
92 }
93 return true;
94 }
95  
96 if (strncmp(trim($line), '[websvn-defineicons]', 19) == 0) {
97 global $icons;
98  
99 if (!isset($icons)) {
100 $icons = array();
101 }
102  
103 // Read all the lines until we reach the end of the definition, storing
104 // each one...
105  
106 if (!$ignore) {
107 while (!feof($handle)) {
108 $line = trim(fgets($handle));
109 if (strncmp($line, '[websvn-enddefineicons]', 22) == 0) {
110 return true;
111 }
112 $eqsign = strpos($line, '=');
113 $match = substr($line, 0, $eqsign);
114 $def = substr($line, $eqsign + 1);
115 $icons[$match] = $def;
116 }
117 }
118 return true;
119 }
120  
121 if (strncmp(trim($line), '[websvn-icon]', 13) == 0) {
122 global $icons, $vars;
123  
124 if (!$ignore) {
125 // The current filetype should be defined my $vars['filetype']
126 if (!empty($icons[$vars['filetype']])) {
127 echo parseTags($icons[$vars['filetype']], $vars);
128 } else if (!empty($icons['*'])) {
129 echo parseTags($icons['*'], $vars);
130 }
131 }
132  
133 return true;
134 }
135  
136 if (strncmp(trim($line), '[websvn-treenode]', 17) == 0) {
137 global $icons, $vars;
138  
139 if (!$ignore) {
140 if ((!empty($icons['i-node'])) && (!empty($icons['t-node'])) && (!empty($icons['l-node']))) {
141 for ($n = 1; $n < $vars['level']; $n++) {
142 if ($vars['last_i_node'][$n]) {
143 echo parseTags($icons['e-node'], $vars);
144 } else {
145 echo parseTags($icons['i-node'], $vars);
146 }
147 }
148  
149 if ($vars['level'] != 0) {
150 if ($vars['node'] == 0) {
151 echo parseTags($icons['t-node'], $vars);
152 } else {
153 echo parseTags($icons['l-node'], $vars);
154 $vars['last_i_node'][$vars['level']] = true;
155 }
156 }
157 }
158 }
159 return true;
160 }
161 return false;
162 }
163  
164 // parseTemplate
165 //
166 // Parse the given template, replacing the variables with the values passed
167  
168 function parseTemplate($file) {
169 global $ignore, $rep, $config, $vars, $listing;
170  
171 $template = (($rep) ? $rep->getTemplatePath() : $config->getTemplatePath()).$file;
172 if (!is_file($template)) {
173 print 'No template file found ('.$template.')';
174 exit;
175 }
176  
177 $handle = fopen($template, 'r');
178 $inListing = false;
179 $ignore = false;
180 $listLines = array();
181  
182 while (!feof($handle)) {
183 $line = fgets($handle);
184 // Check for the end of the file list
185 if ($inListing) {
186 if (strcmp(trim($line), '[websvn-endlisting]') == 0) {
187 $inListing = false;
188  
189 // For each item in the list
190 foreach ($listing as $listvars) {
191 // Copy the value for this list item into the $vars array
192 foreach ($listvars as $id => $value) {
193 $vars[$id] = $value;
194 }
195 // Output the list item
196 foreach ($listLines as $line) {
197 if (!parseCommand($line, $vars, $handle) && !$ignore) {
198 print parseTags($line, $vars);
199 }
200 }
201 }
202 } else if ($ignore == false) {
203 $listLines[] = $line;
204 }
205 } else if (parseCommand($line, $vars, $handle)) {
206 continue;
207 } else {
208 // Check for the start of the file list
209 if (strncmp(trim($line), '[websvn-startlisting]', 21) == 0) {
210 $inListing = true;
211 } else {
212 if ($ignore == false) {
213 print parseTags($line, $vars);
214 }
215 }
216 }
217 }
218 fclose($handle);
219 }
220  
221 // parseTags
222 //
223 // Replace all occurences of [websvn:varname] with the give variable
224  
225 function parseTags($line, $vars) {
226 global $lang;
227  
228 // Replace the language strings
229 while (preg_match('|\[lang:([a-zA-Z0-9_]+)\]|', $line, $matches)) {
230 // Make sure that the variable exists
231 if (!isset($lang[$matches[1]])) {
232 $lang[$matches[1]] = '?${matches[1]}?';
233 }
234 $line = str_replace($matches[0], $lang[$matches[1]], $line);
235 }
236  
237 $l = '';
238 // Replace the websvn variables
239 while (preg_match('|\[websvn:([a-zA-Z0-9_]+)\]|', $line, $matches)) {
240 // Find beginning
241 $p = strpos($line, $matches[0]);
242  
243 // add everything up to beginning
244 if ($p > 0) {
245 $l .= substr($line, 0, $p);
246 }
247  
248 // Replace variable (special token, if not exists)
249 $l .= isset($vars[$matches[1]]) ? $vars[$matches[1]]: ('?'.$matches[1].'?');
250  
251 // Remove allready processed part of line
252 $line = substr($line, $p + strlen($matches[0]));
253 }
254  
255 // Rebuild line, add remaining part of line
256 $line = $l.$line;
257  
258 return $line;
259 }
260  
261  
262 // renderTemplate
263 //
264 // Renders the templates for the given view
265  
266 function renderTemplate($view, $readmePath = null)
267 {
268  
269 global $config, $rep, $vars, $listing, $lang, $locwebsvnhttp;
270  
271 // Set the view in the templates variables
272 $vars['template'] = $view;
273  
274 // Check if we are using a PHP powered template or the standard one
275 $path = !empty($rep) ? $rep->getTemplatePath() : $config->getTemplatePath();
276 $path = $path . 'template.php';
277  
278 if (is_readable($path))
279 {
280 $vars['templateentrypoint'] = $path;
281 executePlainPhpTemplate($vars);
282 }
283 else
284 {
285 parseTemplate('header.tmpl');
286 flush(); // http://developer.yahoo.com/performance/rules.html#flush
287 parseTemplate($view . '.tmpl');
288  
289 if (($view === 'directory') || ($view === 'log'))
290 {
291 print '<script type="text/javascript" src="'.$locwebsvnhttp.'/javascript/compare-checkboxes.js"></script>';
292 }
293  
294 if ($readmePath == null)
295 {
296 parseTemplate('footer.tmpl');
297 return;
298 }
299  
300 $svnrep = new SVNRepository($rep);
301 $svnrep->listReadmeContents($readmePath);
302 parseTemplate('footer.tmpl');
303 }
304 }
305  
306 function executePlainPhpTemplate($vars) {
307 require_once $vars['templateentrypoint'];
308 }
309  
310 // {{{ renderTemplate404
311  
312 function renderTemplate404($view, $errorDetail="")
313 {
314 global $vars, $lang;
315 http_response_code(404);
316 $vars['error'] = "WebSVN 404 ".$lang[$errorDetail];
317 renderTemplate($view);
318 exit(0);
319 }
320  
321 // }}}