/core/model/smarty/sysplugins/smarty_internal_data.php

https://github.com/hatone/revolution · PHP · 454 lines · 280 code · 18 blank · 156 comment · 84 complexity · 7fbb6eed54cd8735493433d0c72e491a 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);
  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. $_result[$key] = $var->value;
  194. }
  195. // not found, try at parent
  196. if ($search_parents) {
  197. $_ptr = $_ptr->parent;
  198. } else {
  199. $_ptr = null;
  200. }
  201. }
  202. if ($search_parents && isset(Smarty::$global_tpl_vars)) {
  203. foreach (Smarty::$global_tpl_vars AS $key => $var) {
  204. $_result[$key] = $var->value;
  205. }
  206. }
  207. return $_result;
  208. }
  209. }
  210. /**
  211. * clear the given assigned template variable.
  212. *
  213. * @param string $ |array $tpl_var the template variable(s) to clear
  214. */
  215. public function clearAssign($tpl_var)
  216. {
  217. if (is_array($tpl_var)) {
  218. foreach ($tpl_var as $curr_var) {
  219. unset($this->tpl_vars[$curr_var]);
  220. }
  221. } else {
  222. unset($this->tpl_vars[$tpl_var]);
  223. }
  224. }
  225. /**
  226. * clear all the assigned template variables.
  227. */
  228. public function clearAllAssign()
  229. {
  230. $this->tpl_vars = array();
  231. }
  232. /**
  233. * load a config file, optionally load just selected sections
  234. *
  235. * @param string $config_file filename
  236. * @param mixed $sections array of section names, single section or null
  237. */
  238. public function configLoad($config_file, $sections = null)
  239. {
  240. // load Config class
  241. $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
  242. $config->loadConfigVars($sections);
  243. }
  244. /**
  245. * gets the object of a Smarty variable
  246. *
  247. * @param string $variable the name of the Smarty variable
  248. * @param object $_ptr optional pointer to data object
  249. * @param boolean $search_parents search also in parent data
  250. * @return object the object of the variable
  251. */
  252. public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
  253. {
  254. if ($_ptr === null) {
  255. $_ptr = $this;
  256. } while ($_ptr !== null) {
  257. if (isset($_ptr->tpl_vars[$variable])) {
  258. // found it, return it
  259. return $_ptr->tpl_vars[$variable];
  260. }
  261. // not found, try at parent
  262. if ($search_parents) {
  263. $_ptr = $_ptr->parent;
  264. } else {
  265. $_ptr = null;
  266. }
  267. }
  268. if (isset(Smarty::$global_tpl_vars[$variable])) {
  269. // found it, return it
  270. return Smarty::$global_tpl_vars[$variable];
  271. }
  272. if ($this->smarty->error_unassigned && $error_enable) {
  273. throw new SmartyException('Undefined Smarty variable "' . $variable . '"');
  274. } else {
  275. return new Undefined_Smarty_Variable;
  276. }
  277. }
  278. /**
  279. * gets a config variable
  280. *
  281. * @param string $variable the name of the config variable
  282. * @return mixed the value of the config variable
  283. */
  284. public function getConfigVariable($variable)
  285. {
  286. $_ptr = $this;
  287. while ($_ptr !== null) {
  288. if (isset($_ptr->config_vars[$variable])) {
  289. // found it, return it
  290. return $_ptr->config_vars[$variable];
  291. }
  292. // not found, try at parent
  293. $_ptr = $_ptr->parent;
  294. }
  295. if ($this->smarty->error_unassigned) {
  296. throw new SmartyException('Undefined config variable "' . $variable . '"');
  297. } else {
  298. return null;
  299. }
  300. }
  301. /**
  302. * gets a stream variable
  303. *
  304. * @param string $variable the stream of the variable
  305. * @return mixed the value of the stream variable
  306. */
  307. public function getStreamVariable($variable)
  308. {
  309. $_result = '';
  310. if ($fp = fopen($variable, 'r+')) {
  311. while (!feof($fp)) {
  312. $_result .= fgets($fp);
  313. }
  314. fclose($fp);
  315. return $_result;
  316. }
  317. if ($this->smarty->error_unassigned) {
  318. throw new SmartyException('Undefined stream variable "' . $variable . '"');
  319. } else {
  320. return null;
  321. }
  322. }
  323. /**
  324. * Returns a single or all config variables
  325. *
  326. * @param string $varname variable name or null
  327. * @return string variable value or or array of variables
  328. */
  329. function getConfigVars($varname = null)
  330. {
  331. if (isset($varname)) {
  332. if (isset($this->config_vars[$varname])) {
  333. return $this->config_vars[$varname];
  334. } else {
  335. return '';
  336. }
  337. } else {
  338. return $this->config_vars;
  339. }
  340. }
  341. /**
  342. * Deassigns a single or all config variables
  343. *
  344. * @param string $varname variable name or null
  345. */
  346. function clearConfig($varname = null)
  347. {
  348. if (isset($varname)) {
  349. unset($this->config_vars[$varname]);
  350. return;
  351. } else {
  352. $this->config_vars = array();
  353. return;
  354. }
  355. }
  356. }
  357. /**
  358. * class for the Smarty data object
  359. *
  360. * The Smarty data object will hold Smarty variables in the current scope
  361. *
  362. * @param object $parent tpl_vars next higher level of Smarty variables
  363. */
  364. class Smarty_Data extends Smarty_Internal_Data {
  365. // array of variable objects
  366. public $tpl_vars = array();
  367. // back pointer to parent object
  368. public $parent = null;
  369. // config vars
  370. public $config_vars = array();
  371. // Smarty object
  372. public $smarty = null;
  373. /**
  374. * create Smarty data object
  375. */
  376. public function __construct ($_parent = null, $smarty = null)
  377. {
  378. $this->smarty = $smarty;
  379. if (is_object($_parent)) {
  380. // when object set up back pointer
  381. $this->parent = $_parent;
  382. } elseif (is_array($_parent)) {
  383. // set up variable values
  384. foreach ($_parent as $_key => $_val) {
  385. $this->tpl_vars[$_key] = new Smarty_variable($_val);
  386. }
  387. } elseif ($_parent != null) {
  388. throw new SmartyException("Wrong type for template variables");
  389. }
  390. }
  391. }
  392. /**
  393. * class for the Smarty variable object
  394. *
  395. * This class defines the Smarty variable object
  396. */
  397. class Smarty_Variable {
  398. // template variable
  399. public $value;
  400. public $nocache;
  401. public $scope;
  402. /**
  403. * create Smarty variable object
  404. *
  405. * @param mixed $value the value to assign
  406. * @param boolean $nocache if true any output of this variable will be not cached
  407. * @param boolean $scope the scope the variable will have (local,parent or root)
  408. */
  409. public function __construct ($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
  410. {
  411. $this->value = $value;
  412. $this->nocache = $nocache;
  413. $this->scope = $scope;
  414. }
  415. public function __toString ()
  416. {
  417. return $this->value;
  418. }
  419. }
  420. /**
  421. * class for undefined variable object
  422. *
  423. * This class defines an object for undefined variable handling
  424. */
  425. class Undefined_Smarty_Variable {
  426. // return always false
  427. public function __get ($name)
  428. {
  429. if ($name == 'nocache') {
  430. return false;
  431. } else {
  432. return null;
  433. }
  434. }
  435. }
  436. ?>