XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
template.php
1<?php
13
14if (!defined('SMARTY_DIR')) {
15 exit();
16}
20require_once SMARTY_DIR.'Smarty.class.php';
21
22
23class XoopsTpl extends Smarty
24{
25
30 public $_canUpdateFromFile = false;
31
35 public function __construct()
36 {
37 global $xoopsConfig;
38 $this->Smarty();
39 $this->compile_id = XOOPS_URL;
40 if (1 == $xoopsConfig['theme_fromfile']) {
41 $this->_canUpdateFromFile = true;
42 $this->compile_check = false; /* This should be set to false on an active site*/
43 $this->force_compile = true; /* This setting overrides $compile_check. By default this is FALSE.
44 This is handy for development and debugging. It should never be used in a production environment.*/
45 } else {
46 $this->_canUpdateFromFile = false;
47 $this->compile_check = false;
48 $this->force_compile = false;
49 }
50 $this->left_delimiter = '<{';
51 $this->right_delimiter = '}>';
52 $this->template_dir = XOOPS_THEME_PATH;
53 $this->cache_dir = XOOPS_CACHE_PATH;
54 $this->compile_dir = XOOPS_COMPILE_PATH;
55 //Loading under root_path for compatibility with XCL2.1
56 //$this->plugins_dir = [SMARTY_DIR . 'plugins', XOOPS_ROOT_PATH . '/class/smarty/plugins'];
57 $this->plugins_dir = [SMARTY_DIR . 'plugins'];
58 // Legacy_RenderSystem
59 // $this->default_template_handler_func = 'xoops_template_create';
60 $this->use_sub_dirs = false;
61
62 // Legacy compatility requirement for development and debugging of templates
63 // Because D3 modules can use XoopsTpl class without Cube's boot
64 if (isset($GLOBALS['xoopsUserIsAdmin'])) {
65 $isadmin['xoops_isadmin']=$GLOBALS['xoopsUserIsAdmin'];
66 }else{
67 $isadmin['xoops_isadmin']=false;
68 }
69
70 $this->assign(
71 [
72 'xoops_url' => XOOPS_URL,
73 'xoops_rootpath' => XOOPS_ROOT_PATH,
74 'xoops_langcode' => _LANGCODE,
75 'xoops_charset' => _CHARSET,
76 'xoops_version' => XOOPS_VERSION,
77 'xoops_upload_url' => XOOPS_UPLOAD_URL,
78 ]
79 );
80
81 if (empty($this->debug_tpl)) {
82 // set path to debug template
83 $this->debug_tpl = XOOPS_ROOT_PATH.'/modules/legacy/templates/xoops_debug.tpl';
84 if ($this->security && is_file($this->debug_tpl)) {
85 $this->secure_dir[] = realpath($this->debug_tpl);
86 }
87 // set config debug mode
88 if ($xoopsConfig['debug_mode'] == 3) {
89 $this->debugging = true;
90 }
91 $this->debug_tpl = 'file:' . XOOPS_ROOT_PATH.'/modules/legacy/templates/xoops_debug.tpl';
92 }
93
94 // Delegate 'XoopsTpl.New'
95 // Delegate may define additional initialization code for XoopTpl Instance;
96 // varArgs :
97 // 'xoopsTpl' [I/O] : $this
98 //
99 XCube_DelegateUtils::call('XoopsTpl.New', new XCube_Ref($this));
100 }
101 public function XoopsTpl()
102 {
103 return $this->__construct();
104 }
105
111 public function xoops_setTemplateDir(string $dirname)
112 {
113 $this->template_dir = $dirname;
114 }
115
121 public function xoops_getTemplateDir()
122 {
123 return $this->template_dir;
124 }
125
131 public function xoops_setDebugging($flag=false)
132 {
133 $this->debugging = is_bool($flag) ? $flag : false;
134 }
135
141 public function xoops_setCaching($num=0)
142 {
143 $this->caching = (int)$num;
144 }
145
151 public function xoops_setCacheTime($num=0)
152 {
153 $num = (int)$num;
154 if ($num <= 0) {
155 $this->caching = 0;
156 } else {
157 $this->cache_lifetime = $num;
158 }
159 }
160
166 public function xoops_setCompileDir($dirname)
167 {
168 $this->compile_dir = $dirname;
169 }
170
176 public function xoops_setCacheDir($dirname)
177 {
178 $this->cache_dir = $dirname;
179 }
180
189 public function xoops_fetchFromData(&$data)
190 {
191 $dummyfile = XOOPS_CACHE_PATH.'/dummy_'.time();
192 $fp = fopen($dummyfile, 'w');
193 fwrite($fp, $data);
194 fclose($fp);
195 $fetched = $this->fetch('file:'.$dummyfile);
196 unlink($dummyfile);
197 $this->clear_compiled_tpl('file:'.$dummyfile);
198 return $fetched;
199 }
200
204 public function xoops_canUpdateFromFile()
205 {
207 }
208
209 public function &fetchBlock($template, $bid)
210 {
211 $ret = $this->fetch('db:'.$template, $bid);
212 return $ret;
213 }
214
215 public function isBlockCached($template, $bid)
216 {
217 return $this->is_cached('db:'.$template, 'blk_'.$bid);
218 }
219
220 public function isModuleCached($templateName, $dirname)
221 {
222 if (!$templateName) {
223 $templateName='system_dummy.html';
224 }
225
226 return $this->is_cached('db:'.$templateName, $this->getModuleCachedTemplateId($dirname));
227 }
228
229 public function fetchModule($templateName, $dirname)
230 {
231 if (!$templateName) {
232 $templateName='system_dummy.html';
233 }
234
235 return $this->fetch('db:'.$templateName, $this->getModuleCachedTemplateId($dirname));
236 }
237
238 public function getModuleCachedTemplateId($dirname)
239 {
240 return 'mod_'.$dirname.'|'.md5(str_replace(XOOPS_URL, '', $GLOBALS['xoopsRequestUri']));
241 }
242
248 public function fetchDebugConsole()
249 {
250 if ($this->debugging) {
251 // capture time for debugging info
252 $_params = [];
253 require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
254 $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
255 require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
256 return smarty_core_display_debug_console($_params, $this);
257 }
258 }
259}
260
261
269function xoops_template_touch($tpl_id, $clear_old = true)
270{
271 $result = null;
272
273 // RaiseEvent 'Legacy.XoopsTpl.TemplateTouch'
274 // Delegate may define new template touch logic (with XC21, only for clear cache & compiled template)
275 // varArgs :
276 // 'xoopsTpl' [I/O] : $this
277 //
278 XCube_DelegateUtils::call('Legacy.XoopsTpl.TemplateTouch', $tpl_id, $clear_old, new XCube_Ref($result));
279
280 if (null == $result) {
281 $tpl = new XoopsTpl();
282 $tpl->force_compile = true;
283 $tplfile_handler =& xoops_gethandler('tplfile');
284 $tplfile =& $tplfile_handler->get($tpl_id);
285 if (is_object($tplfile)) {
286 $file = $tplfile->getVar('tpl_file');
287 if ($clear_old) {
288 $tpl->clear_cache('db:'.$file);
289 $tpl->clear_compiled_tpl('db:'.$file);
290 }
291 // $tpl->fetch('db:'.$file);
292 return true;
293 }
294 return false;
295 }
296
297 return $result;
298}
299
312function xoops_template_create($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
313{
314 if ('db' == $resource_type) {
315 $file_handler =& xoops_gethandler('tplfile');
316 $tpl =& $file_handler->find('default', null, null, null, $resource_name, true);
317 if (count($tpl) > 0 && is_object($tpl[0])) {
318 $template_source = $tpl[0]->getSource();
319 $template_timestamp = $tpl[0]->getLastModified();
320 return true;
321 }
322 }
323
324 return false;
325}
326
335 function xoops_template_clear_module_cache($mid)
336 {
337 $block_arr =& XoopsBlock::sGetByModule($mid);
338 $count = count($block_arr);
339 if ($count > 0) {
340 $xoopsTpl = new XoopsTpl();
341 $xoopsTpl->xoops_setCaching(2);
342 foreach ($block_arr as $iValue) {
343 if ('' !== $iValue->getVar('template')) {
344 $xoopsTpl->clear_cache('db:'. $iValue->getVar('template'), 'blk_'. $iValue->getVar('bid'));
345 }
346 }
347 }
348 }
349
__construct()
Definition template.php:35
xoops_getTemplateDir()
Definition template.php:121
xoops_canUpdateFromFile()
Definition template.php:204
xoops_setTemplateDir(string $dirname)
Definition template.php:111
xoops_setCompileDir($dirname)
Definition template.php:166
xoops_setCacheTime($num=0)
Definition template.php:151
$_canUpdateFromFile
Definition template.php:30
xoops_setCaching($num=0)
Definition template.php:141
xoops_fetchFromData(&$data)
Definition template.php:189
fetchDebugConsole()
Definition template.php:248
xoops_setDebugging($flag=false)
Definition template.php:131
xoops_setCacheDir($dirname)
Definition template.php:176