Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
304 | alex-w | 1 | <?php |
2 | |||
3 | /** |
||
4 | * Smarty Internal Plugin TemplateBase |
||
5 | * |
||
6 | * This file contains the basic classes and methodes for template and variable creation |
||
7 | * |
||
8 | * @package Smarty |
||
9 | * @subpackage Templates |
||
10 | * @author Uwe Tews |
||
11 | */ |
||
12 | |||
13 | /** |
||
14 | * Base class with template and variable methodes |
||
15 | */ |
||
16 | class Smarty_Internal_TemplateBase { |
||
17 | // class used for templates |
||
18 | public $template_class = 'Smarty_Internal_Template'; |
||
19 | |||
20 | /** |
||
21 | * assigns a Smarty variable |
||
22 | * |
||
23 | * @param array $ |string $tpl_var the template variable name(s) |
||
24 | * @param mixed $value the value to assign |
||
25 | * @param boolean $nocache if true any output of this variable will be not cached |
||
26 | * @param boolean $scope the scope the variable will have (local,parent or root) |
||
27 | */ |
||
28 | public function assign($tpl_var, $value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) |
||
29 | { |
||
30 | if (is_array($tpl_var)) { |
||
31 | foreach ($tpl_var as $_key => $_val) { |
||
32 | if ($_key != '') { |
||
33 | $this->check_tplvar($_key); |
||
34 | $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache, $scope); |
||
35 | } |
||
36 | } |
||
37 | } else { |
||
38 | if ($tpl_var != '') { |
||
39 | $this->check_tplvar($tpl_var); |
||
40 | $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache, $scope); |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | /** |
||
45 | * assigns a global Smarty variable |
||
46 | * |
||
47 | * @param string $varname the global variable name |
||
48 | * @param mixed $value the value to assign |
||
49 | * @param boolean $nocache if true any output of this variable will be not cached |
||
50 | */ |
||
51 | public function assign_global($varname, $value = null, $nocache = false) |
||
52 | { |
||
53 | if ($varname != '') { |
||
54 | $this->check_tplvar($varname); |
||
55 | $this->smarty->global_tpl_vars[$varname] = new Smarty_variable($value, $nocache); |
||
56 | } |
||
57 | } |
||
58 | /** |
||
59 | * assigns values to template variables by reference |
||
60 | * wrapper to assign |
||
61 | */ |
||
62 | public function assign_by_ref($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) |
||
63 | { |
||
64 | $this->assign($tpl_var, $value, $nocache, $scope); |
||
65 | } |
||
66 | /** |
||
67 | * appends values to template variables |
||
68 | * |
||
69 | * @param array $ |string $tpl_var the template variable name(s) |
||
70 | * @param mixed $value the value to append |
||
71 | * @param boolean $merge flag if array elements shall be merged |
||
72 | * @param boolean $nocache if true any output of this variable will be not cached |
||
73 | * @param boolean $scope the scope the variable will have (local,parent or root) |
||
74 | */ |
||
75 | public function append($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) |
||
76 | { |
||
77 | if (is_array($tpl_var)) { |
||
78 | // $tpl_var is an array, ignore $value |
||
79 | foreach ($tpl_var as $_key => $_val) { |
||
80 | if ($_key != '') { |
||
81 | if (!isset($this->tpl_vars[$_key])) { |
||
82 | $this->check_tplvar($_key); |
||
83 | $tpl_var_inst = $this->getVariable($_key, null, true, false); |
||
84 | if ($tpl_var_inst instanceof Undefined_Smarty_Variable) { |
||
85 | $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $scope); |
||
86 | } else { |
||
87 | $this->tpl_vars[$_key] = clone $tpl_var_inst; |
||
88 | if ($scope != SMARTY_LOCAL_SCOPE) { |
||
89 | $this->tpl_vars[$_key]->scope = $scope; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) { |
||
94 | settype($this->tpl_vars[$_key]->value, 'array'); |
||
95 | } |
||
96 | if ($merge && is_array($_val)) { |
||
97 | foreach($_val as $_mkey => $_mval) { |
||
98 | $this->tpl_vars[$_key]->value[$_mkey] = $_mval; |
||
99 | } |
||
100 | } else { |
||
101 | $this->tpl_vars[$_key]->value[] = $_val; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } else { |
||
106 | if ($tpl_var != '' && isset($value)) { |
||
107 | if (!isset($this->tpl_vars[$tpl_var])) { |
||
108 | $this->check_tplvar($tpl_var); |
||
109 | $tpl_var_inst = $this->getVariable($tpl_var, null, true, false); |
||
110 | if ($tpl_var_inst instanceof Undefined_Smarty_Variable) { |
||
111 | $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope); |
||
112 | } else { |
||
113 | $this->tpl_vars[$tpl_var] = clone $tpl_var_inst; |
||
114 | if ($scope != SMARTY_LOCAL_SCOPE) { |
||
115 | $this->tpl_vars[$tpl_var]->scope = $scope; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) { |
||
120 | settype($this->tpl_vars[$tpl_var]->value, 'array'); |
||
121 | } |
||
122 | if ($merge && is_array($value)) { |
||
123 | foreach($value as $_mkey => $_mval) { |
||
124 | $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval; |
||
125 | } |
||
126 | } else { |
||
127 | $this->tpl_vars[$tpl_var]->value[] = $value; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * appends values to template variables by reference |
||
135 | * |
||
136 | * wrapper to append |
||
137 | */ |
||
138 | public function append_by_ref($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) |
||
139 | { |
||
140 | $this->append($tpl_var, $value, $merge, $nocache, $scope); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * check if template variable name is reserved. |
||
145 | * |
||
146 | * @param string $tpl_var the template variable |
||
147 | */ |
||
148 | private function check_tplvar($tpl_var) |
||
149 | { |
||
150 | if (in_array($tpl_var, array('this', 'smarty'))) { |
||
151 | throw new Exception("Cannot assign value to reserved var '{$tpl_var}'"); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * clear the given assigned template variable. |
||
157 | * |
||
158 | * @param string $ |array $tpl_var the template variable(s) to clear |
||
159 | */ |
||
160 | public function clear_assign($tpl_var) |
||
161 | { |
||
162 | if (is_array($tpl_var)) { |
||
163 | foreach ($tpl_var as $curr_var) { |
||
164 | unset($this->tpl_vars[$curr_var]); |
||
165 | } |
||
166 | } else { |
||
167 | unset($this->tpl_vars[$tpl_var]); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * clear all the assigned template variables. |
||
173 | */ |
||
174 | public function clear_all_assign() |
||
175 | { |
||
176 | $this->tpl_vars = array(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * gets the object of a Smarty variable |
||
181 | * |
||
182 | * @param string $variable the name of the Smarty variable |
||
183 | * @param object $_ptr optional pointer to data object |
||
184 | * @param boolean $search_parents search also in parent data |
||
185 | * @return object the object of the variable |
||
186 | */ |
||
187 | public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true) |
||
188 | { |
||
189 | if ($_ptr === null) { |
||
190 | $_ptr = $this; |
||
191 | } while ($_ptr !== null) { |
||
192 | if (isset($_ptr->tpl_vars[$variable])) { |
||
193 | // found it, return it |
||
194 | return $_ptr->tpl_vars[$variable]; |
||
195 | } |
||
196 | // not found, try at parent |
||
197 | if ($search_parents) { |
||
198 | $_ptr = $_ptr->parent; |
||
199 | } else { |
||
200 | $_ptr = null; |
||
201 | } |
||
202 | } |
||
203 | if (isset($this->smarty->global_tpl_vars[$variable])) { |
||
204 | // found it, return it |
||
205 | return $this->smarty->global_tpl_vars[$variable]; |
||
206 | } |
||
207 | if (Smarty::$error_unassigned && $error_enable) { |
||
208 | throw new Exception('Undefined Smarty variable "' . $variable . '"'); |
||
209 | } else { |
||
210 | return new Undefined_Smarty_Variable; |
||
211 | } |
||
212 | } |
||
213 | /** |
||
214 | * gets a config variable |
||
215 | * |
||
216 | * @param string $variable the name of the config variable |
||
217 | * @return mixed the value of the config variable |
||
218 | */ |
||
219 | public function getConfigVariable($variable) |
||
220 | { |
||
221 | $_ptr = $this; |
||
222 | while ($_ptr !== null) { |
||
223 | if (isset($_ptr->config_vars[$variable])) { |
||
224 | // found it, return it |
||
225 | return $_ptr->config_vars[$variable]; |
||
226 | } |
||
227 | // not found, try at parent |
||
228 | $_ptr = $_ptr->parent; |
||
229 | } |
||
230 | if (Smarty::$error_unassigned) { |
||
231 | throw new Exception('Undefined config variable "' . $variable . '"'); |
||
232 | } else { |
||
233 | return ''; |
||
234 | } |
||
235 | } |
||
236 | /** |
||
237 | * gets a stream variable |
||
238 | * |
||
239 | * @param string $variable the stream of the variable |
||
240 | * @return mixed the value of the stream variable |
||
241 | */ |
||
242 | public function getStreamVariable($variable) |
||
243 | { |
||
244 | $_result = ''; |
||
245 | if ($fp = fopen($variable, 'r+')) { |
||
246 | while (!feof($fp)) { |
||
247 | $_result .= fgets($fp); |
||
248 | } |
||
249 | fclose($fp); |
||
250 | return $_result; |
||
251 | } |
||
252 | |||
253 | if (Smarty::$error_unassigned) { |
||
254 | throw new Exception('Undefined stream variable "' . $variable . '"'); |
||
255 | } else { |
||
256 | return ''; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * creates a template object |
||
262 | * |
||
263 | * @param string $template the resource handle of the template file |
||
264 | * @param object $parent next higher level of Smarty variables |
||
265 | * @param mixed $cache_id cache id to be used with this template |
||
266 | * @param mixed $compile_id compile id to be used with this template |
||
267 | * @returns object template object |
||
268 | */ |
||
269 | public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) |
||
270 | { |
||
271 | if (is_object($cache_id) || is_array($cache_id)) { |
||
272 | $parent = $cache_id; |
||
273 | $cache_id = null; |
||
274 | } |
||
275 | if (is_array($parent)) { |
||
276 | $data = $parent; |
||
277 | $parent = null; |
||
278 | } else { |
||
279 | $data = null; |
||
280 | } |
||
281 | if (!is_object($template)) { |
||
282 | // we got a template resource |
||
283 | $_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id); |
||
284 | // already in template cache? |
||
285 | if (isset($this->smarty->template_objects[$_templateId])) { |
||
286 | // return cached template object |
||
287 | $tpl = $this->smarty->template_objects[$_templateId]; |
||
288 | } else { |
||
289 | // create and cache new template object |
||
290 | $tpl = new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id); |
||
291 | } |
||
292 | } else { |
||
293 | // just return a copy of template class |
||
294 | $tpl = $template; |
||
295 | } |
||
296 | // fill data if present |
||
297 | if (is_array($data)) { |
||
298 | // set up variable values |
||
299 | foreach ($data as $_key => $_val) { |
||
300 | $tpl->tpl_vars[$_key] = new Smarty_variable($_val); |
||
301 | } |
||
302 | } |
||
303 | return $tpl; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * generates a template id |
||
308 | * |
||
309 | * @param string $_resource the resource handle of the template file |
||
310 | * @param mixed $_cache_id cache id to be used with this template |
||
311 | * @param mixed $_compile_id compile id to be used with this template |
||
312 | * @returns string a unique template id |
||
313 | */ |
||
314 | function buildTemplateId ($_resource, $_cache_id, $_compile_id) |
||
315 | { |
||
316 | // return md5($_resource . md5($_cache_id) . md5($_compile_id)); |
||
317 | return crc32($_resource . $_cache_id . $_compile_id); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * return current time |
||
322 | * |
||
323 | * @returns double current time |
||
324 | */ |
||
325 | function _get_time() |
||
326 | { |
||
327 | $_mtime = microtime(); |
||
328 | $_mtime = explode(" ", $_mtime); |
||
329 | return (double)($_mtime[1]) + (double)($_mtime[0]); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * class for the Smarty data object |
||
335 | * |
||
336 | * The Smarty data object will hold Smarty variables in the current scope |
||
337 | * |
||
338 | * @param object $parent tpl_vars next higher level of Smarty variables |
||
339 | */ |
||
340 | class Smarty_Data extends Smarty_Internal_TemplateBase { |
||
341 | // array of variable objects |
||
342 | public $tpl_vars = array(); |
||
343 | // back pointer to parent object |
||
344 | public $parent = null; |
||
345 | // config vars |
||
346 | public $config_vars = array(); |
||
347 | /** |
||
348 | * create Smarty data object |
||
349 | */ |
||
350 | public function __construct ($_parent = null) |
||
351 | { |
||
352 | if (is_object($_parent)) { |
||
353 | // when object set up back pointer |
||
354 | $this->parent = $_parent; |
||
355 | } elseif (is_array($_parent)) { |
||
356 | // set up variable values |
||
357 | foreach ($_parent as $_key => $_val) { |
||
358 | $this->tpl_vars[$_key] = new Smarty_variable($_val); |
||
359 | } |
||
360 | } else { |
||
361 | throw new Exception("Wrong type for template variables"); |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | /** |
||
366 | * class for the Smarty variable object |
||
367 | * |
||
368 | * This class defines the Smarty variable object |
||
369 | */ |
||
370 | class Smarty_Variable { |
||
371 | // template variable |
||
372 | public $value; |
||
373 | public $nocache; |
||
374 | public $scope; |
||
375 | /** |
||
376 | * create Smarty variable object |
||
377 | * |
||
378 | * @param mixed $value the value to assign |
||
379 | * @param boolean $nocache if true any output of this variable will be not cached |
||
380 | * @param boolean $scope the scope the variable will have (local,parent or root) |
||
381 | */ |
||
382 | public function __construct ($value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) |
||
383 | { |
||
384 | $this->value = $value; |
||
385 | $this->nocache = $nocache; |
||
386 | $this->scope = $scope; |
||
387 | } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * class for undefined variable object |
||
392 | * |
||
393 | * This class defines an object for undefined variable handling |
||
394 | */ |
||
395 | class Undefined_Smarty_Variable { |
||
396 | // return always false |
||
397 | public function __get ($name) |
||
398 | { |
||
399 | if ($name == 'nocache') { |
||
400 | return false; |
||
401 | } else { |
||
402 | return null; |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | |||
407 | ?> |