PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/library/Smarty/Internal/Data.php

https://gitlab.com/flyhope/Hiblog
PHP | 404 lines | 222 code | 25 blank | 157 comment | 69 complexity | 318800632586623625b2c413b80ea725 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Data
  4. * This file contains the basic classes and methods for template and variable creation
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Base class with template and variable methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. */
  16. class Smarty_Internal_Data
  17. {
  18. /**
  19. * name of class used for templates
  20. *
  21. * @var string
  22. */
  23. public $template_class = 'Smarty_Internal_Template';
  24. /**
  25. * template variables
  26. *
  27. * @var array
  28. */
  29. public $tpl_vars = array();
  30. /**
  31. * parent template (if any)
  32. *
  33. * @var Smarty_Internal_Template
  34. */
  35. public $parent = null;
  36. /**
  37. * configuration settings
  38. *
  39. * @var array
  40. */
  41. public $config_vars = array();
  42. /**
  43. * assigns a Smarty variable
  44. *
  45. * @param array|string $tpl_var the template variable name(s)
  46. * @param mixed $value the value to assign
  47. * @param boolean $nocache if true any output of this variable will be not cached
  48. *
  49. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  50. */
  51. public function assign($tpl_var, $value = null, $nocache = false)
  52. {
  53. if (is_array($tpl_var)) {
  54. foreach ($tpl_var as $_key => $_val) {
  55. if ($_key != '') {
  56. $this->tpl_vars[$_key] = new Smarty_Variable($_val, $nocache);
  57. }
  58. }
  59. } else {
  60. if ($tpl_var != '') {
  61. $this->tpl_vars[$tpl_var] = new Smarty_Variable($value, $nocache);
  62. }
  63. }
  64. return $this;
  65. }
  66. /**
  67. * assigns a global Smarty variable
  68. *
  69. * @param string $varname the global variable name
  70. * @param mixed $value the value to assign
  71. * @param boolean $nocache if true any output of this variable will be not cached
  72. *
  73. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  74. */
  75. public function assignGlobal($varname, $value = null, $nocache = false)
  76. {
  77. if ($varname != '') {
  78. Smarty::$global_tpl_vars[$varname] = new Smarty_Variable($value, $nocache);
  79. $ptr = $this;
  80. while ($ptr instanceof Smarty_Internal_Template) {
  81. $ptr->tpl_vars[$varname] = clone Smarty::$global_tpl_vars[$varname];
  82. $ptr = $ptr->parent;
  83. }
  84. }
  85. return $this;
  86. }
  87. /**
  88. * assigns values to template variables by reference
  89. *
  90. * @param string $tpl_var the template variable name
  91. * @param $value
  92. * @param boolean $nocache if true any output of this variable will be not cached
  93. *
  94. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  95. */
  96. public function assignByRef($tpl_var, &$value, $nocache = false)
  97. {
  98. if ($tpl_var != '') {
  99. $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
  100. $this->tpl_vars[$tpl_var]->value = &$value;
  101. }
  102. return $this;
  103. }
  104. /**
  105. * appends values to template variables
  106. *
  107. * @param array|string $tpl_var the template variable name(s)
  108. * @param mixed $value the value to append
  109. * @param boolean $merge flag if array elements shall be merged
  110. * @param boolean $nocache if true any output of this variable will be not cached
  111. *
  112. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  113. */
  114. public function append($tpl_var, $value = null, $merge = false, $nocache = false)
  115. {
  116. if (is_array($tpl_var)) {
  117. // $tpl_var is an array, ignore $value
  118. foreach ($tpl_var as $_key => $_val) {
  119. if ($_key != '') {
  120. if (!isset($this->tpl_vars[$_key])) {
  121. $tpl_var_inst = $this->getVariable($_key, null, true, false);
  122. if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
  123. $this->tpl_vars[$_key] = new Smarty_Variable(null, $nocache);
  124. } else {
  125. $this->tpl_vars[$_key] = clone $tpl_var_inst;
  126. }
  127. }
  128. if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
  129. settype($this->tpl_vars[$_key]->value, 'array');
  130. }
  131. if ($merge && is_array($_val)) {
  132. foreach ($_val as $_mkey => $_mval) {
  133. $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
  134. }
  135. } else {
  136. $this->tpl_vars[$_key]->value[] = $_val;
  137. }
  138. }
  139. }
  140. } else {
  141. if ($tpl_var != '' && isset($value)) {
  142. if (!isset($this->tpl_vars[$tpl_var])) {
  143. $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
  144. if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
  145. $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
  146. } else {
  147. $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
  148. }
  149. }
  150. if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
  151. settype($this->tpl_vars[$tpl_var]->value, 'array');
  152. }
  153. if ($merge && is_array($value)) {
  154. foreach ($value as $_mkey => $_mval) {
  155. $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
  156. }
  157. } else {
  158. $this->tpl_vars[$tpl_var]->value[] = $value;
  159. }
  160. }
  161. }
  162. return $this;
  163. }
  164. /**
  165. * appends values to template variables by reference
  166. *
  167. * @param string $tpl_var the template variable name
  168. * @param mixed &$value the referenced value to append
  169. * @param boolean $merge flag if array elements shall be merged
  170. *
  171. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  172. */
  173. public function appendByRef($tpl_var, &$value, $merge = false)
  174. {
  175. if ($tpl_var != '' && isset($value)) {
  176. if (!isset($this->tpl_vars[$tpl_var])) {
  177. $this->tpl_vars[$tpl_var] = new Smarty_Variable();
  178. }
  179. if (!is_array($this->tpl_vars[$tpl_var]->value)) {
  180. settype($this->tpl_vars[$tpl_var]->value, 'array');
  181. }
  182. if ($merge && is_array($value)) {
  183. foreach ($value as $_key => $_val) {
  184. $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
  185. }
  186. } else {
  187. $this->tpl_vars[$tpl_var]->value[] = &$value;
  188. }
  189. }
  190. return $this;
  191. }
  192. /**
  193. * Returns a single or all template variables
  194. *
  195. * @param string $varname variable name or null
  196. * @param object $_ptr optional pointer to data object
  197. * @param boolean $search_parents include parent templates?
  198. *
  199. * @return string variable value or or array of variables
  200. */
  201. public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
  202. {
  203. if (isset($varname)) {
  204. $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
  205. if (is_object($_var)) {
  206. return $_var->value;
  207. } else {
  208. return null;
  209. }
  210. } else {
  211. $_result = array();
  212. if ($_ptr === null) {
  213. $_ptr = $this;
  214. }
  215. while ($_ptr !== null) {
  216. foreach ($_ptr->tpl_vars AS $key => $var) {
  217. if (!array_key_exists($key, $_result)) {
  218. $_result[$key] = $var->value;
  219. }
  220. }
  221. // not found, try at parent
  222. if ($search_parents) {
  223. $_ptr = $_ptr->parent;
  224. } else {
  225. $_ptr = null;
  226. }
  227. }
  228. if ($search_parents && isset(Smarty::$global_tpl_vars)) {
  229. foreach (Smarty::$global_tpl_vars AS $key => $var) {
  230. if (!array_key_exists($key, $_result)) {
  231. $_result[$key] = $var->value;
  232. }
  233. }
  234. }
  235. return $_result;
  236. }
  237. }
  238. /**
  239. * clear the given assigned template variable.
  240. *
  241. * @param string|array $tpl_var the template variable(s) to clear
  242. *
  243. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  244. */
  245. public function clearAssign($tpl_var)
  246. {
  247. if (is_array($tpl_var)) {
  248. foreach ($tpl_var as $curr_var) {
  249. unset($this->tpl_vars[$curr_var]);
  250. }
  251. } else {
  252. unset($this->tpl_vars[$tpl_var]);
  253. }
  254. return $this;
  255. }
  256. /**
  257. * clear all the assigned template variables.
  258. *
  259. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  260. */
  261. public function clearAllAssign()
  262. {
  263. $this->tpl_vars = array();
  264. return $this;
  265. }
  266. /**
  267. * load a config file, optionally load just selected sections
  268. *
  269. * @param string $config_file filename
  270. * @param mixed $sections array of section names, single section or null
  271. *
  272. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  273. */
  274. public function configLoad($config_file, $sections = null)
  275. {
  276. // load Config class
  277. Smarty_Internal_Extension_Config::configLoad($this, $config_file, $sections);
  278. return $this;
  279. }
  280. /**
  281. * gets the object of a Smarty variable
  282. *
  283. * @param string $variable the name of the Smarty variable
  284. * @param object $_ptr optional pointer to data object
  285. * @param boolean $search_parents search also in parent data
  286. * @param bool $error_enable
  287. *
  288. * @return object the object of the variable
  289. */
  290. public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
  291. {
  292. if ($_ptr === null) {
  293. $_ptr = $this;
  294. }
  295. while ($_ptr !== null) {
  296. if (isset($_ptr->tpl_vars[$variable])) {
  297. // found it, return it
  298. return $_ptr->tpl_vars[$variable];
  299. }
  300. // not found, try at parent
  301. if ($search_parents) {
  302. $_ptr = $_ptr->parent;
  303. } else {
  304. $_ptr = null;
  305. }
  306. }
  307. if (isset(Smarty::$global_tpl_vars[$variable])) {
  308. // found it, return it
  309. return Smarty::$global_tpl_vars[$variable];
  310. }
  311. $smarty = isset($this->smarty) ? $this->smarty : $this;
  312. if ($smarty->error_unassigned && $error_enable) {
  313. // force a notice
  314. $x = $$variable;
  315. }
  316. return new Smarty_Undefined_Variable;
  317. }
  318. /**
  319. * gets a config variable
  320. *
  321. * @param string $variable the name of the config variable
  322. * @param bool $error_enable
  323. *
  324. * @return mixed the value of the config variable
  325. */
  326. public function getConfigVariable($variable, $error_enable = true)
  327. {
  328. return Smarty_Internal_Extension_Config::getConfigVariable($this, $variable, $error_enable = true);
  329. }
  330. /**
  331. * Returns a single or all config variables
  332. *
  333. * @param string $varname variable name or null
  334. * @param bool $search_parents
  335. *
  336. * @return string variable value or or array of variables
  337. */
  338. public function getConfigVars($varname = null, $search_parents = true)
  339. {
  340. return Smarty_Internal_Extension_Config::getConfigVars($this, $varname, $search_parents);
  341. }
  342. /**
  343. * Deassigns a single or all config variables
  344. *
  345. * @param string $varname variable name or null
  346. *
  347. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
  348. */
  349. public function clearConfig($varname = null)
  350. {
  351. return Smarty_Internal_Extension_Config::clearConfig($this, $varname);
  352. }
  353. /**
  354. * gets a stream variable
  355. *
  356. * @param string $variable the stream of the variable
  357. *
  358. * @throws Smarty_SmartyException
  359. * @return mixed the value of the stream variable
  360. */
  361. public function getStreamVariable($variable)
  362. {
  363. $_result = '';
  364. $fp = fopen($variable, 'r+');
  365. if ($fp) {
  366. while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
  367. $_result .= $current_line;
  368. }
  369. fclose($fp);
  370. return $_result;
  371. }
  372. $smarty = isset($this->smarty) ? $this->smarty : $this;
  373. if ($smarty->error_unassigned) {
  374. throw new Smarty_SmartyException('Undefined stream variable "' . $variable . '"');
  375. } else {
  376. return null;
  377. }
  378. }
  379. }