Passed
Push — master ( f9c2a7...616839 )
by Richard
07:25 queued 11s
created

XoopsTpl   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 90
c 3
b 0
f 0
dl 0
loc 242
rs 10
wmc 30

15 Methods

Rating   Name   Duplication   Size   Complexity  
A _get_auto_id() 0 8 4
A __construct() 0 31 3
A touch() 0 9 1
A fetchFromData() 0 17 3
A xoops_setTemplateDir() 0 5 1
A xoops_setCacheTime() 0 8 2
A clearCache() 0 13 1
A xoops_canUpdateFromFile() 0 5 1
A xoops_fetchFromData() 0 5 1
B setCompileId() 0 13 7
A xoops_setCompileDir() 0 5 1
A xoops_setCaching() 0 5 1
A xoops_setCacheDir() 0 5 1
A xoops_getTemplateDir() 0 5 1
A xoops_setDebugging() 0 5 2
1
<?php
2
/**
3
 * XOOPS template engine class
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       (c) 2000-2021 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @author              Kazumi Ono <[email protected]>
15
 * @author              Skalpa Keo <[email protected]>
16
 * @author              Taiwen Jiang <[email protected]>
17
 * @package             kernel
18
 * @subpackage          core
19
 */
20
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22
/**
23
 * Base class: Smarty template engine
24
 */
25
define('SMARTY_DIR', XOOPS_ROOT_PATH . '/class/smarty/');
26
require_once SMARTY_DIR . 'Smarty.class.php';
27
28
xoops_loadLanguage('global');
29
30
/**
31
 * Template engine
32
 *
33
 * @package             kernel
34
 * @subpackage          core
35
 * @author              Kazumi Ono <[email protected]>
36
 * @copyright       (c) 2000-2021 XOOPS Project (https://xoops.org)
37
 */
38
class XoopsTpl extends Smarty
39
{
40
    /**
41
     * XoopsTpl constructor.
42
     */
43
    public function __construct()
44
    {
45
        global $xoopsConfig;
46
47
        $this->left_delimiter  = '<{';
48
        $this->right_delimiter = '}>';
49
        $this->template_dir    = XOOPS_THEME_PATH;
50
        $this->cache_dir       = XOOPS_VAR_PATH . '/caches/smarty_cache';
51
        $this->compile_dir     = XOOPS_VAR_PATH . '/caches/smarty_compile';
52
        $this->compile_check   = ($xoopsConfig['theme_fromfile'] == 1);
53
        $this->plugins_dir     = array(
54
            XOOPS_ROOT_PATH . '/class/smarty/xoops_plugins',
55
            XOOPS_ROOT_PATH . '/class/smarty/plugins');
56
        if ($xoopsConfig['debug_mode']) {
57
            $this->debugging_ctrl = 'URL';
58
            $this->debug_tpl = XOOPS_ROOT_PATH . '/class/smarty/xoops_tpl/debug.tpl';
59
            if ($xoopsConfig['debug_mode'] == 3) {
60
                $this->debugging = true;
61
            }
62
        }
63
        parent::__construct();
64
        $this->setCompileId();
65
        $this->assign(array(
66
                          'xoops_url'        => XOOPS_URL,
67
                          'xoops_rootpath'   => XOOPS_ROOT_PATH,
68
                          'xoops_langcode'   => _LANGCODE,
69
                          'xoops_charset'    => _CHARSET,
70
                          'xoops_version'    => XOOPS_VERSION,
71
                          'xoops_upload_url' => XOOPS_UPLOAD_URL));
72
        $xoopsPreload = XoopsPreload::getInstance();
73
        $xoopsPreload->triggerEvent('core.class.template.new', array($this));
74
    }
75
76
    /**
77
     * Renders output from template data
78
     *
79
     * @param string $tplSource The template to render
80
     * @param bool   $display   If rendered text should be output or returned
81
     * @param null   $vars
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $vars is correct as it would always require null to be passed?
Loading history...
82
     *
83
     * @return string Rendered output if $display was false
84
     */
85
    public function fetchFromData($tplSource, $display = false, $vars = null)
0 ignored issues
show
Unused Code introduced by
The parameter $display is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    public function fetchFromData($tplSource, /** @scrutinizer ignore-unused */ $display = false, $vars = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        if (!function_exists('smarty_function_eval')) {
88
            require_once SMARTY_DIR . '/plugins/function.eval.php';
89
        }
90
        if (isset($vars)) {
91
            $oldVars = $this->_tpl_vars;
92
            $this->assign($vars);
93
            $out             = smarty_function_eval(array(
94
                                                        'var' => $tplSource), $this);
95
            $this->_tpl_vars = $oldVars;
96
97
            return $out;
98
        }
99
100
        return smarty_function_eval(array(
101
                                        'var' => $tplSource), $this);
102
    }
103
104
    /**
105
     * XoopsTpl::touch
106
     *
107
     * @param  mixed $resourceName
108
     * @return bool
109
     */
110
    public function touch($resourceName)
111
    {
112
        $isForced            = $this->force_compile;
113
        $this->force_compile = true;
114
        $this->clear_cache($resourceName);
115
        $result              = $this->_compile_resource($resourceName, $this->_get_compile_path($resourceName));
116
        $this->force_compile = $isForced;
117
118
        return $result;
119
    }
120
121
    /**
122
     * returns an auto_id for auto-file-functions
123
     *
124
     * @param  string $cache_id
125
     * @param  string $compile_id
126
     * @return string |null
127
     */
128
    public function _get_auto_id($cache_id = null, $compile_id = null)
129
    {
130
        if (isset($cache_id)) {
131
            return isset($compile_id) ? $compile_id . '-' . $cache_id : $cache_id;
132
        } elseif (isset($compile_id)) {
133
            return $compile_id;
134
        } else {
135
            return null;
136
        }
137
    }
138
139
    /**
140
     * XoopsTpl::setCompileId()
141
     *
142
     * @param  mixed $module_dirname
143
     * @param  mixed $theme_set
144
     * @param  mixed $template_set
145
     * @return void
146
     */
147
    public function setCompileId($module_dirname = null, $theme_set = null, $template_set = null)
148
    {
149
        global $xoopsConfig;
150
151
        $template_set      = empty($template_set) ? $xoopsConfig['template_set'] : $template_set;
152
        $theme_set         = empty($theme_set) ? $xoopsConfig['theme_set'] : $theme_set;
153
        if (class_exists('XoopsSystemCpanel', false)) {
154
            $cPrefix = 'cp-';
155
            $theme_set =  isset($xoopsConfig['cpanel']) ? $cPrefix .$xoopsConfig['cpanel'] : $cPrefix . 'default';
156
        }
157
        $module_dirname    = empty($module_dirname) ? (empty($GLOBALS['xoopsModule']) ? 'system' : $GLOBALS['xoopsModule']->getVar('dirname', 'n')) : $module_dirname;
158
        $this->compile_id  = substr(md5(XOOPS_URL), 0, 8) . '-' . $module_dirname . '-' . $theme_set . '-' . $template_set;
159
        $this->_compile_id = $this->compile_id;
160
    }
161
162
    /**
163
     * XoopsTpl::clearCache()
164
     *
165
     * @param  mixed $module_dirname
166
     * @param  mixed $theme_set
167
     * @param  mixed $template_set
168
     * @return bool
169
     */
170
    public function clearCache($module_dirname = null, $theme_set = null, $template_set = null)
171
    {
172
        $compile_id = $this->compile_id;
173
        $this->setCompileId($module_dirname, $template_set, $theme_set);
174
        $_params           = array(
175
            'auto_base'   => $this->cache_dir,
176
            'auto_source' => null,
177
            'auto_id'     => $this->compile_id,
178
            'exp_time'    => null);
179
        $this->_compile_id = $this->compile_id = $compile_id;
180
        require_once SMARTY_CORE_DIR . 'core.rm_auto.php';
181
182
        return smarty_core_rm_auto($_params, $this);
183
    }
184
185
    /**
186
     *
187
     * @deprecated DO NOT USE THESE METHODS, ACCESS THE CORRESPONDING PROPERTIES INSTEAD
188
     * @param $dirname
189
     */
190
    public function xoops_setTemplateDir($dirname)
191
    {
192
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setTemplateDir($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->template_dir=$value;\' instead.');
193
194
        $this->template_dir = $dirname;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function xoops_getTemplateDir()
201
    {
202
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_getTemplateDir()\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->template_dir;\' instead.');
203
204
        return $this->template_dir;
205
    }
206
207
    /**
208
     * @param bool $flag
209
     */
210
    public function xoops_setDebugging($flag = false)
211
    {
212
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setDebugging($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->debugging=$value;\' instead.');
213
214
        $this->debugging = is_bool($flag) ? $flag : false;
0 ignored issues
show
introduced by
The condition is_bool($flag) is always true.
Loading history...
215
    }
216
217
    /**
218
     * @param int $num
219
     */
220
    public function xoops_setCaching($num = 0)
221
    {
222
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setCaching($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->caching=$value;\' instead.');
223
224
        $this->caching = (int)$num;
225
    }
226
227
    /**
228
     * @param $dirname
229
     */
230
    public function xoops_setCompileDir($dirname)
231
    {
232
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setCompileDir($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->compile_dir=$value;\' instead.');
233
234
        $this->compile_dir = $dirname;
235
    }
236
237
    /**
238
     * @param $dirname
239
     */
240
    public function xoops_setCacheDir($dirname)
241
    {
242
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setCacheDir($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->cache_dir=$value;\' instead.');
243
244
        $this->cache_dir = $dirname;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250
    public function xoops_canUpdateFromFile()
251
    {
252
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_canUpdateFromFile()\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->compile_check;\' instead.');
253
254
        return $this->compile_check;
255
    }
256
257
    /**
258
     * @param $data
259
     *
260
     * @return string
261
     */
262
    public function xoops_fetchFromData($data)
263
    {
264
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_fetchFromData($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->fetchFromData($value);\' instead.');
265
266
        return $this->fetchFromData($data);
267
    }
268
269
    /**
270
     * @param int $num
271
     */
272
    public function xoops_setCacheTime($num = 0)
273
    {
274
        $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsTpl->xoops_setCacheTime($value)\' is deprecated since XOOPS 2.5.4, please use \'$xoopsTpl->cache_lifetime=$value;\' instead.');
275
276
        if (($num = (int)$num) <= 0) {
277
            $this->caching = 0;
278
        } else {
279
            $this->cache_lifetime = $num;
280
        }
281
    }
282
}
283
284
/**
285
 * function to update compiled template file in templates_c folder
286
 *
287
 * @param  string  $tpl_id
288
 * @param  boolean $clear_old
289
 * @return boolean
290
 */
291
function xoops_template_touch($tpl_id, $clear_old = true)
0 ignored issues
show
Unused Code introduced by
The parameter $clear_old is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

291
function xoops_template_touch($tpl_id, /** @scrutinizer ignore-unused */ $clear_old = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
292
{
293
    $tplfile_handler = xoops_getHandler('tplfile');
294
    $tplfile         = $tplfile_handler->get($tpl_id);
0 ignored issues
show
Bug introduced by
$tpl_id of type string is incompatible with the type integer expected by parameter $int_id of XoopsObjectHandler::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
    $tplfile         = $tplfile_handler->get(/** @scrutinizer ignore-type */ $tpl_id);
Loading history...
295
296
    if (is_object($tplfile)) {
297
        $file = $tplfile->getVar('tpl_file', 'n');
298
        $tpl  = new XoopsTpl();
299
300
        return $tpl->touch('db:' . $file);
301
    }
302
303
    return false;
304
}
305
306
/**
307
 * Clear the module cache
308
 *
309
 * @param  int $mid Module ID
310
 * @return void
311
 */
312
function xoops_template_clear_module_cache($mid)
313
{
314
    $block_arr = XoopsBlock::getByModule($mid);
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::getByModule() has been deprecated: (This also appears, dead, in XoopsBlockHandler) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

314
    $block_arr = /** @scrutinizer ignore-deprecated */ XoopsBlock::getByModule($mid);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
315
    $count     = count($block_arr);
316
    if ($count > 0) {
317
        $xoopsTpl          = new XoopsTpl();
318
        $xoopsTpl->caching = 2;
319
        for ($i = 0; $i < $count; ++$i) {
320
            if ($block_arr[$i]->getVar('template') != '') {
321
                $xoopsTpl->clear_cache('db:' . $block_arr[$i]->getVar('template'), 'blk_' . $block_arr[$i]->getVar('bid'));
322
            }
323
        }
324
    }
325
}
326