1 <?php
3 /**
4 * Smarty Internal Plugin Data
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 */
13 /**
14 * Base class with template and variable methodes
15 */
16 class Smarty_Internal_Data {
17 // class used for templates
18 public $template_class = 'Smarty_Internal_Template';
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)
29 {
30 if (is_array($tpl_var)) {
31 foreach ($tpl_var as $_key => $_val) {
32 if ($_key != '') {
33 $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache);
34 }
35 }
36 } else {
37 if ($tpl_var != '') {
38 $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache);
39 }
40 }
41 }
42 /**
43 * assigns a global Smarty variable
44 *
45 * @param string $varname the global variable name
46 * @param mixed $value the value to assign
47 * @param boolean $nocache if true any output of this variable will be not cached
48 */
49 public function assignGlobal($varname, $value = null, $nocache = false)
50 {
51 if ($varname != '') {
52 Smarty::$global_tpl_vars[$varname] = new Smarty_variable($value, $nocache);
53 }
54 }
55 /**
56 * assigns values to template variables by reference
57 *
58 * @param string $tpl_var the template variable name
59 * @param mixed $ &$value the referenced value to assign
60 * @param boolean $nocache if true any output of this variable will be not cached
61 */
62 public function assignByRef($tpl_var, &$value, $nocache = false)
63 {
64 if ($tpl_var != '') {
65 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
66 $this->tpl_vars[$tpl_var]->value = &$value;
67 }
68 }
70 /**
71 * wrapper function for Smarty 2 BC
72 *
73 * @param string $tpl_var the template variable name
74 * @param mixed $ &$value the referenced value to assign
75 */
76 public function assign_by_ref($tpl_var, &$value)
77 {
78 if($this->smarty->deprecation_notices)
79 trigger_error("function call 'assign_by_ref' is unknown or deprecated, use 'assignByRef'", E_USER_NOTICE);
80 $this->assignByRef($tpl_var, $value);
81 }
82 /**
83 * appends values to template variables
84 *
85 * @param array $ |string $tpl_var the template variable name(s)
86 * @param mixed $value the value to append
87 * @param boolean $merge flag if array elements shall be merged
88 * @param boolean $nocache if true any output of this variable will be not cached
89 */
90 public function append($tpl_var, $value = null, $merge = false, $nocache = false)
91 {
92 if (is_array($tpl_var)) {
93 // $tpl_var is an array, ignore $value
94 foreach ($tpl_var as $_key => $_val) {
95 if ($_key != '') {
96 if (!isset($this->tpl_vars[$_key])) {
97 $tpl_var_inst = $this->getVariable($_key, null, true, false);
98 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
99 $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache);
100 } else {
101 $this->tpl_vars[$_key] = clone $tpl_var_inst;
102 }
103 }
104 if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
105 settype($this->tpl_vars[$_key]->value, 'array');
106 }
107 if ($merge && is_array($_val)) {
108 foreach($_val as $_mkey => $_mval) {
109 $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
110 }
111 } else {
112 $this->tpl_vars[$_key]->value[] = $_val;
113 }
114 }
115 }
116 } else {
117 if ($tpl_var != '' && isset($value)) {
118 if (!isset($this->tpl_vars[$tpl_var])) {
119 $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
120 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
121 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
122 } else {
123 $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
124 }
125 }
126 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
127 settype($this->tpl_vars[$tpl_var]->value, 'array');
128 }
129 if ($merge && is_array($value)) {
130 foreach($value as $_mkey => $_mval) {
131 $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
132 }
133 } else {
134 $this->tpl_vars[$tpl_var]->value[] = $value;
135 }
136 }
137 }
138 }
140 /**
141 * appends values to template variables by reference
142 *
143 * @param string $tpl_var the template variable name
144 * @param mixed $ &$value the referenced value to append
145 * @param boolean $merge flag if array elements shall be merged
146 */
147 public function appendByRef($tpl_var, &$value, $merge = false)
148 {
149 if ($tpl_var != '' && isset($value)) {
150 if (!isset($this->tpl_vars[$tpl_var])) {
151 $this->tpl_vars[$tpl_var] = new Smarty_variable();
152 }
153 if (!@is_array($this->tpl_vars[$tpl_var]->value)) {
154 settype($this->tpl_vars[$tpl_var]->value, 'array');
155 }
156 if ($merge && is_array($value)) {
157 foreach($value as $_key => $_val) {
158 $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
159 }
160 } else {
161 $this->tpl_vars[$tpl_var]->value[] = &$value;
162 }
163 }
164 }
166 /**
167 *
168 * @param string $tpl_var the template variable name
169 * @param mixed $ &$value the referenced value to append
170 * @param boolean $merge flag if array elements shall be merged
171 */
172 public function append_by_ref($tpl_var, &$value, $merge = false)
173 {
174 if($this->smarty->deprecation_notices)
175 trigger_error("function call 'append_by_ref' is unknown or deprecated, use 'appendByRef'", E_USER_NOTICE);
176 $this->appendByRef($tpl_var, $value, $merge);
177 }
178 /**
179 * Returns a single or all template variables
180 *
181 * @param string $varname variable name or null
182 * @return string variable value or or array of variables
183 */
184 function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
185 {
186 if (isset($varname)) {
187 $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
188 if (is_object($_var)) {
189 return $_var->value;
190 } else {
191 return null;
192 }
193 } else {
194 $_result = array();
195 if ($_ptr === null) {
196 $_ptr = $this;
197 } while ($_ptr !== null) {
198 foreach ($_ptr->tpl_vars AS $key => $var) {
199 if (!array_key_exists($key, $_result)) {
200 $_result[$key] = $var->value;
201 }
202 }
203 // not found, try at parent
204 if ($search_parents) {
205 $_ptr = $_ptr->parent;
206 } else {
207 $_ptr = null;
208 }
209 }
210 if ($search_parents && isset(Smarty::$global_tpl_vars)) {
211 foreach (Smarty::$global_tpl_vars AS $key => $var) {
212 if (!array_key_exists($key, $_result)) {
213 $_result[$key] = $var->value;
214 }
215 }
216 }
217 return $_result;
218 }
219 }
221 /**
222 * clear the given assigned template variable.
223 *
224 * @param string $ |array $tpl_var the template variable(s) to clear
225 */
226 public function clearAssign($tpl_var)
227 {
228 if (is_array($tpl_var)) {
229 foreach ($tpl_var as $curr_var) {
230 unset($this->tpl_vars[$curr_var]);
231 }
232 } else {
233 unset($this->tpl_vars[$tpl_var]);
234 }
235 }
237 /**
238 * clear all the assigned template variables.
239 */
240 public function clearAllAssign()
241 {
242 $this->tpl_vars = array();
243 }
245 /**
246 * load a config file, optionally load just selected sections
247 *
248 * @param string $config_file filename
249 * @param mixed $sections array of section names, single section or null
250 */
251 public function configLoad($config_file, $sections = null)
252 {
253 // load Config class
254 $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
255 $config->loadConfigVars($sections);
256 }
258 /**
259 * gets the object of a Smarty variable
260 *
261 * @param string $variable the name of the Smarty variable
262 * @param object $_ptr optional pointer to data object
263 * @param boolean $search_parents search also in parent data
264 * @return object the object of the variable
265 */
266 public function getVariable($_variable, $_ptr = null, $search_parents = true, $error_enable = true)
267 {
268 if ($_ptr === null) {
269 $_ptr = $this;
270 } while ($_ptr !== null) {
271 if (isset($_ptr->tpl_vars[$_variable])) {
272 // found it, return it
273 return $_ptr->tpl_vars[$_variable];
274 }
275 // not found, try at parent
276 if ($search_parents) {
277 $_ptr = $_ptr->parent;
278 } else {
279 $_ptr = null;
280 }
281 }
282 if (isset(Smarty::$global_tpl_vars[$_variable])) {
283 // found it, return it
284 return Smarty::$global_tpl_vars[$_variable];
285 }
286 if ($this->smarty->error_unassigned && $error_enable) {
287 throw new SmartyException('Undefined Smarty variable "' . $_variable . '"');
288 } else {
289 if ($error_enable) {
290 // force a notice
291 $x = $$_variable;
292 }
293 return new Undefined_Smarty_Variable;
294 }
295 }
296 /**
297 * gets a config variable
298 *
299 * @param string $variable the name of the config variable
300 * @return mixed the value of the config variable
301 */
302 public function getConfigVariable($_variable)
303 {
304 $_ptr = $this;
305 while ($_ptr !== null) {
306 if (isset($_ptr->config_vars[$_variable])) {
307 // found it, return it
308 return $_ptr->config_vars[$_variable];
309 }
310 // not found, try at parent
311 $_ptr = $_ptr->parent;
312 }
313 if ($this->smarty->error_unassigned) {
314 throw new SmartyException('Undefined config variable "' . $_variable . '"');
315 } else {
316 // force a notice
317 $x = $$_variable;
318 return null;
319 }
320 }
322 /**
323 * gets a stream variable
324 *
325 * @param string $variable the stream of the variable
326 * @return mixed the value of the stream variable
327 */
328 public function getStreamVariable($variable)
329 {
330 $_result = '';
331 if ($fp = fopen($variable, 'r+')) {
332 while (!feof($fp) && ($current_line = fgets($fp)) !== false ) {
333 $_result .= $current_line;
334 }
335 fclose($fp);
336 return $_result;
337 }
339 if ($this->smarty->error_unassigned) {
340 throw new SmartyException('Undefined stream variable "' . $variable . '"');
341 } else {
342 return null;
343 }
344 }
346 /**
347 * Returns a single or all config variables
348 *
349 * @param string $varname variable name or null
350 * @return string variable value or or array of variables
351 */
352 function getConfigVars($varname = null, $search_parents = true)
353 {
354 // var_dump($this);
355 $_ptr = $this;
356 $var_array = array();
357 while ($_ptr !== null) {
358 if (isset($varname)) {
359 if (isset($_ptr->config_vars[$varname])) {
360 return $_ptr->config_vars[$varname];
361 }
362 } else {
363 $var_array = array_merge($_ptr->config_vars, $var_array);
364 }
365 // not found, try at parent
366 if ($search_parents) {
367 $_ptr = $_ptr->parent;
368 } else {
369 $_ptr = null;
370 }
371 }
372 if (isset($varname)) {
373 return '';
374 } else {
375 return $var_array;
376 }
377 }
379 /**
380 * Deassigns a single or all config variables
381 *
382 * @param string $varname variable name or null
383 */
384 function clearConfig($varname = null)
385 {
386 if (isset($varname)) {
387 unset($this->config_vars[$varname]);
388 return;
389 } else {
390 $this->config_vars = array();
391 return;
392 }
393 }
395 }
397 /**
398 * class for the Smarty data object
399 *
400 * The Smarty data object will hold Smarty variables in the current scope
401 *
402 * @param object $parent tpl_vars next higher level of Smarty variables
403 */
404 class Smarty_Data extends Smarty_Internal_Data {
405 // array of variable objects
406 public $tpl_vars = array();
407 // back pointer to parent object
408 public $parent = null;
409 // config vars
410 public $config_vars = array();
411 // Smarty object
412 public $smarty = null;
413 /**
414 * create Smarty data object
415 */
416 public function __construct ($_parent = null, $smarty = null)
417 {
418 $this->smarty = $smarty;
419 if (is_object($_parent)) {
420 // when object set up back pointer
421 $this->parent = $_parent;
422 } elseif (is_array($_parent)) {
423 // set up variable values
424 foreach ($_parent as $_key => $_val) {
425 $this->tpl_vars[$_key] = new Smarty_variable($_val);
426 }
427 } elseif ($_parent != null) {
428 throw new SmartyException("Wrong type for template variables");
429 }
430 }
431 }
432 /**
433 * class for the Smarty variable object
434 *
435 * This class defines the Smarty variable object
436 */
437 class Smarty_Variable {
438 // template variable
439 public $value;
440 public $nocache;
441 public $scope;
442 /**
443 * create Smarty variable object
444 *
445 * @param mixed $value the value to assign
446 * @param boolean $nocache if true any output of this variable will be not cached
447 * @param boolean $scope the scope the variable will have (local,parent or root)
448 */
449 public function __construct ($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
450 {
451 $this->value = $value;
452 $this->nocache = $nocache;
453 $this->scope = $scope;
454 }
456 public function __toString ()
457 {
458 return $this->value;
459 }
460 }
462 /**
463 * class for undefined variable object
464 *
465 * This class defines an object for undefined variable handling
466 */
467 class Undefined_Smarty_Variable {
468 // return always false
469 public function __get ($name)
470 {
471 if ($name == 'nocache') {
472 return false;
473 } else {
474 return null;
475 }
476 }
477 }
479 ?>