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-2006 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.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 fall-back template ID
78 var $fallback_template_set_id = '';
81 * The fall-back template directory (relative
82 * path from the main SquirrelMail directory (SM_PATH))
87 var $fallback_template_dir = '';
90 * The fall-back template engine (please use
91 * constants defined in constants.php)
96 var $fallback_template_engine = '';
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:
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
111 var $template_file_cache = array();
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.
122 var $other_template_engine_objects = array();
127 * Please do not call directly. Use Template::construct_template().
129 * @param string $template_set_id the template ID
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)
137 // trigger_error('Please do not use default Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
139 $this->set_up_template($template_set_id);
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.
150 * @param string $template_set_id the template ID
152 * @return object The correct Template object for the given template set
157 function construct_template($template_set_id) {
159 $template = new Template($template_set_id);
160 return $template->get_template_engine_subclass();
165 * Set up internal attributes
167 * This method does most of the work for setting up
168 * newly constructed objects.
170 * @param string $template_set_id the template ID
173 function set_up_template($template_set_id) {
175 // FIXME: do we want to place any restrictions on the ID like
176 // making sure no slashes included?
179 $this->template_set_id
= $template_set_id;
182 $this->fallback_template_set_id
= Template
::get_fallback_template_set();
185 // set up template directories
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
);
193 // determine template engine
194 // FIXME: assuming PHP template engine may not necessarily be a good thing
196 $this->template_engine
= Template
::get_template_config($this->template_set_id
,
201 // get template file cache
203 $this->template_file_cache
= Template
::cache_template_file_hierarchy();
208 * Determine what the ultimate fallback template set is.
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.
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").
219 * @return string The ID of the fallback template set.
224 function get_fallback_template_set($default='default') {
226 // FIXME: do we want to place any restrictions on the ID such as
227 // making sure no slashes included?
229 // values are in main SM config file
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);
237 // iterate through all template sets, is this a valid skin ID?
240 foreach ($aTemplateSet as $aTemplate) {
241 if ($aTemplate['ID'] === $templateset_fallback) {
248 return $templateset_fallback;
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
259 * Determine what the default template set is.
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.
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").
270 * @return string The ID of the default template set.
275 function get_default_template_set($default='default') {
277 // FIXME: do we want to place any restrictions on the ID such as
278 // making sure no slashes included?
280 // values are in main SM config file
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);
288 // iterate through all template sets, is this a valid skin ID?
291 foreach ($aTemplateSet as $aTemplate) {
292 if ($aTemplate['ID'] === $templateset_default) {
299 return $templateset_default;
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
310 * Instantiate and return correct subclass for this template
311 * set's templating engine.
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).
318 * @return object The Template subclass object for the template engine.
321 function get_template_engine_subclass($template_set_id='') {
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
);
329 $engine_class_file = SM_PATH
. 'class/template/'
330 . $engine . 'Template.class.php';
332 if (!file_exists($engine_class_file)) {
333 trigger_error('Unknown template engine (' . $engine
334 . ') was specified in template configuration file',
338 $engine_class = $engine . 'Template';
339 require_once($engine_class_file);
340 return new $engine_class($template_set_id);
345 * Determine the relative template directory path for
346 * the given template ID.
348 * @param string $template_set_id The template ID from which to build
351 * @return string The relative template path (based off of SM_PATH)
356 function calculate_template_file_directory($template_set_id) {
358 return 'templates/' . $template_set_id . '/';
363 * Determine the relative images directory path for
364 * the given template ID.
366 * @param string $template_set_id The template ID from which to build
369 * @return string The relative images path (based off of SM_PATH)
374 function calculate_template_images_directory($template_set_id) {
376 return 'templates/' . $template_set_id . '/images/';
381 * Return the relative template directory path for this template set.
383 * @return string The relative path to the template directory based
384 * from the main SquirrelMail directory (SM_PATH).
387 function get_template_file_directory() {
389 return $this->template_dir
;
394 * Return the template ID for the fallback template set.
396 * @return string The ID of the fallback template set.
399 function get_fallback_template_set_id() {
401 return $this->fallback_template_set_id
;
406 * Return the relative template directory path for the
407 * fallback template set.
409 * @return string The relative path to the fallback template
410 * directory based from the main SquirrelMail
411 * directory (SM_PATH).
414 function get_fallback_template_file_directory() {
416 return $this->fallback_template_dir
;
421 * Get template set config setting
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
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
433 * @param mixed $default When the requested setting
434 * is not found, the contents
435 * of this value are returned
436 * instead (optional; default
438 * NOTE that unlike sqGetGlobalVar(),
439 * this function will also return
440 * the default value if the
441 * requested setting is found
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
455 * @return mixed The desired setting's value or if not found,
456 * the contents of $default are returned.
461 function get_template_config($template_set_id, $setting,
462 $default=NULL, $live_config=FALSE) {
464 sqGetGlobalVar('template_configuration_settings',
465 $template_configuration_settings,
469 if ($live_config) unset($template_configuration_settings[$template_set_id]);
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
476 if (!empty($template_configuration_settings[$template_set_id][$setting]))
477 return $template_configuration_settings[$template_set_id][$setting];
480 // if template set configuration has been loaded, but this
481 // setting is not known, return $default
483 if (!empty($template_configuration_settings[$template_set_id]))
487 // otherwise (template set configuration has not been loaded before),
488 // load it into session and return the desired setting after that
490 $template_config_file = SM_PATH
491 . Template
::calculate_template_file_directory($template_set_id)
494 if (!file_exists($template_config_file)) {
496 trigger_error('No template configuration file was found where expected: ("'
497 . $template_config_file . '")', E_USER_ERROR
);
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...
510 require($template_config_file);
511 $file_contents = implode("\n", file($template_config_file));
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
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]
527 sqsession_register($template_configuration_settings,
528 'template_configuration_settings');
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
534 if (!empty($template_configuration_settings[$template_set_id][$setting]))
535 return $template_configuration_settings[$template_set_id][$setting];
544 * Obtain template file hierarchy from cache.
546 * If the file hierarchy does not exist in session, it is
547 * constructed and stored in session before being returned
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).
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:
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
575 function cache_template_file_hierarchy($regenerate_cache=FALSE,
576 $additional_files=array()) {
578 sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy,
579 SQ_SESSION
, array());
582 if ($regenerate_cache) unset($template_file_hierarchy);
584 if (!empty($template_file_hierarchy)) {
586 // have to add additional files if given before returning
588 if (!empty($additional_files)) {
589 $template_file_hierarchy = array_merge($template_file_hierarchy,
591 sqsession_register($template_file_hierarchy,
592 'template_file_hierarchy');
595 return $template_file_hierarchy;
599 // nothing in cache apparently, so go build it now
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
608 if (empty($sTemplateID)) {
610 trigger_error('Template set ID unknown', E_USER_ERROR
);
614 $template_file_hierarchy = Template
::catalog_template_files($sTemplateID);
616 // additional files, if any
618 if (!empty($additional_files)) {
619 $template_file_hierarchy = array_merge($template_file_hierarchy,
623 sqsession_register($template_file_hierarchy,
624 'template_file_hierarchy');
626 return $template_file_hierarchy;
633 * Traverse template hierarchy and catalogue all template
634 * files (for storing in cache).
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.
641 * @param string $template_set_id The template set in which to
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.
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
663 function catalog_template_files($template_set_id, $file_list=array(), $directory='') {
665 $template_base_dir = SM_PATH
666 . Template
::calculate_template_file_directory($template_set_id);
668 if (empty($directory)) {
669 $directory = $template_base_dir;
672 $files_and_dirs = list_files($directory, '', FALSE, TRUE, FALSE, TRUE);
674 // recurse for all the subdirectories in the template set
676 foreach ($files_and_dirs['DIRECTORIES'] as $dir) {
677 $file_list = Template
::catalog_template_files($template_set_id, $file_list, $dir);
680 // place all found files in the cache
681 // FIXME: assuming PHP template engine may not necessarily be a good thing
683 $engine = Template
::get_template_config($template_set_id,
684 'template_engine', SQ_PHP_TEMPLATE
);
685 foreach ($files_and_dirs['FILES'] as $file) {
687 // remove the part of the file path corresponding to the
688 // template set's base directory
690 $relative_file = substr($file, strlen($template_base_dir));
693 * only put file in cache if not already found in earlier template
694 * PATH should be relative to SquirrelMail top directory
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,
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
711 if ($directory == $template_base_dir) {
713 // use fallback when we run out of parents
715 $fallback_id = Template
::get_fallback_template_set();
716 $parent_id = Template
::get_template_config($template_set_id,
717 'parent_template_set',
720 // were we already all the way to the last level? just exit
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) {
726 if ($fallback_id == $template_set_id) {
730 $file_list = Template
::catalog_template_files($parent_id, $file_list);
739 * Look for a template file in a plugin; add to template
740 * file cache if found.
742 * The file is searched for in the following order:
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>/
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
759 * @return boolean TRUE if the template file was found, FALSE otherwise.
762 function find_and_cache_plugin_template_file($plugin, $file, $template_set_id='') {
764 if (empty($template_set_id))
765 $template_set_id = $this->template_set_id
;
767 $file_path = SM_PATH
. 'plugins/' . $plugin . '/'
768 . $this->calculate_template_file_directory($template_set_id)
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,
781 $this->template_file_cache
782 = $this->cache_template_file_hierarchy(FALSE, $file_list);
787 // not found yet, try parent template set
788 // (use fallback when we run out of parents)
790 $fallback_id = $this->get_fallback_template_set();
791 $parent_id = $this->get_template_config($template_set_id,
792 'parent_template_set',
795 // were we already all the way to the last level? just exit
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) {
801 if ($fallback_id == $template_set_id) {
805 return $this->find_and_cache_plugin_template_file($plugin, $file, $parent_id);
810 * Find the right template file.
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.
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.
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
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.
831 * Plugin authors must note that the $filename MUST be prefaced
832 * with "plugins/<plugin name>/" in order to correctly resolve the
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
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
857 * NOTE that empty directories
858 * are NOT included in the cache!
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
868 function get_template_file_path($filename,
869 $directories_ok=FALSE,
870 $directories_only=FALSE) {
872 if ($directories_only) $directories_ok = TRUE;
875 // only looking for directory listing first...
877 // return list of all files in a directory (and that
880 if ($filename{strlen($filename) - 1} == '/') {
882 $return_array = array();
883 foreach ($this->template_file_cache
as $file => $file_info) {
885 // only want files in the requested directory
886 // (AND not in a subdirectory!)
888 if (!$directories_only && strpos($file, $filename) === 0
889 && strpos($file, '/', strlen($filename)) === FALSE)
890 $return_array[] = SM_PATH
. $file_info['PATH'];
892 // directories too? detect by finding any
893 // array key that matches a file in a sub-directory
894 // of the directory being processed
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'],
902 strrpos($file_info['PATH'], '/'));
903 if (!in_array($directory_name, $return_array))
904 $return_array[] = $directory_name;
908 return $return_array;
913 // just looking for singular file or directory below...
915 // figure out what to do with files not found
917 if ($directories_only ||
empty($this->template_file_cache
[$filename]['PATH'])) {
919 // if looking for directories...
920 // have to iterate through cache and detect
921 // directory by matching any file inside of it
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'],
930 strrpos($file_info['PATH'], '/'));
934 if ($directories_only) return '';
937 // plugins get one more chance
939 if (strpos($filename, 'plugins/') === 0) {
941 $plugin_name = substr($filename, 8, strpos($filename, '/', 8) - 8);
942 $file = substr($filename, strlen($plugin_name) +
9);
944 if (!$this->find_and_cache_plugin_template_file($plugin_name, $file))
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.)
952 // nothing... return empty string (yes, the else is intentional!)
958 return SM_PATH
. $this->template_file_cache
[$filename]['PATH'];
963 * Get template engine needed to render given template file.
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.
970 * @param string $filename The name of the template file,
972 * @return object The needed template object to render the template.
975 function get_rendering_template_engine_object($filename) {
977 // for files that we cannot find engine info for,
980 if (empty($this->template_file_cache
[$filename]['ENGINE']))
984 // otherwise, compare $this' engine to the file's engine
986 $engine = $this->template_file_cache
[$filename]['ENGINE'];
987 if ($this->template_engine
== $engine)
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)
999 if (!empty($this->other_template_engine_objects
[$engine])) {
1000 $rendering_engine = $this->other_template_engine_objects
[$engine];
1003 // otherwise, instantiate new engine object, add to cache
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];
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)
1019 $rendering_engine->clear_all_assign();
1020 $rendering_engine->assign($this->get_template_vars());
1023 // finally ready to go
1025 return $rendering_engine;
1030 * Return all JavaScript files provided by the template.
1032 * All files found in the template set's "js" directory (and
1033 * that of its ancestors) with the extension ".js" are returned.
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)
1041 * @return array The required file names/paths.
1044 function get_javascript_includes($full_path=FALSE) {
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
1051 //$directory = SM_PATH . $this->get_template_file_directory() . 'js';
1052 //$js_files = list_files($directory, '.js', !$full_path);
1054 $js_files = $this->get_template_file_path('js/');
1057 // parse out .js files only
1059 $return_array = array();
1060 foreach ($js_files as $file) {
1062 if (substr($file, strlen($file) - 3) != '.js') continue;
1065 $return_array[] = $file;
1067 $return_array[] = basename($file);
1072 return $return_array;
1077 * Return all alternate stylesheets provided by template.
1079 * All (non-empty) directories found in the template set's
1080 * "css/alternates" directory (and that of its ancestors)
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.
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)
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.
1100 function get_alternative_stylesheets($full_path=FALSE) {
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
1107 $css_directories = $this->get_template_file_path('css/alternates/', TRUE, TRUE);
1112 $return_array = array();
1113 foreach ($css_directories as $directory) {
1115 // CVS directories are not wanted
1117 if (strpos($directory, '/CVS') === strlen($directory) - 4) continue;
1119 $pretty_name = ucwords(str_replace('_', ' ', basename($directory)));
1122 $return_array[$directory] = $pretty_name;
1124 $return_array[basename($directory)] = $pretty_name;
1129 return $return_array;
1134 * Return all standard stylsheets provided by the template.
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.
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)
1146 * @return array The required file names/paths.
1149 function get_stylesheets($full_path=FALSE) {
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
1156 //$directory = SM_PATH . $this->get_template_file_directory() . 'css';
1157 //$css_files = list_files($directory, '.css', !$full_path);
1159 $css_files = $this->get_template_file_path('css/');
1162 // need to leave out "rtl.css"
1164 $return_array = array();
1165 foreach ($css_files as $file) {
1167 if (substr($file, strlen($file) - 4) != '.css') continue;
1168 if (strtolower(basename($file)) == 'rtl.css') continue;
1171 $return_array[] = $file;
1173 $return_array[] = basename($file);
1179 // return sheets for the current template set
1180 // last so we can enable any custom overrides
1181 // of styles in ancestor sheets
1183 return array_reverse($return_array);
1188 * Generate links to all this template set's standard stylesheets
1190 * Subclasses can override this function if stylesheets are
1191 * created differently for the template set's target output
1194 * @return string The stylesheet links as they should be sent
1198 function fetch_standard_stylesheet_links()
1201 $sheets = $this->get_stylesheets(TRUE);
1202 return $this->fetch_external_stylesheet_links($sheets);
1207 * Push out any other stylesheet links as provided (for
1208 * stylesheets not included with the current template set)
1210 * Subclasses can override this function if stylesheets are
1211 * created differently for the template set's target output
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
1223 * @return string The stylesheet links as they should be sent
1227 function fetch_external_stylesheet_links($sheets)
1230 if (!is_array($sheets)) $sheets = array($sheets);
1233 foreach ($sheets as $sheet) {
1234 $output .= create_css_link($sheet);
1242 * Send HTTP header(s) to browser.
1244 * Subclasses can override this function if headers are
1245 * managed differently in the template set's target output
1248 * @param mixed $headers A list of (or a single) header
1252 function header($headers)
1255 if (!is_array($headers)) $headers = array($headers);
1257 foreach ($headers as $header) {
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.
1270 * Subclasses can override this function if stylesheets are
1271 * created differently for the template set's target output
1274 * @return string The stylesheet link as it should be sent
1278 function fetch_right_to_left_stylesheet_link()
1281 // get right template file
1283 $sheet = $this->get_template_file_path('css/rtl.css');
1285 // fall back to SquirrelMail's own default stylesheet
1287 if (empty($sheet)) {
1288 $sheet = SM_PATH
. 'css/rtl.css';
1291 return create_css_link($sheet);
1296 * Display the template
1298 * @param string $file The template file to use
1301 function display($file)
1304 echo $this->fetch($file);
1309 * Applies the template and returns the resultant content string.
1311 * @param string $file The template file to use
1313 * @return string The template contents after applying the given template
1316 function fetch($file) {
1318 // get right template file
1320 $template = $this->get_template_file_path($file);
1323 // special case stylesheet.tpl falls back to SquirrelMail's
1324 // own default stylesheet
1326 if (empty($template) && $file == 'css/stylesheet.tpl') {
1327 $template = SM_PATH
. 'css/default.css';
1331 if (empty($template)) {
1333 trigger_error('The template "' . htmlspecialchars($file)
1334 . '" could not be fetched!', E_USER_ERROR
);
1338 $aPluginOutput = array();
1339 $aPluginOutput = concat_hook_function('template_construct_' . $file,
1340 array($aPluginOutput, $this));
1341 $this->assign('plugin_output', $aPluginOutput);
1343 //$output = $this->apply_template($template);
1344 $rendering_engine = $this->get_rendering_template_engine_object($file);
1345 $output = $rendering_engine->apply_template($template);
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.
1352 $output = filter_hook_function('template_output', $output);
1361 * Assigns values to template variables
1363 * Note: this is an abstract method that must be implemented by subclass.
1365 * @param array|string $tpl_var the template variable name(s)
1366 * @param mixed $value the value to assign
1369 function assign($tpl_var, $value = NULL) {
1371 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the assign() method.', E_USER_ERROR
);
1376 * Assigns values to template variables by reference
1378 * Note: this is an abstract method that must be implemented by subclass.
1380 * @param string $tpl_var the template variable name
1381 * @param mixed $value the referenced value to assign
1384 function assign_by_ref($tpl_var, &$value) {
1386 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the assign_by_ref() method.', E_USER_ERROR
);
1391 * Clears the values of all assigned varaiables.
1394 function clear_all_assign() {
1396 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the clear_all_assign() method.', E_USER_ERROR
);
1401 * Returns assigned variable value(s).
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)
1409 * @return mixed Desired single variable value or list of all
1410 * assigned variable values.
1413 function get_template_vars($varname=NULL) {
1415 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the get_template_vars() method.', E_USER_ERROR
);
1420 * Appends values to template variables
1422 * Note: this is an abstract method that must be implemented by subclass.
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
1435 function append($tpl_var, $value = NULL, $merge = FALSE) {
1437 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the append() method.', E_USER_ERROR
);
1442 * Appends values to template variables by reference
1444 * Note: this is an abstract method that must be implemented by subclass.
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
1457 function append_by_ref($tpl_var, &$value, $merge = FALSE) {
1459 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the append_by_ref() method.', E_USER_ERROR
);
1464 * Applys the template and generates final output destined
1465 * for the user's browser
1467 * Note: this is an abstract method that must be implemented by subclass.
1469 * @param string $filepath The full file path to the template to be applied
1471 * @return string The output for the given template
1474 function apply_template($filepath) {
1476 trigger_error('Template subclass (' . $this->template_engine
. 'Template.class.php) needs to implement the apply_template() method.', E_USER_ERROR
);