| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Copyright 2003, Paul James |
| 5 | * |
| 6 | * This file contains some methods from the Smarty templating engine version |
| 7 | * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>. |
| 8 | * |
| 9 | * The SquirrelMail (Foowd) template implementation. |
| 10 | * Derived from the foowd template implementation and adapted |
| 11 | * for squirrelmail |
| 12 | * @copyright © 2005-2006 The SquirrelMail Project Team |
| 13 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 14 | * @version $Id$ |
| 15 | * @package squirrelmail |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * The SquirrelMail (Foowd) template class. |
| 20 | * |
| 21 | * Basic template class for capturing values and pluging them into a template. |
| 22 | * This class uses a similar API to Smarty. |
| 23 | * |
| 24 | * @author Paul James |
| 25 | * @author Monte Ohrt <monte at ispi.net> |
| 26 | * @author Andrei Zmievski <andrei at php.net> |
| 27 | * @package squirrelmail |
| 28 | */ |
| 29 | class Template |
| 30 | { |
| 31 | /** |
| 32 | * The templates values array |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | var $values = array(); |
| 37 | |
| 38 | /** |
| 39 | * The template directory to use |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | var $template_dir = ''; |
| 44 | |
| 45 | /** |
| 46 | * Template files provided by this template set |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | var $templates_provided = array(); |
| 51 | |
| 52 | /** |
| 53 | * Javascript files required by the template |
| 54 | * |
| 55 | * @var array |
| 56 | */ |
| 57 | var $required_js_files = array(); |
| 58 | |
| 59 | /** |
| 60 | * Javascript files provided by the template. If a JS file is required, but |
| 61 | * not provided, the js file by the same name will be included from the |
| 62 | * default template directory. |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | var $provided_js_files = array(); |
| 67 | |
| 68 | /** |
| 69 | * Additional stylesheets provided by the template. This allows template |
| 70 | * authors (namely me to begin with :p) to provide additional CSS sheets |
| 71 | * to templates while using the default template set stylesheet for other |
| 72 | * definitions. |
| 73 | */ |
| 74 | var $additional_css_sheets = array(); |
| 75 | |
| 76 | /** |
| 77 | * Constructor |
| 78 | * |
| 79 | * @param string $sTplDir where the template set is located |
| 80 | */ |
| 81 | function Template($sTplDir) { |
| 82 | $this->template_dir = $sTplDir; |
| 83 | |
| 84 | // Pull in the tempalte config file |
| 85 | if (!file_exists($this->template_dir . 'template.php')) { |
| 86 | trigger_error('No template.php could be found in the requested template directory ("'.$this->template_dir.'")', E_USER_ERROR); |
| 87 | } else { |
| 88 | include ($this->template_dir . 'template.php'); |
| 89 | $this->templates_provided = is_array($templates_provided) ? $templates_provided : array(); |
| 90 | $this->required_js_files = is_array($required_js_files) ? $required_js_files : array(); |
| 91 | $this->provided_js_files = is_array($provided_js_files) ? $provided_js_files: array(); |
| 92 | $this->additional_css_sheets = is_array($additional_css_sheets) ? $additional_css_sheets : array(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Assigns values to template variables |
| 99 | * |
| 100 | * @param array|string $tpl_var the template variable name(s) |
| 101 | * @param mixed $value the value to assign |
| 102 | */ |
| 103 | function assign($tpl_var, $value = NULL) { |
| 104 | if (is_array($tpl_var)) |
| 105 | { |
| 106 | foreach ($tpl_var as $key => $val) |
| 107 | { |
| 108 | if ($key != '') |
| 109 | $this->values[$key] = $val; |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | if ($tpl_var != '') |
| 115 | $this->values[$tpl_var] = $value; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Assigns values to template variables by reference |
| 121 | * |
| 122 | * @param string $tpl_var the template variable name |
| 123 | * @param mixed $value the referenced value to assign |
| 124 | */ |
| 125 | function assign_by_ref($tpl_var, &$value) |
| 126 | { |
| 127 | if ($tpl_var != '') |
| 128 | $this->values[$tpl_var] = &$value; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Appends values to template variables |
| 133 | * |
| 134 | * @param array|string $tpl_var the template variable name(s) |
| 135 | * @param mixed $value the value to append |
| 136 | */ |
| 137 | function append($tpl_var, $value = NULL, $merge = FALSE) |
| 138 | { |
| 139 | if (is_array($tpl_var)) |
| 140 | { |
| 141 | foreach ($tpl_var as $_key => $_val) |
| 142 | { |
| 143 | if ($_key != '') |
| 144 | { |
| 145 | if(isset($this->values[$_key]) && !is_array($this->values[$_key])) |
| 146 | settype($this->values[$_key],'array'); |
| 147 | |
| 148 | if($merge && is_array($_val)) |
| 149 | { |
| 150 | foreach($_val as $_mkey => $_mval) |
| 151 | $this->values[$_key][$_mkey] = $_mval; |
| 152 | } |
| 153 | else |
| 154 | $this->values[$_key][] = $_val; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | if ($tpl_var != '' && isset($value)) |
| 161 | { |
| 162 | if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var])) |
| 163 | settype($this->values[$tpl_var],'array'); |
| 164 | |
| 165 | if($merge && is_array($value)) |
| 166 | { |
| 167 | foreach($value as $_mkey => $_mval) |
| 168 | $this->values[$tpl_var][$_mkey] = $_mval; |
| 169 | } |
| 170 | else |
| 171 | $this->values[$tpl_var][] = $value; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Appends values to template variables by reference |
| 178 | * |
| 179 | * @param string $tpl_var the template variable name |
| 180 | * @param mixed $value the referenced value to append |
| 181 | */ |
| 182 | function append_by_ref($tpl_var, &$value, $merge = FALSE) |
| 183 | { |
| 184 | if ($tpl_var != '' && isset($value)) |
| 185 | { |
| 186 | if(!@is_array($this->values[$tpl_var])) |
| 187 | settype($this->values[$tpl_var],'array'); |
| 188 | |
| 189 | if ($merge && is_array($value)) |
| 190 | { |
| 191 | foreach($value as $_key => $_val) |
| 192 | $this->values[$tpl_var][$_key] = &$value[$_key]; |
| 193 | } |
| 194 | else |
| 195 | $this->values[$tpl_var][] = &$value; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Display the template |
| 201 | * |
| 202 | * @param string $file The template file to use |
| 203 | */ |
| 204 | function display($file) |
| 205 | { |
| 206 | /** |
| 207 | * We want to make sure that certain variables are always passed to the |
| 208 | * templates b/c they are critical to certain template functions. |
| 209 | */ |
| 210 | $always_include = array('icon_theme_path', 'sTplDir'); |
| 211 | foreach ($always_include as $var) { |
| 212 | if (!isset($this->values[$var]) && isset($GLOBALS[$var])) { |
| 213 | $this->assign($var, $GLOBALS[$var]); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Pull in our config file |
| 218 | $t = &$this->values; // place values array directly in scope |
| 219 | |
| 220 | $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file; |
| 221 | if (!file_exists($template)) { |
| 222 | trigger_error('The template "'.htmlspecialchars($file).'" could not be displayed!', E_USER_ERROR); |
| 223 | } else { |
| 224 | ob_start(); |
| 225 | include($template); |
| 226 | ob_end_flush(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Return the results of applying a template. |
| 232 | * |
| 233 | * @param string $file The template file to use |
| 234 | * @return string A string of the results |
| 235 | */ |
| 236 | function fetch($file) { |
| 237 | $t = &$this->values; // place values array directly in scope |
| 238 | |
| 239 | $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file; |
| 240 | if (!file_exists($template)) { |
| 241 | trigger_error('The template "'.htmlspecialchars($file).'" could not be fetched!', E_USER_ERROR); |
| 242 | } else { |
| 243 | ob_start(); |
| 244 | include($template); |
| 245 | $contents = ob_get_contents(); |
| 246 | ob_end_clean(); |
| 247 | return $contents; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Return paths to the required javascript files. Used when generating page |
| 253 | * header. |
| 254 | * |
| 255 | * @return array $paths |
| 256 | */ |
| 257 | function getJavascriptIncludes () { |
| 258 | $paths = array(); |
| 259 | foreach ($this->required_js_files as $file) { |
| 260 | if (in_array($file, $this->provided_js_files)) |
| 261 | $paths[] = './'.$this->template_dir.'js/'.basename($file); |
| 262 | else $paths[] = SM_PATH .'templates/default/js/'.basename($file); |
| 263 | } |
| 264 | |
| 265 | return $paths; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Return any additional stylsheets provided by the template. Used when |
| 270 | * generating page headers. |
| 271 | * |
| 272 | * @return array $paths |
| 273 | */ |
| 274 | function getAdditionalStyleSheets () { |
| 275 | $paths = array(); |
| 276 | foreach ($this->additional_css_sheets as $css) { |
| 277 | $css = basename($css); |
| 278 | if (strtolower($css) == 'stylesheet.tpl') { |
| 279 | continue; |
| 280 | } |
| 281 | $paths[] = $css; |
| 282 | } |
| 283 | return $paths; |
| 284 | } |
| 285 | } |