class.template.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <?php
  2. /*
  3. * Project: template_lite, a smarter template engine
  4. * File: class.template.php
  5. * Author: Paul Lockaby <paul@paullockaby.com>, Mark Dickenson <akapanamajack@sourceforge.net>
  6. * Copyright: 2003,2004,2005 by Paul Lockaby, 2005,2006 Mark Dickenson
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * The latest version of template_lite can be obtained from:
  23. * http://templatelite.sourceforge.net
  24. *
  25. */
  26. if (!defined('TEMPLATE_LITE_DIR')) {
  27. define('TEMPLATE_LITE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  28. }
  29. class Template_Lite {
  30. // public configuration variables
  31. var $left_delimiter = "{"; // the left delimiter for template tags
  32. var $right_delimiter = "}"; // the right delimiter for template tags
  33. var $cache = false; // whether or not to allow caching of files
  34. var $force_compile = false; // force a compile regardless of saved state
  35. var $template_dir = "templates"; // where the templates are to be found
  36. var $plugins_dir = array("plugins"); // where the plugins are to be found
  37. var $compile_dir = "compiled"; // the directory to store the compiled files in
  38. var $config_dir = "templates"; // where the config files are
  39. var $cache_dir = "cached"; // where cache files are stored
  40. var $config_overwrite = false;
  41. var $config_booleanize = true;
  42. var $config_fix_new_lines = true;
  43. var $config_read_hidden = true;
  44. var $cache_lifetime = 0; // how long the file in cache should be considered "fresh"
  45. var $encode_file_name = true; // Set this to false if you do not want the name of the compiled/cached file to be md5 encoded.
  46. var $php_extract_vars = false; // Set this to true if you want the $this->_tpl variables to be extracted for use by PHP code inside the template.
  47. var $reserved_template_varname = "templatelite";
  48. var $default_modifiers = array();
  49. var $debugging = false;
  50. var $compiler_file = 'class.compiler.php';
  51. var $compiler_class = 'Template_Lite_Compiler';
  52. var $config_class = 'config';
  53. // gzip output configuration
  54. var $send_now = 1;
  55. var $force_compression = 0;
  56. var $compression_level = 9;
  57. var $enable_gzip = 1;
  58. // private internal variables
  59. var $_vars = array(); // stores all internal assigned variables
  60. var $_confs = array(); // stores all internal config variables
  61. var $_plugins = array( 'modifier' => array(),
  62. 'function' => array(),
  63. 'block' => array(),
  64. 'compiler' => array(),
  65. 'resource' => array(),
  66. 'prefilter' => array(),
  67. 'postfilter' => array(),
  68. 'outputfilter' => array());
  69. var $_linenum = 0; // the current line number in the file we are processing
  70. var $_file = ""; // the current file we are processing
  71. var $_config_obj = null;
  72. var $_compile_obj = null;
  73. var $_cache_id = null;
  74. var $_cache_dir = ""; // stores where this specific file is going to be cached
  75. var $_cache_info = array('config' => array(), 'template' => array());
  76. var $_sl_md5 = '39fc70570b8b60cbc1b85839bf242aff';
  77. var $_version = 'V2.10 Template Lite 4 January 2007 (c) 2005-2007 Mark Dickenson. All rights reserved. Released LGPL.';
  78. var $_version_date = "2007-01-04 10:34:21";
  79. var $_config_module_loaded = false;
  80. var $_templatelite_debug_info = array();
  81. var $_templatelite_debug_loop = false;
  82. var $_templatelite_debug_dir = "";
  83. var $_inclusion_depth = 0;
  84. var $_null = null;
  85. var $_resource_type = 1;
  86. var $_resource_time;
  87. var $_sections = array();
  88. var $_foreach = array();
  89. function Template_Lite()
  90. {
  91. $this->_version_date = strtotime($this->_version_date);
  92. }
  93. function load_filter($type, $name)
  94. {
  95. switch ($type)
  96. {
  97. case 'output':
  98. include_once( $this->_get_plugin_dir($type . "filter." . $name . ".php") . $type . "filter." . $name . ".php");
  99. $this->_plugins['outputfilter'][$name] = "template_" . $type . "filter_" . $name;
  100. break;
  101. case 'pre':
  102. case 'post':
  103. if (!isset($this->_plugins[$type . 'filter'][$name]))
  104. {
  105. $this->_plugins[$type . 'filter'][$name] = "template_" . $type . "filter_" . $name;
  106. }
  107. break;
  108. }
  109. }
  110. function assign($key, $value = null)
  111. {
  112. if (is_array($key))
  113. {
  114. foreach($key as $var => $val)
  115. if ($var != "")
  116. {
  117. $this->_vars[$var] = $val;
  118. }
  119. }
  120. else
  121. {
  122. if ($key != "")
  123. {
  124. $this->_vars[$key] = $value;
  125. }
  126. }
  127. }
  128. function assign_by_ref($key, $value = null)
  129. {
  130. if ($key != '')
  131. {
  132. $this->_vars[$key] = &$value;
  133. }
  134. }
  135. function assign_config($key, $value = null)
  136. {
  137. if (is_array($key))
  138. {
  139. foreach($key as $var => $val)
  140. {
  141. if ($var != "")
  142. {
  143. $this->_confs[$var] = $val;
  144. }
  145. }
  146. }
  147. else
  148. {
  149. if ($key != "")
  150. {
  151. $this->_confs[$key] = $value;
  152. }
  153. }
  154. }
  155. function append($key, $value=null, $merge=false)
  156. {
  157. if (is_array($key))
  158. {
  159. foreach ($key as $_key => $_value)
  160. {
  161. if ($_key != '')
  162. {
  163. if(!@is_array($this->_vars[$_key]))
  164. {
  165. settype($this->_vars[$_key],'array');
  166. }
  167. if($merge && is_array($_value))
  168. {
  169. foreach($_value as $_mergekey => $_mergevalue)
  170. {
  171. $this->_vars[$_key][$_mergekey] = $_mergevalue;
  172. }
  173. }
  174. else
  175. {
  176. $this->_vars[$_key][] = $_value;
  177. }
  178. }
  179. }
  180. }
  181. else
  182. {
  183. if ($key != '' && isset($value))
  184. {
  185. if(!@is_array($this->_vars[$key]))
  186. {
  187. settype($this->_vars[$key],'array');
  188. }
  189. if($merge && is_array($value))
  190. {
  191. foreach($value as $_mergekey => $_mergevalue)
  192. {
  193. $this->_vars[$key][$_mergekey] = $_mergevalue;
  194. }
  195. }
  196. else
  197. {
  198. $this->_vars[$key][] = $value;
  199. }
  200. }
  201. }
  202. }
  203. function append_by_ref($key, &$value, $merge=false)
  204. {
  205. if ($key != '' && isset($value))
  206. {
  207. if(!@is_array($this->_vars[$key]))
  208. {
  209. settype($this->_vars[$key],'array');
  210. }
  211. if ($merge && is_array($value))
  212. {
  213. foreach($value as $_key => $_val)
  214. {
  215. $this->_vars[$key][$_key] = &$value[$_key];
  216. }
  217. }
  218. else
  219. {
  220. $this->_vars[$key][] = &$value;
  221. }
  222. }
  223. }
  224. function clear_assign($key = null)
  225. {
  226. if ($key == null)
  227. {
  228. $this->_vars = array();
  229. }
  230. else
  231. {
  232. if (is_array($key))
  233. {
  234. foreach($key as $index => $value)
  235. {
  236. if (in_array($value, $this->_vars))
  237. {
  238. unset($this->_vars[$index]);
  239. }
  240. }
  241. }
  242. else
  243. {
  244. if (in_array($key, $this->_vars))
  245. {
  246. unset($this->_vars[$index]);
  247. }
  248. }
  249. }
  250. }
  251. function clear_all_assign()
  252. {
  253. $this->_vars = array();
  254. }
  255. function clear_config($key = null)
  256. {
  257. if ($key == null)
  258. {
  259. $this->_conf = array();
  260. }
  261. else
  262. {
  263. if (is_array($key))
  264. {
  265. foreach($key as $index => $value)
  266. {
  267. if (in_array($value, $this->_conf))
  268. {
  269. unset($this->_conf[$index]);
  270. }
  271. }
  272. }
  273. else
  274. {
  275. if (in_array($key, $this->_conf))
  276. {
  277. unset($this->_conf[$key]);
  278. }
  279. }
  280. }
  281. }
  282. function &get_template_vars($key = null)
  283. {
  284. if ($key == null)
  285. {
  286. return $this->_vars;
  287. }
  288. else
  289. {
  290. if (isset($this->_vars[$key]))
  291. {
  292. return $this->_vars[$key];
  293. }
  294. else
  295. {
  296. return $this->_null;
  297. }
  298. }
  299. }
  300. function &get_config_vars($key = null)
  301. {
  302. if ($key == null)
  303. {
  304. return $this->_confs;
  305. }
  306. else
  307. {
  308. if (isset($this->_confs[$key]))
  309. {
  310. return $this->_confs[$key];
  311. }
  312. else
  313. {
  314. return $this->_null;
  315. }
  316. }
  317. }
  318. function clear_compiled_tpl($file = null)
  319. {
  320. $this->_destroy_dir($file, null, $this->_get_dir($this->compile_dir));
  321. }
  322. function clear_cache($file = null, $cache_id = null, $compile_id = null, $exp_time = null)
  323. {
  324. if (!$this->cache)
  325. {
  326. return;
  327. }
  328. $this->_destroy_dir($file, $cache_id, $this->_get_dir($this->cache_dir));
  329. }
  330. function clear_all_cache($exp_time = null)
  331. {
  332. $this->clear_cache();
  333. }
  334. function is_cached($file, $cache_id = null)
  335. {
  336. if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id))
  337. {
  338. return true;
  339. }
  340. else
  341. {
  342. return false;
  343. }
  344. }
  345. function register_modifier($modifier, $implementation)
  346. {
  347. $this->_plugins['modifier'][$modifier] = $implementation;
  348. }
  349. function unregister_modifier($modifier)
  350. {
  351. unset($this->_plugins['modifier'][$modifier]);
  352. }
  353. function register_function($function, $implementation)
  354. {
  355. $this->_plugins['function'][$function] = $implementation;
  356. }
  357. function unregister_function($function)
  358. {
  359. unset($this->_plugins['function'][$function]);
  360. }
  361. function register_block($function, $implementation)
  362. {
  363. $this->_plugins['block'][$function] = $implementation;
  364. }
  365. function unregister_block($function)
  366. {
  367. unset($this->_plugins['block'][$function]);
  368. }
  369. function register_compiler($function, $implementation)
  370. {
  371. $this->_plugins['compiler'][$function] = $implementation;
  372. }
  373. function unregister_compiler($function)
  374. {
  375. unset($this->_plugins['compiler'][$function]);
  376. }
  377. function register_prefilter($function)
  378. {
  379. $_name = (is_array($function)) ? $function[1] : $function;
  380. $this->_plugins['prefilter'][$_name] = $_name;
  381. }
  382. function unregister_prefilter($function)
  383. {
  384. unset($this->_plugins['prefilter'][$function]);
  385. }
  386. function register_postfilter($function)
  387. {
  388. $_name = (is_array($function)) ? $function[1] : $function;
  389. $this->_plugins['postfilter'][$_name] = $_name;
  390. }
  391. function unregister_postfilter($function)
  392. {
  393. unset($this->_plugins['postfilter'][$function]);
  394. }
  395. function register_outputfilter($function)
  396. {
  397. $_name = (is_array($function)) ? $function[1] : $function;
  398. $this->_plugins['outputfilter'][$_name] = $_name;
  399. }
  400. function unregister_outputfilter($function)
  401. {
  402. unset($this->_plugins['outputfilter'][$function]);
  403. }
  404. function register_resource($type, $functions)
  405. {
  406. if (count($functions) == 4)
  407. {
  408. $this->_plugins['resource'][$type] = $functions;
  409. }
  410. else
  411. {
  412. $this->trigger_error("malformed function-list for '$type' in register_resource");
  413. }
  414. }
  415. function unregister_resource($type)
  416. {
  417. unset($this->_plugins['resource'][$type]);
  418. }
  419. function template_exists($file)
  420. {
  421. if (file_exists($this->_get_dir($this->template_dir).$file))
  422. {
  423. $this->_resource_time = filemtime($this->_get_dir($this->template_dir).$file);
  424. $this->_resource_type = 1;
  425. return true;
  426. }
  427. else
  428. {
  429. if (file_exists($file))
  430. {
  431. $this->_resource_time = filemtime($file);
  432. $this->_resource_type = "file";
  433. return true;
  434. }
  435. return false;
  436. }
  437. }
  438. function _get_resource($file)
  439. {
  440. $_resource_name = explode(':', trim($file));
  441. if (count($_resource_name) == 1 || $_resource_name[0] == "file")
  442. {
  443. if($_resource_name[0] == "file")
  444. {
  445. $file = substr($file, 5);
  446. }
  447. $exists = $this->template_exists($file);
  448. if (!$exists)
  449. {
  450. $this->trigger_error("file '$file' does not exist", E_USER_ERROR);
  451. }
  452. }
  453. else
  454. {
  455. $this->_resource_type = $_resource_name[0];
  456. $file = substr($file, strlen($this->_resource_type) + 1);
  457. $exists = isset($this->_plugins['resource'][$this->_resource_type]) && call_user_func_array($this->_plugins['resource'][$this->_resource_type][1], array($file, &$resource_timestamp, &$this));
  458. if (!$exists)
  459. {
  460. $this->trigger_error("file '$file' does not exist", E_USER_ERROR);
  461. }
  462. $this->_resource_time = $resource_timestamp;
  463. }
  464. return $file;
  465. }
  466. function display($file, $cache_id = null)
  467. {
  468. $this->fetch($file, $cache_id, true);
  469. }
  470. function fetch($file, $cache_id = null, $display = false)
  471. {
  472. $file = $this->_get_resource($file);
  473. if ($this->debugging)
  474. {
  475. $this->_templatelite_debug_info[] = array('type' => 'template',
  476. 'filename' => $file,
  477. 'depth' => 0,
  478. 'exec_time' => array_sum(explode(' ', microtime())) );
  479. $included_tpls_idx = count($this->_templatelite_debug_info) - 1;
  480. }
  481. $this->_cache_id = $cache_id;
  482. $this->template_dir = $this->_get_dir($this->template_dir);
  483. $this->compile_dir = $this->_get_dir($this->compile_dir);
  484. if ($this->cache)
  485. {
  486. $this->_cache_dir = $this->_build_dir($this->cache_dir, $this->_cache_id);
  487. }
  488. $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
  489. $this->_error_level = $this->debugging ? error_reporting() : error_reporting(error_reporting() & ~E_NOTICE);
  490. // $this->_error_level = error_reporting(E_ALL);
  491. if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id))
  492. {
  493. ob_start();
  494. include($this->_cache_dir.$name);
  495. $output = ob_get_contents();
  496. ob_end_clean();
  497. $output = substr($output, strpos($output, "\n") + 1);
  498. }
  499. else
  500. {
  501. $output = $this->_fetch_compile($file, $cache_id);
  502. if ($this->cache)
  503. {
  504. $f = fopen($this->_cache_dir.$name, "w");
  505. fwrite($f, serialize($this->_cache_info) . "\n$output");
  506. fclose($f);
  507. }
  508. }
  509. if (strpos($output, $this->_sl_md5) !== false)
  510. {
  511. preg_match_all('!' . $this->_sl_md5 . '{_run_insert (.*)}' . $this->_sl_md5 . '!U',$output,$_match);
  512. foreach($_match[1] as $value)
  513. {
  514. $arguments = unserialize($value);
  515. $output = str_replace($this->_sl_md5 . '{_run_insert ' . $value . '}' . $this->_sl_md5, call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)), $output);
  516. }
  517. }
  518. foreach ($this->_plugins['outputfilter'] as $function)
  519. {
  520. $output = $function($output, $this);
  521. }
  522. error_reporting($this->_error_level);
  523. if ($this->debugging)
  524. {
  525. $this->_templatelite_debug_info[$included_tpls_idx]['exec_time'] = array_sum(explode(' ', microtime())) - $this->_templatelite_debug_info[$included_tpls_idx]['exec_time'];
  526. }
  527. if ($display)
  528. {
  529. echo $output;
  530. if($this->debugging && !$this->_templatelite_debug_loop)
  531. {
  532. $this->debugging = false;
  533. if(!function_exists("template_generate_debug_output"))
  534. {
  535. require_once(TEMPLATE_LITE_DIR . "internal/template.generate_debug_output.php");
  536. }
  537. $debug_output = template_generate_debug_output($this);
  538. $this->debugging = true;
  539. echo $debug_output;
  540. }
  541. }
  542. else
  543. {
  544. return $output;
  545. }
  546. }
  547. function config_load($file, $section_name = null, $var_name = null)
  548. {
  549. require_once(TEMPLATE_LITE_DIR . "internal/template.config_loader.php");
  550. }
  551. function _is_cached($file, $cache_id)
  552. {
  553. $this->_cache_dir = $this->_get_dir($this->cache_dir, $cache_id);
  554. $this->config_dir = $this->_get_dir($this->config_dir);
  555. $this->template_dir = $this->_get_dir($this->template_dir);
  556. $file = $this->_get_resource($file);
  557. $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
  558. if (file_exists($this->_cache_dir.$name) && (((time() - filemtime($this->_cache_dir.$name)) < $this->cache_lifetime) || $this->cache_lifetime == -1) && (filemtime($this->_cache_dir.$name) > $this->_resource_time))
  559. {
  560. $fh = fopen($this->_cache_dir.$name, "r");
  561. if (!feof($fh) && ($line = fgets($fh, filesize($this->_cache_dir.$name))))
  562. {
  563. $includes = unserialize($line);
  564. if (isset($includes['template']))
  565. {
  566. foreach($includes['template'] as $value)
  567. {
  568. if (!(file_exists($this->template_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->template_dir.$value))))
  569. {
  570. return false;
  571. }
  572. }
  573. }
  574. if (isset($includes['config']))
  575. {
  576. foreach($includes['config'] as $value)
  577. {
  578. if (!(file_exists($this->config_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->config_dir.$value))))
  579. {
  580. return false;
  581. }
  582. }
  583. }
  584. }
  585. fclose($fh);
  586. }
  587. else
  588. {
  589. return false;
  590. }
  591. return true;
  592. }
  593. function _fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars)
  594. {
  595. if(!function_exists("template_fetch_compile_include"))
  596. {
  597. require_once(TEMPLATE_LITE_DIR . "internal/template.fetch_compile_include.php");
  598. }
  599. return template_fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars, $this);
  600. }
  601. function _fetch_compile($file)
  602. {
  603. $this->template_dir = $this->_get_dir($this->template_dir);
  604. $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
  605. if ($this->cache)
  606. {
  607. array_push($this->_cache_info['template'], $file);
  608. }
  609. if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name)
  610. && (filemtime($this->compile_dir.'c_'.$name) > $this->_resource_time)
  611. && (filemtime($this->compile_dir.'c_'.$name) > $this->_version_date))
  612. {
  613. ob_start();
  614. include($this->compile_dir.'c_'.$name);
  615. $output = ob_get_contents();
  616. ob_end_clean();
  617. error_reporting($this->_error_level);
  618. return $output;
  619. }
  620. $file_contents = "";
  621. if($this->_resource_type == 1)
  622. {
  623. $f = fopen($this->template_dir . $file, "r");
  624. $size = filesize($this->template_dir . $file);
  625. if ($size > 0)
  626. {
  627. $file_contents = fread($f, $size);
  628. }
  629. }
  630. else
  631. if($this->_resource_type == "file")
  632. {
  633. $f = fopen($file, "r");
  634. $size = filesize($file);
  635. if ($size > 0)
  636. {
  637. $file_contents = fread($f, $size);
  638. }
  639. }
  640. else
  641. {
  642. call_user_func_array($this->_plugins['resource'][$this->_resource_type][0], array($file, &$file_contents, &$this));
  643. }
  644. $this->_file = $file;
  645. fclose($f);
  646. if (!is_object($this->_compile_obj))
  647. {
  648. if (file_exists(TEMPLATE_LITE_DIR . $this->compiler_file)) {
  649. require_once(TEMPLATE_LITE_DIR . $this->compiler_file);
  650. } else {
  651. require_once($this->compiler_file);
  652. }
  653. $this->_compile_obj = new $this->compiler_class;
  654. }
  655. $this->_compile_obj->left_delimiter = $this->left_delimiter;
  656. $this->_compile_obj->right_delimiter = $this->right_delimiter;
  657. $this->_compile_obj->plugins_dir = &$this->plugins_dir;
  658. $this->_compile_obj->template_dir = &$this->template_dir;
  659. $this->_compile_obj->_vars = &$this->_vars;
  660. $this->_compile_obj->_confs = &$this->_confs;
  661. $this->_compile_obj->_plugins = &$this->_plugins;
  662. $this->_compile_obj->_linenum = &$this->_linenum;
  663. $this->_compile_obj->_file = &$this->_file;
  664. $this->_compile_obj->php_extract_vars = &$this->php_extract_vars;
  665. $this->_compile_obj->reserved_template_varname = &$this->reserved_template_varname;
  666. $this->_compile_obj->default_modifiers = $this->default_modifiers;
  667. $output = $this->_compile_obj->_compile_file($file_contents);
  668. $f = fopen($this->compile_dir.'c_'.$name, "w");
  669. fwrite($f, $output);
  670. fclose($f);
  671. ob_start();
  672. eval(' ?>' . $output . '<?php ');
  673. $output = ob_get_contents();
  674. ob_end_clean();
  675. return $output;
  676. }
  677. function _run_modifier()
  678. {
  679. $arguments = func_get_args();
  680. list($variable, $modifier, $php_function, $_map_array) = array_splice($arguments, 0, 4);
  681. array_unshift($arguments, $variable);
  682. if ($_map_array && is_array($variable))
  683. {
  684. foreach($variable as $key => $value)
  685. {
  686. if($php_function == "PHP")
  687. {
  688. $variable[$key] = call_user_func_array($modifier, $arguments);
  689. }
  690. else
  691. {
  692. $variable[$key] = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
  693. }
  694. }
  695. }
  696. else
  697. {
  698. if($php_function == "PHP")
  699. {
  700. $variable = call_user_func_array($modifier, $arguments);
  701. }
  702. else
  703. {
  704. $variable = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
  705. }
  706. }
  707. return $variable;
  708. }
  709. function _run_insert($arguments)
  710. {
  711. if ($this->cache)
  712. {
  713. return $this->_sl_md5 . '{_run_insert ' . serialize((array)$arguments) . '}' . $this->_sl_md5;
  714. }
  715. else
  716. {
  717. if (!function_exists('insert_' . $arguments['name']))
  718. {
  719. $this->trigger_error("function 'insert_" . $arguments['name'] . "' does not exist in 'insert'", E_USER_ERROR);
  720. }
  721. if (isset($arguments['assign']))
  722. {
  723. $this->assign($arguments['assign'], call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)));
  724. }
  725. else
  726. {
  727. return call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this));
  728. }
  729. }
  730. }
  731. function _get_dir($dir, $id = null)
  732. {
  733. if (empty($dir))
  734. {
  735. $dir = '.';
  736. }
  737. if (substr($dir, -1) != DIRECTORY_SEPARATOR)
  738. {
  739. $dir .= DIRECTORY_SEPARATOR;
  740. }
  741. if (!empty($id))
  742. {
  743. $_args = explode('|', $id);
  744. if (count($_args) == 1 && empty($_args[0]))
  745. {
  746. return $dir;
  747. }
  748. foreach($_args as $value)
  749. {
  750. $dir .= $value.DIRECTORY_SEPARATOR;
  751. }
  752. }
  753. return $dir;
  754. }
  755. function _get_plugin_dir($plugin_name)
  756. {
  757. static $_path_array = null;
  758. $plugin_dir_path = "";
  759. $_plugin_dir_list = is_array($this->plugins_dir) ? $this->plugins_dir : (array)$this->plugins_dir;
  760. foreach ($_plugin_dir_list as $_plugin_dir)
  761. {
  762. if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir))
  763. {
  764. // path is relative
  765. if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $_plugin_dir . DIRECTORY_SEPARATOR . $plugin_name))
  766. {
  767. $plugin_dir_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . $_plugin_dir . DIRECTORY_SEPARATOR;
  768. break;
  769. }
  770. }
  771. else
  772. {
  773. // path is absolute
  774. if(!isset($_path_array))
  775. {
  776. $_ini_include_path = ini_get('include_path');
  777. if(strstr($_ini_include_path,';'))
  778. {
  779. // windows pathnames
  780. $_path_array = explode(';',$_ini_include_path);
  781. }
  782. else
  783. {
  784. $_path_array = explode(':',$_ini_include_path);
  785. }
  786. }
  787. if(!in_array($_plugin_dir,$_path_array))
  788. {
  789. array_unshift($_path_array,$_plugin_dir);
  790. }
  791. foreach ($_path_array as $_include_path)
  792. {
  793. if (file_exists($_include_path . DIRECTORY_SEPARATOR . $plugin_name))
  794. {
  795. $plugin_dir_path = $_include_path . DIRECTORY_SEPARATOR;
  796. break 2;
  797. }
  798. }
  799. }
  800. }
  801. return $plugin_dir_path;
  802. }
  803. // function _parse_resource_link($resource_link)
  804. // {
  805. // $stuffing = "file:/this/is/the/time_5-23.tpl";
  806. // $stuffing_data = explode(":", $stuffing);
  807. // preg_match_all('/(?:([0-9a-z._-]+))/i', $stuffing, $stuff);
  808. // print_r($stuff);
  809. // echo "<br>Path: " . str_replace($stuff[0][count($stuff[0]) - 1], "", $stuffing);
  810. // echo "<br>Filename: " . $stuff[0][count($stuff[0]) - 1];
  811. // }
  812. function _build_dir($dir, $id)
  813. {
  814. if(!function_exists("template_build_dir"))
  815. {
  816. require_once(TEMPLATE_LITE_DIR . "internal/template.build_dir.php");
  817. }
  818. return template_build_dir($dir, $id, $this);
  819. }
  820. function _destroy_dir($file, $id, $dir)
  821. {
  822. if(!function_exists("template_destroy_dir"))
  823. {
  824. require_once(TEMPLATE_LITE_DIR . "internal/template.destroy_dir.php");
  825. }
  826. return template_destroy_dir($file, $id, $dir, $this);
  827. }
  828. function trigger_error($error_msg, $error_type = E_USER_ERROR, $file = null, $line = null)
  829. {
  830. if(isset($file) && isset($line))
  831. {
  832. $info = ' ('.basename($file).", line $line)";
  833. }
  834. else
  835. {
  836. $info = null;
  837. }
  838. trigger_error('TPL: [in ' . $this->_file . ' line ' . $this->_linenum . "]: syntax error: $error_msg$info", $error_type);
  839. }
  840. }
  841. ?>