smarty_internal_templatebase.php 31 KB

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