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.
11 * @copyright 2003-2020 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @package squirrelmail
15 * @subpackage Template
20 /** load template functions */
21 require(SM_PATH
. 'functions/template/general_util.php');
24 * The SquirrelMail Template class.
26 * Basic template class for capturing values and pluging them into a template.
27 * This class uses a similar API to Smarty.
29 * Methods that must be implemented by subclasses are as follows (see method
30 * stubs below for further information about expected behavior):
40 * @author Paul Lesniewski <paul at squirrelmail.org>
41 * @package squirrelmail
53 var $template_set_id = '';
56 * The template set base directory (relative path from
57 * the main SquirrelMail directory (SM_PATH))
62 var $template_dir = '';
65 * The template engine (please use constants defined in constants.php)
70 var $template_engine = '';
73 * The content type for this template set
75 var $content_type = '';
78 * The fall-back template ID
83 var $fallback_template_set_id = '';
86 * The fall-back template directory (relative
87 * path from the main SquirrelMail directory (SM_PATH))
92 var $fallback_template_dir = '';
95 * The fall-back template engine (please use
96 * constants defined in constants.php)
101 var $fallback_template_engine = '';
104 * Template file cache. Structured as an array, whose keys
105 * are all the template file names (with path information relative
106 * to the template set's base directory, e.g., "css/style.css")
107 * found in all parent template sets including the ultimate fall-back
108 * template set. Array values are sub-arrays with the
109 * following key-value pairs:
111 * PATH -- file path, relative to SM_PATH
112 * SET_ID -- the ID of the template set that this file belongs to
113 * ENGINE -- the engine needed to render this template file
116 var $template_file_cache = array();
119 * Extra template engine class objects for rendering templates
120 * that require a different engine than the one for the current
121 * template set. Keys should be the name of the template engine,
122 * values are the corresponding class objects.
127 var $other_template_engine_objects = array();
132 * Please do not call directly. Use Template::construct_template().
134 * @param string $template_set_id the template ID
137 function Template($template_set_id) {
138 //FIXME: find a way to test that this is ONLY ever called
139 // from the construct_template() method (I doubt it
140 // is worth the trouble to parse the current stack trace)
142 // trigger_error('Please do not use default Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
144 $this->set_up_template($template_set_id);
151 * This method should always be called instead of trying
152 * to get a Template object from the normal/default constructor,
153 * and is necessary in order to control the return value.
155 * @param string $template_set_id the template ID
157 * @return object The correct Template object for the given template set
162 function construct_template($template_set_id) {
164 $template = new Template($template_set_id);
165 $template->override_plugins();
166 return $template->get_template_engine_subclass();
171 * Set up internal attributes
173 * This method does most of the work for setting up
174 * newly constructed objects.
176 * @param string $template_set_id the template ID
179 function set_up_template($template_set_id) {
181 // FIXME: do we want to place any restrictions on the ID like
182 // making sure no slashes included?
185 $this->template_set_id
= $template_set_id;
188 $this->fallback_template_set_id
= Template
::get_fallback_template_set();
191 // set up template directories
194 = Template
::calculate_template_file_directory($this->template_set_id
);
195 $this->fallback_template_dir
196 = Template
::calculate_template_file_directory($this->fallback_template_set_id
);
199 // determine content type, defaulting to text/html
201 $this->content_type
= Template
::get_template_config($this->template_set_id
,
206 // determine template engine
207 // FIXME: assuming PHP template engine may not necessarily be a good thing
209 $this->template_engine
= Template
::get_template_config($this->template_set_id
,
214 // get template file cache
216 $this->template_file_cache
= Template
::cache_template_file_hierarchy($template_set_id);
221 * Determine what the ultimate fallback template set is.
223 * NOTE that if the fallback setting cannot be found in the
224 * main SquirrelMail configuration settings that the value
225 * of $default is returned.
227 * @param string $default The template set ID to use if
228 * the fallback setting cannot be
229 * found in SM config (optional;
230 * defaults to "default").
232 * @return string The ID of the fallback template set.
237 function get_fallback_template_set($default='default') {
239 // FIXME: do we want to place any restrictions on the ID such as
240 // making sure no slashes included?
242 // values are in main SM config file
244 global $templateset_fallback, $aTemplateSet;
245 $aTemplateSet = (!isset($aTemplateSet) ||
!is_array($aTemplateSet)
246 ?
array() : $aTemplateSet);
247 $templateset_fallback = (!isset($templateset_fallback)
248 ?
$default : $templateset_fallback);
250 // iterate through all template sets, is this a valid skin ID?
253 foreach ($aTemplateSet as $aTemplate) {
254 if ($aTemplate['ID'] === $templateset_fallback) {
261 return $templateset_fallback;
263 // FIXME: note that it is possible for $default to
264 // point to an invalid (nonexistent) template set
265 // and that error will not be caught here
272 * Determine what the default template set is.
274 * NOTE that if the default setting cannot be found in the
275 * main SquirrelMail configuration settings that the value
276 * of $default is returned.
278 * @param string $default The template set ID to use if
279 * the default setting cannot be
280 * found in SM config (optional;
281 * defaults to "default").
283 * @return string The ID of the default template set.
288 function get_default_template_set($default='default') {
290 // FIXME: do we want to place any restrictions on the ID such as
291 // making sure no slashes included?
293 // values are in main SM config file
295 global $templateset_default, $aTemplateSet;
296 $aTemplateSet = (!isset($aTemplateSet) ||
!is_array($aTemplateSet)
297 ?
array() : $aTemplateSet);
298 $templateset_default = (!isset($templateset_default)
299 ?
$default : $templateset_default);
301 // iterate through all template sets, is this a valid skin ID?
304 foreach ($aTemplateSet as $aTemplate) {
305 if ($aTemplate['ID'] === $templateset_default) {
312 return $templateset_default;
314 // FIXME: note that it is possible for $default to
315 // point to an invalid (nonexistent) template set
316 // and that error will not be caught here
323 * Determine what the RPC template set is.
325 * NOTE that if the default setting cannot be found in the
326 * main SquirrelMail configuration settings that the value
327 * of $default is returned.
329 * @param string $default The template set ID to use if
330 * the default setting cannot be
331 * found in SM config (optional;
332 * defaults to "default_rpc").
334 * @return string The ID of the RPC template set.
339 function get_rpc_template_set($default='default_rpc') {
341 // FIXME: do we want to place any restrictions on the ID such as
342 // making sure no slashes included?
344 // values are in main SM config file
346 global $rpc_templateset;
347 $rpc_templateset = (!isset($rpc_templateset)
348 ?
$default : $rpc_templateset);
350 // FIXME: note that it is possible for this to
351 // point to an invalid (nonexistent) template set
352 // and that error will not be caught here
354 return $rpc_templateset;
359 * Allow template set to override plugin configuration by either
360 * adding or removing plugins.
362 * NOTE: due to when this code executes, plugins activated here
363 * do not have access to the config_override and loading_prefs
364 * hooks; instead, such plugins can use the
365 * "template_plugins_override_after" hook defined below.
368 function override_plugins() {
370 global $disable_plugins, $plugins, $squirrelmail_plugin_hooks, $null;
371 if ($disable_plugins) return;
373 $add_plugins = Template
::get_template_config($this->template_set_id
,
374 'add_plugins', array());
375 $remove_plugins = Template
::get_template_config($this->template_set_id
,
376 'remove_plugins', array());
378 //FIXME (?) we assume $add_plugins and $remove_plugins are arrays -- we could
379 // error check here, or just assume that template authors or admins
380 // won't screw up their config files
383 // disable all plugins? (can still add some by using $add_plugins)
385 if (in_array('*', $remove_plugins)) {
387 $squirrelmail_plugin_hooks = array();
388 $remove_plugins = array();
392 foreach ($add_plugins as $plugin_name) {
393 // add plugin to global plugin array
395 $plugins[] = $plugin_name;
398 // enable plugin -- emulate code from use_plugin() function
399 // in SquirrelMail core, but also need to call the
400 // "squirrelmail_plugin_init_<plugin_name>" function, which
401 // in static configuration is not called (this inconsistency
402 // could be a source of anomalous-seeming bugs in poorly
405 if (file_exists(SM_PATH
. "plugins/$plugin_name/setup.php")) {
406 include_once(SM_PATH
. "plugins/$plugin_name/setup.php");
408 $function = "squirrelmail_plugin_init_$plugin_name";
409 if (function_exists($function))
414 foreach ($remove_plugins as $plugin_name) {
415 // remove plugin from both global plugin & plugin hook arrays
417 $plugin_key = array_search($plugin_name, $plugins);
418 if (!is_null($plugin_key) && $plugin_key !== FALSE) {
419 unset($plugins[$plugin_key]);
420 if (is_array($squirrelmail_plugin_hooks))
421 foreach (array_keys($squirrelmail_plugin_hooks) as $hookName) {
422 unset($squirrelmail_plugin_hooks[$hookName][$plugin_name]);
427 do_hook('template_plugins_override_after', $null);
432 * Instantiate and return correct subclass for this template
433 * set's templating engine.
435 * @param string $template_set_id The template set whose engine
436 * is to be used as an override
437 * (if not given, this template
438 * set's engine is used) (optional).
440 * @return object The Template subclass object for the template engine.
443 function get_template_engine_subclass($template_set_id='') {
445 if (empty($template_set_id)) $template_set_id = $this->template_set_id
;
446 // FIXME: assuming PHP template engine may not necessarily be a good thing
447 $engine = Template
::get_template_config($template_set_id,
448 'template_engine', SQ_PHP_TEMPLATE
);
451 $engine_class_file = SM_PATH
. 'class/template/'
452 . $engine . 'Template.class.php';
454 if (!file_exists($engine_class_file)) {
455 trigger_error('Unknown template engine (' . $engine
456 . ') was specified in template configuration file',
460 $engine_class = $engine . 'Template';
461 require_once($engine_class_file);
462 return new $engine_class($template_set_id);
467 * Determine the relative template directory path for
468 * the given template ID.
470 * @param string $template_set_id The template ID from which to build
473 * @return string The relative template path (based off of SM_PATH)
478 function calculate_template_file_directory($template_set_id) {
480 return 'templates/' . $template_set_id . '/';
485 * Determine the relative images directory path for
486 * the given template ID.
488 * @param string $template_set_id The template ID from which to build
491 * @return string The relative images path (based off of SM_PATH)
496 function calculate_template_images_directory($template_set_id) {
498 return 'templates/' . $template_set_id . '/images/';
503 * Return the relative template directory path for this template set.
505 * @return string The relative path to the template directory based
506 * from the main SquirrelMail directory (SM_PATH).
509 function get_template_file_directory() {
511 return $this->template_dir
;
516 * Return the template ID for the fallback template set.
518 * @return string The ID of the fallback template set.
521 function get_fallback_template_set_id() {
523 return $this->fallback_template_set_id
;
528 * Return the relative template directory path for the
529 * fallback template set.
531 * @return string The relative path to the fallback template
532 * directory based from the main SquirrelMail
533 * directory (SM_PATH).
536 function get_fallback_template_file_directory() {
538 return $this->fallback_template_dir
;
543 * Return the content-type for this template set.
545 * @return string The content-type.
548 function get_content_type() {
550 return $this->content_type
;
555 * Get template set config setting
557 * Given a template set ID and setting name, returns the
558 * setting's value. Note that settings are cached in
559 * session, so "live" changes to template configuration
560 * won't be reflected until the user logs out and back
563 * @param string $template_set_id The template set for which
564 * to look up the setting.
565 * @param string $setting The name of the setting to
567 * @param mixed $default When the requested setting
568 * is not found, the contents
569 * of this value are returned
570 * instead (optional; default
572 * NOTE that unlike sqGetGlobalVar(),
573 * this function will also return
574 * the default value if the
575 * requested setting is found
577 * @param boolean $live_config When TRUE, the target template
578 * set's configuration file is
579 * reloaded every time this
580 * method is called. Default
581 * behavior is to only load the
582 * configuration file if it had
583 * never been loaded before, but
584 * not again after that (optional;
585 * default FALSE). Use with care!
586 * Should mostly be used for
589 * @return mixed The desired setting's value or if not found,
590 * the contents of $default are returned.
595 function get_template_config($template_set_id, $setting,
596 $default=NULL, $live_config=FALSE) {
598 sqGetGlobalVar('template_configuration_settings',
599 $template_configuration_settings,
603 if ($live_config) unset($template_configuration_settings[$template_set_id]);
606 // NOTE: could use isset() instead of empty() below, but
607 // this function is designed to replace empty values
608 // as well as non-existing values with $default
610 if (!empty($template_configuration_settings[$template_set_id][$setting]))
611 return $template_configuration_settings[$template_set_id][$setting];
614 // if template set configuration has been loaded, but this
615 // setting is not known, return $default
617 if (!empty($template_configuration_settings[$template_set_id]))
621 // otherwise (template set configuration has not been loaded before),
622 // load it into session and return the desired setting after that
624 $template_config_file = SM_PATH
625 . Template
::calculate_template_file_directory($template_set_id)
628 if (!file_exists($template_config_file)) {
630 trigger_error('No template configuration file was found where expected: ("'
631 . $template_config_file . '")', E_USER_ERROR
);
635 // we require() the file to let PHP do the variable value
636 // parsing for us, and read the file in manually so we can
637 // know what variable names are used in the config file
638 // (settings can be different depending on specific requirements
639 // of different template engines)... the other way this may
640 // be accomplished is to somehow diff the symbol table
641 // before/after the require(), but anyway, this code should
642 // only run once for this template set...
644 require($template_config_file);
645 $file_contents = implode("\n", file($template_config_file));
648 // note that this assumes no template settings have
649 // a string in them that looks like a variable name like $x
650 // also note that this will attempt to grab things like
651 // $Id found in CVS headers, so we try to adjust for that
652 // by checking that the variable is actually set
654 preg_match_all('/\$(\w+)/', $file_contents, $variables, PREG_PATTERN_ORDER
);
655 foreach ($variables[1] as $variable) {
656 if (isset($
$variable))
657 $template_configuration_settings[$template_set_id][$variable]
661 sqsession_register($template_configuration_settings,
662 'template_configuration_settings');
664 // NOTE: could use isset() instead of empty() below, but
665 // this function is designed to replace empty values
666 // as well as non-existing values with $default
668 if (!empty($template_configuration_settings[$template_set_id][$setting]))
669 return $template_configuration_settings[$template_set_id][$setting];
678 * Obtain template file hierarchy from cache.
680 * If the file hierarchy does not exist in session, it is
681 * constructed and stored in session before being returned
684 * @param string $template_set_id The template set for which
685 * the cache should be built.
686 * This function will save more
687 * than one set's files, so it
688 * may be called multiple times
689 * with different values for this
690 * argument. When regenerating,
691 * all set caches are dumped.
692 * @param boolean $regenerate_cache When TRUE, the file hierarchy
693 * is reloaded and stored fresh
694 * (optional; default FALSE).
695 * @param array $additional_files Must be in same form as the
696 * files in the file hierarchy
697 * cache. These are then added
698 * to the cache (optional; default
699 * empty - no additional files).
701 * @return array Template file hierarchy array, whose keys
702 * are all the template file names for the given
703 * template set ID (with path information relative
704 * to the template set's base directory, e.g.,
705 * "css/style.css") found in all parent template
706 * sets including the ultimate fall-back template
707 * set. Array values are sub-arrays with the
708 * following key-value pairs:
710 * PATH -- file path, relative to SM_PATH
711 * SET_ID -- the ID of the template set that this file belongs to
712 * ENGINE -- the engine needed to render this template file
717 function cache_template_file_hierarchy($template_set_id,
718 $regenerate_cache=FALSE,
719 $additional_files=array()) {
721 sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy,
722 SQ_SESSION
, array());
725 if ($regenerate_cache) unset($template_file_hierarchy);
727 if (!empty($template_file_hierarchy[$template_set_id])) {
729 // have to add additional files if given before returning
731 if (!empty($additional_files)) {
732 $template_file_hierarchy[$template_set_id]
733 = array_merge($template_file_hierarchy[$template_set_id],
736 sqsession_register($template_file_hierarchy,
737 'template_file_hierarchy');
740 return $template_file_hierarchy[$template_set_id];
744 // nothing in cache apparently, so go build it now
746 $template_file_hierarchy[$template_set_id] = Template
::catalog_template_files($template_set_id);
748 // additional files, if any
750 if (!empty($additional_files)) {
751 $template_file_hierarchy[$template_set_id]
752 = array_merge($template_file_hierarchy[$template_set_id],
756 sqsession_register($template_file_hierarchy,
757 'template_file_hierarchy');
759 return $template_file_hierarchy[$template_set_id];
764 * Traverse template hierarchy and catalogue all template
765 * files (for storing in cache).
767 * Paths to all files in all parent, grand-parent, great grand
768 * parent, etc. template sets (including the fallback template)
769 * are catalogued; for identically named files, the file earlier
770 * in the hierarchy (closest to this template set) is used.
772 * Refuses to traverse directories called ".svn"
774 * @param string $template_set_id The template set in which to
776 * @param array $file_list The file list so far to be added
777 * to (allows recursive behavior)
778 * (optional; default empty array).
779 * @param string $directory The directory in which to search for
780 * files (must be given as full path).
781 * If empty, starts at top-level template
782 * set directory (optional; default empty).
783 * NOTE! Use with care, as behavior is
784 * unpredictable if directory given is not
785 * part of correct template set.
787 * @return mixed The top-level caller will have an array of template
788 * files returned to it; recursive calls to this function
789 * do not receive any return value at all. The format
790 * of the template file array is as described for the
791 * Template class attribute $template_file_cache
796 function catalog_template_files($template_set_id, $file_list=array(), $directory='') {
798 $template_base_dir = SM_PATH
799 . Template
::calculate_template_file_directory($template_set_id);
801 if (empty($directory)) {
802 $directory = $template_base_dir;
806 // bail if we have been asked to traverse a Subversion directory
808 if (strpos($directory, '/.svn') === strlen($directory) - 5) return $file_list;
811 $files_and_dirs = list_files($directory, '', FALSE, TRUE, FALSE, TRUE);
813 // recurse for all the subdirectories in the template set
815 foreach ($files_and_dirs['DIRECTORIES'] as $dir) {
816 $file_list = Template
::catalog_template_files($template_set_id, $file_list, $dir);
819 // place all found files in the cache
820 // FIXME: assuming PHP template engine may not necessarily be a good thing
822 $engine = Template
::get_template_config($template_set_id,
823 'template_engine', SQ_PHP_TEMPLATE
);
824 foreach ($files_and_dirs['FILES'] as $file) {
826 // remove the part of the file path corresponding to the
827 // template set's base directory
829 $relative_file = substr($file, strlen($template_base_dir));
832 * only put file in cache if not already found in earlier template
833 * PATH should be relative to SquirrelMail top directory
835 if (!isset($file_list[$relative_file])) {
836 $file_list[$relative_file] = array(
837 'PATH' => substr($file,strlen(SM_PATH
)),
838 'SET_ID' => $template_set_id,
846 // now if we are currently at the top-level of the template
847 // set base directory, we need to move on to the parent
848 // template set, if any
850 if ($directory == $template_base_dir) {
852 // use fallback when we run out of parents
854 $fallback_id = Template
::get_fallback_template_set();
855 $parent_id = Template
::get_template_config($template_set_id,
856 'parent_template_set',
859 // were we already all the way to the last level? just exit
861 // note that this code allows the fallback set to have
862 // a parent, too, but can result in endless loops
863 // if ($parent_id == $template_set_id) {
865 if ($fallback_id == $template_set_id) {
869 $file_list = Template
::catalog_template_files($parent_id, $file_list);
878 * Look for a template file in a plugin; add to template
879 * file cache if found.
881 * The file is searched for in the following order:
883 * - A directory for the current template set within the plugin:
884 * SM_PATH/plugins/<plugin name>/templates/<template name>/
885 * - In a directory for one of the current template set's ancestor
886 * (inherited) template sets within the plugin:
887 * SM_PATH/plugins/<plugin name>/templates/<parent template name>/
888 * - In a directory for the fallback template set within the plugin:
889 * SM_PATH/plugins/<plugin name>/templates/<fallback template name>/
891 * @param string $plugin The name of the plugin
892 * @param string $file The name of the template file
893 * @param string $template_set_id The ID of the template for which
894 * to start looking for the file
895 * (optional; default is current
898 * @return boolean TRUE if the template file was found, FALSE otherwise.
901 function find_and_cache_plugin_template_file($plugin, $file, $template_set_id='') {
903 if (empty($template_set_id))
904 $template_set_id = $this->template_set_id
;
906 $file_path = SM_PATH
. 'plugins/' . $plugin . '/'
907 . $this->calculate_template_file_directory($template_set_id)
910 if (file_exists($file_path)) {
911 // FIXME: assuming PHP template engine may not necessarily be a good thing
912 $engine = $this->get_template_config($template_set_id,
913 'template_engine', SQ_PHP_TEMPLATE
);
914 $file_list = array('plugins/' . $plugin . '/' . $file => array(
915 'PATH' => substr($file_path, strlen(SM_PATH
)),
916 'SET_ID' => $template_set_id,
920 $this->template_file_cache
921 = $this->cache_template_file_hierarchy($this->template_set_id
,
928 // not found yet, try parent template set
929 // (use fallback when we run out of parents)
931 $fallback_id = $this->get_fallback_template_set();
932 $parent_id = $this->get_template_config($template_set_id,
933 'parent_template_set',
936 // were we already all the way to the last level? just exit
938 // note that this code allows the fallback set to have
939 // a parent, too, but can result in endless loops
940 // if ($parent_id == $template_set_id) {
942 if ($fallback_id == $template_set_id) {
946 return $this->find_and_cache_plugin_template_file($plugin, $file, $parent_id);
951 * Find the right template file.
953 * The template file is taken from the template file cache, thus
954 * the file is taken from the current template, one of its
955 * ancestors or the fallback template.
957 * Note that it is perfectly acceptable to load template files from
958 * template subdirectories. For example, JavaScript templates found
959 * in the js/ subdirectory would be loaded by passing
960 * "js/<javascript file name>" as the $filename.
962 * Note that the caller can also ask for ALL files in a directory
963 * (and those in the same directory for all ancestor template sets)
964 * by giving a $filename that is a directory name (ending with a
967 * If not found and the file is a plugin template file (indicated
968 * by the presence of "plugins/" on the beginning of $filename),
969 * the target plugin is searched for a substitue template file
970 * before just returning nothing.
972 * Plugin authors must note that the $filename MUST be prefaced
973 * with "plugins/<plugin name>/" in order to correctly resolve the
976 * @param string $filename The name of the template file,
977 * possibly prefaced with
978 * "plugins/<plugin name>/"
979 * indicating that it is a plugin
980 * template, or ending with a
981 * slash, indicating that all files
982 * for that directory name should
984 * @param boolean $directories_ok When TRUE, directory names
985 * are acceptable search values,
986 * and when returning a list of
987 * directory contents, sub-directory
988 * names will also be included
989 * (optional; default FALSE).
990 * NOTE that empty directories
991 * are NOT included in the cache!
992 * @param boolean $directories_only When TRUE, only directory names
993 * are included in the returned
994 * results. (optional; default
995 * FALSE). Setting this argument
996 * to TRUE forces $directories_ok
998 * NOTE that empty directories
999 * are NOT included in the cache!
1001 * @return mixed The full path to the template file or a list
1002 * of all files in the given directory if $filename
1003 * ends with a slash; if not found, an empty string
1004 * is returned. The caller is responsible for
1005 * throwing errors or other actions if template
1006 * file is not found.
1009 function get_template_file_path($filename,
1010 $directories_ok=FALSE,
1011 $directories_only=FALSE) {
1013 if ($directories_only) $directories_ok = TRUE;
1016 // only looking for directory listing first...
1018 // return list of all files in a directory (and that
1019 // of any ancestors)
1021 if ($filename{strlen($filename) - 1} == '/') {
1023 $return_array = array();
1024 foreach ($this->template_file_cache
as $file => $file_info) {
1026 // only want files in the requested directory
1027 // (AND not in a subdirectory!)
1029 if (!$directories_only && strpos($file, $filename) === 0
1030 && strpos($file, '/', strlen($filename)) === FALSE)
1031 $return_array[] = SM_PATH
. $file_info['PATH'];
1033 // directories too? detect by finding any
1034 // array key that matches a file in a sub-directory
1035 // of the directory being processed
1037 if ($directories_ok && strpos($file, $filename) === 0
1038 && ($pos = strpos($file, '/', strlen($filename))) !== FALSE
1039 && strpos($file, '/', $pos +
1) === FALSE) {
1040 $directory_name = SM_PATH
1041 . substr($file_info['PATH'],
1043 strrpos($file_info['PATH'], '/'));
1044 if (!in_array($directory_name, $return_array))
1045 $return_array[] = $directory_name;
1049 return $return_array;
1054 // just looking for singular file or directory below...
1056 // figure out what to do with files not found
1058 if ($directories_only ||
empty($this->template_file_cache
[$filename]['PATH'])) {
1060 // if looking for directories...
1061 // have to iterate through cache and detect
1062 // directory by matching any file inside of it
1064 if ($directories_ok) {
1065 foreach ($this->template_file_cache
as $file => $file_info) {
1066 if (strpos($file, $filename) === 0
1067 && ($pos = strpos($file, '/', strlen($filename))) !== FALSE
1068 && strpos($file, '/', $pos +
1) === FALSE) {
1069 return SM_PATH
. substr($file_info['PATH'],
1071 strrpos($file_info['PATH'], '/'));
1075 if ($directories_only) return '';
1078 // plugins get one more chance
1080 if (strpos($filename, 'plugins/') === 0) {
1082 $plugin_name = substr($filename, 8, strpos($filename, '/', 8) - 8);
1083 $file = substr($filename, strlen($plugin_name) +
9);
1085 if (!$this->find_and_cache_plugin_template_file($plugin_name, $file))
1087 //FIXME: technically I guess we should check for directories
1088 // here too, but that's overkill (no need) presently
1089 // (plugin-provided alternate stylesheet dirs?!? bah.)
1093 // nothing... return empty string (yes, the else is intentional!)
1099 return SM_PATH
. $this->template_file_cache
[$filename]['PATH'];
1104 * Get template engine needed to render given template file.
1106 * If at all possible, just returns a reference to $this, but
1107 * some template files may require a different engine, thus
1108 * an object for that engine (which will subsequently be kept
1109 * in this object for future use) is returned.
1111 * @param string $filename The name of the template file,
1113 * @return object The needed template object to render the template.
1116 function get_rendering_template_engine_object($filename) {
1118 // for files that we cannot find engine info for,
1119 // just return $this
1121 if (empty($this->template_file_cache
[$filename]['ENGINE']))
1125 // otherwise, compare $this' engine to the file's engine
1127 $engine = $this->template_file_cache
[$filename]['ENGINE'];
1128 if ($this->template_engine
== $engine)
1132 // need to load another engine... if already instantiated,
1133 // and stored herein, return that
1134 // FIXME: this assumes same engine setup in all template
1135 // set config files that have same engine in common
1136 // (but keeping a separate class object for every
1137 // template set seems like overkill... for now we
1138 // won't do that unless it becomes a problem)
1140 if (!empty($this->other_template_engine_objects
[$engine])) {
1141 $rendering_engine = $this->other_template_engine_objects
[$engine];
1144 // otherwise, instantiate new engine object, add to cache
1148 $template_set_id = $this->template_file_cache
[$filename]['SET_ID'];
1149 $this->other_template_engine_objects
[$engine]
1150 = $this->get_template_engine_subclass($template_set_id);
1151 $rendering_engine = $this->other_template_engine_objects
[$engine];
1155 // now, need to copy over all the assigned variables
1156 // from $this to the rendering engine (YUCK! -- we need
1157 // to discourage template authors from creating
1158 // situations where engine changes occur)
1160 $rendering_engine->clear_all_assign();
1161 $rendering_engine->assign($this->get_template_vars());
1164 // finally ready to go
1166 return $rendering_engine;
1171 * Return all JavaScript files provided by the template.
1173 * All files found in the template set's "js" directory (and
1174 * that of its ancestors) with the extension ".js" are returned.
1176 * @param boolean $full_path When FALSE, only the file names
1177 * are included in the return array;
1178 * otherwise, path information is
1179 * included (relative to SM_PATH)
1180 * (OPTIONAL; default only file names)
1182 * @return array The required file names/paths.
1185 function get_javascript_includes($full_path=FALSE) {
1187 // since any page from a parent template set
1188 // could end up being loaded, we have to load
1189 // all js files from ancestor template sets,
1190 // not just this set
1192 //$directory = SM_PATH . $this->get_template_file_directory() . 'js';
1193 //$js_files = list_files($directory, '.js', !$full_path);
1195 $js_files = $this->get_template_file_path('js/');
1198 // parse out .js files only
1200 $return_array = array();
1201 foreach ($js_files as $file) {
1203 if (substr($file, strlen($file) - 3) != '.js') continue;
1206 $return_array[] = $file;
1208 $return_array[] = basename($file);
1213 return $return_array;
1218 * Return all alternate stylesheets provided by template.
1220 * All (non-empty) directories found in the template set's
1221 * "css/alternates" directory (and that of its ancestors)
1224 * Note that prettified names are constructed herein by
1225 * taking the directory name, changing underscores to spaces
1226 * and capitalizing each word in the resultant name.
1228 * @param boolean $full_path When FALSE, only the file names
1229 * are included in the return array;
1230 * otherwise, path information is
1231 * included (relative to SM_PATH)
1232 * (OPTIONAL; default only file names)
1234 * @return array A list of the available alternate stylesheets,
1235 * where the keys are the file names (formatted
1236 * according to $full_path) for the stylesheets,
1237 * and the values are the prettified version of
1238 * the file names for display to the user.
1241 function get_alternative_stylesheets($full_path=FALSE) {
1243 // since any page from a parent template set
1244 // could end up being loaded, we will load
1245 // all alternate css files from ancestor
1246 // template sets, not just this set
1248 $css_directories = $this->get_template_file_path('css/alternates/', TRUE, TRUE);
1253 $return_array = array();
1254 foreach ($css_directories as $directory) {
1256 // CVS and SVN directories are not wanted
1258 if ((strpos($directory, '/CVS') === strlen($directory) - 4)
1259 ||
(strpos($directory, '/.svn') === strlen($directory) - 5)) continue;
1261 $pretty_name = ucwords(str_replace('_', ' ', basename($directory)));
1264 $return_array[$directory] = $pretty_name;
1266 $return_array[basename($directory)] = $pretty_name;
1271 return $return_array;
1276 * Return all standard stylsheets provided by the template.
1278 * All files found in the template set's "css" directory (and
1279 * that of its ancestors) with the extension ".css" except
1280 * "rtl.css" (which is dealt with separately) are returned.
1282 * @param boolean $full_path When FALSE, only the file names
1283 * are included in the return array;
1284 * otherwise, path information is
1285 * included (relative to SM_PATH)
1286 * (OPTIONAL; default only file names)
1288 * @return array The required file names/paths.
1291 function get_stylesheets($full_path=FALSE) {
1293 // since any page from a parent template set
1294 // could end up being loaded, we have to load
1295 // all css files from ancestor template sets,
1296 // not just this set
1298 //$directory = SM_PATH . $this->get_template_file_directory() . 'css';
1299 //$css_files = list_files($directory, '.css', !$full_path);
1301 $css_files = $this->get_template_file_path('css/');
1304 // need to leave out "rtl.css"
1306 $return_array = array();
1307 foreach ($css_files as $file) {
1309 if (substr($file, strlen($file) - 4) != '.css') continue;
1310 if (strtolower(basename($file)) == 'rtl.css') continue;
1313 $return_array[] = $file;
1315 $return_array[] = basename($file);
1321 // return sheets for the current template set
1322 // last so we can enable any custom overrides
1323 // of styles in ancestor sheets
1325 return array_reverse($return_array);
1330 * Generate links to all this template set's standard stylesheets
1332 * Subclasses can override this function if stylesheets are
1333 * created differently for the template set's target output
1336 * @return string The stylesheet links as they should be sent
1340 function fetch_standard_stylesheet_links()
1343 $sheets = $this->get_stylesheets(TRUE);
1344 return $this->fetch_external_stylesheet_links($sheets);
1349 * Push out any other stylesheet links as provided (for
1350 * stylesheets not included with the current template set)
1352 * Subclasses can override this function if stylesheets are
1353 * created differently for the template set's target output
1356 * @param mixed $sheets List of the desired stylesheets
1357 * (file path to be used in stylesheet
1358 * href attribute) to output (or single
1359 * stylesheet file path).
1360 FIXME: We could make the incoming array more complex so it can
1361 also contain the other parameters for create_css_link()
1362 such as $name, $alt, $mtype, and $xhtml_end
1365 * @return string The stylesheet links as they should be sent
1369 function fetch_external_stylesheet_links($sheets)
1372 if (!is_array($sheets)) $sheets = array($sheets);
1375 foreach ($sheets as $sheet) {
1376 $output .= create_css_link($sheet);
1384 * Send HTTP header(s) to browser.
1386 * Subclasses can override this function if headers are
1387 * managed differently in the engine's target output
1390 * @param mixed $headers A list of (or a single) header
1392 * @param boolean $replace Whether or not to replace header(s)
1393 * previously sent header(s) of the
1394 * same type (this parameter may be
1395 * ignored in some implementations
1396 * of this class if the target interface
1397 * does not support this functionality)
1398 * (OPTIONAL; default = TRUE, always replace).
1401 function header($headers, $replace=TRUE)
1404 if (!is_array($headers)) $headers = array($headers);
1406 foreach ($headers as $header) {
1407 $this->assign('header', $header);
1408 header($this->fetch('header.tpl'), $replace);
1414 * Generate a link to the right-to-left stylesheet for
1415 * this template set by getting the "rtl.css" file from
1416 * this template set, its parent (or grandparent, etc.)
1417 * template set, the fall-back template set, or finally,
1418 * fall back to SquirrelMail's own "rtl.css" if need be.
1420 * Subclasses can override this function if stylesheets are
1421 * created differently for the template set's target output
1424 * @return string The stylesheet link as it should be sent
1428 function fetch_right_to_left_stylesheet_link()
1431 // get right template file
1433 $sheet = $this->get_template_file_path('css/rtl.css');
1435 // fall back to SquirrelMail's own default stylesheet
1437 if (empty($sheet)) {
1438 $sheet = SM_PATH
. 'css/rtl.css';
1441 return create_css_link($sheet);
1446 * Display the template
1448 * @param string $file The template file to use
1451 function display($file)
1454 echo $this->fetch($file);
1459 * Applies the template and returns the resultant content string.
1461 * @param string $file The template file to use
1463 * @return string The template contents after applying the given template
1466 function fetch($file) {
1468 // get right template file
1470 $template = $this->get_template_file_path($file);
1473 // special case stylesheet.tpl falls back to SquirrelMail's
1474 // own default stylesheet
1476 if (empty($template) && $file == 'css/stylesheet.tpl') {
1477 $template = SM_PATH
. 'css/default.css';
1481 if (empty($template)) {
1483 trigger_error('The template "' . sm_encode_html_special_chars($file)
1484 . '" could not be fetched!', E_USER_ERROR
);
1488 $aPluginOutput = array();
1489 $temp = array(&$aPluginOutput, &$this);
1490 $aPluginOutput = concat_hook_function('template_construct_' . $file,
1492 $this->assign('plugin_output', $aPluginOutput);
1494 //$output = $this->apply_template($template);
1495 $rendering_engine = $this->get_rendering_template_engine_object($file);
1496 $output = $rendering_engine->apply_template($template);
1498 // CAUTION: USE OF THIS HOOK IS HIGHLY DISCOURAGED AND CAN
1499 // RESULT IN NOTICABLE PERFORMANCE DEGREDATION. Plugins
1500 // using this hook will probably be rejected by the
1501 // SquirrelMail team.
1503 do_hook('template_output', $output);
1512 * Assigns values to template variables
1514 * Note: this is an abstract method that must be implemented by subclass.
1516 * @param array|string $tpl_var the template variable name(s)
1517 * @param mixed $value the value to assign
1520 function assign($tpl_var, $value = NULL) {
1522 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the assign() method.', E_USER_ERROR
);
1527 * Assigns values to template variables by reference
1529 * Note: this is an abstract method that must be implemented by subclass.
1531 * @param string $tpl_var the template variable name
1532 * @param mixed $value the referenced value to assign
1535 function assign_by_ref($tpl_var, &$value) {
1537 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the assign_by_ref() method.', E_USER_ERROR
);
1542 * Clears the values of all assigned varaiables.
1545 function clear_all_assign() {
1547 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the clear_all_assign() method.', E_USER_ERROR
);
1552 * Returns assigned variable value(s).
1554 * @param string $varname If given, the value of that variable
1555 * is returned, assuming it has been
1556 * previously assigned. If not specified
1557 * an array of all assigned variables is
1558 * returned. (optional)
1560 * @return mixed Desired single variable value or list of all
1561 * assigned variable values.
1564 function get_template_vars($varname=NULL) {
1566 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the get_template_vars() method.', E_USER_ERROR
);
1571 * Appends values to template variables
1573 * Note: this is an abstract method that must be implemented by subclass.
1575 * @param array|string $tpl_var the template variable name(s)
1576 * @param mixed $value the value to append
1577 * @param boolean $merge when $value is given as an array,
1578 * this indicates whether or not that
1579 * array itself should be appended as
1580 * a new template variable value or if
1581 * that array's values should be merged
1582 * into the existing array of template
1586 function append($tpl_var, $value = NULL, $merge = FALSE) {
1588 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the append() method.', E_USER_ERROR
);
1593 * Appends values to template variables by reference
1595 * Note: this is an abstract method that must be implemented by subclass.
1597 * @param string $tpl_var the template variable name
1598 * @param mixed $value the referenced value to append
1599 * @param boolean $merge when $value is given as an array,
1600 * this indicates whether or not that
1601 * array itself should be appended as
1602 * a new template variable value or if
1603 * that array's values should be merged
1604 * into the existing array of template
1608 function append_by_ref($tpl_var, &$value, $merge = FALSE) {
1610 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the append_by_ref() method.', E_USER_ERROR
);
1615 * Applys the template and generates final output destined
1616 * for the user's browser
1618 * Note: this is an abstract method that must be implemented by subclass.
1620 * @param string $filepath The full file path to the template to be applied
1622 * @return string The output for the given template
1625 function apply_template($filepath) {
1627 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the apply_template() method.', E_USER_ERROR
);