/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_data.php

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