PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/XoopsEngine/lib/Xoops/Smarty/Engine.php

https://github.com/xoops-pi/engine
PHP | 287 lines | 162 code | 27 blank | 98 comment | 17 complexity | f5c23b5c583aeebfd5e122fba45c34a0 MD5 | raw file
  1. <?php
  2. /**
  3. * XOOPS SMARTY template engine
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright Xoops Engine http://www.xoopsengine.org/
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @package Xoops_Smarty
  17. * @version $Id$
  18. */
  19. /**
  20. * set SMARTY_DIR to absolute path to Smarty library files.
  21. * Sets SMARTY_DIR only if user application has not already defined it.
  22. */
  23. if (!defined('SMARTY_DIR')) {
  24. define('SMARTY_DIR', XOOPS::path("lib") . DIRECTORY_SEPARATOR . 'Smarty' . DIRECTORY_SEPARATOR);
  25. }
  26. /**
  27. * Base class: Smarty template engine
  28. */
  29. require SMARTY_DIR . 'Smarty.class.php';
  30. /**
  31. * Autoloader
  32. */
  33. function Xoops_Smarty_Autoload($class)
  34. {
  35. $_class = strtolower($class);
  36. if (substr($_class, 0, 16) === 'smarty_internal_' || $_class == 'smarty_security') {
  37. return SMARTY_SYSPLUGINS_DIR . $_class . '.php';
  38. }
  39. }
  40. XOOPS::autoloader()->registerCallback("Xoops_Smarty_Autoload");
  41. /**
  42. * Template engine
  43. */
  44. class Xoops_Smarty_Engine extends Smarty
  45. {
  46. public $currentTemplate;
  47. //private $cache_id;
  48. public function __construct($options = array())
  49. {
  50. parent::__construct();
  51. // caching type
  52. //$this->caching_type = 'xoops';
  53. //$this->caching = false;
  54. $this->left_delimiter = '<{';
  55. $this->right_delimiter = '}>';
  56. $this->cache_dir = XOOPS::path("var") . DIRECTORY_SEPARATOR . "cache" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "cache";
  57. $this->compile_dir = XOOPS::path("var") . DIRECTORY_SEPARATOR . "cache" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "compile";
  58. $this->setTemplateDir(array());
  59. $this->addPluginsDir(__DIR__ . DIRECTORY_SEPARATOR . 'plugins');
  60. $this->template_class = "Xoops_Smarty_Template";
  61. $this->default_template_handler_func = array(&$this, "templateHandler");
  62. $this->setOptions($options);
  63. }
  64. public function setOptions($options = array())
  65. {
  66. $properties = array(
  67. 'caching' => false,
  68. 'compile_check' => false,
  69. 'debugging' => false,
  70. 'force_compile' => false,
  71. 'template_class' => '',
  72. 'error_unassigned' => false,
  73. );
  74. foreach ($options as $key => $val) {
  75. if (array_key_exists($key, $properties)) {
  76. $this->$key = $val;
  77. }
  78. }
  79. if (XOOPS::service('logger')->silent()) {
  80. $this->debugging = false;
  81. }
  82. }
  83. /**
  84. * Create compile_id with purposes of distinguishing theme set, module and domain. Template set is not considered
  85. */
  86. public function setCompileId($theme_set = null, $module_dirname = null)
  87. {
  88. $this->_compile_id = $this->compile_id = $this->generateCompileId($theme_set, $module_dirname);
  89. return $this;
  90. }
  91. public function generateCompileId($theme_set = null, $module_dirname = null)
  92. {
  93. $segs = array();
  94. $segs[] = is_null($module_dirname)
  95. ? XOOPS::config('identifier')
  96. : $module_dirname;
  97. $segs[] = is_null($theme_set)
  98. ? XOOPS::config('theme_set')
  99. : $theme_set;
  100. $segs = array_filter($segs);
  101. $compile_id = empty($segs) ? null : implode('-', $segs);
  102. return $compile_id;
  103. }
  104. /**
  105. * Create cache_id with purposes of distinguishing cache level
  106. */
  107. public function setCacheId($cache_id = null, $level = null)
  108. {
  109. $this->cache_id = Xoops_Zend_Cache::generateId($cache_id, $level);
  110. return $this;
  111. }
  112. /**
  113. * test to see if valid cache exists for this template
  114. *
  115. * @param string $tpl_file name of template file
  116. * @param string $cache_id
  117. * @param string $compile_id
  118. * @return string|false results of {@link _read_cache_file()}
  119. */
  120. public function is_cached($tpl_file, $cache_id = null, $compile_id = null)
  121. {
  122. $cache_id = is_null($cache_id) ? $this->cache_id : $cache_id;
  123. return parent::isCached($tpl_file, $cache_id, $compile_id);
  124. }
  125. /**
  126. * fetches a rendered Smarty template
  127. *
  128. * @param string $template the resource handle of the template file or template object
  129. * @param mixed $cache_id cache id to be used with this template
  130. * @param mixed $compile_id compile id to be used with this template
  131. * @param object $ |null $parent next higher level of Smarty variables
  132. * @return string rendered template output
  133. */
  134. public function fetch($template, $cache_id = null, $compile_id = null, $parent = null, $display = false)
  135. {
  136. $this->currentTemplate = ($template instanceof $this->template_class)
  137. ? $template->buildTemplateFilepath()
  138. : $template;
  139. $cache_id = is_null($cache_id) ? $this->cache_id : $cache_id;
  140. try {
  141. $output = parent::fetch($template, $cache_id, $compile_id, $parent, $display);
  142. } catch (Exception $e) {
  143. trigger_error("<pre>" . $e->__toString() . "</pre><br />" . $template);
  144. $output = $e->getMessage();
  145. }
  146. return $output;
  147. }
  148. /**
  149. * Clears module and theme specified compiled templates
  150. *
  151. * @see Smarty_Internal_Utility::clearCompiledTemplate
  152. */
  153. public function clearTemplate($module_dirname = null, $theme_set = null)
  154. {
  155. $compile_id = $this->generateCompileId($theme_set, $module_dirname);
  156. return $this->clearCompiledTemplate(null, $compile_id);
  157. }
  158. /**
  159. * Clears module and theme specified caches
  160. *
  161. * @see Smarty_Internal_Cache::clear
  162. */
  163. public function clearCaches($module_dirname = null, $theme_set = null)
  164. {
  165. $compile_id = $this->generateCompileId($theme_set, $module_dirname);
  166. return $this->clearCache(null, null, $compile_id);
  167. }
  168. /**
  169. * Clears module caches and compiled templates
  170. */
  171. public function clearModuleCache($module_dirname = null)
  172. {
  173. if (empty($module_dirname)) {
  174. $this->clearTemplate(null, null);
  175. $this->clearCache(null, null);
  176. return true;
  177. }
  178. $themes = XOOPS::service("registry")->theme->read();
  179. foreach (array_keys($themes) as $theme) {
  180. $this->clearTemplate($module_dirname, $theme);
  181. $this->clearCache($module_dirname, $theme);
  182. }
  183. return true;
  184. }
  185. /**
  186. * Clears caches of a specified cache_id
  187. */
  188. public function clearCacheByCacheId($cache_id, $module_dirname = null)
  189. {
  190. if (empty($module_dirname)) {
  191. if (XOOPS::registry("module")) {
  192. $module_dirname = XOOPS::registry("module")->dirname;
  193. }
  194. }
  195. if (empty($cache_id) || empty($module_dirname)) {
  196. return false;
  197. }
  198. $themes = XOOPS::service("registry")->theme->read();
  199. foreach (array_keys($themes) as $theme) {
  200. $compile_id = $this->generateCompileId($theme, $module_dirname);
  201. $this->cache->clear(null, $cacheId, $compile_id);
  202. }
  203. return true;
  204. }
  205. public function getVersion()
  206. {
  207. return self::SMARTY_VERSION; //$this->_version;
  208. }
  209. /**
  210. * Default template handler
  211. *
  212. * Transform simplified path to regular path:
  213. * app/module/template.html => app/module/templates/template.html
  214. * block/module/template.html => app/module/templates/blocks/template.html
  215. * admin/module/template.html => app/module/templates/admin/template.html
  216. *
  217. * @see: Smarty_Internal_Template::buildTemplateFilepath()
  218. *
  219. * @param string $resource_type
  220. * @param string $resource_name
  221. * @param string $template_source
  222. * @param int $template_timestamp
  223. * @param {@Smarty_Internal_Template} $template
  224. * @return string translated template resource path
  225. */
  226. public function templateHandler($resource_type, $resource_name, &$template_source, &$template_timestamp, $template)
  227. {
  228. /**#@+
  229. * This is handled by viewRenderer
  230. * Thus in a template, "templates" need specified, like {include file="app/mymodule/templates/template.html"}
  231. */
  232. // Split "section/item/file" to "section/item" and "file"
  233. // Insert "templates" and assemble asd "section/item/templates/file"
  234. /**#@-*/
  235. $segs = explode("/", $resource_name, 3);
  236. if (count($segs) == 3 && $segs[0] != "theme") {
  237. switch ($segs[0]) {
  238. case "block":
  239. $resource_name = "app/" . $segs[1] . "/templates/blocks/" . $segs[2];
  240. break;
  241. case "admin":
  242. $resource_name = "app/" . $segs[1] . "/templates/admin/" . $segs[2];
  243. break;
  244. case "module":
  245. $resource_name = "module/" . $segs[1] . "/templates/" . $segs[2];
  246. break;
  247. default:
  248. case "app":
  249. $resource_name = "app/" . $segs[1] . "/templates/" . $segs[2];
  250. break;
  251. }
  252. }
  253. $path = XOOPS::registry("view")->resourcePath($resource_name, true);
  254. //trigger_error("path:".$path);
  255. if (!file_exists($path)) {
  256. $path = false;
  257. }
  258. return $path;
  259. }
  260. }