X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=class%2Ftemplate%2FTemplate.class.php;h=c5b6198f6aee23b18a6f428938d3d4d23bcaa9f9;hb=d281e128d4c3149dc353397df2cd2c912b8ea5a7;hp=a8eb2ee96f5669e5f0fe9adc1d1d836b598f174f;hpb=f4138e6082bae9330782ecd002a7abe29a9ca7a4;p=squirrelmail.git diff --git a/class/template/Template.class.php b/class/template/Template.class.php index a8eb2ee9..c5b6198f 100644 --- a/class/template/Template.class.php +++ b/class/template/Template.class.php @@ -18,7 +18,7 @@ */ /** load template functions */ -require(SM_PATH . 'functions/template.php'); +require(SM_PATH . 'functions/template/general_util.php'); /** * The SquirrelMail Template class. @@ -37,7 +37,7 @@ require(SM_PATH . 'functions/template.php'); * append_by_ref() * apply_template() * - * @author Paul Lesniewski + * @author Paul Lesniewski * @package squirrelmail * */ @@ -157,6 +157,7 @@ class Template function construct_template($template_set_id) { $template = new Template($template_set_id); + $template->override_plugins(); return $template->get_template_engine_subclass(); } @@ -306,6 +307,72 @@ class Template } + /** + * Allow template set to override plugin configuration by either + * adding or removing plugins. + * + */ + function override_plugins() { + + global $disable_plugins, $plugins, $squirrelmail_plugin_hooks; + if ($disable_plugins) return; + + $add_plugins = Template::get_template_config($this->template_set_id, + 'add_plugins', array()); + $remove_plugins = Template::get_template_config($this->template_set_id, + 'remove_plugins', array()); + +//FIXME (?) we assume $add_plugins and $remove_plugins are arrays -- we could +// error check here, or just assume that template authors or admins +// won't screw up their config files + + + // disable all plugins? (can still add some by using $add_plugins) + // + if (in_array('*', $remove_plugins)) { + $plugins = array(); + $squirrelmail_plugin_hooks = array(); + $remove_plugins = array(); + } + + + foreach ($add_plugins as $plugin_name) { + // add plugin to global plugin array + // + $plugins[] = $plugin_name; + + + // enable plugin -- emulate code from use_plugin() function + // in SquirrelMail core, but also need to call the + // "squirrelmail_plugin_init_" function, which + // in static configuration is not called (this inconsistency + // could be a source of anomalous-seeming bugs in poorly + // coded plugins) + // + if (file_exists(SM_PATH . "plugins/$plugin_name/setup.php")) { + include_once(SM_PATH . "plugins/$plugin_name/setup.php"); + + $function = "squirrelmail_plugin_init_$plugin_name"; + if (function_exists($function)) + $function(); + } + } + + foreach ($remove_plugins as $plugin_name) { + // remove plugin from both global plugin & plugin hook arrays + // + $plugin_key = array_search($plugin_name, $plugins); + if (!is_null($plugin_key) && $plugin_key !== FALSE) { + unset($plugins[$plugin_key]); + if (is_array($squirrelmail_plugin_hooks)) + foreach (array_keys($squirrelmail_plugin_hooks) as $hookName) { + unset($squirrelmail_plugin_hooks[$hookName][$plugin_name]); + } + } + } + + } + /** * Instantiate and return correct subclass for this template * set's templating engine. @@ -840,6 +907,22 @@ class Template * slash, indicating that all files * for that directory name should * be returned. + * @param boolean $directories_ok When TRUE, directory names + * are acceptable search values, + * and when returning a list of + * directory contents, sub-directory + * names will also be included + * (optional; default FALSE). + * NOTE that empty directories + * are NOT included in the cache! + * @param boolean $directories_only When TRUE, only directory names + * are included in the returned + * results. (optional; default + * FALSE). Setting this argument + * to TRUE forces $directories_ok + * to TRUE as well. + * NOTE that empty directories + * are NOT included in the cache! * * @return mixed The full path to the template file or a list * of all files in the given directory if $filename @@ -849,8 +932,15 @@ class Template * file is not found. * */ - function get_template_file_path($filename) { + function get_template_file_path($filename, + $directories_ok=FALSE, + $directories_only=FALSE) { + if ($directories_only) $directories_ok = TRUE; + + + // only looking for directory listing first... + // // return list of all files in a directory (and that // of any ancestors) // @@ -862,30 +952,73 @@ class Template // only want files in the requested directory // (AND not in a subdirectory!) // - if (strpos($file, $filename) === 0 + if (!$directories_only && strpos($file, $filename) === 0 && strpos($file, '/', strlen($filename)) === FALSE) $return_array[] = SM_PATH . $file_info['PATH']; + // directories too? detect by finding any + // array key that matches a file in a sub-directory + // of the directory being processed + // + if ($directories_ok && strpos($file, $filename) === 0 + && ($pos = strpos($file, '/', strlen($filename))) !== FALSE + && strpos($file, '/', $pos + 1) === FALSE) { + $directory_name = SM_PATH + . substr($file_info['PATH'], + 0, + strrpos($file_info['PATH'], '/')); + if (!in_array($directory_name, $return_array)) + $return_array[] = $directory_name; + } + } return $return_array; } + + // just looking for singular file or directory below... + // // figure out what to do with files not found // - if (empty($this->template_file_cache[$filename]['PATH'])) { + if ($directories_only || empty($this->template_file_cache[$filename]['PATH'])) { + + // if looking for directories... + // have to iterate through cache and detect + // directory by matching any file inside of it + // + if ($directories_ok) { + foreach ($this->template_file_cache as $file => $file_info) { + if (strpos($file, $filename) === 0 + && ($pos = strpos($file, '/', strlen($filename))) !== FALSE + && strpos($file, '/', $pos + 1) === FALSE) { + return SM_PATH . substr($file_info['PATH'], + 0, + strrpos($file_info['PATH'], '/')); + } + } + + if ($directories_only) return ''; + } - // plugins get one more chance below; any other - // files we just give up now + // plugins get one more chance // - if (strpos($filename, 'plugins/') !== 0) - return ''; + if (strpos($filename, 'plugins/') === 0) { + + $plugin_name = substr($filename, 8, strpos($filename, '/', 8) - 8); + $file = substr($filename, strlen($plugin_name) + 9); - $plugin_name = substr($filename, 8, strpos($filename, '/', 8) - 8); - $file = substr($filename, strlen($plugin_name) + 9); + if (!$this->find_and_cache_plugin_template_file($plugin_name, $file)) + return ''; + //FIXME: technically I guess we should check for directories + // here too, but that's overkill (no need) presently + // (plugin-provided alternate stylesheet dirs?!? bah.) - if (!$this->find_and_cache_plugin_template_file($plugin_name, $file)) - return ''; + } + + // nothing... return empty string (yes, the else is intentional!) + // + else return ''; } @@ -1010,14 +1143,13 @@ class Template /** * Return all alternate stylesheets provided by template. * - * All files found in the template set's "css/alternates" - * directory (and that of its ancestors) with the extension - * ".css" are returned. + * All (non-empty) directories found in the template set's + * "css/alternates" directory (and that of its ancestors) + * are returned. * * Note that prettified names are constructed herein by - * taking the file name, changing underscores to spaces, - * removing the ".css" from the end of the file, and - * capitalizing each word in the resultant name. + * taking the directory name, changing underscores to spaces + * and capitalizing each word in the resultant name. * * @param boolean $full_path When FALSE, only the file names * are included in the return array; @@ -1039,25 +1171,24 @@ class Template // all alternate css files from ancestor // template sets, not just this set // - //$directory = SM_PATH . $this->get_template_file_directory() . 'css/alternates'; - //$css_files = list_files($directory, '.css', !$full_path); - // - $css_files = $this->get_template_file_path('css/alternates/'); + $css_directories = $this->get_template_file_path('css/alternates/', TRUE, TRUE); - // parse out .css files only + // prettify names // $return_array = array(); - foreach ($css_files as $file) { + foreach ($css_directories as $directory) { - if (substr($file, strlen($file) - 4) != '.css') continue; + // CVS directories are not wanted + // + if (strpos($directory, '/CVS') === strlen($directory) - 4) continue; - $pretty_name = ucwords(str_replace('_', ' ', substr(basename($file), 0, -4))); + $pretty_name = ucwords(str_replace('_', ' ', basename($directory))); if ($full_path) { - $return_array[$file] = $pretty_name; + $return_array[$directory] = $pretty_name; } else { - $return_array[basename($file)] = $pretty_name; + $return_array[basename($directory)] = $pretty_name; } } @@ -1273,7 +1404,7 @@ FIXME: We could make the incoming array more complex so it can $aPluginOutput = array(); $aPluginOutput = concat_hook_function('template_construct_' . $file, - array($aPluginOutput, $this)); + $temp=array(&$aPluginOutput, &$this)); $this->assign('plugin_output', $aPluginOutput); //$output = $this->apply_template($template); @@ -1285,7 +1416,7 @@ FIXME: We could make the incoming array more complex so it can // using this hook will probably be rejected by the // SquirrelMail team. // - $output = filter_hook_function('template_output', $output); + do_hook('template_output', $output); return $output;