Smarty.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: Smarty.class.php
  5. * SVN: $Id: Smarty.class.php 3624 2010-07-07 22:08:10Z Uwe.Tews $
  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. * @package Smarty
  30. * @version 3-SVN$Rev: 3286 $
  31. */
  32. /**
  33. * define shorthand directory separator constant
  34. */
  35. if (!defined('DS')) {
  36. define('DS', DIRECTORY_SEPARATOR);
  37. }
  38. /**
  39. * set SMARTY_DIR to absolute path to Smarty library files.
  40. * Sets SMARTY_DIR only if user application has not already defined it.
  41. */
  42. if (!defined('SMARTY_DIR')) {
  43. define('SMARTY_DIR', dirname(__FILE__) . DS);
  44. }
  45. /**
  46. * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
  47. * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
  48. */
  49. if (!defined('SMARTY_SYSPLUGINS_DIR')) {
  50. define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
  51. }
  52. if (!defined('SMARTY_PLUGINS_DIR')) {
  53. define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
  54. }
  55. if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
  56. define('SMARTY_RESOURCE_CHAR_SET', 'UTF-8');
  57. }
  58. if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
  59. define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
  60. }
  61. /**
  62. * define variable scopes
  63. */
  64. define('SMARTY_LOCAL_SCOPE', 0);
  65. define('SMARTY_PARENT_SCOPE', 1);
  66. define('SMARTY_ROOT_SCOPE', 2);
  67. define('SMARTY_GLOBAL_SCOPE', 3);
  68. /**
  69. * define caching modes
  70. */
  71. define('SMARTY_CACHING_OFF', 0);
  72. define('SMARTY_CACHING_LIFETIME_CURRENT', 1);
  73. define('SMARTY_CACHING_LIFETIME_SAVED', 2);
  74. /**
  75. * This determines how Smarty handles "<?php ... ?>" tags in templates.
  76. * possible values:
  77. */
  78. define('SMARTY_PHP_PASSTHRU', 0); //-> print tags as plain text
  79. define('SMARTY_PHP_QUOTE', 1); //-> escape tags as entities
  80. define('SMARTY_PHP_REMOVE', 2); //-> escape tags as entities
  81. define('SMARTY_PHP_ALLOW', 3); //-> escape tags as entities
  82. /**
  83. * register the class autoloader
  84. */
  85. if (!defined('SMARTY_SPL_AUTOLOAD')) {
  86. define('SMARTY_SPL_AUTOLOAD', 0);
  87. }
  88. if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
  89. $registeredAutoLoadFunctions = spl_autoload_functions();
  90. if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
  91. spl_autoload_register();
  92. }
  93. } else {
  94. spl_autoload_register('smartyAutoload');
  95. }
  96. /**
  97. * This is the main Smarty class
  98. */
  99. class Smarty extends Smarty_Internal_Data {
  100. // smarty version
  101. const SMARTY_VERSION = 'Smarty3-RC3';
  102. // auto literal on delimiters with whitspace
  103. public $auto_literal = true;
  104. // display error on not assigned variables
  105. public $error_unassigned = false;
  106. // template directory
  107. public $template_dir = null;
  108. // default template handler
  109. public $default_template_handler_func = null;
  110. // compile directory
  111. public $compile_dir = null;
  112. // plugins directory
  113. public $plugins_dir = null;
  114. // cache directory
  115. public $cache_dir = null;
  116. // config directory
  117. public $config_dir = null;
  118. // force template compiling?
  119. public $force_compile = false;
  120. // check template for modifications?
  121. public $compile_check = true;
  122. // locking concurrent compiles
  123. public $compile_locking = true;
  124. // use sub dirs for compiled/cached files?
  125. public $use_sub_dirs = false;
  126. // compile_error?
  127. public $compile_error = false;
  128. // caching enabled
  129. public $caching = false;
  130. // merge compiled includea
  131. public $merge_compiled_includes = false;
  132. // cache lifetime
  133. public $cache_lifetime = 3600;
  134. // force cache file creation
  135. public $force_cache = false;
  136. // cache_id
  137. public $cache_id = null;
  138. // compile_id
  139. public $compile_id = null;
  140. // template delimiters
  141. public $left_delimiter = "{";
  142. public $right_delimiter = "}";
  143. // security
  144. public $security_class = 'Smarty_Security';
  145. public $php_handling = SMARTY_PHP_PASSTHRU;
  146. public $allow_php_tag = false;
  147. public $allow_php_templates = false;
  148. public $security = false;
  149. public $security_policy = null;
  150. public $security_handler = null;
  151. public $direct_access_security = true;
  152. public $trusted_dir = array();
  153. // debug mode
  154. public $debugging = false;
  155. public $debugging_ctrl = 'NONE';
  156. public $smarty_debug_id = 'SMARTY_DEBUG';
  157. public $debug_tpl = null;
  158. // When set, smarty does uses this value as error_reporting-level.
  159. public $error_reporting = null;
  160. // config var settings
  161. public $config_overwrite = true; //Controls whether variables with the same name overwrite each other.
  162. public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean
  163. public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file.
  164. // config vars
  165. public $config_vars = array();
  166. // assigned tpl vars
  167. public $tpl_vars = array();
  168. // assigned global tpl vars
  169. public $global_tpl_vars = array();
  170. // dummy parent object
  171. public $parent = null;
  172. // global template functions
  173. public $template_functions = array();
  174. // resource type used if none given
  175. public $default_resource_type = 'file';
  176. // caching type
  177. public $caching_type = 'file';
  178. // internal cache resource types
  179. public $cache_resource_types = array('file');
  180. // internal cache resource objects
  181. public $cache_resource_objects = array();
  182. // internal config properties
  183. public $properties = array();
  184. // config type
  185. public $default_config_type = 'file';
  186. // exception handler: array('ExceptionClass','ExceptionMethod');
  187. public $exception_handler = null;
  188. // cached template objects
  189. public $template_objects = null;
  190. // check If-Modified-Since headers
  191. public $cache_modified_check = false;
  192. // registered plugins
  193. public $registered_plugins = array();
  194. // plugin search order
  195. public $plugin_search_order = array('function', 'block', 'compiler', 'class');
  196. // registered objects
  197. public $registered_objects = array();
  198. // registered classes
  199. public $registered_classes = array();
  200. // registered filters
  201. public $registered_filters = array();
  202. // autoload filter
  203. public $autoload_filters = array();
  204. // status of filter on variable output
  205. public $variable_filter = true;
  206. // default modifier
  207. public $default_modifiers = array();
  208. // global internal smarty vars
  209. public $_smarty_vars = array();
  210. // start time for execution time calculation
  211. public $start_time = 0;
  212. // default file permissions
  213. public $_file_perms = 0644;
  214. // default dir permissions
  215. public $_dir_perms = 0771;
  216. // smarty object reference
  217. public $smarty = null;
  218. // block data at template inheritance
  219. public $block_data = array();
  220. public $block_data_stack = array();
  221. // block tag hierarchy
  222. public $_tag_stack = array();
  223. // plugins
  224. public $_plugins = array();
  225. // generate deprecated function call notices?
  226. public $deprecation_notices = true;
  227. /**
  228. * Class constructor, initializes basic smarty properties
  229. */
  230. public function __construct()
  231. {
  232. // self reference needed by other classes methods
  233. $this->smarty = $this;
  234. if (is_callable('mb_internal_encoding')) {
  235. mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
  236. }
  237. $this->start_time = microtime(true);
  238. // set exception handler
  239. if (!empty($this->exception_handler))
  240. set_exception_handler($this->exception_handler);
  241. // set default dirs
  242. $this->template_dir = array('.' . DS . 'templates' . DS);
  243. $this->compile_dir = '.' . DS . 'templates_c' . DS;
  244. $this->plugins_dir = array(SMARTY_PLUGINS_DIR);
  245. $this->cache_dir = '.' . DS . 'cache' . DS;
  246. $this->config_dir = '.' . DS . 'configs' . DS;
  247. $this->debug_tpl = SMARTY_DIR . 'debug.tpl';
  248. if (!$this->debugging && $this->debugging_ctrl == 'URL') {
  249. if (isset($_SERVER['QUERY_STRING'])) {
  250. $_query_string = $_SERVER['QUERY_STRING'];
  251. } else {
  252. $_query_string = '';
  253. }
  254. if (false !== strpos($_query_string, $this->smarty_debug_id)) {
  255. if (false !== strpos($_query_string, $this->smarty_debug_id . '=on')) {
  256. // enable debugging for this browser session
  257. setcookie('SMARTY_DEBUG', true);
  258. $this->debugging = true;
  259. } elseif (false !== strpos($_query_string, $this->smarty_debug_id . '=off')) {
  260. // disable debugging for this browser session
  261. setcookie('SMARTY_DEBUG', false);
  262. $this->debugging = false;
  263. } else {
  264. // enable debugging for this page
  265. $this->debugging = true;
  266. }
  267. } else {
  268. if (isset($_COOKIE['SMARTY_DEBUG'])) {
  269. $this->debugging = true;
  270. }
  271. }
  272. }
  273. if (isset($_SERVER['SCRIPT_NAME'])) {
  274. $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
  275. }
  276. }
  277. /**
  278. * Class destructor
  279. */
  280. public function __destruct()
  281. {
  282. // restore to previous exception handler, if any
  283. if (!empty($this->exception_handler))
  284. restore_exception_handler();
  285. }
  286. /**
  287. * fetches a rendered Smarty template
  288. *
  289. * @param string $template the resource handle of the template file or template object
  290. * @param mixed $cache_id cache id to be used with this template
  291. * @param mixed $compile_id compile id to be used with this template
  292. * @param object $ |null $parent next higher level of Smarty variables
  293. * @return string rendered template output
  294. */
  295. public function fetch($template, $cache_id = null, $compile_id = null, $parent = null, $display = false)
  296. {
  297. if (is_object($cache_id)) {
  298. $parent = $cache_id;
  299. $cache_id = null;
  300. }
  301. if ($parent === null) {
  302. // get default Smarty data object
  303. $parent = $this;
  304. }
  305. array_push($this->block_data_stack, $this->block_data);
  306. $this->block_data = array();
  307. // create template object if necessary
  308. ($template instanceof $this->template_class)? $_template = $template :
  309. $_template = $this->createTemplate ($template, $cache_id, $compile_id, $parent);
  310. $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting)
  311. ? $this->error_reporting : error_reporting() &~E_NOTICE);
  312. // obtain data for cache modified check
  313. if ($this->cache_modified_check && $this->caching && $display) {
  314. $_isCached = $_template->isCached() && !$_template->has_nocache_code;
  315. if ($_isCached) {
  316. $_gmt_mtime = gmdate('D, d M Y H:i:s', $_template->getCachedTimestamp()) . ' GMT';
  317. } else {
  318. $_gmt_mtime = '';
  319. }
  320. }
  321. // return redered template
  322. if (isset($this->autoload_filters['output']) || isset($this->registered_filters['output'])) {
  323. $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $this, $_template);
  324. } else {
  325. $_output = $_template->getRenderedTemplate();
  326. }
  327. $_template->rendered_content = null;
  328. error_reporting($_smarty_old_error_level);
  329. // display or fetch
  330. if ($display) {
  331. if ($this->caching && $this->cache_modified_check) {
  332. $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  333. if ($_isCached && $_gmt_mtime == $_last_modified_date) {
  334. if (php_sapi_name() == 'cgi')
  335. header('Status: 304 Not Modified');
  336. else
  337. header('HTTP/1.1 304 Not Modified');
  338. } else {
  339. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->getCachedTimestamp()) . ' GMT');
  340. echo $_output;
  341. }
  342. } else {
  343. echo $_output;
  344. }
  345. // debug output
  346. if ($this->debugging) {
  347. Smarty_Internal_Debug::display_debug($this);
  348. }
  349. $this->block_data = array_pop($this->block_data_stack);
  350. return;
  351. } else {
  352. // return fetched content
  353. $this->block_data = array_pop($this->block_data_stack);
  354. return $_output;
  355. }
  356. }
  357. /**
  358. * displays a Smarty template
  359. *
  360. * @param string $ |object $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, $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 i 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. * @return boolean cache status
  377. */
  378. public function isCached($template, $cache_id = null, $compile_id = null)
  379. {
  380. if (!($template instanceof $this->template_class)) {
  381. $template = $this->createTemplate ($template, $cache_id, $compile_id, $this);
  382. }
  383. // return cache status of template
  384. return $template->isCached();
  385. }
  386. /**
  387. * creates a data object
  388. *
  389. * @param object $parent next higher level of Smarty variables
  390. * @returns object data object
  391. */
  392. public function createData($parent = null)
  393. {
  394. return new Smarty_Data($parent, $this);
  395. }
  396. /**
  397. * creates a template object
  398. *
  399. * @param string $template the resource handle of the template file
  400. * @param object $parent next higher level of Smarty variables
  401. * @param mixed $cache_id cache id to be used with this template
  402. * @param mixed $compile_id compile id to be used with this template
  403. * @returns object template object
  404. */
  405. public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
  406. {
  407. if (is_object($cache_id) || is_array($cache_id)) {
  408. $parent = $cache_id;
  409. $cache_id = null;
  410. }
  411. if (is_array($parent)) {
  412. $data = $parent;
  413. $parent = null;
  414. } else {
  415. $data = null;
  416. }
  417. if (!is_object($template)) {
  418. // we got a template resource
  419. // already in template cache?
  420. $_templateId = crc32($template . $cache_id . $compile_id);
  421. if (isset($this->template_objects[$_templateId]) && $this->caching) {
  422. // return cached template object
  423. $tpl = $this->template_objects[$_templateId];
  424. } else {
  425. // create new template object
  426. $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
  427. }
  428. } else {
  429. // just return a copy of template class
  430. $tpl = $template;
  431. }
  432. // fill data if present
  433. if (is_array($data)) {
  434. // set up variable values
  435. foreach ($data as $_key => $_val) {
  436. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  437. }
  438. }
  439. return $tpl;
  440. }
  441. /**
  442. * Loads security class and enables security
  443. */
  444. public function enableSecurity()
  445. {
  446. if (isset($this->security_class)) {
  447. $this->security_policy = new $this->security_class;
  448. $this->security_handler = new Smarty_Internal_Security_Handler($this);
  449. $this->security = true;
  450. } else {
  451. throw new Exception('Property security_class is not defined');
  452. }
  453. }
  454. /**
  455. * Disable security
  456. */
  457. public function disableSecurity()
  458. {
  459. $this->security = false;
  460. }
  461. /**
  462. * Set template directory
  463. *
  464. * @param string $ |array $template_dir folder(s) of template sorces
  465. */
  466. public function setTemplateDir($template_dir)
  467. {
  468. $this->template_dir = (array)$template_dir;
  469. return;
  470. }
  471. /**
  472. * Adds template directory(s) to existing ones
  473. *
  474. * @param string $ |array $template_dir folder(s) of template sources
  475. */
  476. public function addTemplateDir($template_dir)
  477. {
  478. $this->template_dir = array_merge((array)$this->template_dir, (array)$template_dir);
  479. $this->template_dir = array_unique($this->template_dir);
  480. return;
  481. }
  482. /**
  483. * Check if a template resource exists
  484. *
  485. * @param string $resource_name template name
  486. * @return boolean status
  487. */
  488. function templateExists($resource_name)
  489. {
  490. // create template object
  491. $tpl = new $this->template_class($resource_name, $this);
  492. // check if it does exists
  493. return $tpl->isExisting();
  494. }
  495. /**
  496. * Takes unknown classes and loads plugin files for them
  497. * class name format: Smarty_PluginType_PluginName
  498. * plugin filename format: plugintype.pluginname.php
  499. *
  500. * @param string $plugin_name class plugin name to load
  501. * @return string |boolean filepath of loaded file or false
  502. */
  503. public function loadPlugin($plugin_name, $check = true)
  504. {
  505. // if function or class exists, exit silently (already loaded)
  506. if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false)))
  507. return true;
  508. // Plugin name is expected to be: Smarty_[Type]_[Name]
  509. $_plugin_name = strtolower($plugin_name);
  510. $_name_parts = explode('_', $_plugin_name, 3);
  511. // class name must have three parts to be valid plugin
  512. if (count($_name_parts) < 3 || $_name_parts[0] !== 'smarty') {
  513. throw new Exception("plugin {$plugin_name} is not a valid name format");
  514. return false;
  515. }
  516. // if type is "internal", get plugin from sysplugins
  517. if ($_name_parts[1] == 'internal') {
  518. $file = SMARTY_SYSPLUGINS_DIR . $_plugin_name . '.php';
  519. if (file_exists($file)) {
  520. require_once($file);
  521. return $file;
  522. } else {
  523. return false;
  524. }
  525. }
  526. // plugin filename is expected to be: [type].[name].php
  527. $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
  528. // loop through plugin dirs and find the plugin
  529. foreach((array)$this->plugins_dir as $_plugin_dir) {
  530. if (strpos('/\\', substr($_plugin_dir, -1)) === false) {
  531. $_plugin_dir .= DS;
  532. }
  533. $file = $_plugin_dir . $_plugin_filename;
  534. if (file_exists($file)) {
  535. require_once($file);
  536. return $file;
  537. }
  538. }
  539. // no plugin loaded
  540. return false;
  541. }
  542. /**
  543. * load a filter of specified type and name
  544. *
  545. * @param string $type filter type
  546. * @param string $name filter name
  547. * @return bool
  548. */
  549. function loadFilter($type, $name)
  550. {
  551. $_plugin = "smarty_{$type}filter_{$name}";
  552. $_filter_name = $_plugin;
  553. if ($this->loadPlugin($_plugin)) {
  554. if (class_exists($_plugin, false)) {
  555. $_plugin = array($_plugin, 'execute');
  556. }
  557. if (is_callable($_plugin)) {
  558. return $this->registered_filters[$type][$_filter_name] = $_plugin;
  559. }
  560. }
  561. throw new Exception("{$type}filter \"{$name}\" not callable");
  562. return false;
  563. }
  564. /**
  565. * Sets the exception handler for Smarty.
  566. *
  567. * @param mixed $handler function name or array with object/method names
  568. * @return string previous exception handler
  569. */
  570. public function setExceptionHandler($handler)
  571. {
  572. $this->exception_handler = $handler;
  573. return set_exception_handler($handler);
  574. }
  575. /**
  576. * trigger Smarty error
  577. *
  578. * @param string $error_msg
  579. * @param integer $error_type
  580. */
  581. public function trigger_error($error_msg, $error_type = E_USER_WARNING)
  582. {
  583. throw new Exception("Smarty error: $error_msg");
  584. }
  585. /**
  586. * Return internal filter name
  587. *
  588. * @param callback $function_name
  589. */
  590. public function _get_filter_name($function_name)
  591. {
  592. if (is_array($function_name)) {
  593. $_class_name = (is_object($function_name[0]) ?
  594. get_class($function_name[0]) : $function_name[0]);
  595. return $_class_name . '_' . $function_name[1];
  596. } else {
  597. return $function_name;
  598. }
  599. }
  600. /**
  601. * Adds directory of plugin files
  602. *
  603. * @param object $smarty
  604. * @param string $ |array $ plugins folder
  605. * @return
  606. */
  607. function addPluginsDir($plugins_dir)
  608. {
  609. $this->plugins_dir = array_merge((array)$this->plugins_dir, (array)$plugins_dir);
  610. $this->plugins_dir = array_unique($this->plugins_dir);
  611. return;
  612. }
  613. /**
  614. * Returns a single or all global variables
  615. *
  616. * @param object $smarty
  617. * @param string $varname variable name or null
  618. * @return string variable value or or array of variables
  619. */
  620. function getGlobal($varname = null)
  621. {
  622. if (isset($varname)) {
  623. if (isset($this->global_tpl_vars[$varname])) {
  624. return $this->global_tpl_vars[$varname]->value;
  625. } else {
  626. return '';
  627. }
  628. } else {
  629. $_result = array();
  630. foreach ($this->global_tpl_vars AS $key => $var) {
  631. $_result[$key] = $var->value;
  632. }
  633. return $_result;
  634. }
  635. }
  636. /**
  637. * return a reference to a registered object
  638. *
  639. * @param string $name object name
  640. * @return object
  641. */
  642. function getRegisteredObject($name)
  643. {
  644. if (!isset($this->registered_objects[$name]))
  645. throw new Exception("'$name' is not a registered object");
  646. if (!is_object($this->registered_objects[$name][0]))
  647. throw new Exception("registered '$name' is not an object");
  648. return $this->registered_objects[$name][0];
  649. }
  650. /**
  651. * return name of debugging template
  652. *
  653. * @return string
  654. */
  655. function getDebugTemplate()
  656. {
  657. return $this->debug_tpl;
  658. }
  659. /**
  660. * set the debug template
  661. *
  662. * @param string $tpl_name
  663. * @return bool
  664. */
  665. function setDebugTemplate($tpl_name)
  666. {
  667. return $this->debug_tpl = $tpl_name;
  668. }
  669. /**
  670. * lazy loads (valid) property objects
  671. *
  672. * @param string $name property name
  673. */
  674. public function __get($name)
  675. {
  676. if (in_array($name, array('register', 'unregister', 'utility', 'cache'))) {
  677. $class = "Smarty_Internal_" . ucfirst($name);
  678. $this->$name = new $class($this);
  679. return $this->$name;
  680. } else if ($name == '_version') {
  681. // Smarty 2 BC
  682. $this->_version = self::SMARTY_VERSION;
  683. return $this->_version;
  684. }
  685. return null;
  686. }
  687. /**
  688. * Takes unknown class methods and lazy loads sysplugin files for them
  689. * class name format: Smarty_Method_MethodName
  690. * plugin filename format: method.methodname.php
  691. *
  692. * @param string $name unknown methode name
  693. * @param array $args aurgument array
  694. */
  695. public function __call($name, $args)
  696. {
  697. static $camel_func;
  698. if (!isset($camel_func))
  699. $camel_func = create_function('$c', 'return "_" . strtolower($c[1]);');
  700. // PHP4 call to constructor?
  701. if (strtolower($name) == 'smarty') {
  702. throw new Exception('Please use parent::__construct() to call parent constuctor');
  703. return false;
  704. }
  705. // see if this is a set/get for a property
  706. $first3 = strtolower(substr($name, 0, 3));
  707. if (in_array($first3, array('set', 'get')) && substr($name, 3, 1) !== '_') {
  708. // try to keep case correct for future PHP 6.0 case-sensitive class methods
  709. // lcfirst() not available < PHP 5.3.0, so improvise
  710. $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
  711. // convert camel case to underscored name
  712. $property_name = preg_replace_callback('/([A-Z])/', $camel_func, $property_name);
  713. if (!property_exists($this, $property_name)) {
  714. throw new Exception("property '$property_name' does not exist.");
  715. return false;
  716. }
  717. if ($first3 == 'get')
  718. return $this->$property_name;
  719. else
  720. return $this->$property_name = $args[0];
  721. }
  722. // Smarty Backward Compatible wrapper
  723. if (!isset($this->wrapper)) {
  724. $this->wrapper = new Smarty_Internal_Wrapper($this);
  725. }
  726. return $this->wrapper->convert($name, $args);
  727. }
  728. }
  729. function smartyAutoload($class)
  730. {
  731. $_class = strtolower($class);
  732. if (substr($_class, 0, 16) === 'smarty_internal_' || $_class == 'smarty_security') {
  733. include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
  734. }
  735. }
  736. ?>