smarty_internal_template.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Template
  4. *
  5. * This file contains the Smarty template engine
  6. *
  7. * @package Smarty
  8. * @subpackage Template
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Main class with template data structures and methods
  13. *
  14. * @package Smarty
  15. * @subpackage Template
  16. *
  17. * @property Smarty_Template_Source $source
  18. * @property Smarty_Template_Compiled $compiled
  19. * @property Smarty_Template_Cached $cached
  20. */
  21. class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
  22. /**
  23. * cache_id
  24. * @var string
  25. */
  26. public $cache_id = null;
  27. /**
  28. * $compile_id
  29. * @var string
  30. */
  31. public $compile_id = null;
  32. /**
  33. * caching enabled
  34. * @var boolean
  35. */
  36. public $caching = null;
  37. /**
  38. * cache lifetime in seconds
  39. * @var integer
  40. */
  41. public $cache_lifetime = null;
  42. /**
  43. * Template resource
  44. * @var string
  45. */
  46. public $template_resource = null;
  47. /**
  48. * flag if compiled template is invalid and must be (re)compiled
  49. * @var bool
  50. */
  51. public $mustCompile = null;
  52. /**
  53. * flag if template does contain nocache code sections
  54. * @var bool
  55. */
  56. public $has_nocache_code = false;
  57. /**
  58. * special compiled and cached template properties
  59. * @var array
  60. */
  61. public $properties = array('file_dependency' => array(),
  62. 'nocache_hash' => '',
  63. 'function' => array());
  64. /**
  65. * required plugins
  66. * @var array
  67. */
  68. public $required_plugins = array('compiled' => array(), 'nocache' => array());
  69. /**
  70. * Global smarty instance
  71. * @var Smarty
  72. */
  73. public $smarty = null;
  74. /**
  75. * blocks for template inheritance
  76. * @var array
  77. */
  78. public $block_data = array();
  79. /**
  80. * variable filters
  81. * @var array
  82. */
  83. public $variable_filters = array();
  84. /**
  85. * optional log of tag/attributes
  86. * @var array
  87. */
  88. public $used_tags = array();
  89. /**
  90. * internal flag to allow relative path in child template blocks
  91. * @var bool
  92. */
  93. public $allow_relative_path = false;
  94. /**
  95. * internal capture runtime stack
  96. * @var array
  97. */
  98. public $_capture_stack = array(0 => array());
  99. /**
  100. * Create template data object
  101. *
  102. * Some of the global Smarty settings copied to template scope
  103. * It load the required template resources and cacher plugins
  104. *
  105. * @param string $template_resource template resource string
  106. * @param Smarty $smarty Smarty instance
  107. * @param Smarty_Internal_Template $_parent back pointer to parent object with variables or null
  108. * @param mixed $_cache_id cache id or null
  109. * @param mixed $_compile_id compile id or null
  110. * @param bool $_caching use caching?
  111. * @param int $_cache_lifetime cache life-time in seconds
  112. */
  113. public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
  114. {
  115. $this->smarty = &$smarty;
  116. // Smarty parameter
  117. $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
  118. $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
  119. $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
  120. if ($this->caching === true)
  121. $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  122. $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
  123. $this->parent = $_parent;
  124. // Template resource
  125. $this->template_resource = $template_resource;
  126. // copy block data of template inheritance
  127. if ($this->parent instanceof Smarty_Internal_Template) {
  128. $this->block_data = $this->parent->block_data;
  129. }
  130. }
  131. /**
  132. * Returns if the current template must be compiled by the Smarty compiler
  133. *
  134. * It does compare the timestamps of template source and the compiled templates and checks the force compile configuration
  135. *
  136. * @return boolean true if the template must be compiled
  137. */
  138. public function mustCompile()
  139. {
  140. if (!$this->source->exists) {
  141. if ($this->parent instanceof Smarty_Internal_Template) {
  142. $parent_resource = " in '$this->parent->template_resource}'";
  143. } else {
  144. $parent_resource = '';
  145. }
  146. throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
  147. }
  148. if ($this->mustCompile === null) {
  149. $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false ||
  150. ($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp)));
  151. }
  152. return $this->mustCompile;
  153. }
  154. /**
  155. * Compiles the template
  156. *
  157. * If the template is not evaluated the compiled template is saved on disk
  158. */
  159. public function compileTemplateSource()
  160. {
  161. if (!$this->source->recompiled) {
  162. $this->properties['file_dependency'] = array();
  163. if ($this->source->components) {
  164. // uses real resource for file dependency
  165. $source = end($this->source->components);
  166. $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
  167. } else {
  168. $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type);
  169. }
  170. }
  171. if ($this->smarty->debugging) {
  172. Smarty_Internal_Debug::start_compile($this);
  173. }
  174. // compile locking
  175. if ($this->smarty->compile_locking && !$this->source->recompiled) {
  176. if ($saved_timestamp = $this->compiled->timestamp) {
  177. touch($this->compiled->filepath);
  178. }
  179. }
  180. // call compiler
  181. try {
  182. $code = $this->compiler->compileTemplate($this);
  183. } catch (Exception $e) {
  184. // restore old timestamp in case of error
  185. if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
  186. touch($this->compiled->filepath, $saved_timestamp);
  187. }
  188. throw $e;
  189. }
  190. // compiling succeded
  191. if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
  192. // write compiled template
  193. $_filepath = $this->compiled->filepath;
  194. if ($_filepath === false)
  195. throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
  196. Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty);
  197. $this->compiled->exists = true;
  198. $this->compiled->isCompiled = true;
  199. }
  200. if ($this->smarty->debugging) {
  201. Smarty_Internal_Debug::end_compile($this);
  202. }
  203. // release compiler object to free memory
  204. unset($this->compiler);
  205. }
  206. /**
  207. * Writes the cached template output
  208. *
  209. * @return bool
  210. */
  211. public function writeCachedContent($content)
  212. {
  213. if ($this->source->recompiled || !($this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED)) {
  214. // don't write cache file
  215. return false;
  216. }
  217. $this->properties['cache_lifetime'] = $this->cache_lifetime;
  218. $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
  219. $content = $this->createTemplateCodeFrame($content, true);
  220. $_smarty_tpl = $this;
  221. eval("?>" . $content);
  222. $this->cached->valid = true;
  223. $this->cached->processed = true;
  224. return $this->cached->write($this, $content);
  225. }
  226. /**
  227. * Template code runtime function to get subtemplate content
  228. *
  229. * @param string $template the resource handle of the template file
  230. * @param mixed $cache_id cache id to be used with this template
  231. * @param mixed $compile_id compile id to be used with this template
  232. * @param integer $caching cache mode
  233. * @param integer $cache_lifetime life time of cache data
  234. * @param array $vars optional variables to assign
  235. * @param int $parent_scope scope in which {include} should execute
  236. * @returns string template content
  237. */
  238. public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
  239. {
  240. // already in template cache?
  241. if ($this->smarty->allow_ambiguous_resources) {
  242. $_templateId = Smarty_Resource::getUniqueTemplateName($this->smarty, $template) . $cache_id . $compile_id;
  243. } else {
  244. $_templateId = $this->smarty->joined_template_dir . '#' . $template . $cache_id . $compile_id;
  245. }
  246. if (isset($_templateId[150])) {
  247. $_templateId = sha1($_templateId);
  248. }
  249. if (isset($this->smarty->template_objects[$_templateId])) {
  250. // clone cached template object because of possible recursive call
  251. $tpl = clone $this->smarty->template_objects[$_templateId];
  252. $tpl->parent = $this;
  253. $tpl->caching = $caching;
  254. $tpl->cache_lifetime = $cache_lifetime;
  255. } else {
  256. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  257. }
  258. // get variables from calling scope
  259. if ($parent_scope == Smarty::SCOPE_LOCAL) {
  260. $tpl->tpl_vars = $this->tpl_vars;
  261. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  262. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  263. $tpl->tpl_vars = &$this->tpl_vars;
  264. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  265. $tpl->tpl_vars = &Smarty::$global_tpl_vars;
  266. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  267. $tpl->tpl_vars = &$this->tpl_vars;
  268. } else {
  269. $tpl->tpl_vars = &$scope_ptr->tpl_vars;
  270. }
  271. $tpl->config_vars = $this->config_vars;
  272. if (!empty($data)) {
  273. // set up variable values
  274. foreach ($data as $_key => $_val) {
  275. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  276. }
  277. }
  278. return $tpl->fetch(null, null, null, null, false, false, true);
  279. }
  280. /**
  281. * Template code runtime function to set up an inline subtemplate
  282. *
  283. * @param string $template the resource handle of the template file
  284. * @param mixed $cache_id cache id to be used with this template
  285. * @param mixed $compile_id compile id to be used with this template
  286. * @param integer $caching cache mode
  287. * @param integer $cache_lifetime life time of cache data
  288. * @param array $vars optional variables to assign
  289. * @param int $parent_scope scope in which {include} should execute
  290. * @param string $hash nocache hash code
  291. * @returns string template content
  292. */
  293. public function setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
  294. {
  295. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  296. $tpl->properties['nocache_hash'] = $hash;
  297. // get variables from calling scope
  298. if ($parent_scope == Smarty::SCOPE_LOCAL ) {
  299. $tpl->tpl_vars = $this->tpl_vars;
  300. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  301. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  302. $tpl->tpl_vars = &$this->tpl_vars;
  303. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  304. $tpl->tpl_vars = &Smarty::$global_tpl_vars;
  305. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  306. $tpl->tpl_vars = &$this->tpl_vars;
  307. } else {
  308. $tpl->tpl_vars = &$scope_ptr->tpl_vars;
  309. }
  310. $tpl->config_vars = $this->config_vars;
  311. if (!empty($data)) {
  312. // set up variable values
  313. foreach ($data as $_key => $_val) {
  314. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  315. }
  316. }
  317. return $tpl;
  318. }
  319. /**
  320. * Create code frame for compiled and cached templates
  321. *
  322. * @param string $content optional template content
  323. * @param bool $cache flag for cache file
  324. * @return string
  325. */
  326. public function createTemplateCodeFrame($content = '', $cache = false)
  327. {
  328. $plugins_string = '';
  329. // include code for plugins
  330. if (!$cache) {
  331. if (!empty($this->required_plugins['compiled'])) {
  332. $plugins_string = '<?php ';
  333. foreach ($this->required_plugins['compiled'] as $tmp) {
  334. foreach ($tmp as $data) {
  335. $file = addslashes($data['file']);
  336. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
  337. }
  338. }
  339. $plugins_string .= '?>';
  340. }
  341. if (!empty($this->required_plugins['nocache'])) {
  342. $this->has_nocache_code = true;
  343. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
  344. foreach ($this->required_plugins['nocache'] as $tmp) {
  345. foreach ($tmp as $data) {
  346. $file = addslashes($data['file']);
  347. $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
  348. }
  349. }
  350. $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
  351. }
  352. }
  353. // build property code
  354. $this->properties['has_nocache_code'] = $this->has_nocache_code;
  355. $output = '';
  356. if (!$this->source->recompiled) {
  357. $output = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
  358. if ($this->smarty->direct_access_security) {
  359. $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
  360. }
  361. }
  362. if ($cache) {
  363. // remove compiled code of{function} definition
  364. unset($this->properties['function']);
  365. if (!empty($this->smarty->template_functions)) {
  366. // copy code of {function} tags called in nocache mode
  367. foreach ($this->smarty->template_functions as $name => $function_data) {
  368. if (isset($function_data['called_nocache'])) {
  369. foreach ($function_data['called_functions'] as $func_name) {
  370. $this->smarty->template_functions[$func_name]['called_nocache'] = true;
  371. }
  372. }
  373. }
  374. foreach ($this->smarty->template_functions as $name => $function_data) {
  375. if (isset($function_data['called_nocache'])) {
  376. unset($function_data['called_nocache'], $function_data['called_functions'], $this->smarty->template_functions[$name]['called_nocache']);
  377. $this->properties['function'][$name] = $function_data;
  378. }
  379. }
  380. }
  381. }
  382. $this->properties['version'] = Smarty::SMARTY_VERSION;
  383. if (!isset($this->properties['unifunc'])) {
  384. $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
  385. }
  386. if (!$this->source->recompiled) {
  387. $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
  388. $output .= '<?php if ($_valid && !is_callable(\'' . $this->properties['unifunc'] . '\')) {function ' . $this->properties['unifunc'] . '($_smarty_tpl) {?>';
  389. }
  390. $output .= $plugins_string;
  391. $output .= $content;
  392. if (!$this->source->recompiled) {
  393. $output .= '<?php }} ?>';
  394. }
  395. return $output;
  396. }
  397. /**
  398. * This function is executed automatically when a compiled or cached template file is included
  399. *
  400. * - Decode saved properties from compiled template and cache files
  401. * - Check if compiled or cache file is valid
  402. *
  403. * @param array $properties special template properties
  404. * @param bool $cache flag if called from cache file
  405. * @return bool flag if compiled or cache file is valid
  406. */
  407. public function decodeProperties($properties, $cache = false)
  408. {
  409. $this->has_nocache_code = $properties['has_nocache_code'];
  410. $this->properties['nocache_hash'] = $properties['nocache_hash'];
  411. if (isset($properties['cache_lifetime'])) {
  412. $this->properties['cache_lifetime'] = $properties['cache_lifetime'];
  413. }
  414. if (isset($properties['file_dependency'])) {
  415. $this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
  416. }
  417. if (!empty($properties['function'])) {
  418. $this->properties['function'] = array_merge($this->properties['function'], $properties['function']);
  419. $this->smarty->template_functions = array_merge($this->smarty->template_functions, $properties['function']);
  420. }
  421. $this->properties['version'] = (isset($properties['version'])) ? $properties['version'] : '';
  422. $this->properties['unifunc'] = $properties['unifunc'];
  423. // check file dependencies at compiled code
  424. $is_valid = true;
  425. if ($this->properties['version'] != Smarty::SMARTY_VERSION) {
  426. $is_valid = false;
  427. } else if (((!$cache && $this->smarty->compile_check && empty($this->compiled->_properties) && !$this->compiled->isCompiled) || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($this->properties['file_dependency'])) {
  428. foreach ($this->properties['file_dependency'] as $_file_to_check) {
  429. if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') {
  430. if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) {
  431. // do not recheck current template
  432. $mtime = $this->source->timestamp;
  433. } else {
  434. // file and php types can be checked without loading the respective resource handlers
  435. $mtime = @filemtime($_file_to_check[0]);
  436. }
  437. } elseif ($_file_to_check[2] == 'string') {
  438. continue;
  439. } else {
  440. $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
  441. $mtime = $source->timestamp;
  442. }
  443. if (!$mtime || $mtime > $_file_to_check[1]) {
  444. $is_valid = false;
  445. break;
  446. }
  447. }
  448. }
  449. if ($cache) {
  450. $this->cached->valid = $is_valid;
  451. } else {
  452. $this->mustCompile = !$is_valid;
  453. }
  454. // store data in reusable Smarty_Template_Compiled
  455. if (!$cache) {
  456. $this->compiled->_properties = $properties;
  457. }
  458. return $is_valid;
  459. }
  460. /**
  461. * Template code runtime function to create a local Smarty variable for array assignments
  462. *
  463. * @param string $tpl_var tempate variable name
  464. * @param bool $nocache cache mode of variable
  465. * @param int $scope scope of variable
  466. */
  467. public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
  468. {
  469. if (!isset($this->tpl_vars[$tpl_var])) {
  470. $this->tpl_vars[$tpl_var] = new Smarty_variable(array(), $nocache, $scope);
  471. } else {
  472. $this->tpl_vars[$tpl_var] = clone $this->tpl_vars[$tpl_var];
  473. if ($scope != Smarty::SCOPE_LOCAL) {
  474. $this->tpl_vars[$tpl_var]->scope = $scope;
  475. }
  476. if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
  477. settype($this->tpl_vars[$tpl_var]->value, 'array');
  478. }
  479. }
  480. }
  481. /**
  482. * Template code runtime function to get pointer to template variable array of requested scope
  483. *
  484. * @param int $scope requested variable scope
  485. * @return array array of template variables
  486. */
  487. public function &getScope($scope)
  488. {
  489. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  490. return $this->parent->tpl_vars;
  491. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  492. $ptr = $this->parent;
  493. while (!empty($ptr->parent)) {
  494. $ptr = $ptr->parent;
  495. }
  496. return $ptr->tpl_vars;
  497. } elseif ($scope == Smarty::SCOPE_GLOBAL) {
  498. return Smarty::$global_tpl_vars;
  499. }
  500. $null = null;
  501. return $null;
  502. }
  503. /**
  504. * Get parent or root of template parent chain
  505. *
  506. * @param int $scope pqrent or root scope
  507. * @return mixed object
  508. */
  509. public function getScopePointer($scope)
  510. {
  511. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  512. return $this->parent;
  513. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  514. $ptr = $this->parent;
  515. while (!empty($ptr->parent)) {
  516. $ptr = $ptr->parent;
  517. }
  518. return $ptr;
  519. }
  520. return null;
  521. }
  522. /**
  523. * [util function] counts an array, arrayaccess/traversable or PDOStatement object
  524. *
  525. * @param mixed $value
  526. * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 for empty elements
  527. */
  528. public function _count($value)
  529. {
  530. if (is_array($value) === true || $value instanceof Countable) {
  531. return count($value);
  532. } elseif ($value instanceof IteratorAggregate) {
  533. // Note: getIterator() returns a Traversable, not an Iterator
  534. // thus rewind() and valid() methods may not be present
  535. return iterator_count($value->getIterator());
  536. } elseif ($value instanceof Iterator) {
  537. return iterator_count($value);
  538. } elseif ($value instanceof PDOStatement) {
  539. return $value->rowCount();
  540. } elseif ($value instanceof Traversable) {
  541. return iterator_count($value);
  542. } elseif ($value instanceof ArrayAccess) {
  543. if ($value->offsetExists(0)) {
  544. return 1;
  545. }
  546. } elseif (is_object($value)) {
  547. return count($value);
  548. }
  549. return 0;
  550. }
  551. /**
  552. * runtime error not matching capture tags
  553. *
  554. */
  555. public function capture_error()
  556. {
  557. throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
  558. }
  559. /**
  560. * Empty cache for this template
  561. *
  562. * @param integer $exp_time expiration time
  563. * @return integer number of cache files deleted
  564. */
  565. public function clearCache($exp_time=null)
  566. {
  567. Smarty_CacheResource::invalidLoadedCache($this->smarty);
  568. return $this->cached->handler->clear($this->smarty, $this->template_name, $this->cache_id, $this->compile_id, $exp_time);
  569. }
  570. /**
  571. * set Smarty property in template context
  572. *
  573. * @param string $property_name property name
  574. * @param mixed $value value
  575. */
  576. public function __set($property_name, $value)
  577. {
  578. switch ($property_name) {
  579. case 'source':
  580. case 'compiled':
  581. case 'cached':
  582. case 'compiler':
  583. $this->$property_name = $value;
  584. return;
  585. // FIXME: routing of template -> smarty attributes
  586. default:
  587. if (property_exists($this->smarty, $property_name)) {
  588. $this->smarty->$property_name = $value;
  589. return;
  590. }
  591. }
  592. throw new SmartyException("invalid template property '$property_name'.");
  593. }
  594. /**
  595. * get Smarty property in template context
  596. *
  597. * @param string $property_name property name
  598. */
  599. public function __get($property_name)
  600. {
  601. switch ($property_name) {
  602. case 'source':
  603. if (strlen($this->template_resource) == 0) {
  604. throw new SmartyException('Missing template name');
  605. }
  606. $this->source = Smarty_Resource::source($this);
  607. // cache template object under a unique ID
  608. // do not cache eval resources
  609. if ($this->source->type != 'eval') {
  610. if ($this->smarty->allow_ambiguous_resources) {
  611. $_templateId = $this->source->unique_resource . $this->cache_id . $this->compile_id;
  612. } else {
  613. $_templateId = $this->smarty->joined_template_dir . '#' . $this->template_resource . $this->cache_id . $this->compile_id;
  614. }
  615. if (isset($_templateId[150])) {
  616. $_templateId = sha1($_templateId);
  617. }
  618. $this->smarty->template_objects[$_templateId] = $this;
  619. }
  620. return $this->source;
  621. case 'compiled':
  622. $this->compiled = $this->source->getCompiled($this);
  623. return $this->compiled;
  624. case 'cached':
  625. if (!class_exists('Smarty_Template_Cached')) {
  626. include SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  627. }
  628. $this->cached = new Smarty_Template_Cached($this);
  629. return $this->cached;
  630. case 'compiler':
  631. $this->smarty->loadPlugin($this->source->compiler_class);
  632. $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty);
  633. return $this->compiler;
  634. // FIXME: routing of template -> smarty attributes
  635. default:
  636. if (property_exists($this->smarty, $property_name)) {
  637. return $this->smarty->$property_name;
  638. }
  639. }
  640. throw new SmartyException("template property '$property_name' does not exist.");
  641. }
  642. /**
  643. * Template data object destrutor
  644. *
  645. */
  646. public function __destruct()
  647. {
  648. if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
  649. $this->cached->handler->releaseLock($this->smarty, $this->cached);
  650. }
  651. }
  652. }
  653. ?>