Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Smarty.class.php 39KB

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