de4d58cb |
1 | <?php |
de4d58cb |
2 | /** |
3 | * Template.class.php |
4 | * |
5 | * This file contains an abstract (PHP 4, so "abstract" is relative) |
6 | * class meant to define the basic template interface for the |
7 | * SquirrelMail core application. Subclasses should extend this |
8 | * class with any custom functionality needed to interface a target |
9 | * templating engine with SquirrelMail. |
10 | * |
11 | * @copyright © 2003-2006 The SquirrelMail Project Team |
12 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
13 | * @version $Id$ |
14 | * @package squirrelmail |
15 | * @subpackage Template |
16 | * @since 1.5.2 |
17 | * |
18 | */ |
19 | |
948be6d4 |
20 | /** load template functions */ |
21 | require(SM_PATH . 'functions/template.php'); |
22 | |
de4d58cb |
23 | /** |
24 | * The SquirrelMail Template class. |
25 | * |
26 | * Basic template class for capturing values and pluging them into a template. |
27 | * This class uses a similar API to Smarty. |
28 | * |
29 | * Methods that must be implemented by subclasses are as follows (see method |
30 | * stubs below for further information about expected behavior): |
31 | * |
32 | * assign() |
33 | * assign_by_ref() |
d4c2aa24 |
34 | * clear_all_assign() |
35 | * get_template_vars() |
de4d58cb |
36 | * append() |
37 | * append_by_ref() |
38 | * apply_template() |
39 | * |
01520835 |
40 | * @author Paul Lesniewski <paul at squirrelmail.org> |
de4d58cb |
41 | * @package squirrelmail |
42 | * |
43 | */ |
44 | class Template |
45 | { |
46 | |
47 | /** |
48 | * The template ID |
49 | * |
50 | * @var string |
51 | * |
52 | */ |
d4c2aa24 |
53 | var $template_set_id = ''; |
de4d58cb |
54 | |
55 | /** |
d4c2aa24 |
56 | * The template set base directory (relative path from |
57 | * the main SquirrelMail directory (SM_PATH)) |
de4d58cb |
58 | * |
59 | * @var string |
60 | * |
61 | */ |
62 | var $template_dir = ''; |
63 | |
64 | /** |
65 | * The template engine (please use constants defined in constants.php) |
66 | * |
67 | * @var string |
68 | * |
69 | */ |
70 | var $template_engine = ''; |
71 | |
72 | /** |
d4c2aa24 |
73 | * The fall-back template ID |
de4d58cb |
74 | * |
75 | * @var string |
76 | * |
77 | */ |
d4c2aa24 |
78 | var $fallback_template_set_id = ''; |
de4d58cb |
79 | |
80 | /** |
d4c2aa24 |
81 | * The fall-back template directory (relative |
82 | * path from the main SquirrelMail directory (SM_PATH)) |
de4d58cb |
83 | * |
84 | * @var string |
85 | * |
86 | */ |
d4c2aa24 |
87 | var $fallback_template_dir = ''; |
de4d58cb |
88 | |
89 | /** |
d4c2aa24 |
90 | * The fall-back template engine (please use |
91 | * constants defined in constants.php) |
de4d58cb |
92 | * |
93 | * @var string |
94 | * |
95 | */ |
d4c2aa24 |
96 | var $fallback_template_engine = ''; |
de4d58cb |
97 | |
98 | /** |
d4c2aa24 |
99 | * Template file cache. Structured as an array, whose keys |
100 | * are all the template file names (with path information relative |
101 | * to the template set's base directory, e.g., "css/style.css") |
102 | * found in all parent template sets including the ultimate fall-back |
103 | * template set. Array values are sub-arrays with the |
104 | * following key-value pairs: |
de4d58cb |
105 | * |
d4c2aa24 |
106 | * PATH -- file path, relative to SM_PATH |
107 | * SET_ID -- the ID of the template set that this file belongs to |
108 | * ENGINE -- the engine needed to render this template file |
de4d58cb |
109 | * |
110 | */ |
d4c2aa24 |
111 | var $template_file_cache = array(); |
de4d58cb |
112 | |
740c26f7 |
113 | /** |
d4c2aa24 |
114 | * Extra template engine class objects for rendering templates |
115 | * that require a different engine than the one for the current |
116 | * template set. Keys should be the name of the template engine, |
117 | * values are the corresponding class objects. |
118 | * |
119 | * @var array |
120 | * |
121 | */ |
122 | var $other_template_engine_objects = array(); |
123 | |
de4d58cb |
124 | /** |
125 | * Constructor |
126 | * |
127 | * Please do not call directly. Use Template::construct_template(). |
128 | * |
d4c2aa24 |
129 | * @param string $template_set_id the template ID |
de4d58cb |
130 | * |
131 | */ |
d4c2aa24 |
132 | function Template($template_set_id) { |
de4d58cb |
133 | //FIXME: find a way to test that this is ONLY ever called |
134 | // from the construct_template() method (I doubt it |
135 | // is worth the trouble to parse the current stack trace) |
136 | // if (???) |
137 | // trigger_error('Please do not use default Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR); |
138 | |
d4c2aa24 |
139 | $this->set_up_template($template_set_id); |
de4d58cb |
140 | |
141 | } |
142 | |
143 | /** |
144 | * Construct Template |
145 | * |
146 | * This method should always be called instead of trying |
147 | * to get a Template object from the normal/default constructor, |
148 | * and is necessary in order to control the return value. |
149 | * |
d4c2aa24 |
150 | * @param string $template_set_id the template ID |
de4d58cb |
151 | * |
152 | * @return object The correct Template object for the given template set |
153 | * |
d4c2aa24 |
154 | * @static |
155 | * |
de4d58cb |
156 | */ |
d4c2aa24 |
157 | function construct_template($template_set_id) { |
de4d58cb |
158 | |
d4c2aa24 |
159 | $template = new Template($template_set_id); |
de4d58cb |
160 | return $template->get_template_engine_subclass(); |
161 | |
162 | } |
163 | |
164 | /** |
165 | * Set up internal attributes |
166 | * |
167 | * This method does most of the work for setting up |
168 | * newly constructed objects. |
169 | * |
d4c2aa24 |
170 | * @param string $template_set_id the template ID |
de4d58cb |
171 | * |
172 | */ |
d4c2aa24 |
173 | function set_up_template($template_set_id) { |
de4d58cb |
174 | |
175 | // FIXME: do we want to place any restrictions on the ID like |
176 | // making sure no slashes included? |
177 | // get template ID |
178 | // |
d4c2aa24 |
179 | $this->template_set_id = $template_set_id; |
de4d58cb |
180 | |
181 | |
d4c2aa24 |
182 | $this->fallback_template_set_id = Template::get_fallback_template_set(); |
de4d58cb |
183 | |
184 | |
185 | // set up template directories |
186 | // |
187 | $this->template_dir |
d4c2aa24 |
188 | = Template::calculate_template_file_directory($this->template_set_id); |
189 | $this->fallback_template_dir |
190 | = Template::calculate_template_file_directory($this->fallback_template_set_id); |
de4d58cb |
191 | |
192 | |
d4c2aa24 |
193 | // determine template engine |
194 | // FIXME: assuming PHP template engine may not necessarily be a good thing |
de4d58cb |
195 | // |
d4c2aa24 |
196 | $this->template_engine = Template::get_template_config($this->template_set_id, |
197 | 'template_engine', |
198 | SQ_PHP_TEMPLATE); |
de4d58cb |
199 | |
de4d58cb |
200 | |
d4c2aa24 |
201 | // get template file cache |
202 | // |
203 | $this->template_file_cache = Template::cache_template_file_hierarchy(); |
de4d58cb |
204 | |
d4c2aa24 |
205 | } |
de4d58cb |
206 | |
d4c2aa24 |
207 | /** |
208 | * Determine what the ultimate fallback template set is. |
209 | * |
210 | * NOTE that if the fallback setting cannot be found in the |
211 | * main SquirrelMail configuration settings that the value |
212 | * of $default is returned. |
213 | * |
214 | * @param string $default The template set ID to use if |
215 | * the fallback setting cannot be |
216 | * found in SM config (optional; |
217 | * defaults to "default"). |
218 | * |
219 | * @return string The ID of the fallback template set. |
220 | * |
221 | * @static |
222 | * |
223 | */ |
224 | function get_fallback_template_set($default='default') { |
de4d58cb |
225 | |
d4c2aa24 |
226 | // FIXME: do we want to place any restrictions on the ID such as |
227 | // making sure no slashes included? |
de4d58cb |
228 | |
d4c2aa24 |
229 | // values are in main SM config file |
de4d58cb |
230 | // |
d4c2aa24 |
231 | global $templateset_fallback, $aTemplateSet; |
232 | $aTemplateSet = (!isset($aTemplateSet) || !is_array($aTemplateSet) |
233 | ? array() : $aTemplateSet); |
234 | $templateset_fallback = (!isset($templateset_fallback) |
293906dd |
235 | ? $default : $templateset_fallback); |
d4c2aa24 |
236 | |
293906dd |
237 | // iterate through all template sets, is this a valid skin ID? |
238 | // |
239 | $found_it = FALSE; |
240 | foreach ($aTemplateSet as $aTemplate) { |
f4138e60 |
241 | if ($aTemplate['ID'] === $templateset_fallback) { |
293906dd |
242 | $found_it = TRUE; |
243 | break; |
244 | } |
245 | } |
246 | |
247 | if ($found_it) |
248 | return $templateset_fallback; |
249 | |
250 | // FIXME: note that it is possible for $default to |
251 | // point to an invalid (nonexistent) template set |
252 | // and that error will not be caught here |
253 | // |
254 | return $default; |
d4c2aa24 |
255 | |
256 | } |
257 | |
258 | /** |
259 | * Determine what the default template set is. |
260 | * |
261 | * NOTE that if the default setting cannot be found in the |
262 | * main SquirrelMail configuration settings that the value |
263 | * of $default is returned. |
264 | * |
265 | * @param string $default The template set ID to use if |
266 | * the default setting cannot be |
267 | * found in SM config (optional; |
268 | * defaults to "default"). |
269 | * |
270 | * @return string The ID of the default template set. |
271 | * |
272 | * @static |
273 | * |
274 | */ |
275 | function get_default_template_set($default='default') { |
276 | |
277 | // FIXME: do we want to place any restrictions on the ID such as |
278 | // making sure no slashes included? |
279 | |
280 | // values are in main SM config file |
281 | // |
282 | global $templateset_default, $aTemplateSet; |
283 | $aTemplateSet = (!isset($aTemplateSet) || !is_array($aTemplateSet) |
284 | ? array() : $aTemplateSet); |
285 | $templateset_default = (!isset($templateset_default) |
293906dd |
286 | ? $default : $templateset_default); |
d4c2aa24 |
287 | |
293906dd |
288 | // iterate through all template sets, is this a valid skin ID? |
289 | // |
290 | $found_it = FALSE; |
291 | foreach ($aTemplateSet as $aTemplate) { |
f4138e60 |
292 | if ($aTemplate['ID'] === $templateset_default) { |
293906dd |
293 | $found_it = TRUE; |
294 | break; |
295 | } |
296 | } |
297 | |
298 | if ($found_it) |
299 | return $templateset_default; |
300 | |
301 | // FIXME: note that it is possible for $default to |
302 | // point to an invalid (nonexistent) template set |
303 | // and that error will not be caught here |
304 | // |
305 | return $default; |
de4d58cb |
306 | |
307 | } |
308 | |
309 | /** |
310 | * Instantiate and return correct subclass for this template |
311 | * set's templating engine. |
312 | * |
d4c2aa24 |
313 | * @param string $template_set_id The template set whose engine |
314 | * is to be used as an override |
315 | * (if not given, this template |
316 | * set's engine is used) (optional). |
317 | * |
de4d58cb |
318 | * @return object The Template subclass object for the template engine. |
319 | * |
320 | */ |
d4c2aa24 |
321 | function get_template_engine_subclass($template_set_id='') { |
322 | |
323 | if (empty($template_set_id)) $template_set_id = $this->template_set_id; |
324 | // FIXME: assuming PHP template engine may not necessarily be a good thing |
325 | $engine = Template::get_template_config($template_set_id, |
326 | 'template_engine', SQ_PHP_TEMPLATE); |
327 | |
de4d58cb |
328 | |
329 | $engine_class_file = SM_PATH . 'class/template/' |
d4c2aa24 |
330 | . $engine . 'Template.class.php'; |
de4d58cb |
331 | |
332 | if (!file_exists($engine_class_file)) { |
d4c2aa24 |
333 | trigger_error('Unknown template engine (' . $engine |
de4d58cb |
334 | . ') was specified in template configuration file', |
335 | E_USER_ERROR); |
336 | } |
337 | |
d4c2aa24 |
338 | $engine_class = $engine . 'Template'; |
339 | require_once($engine_class_file); |
340 | return new $engine_class($template_set_id); |
de4d58cb |
341 | |
342 | } |
343 | |
344 | /** |
345 | * Determine the relative template directory path for |
346 | * the given template ID. |
347 | * |
d4c2aa24 |
348 | * @param string $template_set_id The template ID from which to build |
349 | * the directory path |
de4d58cb |
350 | * |
351 | * @return string The relative template path (based off of SM_PATH) |
352 | * |
d4c2aa24 |
353 | * @static |
354 | * |
de4d58cb |
355 | */ |
d4c2aa24 |
356 | function calculate_template_file_directory($template_set_id) { |
de4d58cb |
357 | |
d4c2aa24 |
358 | return 'templates/' . $template_set_id . '/'; |
de4d58cb |
359 | |
360 | } |
361 | |
362 | /** |
363 | * Determine the relative images directory path for |
364 | * the given template ID. |
365 | * |
d4c2aa24 |
366 | * @param string $template_set_id The template ID from which to build |
367 | * the directory path |
de4d58cb |
368 | * |
369 | * @return string The relative images path (based off of SM_PATH) |
370 | * |
d4c2aa24 |
371 | * @static |
372 | * |
de4d58cb |
373 | */ |
d4c2aa24 |
374 | function calculate_template_images_directory($template_set_id) { |
de4d58cb |
375 | |
d4c2aa24 |
376 | return 'templates/' . $template_set_id . '/images/'; |
de4d58cb |
377 | |
378 | } |
379 | |
380 | /** |
381 | * Return the relative template directory path for this template set. |
382 | * |
383 | * @return string The relative path to the template directory based |
384 | * from the main SquirrelMail directory (SM_PATH). |
385 | * |
386 | */ |
387 | function get_template_file_directory() { |
388 | |
389 | return $this->template_dir; |
390 | |
391 | } |
392 | |
d4c2aa24 |
393 | /** |
394 | * Return the template ID for the fallback template set. |
395 | * |
396 | * @return string The ID of the fallback template set. |
397 | * |
398 | */ |
399 | function get_fallback_template_set_id() { |
400 | |
401 | return $this->fallback_template_set_id; |
402 | |
403 | } |
de4d58cb |
404 | |
405 | /** |
d4c2aa24 |
406 | * Return the relative template directory path for the |
407 | * fallback template set. |
de4d58cb |
408 | * |
d4c2aa24 |
409 | * @return string The relative path to the fallback template |
410 | * directory based from the main SquirrelMail |
411 | * directory (SM_PATH). |
412 | * |
413 | */ |
414 | function get_fallback_template_file_directory() { |
415 | |
416 | return $this->fallback_template_dir; |
417 | |
418 | } |
419 | |
420 | /** |
421 | * Get template set config setting |
422 | * |
423 | * Given a template set ID and setting name, returns the |
424 | * setting's value. Note that settings are cached in |
425 | * session, so "live" changes to template configuration |
426 | * won't be reflected until the user logs out and back |
427 | * in again. |
428 | * |
429 | * @param string $template_set_id The template set for which |
430 | * to look up the setting. |
431 | * @param string $setting The name of the setting to |
432 | * retrieve. |
433 | * @param mixed $default When the requested setting |
434 | * is not found, the contents |
435 | * of this value are returned |
436 | * instead (optional; default |
437 | * is NULL). |
438 | * NOTE that unlike sqGetGlobalVar(), |
439 | * this function will also return |
440 | * the default value if the |
441 | * requested setting is found |
442 | * but is empty. |
443 | * @param boolean $live_config When TRUE, the target template |
444 | * set's configuration file is |
445 | * reloaded every time this |
446 | * method is called. Default |
447 | * behavior is to only load the |
448 | * configuration file if it had |
449 | * never been loaded before, but |
450 | * not again after that (optional; |
451 | * default FALSE). Use with care! |
452 | * Should mostly be used for |
453 | * debugging. |
454 | * |
455 | * @return mixed The desired setting's value or if not found, |
456 | * the contents of $default are returned. |
457 | * |
458 | * @static |
459 | * |
460 | */ |
461 | function get_template_config($template_set_id, $setting, |
462 | $default=NULL, $live_config=FALSE) { |
463 | |
464 | sqGetGlobalVar('template_configuration_settings', |
465 | $template_configuration_settings, |
466 | SQ_SESSION, |
467 | array()); |
468 | |
469 | if ($live_config) unset($template_configuration_settings[$template_set_id]); |
470 | |
471 | |
472 | // NOTE: could use isset() instead of empty() below, but |
473 | // this function is designed to replace empty values |
474 | // as well as non-existing values with $default |
475 | // |
476 | if (!empty($template_configuration_settings[$template_set_id][$setting])) |
477 | return $template_configuration_settings[$template_set_id][$setting]; |
478 | |
479 | |
480 | // if template set configuration has been loaded, but this |
481 | // setting is not known, return $default |
482 | // |
483 | if (!empty($template_configuration_settings[$template_set_id])) |
484 | return $default; |
485 | |
486 | |
487 | // otherwise (template set configuration has not been loaded before), |
488 | // load it into session and return the desired setting after that |
489 | // |
490 | $template_config_file = SM_PATH |
491 | . Template::calculate_template_file_directory($template_set_id) |
492 | . 'config.php'; |
493 | |
494 | if (!file_exists($template_config_file)) { |
495 | |
496 | trigger_error('No template configuration file was found where expected: ("' |
497 | . $template_config_file . '")', E_USER_ERROR); |
498 | |
499 | } else { |
500 | |
501 | // we require() the file to let PHP do the variable value |
502 | // parsing for us, and read the file in manually so we can |
503 | // know what variable names are used in the config file |
504 | // (settings can be different depending on specific requirements |
505 | // of different template engines)... the other way this may |
506 | // be accomplished is to somehow diff the symbol table |
507 | // before/after the require(), but anyway, this code should |
508 | // only run once for this template set... |
509 | // |
510 | require($template_config_file); |
511 | $file_contents = implode("\n", file($template_config_file)); |
512 | |
513 | |
514 | // note that this assumes no template settings have |
515 | // a string in them that looks like a variable name like $x |
516 | // also note that this will attempt to grab things like |
517 | // $Id found in CVS headers, so we try to adjust for that |
518 | // by checking that the variable is actually set |
519 | // |
520 | preg_match_all('/\$(\w+)/', $file_contents, $variables, PREG_PATTERN_ORDER); |
521 | foreach ($variables[1] as $variable) { |
522 | if (isset($$variable)) |
523 | $template_configuration_settings[$template_set_id][$variable] |
524 | = $$variable; |
525 | } |
526 | |
527 | sqsession_register($template_configuration_settings, |
528 | 'template_configuration_settings'); |
529 | |
530 | // NOTE: could use isset() instead of empty() below, but |
531 | // this function is designed to replace empty values |
532 | // as well as non-existing values with $default |
533 | // |
534 | if (!empty($template_configuration_settings[$template_set_id][$setting])) |
535 | return $template_configuration_settings[$template_set_id][$setting]; |
536 | else |
537 | return $default; |
538 | |
539 | } |
540 | |
541 | } |
542 | |
543 | /** |
544 | * Obtain template file hierarchy from cache. |
545 | * |
546 | * If the file hierarchy does not exist in session, it is |
547 | * constructed and stored in session before being returned |
548 | * to the caller. |
549 | * |
550 | * @param boolean $regenerate_cache When TRUE, the file hierarchy |
551 | * is reloaded and stored fresh |
552 | * (optional; default FALSE). |
553 | * @param array $additional_files Must be in same form as the |
554 | * files in the file hierarchy |
555 | * cache. These are then added |
556 | * to the cache (optional; default |
557 | * empty - no additional files). |
558 | * |
559 | * @return array Template file hierarchy array, whose keys |
560 | * are all the template file names (with path |
561 | * information relative to the template set's |
562 | * base directory, e.g., "css/style.css") |
563 | * found in all parent template sets including |
564 | * the ultimate fall-back template set. |
565 | * Array values are sub-arrays with the |
566 | * following key-value pairs: |
567 | * |
568 | * PATH -- file path, relative to SM_PATH |
569 | * SET_ID -- the ID of the template set that this file belongs to |
570 | * ENGINE -- the engine needed to render this template file |
571 | * |
572 | * @static |
de4d58cb |
573 | * |
574 | */ |
d4c2aa24 |
575 | function cache_template_file_hierarchy($regenerate_cache=FALSE, |
576 | $additional_files=array()) { |
577 | |
578 | sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy, |
579 | SQ_SESSION, array()); |
580 | |
581 | |
582 | if ($regenerate_cache) unset($template_file_hierarchy); |
583 | |
d4c2aa24 |
584 | if (!empty($template_file_hierarchy)) { |
585 | |
586 | // have to add additional files if given before returning |
587 | // |
588 | if (!empty($additional_files)) { |
589 | $template_file_hierarchy = array_merge($template_file_hierarchy, |
590 | $additional_files); |
591 | sqsession_register($template_file_hierarchy, |
592 | 'template_file_hierarchy'); |
593 | } |
594 | |
595 | return $template_file_hierarchy; |
596 | } |
597 | |
598 | |
599 | // nothing in cache apparently, so go build it now |
600 | // |
601 | // FIXME: not sure if there is any possibility that |
602 | // this could be called when $sTemplateID has |
603 | // yet to be defined... throw error for now, |
604 | // but if the error occurs, it's a coding error |
605 | // rather than a configuration error |
606 | // |
607 | global $sTemplateID; |
608 | if (empty($sTemplateID)) { |
609 | |
610 | trigger_error('Template set ID unknown', E_USER_ERROR); |
611 | |
612 | } else { |
613 | |
614 | $template_file_hierarchy = Template::catalog_template_files($sTemplateID); |
615 | |
616 | // additional files, if any |
617 | // |
618 | if (!empty($additional_files)) { |
619 | $template_file_hierarchy = array_merge($template_file_hierarchy, |
620 | $additional_files); |
621 | } |
622 | |
623 | sqsession_register($template_file_hierarchy, |
624 | 'template_file_hierarchy'); |
625 | |
626 | return $template_file_hierarchy; |
627 | |
628 | } |
629 | |
630 | } |
631 | |
632 | /** |
633 | * Traverse template hierarchy and catalogue all template |
634 | * files (for storing in cache). |
635 | * |
636 | * Paths to all files in all parent, grand-parent, great grand |
637 | * parent, etc. template sets (including the fallback template) |
638 | * are catalogued; for identically named files, the file earlier |
639 | * in the hierarchy (closest to this template set) is used. |
640 | * |
641 | * @param string $template_set_id The template set in which to |
642 | * search for files |
643 | * @param array $file_list The file list so far to be added |
644 | * to (allows recursive behavior) |
645 | * (optional; default empty array). |
646 | * @param string $directory The directory in which to search for |
647 | * files (must be given as full path). |
648 | * If empty, starts at top-level template |
649 | * set directory (optional; default empty). |
650 | * NOTE! Use with care, as behavior is |
651 | * unpredictable if directory given is not |
652 | * part of correct template set. |
653 | * |
654 | * @return mixed The top-level caller will have an array of template |
655 | * files returned to it; recursive calls to this function |
656 | * do not receive any return value at all. The format |
657 | * of the template file array is as described for the |
658 | * Template class attribute $template_file_cache |
659 | * |
660 | * @static |
661 | * |
662 | */ |
663 | function catalog_template_files($template_set_id, $file_list=array(), $directory='') { |
664 | |
665 | $template_base_dir = SM_PATH |
666 | . Template::calculate_template_file_directory($template_set_id); |
667 | |
668 | if (empty($directory)) { |
669 | $directory = $template_base_dir; |
670 | } |
671 | |
672 | $files_and_dirs = list_files($directory, '', FALSE, TRUE, FALSE, TRUE); |
de4d58cb |
673 | |
d4c2aa24 |
674 | // recurse for all the subdirectories in the template set |
675 | // |
676 | foreach ($files_and_dirs['DIRECTORIES'] as $dir) { |
677 | $file_list = Template::catalog_template_files($template_set_id, $file_list, $dir); |
678 | } |
679 | |
680 | // place all found files in the cache |
681 | // FIXME: assuming PHP template engine may not necessarily be a good thing |
682 | // |
683 | $engine = Template::get_template_config($template_set_id, |
684 | 'template_engine', SQ_PHP_TEMPLATE); |
685 | foreach ($files_and_dirs['FILES'] as $file) { |
686 | |
687 | // remove the part of the file path corresponding to the |
688 | // template set's base directory |
689 | // |
690 | $relative_file = substr($file, strlen($template_base_dir)); |
691 | |
948be6d4 |
692 | /** |
693 | * only put file in cache if not already found in earlier template |
694 | * PATH should be relative to SquirrelMail top directory |
695 | */ |
d4c2aa24 |
696 | if (!isset($file_list[$relative_file])) { |
697 | $file_list[$relative_file] = array( |
948be6d4 |
698 | 'PATH' => substr($file,strlen(SM_PATH)), |
d4c2aa24 |
699 | 'SET_ID' => $template_set_id, |
700 | 'ENGINE' => $engine, |
701 | ); |
702 | } |
703 | |
704 | } |
705 | |
706 | |
707 | // now if we are currently at the top-level of the template |
708 | // set base directory, we need to move on to the parent |
709 | // template set, if any |
710 | // |
711 | if ($directory == $template_base_dir) { |
712 | |
713 | // use fallback when we run out of parents |
714 | // |
715 | $fallback_id = Template::get_fallback_template_set(); |
716 | $parent_id = Template::get_template_config($template_set_id, |
717 | 'parent_template_set', |
718 | $fallback_id); |
719 | |
720 | // were we already all the way to the last level? just exit |
721 | // |
722 | // note that this code allows the fallback set to have |
723 | // a parent, too, but can result in endless loops |
724 | // if ($parent_id == $template_set_id) { |
725 | // |
726 | if ($fallback_id == $template_set_id) { |
727 | return $file_list; |
728 | } |
729 | |
730 | $file_list = Template::catalog_template_files($parent_id, $file_list); |
731 | |
732 | } |
733 | |
734 | return $file_list; |
de4d58cb |
735 | |
736 | } |
737 | |
d4c2aa24 |
738 | /** |
739 | * Look for a template file in a plugin; add to template |
740 | * file cache if found. |
741 | * |
742 | * The file is searched for in the following order: |
743 | * |
744 | * - A directory for the current template set within the plugin: |
745 | * SM_PATH/plugins/<plugin name>/templates/<template name>/ |
746 | * - In a directory for one of the current template set's ancestor |
747 | * (inherited) template sets within the plugin: |
748 | * SM_PATH/plugins/<plugin name>/templates/<parent template name>/ |
749 | * - In a directory for the fallback template set within the plugin: |
750 | * SM_PATH/plugins/<plugin name>/templates/<fallback template name>/ |
751 | * |
752 | * @param string $plugin The name of the plugin |
753 | * @param string $file The name of the template file |
754 | * @param string $template_set_id The ID of the template for which |
755 | * to start looking for the file |
756 | * (optional; default is current |
757 | * template set ID). |
758 | * |
759 | * @return boolean TRUE if the template file was found, FALSE otherwise. |
760 | * |
761 | */ |
762 | function find_and_cache_plugin_template_file($plugin, $file, $template_set_id='') { |
763 | |
764 | if (empty($template_set_id)) |
765 | $template_set_id = $this->template_set_id; |
766 | |
767 | $file_path = SM_PATH . 'plugins/' . $plugin . '/' |
768 | . $this->calculate_template_file_directory($template_set_id) |
769 | . $file; |
770 | |
771 | if (file_exists($file_path)) { |
772 | // FIXME: assuming PHP template engine may not necessarily be a good thing |
773 | $engine = $this->get_template_config($template_set_id, |
774 | 'template_engine', SQ_PHP_TEMPLATE); |
775 | $file_list = array('plugins/' . $plugin . '/' . $file => array( |
eb2425ec |
776 | 'PATH' => substr($file_path, strlen(SM_PATH)), |
777 | 'SET_ID' => $template_set_id, |
778 | 'ENGINE' => $engine, |
d4c2aa24 |
779 | ) |
780 | ); |
781 | $this->template_file_cache |
782 | = $this->cache_template_file_hierarchy(FALSE, $file_list); |
783 | return TRUE; |
784 | } |
785 | |
786 | |
787 | // not found yet, try parent template set |
788 | // (use fallback when we run out of parents) |
789 | // |
790 | $fallback_id = $this->get_fallback_template_set(); |
791 | $parent_id = $this->get_template_config($template_set_id, |
792 | 'parent_template_set', |
793 | $fallback_id); |
794 | |
795 | // were we already all the way to the last level? just exit |
796 | // |
797 | // note that this code allows the fallback set to have |
798 | // a parent, too, but can result in endless loops |
799 | // if ($parent_id == $template_set_id) { |
800 | // |
801 | if ($fallback_id == $template_set_id) { |
802 | return FALSE; |
803 | } |
804 | |
805 | return $this->find_and_cache_plugin_template_file($plugin, $file, $parent_id); |
806 | |
807 | } |
de4d58cb |
808 | |
809 | /** |
810 | * Find the right template file. |
811 | * |
d4c2aa24 |
812 | * The template file is taken from the template file cache, thus |
813 | * the file is taken from the current template, one of its |
814 | * ancestors or the fallback template. |
815 | * |
816 | * Note that it is perfectly acceptable to load template files from |
817 | * template subdirectories. For example, JavaScript templates found |
818 | * in the js/ subdirectory would be loaded by passing |
819 | * "js/<javascript file name>" as the $filename. |
820 | * |
821 | * Note that the caller can also ask for ALL files in a directory |
822 | * (and those in the same directory for all ancestor template sets) |
823 | * by giving a $filename that is a directory name (ending with a |
824 | * slash). |
825 | * |
826 | * If not found and the file is a plugin template file (indicated |
827 | * by the presence of "plugins/" on the beginning of $filename), |
828 | * the target plugin is searched for a substitue template file |
829 | * before just returning nothing. |
de4d58cb |
830 | * |
831 | * Plugin authors must note that the $filename MUST be prefaced |
832 | * with "plugins/<plugin name>/" in order to correctly resolve the |
833 | * template file. |
834 | * |
de4d58cb |
835 | * @param string $filename The name of the template file, |
836 | * possibly prefaced with |
837 | * "plugins/<plugin name>/" |
838 | * indicating that it is a plugin |
d4c2aa24 |
839 | * template, or ending with a |
840 | * slash, indicating that all files |
841 | * for that directory name should |
842 | * be returned. |
e5d57179 |
843 | * @param boolean $directories_ok When TRUE, directory names |
844 | * are acceptable search values, |
845 | * and when returning a list of |
846 | * directory contents, sub-directory |
847 | * names will also be included |
848 | * (optional; default FALSE). |
849 | * NOTE that empty directories |
850 | * are NOT included in the cache! |
851 | * @param boolean $directories_only When TRUE, only directory names |
852 | * are included in the returned |
853 | * results. (optional; default |
854 | * FALSE). Setting this argument |
855 | * to TRUE forces $directories_ok |
856 | * to TRUE as well. |
857 | * NOTE that empty directories |
858 | * are NOT included in the cache! |
d4c2aa24 |
859 | * |
860 | * @return mixed The full path to the template file or a list |
861 | * of all files in the given directory if $filename |
862 | * ends with a slash; if not found, an empty string |
863 | * is returned. The caller is responsible for |
864 | * throwing errors or other actions if template |
865 | * file is not found. |
de4d58cb |
866 | * |
867 | */ |
e5d57179 |
868 | function get_template_file_path($filename, |
869 | $directories_ok=FALSE, |
870 | $directories_only=FALSE) { |
de4d58cb |
871 | |
e5d57179 |
872 | if ($directories_only) $directories_ok = TRUE; |
873 | |
874 | |
875 | // only looking for directory listing first... |
876 | // |
d4c2aa24 |
877 | // return list of all files in a directory (and that |
878 | // of any ancestors) |
de4d58cb |
879 | // |
d4c2aa24 |
880 | if ($filename{strlen($filename) - 1} == '/') { |
881 | |
882 | $return_array = array(); |
883 | foreach ($this->template_file_cache as $file => $file_info) { |
884 | |
885 | // only want files in the requested directory |
886 | // (AND not in a subdirectory!) |
887 | // |
e5d57179 |
888 | if (!$directories_only && strpos($file, $filename) === 0 |
d4c2aa24 |
889 | && strpos($file, '/', strlen($filename)) === FALSE) |
948be6d4 |
890 | $return_array[] = SM_PATH . $file_info['PATH']; |
de4d58cb |
891 | |
e5d57179 |
892 | // directories too? detect by finding any |
893 | // array key that matches a file in a sub-directory |
894 | // of the directory being processed |
895 | // |
896 | if ($directories_ok && strpos($file, $filename) === 0 |
897 | && ($pos = strpos($file, '/', strlen($filename))) !== FALSE |
898 | && strpos($file, '/', $pos + 1) === FALSE) { |
899 | $directory_name = SM_PATH |
900 | . substr($file_info['PATH'], |
901 | 0, |
902 | strrpos($file_info['PATH'], '/')); |
903 | if (!in_array($directory_name, $return_array)) |
904 | $return_array[] = $directory_name; |
905 | } |
906 | |
d4c2aa24 |
907 | } |
908 | return $return_array; |
909 | |
910 | } |
911 | |
e5d57179 |
912 | |
913 | // just looking for singular file or directory below... |
914 | // |
d4c2aa24 |
915 | // figure out what to do with files not found |
916 | // |
e5d57179 |
917 | if ($directories_only || empty($this->template_file_cache[$filename]['PATH'])) { |
918 | |
919 | // if looking for directories... |
920 | // have to iterate through cache and detect |
921 | // directory by matching any file inside of it |
922 | // |
923 | if ($directories_ok) { |
924 | foreach ($this->template_file_cache as $file => $file_info) { |
925 | if (strpos($file, $filename) === 0 |
926 | && ($pos = strpos($file, '/', strlen($filename))) !== FALSE |
927 | && strpos($file, '/', $pos + 1) === FALSE) { |
928 | return SM_PATH . substr($file_info['PATH'], |
929 | 0, |
930 | strrpos($file_info['PATH'], '/')); |
931 | } |
932 | } |
933 | |
934 | if ($directories_only) return ''; |
935 | } |
d4c2aa24 |
936 | |
e5d57179 |
937 | // plugins get one more chance |
de4d58cb |
938 | // |
e5d57179 |
939 | if (strpos($filename, 'plugins/') === 0) { |
940 | |
941 | $plugin_name = substr($filename, 8, strpos($filename, '/', 8) - 8); |
942 | $file = substr($filename, strlen($plugin_name) + 9); |
943 | |
944 | if (!$this->find_and_cache_plugin_template_file($plugin_name, $file)) |
945 | return ''; |
946 | //FIXME: technically I guess we should check for directories |
947 | // here too, but that's overkill (no need) presently |
948 | // (plugin-provided alternate stylesheet dirs?!? bah.) |
de4d58cb |
949 | |
e5d57179 |
950 | } |
de4d58cb |
951 | |
e5d57179 |
952 | // nothing... return empty string (yes, the else is intentional!) |
953 | // |
954 | else return ''; |
de4d58cb |
955 | |
d4c2aa24 |
956 | } |
de4d58cb |
957 | |
948be6d4 |
958 | return SM_PATH . $this->template_file_cache[$filename]['PATH']; |
de4d58cb |
959 | |
d4c2aa24 |
960 | } |
de4d58cb |
961 | |
d4c2aa24 |
962 | /** |
963 | * Get template engine needed to render given template file. |
964 | * |
965 | * If at all possible, just returns a reference to $this, but |
966 | * some template files may require a different engine, thus |
967 | * an object for that engine (which will subsequently be kept |
968 | * in this object for future use) is returned. |
969 | * |
970 | * @param string $filename The name of the template file, |
971 | * |
972 | * @return object The needed template object to render the template. |
973 | * |
974 | */ |
975 | function get_rendering_template_engine_object($filename) { |
976 | |
977 | // for files that we cannot find engine info for, |
978 | // just return $this |
979 | // |
980 | if (empty($this->template_file_cache[$filename]['ENGINE'])) |
981 | return $this; |
982 | |
983 | |
984 | // otherwise, compare $this' engine to the file's engine |
985 | // |
986 | $engine = $this->template_file_cache[$filename]['ENGINE']; |
987 | if ($this->template_engine == $engine) |
988 | return $this; |
989 | |
990 | |
991 | // need to load another engine... if already instantiated, |
992 | // and stored herein, return that |
993 | // FIXME: this assumes same engine setup in all template |
994 | // set config files that have same engine in common |
995 | // (but keeping a separate class object for every |
996 | // template set seems like overkill... for now we |
997 | // won't do that unless it becomes a problem) |
998 | // |
999 | if (!empty($this->other_template_engine_objects[$engine])) { |
1000 | $rendering_engine = $this->other_template_engine_objects[$engine]; |
de4d58cb |
1001 | |
de4d58cb |
1002 | |
d4c2aa24 |
1003 | // otherwise, instantiate new engine object, add to cache |
1004 | // and return it |
1005 | // |
1006 | } else { |
1007 | $template_set_id = $this->template_file_cache[$filename]['SET_ID']; |
1008 | $this->other_template_engine_objects[$engine] |
1009 | = $this->get_template_engine_subclass($template_set_id); |
1010 | $rendering_engine = $this->other_template_engine_objects[$engine]; |
1011 | } |
de4d58cb |
1012 | |
de4d58cb |
1013 | |
d4c2aa24 |
1014 | // now, need to copy over all the assigned variables |
1015 | // from $this to the rendering engine (YUCK! -- we need |
1016 | // to discourage template authors from creating |
1017 | // situations where engine changes occur) |
1018 | // |
1019 | $rendering_engine->clear_all_assign(); |
1020 | $rendering_engine->assign($this->get_template_vars()); |
de4d58cb |
1021 | |
de4d58cb |
1022 | |
d4c2aa24 |
1023 | // finally ready to go |
1024 | // |
1025 | return $rendering_engine; |
de4d58cb |
1026 | |
d4c2aa24 |
1027 | } |
de4d58cb |
1028 | |
d4c2aa24 |
1029 | /** |
1030 | * Return all JavaScript files provided by the template. |
1031 | * |
1032 | * All files found in the template set's "js" directory (and |
1033 | * that of its ancestors) with the extension ".js" are returned. |
1034 | * |
1035 | * @param boolean $full_path When FALSE, only the file names |
1036 | * are included in the return array; |
1037 | * otherwise, path information is |
1038 | * included (relative to SM_PATH) |
1039 | * (OPTIONAL; default only file names) |
1040 | * |
1041 | * @return array The required file names/paths. |
1042 | * |
1043 | */ |
1044 | function get_javascript_includes($full_path=FALSE) { |
de4d58cb |
1045 | |
d4c2aa24 |
1046 | // since any page from a parent template set |
1047 | // could end up being loaded, we have to load |
1048 | // all js files from ancestor template sets, |
1049 | // not just this set |
1050 | // |
1051 | //$directory = SM_PATH . $this->get_template_file_directory() . 'js'; |
1052 | //$js_files = list_files($directory, '.js', !$full_path); |
1053 | // |
1054 | $js_files = $this->get_template_file_path('js/'); |
1055 | |
1056 | |
1057 | // parse out .js files only |
1058 | // |
1059 | $return_array = array(); |
1060 | foreach ($js_files as $file) { |
1061 | |
1062 | if (substr($file, strlen($file) - 3) != '.js') continue; |
de4d58cb |
1063 | |
d4c2aa24 |
1064 | if ($full_path) { |
1065 | $return_array[] = $file; |
1066 | } else { |
1067 | $return_array[] = basename($file); |
de4d58cb |
1068 | } |
1069 | |
1070 | } |
1071 | |
d4c2aa24 |
1072 | return $return_array; |
de4d58cb |
1073 | |
1074 | } |
1075 | |
1076 | /** |
d4c2aa24 |
1077 | * Return all alternate stylesheets provided by template. |
1078 | * |
e5d57179 |
1079 | * All (non-empty) directories found in the template set's |
1080 | * "css/alternates" directory (and that of its ancestors) |
1081 | * are returned. |
d4c2aa24 |
1082 | * |
1083 | * Note that prettified names are constructed herein by |
e5d57179 |
1084 | * taking the directory name, changing underscores to spaces |
1085 | * and capitalizing each word in the resultant name. |
de4d58cb |
1086 | * |
1087 | * @param boolean $full_path When FALSE, only the file names |
1088 | * are included in the return array; |
1089 | * otherwise, path information is |
1090 | * included (relative to SM_PATH) |
1091 | * (OPTIONAL; default only file names) |
1092 | * |
d4c2aa24 |
1093 | * @return array A list of the available alternate stylesheets, |
1094 | * where the keys are the file names (formatted |
1095 | * according to $full_path) for the stylesheets, |
1096 | * and the values are the prettified version of |
1097 | * the file names for display to the user. |
1098 | * |
de4d58cb |
1099 | */ |
d4c2aa24 |
1100 | function get_alternative_stylesheets($full_path=FALSE) { |
de4d58cb |
1101 | |
d4c2aa24 |
1102 | // since any page from a parent template set |
1103 | // could end up being loaded, we will load |
1104 | // all alternate css files from ancestor |
1105 | // template sets, not just this set |
1106 | // |
e5d57179 |
1107 | $css_directories = $this->get_template_file_path('css/alternates/', TRUE, TRUE); |
d4c2aa24 |
1108 | |
1109 | |
e5d57179 |
1110 | // prettify names |
d4c2aa24 |
1111 | // |
1112 | $return_array = array(); |
e5d57179 |
1113 | foreach ($css_directories as $directory) { |
d4c2aa24 |
1114 | |
e5d57179 |
1115 | // CVS directories are not wanted |
1116 | // |
1117 | if (strpos($directory, '/CVS') === strlen($directory) - 4) continue; |
d4c2aa24 |
1118 | |
e5d57179 |
1119 | $pretty_name = ucwords(str_replace('_', ' ', basename($directory))); |
d4c2aa24 |
1120 | |
1121 | if ($full_path) { |
e5d57179 |
1122 | $return_array[$directory] = $pretty_name; |
d4c2aa24 |
1123 | } else { |
e5d57179 |
1124 | $return_array[basename($directory)] = $pretty_name; |
de4d58cb |
1125 | } |
d4c2aa24 |
1126 | |
de4d58cb |
1127 | } |
1128 | |
d4c2aa24 |
1129 | return $return_array; |
de4d58cb |
1130 | |
1131 | } |
1132 | |
1133 | /** |
1134 | * Return all standard stylsheets provided by the template. |
1135 | * |
d4c2aa24 |
1136 | * All files found in the template set's "css" directory (and |
1137 | * that of its ancestors) with the extension ".css" except |
1138 | * "rtl.css" (which is dealt with separately) are returned. |
de4d58cb |
1139 | * |
1140 | * @param boolean $full_path When FALSE, only the file names |
1141 | * are included in the return array; |
1142 | * otherwise, path information is |
1143 | * included (relative to SM_PATH) |
1144 | * (OPTIONAL; default only file names) |
1145 | * |
1146 | * @return array The required file names/paths. |
1147 | * |
1148 | */ |
1149 | function get_stylesheets($full_path=FALSE) { |
1150 | |
d4c2aa24 |
1151 | // since any page from a parent template set |
1152 | // could end up being loaded, we have to load |
1153 | // all css files from ancestor template sets, |
1154 | // not just this set |
1155 | // |
1156 | //$directory = SM_PATH . $this->get_template_file_directory() . 'css'; |
1157 | //$css_files = list_files($directory, '.css', !$full_path); |
1158 | // |
1159 | $css_files = $this->get_template_file_path('css/'); |
1160 | |
de4d58cb |
1161 | |
1162 | // need to leave out "rtl.css" |
d4c2aa24 |
1163 | // |
de4d58cb |
1164 | $return_array = array(); |
d4c2aa24 |
1165 | foreach ($css_files as $file) { |
de4d58cb |
1166 | |
d4c2aa24 |
1167 | if (substr($file, strlen($file) - 4) != '.css') continue; |
1168 | if (strtolower(basename($file)) == 'rtl.css') continue; |
de4d58cb |
1169 | |
d4c2aa24 |
1170 | if ($full_path) { |
1171 | $return_array[] = $file; |
1172 | } else { |
1173 | $return_array[] = basename($file); |
1174 | } |
de4d58cb |
1175 | |
1176 | } |
1177 | |
c404d448 |
1178 | |
1179 | // return sheets for the current template set |
1180 | // last so we can enable any custom overrides |
1181 | // of styles in ancestor sheets |
1182 | // |
1183 | return array_reverse($return_array); |
de4d58cb |
1184 | |
1185 | } |
1186 | |
1187 | /** |
1188 | * Generate links to all this template set's standard stylesheets |
1189 | * |
1190 | * Subclasses can override this function if stylesheets are |
1191 | * created differently for the template set's target output |
1192 | * interface. |
1193 | * |
1194 | * @return string The stylesheet links as they should be sent |
1195 | * to the browser. |
1196 | * |
1197 | */ |
1198 | function fetch_standard_stylesheet_links() |
1199 | { |
1200 | |
1201 | $sheets = $this->get_stylesheets(TRUE); |
1202 | return $this->fetch_external_stylesheet_links($sheets); |
1203 | |
1204 | } |
1205 | |
1206 | /** |
1207 | * Push out any other stylesheet links as provided (for |
1208 | * stylesheets not included with the current template set) |
1209 | * |
1210 | * Subclasses can override this function if stylesheets are |
1211 | * created differently for the template set's target output |
1212 | * interface. |
1213 | * |
1214 | * @param mixed $sheets List of the desired stylesheets |
1215 | * (file path to be used in stylesheet |
1216 | * href attribute) to output (or single |
1217 | * stylesheet file path). |
1218 | FIXME: We could make the incoming array more complex so it can |
1219 | also contain the other parameters for create_css_link() |
1220 | such as $name, $alt, $mtype, and $xhtml_end |
1221 | But do we need to? |
1222 | * |
1223 | * @return string The stylesheet links as they should be sent |
1224 | * to the browser. |
1225 | * |
1226 | */ |
1227 | function fetch_external_stylesheet_links($sheets) |
1228 | { |
1229 | |
1230 | if (!is_array($sheets)) $sheets = array($sheets); |
1231 | $output = ''; |
1232 | |
1233 | foreach ($sheets as $sheet) { |
1234 | $output .= create_css_link($sheet); |
1235 | } |
1236 | |
1237 | return $output; |
1238 | |
1239 | } |
1240 | |
1241 | /** |
1242 | * Send HTTP header(s) to browser. |
1243 | * |
1244 | * Subclasses can override this function if headers are |
1245 | * managed differently in the template set's target output |
1246 | * interface. |
1247 | * |
1248 | * @param mixed $headers A list of (or a single) header |
1249 | * text to be sent. |
1250 | * |
1251 | */ |
1252 | function header($headers) |
1253 | { |
1254 | |
1255 | if (!is_array($headers)) $headers = array($headers); |
1256 | |
1257 | foreach ($headers as $header) { |
1258 | header($header); |
1259 | } |
1260 | |
1261 | } |
1262 | |
1263 | /** |
1264 | * Generate a link to the right-to-left stylesheet for |
d4c2aa24 |
1265 | * this template set by getting the "rtl.css" file from |
1266 | * this template set, its parent (or grandparent, etc.) |
1267 | * template set, the fall-back template set, or finally, |
1268 | * fall back to SquirrelMail's own "rtl.css" if need be. |
de4d58cb |
1269 | * |
1270 | * Subclasses can override this function if stylesheets are |
1271 | * created differently for the template set's target output |
1272 | * interface. |
1273 | * |
1274 | * @return string The stylesheet link as it should be sent |
1275 | * to the browser. |
1276 | * |
1277 | */ |
1278 | function fetch_right_to_left_stylesheet_link() |
1279 | { |
1280 | |
1281 | // get right template file |
1282 | // |
1283 | $sheet = $this->get_template_file_path('css/rtl.css'); |
1284 | |
1285 | // fall back to SquirrelMail's own default stylesheet |
1286 | // |
1287 | if (empty($sheet)) { |
1288 | $sheet = SM_PATH . 'css/rtl.css'; |
1289 | } |
1290 | |
1291 | return create_css_link($sheet); |
1292 | |
1293 | } |
1294 | |
1295 | /** |
1296 | * Display the template |
1297 | * |
1298 | * @param string $file The template file to use |
1299 | * |
1300 | */ |
1301 | function display($file) |
1302 | { |
1303 | |
1304 | echo $this->fetch($file); |
1305 | |
1306 | } |
1307 | |
1308 | /** |
1309 | * Applies the template and returns the resultant content string. |
1310 | * |
1311 | * @param string $file The template file to use |
1312 | * |
1313 | * @return string The template contents after applying the given template |
1314 | * |
1315 | */ |
1316 | function fetch($file) { |
1317 | |
1318 | // get right template file |
1319 | // |
1320 | $template = $this->get_template_file_path($file); |
1321 | |
d4c2aa24 |
1322 | |
de4d58cb |
1323 | // special case stylesheet.tpl falls back to SquirrelMail's |
1324 | // own default stylesheet |
1325 | // |
1326 | if (empty($template) && $file == 'css/stylesheet.tpl') { |
1327 | $template = SM_PATH . 'css/default.css'; |
1328 | } |
1329 | |
d4c2aa24 |
1330 | |
de4d58cb |
1331 | if (empty($template)) { |
1332 | |
1333 | trigger_error('The template "' . htmlspecialchars($file) |
1334 | . '" could not be fetched!', E_USER_ERROR); |
1335 | |
1336 | } else { |
1337 | |
1338 | $aPluginOutput = array(); |
1339 | $aPluginOutput = concat_hook_function('template_construct_' . $file, |
d849b570 |
1340 | $temp=array(&$aPluginOutput, &$this)); |
de4d58cb |
1341 | $this->assign('plugin_output', $aPluginOutput); |
1342 | |
d4c2aa24 |
1343 | //$output = $this->apply_template($template); |
1344 | $rendering_engine = $this->get_rendering_template_engine_object($file); |
1345 | $output = $rendering_engine->apply_template($template); |
de4d58cb |
1346 | |
1347 | // CAUTION: USE OF THIS HOOK IS HIGHLY DISCOURAGED AND CAN |
1348 | // RESULT IN NOTICABLE PERFORMANCE DEGREDATION. Plugins |
1349 | // using this hook will probably be rejected by the |
1350 | // SquirrelMail team. |
1351 | // |
d849b570 |
1352 | do_hook('template_output', $output); |
de4d58cb |
1353 | |
1354 | return $output; |
1355 | |
1356 | } |
1357 | |
1358 | } |
1359 | |
1360 | /** |
1361 | * Assigns values to template variables |
1362 | * |
1363 | * Note: this is an abstract method that must be implemented by subclass. |
1364 | * |
1365 | * @param array|string $tpl_var the template variable name(s) |
1366 | * @param mixed $value the value to assign |
1367 | * |
1368 | */ |
1369 | function assign($tpl_var, $value = NULL) { |
1370 | |
1371 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the assign() method.', E_USER_ERROR); |
1372 | |
1373 | } |
1374 | |
1375 | /** |
1376 | * Assigns values to template variables by reference |
1377 | * |
1378 | * Note: this is an abstract method that must be implemented by subclass. |
1379 | * |
1380 | * @param string $tpl_var the template variable name |
1381 | * @param mixed $value the referenced value to assign |
1382 | * |
1383 | */ |
1384 | function assign_by_ref($tpl_var, &$value) { |
1385 | |
1386 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the assign_by_ref() method.', E_USER_ERROR); |
1387 | |
1388 | } |
1389 | |
d4c2aa24 |
1390 | /** |
1391 | * Clears the values of all assigned varaiables. |
1392 | * |
1393 | */ |
1394 | function clear_all_assign() { |
1395 | |
1396 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the clear_all_assign() method.', E_USER_ERROR); |
1397 | |
1398 | } |
1399 | |
1400 | /** |
1401 | * Returns assigned variable value(s). |
1402 | * |
1403 | * @param string $varname If given, the value of that variable |
1404 | * is returned, assuming it has been |
1405 | * previously assigned. If not specified |
1406 | * an array of all assigned variables is |
1407 | * returned. (optional) |
1408 | * |
1409 | * @return mixed Desired single variable value or list of all |
1410 | * assigned variable values. |
1411 | * |
1412 | */ |
1413 | function get_template_vars($varname=NULL) { |
1414 | |
1415 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the get_template_vars() method.', E_USER_ERROR); |
1416 | |
1417 | } |
1418 | |
de4d58cb |
1419 | /** |
1420 | * Appends values to template variables |
1421 | * |
1422 | * Note: this is an abstract method that must be implemented by subclass. |
1423 | * |
1424 | * @param array|string $tpl_var the template variable name(s) |
1425 | * @param mixed $value the value to append |
1426 | * @param boolean $merge when $value is given as an array, |
1427 | * this indicates whether or not that |
1428 | * array itself should be appended as |
1429 | * a new template variable value or if |
1430 | * that array's values should be merged |
1431 | * into the existing array of template |
1432 | * variable values |
1433 | * |
1434 | */ |
1435 | function append($tpl_var, $value = NULL, $merge = FALSE) { |
1436 | |
1437 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the append() method.', E_USER_ERROR); |
1438 | |
1439 | } |
1440 | |
1441 | /** |
1442 | * Appends values to template variables by reference |
1443 | * |
1444 | * Note: this is an abstract method that must be implemented by subclass. |
1445 | * |
1446 | * @param string $tpl_var the template variable name |
1447 | * @param mixed $value the referenced value to append |
1448 | * @param boolean $merge when $value is given as an array, |
1449 | * this indicates whether or not that |
1450 | * array itself should be appended as |
1451 | * a new template variable value or if |
1452 | * that array's values should be merged |
1453 | * into the existing array of template |
1454 | * variable values |
1455 | * |
1456 | */ |
1457 | function append_by_ref($tpl_var, &$value, $merge = FALSE) { |
1458 | |
1459 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the append_by_ref() method.', E_USER_ERROR); |
1460 | |
1461 | } |
1462 | |
1463 | /** |
1464 | * Applys the template and generates final output destined |
1465 | * for the user's browser |
1466 | * |
1467 | * Note: this is an abstract method that must be implemented by subclass. |
1468 | * |
1469 | * @param string $filepath The full file path to the template to be applied |
1470 | * |
1471 | * @return string The output for the given template |
1472 | * |
1473 | */ |
1474 | function apply_template($filepath) { |
1475 | |
1476 | trigger_error('Template subclass (' . $this->template_engine . 'Template.class.php) needs to implement the apply_template() method.', E_USER_ERROR); |
1477 | |
1478 | } |
1479 | |
1480 | } |
1481 | |