PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/smarty/vendor/smarty/sysplugins/smarty_internal_templatebase.php

https://bitbucket.org/sudak/rating
PHP | 811 lines | 524 code | 41 blank | 246 comment | 154 complexity | 3cc63cab45dc7d6c75657f2bb708f054 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. array_unshift($_template->_capture_stack,array());
  175. //
  176. // render compiled template
  177. //
  178. $_template->properties['unifunc']($_template);
  179. // any unclosed {capture} tags ?
  180. if (isset($_template->_capture_stack[0][0])) {
  181. $_template->capture_error();
  182. }
  183. array_shift($_template->_capture_stack);
  184. } catch (Exception $e) {
  185. ob_get_clean();
  186. throw $e;
  187. }
  188. }
  189. } else {
  190. if ($_template->source->uncompiled) {
  191. if ($this->smarty->debugging) {
  192. Smarty_Internal_Debug::start_render($_template);
  193. }
  194. try {
  195. ob_start();
  196. $_template->source->renderUncompiled($_template);
  197. } catch (Exception $e) {
  198. ob_get_clean();
  199. throw $e;
  200. }
  201. } else {
  202. throw new SmartyException("Resource '$_template->source->type' must have 'renderUncompiled' method");
  203. }
  204. }
  205. $_output = ob_get_clean();
  206. if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) {
  207. $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
  208. }
  209. if ($_template->parent instanceof Smarty_Internal_Template) {
  210. $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']);
  211. foreach ($_template->required_plugins as $code => $tmp1) {
  212. foreach ($tmp1 as $name => $tmp) {
  213. foreach ($tmp as $type => $data) {
  214. $_template->parent->required_plugins[$code][$name][$type] = $data;
  215. }
  216. }
  217. }
  218. }
  219. if ($this->smarty->debugging) {
  220. Smarty_Internal_Debug::end_render($_template);
  221. }
  222. // write to cache when nessecary
  223. if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) {
  224. if ($this->smarty->debugging) {
  225. Smarty_Internal_Debug::start_cache($_template);
  226. }
  227. $_template->properties['has_nocache_code'] = false;
  228. // get text between non-cached items
  229. $cache_split = preg_split("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output);
  230. // get non-cached items
  231. preg_match_all("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output, $cache_parts);
  232. $output = '';
  233. // loop over items, stitch back together
  234. foreach ($cache_split as $curr_idx => $curr_split) {
  235. // escape PHP tags in template content
  236. $output .= preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php echo \'$1\'; ?>', $curr_split);
  237. if (isset($cache_parts[0][$curr_idx])) {
  238. $_template->properties['has_nocache_code'] = true;
  239. // remove nocache tags from cache output
  240. $output .= preg_replace("!/\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!", '', $cache_parts[0][$curr_idx]);
  241. }
  242. }
  243. if (!$no_output_filter && !$_template->has_nocache_code && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
  244. $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template);
  245. }
  246. // rendering (must be done before writing cache file because of {function} nocache handling)
  247. $_smarty_tpl = $_template;
  248. try {
  249. ob_start();
  250. eval("?>" . $output);
  251. $_output = ob_get_clean();
  252. } catch (Exception $e) {
  253. ob_get_clean();
  254. throw $e;
  255. }
  256. // write cache file content
  257. $_template->writeCachedContent($output);
  258. if ($this->smarty->debugging) {
  259. Smarty_Internal_Debug::end_cache($_template);
  260. }
  261. } else {
  262. // var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output);
  263. if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) {
  264. // replace nocache_hash
  265. $_output = str_replace("{$_template->properties['nocache_hash']}", $_template->parent->properties['nocache_hash'], $_output);
  266. $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code;
  267. }
  268. }
  269. } else {
  270. if ($this->smarty->debugging) {
  271. Smarty_Internal_Debug::start_cache($_template);
  272. }
  273. try {
  274. ob_start();
  275. array_unshift($_template->_capture_stack,array());
  276. //
  277. // render cached template
  278. //
  279. $_template->properties['unifunc']($_template);
  280. // any unclosed {capture} tags ?
  281. if (isset($_template->_capture_stack[0][0])) {
  282. $_template->capture_error();
  283. }
  284. array_shift($_template->_capture_stack);
  285. $_output = ob_get_clean();
  286. } catch (Exception $e) {
  287. ob_get_clean();
  288. throw $e;
  289. }
  290. if ($this->smarty->debugging) {
  291. Smarty_Internal_Debug::end_cache($_template);
  292. }
  293. }
  294. 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']))) {
  295. $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template);
  296. }
  297. if (isset($this->error_reporting)) {
  298. error_reporting($_smarty_old_error_level);
  299. }
  300. // display or fetch
  301. if ($display) {
  302. if ($this->caching && $this->cache_modified_check) {
  303. $_isCached = $_template->isCached() && !$_template->has_nocache_code;
  304. $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  305. if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) {
  306. switch (PHP_SAPI) {
  307. case 'cgi': // php-cgi < 5.3
  308. case 'cgi-fcgi': // php-cgi >= 5.3
  309. case 'fpm-fcgi': // php-fpm >= 5.3.3
  310. header('Status: 304 Not Modified');
  311. break;
  312. case 'cli':
  313. if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
  314. $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
  315. }
  316. break;
  317. default:
  318. header('HTTP/1.1 304 Not Modified');
  319. break;
  320. }
  321. } else {
  322. switch (PHP_SAPI) {
  323. case 'cli':
  324. if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
  325. $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT';
  326. }
  327. break;
  328. default:
  329. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT');
  330. break;
  331. }
  332. echo $_output;
  333. }
  334. } else {
  335. echo $_output;
  336. }
  337. // debug output
  338. if ($this->smarty->debugging) {
  339. Smarty_Internal_Debug::display_debug($this);
  340. }
  341. if ($merge_tpl_vars) {
  342. // restore local variables
  343. $_template->tpl_vars = $save_tpl_vars;
  344. $_template->config_vars = $save_config_vars;
  345. }
  346. return;
  347. } else {
  348. if ($merge_tpl_vars) {
  349. // restore local variables
  350. $_template->tpl_vars = $save_tpl_vars;
  351. $_template->config_vars = $save_config_vars;
  352. }
  353. // return fetched content
  354. return $_output;
  355. }
  356. }
  357. /**
  358. * displays a Smarty template
  359. *
  360. * @param string $template the resource handle of the template file or template object
  361. * @param mixed $cache_id cache id to be used with this template
  362. * @param mixed $compile_id compile id to be used with this template
  363. * @param object $parent next higher level of Smarty variables
  364. */
  365. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  366. {
  367. // display template
  368. $this->fetch($template, $cache_id, $compile_id, $parent, true);
  369. }
  370. /**
  371. * test if cache is valid
  372. *
  373. * @param string|object $template the resource handle of the template file or template object
  374. * @param mixed $cache_id cache id to be used with this template
  375. * @param mixed $compile_id compile id to be used with this template
  376. * @param object $parent next higher level of Smarty variables
  377. * @return boolean cache status
  378. */
  379. public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
  380. {
  381. if ($template === null && $this instanceof $this->template_class) {
  382. return $this->cached->valid;
  383. }
  384. if (!($template instanceof $this->template_class)) {
  385. if ($parent === null) {
  386. $parent = $this;
  387. }
  388. $template = $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
  389. }
  390. // return cache status of template
  391. return $template->cached->valid;
  392. }
  393. /**
  394. * creates a data object
  395. *
  396. * @param object $parent next higher level of Smarty variables
  397. * @returns Smarty_Data data object
  398. */
  399. public function createData($parent = null)
  400. {
  401. return new Smarty_Data($parent, $this);
  402. }
  403. /**
  404. * Registers plugin to be used in templates
  405. *
  406. * @param string $type plugin type
  407. * @param string $tag name of template tag
  408. * @param callback $callback PHP callback to register
  409. * @param boolean $cacheable if true (default) this fuction is cachable
  410. * @param array $cache_attr caching attributes if any
  411. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  412. * @throws SmartyException when the plugin tag is invalid
  413. */
  414. public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
  415. {
  416. if (isset($this->smarty->registered_plugins[$type][$tag])) {
  417. throw new SmartyException("Plugin tag \"{$tag}\" already registered");
  418. } elseif (!is_callable($callback)) {
  419. throw new SmartyException("Plugin \"{$tag}\" not callable");
  420. } else {
  421. $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
  422. }
  423. return $this;
  424. }
  425. /**
  426. * Unregister Plugin
  427. *
  428. * @param string $type of plugin
  429. * @param string $tag name of plugin
  430. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  431. */
  432. public function unregisterPlugin($type, $tag)
  433. {
  434. if (isset($this->smarty->registered_plugins[$type][$tag])) {
  435. unset($this->smarty->registered_plugins[$type][$tag]);
  436. }
  437. return $this;
  438. }
  439. /**
  440. * Registers a resource to fetch a template
  441. *
  442. * @param string $type name of resource type
  443. * @param Smarty_Resource|array $callback or instance of Smarty_Resource, or array of callbacks to handle resource (deprecated)
  444. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  445. */
  446. public function registerResource($type, $callback)
  447. {
  448. $this->smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false);
  449. return $this;
  450. }
  451. /**
  452. * Unregisters a resource
  453. *
  454. * @param string $type name of resource type
  455. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  456. */
  457. public function unregisterResource($type)
  458. {
  459. if (isset($this->smarty->registered_resources[$type])) {
  460. unset($this->smarty->registered_resources[$type]);
  461. }
  462. return $this;
  463. }
  464. /**
  465. * Registers a cache resource to cache a template's output
  466. *
  467. * @param string $type name of cache resource type
  468. * @param Smarty_CacheResource $callback instance of Smarty_CacheResource to handle output caching
  469. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  470. */
  471. public function registerCacheResource($type, Smarty_CacheResource $callback)
  472. {
  473. $this->smarty->registered_cache_resources[$type] = $callback;
  474. return $this;
  475. }
  476. /**
  477. * Unregisters a cache resource
  478. *
  479. * @param string $type name of cache resource type
  480. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  481. */
  482. public function unregisterCacheResource($type)
  483. {
  484. if (isset($this->smarty->registered_cache_resources[$type])) {
  485. unset($this->smarty->registered_cache_resources[$type]);
  486. }
  487. return $this;
  488. }
  489. /**
  490. * Registers object to be used in templates
  491. *
  492. * @param string $object name of template object
  493. * @param object $object_impl the referenced PHP object to register
  494. * @param array $allowed list of allowed methods (empty = all)
  495. * @param boolean $smarty_args smarty argument format, else traditional
  496. * @param array $block_methods list of block-methods
  497. * @param array $block_functs list of methods that are block format
  498. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  499. * @throws SmartyException if any of the methods in $allowed or $block_methods are invalid
  500. */
  501. public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  502. {
  503. // test if allowed methodes callable
  504. if (!empty($allowed)) {
  505. foreach ((array) $allowed as $method) {
  506. if (!is_callable(array($object_impl, $method))) {
  507. throw new SmartyException("Undefined method '$method' in registered object");
  508. }
  509. }
  510. }
  511. // test if block methodes callable
  512. if (!empty($block_methods)) {
  513. foreach ((array) $block_methods as $method) {
  514. if (!is_callable(array($object_impl, $method))) {
  515. throw new SmartyException("Undefined method '$method' in registered object");
  516. }
  517. }
  518. }
  519. // register the object
  520. $this->smarty->registered_objects[$object_name] =
  521. array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods);
  522. return $this;
  523. }
  524. /**
  525. * return a reference to a registered object
  526. *
  527. * @param string $name object name
  528. * @return object
  529. * @throws SmartyException if no such object is found
  530. */
  531. public function getRegisteredObject($name)
  532. {
  533. if (!isset($this->smarty->registered_objects[$name])) {
  534. throw new SmartyException("'$name' is not a registered object");
  535. }
  536. if (!is_object($this->smarty->registered_objects[$name][0])) {
  537. throw new SmartyException("registered '$name' is not an object");
  538. }
  539. return $this->smarty->registered_objects[$name][0];
  540. }
  541. /**
  542. * unregister an object
  543. *
  544. * @param string $name object name
  545. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  546. */
  547. public function unregisterObject($name)
  548. {
  549. if (isset($this->smarty->registered_objects[$name])) {
  550. unset($this->smarty->registered_objects[$name]);
  551. }
  552. return $this;
  553. }
  554. /**
  555. * Registers static classes to be used in templates
  556. *
  557. * @param string $class name of template class
  558. * @param string $class_impl the referenced PHP class to register
  559. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  560. * @throws SmartyException if $class_impl does not refer to an existing class
  561. */
  562. public function registerClass($class_name, $class_impl)
  563. {
  564. // test if exists
  565. if (!class_exists($class_impl)) {
  566. throw new SmartyException("Undefined class '$class_impl' in register template class");
  567. }
  568. // register the class
  569. $this->smarty->registered_classes[$class_name] = $class_impl;
  570. return $this;
  571. }
  572. /**
  573. * Registers a default plugin handler
  574. *
  575. * @param callable $callback class/method name
  576. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  577. * @throws SmartyException if $callback is not callable
  578. */
  579. public function registerDefaultPluginHandler($callback)
  580. {
  581. if (is_callable($callback)) {
  582. $this->smarty->default_plugin_handler_func = $callback;
  583. } else {
  584. throw new SmartyException("Default plugin handler '$callback' not callable");
  585. }
  586. return $this;
  587. }
  588. /**
  589. * Registers a default template handler
  590. *
  591. * @param callable $callback class/method name
  592. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  593. * @throws SmartyException if $callback is not callable
  594. */
  595. public function registerDefaultTemplateHandler($callback)
  596. {
  597. if (is_callable($callback)) {
  598. $this->smarty->default_template_handler_func = $callback;
  599. } else {
  600. throw new SmartyException("Default template handler '$callback' not callable");
  601. }
  602. return $this;
  603. }
  604. /**
  605. * Registers a default template handler
  606. *
  607. * @param callable $callback class/method name
  608. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  609. * @throws SmartyException if $callback is not callable
  610. */
  611. public function registerDefaultConfigHandler($callback)
  612. {
  613. if (is_callable($callback)) {
  614. $this->smarty->default_config_handler_func = $callback;
  615. } else {
  616. throw new SmartyException("Default config handler '$callback' not callable");
  617. }
  618. return $this;
  619. }
  620. /**
  621. * Registers a filter function
  622. *
  623. * @param string $type filter type
  624. * @param callback $callback
  625. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  626. */
  627. public function registerFilter($type, $callback)
  628. {
  629. $this->smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback;
  630. return $this;
  631. }
  632. /**
  633. * Unregisters a filter function
  634. *
  635. * @param string $type filter type
  636. * @param callback $callback
  637. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  638. */
  639. public function unregisterFilter($type, $callback)
  640. {
  641. $name = $this->_get_filter_name($callback);
  642. if (isset($this->smarty->registered_filters[$type][$name])) {
  643. unset($this->smarty->registered_filters[$type][$name]);
  644. }
  645. return $this;
  646. }
  647. /**
  648. * Return internal filter name
  649. *
  650. * @param callback $function_name
  651. * @return string internal filter name
  652. */
  653. public function _get_filter_name($function_name)
  654. {
  655. if (is_array($function_name)) {
  656. $_class_name = (is_object($function_name[0]) ?
  657. get_class($function_name[0]) : $function_name[0]);
  658. return $_class_name . '_' . $function_name[1];
  659. } else {
  660. return $function_name;
  661. }
  662. }
  663. /**
  664. * load a filter of specified type and name
  665. *
  666. * @param string $type filter type
  667. * @param string $name filter name
  668. * @throws SmartyException if filter could not be loaded
  669. */
  670. public function loadFilter($type, $name)
  671. {
  672. $_plugin = "smarty_{$type}filter_{$name}";
  673. $_filter_name = $_plugin;
  674. if ($this->smarty->loadPlugin($_plugin)) {
  675. if (class_exists($_plugin, false)) {
  676. $_plugin = array($_plugin, 'execute');
  677. }
  678. if (is_callable($_plugin)) {
  679. $this->smarty->registered_filters[$type][$_filter_name] = $_plugin;
  680. return true;
  681. }
  682. }
  683. throw new SmartyException("{$type}filter \"{$name}\" not callable");
  684. }
  685. /**
  686. * unload a filter of specified type and name
  687. *
  688. * @param string $type filter type
  689. * @param string $name filter name
  690. * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
  691. */
  692. public function unloadFilter($type, $name)
  693. {
  694. $_filter_name = "smarty_{$type}filter_{$name}";
  695. if (isset($this->smarty->registered_filters[$type][$_filter_name])) {
  696. unset ($this->smarty->registered_filters[$type][$_filter_name]);
  697. }
  698. return $this;
  699. }
  700. /**
  701. * preg_replace callback to convert camelcase getter/setter to underscore property names
  702. *
  703. * @param string $match match string
  704. * @return string replacemant
  705. */
  706. private function replaceCamelcase($match) {
  707. return "_" . strtolower($match[1]);
  708. }
  709. /**
  710. * Handle unknown class methods
  711. *
  712. * @param string $name unknown method-name
  713. * @param array $args argument array
  714. */
  715. public function __call($name, $args)
  716. {
  717. static $_prefixes = array('set' => true, 'get' => true);
  718. static $_resolved_property_name = array();
  719. static $_resolved_property_source = array();
  720. // method of Smarty object?
  721. if (method_exists($this->smarty, $name)) {
  722. return call_user_func_array(array($this->smarty, $name), $args);
  723. }
  724. // see if this is a set/get for a property
  725. $first3 = strtolower(substr($name, 0, 3));
  726. if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') {
  727. if (isset($_resolved_property_name[$name])) {
  728. $property_name = $_resolved_property_name[$name];
  729. } else {
  730. // try to keep case correct for future PHP 6.0 case-sensitive class methods
  731. // lcfirst() not available < PHP 5.3.0, so improvise
  732. $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
  733. // convert camel case to underscored name
  734. $property_name = preg_replace_callback('/([A-Z])/', array($this,'replaceCamelcase'), $property_name);
  735. $_resolved_property_name[$name] = $property_name;
  736. }
  737. if (isset($_resolved_property_source[$property_name])) {
  738. $_is_this = $_resolved_property_source[$property_name];
  739. } else {
  740. $_is_this = null;
  741. if (property_exists($this, $property_name)) {
  742. $_is_this = true;
  743. } else if (property_exists($this->smarty, $property_name)) {
  744. $_is_this = false;
  745. }
  746. $_resolved_property_source[$property_name] = $_is_this;
  747. }
  748. if ($_is_this) {
  749. if ($first3 == 'get')
  750. return $this->$property_name;
  751. else
  752. return $this->$property_name = $args[0];
  753. } else if ($_is_this === false) {
  754. if ($first3 == 'get')
  755. return $this->smarty->$property_name;
  756. else
  757. return $this->smarty->$property_name = $args[0];
  758. } else {
  759. throw new SmartyException("property '$property_name' does not exist.");
  760. return false;
  761. }
  762. }
  763. if ($name == 'Smarty') {
  764. throw new SmartyException("PHP5 requires you to call __construct() instead of Smarty()");
  765. }
  766. // must be unknown
  767. throw new SmartyException("Call of unknown method '$name'.");
  768. }
  769. }
  770. ?>