PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/template_engines_bench/libs/smarty-light/src/class.template.php

https://github.com/limb-php-framework/limb-tools
PHP | 472 lines | 386 code | 48 blank | 38 comment | 129 complexity | 67c9f0e653fa0a7cf32734555e666405 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Project: Smarty-Light, a smarter template engine
  4. * File: class.template.php
  5. * Author: Paul Lockaby <paul@paullockaby.com>
  6. * Version: 2.2.11
  7. * Copyright: 2003,2004,2005 by Paul Lockaby
  8. * Credit: This work is a light version of Smarty: the PHP compiling
  9. * template engine, v2.5.0-CVS. Smarty was originally
  10. * programmed by Monte Ohrt and Andrei Zmievski and can be
  11. * found at http://smarty.php.net
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * You may contact the author of Smarty-Light by e-mail at:
  28. * paul@paullockaby.com
  29. *
  30. * The latest version of Smarty-Light can be obtained from:
  31. * http://www.paullockaby.com/projects/smarty-light
  32. *
  33. */
  34. class template {
  35. // public configuration variables
  36. var $left_tag = "<%"; // the left delimiter for template tags
  37. var $right_tag = "%>"; // the right delimiter for template tags
  38. var $cache = false; // whether or not to allow caching of files
  39. var $force_compile = false; // force a compile regardless of saved state
  40. var $template_dir = "templates"; // where the templates are to be found
  41. var $plugin_dir = "plugins"; // where the plugins are to be found
  42. var $compile_dir = "compiled"; // the directory to store the compiled files in
  43. var $config_dir = "templates"; // where the config files are
  44. var $cache_dir = "cached"; // where cache files are stored
  45. var $config_overwrite = false;
  46. var $config_booleanize = true;
  47. var $config_fix_new_lines = true;
  48. var $config_read_hidden = true;
  49. var $cache_lifetime = 0; // how long the file in cache should be considered "fresh"
  50. // private internal variables
  51. var $_vars = array(); // stores all internal assigned variables
  52. var $_confs = array(); // stores all internal config variables
  53. var $_plugins = array(); // stores all internal plugins
  54. var $_linenum = 0; // the current line number in the file we are processing
  55. var $_file = ""; // the current file we are processing
  56. var $_config_obj = null;
  57. var $_compile_obj = null;
  58. var $_cache_id = null;
  59. var $_cache_dir = ""; // stores where this specific file is going to be cached
  60. var $_cache_info = array('config' => array(), 'template' => array());
  61. var $_sl_md5 = '39fc70570b8b60cbc1b85839bf242aff';
  62. function assign($key, $value = null) {
  63. if (is_array($key)) {
  64. foreach($key as $var => $val)
  65. if ($var != "" && !is_numeric($var))
  66. $this->_vars[$var] = $val;
  67. } else {
  68. if ($key != "" && !is_numeric($key))
  69. $this->_vars[$key] = $value;
  70. }
  71. }
  72. function assign_config($key, $value = null) {
  73. if (is_array($key)) {
  74. foreach($key as $var => $val)
  75. if ($var != "" && !is_numeric($var))
  76. $this->_confs[$var] = $val;
  77. } else {
  78. if ($key != "" && !is_numeric($key))
  79. $this->_confs[$key] = $value;
  80. }
  81. }
  82. function clear($key = null) {
  83. if ($key == null) {
  84. $this->_vars = array();
  85. } else {
  86. if (is_array($key)) {
  87. foreach($key as $index => $value)
  88. if (in_array($value, $this->_vars))
  89. unset($this->_vars[$index]);
  90. } else {
  91. if (in_array($key, $this->_vars))
  92. unset($this->_vars[$index]);
  93. }
  94. }
  95. }
  96. function clear_config($key = null) {
  97. if ($key == null) {
  98. $this->_conf = array();
  99. } else {
  100. if (is_array($key)) {
  101. foreach($key as $index => $value)
  102. if (in_array($value, $this->_conf))
  103. unset($this->_conf[$index]);
  104. } else {
  105. if (in_array($key, $this->_conf))
  106. unset($this->_conf[$key]);
  107. }
  108. }
  109. }
  110. function &get_vars($key = null) {
  111. if ($key == null) {
  112. return $this->_vars;
  113. } else {
  114. if (isset($this->_vars[$key]))
  115. return $this->_vars[$key];
  116. else
  117. return null;
  118. }
  119. }
  120. function &get_config_vars($key = null) {
  121. if ($key == null) {
  122. return $this->_confs;
  123. } else {
  124. if (isset($this->_confs[$key]))
  125. return $this->_confs[$key];
  126. else
  127. return null;
  128. }
  129. }
  130. function clear_compiled($file = null) {
  131. $this->_destroy_dir($file, null, $this->_get_dir($this->compile_dir));
  132. }
  133. function clear_cached($file = null, $cache_id = null) {
  134. if (!$this->cache)
  135. return;
  136. $this->_destroy_dir($file, $cache_id, $this->_get_dir($this->cache_dir));
  137. }
  138. function is_cached($file, $cache_id = null) {
  139. if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id)) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }
  145. function register_modifier($modifier, $implementation) {
  146. $this->_plugins['modifier'][$modifier] = $implementation;
  147. }
  148. function unregister_modifier($modifier) {
  149. if (isset($this->_plugins['modifier'][$modifier]))
  150. unset($this->_plugins['modifier'][$modifier]);
  151. }
  152. function register_function($function, $implementation) {
  153. $this->_plugins['function'][$function] = $implementation;
  154. }
  155. function unregister_function($function) {
  156. if (isset($this->_plugins['function'][$function]))
  157. unset($this->_plugins['function'][$function]);
  158. }
  159. function register_block($function, $implementation) {
  160. $this->_plugins['block'][$function] = $implementation;
  161. }
  162. function unregister_block($function) {
  163. if (isset($this->_plugins['block'][$function]))
  164. unset($this->_plugins['block'][$function]);
  165. }
  166. function template_exists($file) {
  167. if (file_exists($this->_get_dir($this->template_dir).$file)) {
  168. return true;
  169. } else {
  170. return false;
  171. }
  172. }
  173. function display($file, $cache_id = null) {
  174. $this->fetch($file, $cache_id, true);
  175. }
  176. function fetch($file, $cache_id = null, $display = false) {
  177. $this->_cache_id = $cache_id;
  178. $this->template_dir = $this->_get_dir($this->template_dir);
  179. $this->compile_dir = $this->_get_dir($this->compile_dir);
  180. if ($this->cache)
  181. $this->_cache_dir = $this->_build_dir($this->cache_dir, $this->_cache_id);
  182. $name = md5($this->template_dir.$file).'.php';
  183. // don't display any errors
  184. $this->_error_level = error_reporting(error_reporting() & ~E_NOTICE);
  185. if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id)) {
  186. ob_start();
  187. include($this->_cache_dir.$name);
  188. $output = ob_get_contents();
  189. ob_end_clean();
  190. $output = substr($output, strpos($output, "\n") + 1);
  191. } else {
  192. $output = $this->_fetch_compile($file, $cache_id);
  193. if ($this->cache) {
  194. $f = fopen($this->_cache_dir.$name, "w");
  195. fwrite($f, serialize($this->_cache_info) . "\n$output");
  196. fclose($f);
  197. }
  198. }
  199. if (strpos($output, $this->_sl_md5) !== false) {
  200. preg_match_all('!' . $this->_sl_md5 . '{_run_insert (.*)}' . $this->_sl_md5 . '!U',$output,$_match);
  201. foreach($_match[1] as $value) {
  202. $arguments = unserialize($value);
  203. $output = str_replace($this->_sl_md5 . '{_run_insert ' . $value . '}' . $this->_sl_md5, call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)), $output);
  204. }
  205. }
  206. // return error reporting to normal
  207. error_reporting($this->_error_level);
  208. if ($display) {
  209. echo $output;
  210. } else {
  211. return $output;
  212. }
  213. }
  214. function config_load($file, $section_name = null, $var_name = null) {
  215. $this->template_dir = $this->_get_dir($this->template_dir);
  216. $this->config_dir = $this->_get_dir($this->config_dir);
  217. $this->compile_dir = $this->_get_dir($this->compile_dir);
  218. $name = md5($this->template_dir.$file.$section_name.$var_name).'.php';
  219. if ($this->cache) {
  220. array_push($this->_cache_info['config'], $file);
  221. }
  222. if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name) && (filemtime($this->compile_dir.'c_'.$name) > filemtime($this->config_dir.$file))) {
  223. include($this->compile_dir.'c_'.$name);
  224. return true;
  225. }
  226. if (!is_object($this->_config_obj)) {
  227. require_once("class.config.php");
  228. $this->_config_obj = new config;
  229. $this->_config_obj->overwrite = $this->config_overwrite;
  230. $this->_config_obj->booleanize = $this->config_booleanize;
  231. $this->_config_obj->fix_new_lines = $this->config_fix_new_lines;
  232. $this->_config_obj->read_hidden = $this->config_read_hidden;
  233. }
  234. if (!($_result = $this->_config_obj->config_load($this->config_dir.$file, $section_name, $var_name))) {
  235. return false;
  236. }
  237. if (!empty($var_name) || !empty($section_name)) {
  238. $output = "\$this->_confs = " . var_export($_result, true) . ";";
  239. } else {
  240. // must shift of the bottom level of the array to get rid of the section labels
  241. $_temp = array();
  242. foreach($_result as $value)
  243. $_temp = array_merge($_temp, $value);
  244. $output = "\$this->_confs = " . var_export($_temp, true) . ";";
  245. }
  246. $f = fopen($this->compile_dir.'c_'.$name, "w");
  247. fwrite($f, '<?php ' . $output . ' ?>');
  248. fclose($f);
  249. eval($output);
  250. return true;
  251. }
  252. function _is_cached($file, $cache_id) {
  253. $this->_cache_dir = $this->_get_dir($this->cache_dir, $cache_id);
  254. $this->config_dir = $this->_get_dir($this->config_dir);
  255. $this->template_dir = $this->_get_dir($this->template_dir);
  256. $name = md5($this->template_dir.$file).'.php';
  257. 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) > filemtime($this->template_dir.$file))) {
  258. $fh = fopen($this->_cache_dir.$name, "r");
  259. if (!feof($fh) && ($line = fgets($fh, filesize($this->_cache_dir.$name)))) {
  260. $includes = unserialize($line);
  261. if (isset($includes['template']))
  262. foreach($includes['template'] as $value)
  263. if (!(file_exists($this->template_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->template_dir.$value))))
  264. return false;
  265. if (isset($includes['config']))
  266. foreach($includes['config'] as $value)
  267. if (!(file_exists($this->config_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->config_dir.$value))))
  268. return false;
  269. }
  270. fclose($fh);
  271. } else {
  272. return false;
  273. }
  274. return true;
  275. }
  276. function _fetch_compile($file) {
  277. $this->template_dir = $this->_get_dir($this->template_dir);
  278. $name = md5($this->template_dir.$file).'.php';
  279. if ($this->cache) {
  280. array_push($this->_cache_info['template'], $file);
  281. }
  282. if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name) && (filemtime($this->compile_dir.'c_'.$name) > filemtime($this->template_dir.$file))) {
  283. ob_start();
  284. include($this->compile_dir.'c_'.$name);
  285. $output = ob_get_contents();
  286. ob_end_clean();
  287. error_reporting($this->_error_level);
  288. return $output;
  289. }
  290. if ($this->template_exists($file)) {
  291. $f = fopen($this->template_dir.$file, "r");
  292. $size = filesize($this->template_dir.$file);
  293. if ($size > 0) {
  294. $file_contents = fread($f, filesize($this->template_dir.$file));
  295. } else {
  296. $file_contents = "";
  297. }
  298. $this->_file = $file;
  299. fclose($f);
  300. } else {
  301. $this->trigger_error("file '$file' does not exist", E_USER_ERROR);
  302. }
  303. if (!is_object($this->_compile_obj)) {
  304. require_once("class.compiler.php");
  305. $this->_compile_obj = new compiler;
  306. }
  307. $this->_compile_obj->left_tag = $this->left_tag;
  308. $this->_compile_obj->right_tag = $this->right_tag;
  309. $this->_compile_obj->plugin_dir = &$this->plugin_dir;
  310. $this->_compile_obj->template_dir = &$this->template_dir;
  311. $this->_compile_obj->_vars = &$this->_vars;
  312. $this->_compile_obj->_confs = &$this->_confs;
  313. $this->_compile_obj->_plugins = &$this->_plugins;
  314. $this->_compile_obj->_linenum = &$this->_linenum;
  315. $this->_compile_obj->_file = &$this->_file;
  316. $output = $this->_compile_obj->_compile_file($file_contents);
  317. $f = fopen($this->compile_dir.'c_'.$name, "w");
  318. fwrite($f, $output);
  319. fclose($f);
  320. ob_start();
  321. eval(' ?>' . $output . '<?php ');
  322. $output = ob_get_contents();
  323. ob_end_clean();
  324. return $output;
  325. }
  326. function _run_modifier() {
  327. $arguments = func_get_args();
  328. list($variable, $modifier, $_map_array) = array_splice($arguments, 0, 3);
  329. array_unshift($arguments, $variable);
  330. if ($_map_array && is_array($variable))
  331. foreach($variable as $key => $value)
  332. $variable[$key] = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
  333. else
  334. $variable = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
  335. return $variable;
  336. }
  337. function _run_insert($arguments) {
  338. if ($this->cache) {
  339. return $this->_sl_md5 . '{_run_insert ' . serialize((array)$arguments) . '}' . $this->_sl_md5;
  340. } else {
  341. if (!function_exists('insert_' . $arguments['name']))
  342. $this->trigger_error("function 'insert_" . $arguments['name'] . "' does not exist in 'insert'", E_USER_ERROR);
  343. if (isset($arguments['assign']))
  344. $this->assign($arguments['assign'], call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)));
  345. else
  346. return call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this));
  347. }
  348. }
  349. function _get_dir($dir, $id = null) {
  350. if (empty($dir))
  351. $dir = '.';
  352. if (substr($dir, -1) != DIRECTORY_SEPARATOR)
  353. $dir .= DIRECTORY_SEPARATOR;
  354. if (!empty($id)) {
  355. $_args = explode('|', $id);
  356. if (count($_args) == 1 && empty($_args[0]))
  357. return $dir;
  358. foreach($_args as $value)
  359. $dir .= $value.DIRECTORY_SEPARATOR;
  360. }
  361. return $dir;
  362. }
  363. function _build_dir($dir, $id) {
  364. $_args = explode('|', $id);
  365. if (count($_args) == 1 && empty($_args[0]))
  366. return $this->_get_dir($dir);
  367. $_result = $this->_get_dir($dir);
  368. foreach($_args as $value) {
  369. $_result .= $value.DIRECTORY_SEPARATOR;
  370. if (!is_dir($_result)) @mkdir($_result, 0777);
  371. }
  372. return $_result;
  373. }
  374. function _destroy_dir($file, $id, $dir) {
  375. if ($file == null && $id == null) {
  376. if (is_dir($dir))
  377. if($d = opendir($dir))
  378. while(($f = readdir($d)) !== false)
  379. if ($f != '.' && $f != '..')
  380. $this->_rm_dir($dir.$f.DIRECTORY_SEPARATOR);
  381. } else {
  382. if ($id == null) {
  383. $this->template_dir = $this->_get_dir($this->template_dir);
  384. @unlink($dir.md5($this->template_dir.$file).'.php');
  385. } else {
  386. $_args = "";
  387. foreach(explode('|', $id) as $value)
  388. $_args .= $value.DIRECTORY_SEPARATOR;
  389. $this->_rm_dir($dir.DIRECTORY_SEPARATOR.$_args);
  390. }
  391. }
  392. }
  393. function _rm_dir($dir) {
  394. if (is_file(substr($dir, 0, -1))) {
  395. @unlink(substr($dir, 0, -1));
  396. return;
  397. }
  398. if ($d = opendir($dir)) {
  399. while(($f = readdir($d)) !== false) {
  400. if ($f != '.' && $f != '..') {
  401. $this->_rm_dir($dir.$f.DIRECTORY_SEPARATOR);
  402. }
  403. }
  404. @rmdir($dir.$f);
  405. }
  406. }
  407. function _escape_chars($string) {
  408. // taken directly from Smarty at http://smarty.php.net/
  409. if(!is_array($string)) {
  410. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  411. $string = htmlspecialchars($string);
  412. $string = str_replace(array('%%%SMARTY_START%%%','%%%SMARTY_END%%%'), array('&',';'), $string);
  413. }
  414. return $string;
  415. }
  416. function trigger_error($error_msg, $error_type = E_USER_ERROR, $file = null, $line = null) {
  417. if(isset($file) && isset($line))
  418. $info = ' ('.basename($file).", line $line)";
  419. else
  420. $info = null;
  421. trigger_error('TPL: [in ' . $this->_file . ' line ' . $this->_linenum . "]: syntax error: $error_msg$info", $error_type);
  422. }
  423. }
  424. ?>