+ /**
+ * Obtain template file hierarchy from cache.
+ *
+ * If the file hierarchy does not exist in session, it is
+ * constructed and stored in session before being returned
+ * to the caller.
+ *
+ * @param boolean $regenerate_cache When TRUE, the file hierarchy
+ * is reloaded and stored fresh
+ * (optional; default FALSE).
+ * @param array $additional_files Must be in same form as the
+ * files in the file hierarchy
+ * cache. These are then added
+ * to the cache (optional; default
+ * empty - no additional files).
+ *
+ * @return array Template file hierarchy array, whose keys
+ * are all the template file names (with path
+ * information relative to the template set's
+ * base directory, e.g., "css/style.css")
+ * found in all parent template sets including
+ * the ultimate fall-back template set.
+ * Array values are sub-arrays with the
+ * following key-value pairs:
+ *
+ * PATH -- file path, relative to SM_PATH
+ * SET_ID -- the ID of the template set that this file belongs to
+ * ENGINE -- the engine needed to render this template file
+ *
+ * @static
+ *
+ */
+ function cache_template_file_hierarchy($regenerate_cache=FALSE,
+ $additional_files=array()) {
+
+ sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy,
+ SQ_SESSION, array());
+
+
+ if ($regenerate_cache) unset($template_file_hierarchy);
+
+ if (!empty($template_file_hierarchy)) {
+
+ // have to add additional files if given before returning
+ //
+ if (!empty($additional_files)) {
+ $template_file_hierarchy = array_merge($template_file_hierarchy,
+ $additional_files);
+ sqsession_register($template_file_hierarchy,
+ 'template_file_hierarchy');
+ }
+
+ return $template_file_hierarchy;
+ }
+
+
+ // nothing in cache apparently, so go build it now
+ //
+ // FIXME: not sure if there is any possibility that
+ // this could be called when $sTemplateID has
+ // yet to be defined... throw error for now,
+ // but if the error occurs, it's a coding error
+ // rather than a configuration error
+ //
+ global $sTemplateID;
+ if (empty($sTemplateID)) {
+
+ trigger_error('Template set ID unknown', E_USER_ERROR);
+
+ } else {
+
+ $template_file_hierarchy = Template::catalog_template_files($sTemplateID);
+
+ // additional files, if any
+ //
+ if (!empty($additional_files)) {
+ $template_file_hierarchy = array_merge($template_file_hierarchy,
+ $additional_files);
+ }
+
+ sqsession_register($template_file_hierarchy,
+ 'template_file_hierarchy');
+
+ return $template_file_hierarchy;
+
+ }
+
+ }
+
+ /**
+ * Traverse template hierarchy and catalogue all template
+ * files (for storing in cache).
+ *
+ * Paths to all files in all parent, grand-parent, great grand
+ * parent, etc. template sets (including the fallback template)
+ * are catalogued; for identically named files, the file earlier
+ * in the hierarchy (closest to this template set) is used.
+ *
+ * @param string $template_set_id The template set in which to
+ * search for files
+ * @param array $file_list The file list so far to be added
+ * to (allows recursive behavior)
+ * (optional; default empty array).
+ * @param string $directory The directory in which to search for
+ * files (must be given as full path).
+ * If empty, starts at top-level template
+ * set directory (optional; default empty).
+ * NOTE! Use with care, as behavior is
+ * unpredictable if directory given is not
+ * part of correct template set.
+ *
+ * @return mixed The top-level caller will have an array of template
+ * files returned to it; recursive calls to this function
+ * do not receive any return value at all. The format
+ * of the template file array is as described for the
+ * Template class attribute $template_file_cache
+ *
+ * @static
+ *
+ */
+ function catalog_template_files($template_set_id, $file_list=array(), $directory='') {
+
+ $template_base_dir = SM_PATH
+ . Template::calculate_template_file_directory($template_set_id);
+
+ if (empty($directory)) {
+ $directory = $template_base_dir;
+ }
+
+ $files_and_dirs = list_files($directory, '', FALSE, TRUE, FALSE, TRUE);
+
+ // recurse for all the subdirectories in the template set
+ //
+ foreach ($files_and_dirs['DIRECTORIES'] as $dir) {
+ $file_list = Template::catalog_template_files($template_set_id, $file_list, $dir);
+ }
+
+ // place all found files in the cache
+ // FIXME: assuming PHP template engine may not necessarily be a good thing
+ //
+ $engine = Template::get_template_config($template_set_id,
+ 'template_engine', SQ_PHP_TEMPLATE);
+ foreach ($files_and_dirs['FILES'] as $file) {
+
+ // remove the part of the file path corresponding to the
+ // template set's base directory
+ //
+ $relative_file = substr($file, strlen($template_base_dir));
+
+ /**
+ * only put file in cache if not already found in earlier template
+ * PATH should be relative to SquirrelMail top directory
+ */
+ if (!isset($file_list[$relative_file])) {
+ $file_list[$relative_file] = array(
+ 'PATH' => substr($file,strlen(SM_PATH)),
+ 'SET_ID' => $template_set_id,
+ 'ENGINE' => $engine,
+ );
+ }
+
+ }
+
+
+ // now if we are currently at the top-level of the template
+ // set base directory, we need to move on to the parent
+ // template set, if any
+ //
+ if ($directory == $template_base_dir) {
+
+ // use fallback when we run out of parents
+ //
+ $fallback_id = Template::get_fallback_template_set();
+ $parent_id = Template::get_template_config($template_set_id,
+ 'parent_template_set',
+ $fallback_id);
+
+ // were we already all the way to the last level? just exit
+ //
+ // note that this code allows the fallback set to have
+ // a parent, too, but can result in endless loops
+ // if ($parent_id == $template_set_id) {
+ //
+ if ($fallback_id == $template_set_id) {
+ return $file_list;
+ }
+
+ $file_list = Template::catalog_template_files($parent_id, $file_list);
+
+ }
+
+ return $file_list;
+
+ }
+
+ /**
+ * Look for a template file in a plugin; add to template
+ * file cache if found.
+ *
+ * The file is searched for in the following order:
+ *
+ * - A directory for the current template set within the plugin:
+ * SM_PATH/plugins/<plugin name>/templates/<template name>/
+ * - In a directory for one of the current template set's ancestor
+ * (inherited) template sets within the plugin:
+ * SM_PATH/plugins/<plugin name>/templates/<parent template name>/
+ * - In a directory for the fallback template set within the plugin:
+ * SM_PATH/plugins/<plugin name>/templates/<fallback template name>/
+ *
+ * @param string $plugin The name of the plugin
+ * @param string $file The name of the template file
+ * @param string $template_set_id The ID of the template for which
+ * to start looking for the file
+ * (optional; default is current
+ * template set ID).
+ *
+ * @return boolean TRUE if the template file was found, FALSE otherwise.
+ *
+ */
+ function find_and_cache_plugin_template_file($plugin, $file, $template_set_id='') {
+
+ if (empty($template_set_id))
+ $template_set_id = $this->template_set_id;
+
+ $file_path = SM_PATH . 'plugins/' . $plugin . '/'
+ . $this->calculate_template_file_directory($template_set_id)
+ . $file;
+
+ if (file_exists($file_path)) {
+ // FIXME: assuming PHP template engine may not necessarily be a good thing
+ $engine = $this->get_template_config($template_set_id,
+ 'template_engine', SQ_PHP_TEMPLATE);
+ $file_list = array('plugins/' . $plugin . '/' . $file => array(
+ 'PATH' => substr($file_path, strlen(SM_PATH)),
+ 'SET_ID' => $template_set_id,
+ 'ENGINE' => $engine,
+ )
+ );
+ $this->template_file_cache
+ = $this->cache_template_file_hierarchy(FALSE, $file_list);
+ return TRUE;
+ }
+
+
+ // not found yet, try parent template set
+ // (use fallback when we run out of parents)
+ //
+ $fallback_id = $this->get_fallback_template_set();
+ $parent_id = $this->get_template_config($template_set_id,
+ 'parent_template_set',
+ $fallback_id);
+
+ // were we already all the way to the last level? just exit
+ //
+ // note that this code allows the fallback set to have
+ // a parent, too, but can result in endless loops
+ // if ($parent_id == $template_set_id) {
+ //
+ if ($fallback_id == $template_set_id) {
+ return FALSE;
+ }
+
+ return $this->find_and_cache_plugin_template_file($plugin, $file, $parent_id);
+
+ }