1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core;
13:
14: /**
15: * XOOPS template engine class
16: *
17: * @category Xoops\Core
18: * @package Template
19: * @author Kazumi Ono <onokazu@xoops.org>
20: * @author Skalpa Keo <skalpa@xoops.org>
21: * @author Taiwen Jiang <phppp@users.sourceforge.net>
22: * @copyright 2000-2015 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @link http://xoops.org
25: */
26: class XoopsTpl extends \Smarty
27: {
28: /**
29: * @var \Xoops\Core\Theme\XoopsTheme
30: */
31: public $currentTheme = null;
32:
33: /**
34: * XoopsTpl constructor
35: */
36: public function __construct()
37: {
38: parent::__construct(); // SMARTY_PLUGINS_DIR is initialized into parent
39: $xoops = \Xoops::getInstance();
40: $xoops->events()->triggerEvent('core.template.construct.start', array($this));
41:
42: $this->registerFilter(
43: 'pre',
44: [$this, 'convertLegacyDelimiters']
45: );
46:
47: //$this->left_delimiter = '<{';
48: //$this->right_delimiter = '}>';
49:
50: $this->setTemplateDir(\XoopsBaseConfig::get('themes-path'));
51: $this->setCacheDir(\XoopsBaseConfig::get('smarty-cache'));
52: $this->setCompileDir(\XoopsBaseConfig::get('smarty-compile'));
53: $this->compile_check = ($xoops->getConfig('theme_fromfile') == 1);
54: $this->setPluginsDir(\XoopsBaseConfig::get('smarty-xoops-plugins'));
55: $this->addPluginsDir(SMARTY_PLUGINS_DIR);
56: $this->setCompileId();
57: $this->assign(
58: array('xoops_url' => \XoopsBaseConfig::get('url'),
59: 'xoops_rootpath' => \XoopsBaseConfig::get('root-path'),
60: 'xoops_langcode' => \XoopsLocale::getLangCode(),
61: 'xoops_charset' => \XoopsLocale::getCharset(),
62: 'xoops_version' => \Xoops::VERSION,
63: 'xoops_upload_url' => \XoopsBaseConfig::get('uploads-url'))
64: );
65: }
66:
67: /**
68: * XOOPS legacy used '<{' and '}>' as delimiters rather than using the default '{' and '}'.
69: * This prefilter function converts any legacy delimiters to Smarty default delimiters.
70: *
71: * The intention is to phase out the legacy delimiters entirely.
72: *
73: * @param string $tpl_source template source
74: * @param \Smarty_Internal_Template $template template object
75: *
76: * @return string source with any legacy delimiters converted to standard default delimiters
77: */
78: public function convertLegacyDelimiters($tpl_source, \Smarty_Internal_Template $template)
79: {
80: return str_replace(['<{', '}>'], ['{', '}'], $tpl_source);
81: }
82:
83: /**
84: * XoopsTpl::touch
85: *
86: * @param string $resourceName name of resource
87: *
88: * @return bool
89: */
90: public function touch($resourceName)
91: {
92: $isForced = $this->force_compile;
93: $this->force_compile = true;
94: $this->clearCache($resourceName);
95: $result = true; // $this->_compile_resource($resourceName, $this->_get_compile_path($resourceName));
96: $this->force_compile = $isForced;
97: return $result;
98: }
99:
100: /**
101: * XoopsTpl::setCompileId()
102: *
103: * @param mixed $module_dirname module directory
104: * @param mixed $theme_set theme set
105: * @param mixed $template_set template set
106: *
107: * @return void
108: */
109: public function setCompileId($module_dirname = null, $theme_set = null, $template_set = null)
110: {
111: $xoops = \Xoops::getInstance();
112:
113: $template_set = empty($template_set) ? $xoops->getConfig('template_set') : $template_set;
114: $theme_set = empty($theme_set) ? $xoops->getConfig('theme_set') : $theme_set;
115: $module_dirname = empty($module_dirname) ? $xoops->moduleDirname : $module_dirname;
116: $this->compile_id = substr(md5(\XoopsBaseConfig::get('url')), 0, 8) . '-' . $module_dirname
117: . '-' . $theme_set . '-' . $template_set;
118: //$this->_compile_id = $this->compile_id;
119: }
120:
121: /**
122: * XoopsTpl::clearModuleCompileCache()
123: *
124: * Clean up compiled and cached templates for a module
125: *
126: * TODO - handle $use_sub_dirs cases
127: *
128: * @param mixed $module_dirname module directory
129: * @param mixed $theme_set theme set
130: * @param mixed $template_set template set
131: *
132: * @return int number of deleted cache and compiler files
133: */
134: public function clearModuleCompileCache($module_dirname = null, $theme_set = null, $template_set = null)
135: {
136: $hold_compile_id = $this->compile_id;
137: // $this->setCompileId($module_dirname, $template_set, $theme_set);
138: // TODO - should handle $use_sub_dirs
139: $this->setCompileId($module_dirname, '*', '*');
140: $compile_id = $this->compile_id;
141: $this->compile_id = $hold_compile_id;
142: $compile_id = preg_replace('![^\w\|]+!', '_', $compile_id);
143: $glob = $compile_id . '*.php';
144: $count=0;
145: $files = glob($this->getCompileDir() . '/' . $glob);
146: foreach ($files as $filename) {
147: $count += unlink($filename) ? 1 : 0;
148: }
149: $files = glob($this->getCacheDir() . '/*' . $glob);
150: foreach ($files as $filename) {
151: $count += unlink($filename) ? 1 : 0;
152: }
153: return $count;
154: }
155:
156: /**
157: * Empty cache for a specific template
158: *
159: * This is just a pass-through wrapper with a warning since this method previously existed
160: * only in XoopsTpl, but now is also a regular Smarty method.
161: *
162: * clearModuleCompileCache() is the replacement for the old clearCache
163: *
164: * @param string $template_name template name
165: * @param string $cache_id cache id
166: * @param string $compile_id compile id
167: * @param integer $exp_time expiration time
168: * @param string $type resource type
169: *
170: * @return integer number of cache files deleted
171: */
172: public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
173: {
174: \Xoops::getInstance()->deprecated('XoopsTpl::clearCache() is potentially ambiguous');
175: return parent::clearCache($template_name, $cache_id, $compile_id, $exp_time, $type);
176: }
177: }
178: