PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/quicky/classes/quicky.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 844 lines | 810 code | 16 blank | 18 comment | 130 complexity | 38fb95e45c440ac2ade31156fcc9ae8e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**************************************************************************/
  3. /* Quicky: smart and fast templates
  4. /* ver. 0.5.0.0
  5. /* ===========================
  6. /*
  7. /* Copyright (c)oded 2007-2008 by WP
  8. /* http://quicky-tpl.net
  9. /*
  10. /* Quicky.class.php: API class
  11. /**************************************************************************/
  12. ini_set('zend.ze1_compatibility_mode','Off');
  13. define('QUICKY_DIR',dirname(__FILE__).DIRECTORY_SEPARATOR);
  14. if (!defined('UNIQUE_HASH')) {
  15. define('UNIQUE_HASH',abs(crc32(microtime(TRUE).microtime(TRUE))));
  16. }
  17. if (!defined('UNIQUE_HASH_STATIC')) {
  18. define('UNIQUE_HASH_STATIC','80323d4d5b350a87f174a3de4502483d');
  19. }
  20. if (!function_exists('isInteger')) {
  21. function isInteger($var) {
  22. if (is_int($var)) {
  23. return TRUE;
  24. }
  25. if (is_float($var)) {
  26. return TRUE;
  27. }
  28. if (!is_string($var)) {
  29. return FALSE;
  30. }
  31. return ctype_digit(substr($var,0,1) == '-'?substr($var,1):$var);
  32. }
  33. }
  34. if (!function_exists('gpcvar_str')) {
  35. function gpcvar_str(&$var) {
  36. if (is_array($var)) {
  37. return '';
  38. } return
  39. (string) $var;
  40. }
  41. function gpcvar_strnull(&$var) {
  42. if ($var === NULL) {
  43. return NULL;
  44. } if
  45. (is_array($var)) {
  46. return '';
  47. } return
  48. (string) $var;
  49. }
  50. function gpcvar_int(&$var,$empty = FALSE) {
  51. $var = (string) $var;
  52. if ($empty && !strlen($var)) {
  53. return $var;
  54. }
  55. return ctype_digit(substr($var,0,1) == '-'?substr($var,1):$var)?$var:'0';
  56. }
  57. function gpcvar_float(&$var,$empty = FALSE) {
  58. if ($empty and strlen($var) == 0) {
  59. return '';
  60. } return
  61. floatval($var);
  62. }
  63. function gpcvar_array(&$var) {
  64. return is_array($var)?$var:array();
  65. }
  66. function gpcvar_mixed(&$var) {
  67. return $var;
  68. }
  69. }
  70. class Quicky {
  71. // start Kohana fix
  72. public $template_dir = '../templates/';
  73. public $compile_dir = '../compile/';
  74. public $config_dir = '../config/';
  75. public $cache_dir = '../cache/';
  76. // end Kohana fix
  77. public $plugins_dir = array();
  78. public $_local_vars = array();
  79. public $_tpl_vars_buff = array();
  80. public $_tpl_vars = array();
  81. public $_tpl_config = array();
  82. public $_block_props = array();
  83. public $auto_filename_prefix = '';
  84. public $compilers = array();
  85. public $prefilters = array();
  86. public $postfilters = array();
  87. public $outputfilters = array();
  88. public $compile_check = TRUE;
  89. public $force_compile = FALSE;
  90. public $max_recursion_depth = 128;
  91. public $_auto_detect_forms = FALSE;
  92. public $_detect_forms = array();
  93. public $compiler_prefs = array(
  94. 'inline_includes' => FALSE,
  95. 'allow_php_native' => FALSE,
  96. 'interpret_varname_params' => FALSE,
  97. 'cast_undefined_token_to_strings' => FALSE,
  98. 'export_vars' => TRUE,
  99. 'auto_escape' => FALSE,
  100. );
  101. public $error_reporting;
  102. public $version = '0.5.0.0';
  103. public $caching = 0;
  104. public $cache_lifetime = 60;
  105. public $precompiled_vars = array();
  106. public $lang = '';
  107. public $use_sub_dirs = FALSE;
  108. public $cache_id = '';
  109. public $compile_id = '';
  110. static $obj;
  111. public $context_path = '/';
  112. public $_contexts_data = array();
  113. public $_blocks = array();
  114. public $default_compiler = 'Quicky';
  115. public $debug_mode = FALSE; // DON'T TURN ON IN PRODUCTION ! LOW PERFORMANCE.
  116. public $debug_trace = array(
  117. 'assign' => array(),
  118. 'fetch' => array(),
  119. );
  120. public $depart_scopes = FALSE;
  121. public $local_depart_scopes = FALSE;
  122. public function __construct() {
  123. $this->init();
  124. }
  125. public function init() {
  126. // start Kohana fix
  127. //Load the kohana config
  128. $kohana_config = Kohana::config('quicky');
  129. // set default dirs
  130. $this->template_dir = array($kohana_config->get('template_dir'));
  131. $this->compile_dir = $kohana_config->get('compile_dir');
  132. $this->plugins_dir = array($kohana_config->get('plugin_dir'));
  133. $this->cache_dir = $kohana_config->get('cache_dir');
  134. $this->left_delimiter = $kohana_config->get('ldelim');
  135. $this->right_delimiter = $kohana_config->get('rdelim');
  136. error_reporting ($kohana_config->get('error_reporting'));
  137. // start compile_dir autocreate
  138. if(!file_exists($this->compile_dir) && !mkdir($this->compile_dir)) {
  139. return $this->warning('Can\'t create compile-dir \''.$this->compile_dir.'\'');
  140. }
  141. // stop compile_dir autocreate
  142. // end Kohana fix
  143. $this->plugins_dir = array(QUICKY_DIR.'plugins');
  144. $this->_smarty_vars = &$this->_block_props;
  145. $this->_block_props['capture'] = array();
  146. $this->_block_props['foreach'] = array();
  147. $this->_block_props['section'] = array();
  148. $this->_block_props['begin'] = array();
  149. $this->capture = &$this->_block_props['capture'];
  150. $this->foreach = &$this->_block_props['foreach'];
  151. $this->section = &$this->_block_props['section'];
  152. $this->begin = &$this->_block_props['begin'];
  153. Quicky::$obj = $this;
  154. }
  155. public function append($tpl_var, $value = NULL, $merge = FALSE) {
  156. if (is_array($tpl_var)) {
  157. foreach ($tpl_var as $_key => $_val) {
  158. if ($_key != '') {
  159. if (!is_array($this->_tpl_vars[$_key])) {
  160. settype($this->_tpl_vars[$_key],'array');
  161. }
  162. if ($merge && is_array($_val)) {
  163. foreach($_val as $_mkey => $_mval) {
  164. $this->_tpl_vars[$_key][$_mkey] = $_mval;
  165. }
  166. }
  167. else {
  168. $this->_tpl_vars[$_key][] = $_val;
  169. }
  170. }
  171. }
  172. return;
  173. }
  174. if ($tpl_var != '' && isset($value)) {
  175. if (!is_array($this->_tpl_vars[$tpl_var])) {
  176. settype($this->_tpl_vars[$tpl_var],'array');
  177. }
  178. if ($merge && is_array($value)) {
  179. foreach($value as $_mkey => $_mval) {
  180. $this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
  181. }
  182. }
  183. else {
  184. $this->_tpl_vars[$tpl_var][] = $value;
  185. }
  186. }
  187. }
  188. public function append_by_ref($tpl_var,&$value,$merge = FALSE) {
  189. if ($tpl_var != '' && isset($value)) {
  190. if (!is_array($this->_tpl_vars[$tpl_var])) {
  191. settype($this->_tpl_vars[$tpl_var],'array');
  192. }
  193. if ($merge && is_array($value)) {
  194. foreach($value as $_key => $_val) {
  195. $this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
  196. }
  197. }
  198. else {
  199. $this->_tpl_vars[$tpl_var][] = &$value;
  200. }
  201. }
  202. }
  203. public function register_function($a,$b) {
  204. $this->reg_func[$a] = $b;
  205. }
  206. public function register_block($block) {
  207. if (!in_array($block,$this->_blocks)) {
  208. $this->_blocks[] = $block;
  209. }
  210. return TRUE;
  211. }
  212. public function unregister_block($block) {
  213. if ($k = array_search($block,$this->_blocks)) {
  214. unset($this->_blocks[$k]);
  215. return TRUE;
  216. }
  217. else {
  218. return FALSE;
  219. }
  220. }
  221. public function detect_form($name) {
  222. $this->_detect_forms[] = $name;
  223. }
  224. public function getFormByName($name) {
  225. if (!class_exists('Quicky_form')) {
  226. require_once QUICKY_DIR.'Quicky.form.class.php';
  227. }
  228. return isset(Quicky_form::$forms[$name])?Quicky_form::$forms[$name]:FALSE;
  229. }
  230. public function context_fetch($name) {
  231. $path = $this->context_path($name,FALSE);
  232. if (!function_exists($a = 'quicky_context_'.$name)) {
  233. return $this->warning('Context \''.$path.'\' does not exists');
  234. }
  235. return $a();
  236. }
  237. public function context_set($value = array()) {
  238. $this->_contexts_data[$this->context_path] = $value;
  239. }
  240. function context_iterate($name = '') {
  241. if ($name === '') {
  242. $name = $this->context_path;
  243. }
  244. $this->_contexts_data[$this->context_path($name,FALSE)] = array(array());
  245. }
  246. function load_string($name,$string) {
  247. require_once QUICKY_DIR.'plugins/addons/stringtemplate.class.php';
  248. Quicky_Stringtemplate::$strings[$name] = $string;
  249. }
  250. function context_path($path,$onlyget = FALSE) {
  251. if ($path === '') {
  252. return $this->context_path;
  253. }
  254. if (substr($path,0,1) != '/') {
  255. $path = $this->context_path.$path.'/';
  256. }
  257. if (strpos($path,'../') !== FALSE) {
  258. $e = explode('/',$path);
  259. for ($i = 0, $s = sizeof($e); $i < $s ; ++$i) {
  260. if ($e[$i] == '..') {
  261. unset($e[$i-1]);
  262. unset($e[$i]);
  263. $e = array_values($e);
  264. $i -= 2;
  265. $s -= 2;
  266. }
  267. elseif ($e[$i] == '.') {
  268. unset($e[$i]);
  269. }
  270. }
  271. $path = implode('/',$e);
  272. }
  273. if (!$onlyget) {
  274. return $this->context_path = $path;
  275. }
  276. else {
  277. return $path;
  278. }
  279. }
  280. function _unlink($resource, $exp_time = null) {
  281. if (isset($exp_time)) {
  282. if (time() - @filemtime($resource) >= $exp_time) {
  283. return @unlink($resource);
  284. }
  285. }
  286. else {
  287. return @unlink($resource);
  288. }
  289. }
  290. function fetch_plugin($name) {
  291. if (!is_array($this->plugins_dir)) {
  292. $a = array($this->plugins_dir);
  293. }
  294. else {
  295. $a = $this->plugins_dir;
  296. }
  297. for ($i = 0,$s = sizeof($a); $i < $s; $i++) {
  298. $path = rtrim($a[$i],'/\\').DIRECTORY_SEPARATOR.$name.'.php';
  299. if (is_file($path) && is_readable($path)) {
  300. return $path;
  301. }
  302. }
  303. return FALSE;
  304. }
  305. function register_prefilter($a,$b) {
  306. $this->prefilters[$a] = $b;
  307. }
  308. function unregister_prefilter($a) {
  309. unset($this->prefilters[$a]);
  310. }
  311. function register_postfilter($a,$b) {
  312. $this->postfilters[$a] = $b;
  313. }
  314. function unregister_postfilter($a) {
  315. unset($this->postfilters[$a]);
  316. }
  317. function register_outputfilter($a,$b) {
  318. $this->outputfilters[$a] = $b;
  319. }
  320. function unregister_outputfilter($a) {
  321. unset($this->outputfilters[$a]);
  322. }
  323. function template_exists($file) {
  324. return file_exists($this->_get_template_path($file));
  325. }
  326. function config_load($file,$section = '') {
  327. $path = $this->config_dir.$file;
  328. if (!is_file($path) || !is_readable($path)) {
  329. return $this->warning('Can\'t open config-file \''.$file.'\' ');
  330. }
  331. $ini = parse_ini_file($path,TRUE);
  332. if (!$ini) {
  333. return $this->warning('Errorneus ini-file \''.$file.'\'');
  334. }
  335. $section = (string) $section;
  336. if ($section !== '') {
  337. $ini = (isset($ini[$section]) and is_array($ini[$section]))?$ini[$section]:array();
  338. }
  339. foreach ($ini as $k => $v) {
  340. if (is_array($v)) {
  341. $this->_tpl_config = array_merge($this->_tpl_config,$v);
  342. }
  343. else {
  344. $this->_tpl_config[$k] = $v;
  345. }
  346. }
  347. return;
  348. }
  349. function load_filter($type,$name) {
  350. if (!in_array($type,array('output','pre','post'))) {
  351. return $this->warning('Unknown filter-type \''.$type.'\'');
  352. }
  353. if (!$p = $this->fetch_plugin($type.'filter.'.$name)) {
  354. return $this->warning('Can\'t load '.$type.'-filter \''.$name.'\'');
  355. }
  356. $a = $type.'filters';
  357. if ($type == 'output') {
  358. $this->outputfilters[$name] = 'quicky_'.$type.'filter_'.$name;
  359. }
  360. elseif ($type == 'pre') {
  361. $this->prefilters[$name] = 'quicky_'.$type.'filter_'.$name;
  362. }
  363. elseif ($type == 'post') {
  364. $this->postfilters[$name] = 'quicky_'.$type.'filter_'.$name;
  365. }
  366. include $p;
  367. }
  368. function load_compiler($a) {
  369. if (!isset($this->compilers[$a])) {
  370. $path = QUICKY_DIR.$a.'_compiler.class.php';
  371. if (!is_file($path) || !is_readable($path)) {
  372. $this->warning('Can\'t load compiler \''.$a.'\'.');
  373. return FALSE;
  374. }
  375. require_once $path;
  376. $class_name = $a.'_compiler';
  377. $this->compilers[$a] = new $class_name;
  378. $this->compilers[$a]->parent = $this;
  379. $this->compilers[$a]->prefilters = &$this->prefilters;
  380. $this->compilers[$a]->postfilters = &$this->postfilters;
  381. $this->compilers[$a]->prefs = &$this->compiler_prefs;
  382. $this->compilers[$a]->precompiled_vars = &$this->precompiled_vars;
  383. }
  384. return TRUE;
  385. }
  386. function _eval($string) {
  387. $var = &$this->_tpl_vars;
  388. $config = &$this->_tpl_config;
  389. $capture = &$this->_block_props['capture'];
  390. $foreach = &$this->_block_props['foreach'];
  391. $section = &$this->_block_props['section'];
  392. return eval($string);
  393. }
  394. function register_object($a,$b = NULL) {
  395. return $this->assign($a,$b);
  396. }
  397. function unregister_object($a) {
  398. return $this->clear_assign($a,$b);
  399. }
  400. function get_register_object($a) {
  401. return isset($this->_tpl_vars[$a])?$this->_tpl_vars[$a]:NULL;
  402. }
  403. function get_templates_vars($a = NULL) {
  404. return is_null($a)?$this->_tpl_vars:$this->_tpl_vars[$a];
  405. }
  406. function assign($a,$b = NULL,$scope = NULL) {
  407. if ($this->debug_mode === TRUE) {
  408. $dbg = debug_backtrace();
  409. $this->debug_trace['assign'][] = array(
  410. 'name' => $a,
  411. 'value' => $b,
  412. 'file' => $dbg[0]['file'],
  413. 'line' => $dbg[0]['line']
  414. );
  415. }
  416. if ($scope !== NULL) {
  417. if (is_array($a)) {
  418. $this->_local_vars[$scope] = array_merge($this->_local_vars[$scope],$a);
  419. }
  420. else {
  421. $this->_local_vars[$scope][$a] = $b;
  422. }
  423. }
  424. else {
  425. if (is_array($a)) {
  426. $this->_tpl_vars = array_merge($this->_tpl_vars,$a);
  427. }
  428. else {
  429. $this->_tpl_vars[$a] = $b;
  430. }
  431. }
  432. return TRUE;
  433. }
  434. function define($a,$b = NULL) {
  435. if (is_array($a)) {
  436. $this->_tpl_consts = array_merge($this->_tpl_consts,$a);
  437. }
  438. else {
  439. $this->_tpl_consts[$a] = $b;
  440. }
  441. return TRUE;
  442. }
  443. function assign_by_ref($a,&$b) {
  444. $this->_tpl_vars[$a] = &$b;
  445. return TRUE;
  446. }
  447. function clear_assign($a) {
  448. if (is_array($a)) {
  449. $a = array_values($a);
  450. $s = sizeof($a);
  451. for ($i = 0; $i < $s; $i++) {
  452. unset($this->_tpl_vars[$a[$i]]);
  453. }
  454. }
  455. else {
  456. unset($this->_tpl_vars[$a]);
  457. }
  458. }
  459. function reset() {
  460. $this->_tpl_vars = array();
  461. }
  462. function clear_all_assign() {
  463. $this->reset();
  464. }
  465. function clear_cache($path,$cache_id = NULL,$compile_id = NULL, $exp = -1) {
  466. if ($compile_id === NULL) {
  467. $compile_id = $this->compile_id;
  468. }
  469. if ($cache_id === NULL) {
  470. $cache_id = $this->cache_id;
  471. }
  472. $p = $this->_get_cache_path($path,$cache_id,$compile_id);
  473. if ($cache_id == '*') {
  474. $h = opendir($this->cache_dir);
  475. if (!$h) {
  476. return $this->warning('Can\'t open cache-dir \''.$this->cache_dir.'\'');
  477. }
  478. $e = explode('.',basename($p));
  479. $s = sizeof($e);
  480. $e = $e[$s-2].'.'.$e[$s-1];
  481. $s = strlen($e)*-1;
  482. while (($f = readdir($h)) !== FALSE) {
  483. if (is_file($this->cache_dir.$f) && (substr($f,$s) == $e)) {
  484. unlink($this->cache_dir.$f);
  485. }
  486. }
  487. return TRUE;
  488. }
  489. if (is_file($p) && ($exp == -1 || (filemtime($p) < time()-$exp))) {
  490. return unlink($p);
  491. }
  492. return FALSE;
  493. }
  494. function clear_all_cache($exp = -1) {
  495. $h = opendir($this->cache_dir);
  496. if (!$h) {
  497. return $this->warning('Can\'t open cache-dir \''.$this->cache_dir.'\'');
  498. }
  499. while (($f = readdir($h)) !== FALSE) {
  500. if (is_file($this->cache_dir.$f) && ($exp == -1 || (filemtime($this->cache_dir.$f) < time()-$exp))) {
  501. unlink($this->cache_dir.$f);
  502. }
  503. }
  504. }
  505. function clear_compiled_tpl($path,$compile_id = NULL, $exp = -1) {
  506. if ($compile_id === NULL) {
  507. $compile_id = $this->compile_id;
  508. }
  509. if ($cache_id === NULL) {
  510. $cache_id = $this->cache_id;
  511. }
  512. $p = $this->_get_compile_path($path,$compile_id);
  513. if ($compile_id == '*') {
  514. $h = opendir($this->compile_dir);
  515. if (!$h) {
  516. return $this->warning('Can\'t open compile-dir \''.$this->compile_dir.'\'');
  517. }
  518. $e = explode('.',$p);
  519. while (($f = readdir($h)) !== FALSE) {
  520. if (is_file($this->compile_dir.$f) && strpos($f,'.'.$e[6].'.') !== FALSE) {
  521. unlink($this->compile_dir.$f);
  522. }
  523. }
  524. return TRUE;
  525. }
  526. if (is_file($p) && ($exp == -1 || (filemtime($p) < time()-$exp))) {
  527. return unlink($p);
  528. }
  529. return FALSE;
  530. }
  531. function clear_all_compiled_tpl($exp = -1) {
  532. $h = opendir($this->compile_dir);
  533. if (!$h) {
  534. return $this->warning('Can\'t open compile-dir \''.$this->cache_dir.'\'');
  535. }
  536. while (($f = readdir($h)) !== FALSE) {
  537. if (is_file($this->compile_dir.$f) && ($exp == -1 || (filemtime($this->compile_dir.$f) < time()-$exp))) {
  538. unlink($this->compile_dir.$f);
  539. }
  540. }
  541. }
  542. function warning($err) {
  543. trigger_error($err,E_USER_WARNING);
  544. return FALSE;
  545. }
  546. function _get_template_path($path) {
  547. if ($path == '|debug.tpl') {
  548. return QUICKY_DIR.'debug.tpl';
  549. }
  550. if (strpos($path,'://') !== FALSE) {
  551. return $path;
  552. }
  553. static $cache = array();
  554. if (is_array($this->template_dir) && sizeof($this->template_dir)) {
  555. if (isset($cache[$path])) {
  556. return $cache[$path];
  557. }
  558. foreach ($this->template_dir as &$v) {
  559. if (file_exists($v.$path)) {
  560. return $cache[$path] = $v.$path;
  561. }
  562. }
  563. return $cache[$path] = $this->template_dir[0].$path;
  564. }
  565. return $this->template_dir.$path;
  566. }
  567. function _get_auto_filename($path,$cache_id = NULL,$compile_id = NULL) {
  568. if ($compile_id === NULL) {
  569. $compile_id = $this->compile_id;
  570. }
  571. if ($cache_id === NULL) {
  572. $cache_id = $this->cache_id;
  573. }
  574. $path = $this->_get_template_path($path);
  575. $name = basename($path).($this->auto_filename_prefix !== ''?'.'.$this->auto_filename_prefix:'').($this->lang !== ''?'.'.$this->lang:'').($compile_id !== ''?'.'.$compile_id:'').($cache_id !== ''?'.'.$cache_id:'').'.'.substr(abs(crc32($path)),0,6).'.php';
  576. return $name;
  577. }
  578. function display($path,$cache_id = NULL,$compile_id = NULL, $compiler = 'Quicky') {
  579. return $this->fetch($path,$cache_id,$compile_id,TRUE,$compiler);
  580. }
  581. function is_cached($path,$cache_id = NULL,$compile_id = NULL) {
  582. if ($compile_id === NULL) {
  583. $compile_id = $this->compile_id;
  584. }
  585. if ($cache_id === NULL) {
  586. $cache_id = $this->cache_id;
  587. }
  588. if (!$this->caching) {
  589. return FALSE;
  590. }
  591. $p = $this->_get_cache_path($path,$cache_id,$compile_id);
  592. return (is_file($p) && (($this->cache_lifetime == -1) || (filemtime($p) > time()-$this->cache_lifetime)))?$p:FALSE;
  593. }
  594. function _get_compile_path($path,$compile_id) {
  595. if ($compile_id === NULL) {
  596. $compile_id = $this->compile_id;
  597. }
  598. static $cache = array();
  599. if (isset($cache[$path])) {
  600. return $cache[$path];
  601. }
  602. return $cache[$path] = $this->compile_dir.$this->_get_auto_filename($path,'',$compile_id);
  603. }
  604. function _get_cache_path($path,$cache_id = NULL,$compile_id = NULL) {
  605. if ($compile_id === NULL) {
  606. $compile_id = $this->compile_id;
  607. }
  608. if ($cache_id === NULL) {
  609. $cache_id = $this->cache_id;
  610. }
  611. return $this->cache_dir.$this->_get_auto_filename($path,$cache_id,$compile_id);
  612. }
  613. function dynamic_callback($m) {
  614. return ((isset($m[1]) && $m[1] !== '')?$m[1]:'').'echo \'!'.UNIQUE_HASH.'!non_cache='.base64_encode($m[4]).'! \'; '.((isset($m[5]) && $m[5] !== '')?$m[5]:'');
  615. }
  616. function fetch($path,$cache_id = NULL,$compile_id = NULL,$display = FALSE,$compiler = 'Quicky') {
  617. if ($path === '' or ($path === NULL)) {
  618. return $this->warning('Empty path given');
  619. }
  620. if ($compile_id === NULL) {
  621. $compile_id = $this->compile_id;
  622. }
  623. if ($cache_id === NULL) {
  624. $cache_id = $this->cache_id;
  625. }
  626. if ($this->debug_mode === TRUE) {
  627. $dbg = debug_backtrace();
  628. $this->debug_trace['fetch'][] = array(
  629. 'path' => $path,
  630. 'cache_id' => $cache_id,
  631. 'compile_id' => $cache_id,
  632. 'display' => $display,
  633. 'compiler' => $compiler,
  634. 'file' => $dbg[0]['file'],
  635. 'line' => $dbg[1]['line']
  636. );
  637. }
  638. static $nesting_path = array();
  639. static $_old_block_props = array();
  640. $return = TRUE;
  641. $var = &$this->_tpl_vars;
  642. $const = &$this->_tpl_consts;
  643. $config = &$this->_tpl_config;
  644. $capture = &$this->_block_props['capture'];
  645. $foreach = &$this->_block_props['foreach'];
  646. $section = &$this->_block_props['section'];
  647. $cache = $compile = FALSE;
  648. if (($cache = $this->caching?$this->is_cached($path,$cache_id,$compile_id):FALSE) or ($compile = $this->_compile($path,$compile_id,$compiler))) {
  649. $p = $cache !== FALSE?$cache:$compile;
  650. if (error_reporting() != $this->error_reporting) {
  651. $old_err_rep = error_reporting();
  652. error_reporting($this->error_reporting);
  653. }
  654. else {
  655. $old_err_rep = -1;
  656. }
  657. if (!isset($nesting_path[$path])) {
  658. $nesting_path[$path] = 1;
  659. }
  660. else {
  661. ++$nesting_path[$path];
  662. }
  663. if ($nesting_path[$path] > $this->max_recursion_depth) {
  664. $this->warning('Max recursion depth exceed.');
  665. return;
  666. }
  667. $old_nesting_path = $nesting_path;
  668. $dir = dirname($path);
  669. if ($dir === '') {
  670. $dir = '.';
  671. }
  672. if ($this->caching && !$cache) {
  673. $c = file_get_contents($p);
  674. $a = preg_replace_callback($e = '~(<\?php )?/\*('.preg_quote(UNIQUE_HASH_STATIC,'~').')\{(dynamic)\}\*/ \?>(.*?)(?:<\?php )?/\*\{/\3\}\2\*/( \?>)?~si',array($this,'dynamic_callback'),$c);
  675. $fn = tempnam($this->cache_dir,'tmp');
  676. $fp = fopen($fn,'w');
  677. fwrite($fp,$a);
  678. fclose($fp);
  679. ob_start();
  680. $old = ob_get_contents();
  681. ob_clean();
  682. if ($this->caching == 1) {
  683. $this->caching = 0;
  684. }
  685. include $fn;
  686. $a = ob_get_contents();
  687. ob_end_clean();
  688. echo $old;
  689. unlink($fn);
  690. $a = preg_replace($e = '~!'.preg_quote(UNIQUE_HASH,'~').'!non_cache=(.*?)!~sie','base64_decode("$1")',$a);
  691. $cache = $this->_get_cache_path($path,$cache_id,$compile_id);
  692. $fp = fopen($cache,'w');
  693. fwrite($fp,$a);
  694. fclose($fp);
  695. $p = $cache;
  696. }
  697. if (!$display or sizeof($this->outputfilters) > 0) {
  698. ob_start();
  699. $old = ob_get_contents();
  700. ob_clean();
  701. if ($this->caching == 1) {
  702. $this->caching = 0;
  703. }
  704. include $p;
  705. $a = ob_get_contents();
  706. ob_end_clean();
  707. echo $old;
  708. if (sizeof($this->outputfilters) > 0) {
  709. $filters = array_values($this->outputfilters);
  710. for ($i = 0,$s = sizeof($filters); $i < $s; ++$i) {
  711. $a = call_user_func($filters[$i],$a,$this);
  712. }
  713. }
  714. if ($display) {
  715. echo $a;
  716. }
  717. else {
  718. $return = $a;
  719. }
  720. }
  721. else {
  722. if ($this->caching == 1) {
  723. $this->caching = 0;
  724. }
  725. include $p;
  726. }
  727. $nesting_path = $old_nesting_path;
  728. if ($old_err_rep !== -1) {
  729. error_reporting($old_err_rep);
  730. }
  731. --$nesting_path[$path];
  732. return $return;
  733. }
  734. else {
  735. return FALSE;
  736. }
  737. }
  738. function _is_compiled($path,$compile_id = NULL) {
  739. if ($compile_id === NULL) {
  740. $compile_id = $this->compile_id;
  741. }
  742. if ($this->force_compile) {
  743. return FALSE;
  744. }
  745. $p = $this->_get_compile_path($path,$compile_id);
  746. if (!is_file($p)) {
  747. return FALSE;
  748. }
  749. if ($this->compile_check) {
  750. if (filemtime($this->_get_template_path($path)) <= filemtime($p)) {
  751. return $p;
  752. }
  753. else {
  754. return FALSE;
  755. }
  756. }
  757. else {
  758. return $p;
  759. }
  760. }
  761. function _compile($path,$compile_id = NULL,$compiler,$force = FALSE) {
  762. if ($compile_id === NULL) {
  763. $compile_id = $this->compile_id;
  764. }
  765. if (!$force) {
  766. if ($p = $this->_is_compiled($path,$compile_id)) {
  767. return $p;
  768. }
  769. }
  770. $compiler_ver = array();
  771. $fp = fopen($tp = $this->_get_template_path($path),'r');
  772. if (!$fp) {
  773. $this->warning('Can\'t read template file: '.$path);
  774. return FALSE;
  775. }
  776. if ($l = fgets($fp)) {
  777. preg_match_all('~/(\w+)\=(.*?)(?=/|$)~',$l,$p,PREG_SET_ORDER);
  778. for ($i = 0,$s = sizeof($p); $i < $s; $i++) {
  779. $name = strtolower($p[$i][1]);
  780. $value = $p[$i][2];
  781. if ($name == 'compiler') {
  782. preg_match('~^(\w+)\s*(?:(>=|==|<=|<|>)?\s*(\S*))?~',$value,$q);
  783. $compiler = $q[1];
  784. if (isset($q[2]) && ($q[2] !== '')) {
  785. $compiler_ver = array($q[2],$q[3]);
  786. }
  787. }
  788. }
  789. }
  790. fclose($fp);
  791. if (!$this->load_compiler($compiler)) {
  792. return FALSE;
  793. }
  794. if (sizeof($compiler_ver) > 0) {
  795. if (!version_compare($this->compilers[$compiler]->compiler_version,$compiler_ver[1],$compiler_ver[0])) {
  796. $this->warning('Incompatible version of compiler '.$compiler.' ('.$this->compilers[$compiler]->compiler_version.') for template '.$path.' needed '.$compiler_ver[1]);
  797. return FALSE;
  798. }
  799. }
  800. $source = $this->compilers[$compiler]->_compile_source($tp,$path);
  801. $fp = fopen($c = $this->_get_compile_path($path,$compile_id),'w');
  802. if (!$fp) {
  803. return FALSE;
  804. }
  805. fwrite($fp,$source);
  806. fclose($fp);
  807. return $c;
  808. }
  809. function _compile_string($string,$compiler = NULL) {
  810. if ($compiler === NULL) {
  811. $compiler = $this->default_compiler;
  812. }
  813. $this->load_compiler($compiler);
  814. return $this->compilers[$compiler]->_compile_source_string($string);
  815. }
  816. static function ind($a,$b = 0) {
  817. $s = $a['st'] + abs($a['step']) * ($a['i']+$b);
  818. if ($s < 0) {
  819. return -1;
  820. }
  821. if ($a['step'] < 0) {
  822. $s = $a['s'] - $s;
  823. }
  824. return $s;
  825. }
  826. }