Smarty.class.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: Smarty.class.php
  5. * SVN: $Id: Smarty.class.php 4897 2014-10-14 22:29:58Z Uwe.Tews@googlemail.com $
  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. * @version 3.1.21
  28. */
  29. /**
  30. * define shorthand directory separator constant
  31. */
  32. if (!defined('DS')) {
  33. define('DS', DIRECTORY_SEPARATOR);
  34. }
  35. /**
  36. * set SMARTY_DIR to absolute path to Smarty library files.
  37. * Sets SMARTY_DIR only if user application has not already defined it.
  38. */
  39. if (!defined('SMARTY_DIR')) {
  40. define('SMARTY_DIR', dirname(__FILE__) . DS);
  41. }
  42. /**
  43. * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
  44. * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
  45. */
  46. if (!defined('SMARTY_SYSPLUGINS_DIR')) {
  47. define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
  48. }
  49. if (!defined('SMARTY_PLUGINS_DIR')) {
  50. define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
  51. }
  52. if (!defined('SMARTY_MBSTRING')) {
  53. define('SMARTY_MBSTRING', function_exists('mb_split'));
  54. }
  55. if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
  56. // UTF-8 can only be done properly when mbstring is available!
  57. /**
  58. * @deprecated in favor of Smarty::$_CHARSET
  59. */
  60. define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ? 'UTF-8' : 'ISO-8859-1');
  61. }
  62. if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
  63. /**
  64. * @deprecated in favor of Smarty::$_DATE_FORMAT
  65. */
  66. define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
  67. }
  68. /**
  69. * register the class autoloader
  70. */
  71. if (!defined('SMARTY_SPL_AUTOLOAD')) {
  72. define('SMARTY_SPL_AUTOLOAD', 0);
  73. }
  74. if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
  75. $registeredAutoLoadFunctions = spl_autoload_functions();
  76. if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
  77. spl_autoload_register();
  78. }
  79. } else {
  80. spl_autoload_register('smartyAutoload');
  81. }
  82. /**
  83. * Load always needed external class files
  84. */
  85. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php';
  86. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_templatebase.php';
  87. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_template.php';
  88. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_resource.php';
  89. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_resource_file.php';
  90. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  91. include_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_cacheresource_file.php';
  92. /**
  93. * This is the main Smarty class
  94. *
  95. * @package Smarty
  96. */
  97. class Smarty extends Smarty_Internal_TemplateBase
  98. {
  99. /**#@+
  100. * constant definitions
  101. */
  102. /**
  103. * smarty version
  104. */
  105. const SMARTY_VERSION = 'Smarty-3.1.21-dev';
  106. /**
  107. * define variable scopes
  108. */
  109. const SCOPE_LOCAL = 0;
  110. const SCOPE_PARENT = 1;
  111. const SCOPE_ROOT = 2;
  112. const SCOPE_GLOBAL = 3;
  113. /**
  114. * define caching modes
  115. */
  116. const CACHING_OFF = 0;
  117. const CACHING_LIFETIME_CURRENT = 1;
  118. const CACHING_LIFETIME_SAVED = 2;
  119. /**
  120. * define constant for clearing cache files be saved expiration datees
  121. */
  122. const CLEAR_EXPIRED = - 1;
  123. /**
  124. * define compile check modes
  125. */
  126. const COMPILECHECK_OFF = 0;
  127. const COMPILECHECK_ON = 1;
  128. const COMPILECHECK_CACHEMISS = 2;
  129. /**
  130. * modes for handling of "<?php ... ?>" tags in templates.
  131. */
  132. const PHP_PASSTHRU = 0; //-> print tags as plain text
  133. const PHP_QUOTE = 1; //-> escape tags as entities
  134. const PHP_REMOVE = 2; //-> escape tags as entities
  135. const PHP_ALLOW = 3; //-> escape tags as entities
  136. /**
  137. * filter types
  138. */
  139. const FILTER_POST = 'post';
  140. const FILTER_PRE = 'pre';
  141. const FILTER_OUTPUT = 'output';
  142. const FILTER_VARIABLE = 'variable';
  143. /**
  144. * plugin types
  145. */
  146. const PLUGIN_FUNCTION = 'function';
  147. const PLUGIN_BLOCK = 'block';
  148. const PLUGIN_COMPILER = 'compiler';
  149. const PLUGIN_MODIFIER = 'modifier';
  150. const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  151. /**#@-*/
  152. /**
  153. * assigned global tpl vars
  154. */
  155. public static $global_tpl_vars = array();
  156. /**
  157. * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
  158. */
  159. public static $_previous_error_handler = null;
  160. /**
  161. * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
  162. */
  163. public static $_muted_directories = array();
  164. /**
  165. * Flag denoting if Multibyte String functions are available
  166. */
  167. public static $_MBSTRING = SMARTY_MBSTRING;
  168. /**
  169. * The character set to adhere to (e.g. "UTF-8")
  170. */
  171. public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  172. /**
  173. * The date format to be used internally
  174. * (accepts date() and strftime())
  175. */
  176. public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  177. /**
  178. * Flag denoting if PCRE should run in UTF-8 mode
  179. */
  180. public static $_UTF8_MODIFIER = 'u';
  181. /**
  182. * Flag denoting if operating system is windows
  183. */
  184. public static $_IS_WINDOWS = false;
  185. /**#@+
  186. * variables
  187. */
  188. /**
  189. * auto literal on delimiters with whitspace
  190. *
  191. * @var boolean
  192. */
  193. public $auto_literal = true;
  194. /**
  195. * display error on not assigned variables
  196. *
  197. * @var boolean
  198. */
  199. public $error_unassigned = false;
  200. /**
  201. * look up relative filepaths in include_path
  202. *
  203. * @var boolean
  204. */
  205. public $use_include_path = false;
  206. /**
  207. * template directory
  208. *
  209. * @var array
  210. */
  211. private $template_dir = array();
  212. /**
  213. * joined template directory string used in cache keys
  214. *
  215. * @var string
  216. */
  217. public $joined_template_dir = null;
  218. /**
  219. * joined config directory string used in cache keys
  220. *
  221. * @var string
  222. */
  223. public $joined_config_dir = null;
  224. /**
  225. * default template handler
  226. *
  227. * @var callable
  228. */
  229. public $default_template_handler_func = null;
  230. /**
  231. * default config handler
  232. *
  233. * @var callable
  234. */
  235. public $default_config_handler_func = null;
  236. /**
  237. * default plugin handler
  238. *
  239. * @var callable
  240. */
  241. public $default_plugin_handler_func = null;
  242. /**
  243. * compile directory
  244. *
  245. * @var string
  246. */
  247. private $compile_dir = null;
  248. /**
  249. * plugins directory
  250. *
  251. * @var array
  252. */
  253. private $plugins_dir = array();
  254. /**
  255. * cache directory
  256. *
  257. * @var string
  258. */
  259. private $cache_dir = null;
  260. /**
  261. * config directory
  262. *
  263. * @var array
  264. */
  265. private $config_dir = array();
  266. /**
  267. * force template compiling?
  268. *
  269. * @var boolean
  270. */
  271. public $force_compile = false;
  272. /**
  273. * check template for modifications?
  274. *
  275. * @var boolean
  276. */
  277. public $compile_check = true;
  278. /**
  279. * use sub dirs for compiled/cached files?
  280. *
  281. * @var boolean
  282. */
  283. public $use_sub_dirs = false;
  284. /**
  285. * allow ambiguous resources (that are made unique by the resource handler)
  286. *
  287. * @var boolean
  288. */
  289. public $allow_ambiguous_resources = false;
  290. /**
  291. * caching enabled
  292. *
  293. * @var boolean
  294. */
  295. public $caching = false;
  296. /**
  297. * merge compiled includes
  298. *
  299. * @var boolean
  300. */
  301. public $merge_compiled_includes = false;
  302. /**
  303. * template inheritance merge compiled includes
  304. *
  305. * @var boolean
  306. */
  307. public $inheritance_merge_compiled_includes = true;
  308. /**
  309. * cache lifetime in seconds
  310. *
  311. * @var integer
  312. */
  313. public $cache_lifetime = 3600;
  314. /**
  315. * force cache file creation
  316. *
  317. * @var boolean
  318. */
  319. public $force_cache = false;
  320. /**
  321. * Set this if you want different sets of cache files for the same
  322. * templates.
  323. *
  324. * @var string
  325. */
  326. public $cache_id = null;
  327. /**
  328. * Set this if you want different sets of compiled files for the same
  329. * templates.
  330. *
  331. * @var string
  332. */
  333. public $compile_id = null;
  334. /**
  335. * template left-delimiter
  336. *
  337. * @var string
  338. */
  339. public $left_delimiter = "{";
  340. /**
  341. * template right-delimiter
  342. *
  343. * @var string
  344. */
  345. public $right_delimiter = "}";
  346. /**#@+
  347. * security
  348. */
  349. /**
  350. * class name
  351. * This should be instance of Smarty_Security.
  352. *
  353. * @var string
  354. * @see Smarty_Security
  355. */
  356. public $security_class = 'Smarty_Security';
  357. /**
  358. * implementation of security class
  359. *
  360. * @var Smarty_Security
  361. */
  362. public $security_policy = null;
  363. /**
  364. * controls handling of PHP-blocks
  365. *
  366. * @var integer
  367. */
  368. public $php_handling = self::PHP_PASSTHRU;
  369. /**
  370. * controls if the php template file resource is allowed
  371. *
  372. * @var bool
  373. */
  374. public $allow_php_templates = false;
  375. /**
  376. * Should compiled-templates be prevented from being called directly?
  377. * {@internal
  378. * Currently used by Smarty_Internal_Template only.
  379. * }}
  380. *
  381. * @var boolean
  382. */
  383. public $direct_access_security = true;
  384. /**#@-*/
  385. /**
  386. * debug mode
  387. * Setting this to true enables the debug-console.
  388. *
  389. * @var boolean
  390. */
  391. public $debugging = false;
  392. /**
  393. * This determines if debugging is enable-able from the browser.
  394. * <ul>
  395. * <li>NONE => no debugging control allowed</li>
  396. * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
  397. * </ul>
  398. *
  399. * @var string
  400. */
  401. public $debugging_ctrl = 'NONE';
  402. /**
  403. * Name of debugging URL-param.
  404. * Only used when $debugging_ctrl is set to 'URL'.
  405. * The name of the URL-parameter that activates debugging.
  406. *
  407. * @var type
  408. */
  409. public $smarty_debug_id = 'SMARTY_DEBUG';
  410. /**
  411. * Path of debug template.
  412. *
  413. * @var string
  414. */
  415. public $debug_tpl = null;
  416. /**
  417. * When set, smarty uses this value as error_reporting-level.
  418. *
  419. * @var int
  420. */
  421. public $error_reporting = null;
  422. /**
  423. * Internal flag for getTags()
  424. *
  425. * @var boolean
  426. */
  427. public $get_used_tags = false;
  428. /**#@+
  429. * config var settings
  430. */
  431. /**
  432. * Controls whether variables with the same name overwrite each other.
  433. *
  434. * @var boolean
  435. */
  436. public $config_overwrite = true;
  437. /**
  438. * Controls whether config values of on/true/yes and off/false/no get converted to boolean.
  439. *
  440. * @var boolean
  441. */
  442. public $config_booleanize = true;
  443. /**
  444. * Controls whether hidden config sections/vars are read from the file.
  445. *
  446. * @var boolean
  447. */
  448. public $config_read_hidden = false;
  449. /**#@-*/
  450. /**#@+
  451. * resource locking
  452. */
  453. /**
  454. * locking concurrent compiles
  455. *
  456. * @var boolean
  457. */
  458. public $compile_locking = true;
  459. /**
  460. * Controls whether cache resources should emply locking mechanism
  461. *
  462. * @var boolean
  463. */
  464. public $cache_locking = false;
  465. /**
  466. * seconds to wait for acquiring a lock before ignoring the write lock
  467. *
  468. * @var float
  469. */
  470. public $locking_timeout = 10;
  471. /**#@-*/
  472. /**
  473. * global template functions
  474. *
  475. * @var array
  476. */
  477. public $template_functions = array();
  478. /**
  479. * resource type used if none given
  480. * Must be an valid key of $registered_resources.
  481. *
  482. * @var string
  483. */
  484. public $default_resource_type = 'file';
  485. /**
  486. * caching type
  487. * Must be an element of $cache_resource_types.
  488. *
  489. * @var string
  490. */
  491. public $caching_type = 'file';
  492. /**
  493. * internal config properties
  494. *
  495. * @var array
  496. */
  497. public $properties = array();
  498. /**
  499. * config type
  500. *
  501. * @var string
  502. */
  503. public $default_config_type = 'file';
  504. /**
  505. * cached template objects
  506. *
  507. * @var array
  508. */
  509. public $template_objects = array();
  510. /**
  511. * check If-Modified-Since headers
  512. *
  513. * @var boolean
  514. */
  515. public $cache_modified_check = false;
  516. /**
  517. * registered plugins
  518. *
  519. * @var array
  520. */
  521. public $registered_plugins = array();
  522. /**
  523. * plugin search order
  524. *
  525. * @var array
  526. */
  527. public $plugin_search_order = array('function', 'block', 'compiler', 'class');
  528. /**
  529. * registered objects
  530. *
  531. * @var array
  532. */
  533. public $registered_objects = array();
  534. /**
  535. * registered classes
  536. *
  537. * @var array
  538. */
  539. public $registered_classes = array();
  540. /**
  541. * registered filters
  542. *
  543. * @var array
  544. */
  545. public $registered_filters = array();
  546. /**
  547. * registered resources
  548. *
  549. * @var array
  550. */
  551. public $registered_resources = array();
  552. /**
  553. * resource handler cache
  554. *
  555. * @var array
  556. */
  557. public $_resource_handlers = array();
  558. /**
  559. * registered cache resources
  560. *
  561. * @var array
  562. */
  563. public $registered_cache_resources = array();
  564. /**
  565. * cache resource handler cache
  566. *
  567. * @var array
  568. */
  569. public $_cacheresource_handlers = array();
  570. /**
  571. * autoload filter
  572. *
  573. * @var array
  574. */
  575. public $autoload_filters = array();
  576. /**
  577. * default modifier
  578. *
  579. * @var array
  580. */
  581. public $default_modifiers = array();
  582. /**
  583. * autoescape variable output
  584. *
  585. * @var boolean
  586. */
  587. public $escape_html = false;
  588. /**
  589. * global internal smarty vars
  590. *
  591. * @var array
  592. */
  593. public static $_smarty_vars = array();
  594. /**
  595. * start time for execution time calculation
  596. *
  597. * @var int
  598. */
  599. public $start_time = 0;
  600. /**
  601. * default file permissions
  602. *
  603. * @var int
  604. */
  605. public $_file_perms = 0644;
  606. /**
  607. * default dir permissions
  608. *
  609. * @var int
  610. */
  611. public $_dir_perms = 0771;
  612. /**
  613. * block tag hierarchy
  614. *
  615. * @var array
  616. */
  617. public $_tag_stack = array();
  618. /**
  619. * self pointer to Smarty object
  620. *
  621. * @var Smarty
  622. */
  623. public $smarty;
  624. /**
  625. * required by the compiler for BC
  626. *
  627. * @var string
  628. */
  629. public $_current_file = null;
  630. /**
  631. * internal flag to enable parser debugging
  632. *
  633. * @var bool
  634. */
  635. public $_parserdebug = false;
  636. /**
  637. * Saved parameter of merged templates during compilation
  638. *
  639. * @var array
  640. */
  641. public $merged_templates_func = array();
  642. /**
  643. * Cache of is_file results of loadPlugin()
  644. *
  645. * @var array
  646. */
  647. public static $_is_file_cache= array();
  648. /**#@-*/
  649. /**
  650. * Initialize new Smarty object
  651. */
  652. public function __construct()
  653. {
  654. // selfpointer needed by some other class methods
  655. $this->smarty = $this;
  656. if (is_callable('mb_internal_encoding')) {
  657. mb_internal_encoding(Smarty::$_CHARSET);
  658. }
  659. $this->start_time = microtime(true);
  660. // set default dirs
  661. $this->setTemplateDir('.' . DS . 'templates' . DS)
  662. ->setCompileDir('.' . DS . 'templates_c' . DS)
  663. ->setPluginsDir(SMARTY_PLUGINS_DIR)
  664. ->setCacheDir('.' . DS . 'cache' . DS)
  665. ->setConfigDir('.' . DS . 'configs' . DS);
  666. $this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';
  667. if (isset($_SERVER['SCRIPT_NAME'])) {
  668. $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
  669. }
  670. }
  671. /**
  672. * Class destructor
  673. */
  674. public function __destruct()
  675. {
  676. // intentionally left blank
  677. }
  678. /**
  679. * <<magic>> set selfpointer on cloned object
  680. */
  681. public function __clone()
  682. {
  683. $this->smarty = $this;
  684. }
  685. /**
  686. * <<magic>> Generic getter.
  687. * Calls the appropriate getter function.
  688. * Issues an E_USER_NOTICE if no valid getter is found.
  689. *
  690. * @param string $name property name
  691. *
  692. * @return mixed
  693. */
  694. public function __get($name)
  695. {
  696. $allowed = array(
  697. 'template_dir' => 'getTemplateDir',
  698. 'config_dir' => 'getConfigDir',
  699. 'plugins_dir' => 'getPluginsDir',
  700. 'compile_dir' => 'getCompileDir',
  701. 'cache_dir' => 'getCacheDir',
  702. );
  703. if (isset($allowed[$name])) {
  704. return $this->{$allowed[$name]}();
  705. } else {
  706. trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
  707. }
  708. }
  709. /**
  710. * <<magic>> Generic setter.
  711. * Calls the appropriate setter function.
  712. * Issues an E_USER_NOTICE if no valid setter is found.
  713. *
  714. * @param string $name property name
  715. * @param mixed $value parameter passed to setter
  716. */
  717. public function __set($name, $value)
  718. {
  719. $allowed = array(
  720. 'template_dir' => 'setTemplateDir',
  721. 'config_dir' => 'setConfigDir',
  722. 'plugins_dir' => 'setPluginsDir',
  723. 'compile_dir' => 'setCompileDir',
  724. 'cache_dir' => 'setCacheDir',
  725. );
  726. if (isset($allowed[$name])) {
  727. $this->{$allowed[$name]}($value);
  728. } else {
  729. trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
  730. }
  731. }
  732. /**
  733. * Check if a template resource exists
  734. *
  735. * @param string $resource_name template name
  736. *
  737. * @return boolean status
  738. */
  739. public function templateExists($resource_name)
  740. {
  741. // create template object
  742. $save = $this->template_objects;
  743. $tpl = new $this->template_class($resource_name, $this);
  744. // check if it does exists
  745. $result = $tpl->source->exists;
  746. $this->template_objects = $save;
  747. return $result;
  748. }
  749. /**
  750. * Returns a single or all global variables
  751. *
  752. * @param string $varname variable name or null
  753. *
  754. * @return string variable value or or array of variables
  755. */
  756. public function getGlobal($varname = null)
  757. {
  758. if (isset($varname)) {
  759. if (isset(self::$global_tpl_vars[$varname])) {
  760. return self::$global_tpl_vars[$varname]->value;
  761. } else {
  762. return '';
  763. }
  764. } else {
  765. $_result = array();
  766. foreach (self::$global_tpl_vars AS $key => $var) {
  767. $_result[$key] = $var->value;
  768. }
  769. return $_result;
  770. }
  771. }
  772. /**
  773. * Empty cache folder
  774. *
  775. * @param integer $exp_time expiration time
  776. * @param string $type resource type
  777. *
  778. * @return integer number of cache files deleted
  779. */
  780. public function clearAllCache($exp_time = null, $type = null)
  781. {
  782. // load cache resource and call clearAll
  783. $_cache_resource = Smarty_CacheResource::load($this, $type);
  784. Smarty_CacheResource::invalidLoadedCache($this);
  785. return $_cache_resource->clearAll($this, $exp_time);
  786. }
  787. /**
  788. * Empty cache for a specific template
  789. *
  790. * @param string $template_name template name
  791. * @param string $cache_id cache id
  792. * @param string $compile_id compile id
  793. * @param integer $exp_time expiration time
  794. * @param string $type resource type
  795. *
  796. * @return integer number of cache files deleted
  797. */
  798. public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
  799. {
  800. // load cache resource and call clear
  801. $_cache_resource = Smarty_CacheResource::load($this, $type);
  802. Smarty_CacheResource::invalidLoadedCache($this);
  803. return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
  804. }
  805. /**
  806. * Loads security class and enables security
  807. *
  808. * @param string|Smarty_Security $security_class if a string is used, it must be class-name
  809. *
  810. * @return Smarty current Smarty instance for chaining
  811. * @throws SmartyException when an invalid class name is provided
  812. */
  813. public function enableSecurity($security_class = null)
  814. {
  815. if ($security_class instanceof Smarty_Security) {
  816. $this->security_policy = $security_class;
  817. return $this;
  818. } elseif (is_object($security_class)) {
  819. throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
  820. }
  821. if ($security_class == null) {
  822. $security_class = $this->security_class;
  823. }
  824. if (!class_exists($security_class)) {
  825. throw new SmartyException("Security class '$security_class' is not defined");
  826. } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
  827. throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
  828. } else {
  829. $this->security_policy = new $security_class($this);
  830. }
  831. return $this;
  832. }
  833. /**
  834. * Disable security
  835. *
  836. * @return Smarty current Smarty instance for chaining
  837. */
  838. public function disableSecurity()
  839. {
  840. $this->security_policy = null;
  841. return $this;
  842. }
  843. /**
  844. * Set template directory
  845. *
  846. * @param string|array $template_dir directory(s) of template sources
  847. *
  848. * @return Smarty current Smarty instance for chaining
  849. */
  850. public function setTemplateDir($template_dir)
  851. {
  852. $this->template_dir = array();
  853. foreach ((array) $template_dir as $k => $v) {
  854. $this->template_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
  855. }
  856. $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
  857. return $this;
  858. }
  859. /**
  860. * Add template directory(s)
  861. *
  862. * @param string|array $template_dir directory(s) of template sources
  863. * @param string $key of the array element to assign the template dir to
  864. *
  865. * @return Smarty current Smarty instance for chaining
  866. * @throws SmartyException when the given template directory is not valid
  867. */
  868. public function addTemplateDir($template_dir, $key = null)
  869. {
  870. // make sure we're dealing with an array
  871. $this->template_dir = (array) $this->template_dir;
  872. if (is_array($template_dir)) {
  873. foreach ($template_dir as $k => $v) {
  874. $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
  875. if (is_int($k)) {
  876. // indexes are not merged but appended
  877. $this->template_dir[] = $v;
  878. } else {
  879. // string indexes are overridden
  880. $this->template_dir[$k] = $v;
  881. }
  882. }
  883. } else {
  884. $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($template_dir, '/\\')) . DS;
  885. if ($key !== null) {
  886. // override directory at specified index
  887. $this->template_dir[$key] = $v;
  888. } else {
  889. // append new directory
  890. $this->template_dir[] = $v;
  891. }
  892. }
  893. $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
  894. return $this;
  895. }
  896. /**
  897. * Get template directories
  898. *
  899. * @param mixed $index index of directory to get, null to get all
  900. *
  901. * @return array|string list of template directories, or directory of $index
  902. */
  903. public function getTemplateDir($index = null)
  904. {
  905. if ($index !== null) {
  906. return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null;
  907. }
  908. return (array) $this->template_dir;
  909. }
  910. /**
  911. * Set config directory
  912. *
  913. * @param $config_dir
  914. *
  915. * @return Smarty current Smarty instance for chaining
  916. */
  917. public function setConfigDir($config_dir)
  918. {
  919. $this->config_dir = array();
  920. foreach ((array) $config_dir as $k => $v) {
  921. $this->config_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
  922. }
  923. $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
  924. return $this;
  925. }
  926. /**
  927. * Add config directory(s)
  928. *
  929. * @param string|array $config_dir directory(s) of config sources
  930. * @param mixed $key key of the array element to assign the config dir to
  931. *
  932. * @return Smarty current Smarty instance for chaining
  933. */
  934. public function addConfigDir($config_dir, $key = null)
  935. {
  936. // make sure we're dealing with an array
  937. $this->config_dir = (array) $this->config_dir;
  938. if (is_array($config_dir)) {
  939. foreach ($config_dir as $k => $v) {
  940. $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
  941. if (is_int($k)) {
  942. // indexes are not merged but appended
  943. $this->config_dir[] = $v;
  944. } else {
  945. // string indexes are overridden
  946. $this->config_dir[$k] = $v;
  947. }
  948. }
  949. } else {
  950. $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($config_dir, '/\\')) . DS;
  951. if ($key !== null) {
  952. // override directory at specified index
  953. $this->config_dir[$key] = rtrim($v, '/\\') . DS;
  954. } else {
  955. // append new directory
  956. $this->config_dir[] = rtrim($v, '/\\') . DS;
  957. }
  958. }
  959. $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
  960. return $this;
  961. }
  962. /**
  963. * Get config directory
  964. *
  965. * @param mixed $index index of directory to get, null to get all
  966. *
  967. * @return array|string configuration directory
  968. */
  969. public function getConfigDir($index = null)
  970. {
  971. if ($index !== null) {
  972. return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null;
  973. }
  974. return (array) $this->config_dir;
  975. }
  976. /**
  977. * Set plugins directory
  978. *
  979. * @param string|array $plugins_dir directory(s) of plugins
  980. *
  981. * @return Smarty current Smarty instance for chaining
  982. */
  983. public function setPluginsDir($plugins_dir)
  984. {
  985. $this->plugins_dir = array();
  986. foreach ((array) $plugins_dir as $k => $v) {
  987. $this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
  988. }
  989. return $this;
  990. }
  991. /**
  992. * Adds directory of plugin files
  993. *
  994. * @param $plugins_dir
  995. *
  996. * @return Smarty current Smarty instance for chaining
  997. */
  998. public function addPluginsDir($plugins_dir)
  999. {
  1000. // make sure we're dealing with an array
  1001. $this->plugins_dir = (array) $this->plugins_dir;
  1002. if (is_array($plugins_dir)) {
  1003. foreach ($plugins_dir as $k => $v) {
  1004. if (is_int($k)) {
  1005. // indexes are not merged but appended
  1006. $this->plugins_dir[] = rtrim($v, '/\\') . DS;
  1007. } else {
  1008. // string indexes are overridden
  1009. $this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
  1010. }
  1011. }
  1012. } else {
  1013. // append new directory
  1014. $this->plugins_dir[] = rtrim($plugins_dir, '/\\') . DS;
  1015. }
  1016. $this->plugins_dir = array_unique($this->plugins_dir);
  1017. return $this;
  1018. }
  1019. /**
  1020. * Get plugin directories
  1021. *
  1022. * @return array list of plugin directories
  1023. */
  1024. public function getPluginsDir()
  1025. {
  1026. return (array) $this->plugins_dir;
  1027. }
  1028. /**
  1029. * Set compile directory
  1030. *
  1031. * @param string $compile_dir directory to store compiled templates in
  1032. *
  1033. * @return Smarty current Smarty instance for chaining
  1034. */
  1035. public function setCompileDir($compile_dir)
  1036. {
  1037. $this->compile_dir = rtrim($compile_dir, '/\\') . DS;
  1038. if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
  1039. Smarty::$_muted_directories[$this->compile_dir] = null;
  1040. }
  1041. return $this;
  1042. }
  1043. /**
  1044. * Get compiled directory
  1045. *
  1046. * @return string path to compiled templates
  1047. */
  1048. public function getCompileDir()
  1049. {
  1050. return $this->compile_dir;
  1051. }
  1052. /**
  1053. * Set cache directory
  1054. *
  1055. * @param string $cache_dir directory to store cached templates in
  1056. *
  1057. * @return Smarty current Smarty instance for chaining
  1058. */
  1059. public function setCacheDir($cache_dir)
  1060. {
  1061. $this->cache_dir = rtrim($cache_dir, '/\\') . DS;
  1062. if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
  1063. Smarty::$_muted_directories[$this->cache_dir] = null;
  1064. }
  1065. return $this;
  1066. }
  1067. /**
  1068. * Get cache directory
  1069. *
  1070. * @return string path of cache directory
  1071. */
  1072. public function getCacheDir()
  1073. {
  1074. return $this->cache_dir;
  1075. }
  1076. /**
  1077. * Set default modifiers
  1078. *
  1079. * @param array|string $modifiers modifier or list of modifiers to set
  1080. *
  1081. * @return Smarty current Smarty instance for chaining
  1082. */
  1083. public function setDefaultModifiers($modifiers)
  1084. {
  1085. $this->default_modifiers = (array) $modifiers;
  1086. return $this;
  1087. }
  1088. /**
  1089. * Add default modifiers
  1090. *
  1091. * @param array|string $modifiers modifier or list of modifiers to add
  1092. *
  1093. * @return Smarty current Smarty instance for chaining
  1094. */
  1095. public function addDefaultModifiers($modifiers)
  1096. {
  1097. if (is_array($modifiers)) {
  1098. $this->default_modifiers = array_merge($this->default_modifiers, $modifiers);
  1099. } else {
  1100. $this->default_modifiers[] = $modifiers;
  1101. }
  1102. return $this;
  1103. }
  1104. /**
  1105. * Get default modifiers
  1106. *
  1107. * @return array list of default modifiers
  1108. */
  1109. public function getDefaultModifiers()
  1110. {
  1111. return $this->default_modifiers;
  1112. }
  1113. /**
  1114. * Set autoload filters
  1115. *
  1116. * @param array $filters filters to load automatically
  1117. * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
  1118. *
  1119. * @return Smarty current Smarty instance for chaining
  1120. */
  1121. public function setAutoloadFilters($filters, $type = null)
  1122. {
  1123. if ($type !== null) {
  1124. $this->autoload_filters[$type] = (array) $filters;
  1125. } else {
  1126. $this->autoload_filters = (array) $filters;
  1127. }
  1128. return $this;
  1129. }
  1130. /**
  1131. * Add autoload filters
  1132. *
  1133. * @param array $filters filters to load automatically
  1134. * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
  1135. *
  1136. * @return Smarty current Smarty instance for chaining
  1137. */
  1138. public function addAutoloadFilters($filters, $type = null)
  1139. {
  1140. if ($type !== null) {
  1141. if (!empty($this->autoload_filters[$type])) {
  1142. $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
  1143. } else {
  1144. $this->autoload_filters[$type] = (array) $filters;
  1145. }
  1146. } else {
  1147. foreach ((array) $filters as $key => $value) {
  1148. if (!empty($this->autoload_filters[$key])) {
  1149. $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
  1150. } else {
  1151. $this->autoload_filters[$key] = (array) $value;
  1152. }
  1153. }
  1154. }
  1155. return $this;
  1156. }
  1157. /**
  1158. * Get autoload filters
  1159. *
  1160. * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
  1161. *
  1162. * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified
  1163. */
  1164. public function getAutoloadFilters($type = null)
  1165. {
  1166. if ($type !== null) {
  1167. return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
  1168. }
  1169. return $this->autoload_filters;
  1170. }
  1171. /**
  1172. * return name of debugging template
  1173. *
  1174. * @return string
  1175. */
  1176. public function getDebugTemplate()
  1177. {
  1178. return $this->debug_tpl;
  1179. }
  1180. /**
  1181. * set the debug template
  1182. *
  1183. * @param string $tpl_name
  1184. *
  1185. * @return Smarty current Smarty instance for chaining
  1186. * @throws SmartyException if file is not readable
  1187. */
  1188. public function setDebugTemplate($tpl_name)
  1189. {
  1190. if (!is_readable($tpl_name)) {
  1191. throw new SmartyException("Unknown file '{$tpl_name}'");
  1192. }
  1193. $this->debug_tpl = $tpl_name;
  1194. return $this;
  1195. }
  1196. /**
  1197. * creates a template object
  1198. *
  1199. * @param string $template the resource handle of the template file
  1200. * @param mixed $cache_id cache id to be used with this template
  1201. * @param mixed $compile_id compile id to be used with this template
  1202. * @param object $parent next higher level of Smarty variables
  1203. * @param boolean $do_clone flag is Smarty object shall be cloned
  1204. *
  1205. * @return object template object
  1206. */
  1207. public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
  1208. {
  1209. if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) {
  1210. $parent = $cache_id;
  1211. $cache_id = null;
  1212. }
  1213. if ($parent !== null && is_array($parent)) {
  1214. $data = $parent;
  1215. $parent = null;
  1216. } else {
  1217. $data = null;
  1218. }
  1219. // default to cache_id and compile_id of Smarty object
  1220. $cache_id = $cache_id === null ? $this->cache_id : $cache_id;
  1221. $compile_id = $compile_id === null ? $this->compile_id : $compile_id;
  1222. // already in template cache?
  1223. if ($this->allow_ambiguous_resources) {
  1224. $_templateId = Smarty_Resource::getUniqueTemplateName($this, $template) . $cache_id . $compile_id;
  1225. } else {
  1226. $_templateId = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id;
  1227. }
  1228. if (isset($_templateId[150])) {
  1229. $_templateId = sha1($_templateId);
  1230. }
  1231. if ($do_clone) {
  1232. if (isset($this->template_objects[$_templateId])) {
  1233. // return cached template object
  1234. $tpl = clone $this->template_objects[$_templateId];
  1235. $tpl->smarty = clone $tpl->smarty;
  1236. $tpl->parent = $parent;
  1237. $tpl->tpl_vars = array();
  1238. $tpl->config_vars = array();
  1239. } else {
  1240. $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id);
  1241. }
  1242. } else {
  1243. if (isset($this->template_objects[$_templateId])) {
  1244. // return cached template object
  1245. $tpl = $this->template_objects[$_templateId];
  1246. $tpl->parent = $parent;
  1247. $tpl->tpl_vars = array();
  1248. $tpl->config_vars = array();
  1249. } else {
  1250. $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
  1251. }
  1252. }
  1253. // fill data if present
  1254. if (!empty($data) && is_array($data)) {
  1255. // set up variable values
  1256. foreach ($data as $_key => $_val) {
  1257. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  1258. }
  1259. }
  1260. return $tpl;
  1261. }
  1262. /**
  1263. * Takes unknown classes and loads plugin files for them
  1264. * class name format: Smarty_PluginType_PluginName
  1265. * plugin filename format: plugintype.pluginname.php
  1266. *
  1267. * @param string $plugin_name class plugin name to load
  1268. * @param bool $check check if already loaded
  1269. *
  1270. * @throws SmartyException
  1271. * @return string |boolean filepath of loaded file or false
  1272. */
  1273. public function loadPlugin($plugin_name, $check = true)
  1274. {
  1275. // if function or class exists, exit silently (already loaded)
  1276. if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
  1277. return true;
  1278. }
  1279. // Plugin name is expected to be: Smarty_[Type]_[Name]
  1280. $_name_parts = explode('_', $plugin_name, 3);
  1281. // class name must have three parts to be valid plugin
  1282. // count($_name_parts) < 3 === !isset($_name_parts[2])
  1283. if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') {
  1284. throw new SmartyException("plugin {$plugin_name} is not a valid name format");
  1285. }
  1286. // if type is "internal", get plugin from sysplugins
  1287. if (strtolower($_name_parts[1]) == 'internal') {
  1288. $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
  1289. if (isset(self::$_is_file_cache[$file]) ? self::$_is_file_cache[$file] : self::$_is_file_cache[$file] = is_file($file)) {
  1290. require_once($file);
  1291. return $file;
  1292. } else {
  1293. return false;
  1294. }
  1295. }
  1296. // plugin filename is expected to be: [type].[name].php
  1297. $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
  1298. $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
  1299. // loop through plugin dirs and find the plugin
  1300. foreach ($this->getPluginsDir() as $_plugin_dir) {
  1301. $names = array(
  1302. $_plugin_dir . $_plugin_filename,
  1303. $_plugin_dir . strtolower($_plugin_filename),
  1304. );
  1305. foreach ($names as $file) {
  1306. if (isset(self::$_is_file_cache[$file]) ? self::$_is_file_cache[$file] : self::$_is_file_cache[$file] = is_file($file)) {
  1307. require_once($file);
  1308. return $file;
  1309. }
  1310. if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
  1311. // try PHP include_path
  1312. if ($_stream_resolve_include_path) {
  1313. $file = stream_resolve_include_path($file);
  1314. } else {
  1315. $file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
  1316. }
  1317. if ($file !== false) {
  1318. require_once($file);
  1319. return $file;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. // no plugin loaded
  1325. return false;
  1326. }
  1327. /**
  1328. * Compile all template files
  1329. *
  1330. * @param string $extension file extension
  1331. * @param bool $force_compile force all to recompile
  1332. * @param int $time_limit
  1333. * @param int $max_errors
  1334. *
  1335. * @return integer number of template files recompiled
  1336. */
  1337. public function compileAllTemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  1338. {
  1339. return Smarty_Internal_Utility::compileAllTemplates($extension, $force_compile, $time_limit, $max_errors, $this);
  1340. }
  1341. /**
  1342. * Compile all config files
  1343. *
  1344. * @param string $extension file extension
  1345. * @param bool $force_compile force all to recompile
  1346. * @param int $time_limit
  1347. * @param int $max_errors
  1348. *
  1349. * @return integer number of template files recompiled
  1350. */
  1351. public function compileAllConfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
  1352. {
  1353. return Smarty_Internal_Utility::compileAllConfig($extension, $force_compile, $time_limit, $max_errors, $this);
  1354. }
  1355. /**
  1356. * Delete compiled template file
  1357. *
  1358. * @param string $resource_name template name
  1359. * @param string $compile_id compile id
  1360. * @param integer $exp_time expiration time
  1361. *
  1362. * @return integer number of template files deleted
  1363. */
  1364. public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  1365. {
  1366. return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
  1367. }
  1368. /**
  1369. * Return array of tag/attributes of all tags used by an template
  1370. *
  1371. * @param Smarty_Internal_Template $template
  1372. *
  1373. * @return array of tag/attributes
  1374. */
  1375. public function getTags(Smarty_Internal_Template $template)
  1376. {
  1377. return Smarty_Internal_Utility::getTags($template);
  1378. }
  1379. /**
  1380. * Run installation test
  1381. *
  1382. * @param array $errors Array to write errors into, rather than outputting them
  1383. *
  1384. * @return boolean true if setup is fine, false if something is wrong
  1385. */
  1386. public function testInstall(&$errors = null)
  1387. {
  1388. return Smarty_Internal_Utility::testInstall($this, $errors);
  1389. }
  1390. /**
  1391. * Error Handler to mute expected messages
  1392. *
  1393. * @link http://php.net/set_error_handler
  1394. *
  1395. * @param integer $errno Error level
  1396. * @param $errstr
  1397. * @param $errfile
  1398. * @param $errline
  1399. * @param $errcontext
  1400. *
  1401. * @return boolean
  1402. */
  1403. public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  1404. {
  1405. $_is_muted_directory = false;
  1406. // add the SMARTY_DIR to the list of muted directories
  1407. if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
  1408. $smarty_dir = realpath(SMARTY_DIR);
  1409. if ($smarty_dir !== false) {
  1410. Smarty::$_muted_directories[SMARTY_DIR] = array(
  1411. 'file' => $smarty_dir,
  1412. 'length' => strlen($smarty_dir),
  1413. );
  1414. }
  1415. }
  1416. // walk the muted directories and test against $errfile
  1417. foreach (Smarty::$_muted_directories as $key => &$dir) {
  1418. if (!$dir) {
  1419. // resolve directory and length for speedy comparisons
  1420. $file = realpath($key);
  1421. if ($file === false) {
  1422. // this directory does not exist, remove and skip it
  1423. unset(Smarty::$_muted_directories[$key]);
  1424. continue;
  1425. }
  1426. $dir = array(
  1427. 'file' => $file,
  1428. 'length' => strlen($file),
  1429. );
  1430. }
  1431. if (!strncmp($errfile, $dir['file'], $dir['length'])) {
  1432. $_is_muted_directory = true;
  1433. break;
  1434. }
  1435. }
  1436. // pass to next error handler if this error did not occur inside SMARTY_DIR
  1437. // or the error was within smarty but masked to be ignored
  1438. if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
  1439. if (Smarty::$_previous_error_handler) {
  1440. return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
  1441. } else {
  1442. return false;
  1443. }
  1444. }
  1445. }
  1446. /**
  1447. * Enable error handler to mute expected messages
  1448. *
  1449. * @return void
  1450. */
  1451. public static function muteExpectedErrors()
  1452. {
  1453. /*
  1454. error muting is done because some people implemented custom error_handlers using
  1455. http://php.net/set_error_handler and for some reason did not understand the following paragraph:
  1456. It is important to remember that the standard PHP error handler is completely bypassed for the
  1457. error types specified by error_types unless the callback function returns FALSE.
  1458. error_reporting() settings will have no effect and your error handler will be called regardless -
  1459. however you are still able to read the current value of error_reporting and act appropriately.
  1460. Of particular note is that this value will be 0 if the statement that caused the error was
  1461. prepended by the @ error-control operator.
  1462. Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
  1463. - @filemtime() is almost twice as fast as using an additional file_exists()
  1464. - between file_exists() and filemtime() a possible race condition is opened,
  1465. which does not exist using the simple @filemtime() approach.
  1466. */
  1467. $error_handler = array('Smarty', 'mutingErrorHandler');
  1468. $previous = set_error_handler($error_handler);
  1469. // avoid dead loops
  1470. if ($previous !== $error_handler) {
  1471. Smarty::$_previous_error_handler = $previous;
  1472. }
  1473. }
  1474. /**
  1475. * Disable error handler muting expected messages
  1476. *
  1477. * @return void
  1478. */
  1479. public static function unmuteExpectedErrors()
  1480. {
  1481. restore_error_handler();
  1482. }
  1483. }
  1484. // Check if we're running on windows
  1485. Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  1486. // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
  1487. if (Smarty::$_CHARSET !== 'UTF-8') {
  1488. Smarty::$_UTF8_MODIFIER = '';
  1489. }
  1490. /**
  1491. * Smarty exception class
  1492. *
  1493. * @package Smarty
  1494. */
  1495. class SmartyException extends Exception
  1496. {
  1497. public static $escape = false;
  1498. public function __toString()
  1499. {
  1500. return ' --> Smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- ';
  1501. }
  1502. }
  1503. /**
  1504. * Smarty compiler exception class
  1505. *
  1506. * @package Smarty
  1507. */
  1508. class SmartyCompilerException extends SmartyException
  1509. {
  1510. public function __toString()
  1511. {
  1512. return ' --> Smarty Compiler: ' . $this->message . ' <-- ';
  1513. }
  1514. /**
  1515. * The line number of the template error
  1516. *
  1517. * @type int|null
  1518. */
  1519. public $line = null;
  1520. /**
  1521. * The template source snippet relating to the error
  1522. *
  1523. * @type string|null
  1524. */
  1525. public $source = null;
  1526. /**
  1527. * The raw text of the error message
  1528. *
  1529. * @type string|null
  1530. */
  1531. public $desc = null;
  1532. /**
  1533. * The resource identifier or template name
  1534. *
  1535. * @type string|null
  1536. */
  1537. public $template = null;
  1538. }
  1539. /**
  1540. * Autoloader
  1541. */
  1542. function smartyAutoload($class)
  1543. {
  1544. $_class = strtolower($class);
  1545. static $_classes = array(
  1546. 'smarty_config_source' => true,
  1547. 'smarty_config_compiled' => true,
  1548. 'smarty_security' => true,
  1549. 'smarty_cacheresource' => true,
  1550. 'smarty_cacheresource_custom' => true,
  1551. 'smarty_cacheresource_keyvaluestore' => true,
  1552. 'smarty_resource' => true,
  1553. 'smarty_resource_custom' => true,
  1554. 'smarty_resource_uncompiled' => true,
  1555. 'smarty_resource_recompiled' => true,
  1556. );
  1557. if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) {
  1558. include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
  1559. }
  1560. }