Better hook placement
[squirrelmail.git] / class / template / Template.class.php
... / ...
CommitLineData
1<?php
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 &copy; 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
20/** load template functions */
21require(SM_PATH . 'functions/template.php');
22
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()
34 * clear_all_assign()
35 * get_template_vars()
36 * append()
37 * append_by_ref()
38 * apply_template()
39 *
40 * @author Paul Lesniewski <paul at squirrelmail.org>
41 * @package squirrelmail
42 *
43 */
44class Template
45{
46
47 /**
48 * The template ID
49 *
50 * @var string
51 *
52 */
53 var $template_set_id = '';
54
55 /**
56 * The template set base directory (relative path from
57 * the main SquirrelMail directory (SM_PATH))
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 /**
73 * The fall-back template ID
74 *
75 * @var string
76 *
77 */
78 var $fallback_template_set_id = '';
79
80 /**
81 * The fall-back template directory (relative
82 * path from the main SquirrelMail directory (SM_PATH))
83 *
84 * @var string
85 *
86 */
87 var $fallback_template_dir = '';
88
89 /**
90 * The fall-back template engine (please use
91 * constants defined in constants.php)
92 *
93 * @var string
94 *
95 */
96 var $fallback_template_engine = '';
97
98 /**
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:
105 *
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
109 *
110 */
111 var $template_file_cache = array();
112
113 /**
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
124 /**
125 * Constructor
126 *
127 * Please do not call directly. Use Template::construct_template().
128 *
129 * @param string $template_set_id the template ID
130 *
131 */
132 function Template($template_set_id) {
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
139 $this->set_up_template($template_set_id);
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 *
150 * @param string $template_set_id the template ID
151 *
152 * @return object The correct Template object for the given template set
153 *
154 * @static
155 *
156 */
157 function construct_template($template_set_id) {
158
159 $template = new Template($template_set_id);
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 *
170 * @param string $template_set_id the template ID
171 *
172 */
173 function set_up_template($template_set_id) {
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 //
179 $this->template_set_id = $template_set_id;
180
181
182 $this->fallback_template_set_id = Template::get_fallback_template_set();
183
184
185 // set up template directories
186 //
187 $this->template_dir
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);
191
192
193 // determine template engine
194 // FIXME: assuming PHP template engine may not necessarily be a good thing
195 //
196 $this->template_engine = Template::get_template_config($this->template_set_id,
197 'template_engine',
198 SQ_PHP_TEMPLATE);
199
200
201 // get template file cache
202 //
203 $this->template_file_cache = Template::cache_template_file_hierarchy();
204
205 }
206
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') {
225
226// FIXME: do we want to place any restrictions on the ID such as
227// making sure no slashes included?
228
229 // values are in main SM config file
230 //
231 global $templateset_fallback, $aTemplateSet;
232 $aTemplateSet = (!isset($aTemplateSet) || !is_array($aTemplateSet)
233 ? array() : $aTemplateSet);
234 $templateset_fallback = (!isset($templateset_fallback)
235 ? $default : $templateset_fallback);
236
237 // iterate through all template sets, is this a valid skin ID?
238 //
239 $found_it = FALSE;
240 foreach ($aTemplateSet as $aTemplate) {
241 if ($aTemplate['ID'] === $templateset_fallback) {
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;
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)
286 ? $default : $templateset_default);
287
288 // iterate through all template sets, is this a valid skin ID?
289 //
290 $found_it = FALSE;
291 foreach ($aTemplateSet as $aTemplate) {
292 if ($aTemplate['ID'] === $templateset_default) {
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;
306
307 }
308
309 /**
310 * Instantiate and return correct subclass for this template
311 * set's templating engine.
312 *
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 *
318 * @return object The Template subclass object for the template engine.
319 *
320 */
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
328
329 $engine_class_file = SM_PATH . 'class/template/'
330 . $engine . 'Template.class.php';
331
332 if (!file_exists($engine_class_file)) {
333 trigger_error('Unknown template engine (' . $engine
334 . ') was specified in template configuration file',
335 E_USER_ERROR);
336 }
337
338 $engine_class = $engine . 'Template';
339 require_once($engine_class_file);
340 return new $engine_class($template_set_id);
341
342 }
343
344 /**
345 * Determine the relative template directory path for
346 * the given template ID.
347 *
348 * @param string $template_set_id The template ID from which to build
349 * the directory path
350 *
351 * @return string The relative template path (based off of SM_PATH)
352 *
353 * @static
354 *
355 */
356 function calculate_template_file_directory($template_set_id) {
357
358 return 'templates/' . $template_set_id . '/';
359
360 }
361
362 /**
363 * Determine the relative images directory path for
364 * the given template ID.
365 *
366 * @param string $template_set_id The template ID from which to build
367 * the directory path
368 *
369 * @return string The relative images path (based off of SM_PATH)
370 *
371 * @static
372 *
373 */
374 function calculate_template_images_directory($template_set_id) {
375
376 return 'templates/' . $template_set_id . '/images/';
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
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 }
404
405 /**
406 * Return the relative template directory path for the
407 * fallback template set.
408 *
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
573 *
574 */
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
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);
673
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
692 /**
693 * only put file in cache if not already found in earlier template
694 * PATH should be relative to SquirrelMail top directory
695 */
696 if (!isset($file_list[$relative_file])) {
697 $file_list[$relative_file] = array(
698 'PATH' => substr($file,strlen(SM_PATH)),
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;
735
736 }
737
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(
776 'PATH' => substr($file_path, strlen(SM_PATH)),
777 'SET_ID' => $template_set_id,
778 'ENGINE' => $engine,
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 }
808
809 /**
810 * Find the right template file.
811 *
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.
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 *
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
839 * template, or ending with a
840 * slash, indicating that all files
841 * for that directory name should
842 * be returned.
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!
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.
866 *
867 */
868 function get_template_file_path($filename,
869 $directories_ok=FALSE,
870 $directories_only=FALSE) {
871
872 if ($directories_only) $directories_ok = TRUE;
873
874
875 // only looking for directory listing first...
876 //
877 // return list of all files in a directory (and that
878 // of any ancestors)
879 //
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 //
888 if (!$directories_only && strpos($file, $filename) === 0
889 && strpos($file, '/', strlen($filename)) === FALSE)
890 $return_array[] = SM_PATH . $file_info['PATH'];
891
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
907 }
908 return $return_array;
909
910 }
911
912
913 // just looking for singular file or directory below...
914 //
915 // figure out what to do with files not found
916 //
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 }
936
937 // plugins get one more chance
938 //
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.)
949
950 }
951
952 // nothing... return empty string (yes, the else is intentional!)
953 //
954 else return '';
955
956 }
957
958 return SM_PATH . $this->template_file_cache[$filename]['PATH'];
959
960 }
961
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];
1001
1002
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 }
1012
1013
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());
1021
1022
1023 // finally ready to go
1024 //
1025 return $rendering_engine;
1026
1027 }
1028
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) {
1045
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;
1063
1064 if ($full_path) {
1065 $return_array[] = $file;
1066 } else {
1067 $return_array[] = basename($file);
1068 }
1069
1070 }
1071
1072 return $return_array;
1073
1074 }
1075
1076 /**
1077 * Return all alternate stylesheets provided by template.
1078 *
1079 * All (non-empty) directories found in the template set's
1080 * "css/alternates" directory (and that of its ancestors)
1081 * are returned.
1082 *
1083 * Note that prettified names are constructed herein by
1084 * taking the directory name, changing underscores to spaces
1085 * and capitalizing each word in the resultant name.
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 *
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 *
1099 */
1100 function get_alternative_stylesheets($full_path=FALSE) {
1101
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 //
1107 $css_directories = $this->get_template_file_path('css/alternates/', TRUE, TRUE);
1108
1109
1110 // prettify names
1111 //
1112 $return_array = array();
1113 foreach ($css_directories as $directory) {
1114
1115 // CVS directories are not wanted
1116 //
1117 if (strpos($directory, '/CVS') === strlen($directory) - 4) continue;
1118
1119 $pretty_name = ucwords(str_replace('_', ' ', basename($directory)));
1120
1121 if ($full_path) {
1122 $return_array[$directory] = $pretty_name;
1123 } else {
1124 $return_array[basename($directory)] = $pretty_name;
1125 }
1126
1127 }
1128
1129 return $return_array;
1130
1131 }
1132
1133 /**
1134 * Return all standard stylsheets provided by the template.
1135 *
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.
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
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
1161
1162 // need to leave out "rtl.css"
1163 //
1164 $return_array = array();
1165 foreach ($css_files as $file) {
1166
1167 if (substr($file, strlen($file) - 4) != '.css') continue;
1168 if (strtolower(basename($file)) == 'rtl.css') continue;
1169
1170 if ($full_path) {
1171 $return_array[] = $file;
1172 } else {
1173 $return_array[] = basename($file);
1174 }
1175
1176 }
1177
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);
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).
1218FIXME: 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
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.
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
1322
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
1330
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,
1340 $temp=array(&$aPluginOutput, &$this));
1341 $this->assign('plugin_output', $aPluginOutput);
1342
1343 //$output = $this->apply_template($template);
1344 $rendering_engine = $this->get_rendering_template_engine_object($file);
1345 $output = $rendering_engine->apply_template($template);
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 //
1352 do_hook('template_output', $output);
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
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
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