smarty_internal_data.php 17 KB

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