smarty_internal_data.php 15 KB

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