SmartyBC.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: SmartyBC.class.php
  5. * SVN: $Id: $
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. * smarty-discussion-subscribe@googlegroups.com
  24. *
  25. * @link http://www.smarty.net/
  26. * @copyright 2008 New Digital Group, Inc.
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Uwe Tews
  29. * @author Rodney Rehm
  30. * @package Smarty
  31. */
  32. /**
  33. * @ignore
  34. */
  35. require(dirname(__FILE__) . '/Smarty.class.php');
  36. /**
  37. * Smarty Backward Compatability Wrapper Class
  38. *
  39. * @package Smarty
  40. */
  41. class SmartyBC extends Smarty {
  42. /**
  43. * Smarty 2 BC
  44. * @var string
  45. */
  46. public $_version = self::SMARTY_VERSION;
  47. /**
  48. * Initialize new SmartyBC object
  49. *
  50. * @param array $options options to set during initialization, e.g. array( 'forceCompile' => false )
  51. */
  52. public function __construct(array $options=array())
  53. {
  54. parent::__construct($options);
  55. // register {php} tag
  56. $this->registerPlugin('block', 'php', 'smarty_php_tag');
  57. }
  58. /**
  59. * wrapper for assign_by_ref
  60. *
  61. * @param string $tpl_var the template variable name
  62. * @param mixed &$value the referenced value to assign
  63. */
  64. public function assign_by_ref($tpl_var, &$value)
  65. {
  66. $this->assignByRef($tpl_var, $value);
  67. }
  68. /**
  69. * wrapper for append_by_ref
  70. *
  71. * @param string $tpl_var the template variable name
  72. * @param mixed &$value the referenced value to append
  73. * @param boolean $merge flag if array elements shall be merged
  74. */
  75. public function append_by_ref($tpl_var, &$value, $merge = false)
  76. {
  77. $this->appendByRef($tpl_var, $value, $merge);
  78. }
  79. /**
  80. * clear the given assigned template variable.
  81. *
  82. * @param string $tpl_var the template variable to clear
  83. */
  84. public function clear_assign($tpl_var)
  85. {
  86. $this->clearAssign($tpl_var);
  87. }
  88. /**
  89. * Registers custom function to be used in templates
  90. *
  91. * @param string $function the name of the template function
  92. * @param string $function_impl the name of the PHP function to register
  93. * @param bool $cacheable
  94. * @param mixed $cache_attrs
  95. */
  96. public function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
  97. {
  98. $this->registerPlugin('function', $function, $function_impl, $cacheable, $cache_attrs);
  99. }
  100. /**
  101. * Unregisters custom function
  102. *
  103. * @param string $function name of template function
  104. */
  105. public function unregister_function($function)
  106. {
  107. $this->unregisterPlugin('function', $function);
  108. }
  109. /**
  110. * Registers object to be used in templates
  111. *
  112. * @param string $object name of template object
  113. * @param object $object_impl the referenced PHP object to register
  114. * @param array $allowed list of allowed methods (empty = all)
  115. * @param boolean $smarty_args smarty argument format, else traditional
  116. * @param array $block_functs list of methods that are block format
  117. */
  118. public function register_object($object, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  119. {
  120. settype($allowed, 'array');
  121. settype($smarty_args, 'boolean');
  122. $this->registerObject($object, $object_impl, $allowed, $smarty_args, $block_methods);
  123. }
  124. /**
  125. * Unregisters object
  126. *
  127. * @param string $object name of template object
  128. */
  129. public function unregister_object($object)
  130. {
  131. $this->unregisterObject($object);
  132. }
  133. /**
  134. * Registers block function to be used in templates
  135. *
  136. * @param string $block name of template block
  137. * @param string $block_impl PHP function to register
  138. * @param bool $cacheable
  139. * @param mixed $cache_attrs
  140. */
  141. public function register_block($block, $block_impl, $cacheable=true, $cache_attrs=null)
  142. {
  143. $this->registerPlugin('block', $block, $block_impl, $cacheable, $cache_attrs);
  144. }
  145. /**
  146. * Unregisters block function
  147. *
  148. * @param string $block name of template function
  149. */
  150. public function unregister_block($block)
  151. {
  152. $this->unregisterPlugin('block', $block);
  153. }
  154. /**
  155. * Registers compiler function
  156. *
  157. * @param string $function name of template function
  158. * @param string $function_impl name of PHP function to register
  159. * @param bool $cacheable
  160. */
  161. public function register_compiler_function($function, $function_impl, $cacheable=true)
  162. {
  163. $this->registerPlugin('compiler', $function, $function_impl, $cacheable);
  164. }
  165. /**
  166. * Unregisters compiler function
  167. *
  168. * @param string $function name of template function
  169. */
  170. public function unregister_compiler_function($function)
  171. {
  172. $this->unregisterPlugin('compiler', $function);
  173. }
  174. /**
  175. * Registers modifier to be used in templates
  176. *
  177. * @param string $modifier name of template modifier
  178. * @param string $modifier_impl name of PHP function to register
  179. */
  180. public function register_modifier($modifier, $modifier_impl)
  181. {
  182. $this->registerPlugin('modifier', $modifier, $modifier_impl);
  183. }
  184. /**
  185. * Unregisters modifier
  186. *
  187. * @param string $modifier name of template modifier
  188. */
  189. public function unregister_modifier($modifier)
  190. {
  191. $this->unregisterPlugin('modifier', $modifier);
  192. }
  193. /**
  194. * Registers a resource to fetch a template
  195. *
  196. * @param string $type name of resource
  197. * @param array $functions array of functions to handle resource
  198. */
  199. public function register_resource($type, $functions)
  200. {
  201. $this->registerResource($type, $functions);
  202. }
  203. /**
  204. * Unregisters a resource
  205. *
  206. * @param string $type name of resource
  207. */
  208. public function unregister_resource($type)
  209. {
  210. $this->unregisterResource($type);
  211. }
  212. /**
  213. * Registers a prefilter function to apply
  214. * to a template before compiling
  215. *
  216. * @param callable $function
  217. */
  218. public function register_prefilter($function)
  219. {
  220. $this->registerFilter('pre', $function);
  221. }
  222. /**
  223. * Unregisters a prefilter function
  224. *
  225. * @param callable $function
  226. */
  227. public function unregister_prefilter($function)
  228. {
  229. $this->unregisterFilter('pre', $function);
  230. }
  231. /**
  232. * Registers a postfilter function to apply
  233. * to a compiled template after compilation
  234. *
  235. * @param callable $function
  236. */
  237. public function register_postfilter($function)
  238. {
  239. $this->registerFilter('post', $function);
  240. }
  241. /**
  242. * Unregisters a postfilter function
  243. *
  244. * @param callable $function
  245. */
  246. public function unregister_postfilter($function)
  247. {
  248. $this->unregisterFilter('post', $function);
  249. }
  250. /**
  251. * Registers an output filter function to apply
  252. * to a template output
  253. *
  254. * @param callable $function
  255. */
  256. public function register_outputfilter($function)
  257. {
  258. $this->registerFilter('output', $function);
  259. }
  260. /**
  261. * Unregisters an outputfilter function
  262. *
  263. * @param callable $function
  264. */
  265. public function unregister_outputfilter($function)
  266. {
  267. $this->unregisterFilter('output', $function);
  268. }
  269. /**
  270. * load a filter of specified type and name
  271. *
  272. * @param string $type filter type
  273. * @param string $name filter name
  274. */
  275. public function load_filter($type, $name)
  276. {
  277. $this->loadFilter($type, $name);
  278. }
  279. /**
  280. * clear cached content for the given template and cache id
  281. *
  282. * @param string $tpl_file name of template file
  283. * @param string $cache_id name of cache_id
  284. * @param string $compile_id name of compile_id
  285. * @param string $exp_time expiration time
  286. * @return boolean
  287. */
  288. public function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
  289. {
  290. return $this->clearCache($tpl_file, $cache_id, $compile_id, $exp_time);
  291. }
  292. /**
  293. * clear the entire contents of cache (all templates)
  294. *
  295. * @param string $exp_time expire time
  296. * @return boolean
  297. */
  298. public function clear_all_cache($exp_time = null)
  299. {
  300. return $this->clearCache(null, null, null, $exp_time);
  301. }
  302. /**
  303. * test to see if valid cache exists for this template
  304. *
  305. * @param string $tpl_file name of template file
  306. * @param string $cache_id
  307. * @param string $compile_id
  308. * @return boolean
  309. */
  310. public function is_cached($tpl_file, $cache_id = null, $compile_id = null)
  311. {
  312. return $this->isCached($tpl_file, $cache_id, $compile_id);
  313. }
  314. /**
  315. * clear all the assigned template variables.
  316. */
  317. public function clear_all_assign()
  318. {
  319. $this->clearAllAssign();
  320. }
  321. /**
  322. * clears compiled version of specified template resource,
  323. * or all compiled template files if one is not specified.
  324. * This function is for advanced use only, not normally needed.
  325. *
  326. * @param string $tpl_file
  327. * @param string $compile_id
  328. * @param string $exp_time
  329. * @return boolean results of {@link smarty_core_rm_auto()}
  330. */
  331. public function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
  332. {
  333. return $this->clearCompiledTemplate($tpl_file, $compile_id, $exp_time);
  334. }
  335. /**
  336. * Checks whether requested template exists.
  337. *
  338. * @param string $tpl_file
  339. * @return boolean
  340. */
  341. public function template_exists($tpl_file)
  342. {
  343. return $this->templateExists($tpl_file);
  344. }
  345. /**
  346. * Returns an array containing template variables
  347. *
  348. * @param string $name
  349. * @return array
  350. */
  351. public function get_template_vars($name=null)
  352. {
  353. return $this->getTemplateVars($name);
  354. }
  355. /**
  356. * Returns an array containing config variables
  357. *
  358. * @param string $name
  359. * @return array
  360. */
  361. public function get_config_vars($name=null)
  362. {
  363. return $this->getConfigVars($name);
  364. }
  365. /**
  366. * load configuration values
  367. *
  368. * @param string $file
  369. * @param string $section
  370. * @param string $scope
  371. */
  372. public function config_load($file, $section = null, $scope = 'global')
  373. {
  374. $this->ConfigLoad($file, $section, $scope);
  375. }
  376. /**
  377. * return a reference to a registered object
  378. *
  379. * @param string $name
  380. * @return object
  381. */
  382. public function get_registered_object($name)
  383. {
  384. return $this->getRegisteredObject($name);
  385. }
  386. /**
  387. * clear configuration values
  388. *
  389. * @param string $var
  390. */
  391. public function clear_config($var = null)
  392. {
  393. $this->clearConfig($var);
  394. }
  395. /**
  396. * trigger Smarty error
  397. *
  398. * @param string $error_msg
  399. * @param integer $error_type
  400. */
  401. public function trigger_error($error_msg, $error_type = E_USER_WARNING)
  402. {
  403. trigger_error("Smarty error: $error_msg", $error_type);
  404. }
  405. }
  406. /**
  407. * Smarty {php}{/php} block function
  408. *
  409. * @param array $params parameter list
  410. * @param string $content contents of the block
  411. * @param object $template template object
  412. * @param boolean &$repeat repeat flag
  413. * @return string content re-formatted
  414. */
  415. function smarty_php_tag($params, $content, $template, &$repeat)
  416. {
  417. eval($content);
  418. return '';
  419. }
  420. ?>