SmartyBC.class.php 12 KB

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