1: <?php
2: // $Id: template.php,v 1.1 2007/05/15 02:34:21 minahito Exp $
3: // ------------------------------------------------------------------------ //
4: // XOOPS - PHP Content Management System //
5: // Copyright (c) 2000 XOOPS.org //
6: // <http://www.xoops.org/> //
7: // ------------------------------------------------------------------------ //
8: // This program is free software; you can redistribute it and/or modify //
9: // it under the terms of the GNU General Public License as published by //
10: // the Free Software Foundation; either version 2 of the License, or //
11: // (at your option) any later version. //
12: // //
13: // You may not change or alter any portion of this comment or credits //
14: // of supporting developers from this source code or any supporting //
15: // source code which is considered copyrighted (c) material of the //
16: // original comment or credit authors. //
17: // //
18: // This program is distributed in the hope that it will be useful, //
19: // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21: // GNU General Public License for more details. //
22: // //
23: // You should have received a copy of the GNU General Public License //
24: // along with this program; if not, write to the Free Software //
25: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
26: // ------------------------------------------------------------------------ //
27: // Author: Kazumi Ono (AKA onokazu) //
28: // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29: // Project: The XOOPS Project //
30: // ------------------------------------------------------------------------- //
31:
32: if (!defined('SMARTY_DIR')) {
33: exit();
34: }
35: /**
36: * Base class: Smarty template engine
37: */
38: require_once SMARTY_DIR.'Smarty.class.php';
39:
40: /**
41: * Template engine
42: *
43: * @package kernel
44: * @subpackage core
45: *
46: * @author Kazumi Ono <onokazu@xoops.org>
47: * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org
48: */
49: class XoopsTpl extends Smarty
50: {
51:
52: /**
53: * Allow update of template files from the themes/ directory?
54: * This should be set to false on an active site to increase performance
55: */
56: var $_canUpdateFromFile = false;
57:
58: /**
59: * Constructor
60: **/
61: function XoopsTpl()
62: {
63: global $xoopsConfig;
64: $this->Smarty();
65: $this->compile_id = XOOPS_URL;
66: if ($xoopsConfig['theme_fromfile'] == 1) {
67: $this->_canUpdateFromFile = true;
68: $this->compile_check = true;
69: } else {
70: $this->_canUpdateFromFile = false;
71: $this->compile_check = false;
72: }
73: $this->left_delimiter = '<{';
74: $this->right_delimiter = '}>';
75: $this->template_dir = XOOPS_THEME_PATH;
76: $this->cache_dir = XOOPS_CACHE_PATH;
77: $this->compile_dir = XOOPS_COMPILE_PATH;
78: //loading under root_path for compatibility with XCL2.1
79: $this->plugins_dir = array(SMARTY_DIR.'plugins', XOOPS_ROOT_PATH.'/class/smarty/plugins');
80: // $this->default_template_handler_func = 'xoops_template_create';
81: $this->use_sub_dirs = false;
82:
83: $this->assign(array('xoops_url' => XOOPS_URL,
84: 'xoops_rootpath' => XOOPS_ROOT_PATH,
85: 'xoops_langcode' => _LANGCODE,
86: 'xoops_charset' => _CHARSET,
87: 'xoops_version' => XOOPS_VERSION,
88: 'xoops_upload_url' => XOOPS_UPLOAD_URL
89: ));
90:
91: if(empty($this->debug_tpl)) {
92: // set path to debug template from SMARTY_DIR
93: $this->debug_tpl = XOOPS_ROOT_PATH.'/modules/legacy/templates/xoops_debug.tpl';
94: if($this->security && is_file($this->debug_tpl)) {
95: $this->secure_dir[] = realpath($this->debug_tpl);
96: }
97: $this->debug_tpl = 'file:' . XOOPS_ROOT_PATH.'/modules/legacy/templates/xoops_debug.tpl';
98: }
99:
100: // Delegate 'XoopsTpl.New'
101: // Delegate may define additional initialization code for XoopTpl Instance;
102: // varArgs :
103: // 'xoopsTpl' [I/O] : $this
104: //
105: XCube_DelegateUtils::call('XoopsTpl.New', new XCube_Ref($this));
106: }
107:
108: /**
109: * Set the directory for templates
110: *
111: * @param string $dirname Directory path without a trailing slash
112: **/
113: function xoops_setTemplateDir($dirname)
114: {
115: $this->template_dir = $dirname;
116: }
117:
118: /**
119: * Get the active template directory
120: *
121: * @return string
122: **/
123: function xoops_getTemplateDir()
124: {
125: return $this->template_dir;
126: }
127:
128: /**
129: * Set debugging mode
130: *
131: * @param boolean $flag
132: **/
133: function xoops_setDebugging($flag=false)
134: {
135: $this->debugging = is_bool($flag) ? $flag : false;
136: }
137:
138: /**
139: * Set caching
140: *
141: * @param integer $num
142: **/
143: function xoops_setCaching($num=0)
144: {
145: $this->caching = (int)$num;
146: }
147:
148: /**
149: * Set cache lifetime
150: *
151: * @param integer $num Cache lifetime
152: **/
153: function xoops_setCacheTime($num=0)
154: {
155: $num = (int)$num;
156: if ($num <= 0) {
157: $this->caching = 0;
158: } else {
159: $this->cache_lifetime = $num;
160: }
161: }
162:
163: /**
164: * Set directory for compiled template files
165: *
166: * @param string $dirname Full directory path without a trailing slash
167: **/
168: function xoops_setCompileDir($dirname)
169: {
170: $this->compile_dir = $dirname;
171: }
172:
173: /**
174: * Set the directory for cached template files
175: *
176: * @param string $dirname Full directory path without a trailing slash
177: **/
178: function xoops_setCacheDir($dirname)
179: {
180: $this->cache_dir = $dirname;
181: }
182:
183: /**
184: * Render output from template data
185: *
186: * @deprecated
187: *
188: * @param string $data
189: * @return string Rendered output
190: **/
191: function xoops_fetchFromData(&$data)
192: {
193: $dummyfile = XOOPS_CACHE_PATH.'/dummy_'.time();
194: $fp = fopen($dummyfile, 'w');
195: fwrite($fp, $data);
196: fclose($fp);
197: $fetched = $this->fetch('file:'.$dummyfile);
198: unlink($dummyfile);
199: $this->clear_compiled_tpl('file:'.$dummyfile);
200: return $fetched;
201: }
202:
203: /**
204: *
205: **/
206: function xoops_canUpdateFromFile()
207: {
208: return $this->_canUpdateFromFile;
209: }
210:
211: function &fetchBlock($template,$bid)
212: {
213: $ret = $this->fetch('db:'.$template,$bid);
214: return $ret;
215: }
216:
217: function isBlockCached($template,$bid)
218: {
219: return $this->is_cached('db:'.$template, 'blk_'.$bid);
220: }
221:
222: function isModuleCached($templateName,$dirname)
223: {
224: if(!$templateName)
225: $templateName='system_dummy.html';
226:
227: return $this->is_cached('db:'.$templateName, $this->getModuleCachedTemplateId($dirname));
228: }
229:
230: function fetchModule($templateName,$dirname)
231: {
232: if(!$templateName)
233: $templateName='system_dummy.html';
234:
235: return $this->fetch('db:'.$templateName, $this->getModuleCachedTemplateId($dirname));
236: }
237:
238: function getModuleCachedTemplateId($dirname)
239: {
240: return 'mod_'.$dirname.'|'.md5(str_replace(XOOPS_URL, '', $GLOBALS['xoopsRequestUri']));
241: }
242:
243: /**
244: * Return smarty's debug console if debug mode is active.
245: *
246: * @return string
247: */
248: function fetchDebugConsole()
249: {
250: if ($this->debugging) {
251: // capture time for debugging info
252: $_params = array();
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:
262: /**
263: * function to update compiled template file in templates_c folder
264: *
265: * @param string $tpl_id
266: * @param boolean $clear_old
267: * @return boolean
268: **/
269: function 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 ($result === null) {
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: } else {
296: return $result;
297: }
298: }
299:
300: /**
301: * Smarty default template handler function
302: *
303: * @deprecated
304: *
305: * @param $resource_type
306: * @param $resource_name
307: * @param $template_source
308: * @param $template_timestamp
309: * @param $smarty_obj
310: * @return bool
311: **/
312: function xoops_template_create ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
313: {
314: if ( $resource_type == 'db' ) {
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: } else {
323: }
324: return false;
325: }
326:
327: /**
328: * Clear the module cache
329: *
330: * @deprecated
331: *
332: * @param int $mid Module ID
333: * @return
334: **/
335: function xoops_template_clear_module_cache($mid)
336: {
337: $block_arr =& XoopsBlock::getByModule($mid);
338: $count = count($block_arr);
339: if ($count > 0) {
340: $xoopsTpl = new XoopsTpl();
341: $xoopsTpl->xoops_setCaching(2);
342: for ($i = 0; $i < $count; $i++) {
343: if ($block_arr[$i]->getVar('template') != '') {
344: $xoopsTpl->clear_cache('db:'.$block_arr[$i]->getVar('template'), 'blk_'.$block_arr[$i]->getVar('bid'));
345: }
346: }
347: }
348: }
349: ?>
350: