Smarty.class.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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. *
  20. * For questions, help, comments, discussion, etc., please join the
  21. * Smarty mailing list. Send a blank e-mail to
  22. * smarty-general-subscribe@lists.php.net
  23. *
  24. * @link http://smarty.php.net/
  25. * @copyright 2001-2005 New Digital Group, Inc.
  26. * @author Monte Ohrt <monte at ohrt dot com>
  27. * @author Andrei Zmievski <andrei@php.net>
  28. * @package Smarty
  29. * @version 2.6.17
  30. */
  31. /* $Id: Smarty.class.php,v 1.527 2006/12/01 15:40:05 mohrt Exp $ */
  32. /**
  33. * DIR_SEP isn't used anymore, but third party apps might
  34. */
  35. if(!defined('DIR_SEP')) {
  36. define('DIR_SEP', DIRECTORY_SEPARATOR);
  37. }
  38. /**
  39. * set SMARTY_DIR to absolute path to Smarty library files.
  40. * if not defined, include_path will be used. Sets SMARTY_DIR only if user
  41. * application has not already defined it.
  42. */
  43. if (!defined('SMARTY_DIR')) {
  44. define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  45. }
  46. if (!defined('SMARTY_CORE_DIR')) {
  47. define('SMARTY_CORE_DIR', SMARTY_DIR . 'internals' . DIRECTORY_SEPARATOR);
  48. }
  49. define('SMARTY_PHP_PASSTHRU', 0);
  50. define('SMARTY_PHP_QUOTE', 1);
  51. define('SMARTY_PHP_REMOVE', 2);
  52. define('SMARTY_PHP_ALLOW', 3);
  53. /**
  54. * @package Smarty
  55. */
  56. class Smarty
  57. {
  58. /**#@+
  59. * Smarty Configuration Section
  60. */
  61. /**
  62. * The name of the directory where templates are located.
  63. *
  64. * @var string
  65. */
  66. var $template_dir = 'TEMPLATE';
  67. /**
  68. * The directory where compiled templates are located.
  69. *
  70. * @var string
  71. */
  72. var $compile_dir = 'TEMPLATE_C';
  73. /**
  74. * The directory where config files are located.
  75. *
  76. * @var string
  77. */
  78. var $config_dir = 'configs';
  79. /**
  80. * An array of directories searched for plugins.
  81. *
  82. * @var array
  83. */
  84. var $plugins_dir = array('plugins');
  85. /**
  86. * If debugging is enabled, a debug console window will display
  87. * when the page loads (make sure your browser allows unrequested
  88. * popup windows)
  89. *
  90. * @var boolean
  91. */
  92. var $debugging = false;
  93. /**
  94. * When set, smarty does uses this value as error_reporting-level.
  95. *
  96. * @var boolean
  97. */
  98. var $error_reporting = null;
  99. /**
  100. * This is the path to the debug console template. If not set,
  101. * the default one will be used.
  102. *
  103. * @var string
  104. */
  105. var $debug_tpl = '';
  106. /**
  107. * This determines if debugging is enable-able from the browser.
  108. * <ul>
  109. * <li>NONE => no debugging control allowed</li>
  110. * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
  111. * </ul>
  112. * @link http://www.foo.dom/index.php?SMARTY_DEBUG
  113. * @var string
  114. */
  115. var $debugging_ctrl = 'NONE';
  116. /**
  117. * This tells Smarty whether to check for recompiling or not. Recompiling
  118. * does not need to happen unless a template or config file is changed.
  119. * Typically you enable this during development, and disable for
  120. * production.
  121. *
  122. * @var boolean
  123. */
  124. var $compile_check = true;
  125. /**
  126. * This forces templates to compile every time. Useful for development
  127. * or debugging.
  128. *
  129. * @var boolean
  130. */
  131. var $force_compile = false;
  132. /**
  133. * This enables template caching.
  134. * <ul>
  135. * <li>0 = no caching</li>
  136. * <li>1 = use class cache_lifetime value</li>
  137. * <li>2 = use cache_lifetime in cache file</li>
  138. * </ul>
  139. * @var integer
  140. */
  141. var $caching = 0;
  142. /**
  143. * The name of the directory for cache files.
  144. *
  145. * @var string
  146. */
  147. var $cache_dir = 'cache';
  148. /**
  149. * This is the number of seconds cached content will persist.
  150. * <ul>
  151. * <li>0 = always regenerate cache</li>
  152. * <li>-1 = never expires</li>
  153. * </ul>
  154. *
  155. * @var integer
  156. */
  157. var $cache_lifetime = 3600;
  158. /**
  159. * Only used when $caching is enabled. If true, then If-Modified-Since headers
  160. * are respected with cached content, and appropriate HTTP headers are sent.
  161. * This way repeated hits to a cached page do not send the entire page to the
  162. * client every time.
  163. *
  164. * @var boolean
  165. */
  166. var $cache_modified_check = false;
  167. /**
  168. * This determines how Smarty handles "<?php ... ?>" tags in templates.
  169. * possible values:
  170. * <ul>
  171. * <li>SMARTY_PHP_PASSTHRU -> print tags as plain text</li>
  172. * <li>SMARTY_PHP_QUOTE -> escape tags as entities</li>
  173. * <li>SMARTY_PHP_REMOVE -> remove php tags</li>
  174. * <li>SMARTY_PHP_ALLOW -> execute php tags</li>
  175. * </ul>
  176. *
  177. * @var integer
  178. */
  179. var $php_handling = SMARTY_PHP_PASSTHRU;
  180. /**
  181. * This enables template security. When enabled, many things are restricted
  182. * in the templates that normally would go unchecked. This is useful when
  183. * untrusted parties are editing templates and you want a reasonable level
  184. * of security. (no direct execution of PHP in templates for example)
  185. *
  186. * @var boolean
  187. */
  188. var $security = false;
  189. /**
  190. * This is the list of template directories that are considered secure. This
  191. * is used only if {@link $security} is enabled. One directory per array
  192. * element. {@link $template_dir} is in this list implicitly.
  193. *
  194. * @var array
  195. */
  196. var $secure_dir = array();
  197. /**
  198. * These are the security settings for Smarty. They are used only when
  199. * {@link $security} is enabled.
  200. *
  201. * @var array
  202. */
  203. var $security_settings = array(
  204. 'PHP_HANDLING' => false,
  205. 'IF_FUNCS' => array('array', 'list',
  206. 'isset', 'empty',
  207. 'count', 'sizeof',
  208. 'in_array', 'is_array',
  209. 'true', 'false', 'null'),
  210. 'INCLUDE_ANY' => false,
  211. 'PHP_TAGS' => false,
  212. 'MODIFIER_FUNCS' => array('count'),
  213. 'ALLOW_CONSTANTS' => false
  214. );
  215. /**
  216. * This is an array of directories where trusted php scripts reside.
  217. * {@link $security} is disabled during their inclusion/execution.
  218. *
  219. * @var array
  220. */
  221. var $trusted_dir = array();
  222. /**
  223. * The left delimiter used for the template tags.
  224. *
  225. * @var string
  226. */
  227. var $left_delimiter = '{';
  228. /**
  229. * The right delimiter used for the template tags.
  230. *
  231. * @var string
  232. */
  233. var $right_delimiter = '}';
  234. /**
  235. * The order in which request variables are registered, similar to
  236. * variables_order in php.ini E = Environment, G = GET, P = POST,
  237. * C = Cookies, S = Server
  238. *
  239. * @var string
  240. */
  241. var $request_vars_order = 'EGPCS';
  242. /**
  243. * Indicates wether $HTTP_*_VARS[] (request_use_auto_globals=false)
  244. * are uses as request-vars or $_*[]-vars. note: if
  245. * request_use_auto_globals is true, then $request_vars_order has
  246. * no effect, but the php-ini-value "gpc_order"
  247. *
  248. * @var boolean
  249. */
  250. var $request_use_auto_globals = true;
  251. /**
  252. * Set this if you want different sets of compiled files for the same
  253. * templates. This is useful for things like different languages.
  254. * Instead of creating separate sets of templates per language, you
  255. * set different compile_ids like 'en' and 'de'.
  256. *
  257. * @var string
  258. */
  259. var $compile_id = null;
  260. /**
  261. * This tells Smarty whether or not to use sub dirs in the cache/ and
  262. * templates_c/ directories. sub directories better organized, but
  263. * may not work well with PHP safe mode enabled.
  264. *
  265. * @var boolean
  266. *
  267. */
  268. var $use_sub_dirs = false;
  269. /**
  270. * This is a list of the modifiers to apply to all template variables.
  271. * Put each modifier in a separate array element in the order you want
  272. * them applied. example: <code>array('escape:"htmlall"');</code>
  273. *
  274. * @var array
  275. */
  276. var $default_modifiers = array();
  277. /**
  278. * This is the resource type to be used when not specified
  279. * at the beginning of the resource path. examples:
  280. * $smarty->display('file:index.tpl');
  281. * $smarty->display('db:index.tpl');
  282. * $smarty->display('index.tpl'); // will use default resource type
  283. * {include file="file:index.tpl"}
  284. * {include file="db:index.tpl"}
  285. * {include file="index.tpl"} {* will use default resource type *}
  286. *
  287. * @var array
  288. */
  289. var $default_resource_type = 'file';
  290. /**
  291. * The function used for cache file handling. If not set, built-in caching is used.
  292. *
  293. * @var null|string function name
  294. */
  295. var $cache_handler_func = null;
  296. /**
  297. * This indicates which filters are automatically loaded into Smarty.
  298. *
  299. * @var array array of filter names
  300. */
  301. var $autoload_filters = array();
  302. /**#@+
  303. * @var boolean
  304. */
  305. /**
  306. * This tells if config file vars of the same name overwrite each other or not.
  307. * if disabled, same name variables are accumulated in an array.
  308. */
  309. var $config_overwrite = true;
  310. /**
  311. * This tells whether or not to automatically booleanize config file variables.
  312. * If enabled, then the strings "on", "true", and "yes" are treated as boolean
  313. * true, and "off", "false" and "no" are treated as boolean false.
  314. */
  315. var $config_booleanize = true;
  316. /**
  317. * This tells whether hidden sections [.foobar] are readable from the
  318. * tempalates or not. Normally you would never allow this since that is
  319. * the point behind hidden sections: the application can access them, but
  320. * the templates cannot.
  321. */
  322. var $config_read_hidden = false;
  323. /**
  324. * This tells whether or not automatically fix newlines in config files.
  325. * It basically converts \r (mac) or \r\n (dos) to \n
  326. */
  327. var $config_fix_newlines = true;
  328. /**#@-*/
  329. /**
  330. * If a template cannot be found, this PHP function will be executed.
  331. * Useful for creating templates on-the-fly or other special action.
  332. *
  333. * @var string function name
  334. */
  335. var $default_template_handler_func = '';
  336. /**
  337. * The file that contains the compiler class. This can a full
  338. * pathname, or relative to the php_include path.
  339. *
  340. * @var string
  341. */
  342. var $compiler_file = 'Smarty_Compiler.class.php';
  343. /**
  344. * The class used for compiling templates.
  345. *
  346. * @var string
  347. */
  348. var $compiler_class = 'Smarty_Compiler';
  349. /**
  350. * The class used to load config vars.
  351. *
  352. * @var string
  353. */
  354. var $config_class = 'Config_File';
  355. /**#@+
  356. * END Smarty Configuration Section
  357. * There should be no need to touch anything below this line.
  358. * @access private
  359. */
  360. /**
  361. * where assigned template vars are kept
  362. *
  363. * @var array
  364. */
  365. var $_tpl_vars = array();
  366. /**
  367. * stores run-time $smarty.* vars
  368. *
  369. * @var null|array
  370. */
  371. var $_smarty_vars = null;
  372. /**
  373. * keeps track of sections
  374. *
  375. * @var array
  376. */
  377. var $_sections = array();
  378. /**
  379. * keeps track of foreach blocks
  380. *
  381. * @var array
  382. */
  383. var $_foreach = array();
  384. /**
  385. * keeps track of tag hierarchy
  386. *
  387. * @var array
  388. */
  389. var $_tag_stack = array();
  390. /**
  391. * configuration object
  392. *
  393. * @var Config_file
  394. */
  395. var $_conf_obj = null;
  396. /**
  397. * loaded configuration settings
  398. *
  399. * @var array
  400. */
  401. var $_config = array(array('vars' => array(), 'files' => array()));
  402. /**
  403. * md5 checksum of the string 'Smarty'
  404. *
  405. * @var string
  406. */
  407. var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f';
  408. /**
  409. * Smarty version number
  410. *
  411. * @var string
  412. */
  413. var $_version = '2.6.17';
  414. /**
  415. * current template inclusion depth
  416. *
  417. * @var integer
  418. */
  419. var $_inclusion_depth = 0;
  420. /**
  421. * for different compiled templates
  422. *
  423. * @var string
  424. */
  425. var $_compile_id = null;
  426. /**
  427. * text in URL to enable debug mode
  428. *
  429. * @var string
  430. */
  431. var $_smarty_debug_id = 'SMARTY_DEBUG';
  432. /**
  433. * debugging information for debug console
  434. *
  435. * @var array
  436. */
  437. var $_smarty_debug_info = array();
  438. /**
  439. * info that makes up a cache file
  440. *
  441. * @var array
  442. */
  443. var $_cache_info = array();
  444. /**
  445. * default file permissions
  446. *
  447. * @var integer
  448. */
  449. var $_file_perms = 0644;
  450. /**
  451. * default dir permissions
  452. *
  453. * @var integer
  454. */
  455. var $_dir_perms = 0771;
  456. /**
  457. * registered objects
  458. *
  459. * @var array
  460. */
  461. var $_reg_objects = array();
  462. /**
  463. * table keeping track of plugins
  464. *
  465. * @var array
  466. */
  467. var $_plugins = array(
  468. 'modifier' => array(),
  469. 'function' => array(),
  470. 'block' => array(),
  471. 'compiler' => array(),
  472. 'prefilter' => array(),
  473. 'postfilter' => array(),
  474. 'outputfilter' => array(),
  475. 'resource' => array(),
  476. 'insert' => array());
  477. /**
  478. * cache serials
  479. *
  480. * @var array
  481. */
  482. var $_cache_serials = array();
  483. /**
  484. * name of optional cache include file
  485. *
  486. * @var string
  487. */
  488. var $_cache_include = null;
  489. /**
  490. * indicate if the current code is used in a compiled
  491. * include
  492. *
  493. * @var string
  494. */
  495. var $_cache_including = false;
  496. /**#@-*/
  497. /**
  498. * The class constructor.
  499. */
  500. function Smarty()
  501. {
  502. $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
  503. : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
  504. }
  505. /**
  506. * assigns values to template variables
  507. *
  508. * @param array|string $tpl_var the template variable name(s)
  509. * @param mixed $value the value to assign
  510. */
  511. function assign($tpl_var, $value = null)
  512. {
  513. if (is_array($tpl_var)){
  514. foreach ($tpl_var as $key => $val) {
  515. if ($key != '') {
  516. $this->_tpl_vars[$key] = $val;
  517. }
  518. }
  519. } else {
  520. if ($tpl_var != '')
  521. $this->_tpl_vars[$tpl_var] = $value;
  522. }
  523. }
  524. /**
  525. * assigns values to template variables by reference
  526. *
  527. * @param string $tpl_var the template variable name
  528. * @param mixed $value the referenced value to assign
  529. */
  530. function assign_by_ref($tpl_var, &$value)
  531. {
  532. if ($tpl_var != '')
  533. $this->_tpl_vars[$tpl_var] = &$value;
  534. }
  535. /**
  536. * appends values to template variables
  537. *
  538. * @param array|string $tpl_var the template variable name(s)
  539. * @param mixed $value the value to append
  540. */
  541. function append($tpl_var, $value=null, $merge=false)
  542. {
  543. if (is_array($tpl_var)) {
  544. // $tpl_var is an array, ignore $value
  545. foreach ($tpl_var as $_key => $_val) {
  546. if ($_key != '') {
  547. if(!@is_array($this->_tpl_vars[$_key])) {
  548. settype($this->_tpl_vars[$_key],'array');
  549. }
  550. if($merge && is_array($_val)) {
  551. foreach($_val as $_mkey => $_mval) {
  552. $this->_tpl_vars[$_key][$_mkey] = $_mval;
  553. }
  554. } else {
  555. $this->_tpl_vars[$_key][] = $_val;
  556. }
  557. }
  558. }
  559. } else {
  560. if ($tpl_var != '' && isset($value)) {
  561. if(!@is_array($this->_tpl_vars[$tpl_var])) {
  562. settype($this->_tpl_vars[$tpl_var],'array');
  563. }
  564. if($merge && is_array($value)) {
  565. foreach($value as $_mkey => $_mval) {
  566. $this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
  567. }
  568. } else {
  569. $this->_tpl_vars[$tpl_var][] = $value;
  570. }
  571. }
  572. }
  573. }
  574. /**
  575. * appends values to template variables by reference
  576. *
  577. * @param string $tpl_var the template variable name
  578. * @param mixed $value the referenced value to append
  579. */
  580. function append_by_ref($tpl_var, &$value, $merge=false)
  581. {
  582. if ($tpl_var != '' && isset($value)) {
  583. if(!@is_array($this->_tpl_vars[$tpl_var])) {
  584. settype($this->_tpl_vars[$tpl_var],'array');
  585. }
  586. if ($merge && is_array($value)) {
  587. foreach($value as $_key => $_val) {
  588. $this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
  589. }
  590. } else {
  591. $this->_tpl_vars[$tpl_var][] = &$value;
  592. }
  593. }
  594. }
  595. /**
  596. * clear the given assigned template variable.
  597. *
  598. * @param string $tpl_var the template variable to clear
  599. */
  600. function clear_assign($tpl_var)
  601. {
  602. if (is_array($tpl_var))
  603. foreach ($tpl_var as $curr_var)
  604. unset($this->_tpl_vars[$curr_var]);
  605. else
  606. unset($this->_tpl_vars[$tpl_var]);
  607. }
  608. /**
  609. * Registers custom function to be used in templates
  610. *
  611. * @param string $function the name of the template function
  612. * @param string $function_impl the name of the PHP function to register
  613. */
  614. function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
  615. {
  616. $this->_plugins['function'][$function] =
  617. array($function_impl, null, null, false, $cacheable, $cache_attrs);
  618. }
  619. /**
  620. * Unregisters custom function
  621. *
  622. * @param string $function name of template function
  623. */
  624. function unregister_function($function)
  625. {
  626. unset($this->_plugins['function'][$function]);
  627. }
  628. /**
  629. * Registers object to be used in templates
  630. *
  631. * @param string $object name of template object
  632. * @param object &$object_impl the referenced PHP object to register
  633. * @param null|array $allowed list of allowed methods (empty = all)
  634. * @param boolean $smarty_args smarty argument format, else traditional
  635. * @param null|array $block_functs list of methods that are block format
  636. */
  637. function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  638. {
  639. settype($allowed, 'array');
  640. settype($smarty_args, 'boolean');
  641. $this->_reg_objects[$object] =
  642. array(&$object_impl, $allowed, $smarty_args, $block_methods);
  643. }
  644. /**
  645. * Unregisters object
  646. *
  647. * @param string $object name of template object
  648. */
  649. function unregister_object($object)
  650. {
  651. unset($this->_reg_objects[$object]);
  652. }
  653. /**
  654. * Registers block function to be used in templates
  655. *
  656. * @param string $block name of template block
  657. * @param string $block_impl PHP function to register
  658. */
  659. function register_block($block, $block_impl, $cacheable=true, $cache_attrs=null)
  660. {
  661. $this->_plugins['block'][$block] =
  662. array($block_impl, null, null, false, $cacheable, $cache_attrs);
  663. }
  664. /**
  665. * Unregisters block function
  666. *
  667. * @param string $block name of template function
  668. */
  669. function unregister_block($block)
  670. {
  671. unset($this->_plugins['block'][$block]);
  672. }
  673. /**
  674. * Registers compiler function
  675. *
  676. * @param string $function name of template function
  677. * @param string $function_impl name of PHP function to register
  678. */
  679. function register_compiler_function($function, $function_impl, $cacheable=true)
  680. {
  681. $this->_plugins['compiler'][$function] =
  682. array($function_impl, null, null, false, $cacheable);
  683. }
  684. /**
  685. * Unregisters compiler function
  686. *
  687. * @param string $function name of template function
  688. */
  689. function unregister_compiler_function($function)
  690. {
  691. unset($this->_plugins['compiler'][$function]);
  692. }
  693. /**
  694. * Registers modifier to be used in templates
  695. *
  696. * @param string $modifier name of template modifier
  697. * @param string $modifier_impl name of PHP function to register
  698. */
  699. function register_modifier($modifier, $modifier_impl)
  700. {
  701. $this->_plugins['modifier'][$modifier] =
  702. array($modifier_impl, null, null, false);
  703. }
  704. /**
  705. * Unregisters modifier
  706. *
  707. * @param string $modifier name of template modifier
  708. */
  709. function unregister_modifier($modifier)
  710. {
  711. unset($this->_plugins['modifier'][$modifier]);
  712. }
  713. /**
  714. * Registers a resource to fetch a template
  715. *
  716. * @param string $type name of resource
  717. * @param array $functions array of functions to handle resource
  718. */
  719. function register_resource($type, $functions)
  720. {
  721. if (count($functions)==4) {
  722. $this->_plugins['resource'][$type] =
  723. array($functions, false);
  724. } elseif (count($functions)==5) {
  725. $this->_plugins['resource'][$type] =
  726. array(array(array(&$functions[0], $functions[1])
  727. ,array(&$functions[0], $functions[2])
  728. ,array(&$functions[0], $functions[3])
  729. ,array(&$functions[0], $functions[4]))
  730. ,false);
  731. } else {
  732. $this->trigger_error("malformed function-list for '$type' in register_resource");
  733. }
  734. }
  735. /**
  736. * Unregisters a resource
  737. *
  738. * @param string $type name of resource
  739. */
  740. function unregister_resource($type)
  741. {
  742. unset($this->_plugins['resource'][$type]);
  743. }
  744. /**
  745. * Registers a prefilter function to apply
  746. * to a template before compiling
  747. *
  748. * @param string $function name of PHP function to register
  749. */
  750. function register_prefilter($function)
  751. {
  752. $_name = (is_array($function)) ? $function[1] : $function;
  753. $this->_plugins['prefilter'][$_name]
  754. = array($function, null, null, false);
  755. }
  756. /**
  757. * Unregisters a prefilter function
  758. *
  759. * @param string $function name of PHP function
  760. */
  761. function unregister_prefilter($function)
  762. {
  763. unset($this->_plugins['prefilter'][$function]);
  764. }
  765. /**
  766. * Registers a postfilter function to apply
  767. * to a compiled template after compilation
  768. *
  769. * @param string $function name of PHP function to register
  770. */
  771. function register_postfilter($function)
  772. {
  773. $_name = (is_array($function)) ? $function[1] : $function;
  774. $this->_plugins['postfilter'][$_name]
  775. = array($function, null, null, false);
  776. }
  777. /**
  778. * Unregisters a postfilter function
  779. *
  780. * @param string $function name of PHP function
  781. */
  782. function unregister_postfilter($function)
  783. {
  784. unset($this->_plugins['postfilter'][$function]);
  785. }
  786. /**
  787. * Registers an output filter function to apply
  788. * to a template output
  789. *
  790. * @param string $function name of PHP function
  791. */
  792. function register_outputfilter($function)
  793. {
  794. $_name = (is_array($function)) ? $function[1] : $function;
  795. $this->_plugins['outputfilter'][$_name]
  796. = array($function, null, null, false);
  797. }
  798. /**
  799. * Unregisters an outputfilter function
  800. *
  801. * @param string $function name of PHP function
  802. */
  803. function unregister_outputfilter($function)
  804. {
  805. unset($this->_plugins['outputfilter'][$function]);
  806. }
  807. /**
  808. * load a filter of specified type and name
  809. *
  810. * @param string $type filter type
  811. * @param string $name filter name
  812. */
  813. function load_filter($type, $name)
  814. {
  815. switch ($type) {
  816. case 'output':
  817. $_params = array('plugins' => array(array($type . 'filter', $name, null, null, false)));
  818. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  819. smarty_core_load_plugins($_params, $this);
  820. break;
  821. case 'pre':
  822. case 'post':
  823. if (!isset($this->_plugins[$type . 'filter'][$name]))
  824. $this->_plugins[$type . 'filter'][$name] = false;
  825. break;
  826. }
  827. }
  828. /**
  829. * clear cached content for the given template and cache id
  830. *
  831. * @param string $tpl_file name of template file
  832. * @param string $cache_id name of cache_id
  833. * @param string $compile_id name of compile_id
  834. * @param string $exp_time expiration time
  835. * @return boolean
  836. */
  837. function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
  838. {
  839. if (!isset($compile_id))
  840. $compile_id = $this->compile_id;
  841. if (!isset($tpl_file))
  842. $compile_id = null;
  843. $_auto_id = $this->_get_auto_id($cache_id, $compile_id);
  844. if (!empty($this->cache_handler_func)) {
  845. return call_user_func_array($this->cache_handler_func,
  846. array('clear', &$this, &$dummy, $tpl_file, $cache_id, $compile_id, $exp_time));
  847. } else {
  848. $_params = array('auto_base' => $this->cache_dir,
  849. 'auto_source' => $tpl_file,
  850. 'auto_id' => $_auto_id,
  851. 'exp_time' => $exp_time);
  852. require_once(SMARTY_CORE_DIR . 'core.rm_auto.php');
  853. return smarty_core_rm_auto($_params, $this);
  854. }
  855. }
  856. /**
  857. * clear the entire contents of cache (all templates)
  858. *
  859. * @param string $exp_time expire time
  860. * @return boolean results of {@link smarty_core_rm_auto()}
  861. */
  862. function clear_all_cache($exp_time = null)
  863. {
  864. return $this->clear_cache(null, null, null, $exp_time);
  865. }
  866. /**
  867. * test to see if valid cache exists for this template
  868. *
  869. * @param string $tpl_file name of template file
  870. * @param string $cache_id
  871. * @param string $compile_id
  872. * @return string|false results of {@link _read_cache_file()}
  873. */
  874. function is_cached($tpl_file, $cache_id = null, $compile_id = null)
  875. {
  876. if (!$this->caching)
  877. return false;
  878. if (!isset($compile_id))
  879. $compile_id = $this->compile_id;
  880. $_params = array(
  881. 'tpl_file' => $tpl_file,
  882. 'cache_id' => $cache_id,
  883. 'compile_id' => $compile_id
  884. );
  885. require_once(SMARTY_CORE_DIR . 'core.read_cache_file.php');
  886. return smarty_core_read_cache_file($_params, $this);
  887. }
  888. /**
  889. * clear all the assigned template variables.
  890. *
  891. */
  892. function clear_all_assign()
  893. {
  894. $this->_tpl_vars = array();
  895. }
  896. /**
  897. * clears compiled version of specified template resource,
  898. * or all compiled template files if one is not specified.
  899. * This function is for advanced use only, not normally needed.
  900. *
  901. * @param string $tpl_file
  902. * @param string $compile_id
  903. * @param string $exp_time
  904. * @return boolean results of {@link smarty_core_rm_auto()}
  905. */
  906. function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
  907. {
  908. if (!isset($compile_id)) {
  909. $compile_id = $this->compile_id;
  910. }
  911. $_params = array('auto_base' => $this->compile_dir,
  912. 'auto_source' => $tpl_file,
  913. 'auto_id' => $compile_id,
  914. 'exp_time' => $exp_time,
  915. 'extensions' => array('.inc', '.php'));
  916. require_once(SMARTY_CORE_DIR . 'core.rm_auto.php');
  917. return smarty_core_rm_auto($_params, $this);
  918. }
  919. /**
  920. * Checks whether requested template exists.
  921. *
  922. * @param string $tpl_file
  923. * @return boolean
  924. */
  925. function template_exists($tpl_file)
  926. {
  927. $_params = array('resource_name' => $tpl_file, 'quiet'=>true, 'get_source'=>false);
  928. return $this->_fetch_resource_info($_params);
  929. }
  930. /**
  931. * Returns an array containing template variables
  932. *
  933. * @param string $name
  934. * @param string $type
  935. * @return array
  936. */
  937. function &get_template_vars($name=null)
  938. {
  939. if(!isset($name)) {
  940. return $this->_tpl_vars;
  941. } elseif(isset($this->_tpl_vars[$name])) {
  942. return $this->_tpl_vars[$name];
  943. } else {
  944. // var non-existant, return valid reference
  945. $_tmp = null;
  946. return $_tmp;
  947. }
  948. }
  949. /**
  950. * Returns an array containing config variables
  951. *
  952. * @param string $name
  953. * @param string $type
  954. * @return array
  955. */
  956. function &get_config_vars($name=null)
  957. {
  958. if(!isset($name) && is_array($this->_config[0])) {
  959. return $this->_config[0]['vars'];
  960. } else if(isset($this->_config[0]['vars'][$name])) {
  961. return $this->_config[0]['vars'][$name];
  962. } else {
  963. // var non-existant, return valid reference
  964. $_tmp = null;
  965. return $_tmp;
  966. }
  967. }
  968. /**
  969. * trigger Smarty error
  970. *
  971. * @param string $error_msg
  972. * @param integer $error_type
  973. */
  974. function trigger_error($error_msg, $error_type = E_USER_WARNING)
  975. {
  976. trigger_error("Smarty error: $error_msg", $error_type);
  977. }
  978. /**
  979. * executes & displays the template results
  980. *
  981. * @param string $resource_name
  982. * @param string $cache_id
  983. * @param string $compile_id
  984. */
  985. function display($resource_name, $cache_id = null, $compile_id = null)
  986. {
  987. $this->fetch($resource_name, $cache_id, $compile_id, true);
  988. }
  989. /**
  990. * executes & returns or displays the template results
  991. *
  992. * @param string $resource_name
  993. * @param string $cache_id
  994. * @param string $compile_id
  995. * @param boolean $display
  996. */
  997. function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
  998. {
  999. static $_cache_info = array();
  1000. $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting)
  1001. ? $this->error_reporting : error_reporting() & ~E_NOTICE);
  1002. if (!$this->debugging && $this->debugging_ctrl == 'URL') {
  1003. $_query_string = $this->request_use_auto_globals ? $_SERVER['QUERY_STRING'] : $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'];
  1004. if (@strstr($_query_string, $this->_smarty_debug_id)) {
  1005. if (@strstr($_query_string, $this->_smarty_debug_id . '=on')) {
  1006. // enable debugging for this browser session
  1007. @setcookie('SMARTY_DEBUG', true);
  1008. $this->debugging = true;
  1009. } elseif (@strstr($_query_string, $this->_smarty_debug_id . '=off')) {
  1010. // disable debugging for this browser session
  1011. @setcookie('SMARTY_DEBUG', false);
  1012. $this->debugging = false;
  1013. } else {
  1014. // enable debugging for this page
  1015. $this->debugging = true;
  1016. }
  1017. } else {
  1018. $this->debugging = (bool)($this->request_use_auto_globals ? @$_COOKIE['SMARTY_DEBUG'] : @$GLOBALS['HTTP_COOKIE_VARS']['SMARTY_DEBUG']);
  1019. }
  1020. }
  1021. if ($this->debugging) {
  1022. // capture time for debugging info
  1023. $_params = array();
  1024. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1025. $_debug_start_time = smarty_core_get_microtime($_params, $this);
  1026. $this->_smarty_debug_info[] = array('type' => 'template',
  1027. 'filename' => $resource_name,
  1028. 'depth' => 0);
  1029. $_included_tpls_idx = count($this->_smarty_debug_info) - 1;
  1030. }
  1031. if (!isset($compile_id)) {
  1032. $compile_id = $this->compile_id;
  1033. }
  1034. $this->_compile_id = $compile_id;
  1035. $this->_inclusion_depth = 0;
  1036. if ($this->caching) {
  1037. // save old cache_info, initialize cache_info
  1038. array_push($_cache_info, $this->_cache_info);
  1039. $this->_cache_info = array();
  1040. $_params = array(
  1041. 'tpl_file' => $resource_name,
  1042. 'cache_id' => $cache_id,
  1043. 'compile_id' => $compile_id,
  1044. 'results' => null
  1045. );
  1046. require_once(SMARTY_CORE_DIR . 'core.read_cache_file.php');
  1047. if (smarty_core_read_cache_file($_params, $this)) {
  1048. $_smarty_results = $_params['results'];
  1049. if (!empty($this->_cache_info['insert_tags'])) {
  1050. $_params = array('plugins' => $this->_cache_info['insert_tags']);
  1051. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1052. smarty_core_load_plugins($_params, $this);
  1053. $_params = array('results' => $_smarty_results);
  1054. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  1055. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  1056. }
  1057. if (!empty($this->_cache_info['cache_serials'])) {
  1058. $_params = array('results' => $_smarty_results);
  1059. require_once(SMARTY_CORE_DIR . 'core.process_compiled_include.php');
  1060. $_smarty_results = smarty_core_process_compiled_include($_params, $this);
  1061. }
  1062. if ($display) {
  1063. if ($this->debugging)
  1064. {
  1065. // capture time for debugging info
  1066. $_params = array();
  1067. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1068. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $_debug_start_time;
  1069. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  1070. $_smarty_results .= smarty_core_display_debug_console($_params, $this);
  1071. }
  1072. if ($this->cache_modified_check) {
  1073. $_server_vars = ($this->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  1074. $_last_modified_date = @substr($_server_vars['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_server_vars['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  1075. $_gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']).' GMT';
  1076. if (@count($this->_cache_info['insert_tags']) == 0
  1077. && !$this->_cache_serials
  1078. && $_gmt_mtime == $_last_modified_date) {
  1079. if (php_sapi_name()=='cgi')
  1080. header('Status: 304 Not Modified');
  1081. else
  1082. header('HTTP/1.1 304 Not Modified');
  1083. } else {
  1084. header('Last-Modified: '.$_gmt_mtime);
  1085. echo $_smarty_results;
  1086. }
  1087. } else {
  1088. echo $_smarty_results;
  1089. }
  1090. error_reporting($_smarty_old_error_level);
  1091. // restore initial cache_info
  1092. $this->_cache_info = array_pop($_cache_info);
  1093. return true;
  1094. } else {
  1095. error_reporting($_smarty_old_error_level);
  1096. // restore initial cache_info
  1097. $this->_cache_info = array_pop($_cache_info);
  1098. return $_smarty_results;
  1099. }
  1100. } else {
  1101. $this->_cache_info['template'][$resource_name] = true;
  1102. if ($this->cache_modified_check && $display) {
  1103. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1104. }
  1105. }
  1106. }
  1107. // load filters that are marked as autoload
  1108. if (count($this->autoload_filters)) {
  1109. foreach ($this->autoload_filters as $_filter_type => $_filters) {
  1110. foreach ($_filters as $_filter) {
  1111. $this->load_filter($_filter_type, $_filter);
  1112. }
  1113. }
  1114. }
  1115. $_smarty_compile_path = $this->_get_compile_path($resource_name);
  1116. // if we just need to display the results, don't perform output
  1117. // buffering - for speed
  1118. $_cache_including = $this->_cache_including;
  1119. $this->_cache_including = false;
  1120. if ($display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
  1121. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  1122. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  1123. {
  1124. include($_smarty_compile_path);
  1125. }
  1126. } else {
  1127. ob_start();
  1128. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  1129. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  1130. {
  1131. include($_smarty_compile_path);
  1132. }
  1133. $_smarty_results = ob_get_contents();
  1134. ob_end_clean();
  1135. foreach ((array)$this->_plugins['outputfilter'] as $_output_filter) {
  1136. $_smarty_results = call_user_func_array($_output_filter[0], array($_smarty_results, &$this));
  1137. }
  1138. }
  1139. if ($this->caching) {
  1140. $_params = array('tpl_file' => $resource_name,
  1141. 'cache_id' => $cache_id,
  1142. 'compile_id' => $compile_id,
  1143. 'results' => $_smarty_results);
  1144. require_once(SMARTY_CORE_DIR . 'core.write_cache_file.php');
  1145. smarty_core_write_cache_file($_params, $this);
  1146. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  1147. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  1148. if ($this->_cache_serials) {
  1149. // strip nocache-tags from output
  1150. $_smarty_results = preg_replace('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!s'
  1151. ,''
  1152. ,$_smarty_results);
  1153. }
  1154. // restore initial cache_info
  1155. $this->_cache_info = array_pop($_cache_info);
  1156. }
  1157. $this->_cache_including = $_cache_including;
  1158. if ($display) {
  1159. if (isset($_smarty_results)) { echo $_smarty_results; }
  1160. if ($this->debugging) {
  1161. // capture time for debugging info
  1162. $_params = array();
  1163. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1164. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
  1165. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  1166. echo smarty_core_display_debug_console($_params, $this);
  1167. }
  1168. error_reporting($_smarty_old_error_level);
  1169. return;
  1170. } else {
  1171. error_reporting($_smarty_old_error_level);
  1172. if (isset($_smarty_results)) { return $_smarty_results; }
  1173. }
  1174. }
  1175. /**
  1176. * load configuration values
  1177. *
  1178. * @param string $file
  1179. * @param string $section
  1180. * @param string $scope
  1181. */
  1182. function config_load($file, $section = null, $scope = 'global')
  1183. {
  1184. require_once($this->_get_plugin_filepath('function', 'config_load'));
  1185. smarty_function_config_load(array('file' => $file, 'section' => $section, 'scope' => $scope), $this);
  1186. }
  1187. /**
  1188. * return a reference to a registered object
  1189. *
  1190. * @param string $name
  1191. * @return object
  1192. */
  1193. function &get_registered_object($name) {
  1194. if (!isset($this->_reg_objects[$name]))
  1195. $this->_trigger_fatal_error("'$name' is not a registered object");
  1196. if (!is_object($this->_reg_objects[$name][0]))
  1197. $this->_trigger_fatal_error("registered '$name' is not an object");
  1198. return $this->_reg_objects[$name][0];
  1199. }
  1200. /**
  1201. * clear configuration values
  1202. *
  1203. * @param string $var
  1204. */
  1205. function clear_config($var = null)
  1206. {
  1207. if(!isset($var)) {
  1208. // clear all values
  1209. $this->_config = array(array('vars' => array(),
  1210. 'files' => array()));
  1211. } else {
  1212. unset($this->_config[0]['vars'][$var]);
  1213. }
  1214. }
  1215. /**
  1216. * get filepath of requested plugin
  1217. *
  1218. * @param string $type
  1219. * @param string $name
  1220. * @return string|false
  1221. */
  1222. function _get_plugin_filepath($type, $name)
  1223. {
  1224. $_params = array('type' => $type, 'name' => $name);
  1225. require_once(SMARTY_CORE_DIR . 'core.assemble_plugin_filepath.php');
  1226. return smarty_core_assemble_plugin_filepath($_params, $this);
  1227. }
  1228. /**
  1229. * test if resource needs compiling
  1230. *
  1231. * @param string $resource_name
  1232. * @param string $compile_path
  1233. * @return boolean
  1234. */
  1235. function _is_compiled($resource_name, $compile_path)
  1236. {
  1237. if (!$this->force_compile && file_exists($compile_path)) {
  1238. if (!$this->compile_check) {
  1239. // no need to check compiled file
  1240. return true;
  1241. } else {
  1242. // get file source and timestamp
  1243. $_params = array('resource_name' => $resource_name, 'get_source'=>false);
  1244. if (!$this->_fetch_resource_info($_params)) {
  1245. return false;
  1246. }
  1247. if ($_params['resource_timestamp'] <= filemtime($compile_path)) {
  1248. // template not expired, no recompile
  1249. return true;
  1250. } else {
  1251. // compile template
  1252. return false;
  1253. }
  1254. }
  1255. } else {
  1256. // compiled template does not exist, or forced compile
  1257. return false;
  1258. }
  1259. }
  1260. /**
  1261. * compile the template
  1262. *
  1263. * @param string $resource_name
  1264. * @param string $compile_path
  1265. * @return boolean
  1266. */
  1267. function _compile_resource($resource_name, $compile_path)
  1268. {
  1269. $_params = array('resource_name' => $resource_name);
  1270. if (!$this->_fetch_resource_info($_params)) {
  1271. return false;
  1272. }
  1273. $_source_content = $_params['source_content'];
  1274. $_cache_include = substr($compile_path, 0, -4).'.inc';
  1275. if ($this->_compile_source($resource_name, $_source_content, $_compiled_content, $_cache_include)) {
  1276. // if a _cache_serial was set, we also have to write an include-file:
  1277. if ($this->_cache_include_info) {
  1278. require_once(SMARTY_CORE_DIR . 'core.write_compiled_include.php');
  1279. smarty_core_write_compiled_include(array_merge($this->_cache_include_info, array('compiled_content'=>$_compiled_content, 'resource_name'=>$resource_name)), $this);
  1280. }
  1281. $_params = array('compile_path'=>$compile_path, 'compiled_content' => $_compiled_content);
  1282. require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
  1283. smarty_core_write_compiled_resource($_params, $this);
  1284. return true;
  1285. } else {
  1286. return false;
  1287. }
  1288. }
  1289. /**
  1290. * compile the given source
  1291. *
  1292. * @param string $resource_name
  1293. * @param string $source_content
  1294. * @param string $compiled_content
  1295. * @return boolean
  1296. */
  1297. function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null)
  1298. {
  1299. if (file_exists(SMARTY_DIR . $this->compiler_file)) {
  1300. require_once(SMARTY_DIR . $this->compiler_file);
  1301. } else {
  1302. // use include_path
  1303. require_once($this->compiler_file);
  1304. }
  1305. $smarty_compiler = new $this->compiler_class;
  1306. $smarty_compiler->template_dir = $this->template_dir;
  1307. $smarty_compiler->compile_dir = $this->compile_dir;
  1308. $smarty_compiler->plugins_dir = $this->plugins_dir;
  1309. $smarty_compiler->config_dir = $this->config_dir;
  1310. $smarty_compiler->force_compile = $this->force_compile;
  1311. $smarty_compiler->caching = $this->caching;
  1312. $smarty_compiler->php_handling = $this->php_handling;
  1313. $smarty_compiler->left_delimiter = $this->left_delimiter;
  1314. $smarty_compiler->right_delimiter = $this->right_delimiter;
  1315. $smarty_compiler->_version = $this->_version;
  1316. $smarty_compiler->security = $this->security;
  1317. $smarty_compiler->secure_dir = $this->secure_dir;
  1318. $smarty_compiler->security_settings = $this->security_settings;
  1319. $smarty_compiler->trusted_dir = $this->trusted_dir;
  1320. $smarty_compiler->use_sub_dirs = $this->use_sub_dirs;
  1321. $smarty_compiler->_reg_objects = &$this->_reg_objects;
  1322. $smarty_compiler->_plugins = &$this->_plugins;
  1323. $smarty_compiler->_tpl_vars = &$this->_tpl_vars;
  1324. $smarty_compiler->default_modifiers = $this->default_modifiers;
  1325. $smarty_compiler->compile_id = $this->_compile_id;
  1326. $smarty_compiler->_config = $this->_config;
  1327. $smarty_compiler->request_use_auto_globals = $this->request_use_auto_globals;
  1328. if (isset($cache_include_path) && isset($this->_cache_serials[$cache_include_path])) {
  1329. $smarty_compiler->_cache_serial = $this->_cache_serials[$cache_include_path];
  1330. }
  1331. $smarty_compiler->_cache_include = $cache_include_path;
  1332. $_results = $smarty_compiler->_compile_file($resource_name, $source_content, $compiled_content);
  1333. if ($smarty_compiler->_cache_serial) {
  1334. $this->_cache_include_info = array(
  1335. 'cache_serial'=>$smarty_compiler->_cache_serial
  1336. ,'plugins_code'=>$smarty_compiler->_plugins_code
  1337. ,'include_file_path' => $cache_include_path);
  1338. } else {
  1339. $this->_cache_include_info = null;
  1340. }
  1341. return $_results;
  1342. }
  1343. /**
  1344. * Get the compile path for this resource
  1345. *
  1346. * @param string $resource_name
  1347. * @return string results of {@link _get_auto_filename()}
  1348. */
  1349. function _get_compile_path($resource_name)
  1350. {
  1351. return $this->_get_auto_filename($this->compile_dir, $resource_name,
  1352. $this->_compile_id) . '.php';
  1353. }
  1354. /**
  1355. * fetch the template info. Gets timestamp, and source
  1356. * if get_source is true
  1357. *
  1358. * sets $source_content to the source of the template, and
  1359. * $resource_timestamp to its time stamp
  1360. * @param string $resource_name
  1361. * @param string $source_content
  1362. * @param integer $resource_timestamp
  1363. * @param boolean $get_source
  1364. * @param boolean $quiet
  1365. * @return boolean
  1366. */
  1367. function _fetch_resource_info(&$params)
  1368. {
  1369. if(!isset($params['get_source'])) { $params['get_source'] = true; }
  1370. if(!isset($params['quiet'])) { $params['quiet'] = false; }
  1371. $_return = false;
  1372. $_params = array('resource_name' => $params['resource_name']) ;
  1373. if (isset($params['resource_base_path']))
  1374. $_params['resource_base_path'] = $params['resource_base_path'];
  1375. else
  1376. $_params['resource_base_path'] = $this->template_dir;
  1377. if ($this->_parse_resource_name($_params)) {
  1378. $_resource_type = $_params['resource_type'];
  1379. $_resource_name = $_params['resource_name'];
  1380. switch ($_resource_type) {
  1381. case 'file':
  1382. if ($params['get_source']) {
  1383. $params['source_content'] = $this->_read_file($_resource_name);
  1384. }
  1385. $params['resource_timestamp'] = filemtime($_resource_name);
  1386. $_return = is_file($_resource_name);
  1387. break;
  1388. default:
  1389. // call resource functions to fetch the template source and timestamp
  1390. if ($params['get_source']) {
  1391. $_source_return = isset($this->_plugins['resource'][$_resource_type]) &&
  1392. call_user_func_array($this->_plugins['resource'][$_resource_type][0][0],
  1393. array($_resource_name, &$params['source_content'], &$this));
  1394. } else {
  1395. $_source_return = true;
  1396. }
  1397. $_timestamp_return = isset($this->_plugins['resource'][$_resource_type]) &&
  1398. call_user_func_array($this->_plugins['resource'][$_resource_type][0][1],
  1399. array($_resource_name, &$params['resource_timestamp'], &$this));
  1400. $_return = $_source_return && $_timestamp_return;
  1401. break;
  1402. }
  1403. }
  1404. if (!$_return) {
  1405. // see if we can get a template with the default template handler
  1406. if (!empty($this->default_template_handler_func)) {
  1407. if (!is_callable($this->default_template_handler_func)) {
  1408. $this->trigger_error("default template handler function \"$this->default_template_handler_func\" doesn't exist.");
  1409. } else {
  1410. $_return = call_user_func_array(
  1411. $this->default_template_handler_func,
  1412. array($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
  1413. }
  1414. }
  1415. }
  1416. if (!$_return) {
  1417. if (!$params['quiet']) {
  1418. $this->trigger_error('unable to read resource: "' . $params['resource_name'] . '"');
  1419. }
  1420. } else if ($_return && $this->security) {
  1421. require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
  1422. if (!smarty_core_is_secure($_params, $this)) {
  1423. if (!$params['quiet'])
  1424. $this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
  1425. $params['source_content'] = null;
  1426. $params['resource_timestamp'] = null;
  1427. return false;
  1428. }
  1429. }
  1430. return $_return;
  1431. }
  1432. /**
  1433. * parse out the type and name from the resource
  1434. *
  1435. * @param string $resource_base_path
  1436. * @param string $resource_name
  1437. * @param string $resource_type
  1438. * @param string $resource_name
  1439. * @return boolean
  1440. */
  1441. function _parse_resource_name(&$params)
  1442. {
  1443. // split tpl_path by the first colon
  1444. $_resource_name_parts = explode(':', $params['resource_name'], 2);
  1445. if (count($_resource_name_parts) == 1) {
  1446. // no resource type given
  1447. $params['resource_type'] = $this->default_resource_type;
  1448. $params['resource_name'] = $_resource_name_parts[0];
  1449. } else {
  1450. if(strlen($_resource_name_parts[0]) == 1) {
  1451. // 1 char is not resource type, but part of filepath
  1452. $params['resource_type'] = $this->default_resource_type;
  1453. $params['resource_name'] = $params['resource_name'];
  1454. } else {
  1455. $params['resource_type'] = $_resource_name_parts[0];
  1456. $params['resource_name'] = $_resource_name_parts[1];
  1457. }
  1458. }
  1459. if ($params['resource_type'] == 'file') {
  1460. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $params['resource_name'])) {
  1461. // relative pathname to $params['resource_base_path']
  1462. // use the first directory where the file is found
  1463. foreach ((array)$params['resource_base_path'] as $_curr_path) {
  1464. $_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params['resource_name'];
  1465. if (file_exists($_fullpath) && is_file($_fullpath)) {
  1466. $params['resource_name'] = $_fullpath;
  1467. return true;
  1468. }
  1469. // didn't find the file, try include_path
  1470. $_params = array('file_path' => $_fullpath);
  1471. require_once(SMARTY_CORE_DIR . 'core.get_include_path.php');
  1472. if(smarty_core_get_include_path($_params, $this)) {
  1473. $params['resource_name'] = $_params['new_file_path'];
  1474. return true;
  1475. }
  1476. }
  1477. return false;
  1478. } else {
  1479. /* absolute path */
  1480. return file_exists($params['resource_name']);
  1481. }
  1482. } elseif (empty($this->_plugins['resource'][$params['resource_type']])) {
  1483. $_params = array('type' => $params['resource_type']);
  1484. require_once(SMARTY_CORE_DIR . 'core.load_resource_plugin.php');
  1485. smarty_core_load_resource_plugin($_params, $this);
  1486. }
  1487. return true;
  1488. }
  1489. /**
  1490. * Handle modifiers
  1491. *
  1492. * @param string|null $modifier_name
  1493. * @param array|null $map_array
  1494. * @return string result of modifiers
  1495. */
  1496. function _run_mod_handler()
  1497. {
  1498. $_args = func_get_args();
  1499. list($_modifier_name, $_map_array) = array_splice($_args, 0, 2);
  1500. list($_func_name, $_tpl_file, $_tpl_line) =
  1501. $this->_plugins['modifier'][$_modifier_name];
  1502. $_var = $_args[0];
  1503. foreach ($_var as $_key => $_val) {
  1504. $_args[0] = $_val;
  1505. $_var[$_key] = call_user_func_array($_func_name, $_args);
  1506. }
  1507. return $_var;
  1508. }
  1509. /**
  1510. * Remove starting and ending quotes from the string
  1511. *
  1512. * @param string $string
  1513. * @return string
  1514. */
  1515. function _dequote($string)
  1516. {
  1517. if ((substr($string, 0, 1) == "'" || substr($string, 0, 1) == '"') &&
  1518. substr($string, -1) == substr($string, 0, 1))
  1519. return substr($string, 1, -1);
  1520. else
  1521. return $string;
  1522. }
  1523. /**
  1524. * read in a file
  1525. *
  1526. * @param string $filename
  1527. * @return string
  1528. */
  1529. function _read_file($filename)
  1530. {
  1531. if ( file_exists($filename) && ($fd = @fopen($filename, 'rb')) ) {
  1532. $contents = '';
  1533. while (!feof($fd)) {
  1534. $contents .= fread($fd, 8192);
  1535. }
  1536. fclose($fd);
  1537. return $contents;
  1538. } else {
  1539. return false;
  1540. }
  1541. }
  1542. /**
  1543. * get a concrete filename for automagically created content
  1544. *
  1545. * @param string $auto_base
  1546. * @param string $auto_source
  1547. * @param string $auto_id
  1548. * @return string
  1549. * @staticvar string|null
  1550. * @staticvar string|null
  1551. */
  1552. function _get_auto_filename($auto_base, $auto_source = null, $auto_id = null)
  1553. {
  1554. $_compile_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
  1555. $_return = $auto_base . DIRECTORY_SEPARATOR;
  1556. if(isset($auto_id)) {
  1557. // make auto_id safe for directory names
  1558. $auto_id = str_replace('%7C',$_compile_dir_sep,(urlencode($auto_id)));
  1559. // split into separate directories
  1560. $_return .= $auto_id . $_compile_dir_sep;
  1561. }
  1562. if(isset($auto_source)) {
  1563. // make source name safe for filename
  1564. $_filename = urlencode(basename($auto_source));
  1565. $_crc32 = sprintf('%08X', crc32($auto_source));
  1566. // prepend %% to avoid name conflicts with
  1567. // with $params['auto_id'] names
  1568. $_crc32 = substr($_crc32, 0, 2) . $_compile_dir_sep .
  1569. substr($_crc32, 0, 3) . $_compile_dir_sep . $_crc32;
  1570. $_return .= '%%' . $_crc32 . '%%' . $_filename;
  1571. }
  1572. return $_return;
  1573. }
  1574. /**
  1575. * unlink a file, possibly using expiration time
  1576. *
  1577. * @param string $resource
  1578. * @param integer $exp_time
  1579. */
  1580. function _unlink($resource, $exp_time = null)
  1581. {
  1582. if(isset($exp_time)) {
  1583. if(time() - @filemtime($resource) >= $exp_time) {
  1584. return @unlink($resource);
  1585. }
  1586. } else {
  1587. return @unlink($resource);
  1588. }
  1589. }
  1590. /**
  1591. * returns an auto_id for auto-file-functions
  1592. *
  1593. * @param string $cache_id
  1594. * @param string $compile_id
  1595. * @return string|null
  1596. */
  1597. function _get_auto_id($cache_id=null, $compile_id=null) {
  1598. if (isset($cache_id))
  1599. return (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
  1600. elseif(isset($compile_id))
  1601. return $compile_id;
  1602. else
  1603. return null;
  1604. }
  1605. /**
  1606. * trigger Smarty plugin error
  1607. *
  1608. * @param string $error_msg
  1609. * @param string $tpl_file
  1610. * @param integer $tpl_line
  1611. * @param string $file
  1612. * @param integer $line
  1613. * @param integer $error_type
  1614. */
  1615. function _trigger_fatal_error($error_msg, $tpl_file = null, $tpl_line = null,
  1616. $file = null, $line = null, $error_type = E_USER_ERROR)
  1617. {
  1618. if(isset($file) && isset($line)) {
  1619. $info = ' ('.basename($file).", line $line)";
  1620. } else {
  1621. $info = '';
  1622. }
  1623. if (isset($tpl_line) && isset($tpl_file)) {
  1624. $this->trigger_error('[in ' . $tpl_file . ' line ' . $tpl_line . "]: $error_msg$info", $error_type);
  1625. } else {
  1626. $this->trigger_error($error_msg . $info, $error_type);
  1627. }
  1628. }
  1629. /**
  1630. * callback function for preg_replace, to call a non-cacheable block
  1631. * @return string
  1632. */
  1633. function _process_compiled_include_callback($match) {
  1634. $_func = '_smarty_tplfunc_'.$match[2].'_'.$match[3];
  1635. ob_start();
  1636. $_func($this);
  1637. $_ret = ob_get_contents();
  1638. ob_end_clean();
  1639. return $_ret;
  1640. }
  1641. /**
  1642. * called for included templates
  1643. *
  1644. * @param string $_smarty_include_tpl_file
  1645. * @param string $_smarty_include_vars
  1646. */
  1647. // $_smarty_include_tpl_file, $_smarty_include_vars
  1648. function _smarty_include($params)
  1649. {
  1650. if ($this->debugging) {
  1651. $_params = array();
  1652. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1653. $debug_start_time = smarty_core_get_microtime($_params, $this);
  1654. $this->_smarty_debug_info[] = array('type' => 'template',
  1655. 'filename' => $params['smarty_include_tpl_file'],
  1656. 'depth' => ++$this->_inclusion_depth);
  1657. $included_tpls_idx = count($this->_smarty_debug_info) - 1;
  1658. }
  1659. $this->_tpl_vars = array_merge($this->_tpl_vars, $params['smarty_include_vars']);
  1660. // config vars are treated as local, so push a copy of the
  1661. // current ones onto the front of the stack
  1662. array_unshift($this->_config, $this->_config[0]);
  1663. $_smarty_compile_path = $this->_get_compile_path($params['smarty_include_tpl_file']);
  1664. if ($this->_is_compiled($params['smarty_include_tpl_file'], $_smarty_compile_path)
  1665. || $this->_compile_resource($params['smarty_include_tpl_file'], $_smarty_compile_path))
  1666. {
  1667. include($_smarty_compile_path);
  1668. }
  1669. // pop the local vars off the front of the stack
  1670. array_shift($this->_config);
  1671. $this->_inclusion_depth--;
  1672. if ($this->debugging) {
  1673. // capture time for debugging info
  1674. $_params = array();
  1675. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1676. $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $debug_start_time;
  1677. }
  1678. if ($this->caching) {
  1679. $this->_cache_info['template'][$params['smarty_include_tpl_file']] = true;
  1680. }
  1681. }
  1682. /**
  1683. * get or set an array of cached attributes for function that is
  1684. * not cacheable
  1685. * @return array
  1686. */
  1687. function &_smarty_cache_attrs($cache_serial, $count) {
  1688. $_cache_attrs =& $this->_cache_info['cache_attrs'][$cache_serial][$count];
  1689. if ($this->_cache_including) {
  1690. /* return next set of cache_attrs */
  1691. $_return = current($_cache_attrs);
  1692. next($_cache_attrs);
  1693. return $_return;
  1694. } else {
  1695. /* add a reference to a new set of cache_attrs */
  1696. $_cache_attrs[] = array();
  1697. return $_cache_attrs[count($_cache_attrs)-1];
  1698. }
  1699. }
  1700. /**
  1701. * wrapper for include() retaining $this
  1702. * @return mixed
  1703. */
  1704. function _include($filename, $once=false, $params=null)
  1705. {
  1706. if ($once) {
  1707. return include_once($filename);
  1708. } else {
  1709. return include($filename);
  1710. }
  1711. }
  1712. /**
  1713. * wrapper for eval() retaining $this
  1714. * @return mixed
  1715. */
  1716. function _eval($code, $params=null)
  1717. {
  1718. return eval($code);
  1719. }
  1720. /**#@-*/
  1721. }
  1722. /* vim: set expandtab: */
  1723. ?>