| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Smarty_Template.class.php |
| 5 | * |
| 6 | * This file contains a Template subclass intended as a bridge between |
| 7 | * SquirrelMail and Smarty. All abstract methods from the Template class |
| 8 | * are implemented here. |
| 9 | * |
| 10 | * @copyright © 2003-2006 The SquirrelMail Project Team |
| 11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 12 | * @version $Id$ |
| 13 | * @package squirrelmail |
| 14 | * @subpackage Template |
| 15 | * @since 1.5.2 |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * The SquirrelMail Smarty Template class. Extends the base |
| 21 | * Template class for use with Smarty template pages. |
| 22 | * |
| 23 | * @author Paul Lesniewski <paul at squirrelmail.org> |
| 24 | * @package squirrelmail |
| 25 | * |
| 26 | */ |
| 27 | class Smarty_Template extends Template |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * The Smarty template object |
| 32 | * |
| 33 | * @var object |
| 34 | * |
| 35 | */ |
| 36 | var $smarty_template = null; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * Please do not call directly. Use Template::construct_template(). |
| 43 | * |
| 44 | * @param string $template_id the template ID |
| 45 | * |
| 46 | */ |
| 47 | function Smarty_Template($template_id) { |
| 48 | //FIXME: find a way to test that this is ONLY ever called |
| 49 | // from parent's construct_template() method (I doubt it |
| 50 | // is worth the trouble to parse the current stack trace) |
| 51 | // if (???) |
| 52 | // trigger_error('Please do not use default Smarty_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR); |
| 53 | |
| 54 | parent::Template($template_id); |
| 55 | |
| 56 | |
| 57 | // load smarty settings |
| 58 | // |
| 59 | // instantiate and set up Smarty object |
| 60 | // |
| 61 | $smarty_path |
| 62 | = Template::get_template_config($this->template_set_id, 'smarty_path'); |
| 63 | require($smarty_path); |
| 64 | $this->smarty_template = new Smarty(); |
| 65 | $this->smarty_template->compile_dir |
| 66 | = Template::get_template_config($this->template_set_id, 'smarty_compile_dir'); |
| 67 | $this->smarty_template->cache_dir |
| 68 | = Template::get_template_config($this->template_set_id, 'smarty_cache_dir'); |
| 69 | $this->smarty_template->config_dir |
| 70 | = Template::get_template_config($this->template_set_id, 'smarty_config_dir'); |
| 71 | |
| 72 | // note that we do not use Smarty's template_dir |
| 73 | // because SquirrelMail has its own method of |
| 74 | // determining template file paths |
| 75 | // |
| 76 | //$this->smarty_template->template_dir = |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Assigns values to template variables |
| 82 | * |
| 83 | * @param array|string $tpl_var the template variable name(s) |
| 84 | * @param mixed $value the value to assign |
| 85 | FIXME: Proposed idea to add a parameter here that turns variable |
| 86 | encoding on, so that we can make sure output is always |
| 87 | run through something like htmlspecialchars() (maybe even nl2br()?) |
| 88 | * |
| 89 | */ |
| 90 | function assign($tpl_var, $value = NULL) { |
| 91 | |
| 92 | $this->smarty_template->assign($tpl_var, $value); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Assigns values to template variables by reference |
| 98 | * |
| 99 | * @param string $tpl_var the template variable name |
| 100 | * @param mixed $value the referenced value to assign |
| 101 | FIXME: Proposed idea to add a parameter here that turns variable |
| 102 | encoding on, so that we can make sure output is always |
| 103 | run through something like htmlspecialchars() (maybe even nl2br()?) |
| 104 | * |
| 105 | */ |
| 106 | function assign_by_ref($tpl_var, &$value) { |
| 107 | |
| 108 | $this->smarty_template->assign_by_ref($tpl_var, $value); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Clears the values of all assigned varaiables. |
| 114 | * |
| 115 | */ |
| 116 | function clear_all_assign() { |
| 117 | |
| 118 | $this->smarty_template->clear_all_assign(); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns assigned variable value(s). |
| 124 | * |
| 125 | * @param string $varname If given, the value of that variable |
| 126 | * is returned, assuming it has been |
| 127 | * previously assigned. If not specified |
| 128 | * an array of all assigned variables is |
| 129 | * returned. (optional) |
| 130 | * |
| 131 | * @return mixed Desired single variable value or list of all |
| 132 | * assigned variable values. |
| 133 | * |
| 134 | */ |
| 135 | function get_template_vars($varname=NULL) { |
| 136 | |
| 137 | return $this->smarty_template->get_template_vars($varname); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Appends values to template variables |
| 143 | * |
| 144 | * @param array|string $tpl_var the template variable name(s) |
| 145 | * @param mixed $value the value to append |
| 146 | * @param boolean $merge when $value is given as an array, |
| 147 | * this indicates whether or not that |
| 148 | * array itself should be appended as |
| 149 | * a new template variable value or if |
| 150 | * that array's values should be merged |
| 151 | * into the existing array of template |
| 152 | * variable values |
| 153 | FIXME: Proposed idea to add a parameter here that turns variable |
| 154 | encoding on, so that we can make sure output is always |
| 155 | run through something like htmlspecialchars() (maybe even nl2br()?) |
| 156 | * |
| 157 | */ |
| 158 | function append($tpl_var, $value = NULL, $merge = FALSE) { |
| 159 | |
| 160 | $this->smarty_template->append($tpl_var, $value, $merge); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Appends values to template variables by reference |
| 166 | * |
| 167 | * @param string $tpl_var the template variable name |
| 168 | * @param mixed $value the referenced value to append |
| 169 | * @param boolean $merge when $value is given as an array, |
| 170 | * this indicates whether or not that |
| 171 | * array itself should be appended as |
| 172 | * a new template variable value or if |
| 173 | * that array's values should be merged |
| 174 | * into the existing array of template |
| 175 | * variable values |
| 176 | FIXME: Proposed idea to add a parameter here that turns variable |
| 177 | encoding on, so that we can make sure output is always |
| 178 | run through something like htmlspecialchars() (maybe even nl2br()?) |
| 179 | * |
| 180 | */ |
| 181 | function append_by_ref($tpl_var, &$value, $merge = FALSE) { |
| 182 | |
| 183 | $this->smarty_template->append_by_ref($tpl_var, $value, $merge); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Applys the template and generates final output destined |
| 189 | * for the user's browser |
| 190 | * |
| 191 | * @param string $filepath The full file path to the template to be applied |
| 192 | * |
| 193 | * @return string The output for the given template |
| 194 | * |
| 195 | */ |
| 196 | function apply_template($filepath) { |
| 197 | |
| 198 | // if being passed a raw .css or .js file, default |
| 199 | // Smarty delimiters will cause errors |
| 200 | // |
| 201 | if (strrpos($filepath, '.css') === (strlen($filepath) - 4) |
| 202 | || strrpos($filepath, '.js') === (strlen($filepath) - 3)) { |
| 203 | $this->smarty_template->left_delimiter = '{='; |
| 204 | $this->smarty_template->right_delimiter = '=}'; |
| 205 | } |
| 206 | |
| 207 | // Smarty wants absolute paths |
| 208 | // |
| 209 | if (strpos($filepath, '/') === 0) |
| 210 | return $this->smarty_template->fetch('file:' . $filepath); |
| 211 | else |
| 212 | return $this->smarty_template->fetch('file:' . getcwd() . '/' . $filepath); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |