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

/demo/scalr_newui/app/src/Lib/UI/Smarty/SmartyExt.class.php

https://github.com/kennethjiang/Wolke
PHP | 292 lines | 221 code | 38 blank | 33 comment | 55 complexity | ee8f76f7e03ceb5f14c8c3f97781bbb2 MD5 | raw file
  1. <?php
  2. class SmartyExt extends Smarty
  3. {
  4. /**#@-*/
  5. /**
  6. * The class constructor.
  7. */
  8. function __construct()
  9. {
  10. parent::Smarty();
  11. if (file_exists(SMARTY_DIR . $this->compiler_file))
  12. {
  13. require_once(SMARTY_DIR . $this->compiler_file);
  14. }
  15. else
  16. {
  17. // use include_path
  18. require_once($this->compiler_file);
  19. }
  20. $this->compiler_file = "Smarty_CompilerExt.class.php";
  21. $this->compiler_class = "Smarty_CompilerExt";
  22. }
  23. /**
  24. * executes & returns or displays the template results
  25. *
  26. * @param string $resource_name
  27. * @param string $cache_id
  28. * @param string $compile_id
  29. * @param boolean $display
  30. */
  31. function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
  32. {
  33. static $_cache_info = array();
  34. $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting)
  35. ? $this->error_reporting : error_reporting() & ~E_NOTICE);
  36. if (!$this->debugging && $this->debugging_ctrl == 'URL') {
  37. $_query_string = $this->request_use_auto_globals ? $_SERVER['QUERY_STRING'] : $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'];
  38. if (@strstr($_query_string, $this->_smarty_debug_id)) {
  39. if (@strstr($_query_string, $this->_smarty_debug_id . '=on')) {
  40. // enable debugging for this browser session
  41. @setcookie('SMARTY_DEBUG', true);
  42. $this->debugging = true;
  43. } elseif (@strstr($_query_string, $this->_smarty_debug_id . '=off')) {
  44. // disable debugging for this browser session
  45. @setcookie('SMARTY_DEBUG', false);
  46. $this->debugging = false;
  47. } else {
  48. // enable debugging for this page
  49. $this->debugging = true;
  50. }
  51. } else {
  52. $this->debugging = (bool)($this->request_use_auto_globals ? @$_COOKIE['SMARTY_DEBUG'] : @$GLOBALS['HTTP_COOKIE_VARS']['SMARTY_DEBUG']);
  53. }
  54. }
  55. if ($this->debugging) {
  56. // capture time for debugging info
  57. $_params = array();
  58. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  59. $_debug_start_time = smarty_core_get_microtime($_params, $this);
  60. $this->_smarty_debug_info[] = array('type' => 'template',
  61. 'filename' => $resource_name,
  62. 'depth' => 0);
  63. $_included_tpls_idx = count($this->_smarty_debug_info) - 1;
  64. }
  65. if (!isset($compile_id)) {
  66. $compile_id = $this->compile_id;
  67. }
  68. $this->_compile_id = $compile_id;
  69. $this->_inclusion_depth = 0;
  70. if ($this->caching) {
  71. // save old cache_info, initialize cache_info
  72. array_push($_cache_info, $this->_cache_info);
  73. $this->_cache_info = array();
  74. $_params = array(
  75. 'tpl_file' => $resource_name,
  76. 'cache_id' => $cache_id,
  77. 'compile_id' => $compile_id,
  78. 'results' => null
  79. );
  80. require_once(SMARTY_CORE_DIR . 'core.read_cache_file.php');
  81. if (smarty_core_read_cache_file($_params, $this)) {
  82. $_smarty_results = $_params['results'];
  83. if (!empty($this->_cache_info['insert_tags'])) {
  84. $_params = array('plugins' => $this->_cache_info['insert_tags']);
  85. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  86. smarty_core_load_plugins($_params, $this);
  87. $_params = array('results' => $_smarty_results);
  88. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  89. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  90. }
  91. if (!empty($this->_cache_info['cache_serials'])) {
  92. $_params = array('results' => $_smarty_results);
  93. require_once(SMARTY_CORE_DIR . 'core.process_compiled_include.php');
  94. $_smarty_results = smarty_core_process_compiled_include($_params, $this);
  95. }
  96. if ($display) {
  97. if ($this->debugging)
  98. {
  99. // capture time for debugging info
  100. $_params = array();
  101. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  102. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $_debug_start_time;
  103. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  104. $_smarty_results .= smarty_core_display_debug_console($_params, $this);
  105. }
  106. if ($this->cache_modified_check) {
  107. $_server_vars = ($this->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  108. $_last_modified_date = @substr($_server_vars['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_server_vars['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  109. $_gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']).' GMT';
  110. if (@count($this->_cache_info['insert_tags']) == 0
  111. && !$this->_cache_serials
  112. && $_gmt_mtime == $_last_modified_date) {
  113. if (php_sapi_name()=='cgi')
  114. header('Status: 304 Not Modified');
  115. else
  116. header('HTTP/1.1 304 Not Modified');
  117. } else {
  118. header('Last-Modified: '.$_gmt_mtime);
  119. echo $_smarty_results;
  120. }
  121. } else {
  122. echo $_smarty_results;
  123. }
  124. error_reporting($_smarty_old_error_level);
  125. // restore initial cache_info
  126. $this->_cache_info = array_pop($_cache_info);
  127. return true;
  128. } else {
  129. error_reporting($_smarty_old_error_level);
  130. // restore initial cache_info
  131. $this->_cache_info = array_pop($_cache_info);
  132. return $_smarty_results;
  133. }
  134. } else {
  135. $this->_cache_info['template'][$resource_name] = true;
  136. if ($this->cache_modified_check && $display) {
  137. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  138. }
  139. }
  140. }
  141. // load filters that are marked as autoload
  142. if (count($this->autoload_filters)) {
  143. foreach ($this->autoload_filters as $_filter_type => $_filters) {
  144. foreach ($_filters as $_filter) {
  145. $this->load_filter($_filter_type, $_filter);
  146. }
  147. }
  148. }
  149. $_smarty_compile_path = $this->_get_compile_path($resource_name);
  150. // if we just need to display the results, don't perform output
  151. // buffering - for speed
  152. $_cache_including = $this->_cache_including;
  153. $this->_cache_including = false;
  154. if ($display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
  155. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  156. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  157. {
  158. $this->_eval_compiled_resource($_smarty_compile_path);
  159. }
  160. } else {
  161. ob_start();
  162. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  163. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  164. {
  165. $this->_eval_compiled_resource($_smarty_compile_path);
  166. }
  167. $_smarty_results = ob_get_contents();
  168. ob_end_clean();
  169. foreach ((array)$this->_plugins['outputfilter'] as $_output_filter) {
  170. $_smarty_results = call_user_func_array($_output_filter[0], array($_smarty_results, &$this));
  171. }
  172. }
  173. if ($this->caching) {
  174. $_params = array('tpl_file' => $resource_name,
  175. 'cache_id' => $cache_id,
  176. 'compile_id' => $compile_id,
  177. 'results' => $_smarty_results);
  178. require_once(SMARTY_CORE_DIR . 'core.write_cache_file.php');
  179. smarty_core_write_cache_file($_params, $this);
  180. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  181. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  182. if ($this->_cache_serials) {
  183. // strip nocache-tags from output
  184. $_smarty_results = preg_replace('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!s'
  185. ,''
  186. ,$_smarty_results);
  187. }
  188. // restore initial cache_info
  189. $this->_cache_info = array_pop($_cache_info);
  190. }
  191. $this->_cache_including = $_cache_including;
  192. if ($display) {
  193. if (isset($_smarty_results)) { echo $_smarty_results; }
  194. if ($this->debugging) {
  195. // capture time for debugging info
  196. $_params = array();
  197. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  198. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
  199. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  200. echo smarty_core_display_debug_console($_params, $this);
  201. }
  202. error_reporting($_smarty_old_error_level);
  203. return;
  204. } else {
  205. error_reporting($_smarty_old_error_level);
  206. if (isset($_smarty_results)) { return $_smarty_results; }
  207. }
  208. }
  209. function _eval_compiled_resource($compile_path)
  210. {
  211. $contents = file_get_contents($compile_path);
  212. if (class_exists("Validator"))
  213. if (Validator::ScanForDebugCode($contents))
  214. throw new Exception("Invoked code contains disallowed function calls or classes. Please consult EPP-DRS onlne docs for a full list of disallowed names.", E_ERROR);
  215. eval($contents);
  216. }
  217. function _smarty_include($params)
  218. {
  219. if ($this->debugging) {
  220. $_params = array();
  221. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  222. $debug_start_time = smarty_core_get_microtime($_params, $this);
  223. $this->_smarty_debug_info[] = array('type' => 'template',
  224. 'filename' => $params['smarty_include_tpl_file'],
  225. 'depth' => ++$this->_inclusion_depth);
  226. $included_tpls_idx = count($this->_smarty_debug_info) - 1;
  227. }
  228. $this->_tpl_vars = array_merge($this->_tpl_vars, $params['smarty_include_vars']);
  229. // config vars are treated as local, so push a copy of the
  230. // current ones onto the front of the stack
  231. array_unshift($this->_config, $this->_config[0]);
  232. $_smarty_compile_path = $this->_get_compile_path($params['smarty_include_tpl_file']);
  233. if ($this->_is_compiled($params['smarty_include_tpl_file'], $_smarty_compile_path)
  234. || $this->_compile_resource($params['smarty_include_tpl_file'], $_smarty_compile_path))
  235. {
  236. $this->_eval_compiled_resource($_smarty_compile_path);
  237. }
  238. // pop the local vars off the front of the stack
  239. array_shift($this->_config);
  240. $this->_inclusion_depth--;
  241. if ($this->debugging) {
  242. // capture time for debugging info
  243. $_params = array();
  244. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  245. $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $debug_start_time;
  246. }
  247. if ($this->caching) {
  248. $this->_cache_info['template'][$params['smarty_include_tpl_file']] = true;
  249. }
  250. }
  251. /**#@-*/
  252. }
  253. /* vim: set expandtab: */
  254. ?>