Smarty.class.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: Smarty.class.php
  5. *
  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. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. * For questions, help, comments, discussion, etc., please join the
  20. * Smarty mailing list. Send a blank e-mail to
  21. * smarty-discussion-subscribe@googlegroups.com
  22. *
  23. * @link http://www.smarty.net/
  24. * @copyright 2017 New Digital Group, Inc.
  25. * @copyright 2017 Uwe Tews
  26. * @author Monte Ohrt <monte at ohrt dot com>
  27. * @author Uwe Tews
  28. * @author Rodney Rehm
  29. * @package Smarty
  30. * @version 3.1.32-dev
  31. */
  32. /**
  33. * set SMARTY_DIR to absolute path to Smarty library files.
  34. * Sets SMARTY_DIR only if user application has not already defined it.
  35. */
  36. if (!defined('SMARTY_DIR')) {
  37. define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  38. }
  39. /**
  40. * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
  41. * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
  42. */
  43. if (!defined('SMARTY_SYSPLUGINS_DIR')) {
  44. define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR);
  45. }
  46. if (!defined('SMARTY_PLUGINS_DIR')) {
  47. define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DIRECTORY_SEPARATOR);
  48. }
  49. if (!defined('SMARTY_MBSTRING')) {
  50. define('SMARTY_MBSTRING', function_exists('mb_get_info'));
  51. }
  52. if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
  53. // UTF-8 can only be done properly when mbstring is available!
  54. /**
  55. * @deprecated in favor of Smarty::$_CHARSET
  56. */
  57. define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ? 'UTF-8' : 'ISO-8859-1');
  58. }
  59. if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
  60. /**
  61. * @deprecated in favor of Smarty::$_DATE_FORMAT
  62. */
  63. define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
  64. }
  65. /**
  66. * Load Smarty_Autoloader
  67. */
  68. if (!class_exists('Smarty_Autoloader')) {
  69. include dirname(__FILE__) . '/bootstrap.php';
  70. }
  71. /**
  72. * Load always needed external class files
  73. */
  74. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php';
  75. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_extension_handler.php';
  76. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_templatebase.php';
  77. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_template.php';
  78. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_resource.php';
  79. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_variable.php';
  80. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_template_source.php';
  81. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_template_resource_base.php';
  82. require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_resource_file.php';
  83. /**
  84. * This is the main Smarty class
  85. *
  86. * @package Smarty
  87. *
  88. * The following methods will be dynamically loaded by the extension handler when they are called.
  89. * They are located in a corresponding Smarty_Internal_Method_xxxx class
  90. *
  91. * @method int clearAllCache(int $exp_time = null, string $type = null)
  92. * @method int clearCache(string $template_name, string $cache_id = null, string $compile_id = null, int $exp_time = null, string $type = null)
  93. * @method int compileAllTemplates(string $extension = '.tpl', bool $force_compile = false, int $time_limit = 0, int $max_errors = null)
  94. * @method int compileAllConfig(string $extension = '.conf', bool $force_compile = false, int $time_limit = 0, int $max_errors = null)
  95. * @method int clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  96. */
  97. class Smarty extends Smarty_Internal_TemplateBase
  98. {
  99. /**#@+
  100. * constant definitions
  101. */
  102. /**
  103. * smarty version
  104. */
  105. const SMARTY_VERSION = '3.1.32-dev-11';
  106. /**
  107. * define variable scopes
  108. */
  109. const SCOPE_LOCAL = 1;
  110. const SCOPE_PARENT = 2;
  111. const SCOPE_TPL_ROOT = 4;
  112. const SCOPE_ROOT = 8;
  113. const SCOPE_SMARTY = 16;
  114. const SCOPE_GLOBAL = 32;
  115. /**
  116. * define caching modes
  117. */
  118. const CACHING_OFF = 0;
  119. const CACHING_LIFETIME_CURRENT = 1;
  120. const CACHING_LIFETIME_SAVED = 2;
  121. /**
  122. * define constant for clearing cache files be saved expiration dates
  123. */
  124. const CLEAR_EXPIRED = - 1;
  125. /**
  126. * define compile check modes
  127. */
  128. const COMPILECHECK_OFF = 0;
  129. const COMPILECHECK_ON = 1;
  130. const COMPILECHECK_CACHEMISS = 2;
  131. /**
  132. * define debug modes
  133. */
  134. const DEBUG_OFF = 0;
  135. const DEBUG_ON = 1;
  136. const DEBUG_INDIVIDUAL = 2;
  137. /**
  138. * modes for handling of "<?php ... ?>" tags in templates.
  139. */
  140. const PHP_PASSTHRU = 0; //-> print tags as plain text
  141. const PHP_QUOTE = 1; //-> escape tags as entities
  142. const PHP_REMOVE = 2; //-> escape tags as entities
  143. const PHP_ALLOW = 3; //-> escape tags as entities
  144. /**
  145. * filter types
  146. */
  147. const FILTER_POST = 'post';
  148. const FILTER_PRE = 'pre';
  149. const FILTER_OUTPUT = 'output';
  150. const FILTER_VARIABLE = 'variable';
  151. /**
  152. * plugin types
  153. */
  154. const PLUGIN_FUNCTION = 'function';
  155. const PLUGIN_BLOCK = 'block';
  156. const PLUGIN_COMPILER = 'compiler';
  157. const PLUGIN_MODIFIER = 'modifier';
  158. const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  159. /**
  160. * Resource caching modes
  161. * (not used since 3.1.30)
  162. */
  163. const RESOURCE_CACHE_OFF = 0;
  164. const RESOURCE_CACHE_AUTOMATIC = 1; // cache template objects by rules
  165. const RESOURCE_CACHE_TEMPLATE = 2; // cache all template objects
  166. const RESOURCE_CACHE_ON = 4; // cache source and compiled resources
  167. /**#@-*/
  168. /**
  169. * assigned global tpl vars
  170. */
  171. public static $global_tpl_vars = array();
  172. /**
  173. * error handler returned by set_error_handler() in Smarty::muteExpectedErrors()
  174. */
  175. public static $_previous_error_handler = null;
  176. /**
  177. * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
  178. */
  179. public static $_muted_directories = array();
  180. /**
  181. * Flag denoting if Multibyte String functions are available
  182. */
  183. public static $_MBSTRING = SMARTY_MBSTRING;
  184. /**
  185. * The character set to adhere to (e.g. "UTF-8")
  186. */
  187. public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  188. /**
  189. * The date format to be used internally
  190. * (accepts date() and strftime())
  191. */
  192. public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  193. /**
  194. * Flag denoting if PCRE should run in UTF-8 mode
  195. */
  196. public static $_UTF8_MODIFIER = 'u';
  197. /**
  198. * Flag denoting if operating system is windows
  199. */
  200. public static $_IS_WINDOWS = false;
  201. /**#@+
  202. * variables
  203. */
  204. /**
  205. * auto literal on delimiters with whitespace
  206. *
  207. * @var boolean
  208. */
  209. public $auto_literal = true;
  210. /**
  211. * display error on not assigned variables
  212. *
  213. * @var boolean
  214. */
  215. public $error_unassigned = false;
  216. /**
  217. * look up relative file path in include_path
  218. *
  219. * @var boolean
  220. */
  221. public $use_include_path = false;
  222. /**
  223. * template directory
  224. *
  225. * @var array
  226. */
  227. protected $template_dir = array('./templates/');
  228. /**
  229. * flags for normalized template directory entries
  230. *
  231. * @var array
  232. */
  233. protected $_processedTemplateDir = array();
  234. /**
  235. * flag if template_dir is normalized
  236. *
  237. * @var bool
  238. */
  239. public $_templateDirNormalized = false;
  240. /**
  241. * joined template directory string used in cache keys
  242. *
  243. * @var string
  244. */
  245. public $_joined_template_dir = null;
  246. /**
  247. * config directory
  248. *
  249. * @var array
  250. */
  251. protected $config_dir = array('./configs/');
  252. /**
  253. * flags for normalized template directory entries
  254. *
  255. * @var array
  256. */
  257. protected $_processedConfigDir = array();
  258. /**
  259. * flag if config_dir is normalized
  260. *
  261. * @var bool
  262. */
  263. public $_configDirNormalized = false;
  264. /**
  265. * joined config directory string used in cache keys
  266. *
  267. * @var string
  268. */
  269. public $_joined_config_dir = null;
  270. /**
  271. * default template handler
  272. *
  273. * @var callable
  274. */
  275. public $default_template_handler_func = null;
  276. /**
  277. * default config handler
  278. *
  279. * @var callable
  280. */
  281. public $default_config_handler_func = null;
  282. /**
  283. * default plugin handler
  284. *
  285. * @var callable
  286. */
  287. public $default_plugin_handler_func = null;
  288. /**
  289. * compile directory
  290. *
  291. * @var string
  292. */
  293. protected $compile_dir = './templates_c/';
  294. /**
  295. * flag if template_dir is normalized
  296. *
  297. * @var bool
  298. */
  299. public $_compileDirNormalized = false;
  300. /**
  301. * plugins directory
  302. *
  303. * @var array
  304. */
  305. protected $plugins_dir = array();
  306. /**
  307. * flag if plugins_dir is normalized
  308. *
  309. * @var bool
  310. */
  311. public $_pluginsDirNormalized = false;
  312. /**
  313. * cache directory
  314. *
  315. * @var string
  316. */
  317. protected $cache_dir = './cache/';
  318. /**
  319. * flag if template_dir is normalized
  320. *
  321. * @var bool
  322. */
  323. public $_cacheDirNormalized = false;
  324. /**
  325. * force template compiling?
  326. *
  327. * @var boolean
  328. */
  329. public $force_compile = false;
  330. /**
  331. * check template for modifications?
  332. *
  333. * @var boolean
  334. */
  335. public $compile_check = true;
  336. /**
  337. * use sub dirs for compiled/cached files?
  338. *
  339. * @var boolean
  340. */
  341. public $use_sub_dirs = false;
  342. /**
  343. * allow ambiguous resources (that are made unique by the resource handler)
  344. *
  345. * @var boolean
  346. */
  347. public $allow_ambiguous_resources = false;
  348. /**
  349. * merge compiled includes
  350. *
  351. * @var boolean
  352. */
  353. public $merge_compiled_includes = false;
  354. /*
  355. * flag for behaviour when extends: resource and {extends} tag are used simultaneous
  356. * if false disable execution of {extends} in templates called by extends resource.
  357. * (behaviour as versions < 3.1.28)
  358. *
  359. * @var boolean
  360. */
  361. public $extends_recursion = true;
  362. /**
  363. * force cache file creation
  364. *
  365. * @var boolean
  366. */
  367. public $force_cache = false;
  368. /**
  369. * template left-delimiter
  370. *
  371. * @var string
  372. */
  373. public $left_delimiter = "{";
  374. /**
  375. * template right-delimiter
  376. *
  377. * @var string
  378. */
  379. public $right_delimiter = "}";
  380. /**#@+
  381. * security
  382. */
  383. /**
  384. * class name
  385. * This should be instance of Smarty_Security.
  386. *
  387. * @var string
  388. * @see Smarty_Security
  389. */
  390. public $security_class = 'Smarty_Security';
  391. /**
  392. * implementation of security class
  393. *
  394. * @var Smarty_Security
  395. */
  396. public $security_policy = null;
  397. /**
  398. * controls handling of PHP-blocks
  399. *
  400. * @var integer
  401. */
  402. public $php_handling = self::PHP_PASSTHRU;
  403. /**
  404. * controls if the php template file resource is allowed
  405. *
  406. * @var bool
  407. */
  408. public $allow_php_templates = false;
  409. /**#@-*/
  410. /**
  411. * debug mode
  412. * Setting this to true enables the debug-console.
  413. *
  414. * @var boolean
  415. */
  416. public $debugging = false;
  417. /**
  418. * This determines if debugging is enable-able from the browser.
  419. * <ul>
  420. * <li>NONE => no debugging control allowed</li>
  421. * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
  422. * </ul>
  423. *
  424. * @var string
  425. */
  426. public $debugging_ctrl = 'NONE';
  427. /**
  428. * Name of debugging URL-param.
  429. * Only used when $debugging_ctrl is set to 'URL'.
  430. * The name of the URL-parameter that activates debugging.
  431. *
  432. * @var string
  433. */
  434. public $smarty_debug_id = 'SMARTY_DEBUG';
  435. /**
  436. * Path of debug template.
  437. *
  438. * @var string
  439. */
  440. public $debug_tpl = null;
  441. /**
  442. * When set, smarty uses this value as error_reporting-level.
  443. *
  444. * @var int
  445. */
  446. public $error_reporting = null;
  447. /**#@+
  448. * config var settings
  449. */
  450. /**
  451. * Controls whether variables with the same name overwrite each other.
  452. *
  453. * @var boolean
  454. */
  455. public $config_overwrite = true;
  456. /**
  457. * Controls whether config values of on/true/yes and off/false/no get converted to boolean.
  458. *
  459. * @var boolean
  460. */
  461. public $config_booleanize = true;
  462. /**
  463. * Controls whether hidden config sections/vars are read from the file.
  464. *
  465. * @var boolean
  466. */
  467. public $config_read_hidden = false;
  468. /**#@-*/
  469. /**#@+
  470. * resource locking
  471. */
  472. /**
  473. * locking concurrent compiles
  474. *
  475. * @var boolean
  476. */
  477. public $compile_locking = true;
  478. /**
  479. * Controls whether cache resources should use locking mechanism
  480. *
  481. * @var boolean
  482. */
  483. public $cache_locking = false;
  484. /**
  485. * seconds to wait for acquiring a lock before ignoring the write lock
  486. *
  487. * @var float
  488. */
  489. public $locking_timeout = 10;
  490. /**#@-*/
  491. /**
  492. * resource type used if none given
  493. * Must be an valid key of $registered_resources.
  494. *
  495. * @var string
  496. */
  497. public $default_resource_type = 'file';
  498. /**
  499. * caching type
  500. * Must be an element of $cache_resource_types.
  501. *
  502. * @var string
  503. */
  504. public $caching_type = 'file';
  505. /**
  506. * config type
  507. *
  508. * @var string
  509. */
  510. public $default_config_type = 'file';
  511. /**
  512. * check If-Modified-Since headers
  513. *
  514. * @var boolean
  515. */
  516. public $cache_modified_check = false;
  517. /**
  518. * registered plugins
  519. *
  520. * @var array
  521. */
  522. public $registered_plugins = array();
  523. /**
  524. * registered objects
  525. *
  526. * @var array
  527. */
  528. public $registered_objects = array();
  529. /**
  530. * registered classes
  531. *
  532. * @var array
  533. */
  534. public $registered_classes = array();
  535. /**
  536. * registered filters
  537. *
  538. * @var array
  539. */
  540. public $registered_filters = array();
  541. /**
  542. * registered resources
  543. *
  544. * @var array
  545. */
  546. public $registered_resources = array();
  547. /**
  548. * registered cache resources
  549. *
  550. * @var array
  551. */
  552. public $registered_cache_resources = array();
  553. /**
  554. * autoload filter
  555. *
  556. * @var array
  557. */
  558. public $autoload_filters = array();
  559. /**
  560. * default modifier
  561. *
  562. * @var array
  563. */
  564. public $default_modifiers = array();
  565. /**
  566. * autoescape variable output
  567. *
  568. * @var boolean
  569. */
  570. public $escape_html = false;
  571. /**
  572. * start time for execution time calculation
  573. *
  574. * @var int
  575. */
  576. public $start_time = 0;
  577. /**
  578. * required by the compiler for BC
  579. *
  580. * @var string
  581. */
  582. public $_current_file = null;
  583. /**
  584. * internal flag to enable parser debugging
  585. *
  586. * @var bool
  587. */
  588. public $_parserdebug = false;
  589. /**
  590. * This object type (Smarty = 1, template = 2, data = 4)
  591. *
  592. * @var int
  593. */
  594. public $_objType = 1;
  595. /**
  596. * Debug object
  597. *
  598. * @var Smarty_Internal_Debug
  599. */
  600. public $_debug = null;
  601. /**
  602. * Directory separator
  603. *
  604. * @var string
  605. */
  606. public $ds = DIRECTORY_SEPARATOR;
  607. /**
  608. * removed properties
  609. *
  610. * @var string[]
  611. */
  612. protected $obsoleteProperties = array('resource_caching', 'template_resource_caching', 'direct_access_security',
  613. '_dir_perms', '_file_perms', 'plugin_search_order',
  614. 'inheritance_merge_compiled_includes', 'resource_cache_mode',);
  615. /**
  616. * List of private properties which will call getter/setter on a direct access
  617. *
  618. * @var string[]
  619. */
  620. protected $accessMap = array('template_dir' => 'TemplateDir', 'config_dir' => 'ConfigDir',
  621. 'plugins_dir' => 'PluginsDir', 'compile_dir' => 'CompileDir',
  622. 'cache_dir' => 'CacheDir',);
  623. /**#@-*/
  624. /**
  625. * Initialize new Smarty object
  626. */
  627. public function __construct()
  628. {
  629. $this->_clearTemplateCache();
  630. parent::__construct();
  631. if (is_callable('mb_internal_encoding')) {
  632. mb_internal_encoding(Smarty::$_CHARSET);
  633. }
  634. $this->start_time = microtime(true);
  635. if (isset($_SERVER[ 'SCRIPT_NAME' ])) {
  636. Smarty::$global_tpl_vars[ 'SCRIPT_NAME' ] = new Smarty_Variable($_SERVER[ 'SCRIPT_NAME' ]);
  637. }
  638. // Check if we're running on windows
  639. Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  640. // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
  641. if (Smarty::$_CHARSET !== 'UTF-8') {
  642. Smarty::$_UTF8_MODIFIER = '';
  643. }
  644. }
  645. /**
  646. * Check if a template resource exists
  647. *
  648. * @param string $resource_name template name
  649. *
  650. * @return boolean status
  651. */
  652. public function templateExists($resource_name)
  653. {
  654. // create source object
  655. $source = Smarty_Template_Source::load(null, $this, $resource_name);
  656. return $source->exists;
  657. }
  658. /**
  659. * Loads security class and enables security
  660. *
  661. * @param string|Smarty_Security $security_class if a string is used, it must be class-name
  662. *
  663. * @return Smarty current Smarty instance for chaining
  664. * @throws SmartyException when an invalid class name is provided
  665. */
  666. public function enableSecurity($security_class = null)
  667. {
  668. Smarty_Security::enableSecurity($this, $security_class);
  669. return $this;
  670. }
  671. /**
  672. * Disable security
  673. *
  674. * @return Smarty current Smarty instance for chaining
  675. */
  676. public function disableSecurity()
  677. {
  678. $this->security_policy = null;
  679. return $this;
  680. }
  681. /**
  682. * Set template directory
  683. *
  684. * @param string|array $template_dir directory(s) of template sources
  685. * @param bool $isConfig true for config_dir
  686. *
  687. * @return \Smarty current Smarty instance for chaining
  688. */
  689. public function setTemplateDir($template_dir, $isConfig = false)
  690. {
  691. if ($isConfig) {
  692. $this->config_dir = array();
  693. $this->_processedConfigDir = array();
  694. } else {
  695. $this->template_dir = array();
  696. $this->_processedTemplateDir = array();
  697. }
  698. $this->addTemplateDir($template_dir, null, $isConfig);
  699. return $this;
  700. }
  701. /**
  702. * Add template directory(s)
  703. *
  704. * @param string|array $template_dir directory(s) of template sources
  705. * @param string $key of the array element to assign the template dir to
  706. * @param bool $isConfig true for config_dir
  707. *
  708. * @return Smarty current Smarty instance for chaining
  709. */
  710. public function addTemplateDir($template_dir, $key = null, $isConfig = false)
  711. {
  712. if ($isConfig) {
  713. $processed = &$this->_processedConfigDir;
  714. $dir = &$this->config_dir;
  715. $this->_configDirNormalized = false;
  716. } else {
  717. $processed = &$this->_processedTemplateDir;
  718. $dir = &$this->template_dir;
  719. $this->_templateDirNormalized = false;
  720. }
  721. if (is_array($template_dir)) {
  722. foreach ($template_dir as $k => $v) {
  723. if (is_int($k)) {
  724. // indexes are not merged but appended
  725. $dir[] = $v;
  726. } else {
  727. // string indexes are overridden
  728. $dir[ $k ] = $v;
  729. unset($processed[ $key ]);
  730. }
  731. }
  732. } else {
  733. if ($key !== null) {
  734. // override directory at specified index
  735. $dir[ $key ] = $template_dir;
  736. unset($processed[ $key ]);
  737. } else {
  738. // append new directory
  739. $dir[] = $template_dir;
  740. }
  741. }
  742. return $this;
  743. }
  744. /**
  745. * Get template directories
  746. *
  747. * @param mixed $index index of directory to get, null to get all
  748. * @param bool $isConfig true for config_dir
  749. *
  750. * @return array list of template directories, or directory of $index
  751. */
  752. public function getTemplateDir($index = null, $isConfig = false)
  753. {
  754. if ($isConfig) {
  755. $dir = &$this->config_dir;
  756. } else {
  757. $dir = &$this->template_dir;
  758. }
  759. if ($isConfig ? !$this->_configDirNormalized : !$this->_templateDirNormalized) {
  760. $this->_nomalizeTemplateConfig($isConfig);
  761. }
  762. if ($index !== null) {
  763. return isset($dir[ $index ]) ? $dir[ $index ] : null;
  764. }
  765. return $dir;
  766. }
  767. /**
  768. * Set config directory
  769. *
  770. * @param $config_dir
  771. *
  772. * @return Smarty current Smarty instance for chaining
  773. */
  774. public function setConfigDir($config_dir)
  775. {
  776. return $this->setTemplateDir($config_dir, true);
  777. }
  778. /**
  779. * Add config directory(s)
  780. *
  781. * @param string|array $config_dir directory(s) of config sources
  782. * @param mixed $key key of the array element to assign the config dir to
  783. *
  784. * @return Smarty current Smarty instance for chaining
  785. */
  786. public function addConfigDir($config_dir, $key = null)
  787. {
  788. return $this->addTemplateDir($config_dir, $key, true);
  789. }
  790. /**
  791. * Get config directory
  792. *
  793. * @param mixed $index index of directory to get, null to get all
  794. *
  795. * @return array configuration directory
  796. */
  797. public function getConfigDir($index = null)
  798. {
  799. return $this->getTemplateDir($index, true);
  800. }
  801. /**
  802. * Set plugins directory
  803. *
  804. * @param string|array $plugins_dir directory(s) of plugins
  805. *
  806. * @return Smarty current Smarty instance for chaining
  807. */
  808. public function setPluginsDir($plugins_dir)
  809. {
  810. $this->plugins_dir = (array) $plugins_dir;
  811. $this->_pluginsDirNormalized = false;
  812. return $this;
  813. }
  814. /**
  815. * Adds directory of plugin files
  816. *
  817. * @param null|array|string $plugins_dir
  818. *
  819. * @return Smarty current Smarty instance for chaining
  820. */
  821. public function addPluginsDir($plugins_dir)
  822. {
  823. if (empty($this->plugins_dir)) {
  824. $this->plugins_dir[] = SMARTY_PLUGINS_DIR;
  825. }
  826. $this->plugins_dir = array_merge($this->plugins_dir, (array) $plugins_dir);
  827. $this->_pluginsDirNormalized = false;
  828. return $this;
  829. }
  830. /**
  831. * Get plugin directories
  832. *
  833. * @return array list of plugin directories
  834. */
  835. public function getPluginsDir()
  836. {
  837. if (empty($this->plugins_dir)) {
  838. $this->plugins_dir[] = SMARTY_PLUGINS_DIR;
  839. $this->_pluginsDirNormalized = false;
  840. }
  841. if (!$this->_pluginsDirNormalized) {
  842. if (!is_array($this->plugins_dir)) {
  843. $this->plugins_dir = (array) $this->plugins_dir;
  844. }
  845. foreach ($this->plugins_dir as $k => $v) {
  846. $this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
  847. }
  848. $this->_cache[ 'plugin_files' ] = array();
  849. $this->_pluginsDirNormalized = true;
  850. }
  851. return $this->plugins_dir;
  852. }
  853. /**
  854. *
  855. * @param string $compile_dir directory to store compiled templates in
  856. *
  857. * @return Smarty current Smarty instance for chaining
  858. */
  859. public function setCompileDir($compile_dir)
  860. {
  861. $this->_normalizeDir('compile_dir', $compile_dir);
  862. $this->_compileDirNormalized = true;
  863. return $this;
  864. }
  865. /**
  866. * Get compiled directory
  867. *
  868. * @return string path to compiled templates
  869. */
  870. public function getCompileDir()
  871. {
  872. if (!$this->_compileDirNormalized) {
  873. $this->_normalizeDir('compile_dir', $this->compile_dir);
  874. $this->_compileDirNormalized = true;
  875. }
  876. return $this->compile_dir;
  877. }
  878. /**
  879. * Set cache directory
  880. *
  881. * @param string $cache_dir directory to store cached templates in
  882. *
  883. * @return Smarty current Smarty instance for chaining
  884. */
  885. public function setCacheDir($cache_dir)
  886. {
  887. $this->_normalizeDir('cache_dir', $cache_dir);
  888. $this->_cacheDirNormalized = true;
  889. return $this;
  890. }
  891. /**
  892. * Get cache directory
  893. *
  894. * @return string path of cache directory
  895. */
  896. public function getCacheDir()
  897. {
  898. if (!$this->_cacheDirNormalized) {
  899. $this->_normalizeDir('cache_dir', $this->cache_dir);
  900. $this->_cacheDirNormalized = true;
  901. }
  902. return $this->cache_dir;
  903. }
  904. /**
  905. * Normalize and set directory string
  906. *
  907. * @param string $dirName cache_dir or compile_dir
  908. * @param string $dir filepath of folder
  909. */
  910. private function _normalizeDir($dirName, $dir)
  911. {
  912. $this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . $this->ds, true);
  913. if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) {
  914. Smarty::$_muted_directories[ $this->{$dirName} ] = null;
  915. }
  916. }
  917. /**
  918. * Normalize template_dir or config_dir
  919. *
  920. * @param bool $isConfig true for config_dir
  921. *
  922. */
  923. private function _nomalizeTemplateConfig($isConfig)
  924. {
  925. if ($isConfig) {
  926. $processed = &$this->_processedConfigDir;
  927. $dir = &$this->config_dir;
  928. } else {
  929. $processed = &$this->_processedTemplateDir;
  930. $dir = &$this->template_dir;
  931. }
  932. if (!is_array($dir)) {
  933. $dir = (array) $dir;
  934. }
  935. foreach ($dir as $k => $v) {
  936. if (!isset($processed[ $k ])) {
  937. $dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
  938. $processed[ $k ] = true;
  939. }
  940. }
  941. $isConfig ? $this->_configDirNormalized = true : $this->_templateDirNormalized = true;
  942. $isConfig ? $this->_joined_config_dir = join('#', $this->config_dir) :
  943. $this->_joined_template_dir = join('#', $this->template_dir);
  944. }
  945. /**
  946. * creates a template object
  947. *
  948. * @param string $template the resource handle of the template file
  949. * @param mixed $cache_id cache id to be used with this template
  950. * @param mixed $compile_id compile id to be used with this template
  951. * @param object $parent next higher level of Smarty variables
  952. * @param boolean $do_clone flag is Smarty object shall be cloned
  953. *
  954. * @return object template object
  955. */
  956. public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
  957. {
  958. if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) {
  959. $parent = $cache_id;
  960. $cache_id = null;
  961. }
  962. if ($parent !== null && is_array($parent)) {
  963. $data = $parent;
  964. $parent = null;
  965. } else {
  966. $data = null;
  967. }
  968. if (!$this->_templateDirNormalized) {
  969. $this->_nomalizeTemplateConfig(false);
  970. }
  971. $_templateId = $this->_getTemplateId($template, $cache_id, $compile_id);
  972. $tpl = null;
  973. if ($this->caching && isset(Smarty_Internal_Template::$isCacheTplObj[ $_templateId ])) {
  974. $tpl = $do_clone ? clone Smarty_Internal_Template::$isCacheTplObj[ $_templateId ] :
  975. Smarty_Internal_Template::$isCacheTplObj[ $_templateId ];
  976. $tpl->inheritance = null;
  977. $tpl->tpl_vars = $tpl->config_vars = array();
  978. } else if (!$do_clone && isset(Smarty_Internal_Template::$tplObjCache[ $_templateId ])) {
  979. $tpl = clone Smarty_Internal_Template::$tplObjCache[ $_templateId ];
  980. $tpl->inheritance = null;
  981. $tpl->tpl_vars = $tpl->config_vars = array();
  982. } else {
  983. /* @var Smarty_Internal_Template $tpl */
  984. $tpl = new $this->template_class($template, $this, null, $cache_id, $compile_id, null, null);
  985. $tpl->templateId = $_templateId;
  986. }
  987. if ($do_clone) {
  988. $tpl->smarty = clone $tpl->smarty;
  989. }
  990. $tpl->parent = $parent ? $parent : $this;
  991. // fill data if present
  992. if (!empty($data) && is_array($data)) {
  993. // set up variable values
  994. foreach ($data as $_key => $_val) {
  995. $tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val);
  996. }
  997. }
  998. if ($this->debugging || $this->debugging_ctrl == 'URL') {
  999. $tpl->smarty->_debug = new Smarty_Internal_Debug();
  1000. // check URL debugging control
  1001. if (!$this->debugging && $this->debugging_ctrl == 'URL') {
  1002. $tpl->smarty->_debug->debugUrl($tpl->smarty);
  1003. }
  1004. }
  1005. return $tpl;
  1006. }
  1007. /**
  1008. * Takes unknown classes and loads plugin files for them
  1009. * class name format: Smarty_PluginType_PluginName
  1010. * plugin filename format: plugintype.pluginname.php
  1011. *
  1012. * @param string $plugin_name class plugin name to load
  1013. * @param bool $check check if already loaded
  1014. *
  1015. * @throws SmartyException
  1016. * @return string |boolean filepath of loaded file or false
  1017. */
  1018. public function loadPlugin($plugin_name, $check = true)
  1019. {
  1020. return $this->ext->loadPlugin->loadPlugin($this, $plugin_name, $check);
  1021. }
  1022. /**
  1023. * Get unique template id
  1024. *
  1025. * @param string $template_name
  1026. * @param null|mixed $cache_id
  1027. * @param null|mixed $compile_id
  1028. * @param null $caching
  1029. * @param \Smarty_Internal_Template $template
  1030. *
  1031. * @return string
  1032. */
  1033. public function _getTemplateId($template_name, $cache_id = null, $compile_id = null, $caching = null,
  1034. Smarty_Internal_Template $template = null)
  1035. {
  1036. $template_name = (strpos($template_name, ':') === false) ? "{$this->default_resource_type}:{$template_name}" :
  1037. $template_name;
  1038. $cache_id = $cache_id === null ? $this->cache_id : $cache_id;
  1039. $compile_id = $compile_id === null ? $this->compile_id : $compile_id;
  1040. $caching = (int) ($caching === null ? $this->caching : $caching);
  1041. if ((isset($template) && strpos($template_name, ':.') !== false) || $this->allow_ambiguous_resources) {
  1042. $_templateId =
  1043. Smarty_Resource::getUniqueTemplateName((isset($template) ? $template : $this), $template_name) .
  1044. "#{$cache_id}#{$compile_id}#{$caching}";
  1045. } else {
  1046. $_templateId = $this->_joined_template_dir . "#{$template_name}#{$cache_id}#{$compile_id}#{$caching}";
  1047. }
  1048. if (isset($_templateId[ 150 ])) {
  1049. $_templateId = sha1($_templateId);
  1050. }
  1051. return $_templateId;
  1052. }
  1053. /**
  1054. * Normalize path
  1055. * - remove /./ and /../
  1056. * - make it absolute if required
  1057. *
  1058. * @param string $path file path
  1059. * @param bool $realpath if true - convert to absolute
  1060. * false - convert to relative
  1061. * null - keep as it is but remove /./ /../
  1062. *
  1063. * @return string
  1064. */
  1065. public function _realpath($path, $realpath = null)
  1066. {
  1067. $nds = $this->ds == '/' ? '\\' : '/';
  1068. // normalize $this->ds
  1069. $path = str_replace($nds, $this->ds, $path);
  1070. preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(?:[[:print:]]*))$%',
  1071. $path, $parts);
  1072. $path = $parts[ 'path' ];
  1073. if ($parts[ 'root' ] == '\\') {
  1074. $parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ];
  1075. } else {
  1076. if ($realpath !== null && !$parts[ 'root' ]) {
  1077. $path = getcwd() . $this->ds . $path;
  1078. }
  1079. }
  1080. // remove noop 'DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR' and 'DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR' patterns
  1081. $path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#', $this->ds, $path);
  1082. // resolve '..DIRECTORY_SEPARATOR' pattern, smallest first
  1083. if (strpos($path, '..' . $this->ds) != false &&
  1084. preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#', $path, $match)
  1085. ) {
  1086. $counts = array();
  1087. foreach ($match[ 0 ] as $m) {
  1088. $counts[] = (int) ((strlen($m) - 1) / 3);
  1089. }
  1090. sort($counts);
  1091. foreach ($counts as $count) {
  1092. $path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count .
  1093. '}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#',
  1094. $this->ds, $path);
  1095. }
  1096. }
  1097. return $parts[ 'root' ] . $path;
  1098. }
  1099. /**
  1100. * Empty template objects cache
  1101. */
  1102. public function _clearTemplateCache()
  1103. {
  1104. Smarty_Internal_Template::$isCacheTplObj = array();
  1105. Smarty_Internal_Template::$tplObjCache = array();
  1106. }
  1107. /**
  1108. * Get Smarty object
  1109. *
  1110. * @return Smarty
  1111. */
  1112. public function _getSmartyObj()
  1113. {
  1114. return $this;
  1115. }
  1116. /**
  1117. * @param boolean $compile_check
  1118. */
  1119. public function setCompileCheck($compile_check)
  1120. {
  1121. $this->compile_check = $compile_check;
  1122. }
  1123. /**
  1124. * @param boolean $use_sub_dirs
  1125. */
  1126. public function setUseSubDirs($use_sub_dirs)
  1127. {
  1128. $this->use_sub_dirs = $use_sub_dirs;
  1129. }
  1130. /**
  1131. * @param int $error_reporting
  1132. */
  1133. public function setErrorReporting($error_reporting)
  1134. {
  1135. $this->error_reporting = $error_reporting;
  1136. }
  1137. /**
  1138. * @param boolean $escape_html
  1139. */
  1140. public function setEscapeHtml($escape_html)
  1141. {
  1142. $this->escape_html = $escape_html;
  1143. }
  1144. /**
  1145. * @param boolean $auto_literal
  1146. */
  1147. public function setAutoLiteral($auto_literal)
  1148. {
  1149. $this->auto_literal = $auto_literal;
  1150. }
  1151. /**
  1152. * @param boolean $force_compile
  1153. */
  1154. public function setForceCompile($force_compile)
  1155. {
  1156. $this->force_compile = $force_compile;
  1157. }
  1158. /**
  1159. * @param boolean $merge_compiled_includes
  1160. */
  1161. public function setMergeCompiledIncludes($merge_compiled_includes)
  1162. {
  1163. $this->merge_compiled_includes = $merge_compiled_includes;
  1164. }
  1165. /**
  1166. * @param string $left_delimiter
  1167. */
  1168. public function setLeftDelimiter($left_delimiter)
  1169. {
  1170. $this->left_delimiter = $left_delimiter;
  1171. }
  1172. /**
  1173. * @param string $right_delimiter
  1174. */
  1175. public function setRightDelimiter($right_delimiter)
  1176. {
  1177. $this->right_delimiter = $right_delimiter;
  1178. }
  1179. /**
  1180. * @param boolean $debugging
  1181. */
  1182. public function setDebugging($debugging)
  1183. {
  1184. $this->debugging = $debugging;
  1185. }
  1186. /**
  1187. * @param boolean $config_overwrite
  1188. */
  1189. public function setConfigOverwrite($config_overwrite)
  1190. {
  1191. $this->config_overwrite = $config_overwrite;
  1192. }
  1193. /**
  1194. * @param boolean $config_booleanize
  1195. */
  1196. public function setConfigBooleanize($config_booleanize)
  1197. {
  1198. $this->config_booleanize = $config_booleanize;
  1199. }
  1200. /**
  1201. * @param boolean $config_read_hidden
  1202. */
  1203. public function setConfigReadHidden($config_read_hidden)
  1204. {
  1205. $this->config_read_hidden = $config_read_hidden;
  1206. }
  1207. /**
  1208. * @param boolean $compile_locking
  1209. */
  1210. public function setCompileLocking($compile_locking)
  1211. {
  1212. $this->compile_locking = $compile_locking;
  1213. }
  1214. /**
  1215. * @param string $default_resource_type
  1216. */
  1217. public function setDefaultResourceType($default_resource_type)
  1218. {
  1219. $this->default_resource_type = $default_resource_type;
  1220. }
  1221. /**
  1222. * @param string $caching_type
  1223. */
  1224. public function setCachingType($caching_type)
  1225. {
  1226. $this->caching_type = $caching_type;
  1227. }
  1228. /**
  1229. * Test install
  1230. *
  1231. * @param null $errors
  1232. */
  1233. public function testInstall(&$errors = null)
  1234. {
  1235. Smarty_Internal_TestInstall::testInstall($this, $errors);
  1236. }
  1237. /**
  1238. * <<magic>> Generic getter.
  1239. * Calls the appropriate getter function.
  1240. * Issues an E_USER_NOTICE if no valid getter is found.
  1241. *
  1242. * @param string $name property name
  1243. *
  1244. * @return mixed
  1245. */
  1246. public function __get($name)
  1247. {
  1248. if (isset($this->accessMap[ $name ])) {
  1249. $method = 'get' . $this->accessMap[ $name ];
  1250. return $this->{$method}();
  1251. } elseif (isset($this->_cache[ $name ])) {
  1252. return $this->_cache[ $name ];
  1253. } elseif (in_array($name, $this->obsoleteProperties)) {
  1254. return null;
  1255. } else {
  1256. trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
  1257. }
  1258. return null;
  1259. }
  1260. /**
  1261. * <<magic>> Generic setter.
  1262. * Calls the appropriate setter function.
  1263. * Issues an E_USER_NOTICE if no valid setter is found.
  1264. *
  1265. * @param string $name property name
  1266. * @param mixed $value parameter passed to setter
  1267. */
  1268. public function __set($name, $value)
  1269. {
  1270. if (isset($this->accessMap[ $name ])) {
  1271. $method = 'set' . $this->accessMap[ $name ];
  1272. $this->{$method}($value);
  1273. } elseif (in_array($name, $this->obsoleteProperties)) {
  1274. return;
  1275. } else {
  1276. if (is_object($value) && method_exists($value, $name)) {
  1277. $this->$name = $value;
  1278. } else {
  1279. trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
  1280. }
  1281. }
  1282. }
  1283. /**
  1284. * Error Handler to mute expected messages
  1285. *
  1286. * @link http://php.net/set_error_handler
  1287. *
  1288. * @param integer $errno Error level
  1289. * @param $errstr
  1290. * @param $errfile
  1291. * @param $errline
  1292. * @param $errcontext
  1293. *
  1294. * @return bool|void
  1295. */
  1296. public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  1297. {
  1298. $_is_muted_directory = false;
  1299. // add the SMARTY_DIR to the list of muted directories
  1300. if (!isset(Smarty::$_muted_directories[ SMARTY_DIR ])) {
  1301. $smarty_dir = realpath(SMARTY_DIR);
  1302. if ($smarty_dir !== false) {
  1303. Smarty::$_muted_directories[ SMARTY_DIR ] =
  1304. array('file' => $smarty_dir, 'length' => strlen($smarty_dir),);
  1305. }
  1306. }
  1307. // walk the muted directories and test against $errfile
  1308. foreach (Smarty::$_muted_directories as $key => &$dir) {
  1309. if (!$dir) {
  1310. // resolve directory and length for speedy comparisons
  1311. $file = realpath($key);
  1312. if ($file === false) {
  1313. // this directory does not exist, remove and skip it
  1314. unset(Smarty::$_muted_directories[ $key ]);
  1315. continue;
  1316. }
  1317. $dir = array('file' => $file, 'length' => strlen($file),);
  1318. }
  1319. if (!strncmp($errfile, $dir[ 'file' ], $dir[ 'length' ])) {
  1320. $_is_muted_directory = true;
  1321. break;
  1322. }
  1323. }
  1324. // pass to next error handler if this error did not occur inside SMARTY_DIR
  1325. // or the error was within smarty but masked to be ignored
  1326. if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
  1327. if (Smarty::$_previous_error_handler) {
  1328. return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline,
  1329. $errcontext);
  1330. } else {
  1331. return false;
  1332. }
  1333. }
  1334. return;
  1335. }
  1336. /**
  1337. * Enable error handler to mute expected messages
  1338. *
  1339. * @return void
  1340. */
  1341. public static function muteExpectedErrors()
  1342. {
  1343. /*
  1344. error muting is done because some people implemented custom error_handlers using
  1345. http://php.net/set_error_handler and for some reason did not understand the following paragraph:
  1346. It is important to remember that the standard PHP error handler is completely bypassed for the
  1347. error types specified by error_types unless the callback function returns FALSE.
  1348. error_reporting() settings will have no effect and your error handler will be called regardless -
  1349. however you are still able to read the current value of error_reporting and act appropriately.
  1350. Of particular note is that this value will be 0 if the statement that caused the error was
  1351. prepended by the @ error-control operator.
  1352. Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
  1353. - @filemtime() is almost twice as fast as using an additional file_exists()
  1354. - between file_exists() and filemtime() a possible race condition is opened,
  1355. which does not exist using the simple @filemtime() approach.
  1356. */
  1357. $error_handler = array('Smarty', 'mutingErrorHandler');
  1358. $previous = set_error_handler($error_handler);
  1359. // avoid dead loops
  1360. if ($previous !== $error_handler) {
  1361. Smarty::$_previous_error_handler = $previous;
  1362. }
  1363. }
  1364. /**
  1365. * Disable error handler muting expected messages
  1366. *
  1367. * @return void
  1368. */
  1369. public static function unmuteExpectedErrors()
  1370. {
  1371. restore_error_handler();
  1372. }
  1373. }