smarty_internal_templatebase.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Base
  4. *
  5. * This file contains the basic shared methodes for template handling
  6. *
  7. * @package Smarty
  8. * @subpackage Template
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Class with shared template methodes
  13. *
  14. * @package Smarty
  15. * @subpackage Template
  16. */
  17. abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
  18. {
  19. /**
  20. * fetches a rendered Smarty template
  21. *
  22. * @param string $template the resource handle of the template file or template object
  23. * @param mixed $cache_id cache id to be used with this template
  24. * @param mixed $compile_id compile id to be used with this template
  25. * @param object $parent next higher level of Smarty variables
  26. * @param bool $display true: display, false: fetch
  27. * @param bool $merge_tpl_vars if true parent template variables merged in to local scope
  28. * @param bool $no_output_filter if true do not run output filter
  29. * @return string rendered template output
  30. */
  31. public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
  32. {
  33. if ($template === null && $this instanceof $this->template_class) {
  34. $template = $this;
  35. }
  36. if (!empty($cache_id) && is_object($cache_id)) {
  37. $parent = $cache_id;
  38. $cache_id = null;
  39. }
  40. if ($parent === null && ($this instanceof Smarty || is_string($template))) {
  41. $parent = $this;
  42. }
  43. // create template object if necessary
  44. $_template = ($template instanceof $this->template_class)
  45. ? $template
  46. : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
  47. // if called by Smarty object make sure we use current caching status
  48. if ($this instanceof Smarty) {
  49. $_template->caching = $this->caching;
  50. }
  51. // merge all variable scopes into template
  52. if ($merge_tpl_vars) {
  53. // save local variables
  54. $save_tpl_vars = $_template->tpl_vars;
  55. $save_config_vars = $_template->config_vars;
  56. $ptr_array = array($_template);
  57. $ptr = $_template;
  58. while (isset($ptr->parent)) {
  59. $ptr_array[] = $ptr = $ptr->parent;
  60. }
  61. $ptr_array = array_reverse($ptr_array);
  62. $parent_ptr = reset($ptr_array);
  63. $tpl_vars = $parent_ptr->tpl_vars;
  64. $config_vars = $parent_ptr->config_vars;
  65. while ($parent_ptr = next($ptr_array)) {
  66. if (!empty($parent_ptr->tpl_vars)) {
  67. $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars);
  68. }
  69. if (!empty($parent_ptr->config_vars)) {
  70. $config_vars = array_merge($config_vars, $parent_ptr->config_vars);
  71. }
  72. }
  73. if (!empty(Smarty::$global_tpl_vars)) {
  74. $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars);
  75. }
  76. $_template->tpl_vars = $tpl_vars;
  77. $_template->config_vars = $config_vars;
  78. }
  79. // dummy local smarty variable
  80. if (!isset($_template->tpl_vars['smarty'])) {
  81. $_template->tpl_vars['smarty'] = new Smarty_Variable;
  82. }
  83. if (isset($this->smarty->error_reporting)) {
  84. $_smarty_old_error_level = error_reporting($this->smarty->error_reporting);
  85. }
  86. // check URL debugging control
  87. if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') {
  88. if (isset($_SERVER['QUERY_STRING'])) {
  89. $_query_string = $_SERVER['QUERY_STRING'];
  90. } else {
  91. $_query_string = '';
  92. }
  93. if (false !== strpos($_query_string, $this->smarty->smarty_debug_id)) {
  94. if (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=on')) {
  95. // enable debugging for this browser session
  96. setcookie('SMARTY_DEBUG', true);
  97. $this->smarty->debugging = true;
  98. } elseif (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=off')) {
  99. // disable debugging for this browser session
  100. setcookie('SMARTY_DEBUG', false);
  101. $this->smarty->debugging = false;
  102. } else {
  103. // enable debugging for this page
  104. $this->smarty->debugging = true;
  105. }
  106. } else {
  107. if (isset($_COOKIE['SMARTY_DEBUG'])) {
  108. $this->smarty->debugging = true;
  109. }
  110. }
  111. }
  112. // must reset merge template date
  113. $_template->smarty->merged_templates_func = array();
  114. // get rendered template
  115. // disable caching for evaluated code
  116. if ($_template->source->recompiled) {
  117. $_template->caching = false;
  118. }
  119. // checks if template exists
  120. if (!$_template->source->exists) {
  121. if ($_template->parent instanceof Smarty_Internal_Template) {
  122. $parent_resource = " in '{$_template->parent->template_resource}'";
  123. } else {
  124. $parent_resource = '';
  125. }
  126. throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}");
  127. }
  128. // read from cache or render
  129. if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) {
  130. // render template (not loaded and not in cache)
  131. if (!$_template->source->uncompiled) {
  132. $_smarty_tpl = $_template;
  133. if ($_template->source->recompiled) {
  134. $code = $_template->compiler->compileTemplate($_template);
  135. if ($this->smarty->debugging) {
  136. Smarty_Internal_Debug::start_render($_template);
  137. }
  138. try {
  139. ob_start();
  140. eval("?>" . $code);
  141. unset($code);
  142. } catch (Exception $e) {
  143. ob_get_clean();
  144. throw $e;
  145. }
  146. } else {
  147. if (!$_template->compiled->exists || ($_template->smarty->force_compile && !$_template->compiled->isCompiled)) {
  148. $_template->compileTemplateSource();
  149. $code = file_get_contents($_template->compiled->filepath);
  150. eval("?>" . $code);
  151. unset($code);
  152. $_template->compiled->loaded = true;
  153. $_template->compiled->isCompiled = true;
  154. }
  155. if ($this->smarty->debugging) {
  156. Smarty_Internal_Debug::start_render($_template);
  157. }
  158. if (!$_template->compiled->loaded) {
  159. include($_template->compiled->filepath);
  160. if ($_template->mustCompile) {
  161. // recompile and load again
  162. $_template->compileTemplateSource();
  163. $code = file_get_contents($_template->compiled->filepath);
  164. eval("?>" . $code);
  165. unset($code);
  166. $_template->compiled->isCompiled = true;
  167. }
  168. $_template->compiled->loaded = true;
  169. } else {
  170. $_template->decodeProperties($_template->compiled->_properties, false);
  171. }
  172. try {
  173. ob_start();
  174. if (empty($_template->properties['unifunc']) || !is_callable($_template->properties['unifunc'])) {
  175. throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
  176. }
  177. array_unshift($_template->_capture_stack,array());
  178. //
  179. // render compiled template
  180. //
  181. $_template->properties['unifunc']($_template);
  182. // any unclosed {capture} tags ?
  183. if (isset($_template->_capture_stack[0][0])) {
  184. $_template->capture_error();
  185. }
  186. array_shift($_template->_capture_stack);
  187. } catch (Exception $e) {
  188. ob_get_clean();
  189. throw $e;
  190. }
  191. }
  192. } else {
  193. if ($_template->source->uncompiled) {
  194. if ($this->smarty->debugging) {
  195. Smarty_Internal_Debug::start_render($_template);
  196. }
  197. try {
  198. ob_start();
  199. $_template->source->renderUncompiled($_template);
  200. } catch (Exception $e) {
  201. ob_get_clean();
  202. throw $e;
  203. }
  204. } else {
  205. throw new SmartyException("Resource '$_template->source->type' must have 'renderUncompiled' method");
  206. }
  207. }
  208. $_output = ob_get_clean();
  209. if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) {
  210. $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
  211. }
  212. if ($_template->parent instanceof Smarty_Internal_Template) {
  213. $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']);
  214. foreach ($_template->required_plugins as $code => $tmp1) {
  215. foreach ($tmp1 as $name => $tmp) {
  216. foreach ($tmp as $type => $data) {
  217. $_template->parent->required_plugins[$code][$name][$type] = $data;
  218. }
  219. }
  220. }
  221. }
  222. if ($this->smarty->debugging) {
  223. Smarty_Internal_Debug::end_render($_template);
  224. }
  225. // write to cache when nessecary
  226. if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) {
  227. if ($this->smarty->debugging) {
  228. Smarty_Internal_Debug::start_cache($_template);
  229. }
  230. $_template->properties['has_nocache_code'] = false;
  231. // get text between non-cached items
  232. $cache_split = preg_split("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output);
  233. // get non-cached items
  234. preg_match_all("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output, $cache_parts);
  235. $output = '';
  236. // loop over items, stitch back together
  237. foreach ($cache_split as $curr_idx => $curr_split) {
  238. // escape PHP tags in template content
  239. $output .= preg_replace('/(<%|%>|<\?php|<\?|\?>)/', "<?php echo '\$1'; ?>\n", $curr_split);
  240. if (isset($cache_parts[0][$curr_idx])) {
  241. $_template->properties['has_nocache_code'] = true;
  242. // remove nocache tags from cache output
  243. $output .= preg_replace("!/\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!", '', $cache_parts[0][$curr_idx]);
  244. }
  245. }
  246. if (!$no_output_filter && !$_template->has_nocache_code && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
  247. $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template);
  248. }
  249. // rendering (must be done before writing cache file because of {function} nocache handling)
  250. $_smarty_tpl = $_template;
  251. try {
  252. ob_start();
  253. eval("?>" . $output);
  254. $_output = ob_get_clean();
  255. } catch (Exception $e) {
  256. ob_get_clean();
  257. throw $e;
  258. }
  259. // write cache file content
  260. $_template->writeCachedContent($output);
  261. if ($this->smarty->debugging) {
  262. Smarty_Internal_Debug::end_cache($_template);
  263. }
  264. } else {
  265. // var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output);
  266. if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) {
  267. // replace nocache_hash
  268. $_output = str_replace("{$_template->properties['nocache_hash']}", $_template->parent->properties['nocache_hash'], $_output);
  269. $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code;
  270. }
  271. }
  272. } else {
  273. if ($this->smarty->debugging) {
  274. Smarty_Internal_Debug::start_cache($_template);
  275. }
  276. try {
  277. ob_start();
  278. array_unshift($_template->_capture_stack,array());
  279. //
  280. // render cached template
  281. //
  282. $_template->properties['unifunc']($_template);
  283. // any unclosed {capture} tags ?
  284. if (isset($_template->_capture_stack[0][0])) {
  285. $_template->capture_error();
  286. }
  287. array_shift($_template->_capture_stack);
  288. $_output = ob_get_clean();
  289. } catch (Exception $e) {
  290. ob_get_clean();
  291. throw $e;
  292. }
  293. if ($this->smarty->debugging) {
  294. Smarty_Internal_Debug::end_cache($_template);
  295. }
  296. }
  297. if ((!$this->caching || $_template->has_nocache_code || $_template->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
  298. $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template);
  299. }
  300. if (isset($this->error_reporting)) {
  301. error_reporting($_smarty_old_error_level);
  302. }
  303. // display or fetch
  304. if ($display) {
  305. if ($this->caching && $this->cache_modified_check) {
  306. $_isCached = $_template->isCached() && !$_template->has_nocache_code;
  307. $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  308. if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) {
  309. switch (PHP_SAPI) {
  310. case 'cgi': // php-cgi < 5.3
  311. case 'cgi-fcgi': // php-cgi >= 5.3
  312. case 'fpm-fcgi': // php-fpm >= 5.3.3
  313. header('Status: 304 Not Modified');
  314. break;
  315. case 'cli':
  316. if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
  317. $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
  318. }
  319. break;
  320. default:
  321. header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
  322. break;
  323. }
  324. } else {
  325. switch (PHP_SAPI) {
  326. case 'cli':
  327. if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
  328. $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT';
  329. }
  330. break;
  331. default:
  332. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT');
  333. break;
  334. }
  335. echo $_output;
  336. }
  337. } else {
  338. echo $_output;
  339. }
  340. // debug output
  341. if ($this->smarty->debugging) {
  342. Smarty_Internal_Debug::display_debug($this);
  343. }
  344. if ($merge_tpl_vars) {
  345. // restore local variables
  346. $_template->tpl_vars = $save_tpl_vars;
  347. $_template->config_vars = $save_config_vars;
  348. }
  349. return;
  350. } else {
  351. if ($merge_tpl_vars) {
  352. // restore local variables
  353. $_template->tpl_vars = $save_tpl_vars;
  354. $_template->config_vars = $save_config_vars;
  355. }
  356. // return fetched content
  357. return $_output;
  358. }
  359. }
  360. /**
  361. * displays a Smarty template
  362. *
  363. * @param string $template the resource handle of the template file or template object
  364. * @param mixed $cache_id cache id to be used with this template
  365. * @param mixed $compile_id compile id to be used with this template
  366. * @param object $parent next higher level of Smarty variables
  367. */
  368. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  369. {
  370. // display template
  371. $this->fetch($template, $cache_id, $compile_id, $parent, true);
  372. }
  373. /**
  374. * test if cache is valid
  375. *
  376. * @param string|object $template the resource handle of the template file or template object
  377. * @param mixed $cache_id cache id to be used with this template
  378. * @param mixed $compile_id compile id to be used with this template
  379. * @param object $parent next higher level of Smarty variables
  380. * @return boolean cache status
  381. */
  382. public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
  383. {
  384. if ($template === null && $this instanceof $this->template_class) {
  385. return $this->cached->valid;
  386. }
  387. if (!($template instanceof $this->template_class)) {
  388. if ($parent === null) {
  389. $parent = $this;
  390. }
  391. $template = $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
  392. }
  393. // return cache status of template
  394. return $template->cached->valid;
  395. }
  396. /**
  397. * creates a data object
  398. *
  399. * @param object $parent next higher level of Smarty variables
  400. * @returns Smarty_Data data object
  401. */
  402. public function createData($parent = null)
  403. {
  404. return new Smarty_Data($parent, $this);
  405. }
  406. /**
  407. * Registers plugin to be used in templates
  408. *
  409. * @param string $type plugin type
  410. * @param string $tag name of template tag
  411. * @param callback $callback PHP callback to register
  412. * @param boolean $cacheable if true (default) this fuction is cachable
  413. * @param array $cache_attr caching attributes if any
  414. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  415. * @throws SmartyException when the plugin tag is invalid
  416. */
  417. public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
  418. {
  419. if (isset($this->smarty->registered_plugins[$type][$tag])) {
  420. throw new SmartyException("Plugin tag \"{$tag}\" already registered");
  421. } elseif (!is_callable($callback)) {
  422. throw new SmartyException("Plugin \"{$tag}\" not callable");
  423. } else {
  424. $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
  425. }
  426. return $this;
  427. }
  428. /**
  429. * Unregister Plugin
  430. *
  431. * @param string $type of plugin
  432. * @param string $tag name of plugin
  433. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  434. */
  435. public function unregisterPlugin($type, $tag)
  436. {
  437. if (isset($this->smarty->registered_plugins[$type][$tag])) {
  438. unset($this->smarty->registered_plugins[$type][$tag]);
  439. }
  440. return $this;
  441. }
  442. /**
  443. * Registers a resource to fetch a template
  444. *
  445. * @param string $type name of resource type
  446. * @param Smarty_Resource|array $callback or instance of Smarty_Resource, or array of callbacks to handle resource (deprecated)
  447. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  448. */
  449. public function registerResource($type, $callback)
  450. {
  451. $this->smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false);
  452. return $this;
  453. }
  454. /**
  455. * Unregisters a resource
  456. *
  457. * @param string $type name of resource type
  458. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  459. */
  460. public function unregisterResource($type)
  461. {
  462. if (isset($this->smarty->registered_resources[$type])) {
  463. unset($this->smarty->registered_resources[$type]);
  464. }
  465. return $this;
  466. }
  467. /**
  468. * Registers a cache resource to cache a template's output
  469. *
  470. * @param string $type name of cache resource type
  471. * @param Smarty_CacheResource $callback instance of Smarty_CacheResource to handle output caching
  472. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  473. */
  474. public function registerCacheResource($type, Smarty_CacheResource $callback)
  475. {
  476. $this->smarty->registered_cache_resources[$type] = $callback;
  477. return $this;
  478. }
  479. /**
  480. * Unregisters a cache resource
  481. *
  482. * @param string $type name of cache resource type
  483. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  484. */
  485. public function unregisterCacheResource($type)
  486. {
  487. if (isset($this->smarty->registered_cache_resources[$type])) {
  488. unset($this->smarty->registered_cache_resources[$type]);
  489. }
  490. return $this;
  491. }
  492. /**
  493. * Registers object to be used in templates
  494. *
  495. * @param string $object name of template object
  496. * @param object $object_impl the referenced PHP object to register
  497. * @param array $allowed list of allowed methods (empty = all)
  498. * @param boolean $smarty_args smarty argument format, else traditional
  499. * @param array $block_methods list of block-methods
  500. * @param array $block_functs list of methods that are block format
  501. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  502. * @throws SmartyException if any of the methods in $allowed or $block_methods are invalid
  503. */
  504. public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  505. {
  506. // test if allowed methodes callable
  507. if (!empty($allowed)) {
  508. foreach ((array) $allowed as $method) {
  509. if (!is_callable(array($object_impl, $method)) && !property_exists($object_impl, $method)) {
  510. throw new SmartyException("Undefined method or property '$method' in registered object");
  511. }
  512. }
  513. }
  514. // test if block methodes callable
  515. if (!empty($block_methods)) {
  516. foreach ((array) $block_methods as $method) {
  517. if (!is_callable(array($object_impl, $method))) {
  518. throw new SmartyException("Undefined method '$method' in registered object");
  519. }
  520. }
  521. }
  522. // register the object
  523. $this->smarty->registered_objects[$object_name] =
  524. array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods);
  525. return $this;
  526. }
  527. /**
  528. * return a reference to a registered object
  529. *
  530. * @param string $name object name
  531. * @return object
  532. * @throws SmartyException if no such object is found
  533. */
  534. public function getRegisteredObject($name)
  535. {
  536. if (!isset($this->smarty->registered_objects[$name])) {
  537. throw new SmartyException("'$name' is not a registered object");
  538. }
  539. if (!is_object($this->smarty->registered_objects[$name][0])) {
  540. throw new SmartyException("registered '$name' is not an object");
  541. }
  542. return $this->smarty->registered_objects[$name][0];
  543. }
  544. /**
  545. * unregister an object
  546. *
  547. * @param string $name object name
  548. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  549. */
  550. public function unregisterObject($name)
  551. {
  552. if (isset($this->smarty->registered_objects[$name])) {
  553. unset($this->smarty->registered_objects[$name]);
  554. }
  555. return $this;
  556. }
  557. /**
  558. * Registers static classes to be used in templates
  559. *
  560. * @param string $class name of template class
  561. * @param string $class_impl the referenced PHP class to register
  562. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  563. * @throws SmartyException if $class_impl does not refer to an existing class
  564. */
  565. public function registerClass($class_name, $class_impl)
  566. {
  567. // test if exists
  568. if (!class_exists($class_impl)) {
  569. throw new SmartyException("Undefined class '$class_impl' in register template class");
  570. }
  571. // register the class
  572. $this->smarty->registered_classes[$class_name] = $class_impl;
  573. return $this;
  574. }
  575. /**
  576. * Registers a default plugin handler
  577. *
  578. * @param callable $callback class/method name
  579. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  580. * @throws SmartyException if $callback is not callable
  581. */
  582. public function registerDefaultPluginHandler($callback)
  583. {
  584. if (is_callable($callback)) {
  585. $this->smarty->default_plugin_handler_func = $callback;
  586. } else {
  587. throw new SmartyException("Default plugin handler '$callback' not callable");
  588. }
  589. return $this;
  590. }
  591. /**
  592. * Registers a default template handler
  593. *
  594. * @param callable $callback class/method name
  595. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  596. * @throws SmartyException if $callback is not callable
  597. */
  598. public function registerDefaultTemplateHandler($callback)
  599. {
  600. if (is_callable($callback)) {
  601. $this->smarty->default_template_handler_func = $callback;
  602. } else {
  603. throw new SmartyException("Default template handler '$callback' not callable");
  604. }
  605. return $this;
  606. }
  607. /**
  608. * Registers a default template handler
  609. *
  610. * @param callable $callback class/method name
  611. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  612. * @throws SmartyException if $callback is not callable
  613. */
  614. public function registerDefaultConfigHandler($callback)
  615. {
  616. if (is_callable($callback)) {
  617. $this->smarty->default_config_handler_func = $callback;
  618. } else {
  619. throw new SmartyException("Default config handler '$callback' not callable");
  620. }
  621. return $this;
  622. }
  623. /**
  624. * Registers a filter function
  625. *
  626. * @param string $type filter type
  627. * @param callback $callback
  628. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  629. */
  630. public function registerFilter($type, $callback)
  631. {
  632. $this->smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback;
  633. return $this;
  634. }
  635. /**
  636. * Unregisters a filter function
  637. *
  638. * @param string $type filter type
  639. * @param callback $callback
  640. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  641. */
  642. public function unregisterFilter($type, $callback)
  643. {
  644. $name = $this->_get_filter_name($callback);
  645. if (isset($this->smarty->registered_filters[$type][$name])) {
  646. unset($this->smarty->registered_filters[$type][$name]);
  647. }
  648. return $this;
  649. }
  650. /**
  651. * Return internal filter name
  652. *
  653. * @param callback $function_name
  654. * @return string internal filter name
  655. */
  656. public function _get_filter_name($function_name)
  657. {
  658. if (is_array($function_name)) {
  659. $_class_name = (is_object($function_name[0]) ?
  660. get_class($function_name[0]) : $function_name[0]);
  661. return $_class_name . '_' . $function_name[1];
  662. } else {
  663. return $function_name;
  664. }
  665. }
  666. /**
  667. * load a filter of specified type and name
  668. *
  669. * @param string $type filter type
  670. * @param string $name filter name
  671. * @throws SmartyException if filter could not be loaded
  672. */
  673. public function loadFilter($type, $name)
  674. {
  675. $_plugin = "smarty_{$type}filter_{$name}";
  676. $_filter_name = $_plugin;
  677. if ($this->smarty->loadPlugin($_plugin)) {
  678. if (class_exists($_plugin, false)) {
  679. $_plugin = array($_plugin, 'execute');
  680. }
  681. if (is_callable($_plugin)) {
  682. $this->smarty->registered_filters[$type][$_filter_name] = $_plugin;
  683. return true;
  684. }
  685. }
  686. throw new SmartyException("{$type}filter \"{$name}\" not callable");
  687. }
  688. /**
  689. * unload a filter of specified type and name
  690. *
  691. * @param string $type filter type
  692. * @param string $name filter name
  693. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  694. */
  695. public function unloadFilter($type, $name)
  696. {
  697. $_filter_name = "smarty_{$type}filter_{$name}";
  698. if (isset($this->smarty->registered_filters[$type][$_filter_name])) {
  699. unset ($this->smarty->registered_filters[$type][$_filter_name]);
  700. }
  701. return $this;
  702. }
  703. /**
  704. * preg_replace callback to convert camelcase getter/setter to underscore property names
  705. *
  706. * @param string $match match string
  707. * @return string replacemant
  708. */
  709. private function replaceCamelcase($match)
  710. {
  711. return "_" . strtolower($match[1]);
  712. }
  713. /**
  714. * Handle unknown class methods
  715. *
  716. * @param string $name unknown method-name
  717. * @param array $args argument array
  718. */
  719. public function __call($name, $args)
  720. {
  721. static $_prefixes = array('set' => true, 'get' => true);
  722. static $_resolved_property_name = array();
  723. static $_resolved_property_source = array();
  724. // method of Smarty object?
  725. if (method_exists($this->smarty, $name)) {
  726. return call_user_func_array(array($this->smarty, $name), $args);
  727. }
  728. // see if this is a set/get for a property
  729. $first3 = strtolower(substr($name, 0, 3));
  730. if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') {
  731. if (isset($_resolved_property_name[$name])) {
  732. $property_name = $_resolved_property_name[$name];
  733. } else {
  734. // try to keep case correct for future PHP 6.0 case-sensitive class methods
  735. // lcfirst() not available < PHP 5.3.0, so improvise
  736. $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
  737. // convert camel case to underscored name
  738. $property_name = preg_replace_callback('/([A-Z])/', array($this,'replaceCamelcase'), $property_name);
  739. $_resolved_property_name[$name] = $property_name;
  740. }
  741. if (isset($_resolved_property_source[$property_name])) {
  742. $_is_this = $_resolved_property_source[$property_name];
  743. } else {
  744. $_is_this = null;
  745. if (property_exists($this, $property_name)) {
  746. $_is_this = true;
  747. } elseif (property_exists($this->smarty, $property_name)) {
  748. $_is_this = false;
  749. }
  750. $_resolved_property_source[$property_name] = $_is_this;
  751. }
  752. if ($_is_this) {
  753. if ($first3 == 'get')
  754. return $this->$property_name;
  755. else
  756. return $this->$property_name = $args[0];
  757. } elseif ($_is_this === false) {
  758. if ($first3 == 'get')
  759. return $this->smarty->$property_name;
  760. else
  761. return $this->smarty->$property_name = $args[0];
  762. } else {
  763. throw new SmartyException("property '$property_name' does not exist.");
  764. return false;
  765. }
  766. }
  767. if ($name == 'Smarty') {
  768. throw new SmartyException("PHP5 requires you to call __construct() instead of Smarty()");
  769. }
  770. // must be unknown
  771. throw new SmartyException("Call of unknown method '$name'.");
  772. }
  773. }