smarty_internal_template.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Template
  4. * This file contains the Smarty template engine
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Main class with template data structures and methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. *
  16. * @property Smarty_Template_Compiled $compiled
  17. * @property Smarty_Template_Cached $cached
  18. * @property Smarty_Internal_TemplateCompilerBase $compiler
  19. *
  20. * The following methods will be dynamically loaded by the extension handler when they are called.
  21. * They are located in a corresponding Smarty_Internal_Method_xxxx class
  22. *
  23. * @method bool mustCompile()
  24. */
  25. class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
  26. {
  27. /**
  28. * This object type (Smarty = 1, template = 2, data = 4)
  29. *
  30. * @var int
  31. */
  32. public $_objType = 2;
  33. /**
  34. * Global smarty instance
  35. *
  36. * @var Smarty
  37. */
  38. public $smarty = null;
  39. /**
  40. * Source instance
  41. *
  42. * @var Smarty_Template_Source|Smarty_Template_Config
  43. */
  44. public $source = null;
  45. /**
  46. * Inheritance runtime extension
  47. *
  48. * @var Smarty_Internal_Runtime_Inheritance
  49. */
  50. public $inheritance = null;
  51. /**
  52. * Template resource
  53. *
  54. * @var string
  55. */
  56. public $template_resource = null;
  57. /**
  58. * flag if compiled template is invalid and must be (re)compiled
  59. *
  60. * @var bool
  61. */
  62. public $mustCompile = null;
  63. /**
  64. * Template Id
  65. *
  66. * @var null|string
  67. */
  68. public $templateId = null;
  69. /**
  70. * Scope in which variables shall be assigned
  71. *
  72. * @var int
  73. */
  74. public $scope = 0;
  75. /**
  76. * Flag which is set while rending a cache file
  77. *
  78. * @var bool
  79. */
  80. public $isRenderingCache = false;
  81. /**
  82. * Callbacks called before rendering template
  83. *
  84. * @var callback[]
  85. */
  86. public $startRenderCallbacks = array();
  87. /**
  88. * Callbacks called after rendering template
  89. *
  90. * @var callback[]
  91. */
  92. public $endRenderCallbacks = array();
  93. /**
  94. * Create template data object
  95. * Some of the global Smarty settings copied to template scope
  96. * It load the required template resources and caching plugins
  97. *
  98. * @param string $template_resource template resource string
  99. * @param Smarty $smarty Smarty instance
  100. * @param \Smarty_Internal_Template|\Smarty|\Smarty_Internal_Data $_parent back pointer to parent object
  101. * with variables or null
  102. * @param mixed $_cache_id cache id or null
  103. * @param mixed $_compile_id compile id or null
  104. * @param bool $_caching use caching?
  105. * @param int $_cache_lifetime cache life-time in seconds
  106. *
  107. * @throws \SmartyException
  108. */
  109. public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null,
  110. $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
  111. {
  112. $this->smarty = $smarty;
  113. // Smarty parameter
  114. $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
  115. $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
  116. $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
  117. if ($this->caching === true) {
  118. $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  119. }
  120. $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
  121. $this->parent = $_parent;
  122. // Template resource
  123. $this->template_resource = $template_resource;
  124. $this->source = Smarty_Template_Source::load($this);
  125. parent::__construct();
  126. if ($smarty->security_policy && method_exists($smarty->security_policy, 'registerCallBacks')) {
  127. $smarty->security_policy->registerCallBacks($this);
  128. }
  129. }
  130. /**
  131. * render template
  132. *
  133. * @param bool $no_output_filter if true do not run output filter
  134. * @param null|bool $display true: display, false: fetch null: sub-template
  135. *
  136. * @return string
  137. * @throws \SmartyException
  138. */
  139. public function render($no_output_filter = true, $display = null)
  140. {
  141. $parentIsTpl = isset($this->parent) && $this->parent->_objType == 2;
  142. if ($this->smarty->debugging) {
  143. if (!isset($this->smarty->_debug)) {
  144. $this->smarty->_debug = new Smarty_Internal_Debug();
  145. }
  146. $this->smarty->_debug->start_template($this, $display);
  147. }
  148. // checks if template exists
  149. if (!$this->source->exists) {
  150. throw new SmartyException("Unable to load template '{$this->source->type}:{$this->source->name}'" .
  151. ($parentIsTpl ? " in '{$this->parent->template_resource}'" : ''));
  152. }
  153. // disable caching for evaluated code
  154. if ($this->source->handler->recompiled) {
  155. $this->caching = false;
  156. }
  157. // read from cache or render
  158. $isCacheTpl =
  159. $this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED;
  160. if ($isCacheTpl) {
  161. if (!isset($this->cached) || $this->cached->cache_id !== $this->cache_id ||
  162. $this->cached->compile_id !== $this->compile_id
  163. ) {
  164. $this->loadCached(true);
  165. }
  166. $this->cached->render($this, $no_output_filter);
  167. } else {
  168. if (!isset($this->compiled) || $this->compiled->compile_id !== $this->compile_id) {
  169. $this->loadCompiled(true);
  170. }
  171. $this->compiled->render($this);
  172. }
  173. // display or fetch
  174. if ($display) {
  175. if ($this->caching && $this->smarty->cache_modified_check) {
  176. $this->smarty->ext->_cacheModify->cacheModifiedCheck($this->cached, $this,
  177. isset($content) ? $content : ob_get_clean());
  178. } else {
  179. if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) &&
  180. !$no_output_filter && (isset($this->smarty->autoload_filters[ 'output' ]) ||
  181. isset($this->smarty->registered_filters[ 'output' ]))
  182. ) {
  183. echo $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
  184. } else {
  185. echo ob_get_clean();
  186. }
  187. }
  188. if ($this->smarty->debugging) {
  189. $this->smarty->_debug->end_template($this);
  190. // debug output
  191. $this->smarty->_debug->display_debug($this, true);
  192. }
  193. return '';
  194. } else {
  195. if ($this->smarty->debugging) {
  196. $this->smarty->_debug->end_template($this);
  197. if ($this->smarty->debugging === 2 && $display === false) {
  198. $this->smarty->_debug->display_debug($this, true);
  199. }
  200. }
  201. if ($parentIsTpl) {
  202. foreach ($this->compiled->required_plugins as $code => $tmp1) {
  203. foreach ($tmp1 as $name => $tmp) {
  204. foreach ($tmp as $type => $data) {
  205. $this->parent->compiled->required_plugins[ $code ][ $name ][ $type ] = $data;
  206. }
  207. }
  208. }
  209. }
  210. if (!$no_output_filter &&
  211. (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) &&
  212. (isset($this->smarty->autoload_filters[ 'output' ]) ||
  213. isset($this->smarty->registered_filters[ 'output' ]))
  214. ) {
  215. return $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
  216. }
  217. // return cache content
  218. return null;
  219. }
  220. }
  221. /**
  222. * Runtime function to render sub-template
  223. *
  224. * @param string $template template name
  225. * @param mixed $cache_id cache id
  226. * @param mixed $compile_id compile id
  227. * @param integer $caching cache mode
  228. * @param integer $cache_lifetime life time of cache data
  229. * @param array $data passed parameter template variables
  230. * @param int $scope scope in which {include} should execute
  231. * @param bool $forceTplCache cache template object
  232. * @param string $uid file dependency uid
  233. * @param string $content_func function name
  234. *
  235. */
  236. public function _subTemplateRender($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $scope,
  237. $forceTplCache, $uid = null, $content_func = null)
  238. {
  239. $tpl = clone $this;
  240. $tpl->parent = $this;
  241. $smarty = &$this->smarty;
  242. $_templateId = $smarty->_getTemplateId($template, $cache_id, $compile_id, $caching, $tpl);
  243. // recursive call ?
  244. if (isset($tpl->templateId) ? $tpl->templateId : $tpl->_getTemplateId() != $_templateId) {
  245. // already in template cache?
  246. if (isset($smarty->_cache[ 'tplObjects' ][ $_templateId ])) {
  247. // copy data from cached object
  248. $cachedTpl = &$smarty->_cache[ 'tplObjects' ][ $_templateId ];
  249. $tpl->templateId = $cachedTpl->templateId;
  250. $tpl->template_resource = $cachedTpl->template_resource;
  251. $tpl->cache_id = $cachedTpl->cache_id;
  252. $tpl->compile_id = $cachedTpl->compile_id;
  253. $tpl->source = $cachedTpl->source;
  254. if (isset($cachedTpl->compiled)) {
  255. $tpl->compiled = $cachedTpl->compiled;
  256. } else {
  257. unset($tpl->compiled);
  258. }
  259. if ($caching != 9999 && isset($cachedTpl->cached)) {
  260. $tpl->cached = $cachedTpl->cached;
  261. } else {
  262. unset($tpl->cached);
  263. }
  264. } else {
  265. $tpl->templateId = $_templateId;
  266. $tpl->template_resource = $template;
  267. $tpl->cache_id = $cache_id;
  268. $tpl->compile_id = $compile_id;
  269. if (isset($uid)) {
  270. // for inline templates we can get all resource information from file dependency
  271. list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ];
  272. $tpl->source = new Smarty_Template_Source($smarty, $filepath, $type, $filepath);
  273. $tpl->source->filepath = $filepath;
  274. $tpl->source->timestamp = $timestamp;
  275. $tpl->source->exists = true;
  276. $tpl->source->uid = $uid;
  277. } else {
  278. $tpl->source = Smarty_Template_Source::load($tpl);
  279. unset($tpl->compiled);
  280. }
  281. if ($caching != 9999) {
  282. unset($tpl->cached);
  283. }
  284. }
  285. } else {
  286. // on recursive calls force caching
  287. $forceTplCache = true;
  288. }
  289. $tpl->caching = $caching;
  290. $tpl->cache_lifetime = $cache_lifetime;
  291. // set template scope
  292. $tpl->scope = $scope;
  293. if (!isset($smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
  294. // check if template object should be cached
  295. if ($forceTplCache || (isset($smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
  296. $smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) ||
  297. ($tpl->_isParentTemplate() && isset($smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]))
  298. ) {
  299. $smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl;
  300. }
  301. }
  302. if (!empty($data)) {
  303. // set up variable values
  304. foreach ($data as $_key => $_val) {
  305. $tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val, $this->isRenderingCache);
  306. }
  307. }
  308. if ($tpl->caching == 9999) {
  309. if (!isset($tpl->compiled)) {
  310. $this->loadCompiled(true);
  311. }
  312. if ($tpl->compiled->has_nocache_code) {
  313. $this->cached->hashes[ $tpl->compiled->nocache_hash ] = true;
  314. }
  315. }
  316. $tpl->_cache = array();
  317. if (isset($uid)) {
  318. if ($smarty->debugging) {
  319. if (!isset($smarty->_debug)) {
  320. $smarty->_debug = new Smarty_Internal_Debug();
  321. }
  322. $smarty->_debug->start_template($tpl);
  323. $smarty->_debug->start_render($tpl);
  324. }
  325. $tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
  326. if ($smarty->debugging) {
  327. $smarty->_debug->end_template($tpl);
  328. $smarty->_debug->end_render($tpl);
  329. }
  330. } else {
  331. if (isset($tpl->compiled)) {
  332. $tpl->compiled->render($tpl);
  333. } else {
  334. $tpl->render();
  335. }
  336. }
  337. }
  338. /**
  339. * Get called sub-templates and save call count
  340. *
  341. */
  342. public function _subTemplateRegister()
  343. {
  344. foreach ($this->compiled->includes as $name => $count) {
  345. if (isset($this->smarty->_cache[ 'subTplInfo' ][ $name ])) {
  346. $this->smarty->_cache[ 'subTplInfo' ][ $name ] += $count;
  347. } else {
  348. $this->smarty->_cache[ 'subTplInfo' ][ $name ] = $count;
  349. }
  350. }
  351. }
  352. /**
  353. * Check if parent is template object
  354. *
  355. * @return bool true if parent is template
  356. */
  357. public function _isParentTemplate()
  358. {
  359. return isset($this->parent) && $this->parent->_objType == 2;
  360. }
  361. /**
  362. * Assign variable in scope
  363. *
  364. * @param string $varName variable name
  365. * @param mixed $value value
  366. * @param bool $nocache nocache flag
  367. * @param int $scope scope into which variable shall be assigned
  368. *
  369. */
  370. public function _assignInScope($varName, $value, $nocache = false, $scope = 0)
  371. {
  372. if (isset($this->tpl_vars[ $varName ])) {
  373. $this->tpl_vars[ $varName ] = clone $this->tpl_vars[ $varName ];
  374. $this->tpl_vars[ $varName ]->value = $value;
  375. if ($nocache || $this->isRenderingCache) {
  376. $this->tpl_vars[ $varName ]->nocache = true;
  377. }
  378. } else {
  379. $this->tpl_vars[ $varName ] = new Smarty_Variable($value, $nocache || $this->isRenderingCache);
  380. }
  381. if ($scope >= 0) {
  382. if (isset($scope) || isset($this->scope)) {
  383. $this->smarty->ext->_updateScope->_updateScope($this, $varName, $scope);
  384. }
  385. }
  386. }
  387. /**
  388. * This function is executed automatically when a compiled or cached template file is included
  389. * - Decode saved properties from compiled template and cache files
  390. * - Check if compiled or cache file is valid
  391. *
  392. * @param \Smarty_Internal_Template $tpl
  393. * @param array $properties special template properties
  394. * @param bool $cache flag if called from cache file
  395. *
  396. * @return bool flag if compiled or cache file is valid
  397. * @throws \SmartyException
  398. */
  399. public function _decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false)
  400. {
  401. $is_valid = true;
  402. if (Smarty::SMARTY_VERSION != $properties[ 'version' ]) {
  403. // new version must rebuild
  404. $is_valid = false;
  405. } elseif ($is_valid && !empty($properties[ 'file_dependency' ]) &&
  406. ((!$cache && $tpl->smarty->compile_check) || $tpl->smarty->compile_check == 1)
  407. ) {
  408. // check file dependencies at compiled code
  409. foreach ($properties[ 'file_dependency' ] as $_file_to_check) {
  410. if ($_file_to_check[ 2 ] == 'file' || $_file_to_check[ 2 ] == 'php') {
  411. if ($tpl->source->filepath == $_file_to_check[ 0 ]) {
  412. // do not recheck current template
  413. continue;
  414. //$mtime = $tpl->source->getTimeStamp();
  415. } else {
  416. // file and php types can be checked without loading the respective resource handlers
  417. $mtime = is_file($_file_to_check[ 0 ]) ? filemtime($_file_to_check[ 0 ]) : false;
  418. }
  419. } else {
  420. $handler = Smarty_Resource::load($tpl->smarty, $_file_to_check[ 2 ]);
  421. if ($handler->checkTimestamps()) {
  422. $source = Smarty_Template_Source::load($tpl, $tpl->smarty, $_file_to_check[ 0 ]);
  423. $mtime = $source->getTimeStamp();
  424. } else {
  425. continue;
  426. }
  427. }
  428. if ($mtime === false || $mtime > $_file_to_check[ 1 ]) {
  429. $is_valid = false;
  430. break;
  431. }
  432. }
  433. }
  434. if ($cache) {
  435. // CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
  436. if ($tpl->caching === Smarty::CACHING_LIFETIME_SAVED && $properties[ 'cache_lifetime' ] >= 0 &&
  437. (time() > ($tpl->cached->timestamp + $properties[ 'cache_lifetime' ]))
  438. ) {
  439. $is_valid = false;
  440. }
  441. $tpl->cached->cache_lifetime = $properties[ 'cache_lifetime' ];
  442. $tpl->cached->valid = $is_valid;
  443. $resource = $tpl->cached;
  444. } else {
  445. $tpl->mustCompile = !$is_valid;
  446. $resource = $tpl->compiled;
  447. $resource->includes = isset($properties[ 'includes' ]) ? $properties[ 'includes' ] : array();
  448. }
  449. if ($is_valid) {
  450. $resource->unifunc = $properties[ 'unifunc' ];
  451. $resource->has_nocache_code = $properties[ 'has_nocache_code' ];
  452. // $tpl->compiled->nocache_hash = $properties['nocache_hash'];
  453. $resource->file_dependency = $properties[ 'file_dependency' ];
  454. }
  455. return $is_valid && !function_exists($properties[ 'unifunc' ]);
  456. }
  457. /**
  458. * Compiles the template
  459. * If the template is not evaluated the compiled template is saved on disk
  460. */
  461. public function compileTemplateSource()
  462. {
  463. return $this->compiled->compileTemplateSource($this);
  464. }
  465. /**
  466. * Writes the content to cache resource
  467. *
  468. * @param string $content
  469. *
  470. * @return bool
  471. */
  472. public function writeCachedContent($content)
  473. {
  474. return $this->smarty->ext->_updateCache->writeCachedContent($this->cached, $this, $content);
  475. }
  476. /**
  477. * Get unique template id
  478. *
  479. * @return string
  480. */
  481. public function _getTemplateId()
  482. {
  483. return isset($this->templateId) ? $this->templateId : $this->templateId =
  484. $this->smarty->_getTemplateId($this->template_resource, $this->cache_id, $this->compile_id);
  485. }
  486. /**
  487. * runtime error not matching capture tags
  488. */
  489. public function capture_error()
  490. {
  491. throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
  492. }
  493. /**
  494. * Load compiled object
  495. *
  496. * @param bool $force force new compiled object
  497. */
  498. public function loadCompiled($force = false)
  499. {
  500. if ($force || !isset($this->compiled)) {
  501. $this->compiled = Smarty_Template_Compiled::load($this);
  502. }
  503. }
  504. /**
  505. * Load cached object
  506. *
  507. * @param bool $force force new cached object
  508. */
  509. public function loadCached($force = false)
  510. {
  511. if ($force || !isset($this->cached)) {
  512. $this->cached = Smarty_Template_Cached::load($this);
  513. }
  514. }
  515. /**
  516. * Load inheritance object
  517. *
  518. */
  519. public function _loadInheritance()
  520. {
  521. if (!isset($this->inheritance)) {
  522. $this->inheritance = new Smarty_Internal_Runtime_Inheritance();
  523. }
  524. }
  525. /**
  526. * Unload inheritance object
  527. *
  528. */
  529. public function _cleanUp()
  530. {
  531. $this->startRenderCallbacks = array();
  532. $this->endRenderCallbacks = array();
  533. $this->inheritance = null;
  534. }
  535. /**
  536. * Load compiler object
  537. *
  538. * @throws \SmartyException
  539. */
  540. public function loadCompiler()
  541. {
  542. if (!class_exists($this->source->compiler_class)) {
  543. $this->smarty->loadPlugin($this->source->compiler_class);
  544. }
  545. $this->compiler =
  546. new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class,
  547. $this->smarty);
  548. }
  549. /**
  550. * Handle unknown class methods
  551. *
  552. * @param string $name unknown method-name
  553. * @param array $args argument array
  554. *
  555. * @return mixed
  556. * @throws SmartyException
  557. */
  558. public function __call($name, $args)
  559. {
  560. // method of Smarty object?
  561. if (method_exists($this->smarty, $name)) {
  562. return call_user_func_array(array($this->smarty, $name), $args);
  563. }
  564. // parent
  565. return parent::__call($name, $args);
  566. }
  567. /**
  568. * set Smarty property in template context
  569. *
  570. * @param string $property_name property name
  571. * @param mixed $value value
  572. *
  573. * @throws SmartyException
  574. */
  575. public function __set($property_name, $value)
  576. {
  577. switch ($property_name) {
  578. case 'compiled':
  579. case 'cached':
  580. case 'compiler':
  581. $this->$property_name = $value;
  582. return;
  583. default:
  584. // Smarty property ?
  585. if (property_exists($this->smarty, $property_name)) {
  586. $this->smarty->$property_name = $value;
  587. return;
  588. }
  589. }
  590. throw new SmartyException("invalid template property '$property_name'.");
  591. }
  592. /**
  593. * get Smarty property in template context
  594. *
  595. * @param string $property_name property name
  596. *
  597. * @return mixed|Smarty_Template_Cached
  598. * @throws SmartyException
  599. */
  600. public function __get($property_name)
  601. {
  602. switch ($property_name) {
  603. case 'compiled':
  604. $this->loadCompiled();
  605. return $this->compiled;
  606. case 'cached':
  607. $this->loadCached();
  608. return $this->cached;
  609. case 'compiler':
  610. $this->loadCompiler();
  611. return $this->compiler;
  612. default:
  613. // Smarty property ?
  614. if (property_exists($this->smarty, $property_name)) {
  615. return $this->smarty->$property_name;
  616. }
  617. }
  618. throw new SmartyException("template property '$property_name' does not exist.");
  619. }
  620. /**
  621. * Template data object destructor
  622. */
  623. public function __destruct()
  624. {
  625. if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
  626. $this->cached->handler->releaseLock($this->smarty, $this->cached);
  627. }
  628. }
  629. }