/smarty/sysplugins/smarty_internal_data.php

https://github.com/taptouchclick/The-Event-Day · PHP · 479 lines · 300 code · 19 blank · 160 comment · 92 complexity · 6274af2d60042ec3589c41329143ec75 MD5 · raw file

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