1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * template.php |
---|
4 | * ------------------- |
---|
5 | * begin : Saturday, Feb 13, 2001 |
---|
6 | * copyright : (C) 2001 The phpBB Group |
---|
7 | * email : support@phpbb.com |
---|
8 | * |
---|
9 | * $Id: template.php 351 2004-02-07 11:50:26Z gweltas $ |
---|
10 | * |
---|
11 | * |
---|
12 | ***************************************************************************/ |
---|
13 | |
---|
14 | /*************************************************************************** |
---|
15 | * |
---|
16 | * This program is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | ***************************************************************************/ |
---|
22 | |
---|
23 | /** |
---|
24 | * Template class. By Nathan Codding of the phpBB group. |
---|
25 | * The interface was originally inspired by PHPLib templates, |
---|
26 | * and the template file formats are quite similar. |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | class Template { |
---|
31 | var $classname = "Template"; |
---|
32 | |
---|
33 | // variable that holds all the data we'll be substituting into |
---|
34 | // the compiled templates. |
---|
35 | // ... |
---|
36 | // This will end up being a multi-dimensional array like this: |
---|
37 | // $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value |
---|
38 | // if it's a root-level variable, it'll be like this: |
---|
39 | // $this->_tpldata[.][0][varname] == value |
---|
40 | var $_tpldata = array(); |
---|
41 | |
---|
42 | // Hash of filenames for each template handle. |
---|
43 | var $files = array(); |
---|
44 | |
---|
45 | // Root template directory. |
---|
46 | var $root = ""; |
---|
47 | |
---|
48 | // this will hash handle names to the compiled code for that handle. |
---|
49 | var $compiled_code = array(); |
---|
50 | |
---|
51 | // This will hold the uncompiled code for that handle. |
---|
52 | var $uncompiled_code = array(); |
---|
53 | |
---|
54 | /** |
---|
55 | * Constructor. Simply sets the root dir. |
---|
56 | * |
---|
57 | */ |
---|
58 | function Template($root = ".") |
---|
59 | { |
---|
60 | $this->set_rootdir($root); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Destroys this template object. Should be called when you're done with it, in order |
---|
65 | * to clear out the template data so you can load/parse a new template set. |
---|
66 | */ |
---|
67 | function destroy() |
---|
68 | { |
---|
69 | $this->_tpldata = array(); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Sets the template root directory for this Template object. |
---|
74 | */ |
---|
75 | function set_rootdir($dir) |
---|
76 | { |
---|
77 | if (!is_dir($dir)) |
---|
78 | { |
---|
79 | return false; |
---|
80 | } |
---|
81 | |
---|
82 | $this->root = $dir; |
---|
83 | return true; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Sets the template filenames for handles. $filename_array |
---|
88 | * should be a hash of handle => filename pairs. |
---|
89 | */ |
---|
90 | function set_filenames($filename_array) |
---|
91 | { |
---|
92 | if (!is_array($filename_array)) |
---|
93 | { |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | reset($filename_array); |
---|
98 | while(list($handle, $filename) = each($filename_array)) |
---|
99 | { |
---|
100 | $this->files[$handle] = $this->make_filename($filename); |
---|
101 | } |
---|
102 | |
---|
103 | return true; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * Load the file for the handle, compile the file, |
---|
109 | * and run the compiled code. This will print out |
---|
110 | * the results of executing the template. |
---|
111 | */ |
---|
112 | function pparse($handle) |
---|
113 | { |
---|
114 | if (!$this->loadfile($handle)) |
---|
115 | { |
---|
116 | die("Template->pparse(): Couldn't load template file for handle $handle"); |
---|
117 | } |
---|
118 | |
---|
119 | // actually compile the template now. |
---|
120 | if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle])) |
---|
121 | { |
---|
122 | // Actually compile the code now. |
---|
123 | $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]); |
---|
124 | } |
---|
125 | |
---|
126 | // Run the compiled code. |
---|
127 | //echo ("<!-- ".$this->compiled_code[$handle]." -->"); |
---|
128 | eval($this->compiled_code[$handle]); |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Inserts the uncompiled code for $handle as the |
---|
134 | * value of $varname in the root-level. This can be used |
---|
135 | * to effectively include a template in the middle of another |
---|
136 | * template. |
---|
137 | * Note that all desired assignments to the variables in $handle should be done |
---|
138 | * BEFORE calling this function. |
---|
139 | */ |
---|
140 | function assign_var_from_handle($varname, $handle) |
---|
141 | { |
---|
142 | if (!$this->loadfile($handle)) |
---|
143 | { |
---|
144 | die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle"); |
---|
145 | } |
---|
146 | |
---|
147 | // Compile it, with the "no echo statements" option on. |
---|
148 | $_str = ""; |
---|
149 | $code = $this->compile($this->uncompiled_code[$handle], true, '_str'); |
---|
150 | |
---|
151 | // evaluate the variable assignment. |
---|
152 | eval($code); |
---|
153 | // assign the value of the generated variable to the given varname. |
---|
154 | $this->assign_var($varname, $_str); |
---|
155 | |
---|
156 | return true; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Block-level variable assignment. Adds a new block iteration with the given |
---|
161 | * variable assignments. Note that this should only be called once per block |
---|
162 | * iteration. |
---|
163 | */ |
---|
164 | function assign_block_vars($blockname, $vararray) |
---|
165 | { |
---|
166 | if (strstr($blockname, '.')) |
---|
167 | { |
---|
168 | // Nested block. |
---|
169 | $blocks = explode('.', $blockname); |
---|
170 | $blockcount = sizeof($blocks) - 1; |
---|
171 | $str = '$this->_tpldata'; |
---|
172 | for ($i = 0; $i < $blockcount; $i++) |
---|
173 | { |
---|
174 | $str .= '[\'' . $blocks[$i] . '.\']'; |
---|
175 | eval('$lastiteration = sizeof(' . $str . ') - 1;'); |
---|
176 | $str .= '[' . $lastiteration . ']'; |
---|
177 | } |
---|
178 | // Now we add the block that we're actually assigning to. |
---|
179 | // We're adding a new iteration to this block with the given |
---|
180 | // variable assignments. |
---|
181 | $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;'; |
---|
182 | |
---|
183 | // Now we evaluate this assignment we've built up. |
---|
184 | eval($str); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | // Top-level block. |
---|
189 | // Add a new iteration to this block with the variable assignments |
---|
190 | // we were given. |
---|
191 | $this->_tpldata[$blockname . '.'][] = $vararray; |
---|
192 | } |
---|
193 | |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Root-level variable assignment. Adds to current assignments, overriding |
---|
199 | * any existing variable assignment with the same name. |
---|
200 | */ |
---|
201 | function assign_vars($vararray) |
---|
202 | { |
---|
203 | reset ($vararray); |
---|
204 | while (list($key, $val) = each($vararray)) |
---|
205 | { |
---|
206 | $this->_tpldata['.'][0][$key] = $val; |
---|
207 | } |
---|
208 | |
---|
209 | return true; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Root-level variable assignment. Adds to current assignments, overriding |
---|
214 | * any existing variable assignment with the same name. |
---|
215 | */ |
---|
216 | function assign_var($varname, $varval) |
---|
217 | { |
---|
218 | $this->_tpldata['.'][0][$varname] = $varval; |
---|
219 | |
---|
220 | return true; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | * Generates a full path+filename for the given filename, which can either |
---|
226 | * be an absolute name, or a name relative to the rootdir for this Template |
---|
227 | * object. |
---|
228 | */ |
---|
229 | function make_filename($filename) |
---|
230 | { |
---|
231 | // Check if it's an absolute or relative path. |
---|
232 | if (substr($filename, 0, 1) != '/') |
---|
233 | { |
---|
234 | $filename = realpath($this->root . '/' . $filename); |
---|
235 | } |
---|
236 | |
---|
237 | if (!file_exists($filename)) |
---|
238 | { |
---|
239 | die("Template->make_filename(): Error - file $filename does not exist"); |
---|
240 | } |
---|
241 | |
---|
242 | return $filename; |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | /** |
---|
247 | * If not already done, load the file for the given handle and populate |
---|
248 | * the uncompiled_code[] hash with its code. Do not compile. |
---|
249 | */ |
---|
250 | function loadfile($handle) |
---|
251 | { |
---|
252 | // If the file for this handle is already loaded and compiled, do nothing. |
---|
253 | if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle])) |
---|
254 | { |
---|
255 | return true; |
---|
256 | } |
---|
257 | |
---|
258 | // If we don't have a file assigned to this handle, die. |
---|
259 | if (!isset($this->files[$handle])) |
---|
260 | { |
---|
261 | die("Template->loadfile(): No file specified for handle $handle"); |
---|
262 | } |
---|
263 | |
---|
264 | $filename = $this->files[$handle]; |
---|
265 | |
---|
266 | $str = implode("", @file($filename)); |
---|
267 | if (empty($str)) |
---|
268 | { |
---|
269 | die("Template->loadfile(): File $filename for handle $handle is empty"); |
---|
270 | } |
---|
271 | |
---|
272 | $this->uncompiled_code[$handle] = $str; |
---|
273 | |
---|
274 | return true; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | /** |
---|
280 | * Compiles the given string of code, and returns |
---|
281 | * the result in a string. |
---|
282 | * If "do_not_echo" is true, the returned code will not be directly |
---|
283 | * executable, but can be used as part of a variable assignment |
---|
284 | * for use in assign_code_from_handle(). |
---|
285 | */ |
---|
286 | function compile($code, $do_not_echo = false, $retvar = '') |
---|
287 | { |
---|
288 | // replace \ with \\ and then ' with \'. |
---|
289 | $code = str_replace('\\', '\\\\', $code); |
---|
290 | $code = str_replace('\'', '\\\'', $code); |
---|
291 | |
---|
292 | // change template varrefs into PHP varrefs |
---|
293 | |
---|
294 | // This one will handle varrefs WITH namespaces |
---|
295 | $varrefs = array(); |
---|
296 | preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs); |
---|
297 | $varcount = sizeof($varrefs[1]); |
---|
298 | for ($i = 0; $i < $varcount; $i++) |
---|
299 | { |
---|
300 | $namespace = $varrefs[1][$i]; |
---|
301 | $varname = $varrefs[3][$i]; |
---|
302 | $new = $this->generate_block_varref($namespace, $varname); |
---|
303 | |
---|
304 | $code = str_replace($varrefs[0][$i], $new, $code); |
---|
305 | } |
---|
306 | |
---|
307 | // This will handle the remaining root-level varrefs |
---|
308 | $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ( ( isset($this->_tpldata[\'.\'][0][\'\1\']) ) ? $this->_tpldata[\'.\'][0][\'\1\'] : \'\' ) . \'', $code); |
---|
309 | |
---|
310 | // Break it up into lines. |
---|
311 | $code_lines = explode("\n", $code); |
---|
312 | |
---|
313 | $block_nesting_level = 0; |
---|
314 | $block_names = array(); |
---|
315 | $block_names[0] = "."; |
---|
316 | |
---|
317 | // Second: prepend echo ', append ' . "\n"; to each line. |
---|
318 | $line_count = sizeof($code_lines); |
---|
319 | for ($i = 0; $i < $line_count; $i++) |
---|
320 | { |
---|
321 | $code_lines[$i] = chop($code_lines[$i]); |
---|
322 | if (preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m)) |
---|
323 | { |
---|
324 | $n[0] = $m[0]; |
---|
325 | $n[1] = $m[1]; |
---|
326 | |
---|
327 | // Added: dougk_ff7-Keeps templates from bombing if begin is on the same line as end.. I think. :) |
---|
328 | if ( preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n) ) |
---|
329 | { |
---|
330 | $block_nesting_level++; |
---|
331 | $block_names[$block_nesting_level] = $m[1]; |
---|
332 | if ($block_nesting_level < 2) |
---|
333 | { |
---|
334 | // Block is not nested. |
---|
335 | $code_lines[$i] = '$_' . $n[1] . '_count = ( isset($this->_tpldata[\'' . $n[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $n[1] . '.\']) : 0;'; |
---|
336 | $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; |
---|
337 | $code_lines[$i] .= "\n" . '{'; |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | // This block is nested. |
---|
342 | |
---|
343 | // Generate a namespace string for this block. |
---|
344 | $namespace = implode('.', $block_names); |
---|
345 | // strip leading period from root level.. |
---|
346 | $namespace = substr($namespace, 2); |
---|
347 | // Get a reference to the data array for this block that depends on the |
---|
348 | // current indices of all parent blocks. |
---|
349 | $varref = $this->generate_block_data_ref($namespace, false); |
---|
350 | // Create the for loop code to iterate over this block. |
---|
351 | $code_lines[$i] = '$_' . $n[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; |
---|
352 | $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; |
---|
353 | $code_lines[$i] .= "\n" . '{'; |
---|
354 | } |
---|
355 | |
---|
356 | // We have the end of a block. |
---|
357 | unset($block_names[$block_nesting_level]); |
---|
358 | $block_nesting_level--; |
---|
359 | $code_lines[$i] .= '} // END ' . $n[1]; |
---|
360 | $m[0] = $n[0]; |
---|
361 | $m[1] = $n[1]; |
---|
362 | } |
---|
363 | else |
---|
364 | { |
---|
365 | // We have the start of a block. |
---|
366 | $block_nesting_level++; |
---|
367 | $block_names[$block_nesting_level] = $m[1]; |
---|
368 | if ($block_nesting_level < 2) |
---|
369 | { |
---|
370 | // Block is not nested. |
---|
371 | $code_lines[$i] = '$_' . $m[1] . '_count = ( isset($this->_tpldata[\'' . $m[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $m[1] . '.\']) : 0;'; |
---|
372 | $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; |
---|
373 | $code_lines[$i] .= "\n" . '{'; |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | // This block is nested. |
---|
378 | |
---|
379 | // Generate a namespace string for this block. |
---|
380 | $namespace = implode('.', $block_names); |
---|
381 | // strip leading period from root level.. |
---|
382 | $namespace = substr($namespace, 2); |
---|
383 | // Get a reference to the data array for this block that depends on the |
---|
384 | // current indices of all parent blocks. |
---|
385 | $varref = $this->generate_block_data_ref($namespace, false); |
---|
386 | // Create the for loop code to iterate over this block. |
---|
387 | $code_lines[$i] = '$_' . $m[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; |
---|
388 | $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; |
---|
389 | $code_lines[$i] .= "\n" . '{'; |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|
393 | else if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m)) |
---|
394 | { |
---|
395 | // We have the end of a block. |
---|
396 | unset($block_names[$block_nesting_level]); |
---|
397 | $block_nesting_level--; |
---|
398 | $code_lines[$i] = '} // END ' . $m[1]; |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | // We have an ordinary line of code. |
---|
403 | if (!$do_not_echo) |
---|
404 | { |
---|
405 | $code_lines[$i] = 'echo \'' . $code_lines[$i] . '\' . "\\n";'; |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | $code_lines[$i] = '$' . $retvar . '.= \'' . $code_lines[$i] . '\' . "\\n";'; |
---|
410 | } |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | // Bring it back into a single string of lines of code. |
---|
415 | $code = implode("\n", $code_lines); |
---|
416 | return $code ; |
---|
417 | |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | /** |
---|
422 | * Generates a reference to the given variable inside the given (possibly nested) |
---|
423 | * block namespace. This is a string of the form: |
---|
424 | * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . ' |
---|
425 | * It's ready to be inserted into an "echo" line in one of the templates. |
---|
426 | * NOTE: expects a trailing "." on the namespace. |
---|
427 | */ |
---|
428 | function generate_block_varref($namespace, $varname) |
---|
429 | { |
---|
430 | // Strip the trailing period. |
---|
431 | $namespace = substr($namespace, 0, strlen($namespace) - 1); |
---|
432 | |
---|
433 | // Get a reference to the data block for this namespace. |
---|
434 | $varref = $this->generate_block_data_ref($namespace, true); |
---|
435 | // Prepend the necessary code to stick this in an echo line. |
---|
436 | |
---|
437 | // Append the variable reference. |
---|
438 | $varref .= '[\'' . $varname . '\']'; |
---|
439 | |
---|
440 | $varref = '\' . ( ( isset(' . $varref . ') ) ? ' . $varref . ' : \'\' ) . \''; |
---|
441 | |
---|
442 | return $varref; |
---|
443 | |
---|
444 | } |
---|
445 | |
---|
446 | |
---|
447 | /** |
---|
448 | * Generates a reference to the array of data values for the given |
---|
449 | * (possibly nested) block namespace. This is a string of the form: |
---|
450 | * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] |
---|
451 | * |
---|
452 | * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above. |
---|
453 | * NOTE: does not expect a trailing "." on the blockname. |
---|
454 | */ |
---|
455 | function generate_block_data_ref($blockname, $include_last_iterator) |
---|
456 | { |
---|
457 | // Get an array of the blocks involved. |
---|
458 | $blocks = explode(".", $blockname); |
---|
459 | $blockcount = sizeof($blocks) - 1; |
---|
460 | $varref = '$this->_tpldata'; |
---|
461 | // Build up the string with everything but the last child. |
---|
462 | for ($i = 0; $i < $blockcount; $i++) |
---|
463 | { |
---|
464 | $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]'; |
---|
465 | } |
---|
466 | // Add the block reference for the last child. |
---|
467 | $varref .= '[\'' . $blocks[$blockcount] . '.\']'; |
---|
468 | // Add the iterator for the last child if requried. |
---|
469 | if ($include_last_iterator) |
---|
470 | { |
---|
471 | $varref .= '[$_' . $blocks[$blockcount] . '_i]'; |
---|
472 | } |
---|
473 | |
---|
474 | return $varref; |
---|
475 | } |
---|
476 | |
---|
477 | } |
---|
478 | |
---|
479 | ?> |
---|