/includes/smarty/sysplugins/smarty_internal_data.php

https://github.com/ACCORD5/TrellisDesk · PHP · 413 lines · 250 code · 15 blank · 148 comment · 80 complexity · 13681763b510ade6a48af5328c53d30d 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 assign_global($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 assign_by_ref($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 append_by_ref($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. * clear the given assigned template variable.
  158. *
  159. * @param string $ |array $tpl_var the template variable(s) to clear
  160. */
  161. public function clear_assign($tpl_var)
  162. {
  163. if (is_array($tpl_var)) {
  164. foreach ($tpl_var as $curr_var) {
  165. unset($this->tpl_vars[$curr_var]);
  166. }
  167. } else {
  168. unset($this->tpl_vars[$tpl_var]);
  169. }
  170. }
  171. /**
  172. * clear all the assigned template variables.
  173. */
  174. public function clear_all_assign()
  175. {
  176. $this->tpl_vars = array();
  177. }
  178. /**
  179. * load a config file, optionally load just selected sections
  180. *
  181. * @param string $config_file filename
  182. * @param mixed $sections array of section names, single section or null
  183. */
  184. public function config_load($config_file, $sections = null)
  185. {
  186. // load Config class
  187. $config = new Smarty_Internal_Config($config_file, $this->smarty);
  188. $config->loadConfigVars($sections, $this);
  189. }
  190. /**
  191. * gets the object of a Smarty variable
  192. *
  193. * @param string $variable the name of the Smarty variable
  194. * @param object $_ptr optional pointer to data object
  195. * @param boolean $search_parents search also in parent data
  196. * @return object the object of the variable
  197. */
  198. public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
  199. {
  200. if ($_ptr === null) {
  201. $_ptr = $this;
  202. } while ($_ptr !== null) {
  203. if (isset($_ptr->tpl_vars[$variable])) {
  204. // found it, return it
  205. return $_ptr->tpl_vars[$variable];
  206. }
  207. // not found, try at parent
  208. if ($search_parents) {
  209. $_ptr = $_ptr->parent;
  210. } else {
  211. $_ptr = null;
  212. }
  213. }
  214. if (isset($this->smarty->global_tpl_vars[$variable])) {
  215. // found it, return it
  216. return $this->smarty->global_tpl_vars[$variable];
  217. }
  218. if ($this->smarty->error_unassigned && $error_enable) {
  219. throw new Exception('Undefined Smarty variable "' . $variable . '"');
  220. } else {
  221. return new Undefined_Smarty_Variable;
  222. }
  223. }
  224. /**
  225. * gets a config variable
  226. *
  227. * @param string $variable the name of the config variable
  228. * @return mixed the value of the config variable
  229. */
  230. public function getConfigVariable($variable)
  231. {
  232. $_ptr = $this;
  233. while ($_ptr !== null) {
  234. if (isset($_ptr->config_vars[$variable])) {
  235. // found it, return it
  236. return $_ptr->config_vars[$variable];
  237. }
  238. // not found, try at parent
  239. $_ptr = $_ptr->parent;
  240. }
  241. if ($this->smarty->error_unassigned) {
  242. throw new Exception('Undefined config variable "' . $variable . '"');
  243. } else {
  244. return '';
  245. }
  246. }
  247. /**
  248. * gets a stream variable
  249. *
  250. * @param string $variable the stream of the variable
  251. * @return mixed the value of the stream variable
  252. */
  253. public function getStreamVariable($variable)
  254. {
  255. $_result = '';
  256. if ($fp = fopen($variable, 'r+')) {
  257. while (!feof($fp)) {
  258. $_result .= fgets($fp);
  259. }
  260. fclose($fp);
  261. return $_result;
  262. }
  263. if ($this->smarty->$error_unassigned) {
  264. throw new Exception('Undefined stream variable "' . $variable . '"');
  265. } else {
  266. return '';
  267. }
  268. }
  269. /**
  270. * creates a template object
  271. *
  272. * @param string $template the resource handle of the template file
  273. * @param object $parent next higher level of Smarty variables
  274. * @param mixed $cache_id cache id to be used with this template
  275. * @param mixed $compile_id compile id to be used with this template
  276. * @returns object template object
  277. */
  278. public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
  279. {
  280. if (is_object($cache_id) || is_array($cache_id)) {
  281. $parent = $cache_id;
  282. $cache_id = null;
  283. }
  284. if (is_array($parent)) {
  285. $data = $parent;
  286. $parent = null;
  287. } else {
  288. $data = null;
  289. }
  290. if (!is_object($template)) {
  291. // we got a template resource
  292. // already in template cache?
  293. $_templateId = crc32($template . $cache_id . $compile_id);
  294. if (isset($this->smarty->template_objects[$_templateId]) && $this->smarty->caching) {
  295. // return cached template object
  296. $tpl = $this->smarty->template_objects[$_templateId];
  297. } else {
  298. // create new template object
  299. $tpl = new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id);
  300. }
  301. } else {
  302. // just return a copy of template class
  303. $tpl = $template;
  304. }
  305. // fill data if present
  306. if (is_array($data)) {
  307. // set up variable values
  308. foreach ($data as $_key => $_val) {
  309. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  310. }
  311. }
  312. return $tpl;
  313. }
  314. /**
  315. * return current time
  316. *
  317. * @returns double current time
  318. */
  319. function _get_time()
  320. {
  321. $_mtime = microtime();
  322. $_mtime = explode(" ", $_mtime);
  323. return (double)($_mtime[1]) + (double)($_mtime[0]);
  324. }
  325. }
  326. /**
  327. * class for the Smarty data object
  328. *
  329. * The Smarty data object will hold Smarty variables in the current scope
  330. *
  331. * @param object $parent tpl_vars next higher level of Smarty variables
  332. */
  333. class Smarty_Data extends Smarty_Internal_Data {
  334. // array of variable objects
  335. public $tpl_vars = array();
  336. // back pointer to parent object
  337. public $parent = null;
  338. // config vars
  339. public $config_vars = array();
  340. /**
  341. * create Smarty data object
  342. */
  343. public function __construct ($_parent = null)
  344. {
  345. if (is_object($_parent)) {
  346. // when object set up back pointer
  347. $this->parent = $_parent;
  348. } elseif (is_array($_parent)) {
  349. // set up variable values
  350. foreach ($_parent as $_key => $_val) {
  351. $this->tpl_vars[$_key] = new Smarty_variable($_val);
  352. }
  353. } else {
  354. throw new Exception("Wrong type for template variables");
  355. }
  356. }
  357. }
  358. /**
  359. * class for the Smarty variable object
  360. *
  361. * This class defines the Smarty variable object
  362. */
  363. class Smarty_Variable {
  364. // template variable
  365. public $value;
  366. public $nocache;
  367. public $scope;
  368. /**
  369. * create Smarty variable object
  370. *
  371. * @param mixed $value the value to assign
  372. * @param boolean $nocache if true any output of this variable will be not cached
  373. * @param boolean $scope the scope the variable will have (local,parent or root)
  374. */
  375. public function __construct ($value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
  376. {
  377. $this->value = $value;
  378. $this->nocache = $nocache;
  379. $this->scope = $scope;
  380. }
  381. }
  382. /**
  383. * class for undefined variable object
  384. *
  385. * This class defines an object for undefined variable handling
  386. */
  387. class Undefined_Smarty_Variable {
  388. // return always false
  389. public function __get ($name)
  390. {
  391. if ($name == 'nocache') {
  392. return false;
  393. } else {
  394. return null;
  395. }
  396. }
  397. }
  398. ?>