source: temp/test-xoops.ec-cube.net/html/class/template.php @ 405

Revision 405, 7.4 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: template.php,v 1.3 2005/10/24 11:44:16 onokazu 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
32if (!defined('SMARTY_DIR')) {
33    exit();
34}
35/**
36 * Base class: Smarty template engine
37 */
38require_once SMARTY_DIR.'Smarty.class.php';
39
40/**
41 * Template engine
42 *
43 * @package     kernel
44 * @subpackage  core
45 *
46 * @author      Kazumi Ono  <[email protected]>
47 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
48 */
49class 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 = null;
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        $this->plugins_dir = array(XOOPS_ROOT_PATH.'/class/smarty/plugins');
79        $this->default_template_handler_func = 'xoops_template_create';
80       
81        // Added by goghs on 11-26 to deal with safe mode
82        //if (ini_get('safe_mode') == "1") {
83            $this->use_sub_dirs = false;
84        //} else {
85        //  $this->use_sub_dirs = true;
86        //}
87        // END
88
89        $this->assign(array('xoops_url' => XOOPS_URL, 'xoops_rootpath' => XOOPS_ROOT_PATH, 'xoops_langcode' => _LANGCODE, 'xoops_charset' => _CHARSET, 'xoops_version' => XOOPS_VERSION, 'xoops_upload_url' => XOOPS_UPLOAD_URL));
90    }
91
92    /**
93     * Set the directory for templates
94     *
95     * @param   string  $dirname    Directory path without a trailing slash
96     **/
97    function xoops_setTemplateDir($dirname)
98    {
99        $this->template_dir = $dirname;
100    }
101
102    /**
103     * Get the active template directory
104     *
105     * @return  string
106     **/
107    function xoops_getTemplateDir()
108    {
109        return $this->template_dir;
110    }
111
112    /**
113     * Set debugging mode
114     *
115     * @param   boolean     $flag
116     **/
117    function xoops_setDebugging($flag=false)
118    {
119        $this->debugging = is_bool($flag) ? $flag : false;
120    }
121
122    /**
123     * Set caching
124     *
125     * @param   integer     $num
126     **/
127    function xoops_setCaching($num=0)
128    {
129        $this->caching = (int)$num;
130    }
131
132    /**
133     * Set cache lifetime
134     *
135     * @param   integer     $num    Cache lifetime
136     **/
137    function xoops_setCacheTime($num=0)
138    {
139        $num = (int)$num;
140        if ($num <= 0) {
141            $this->caching = 0;
142        } else {
143            $this->cache_lifetime = $num;
144        }
145    }
146
147    /**
148     * Set directory for compiled template files
149     *
150     * @param   string  $dirname    Full directory path without a trailing slash
151     **/
152    function xoops_setCompileDir($dirname)
153    {
154        $this->compile_dir = $dirname;
155    }
156
157    /**
158     * Set the directory for cached template files
159     *
160     * @param   string  $dirname    Full directory path without a trailing slash
161     **/
162    function xoops_setCacheDir($dirname)
163    {
164        $this->cache_dir = $dirname;
165    }
166
167    /**
168     * Render output from template data
169     *
170     * @param   string  $data
171     * @return  string  Rendered output 
172     **/
173    function xoops_fetchFromData(&$data)
174    {
175        $dummyfile = XOOPS_CACHE_PATH.'/dummy_'.time();
176        $fp = fopen($dummyfile, 'w');
177        fwrite($fp, $data);
178        fclose($fp);
179        $fetched = $this->fetch('file:'.$dummyfile);
180        unlink($dummyfile);
181        $this->clear_compiled_tpl('file:'.$dummyfile);
182        return $fetched;
183    }
184
185    /**
186     *
187     **/
188    function xoops_canUpdateFromFile()
189    {
190        return $this->_canUpdateFromFile;
191    }
192}
193
194/**
195 * Smarty default template handler function
196 *
197 * @param $resource_type
198 * @param $resource_name
199 * @param $template_source
200 * @param $template_timestamp
201 * @param $smarty_obj
202 * @return  bool
203 **/
204function xoops_template_create ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
205{
206    if ( $resource_type == 'db' ) {
207        $file_handler =& xoops_gethandler('tplfile');
208        $tpl =& $file_handler->find('default', null, null, null, $resource_name, true);
209        if (count($tpl) > 0 && is_object($tpl[0])) {
210            $template_source = $tpl[0]->getSource();
211            $template_timestamp = $tpl[0]->getLastModified();
212            return true;
213        }
214    } else {
215    }
216    return false;
217}
218
219/**
220 * function to update compiled template file in templates_c folder
221 *
222 * @param   string  $tpl_id
223 * @param   boolean $clear_old
224 * @return  boolean
225 **/
226function xoops_template_touch($tpl_id, $clear_old = true)
227{
228    $tpl = new XoopsTpl();
229    $tpl->force_compile = true;
230    $tplfile_handler =& xoops_gethandler('tplfile');
231    $tplfile =& $tplfile_handler->get($tpl_id);
232    if ( is_object($tplfile) ) {
233        $file = $tplfile->getVar('tpl_file');
234        if ($clear_old) {
235            $tpl->clear_cache('db:'.$file);
236            $tpl->clear_compiled_tpl('db:'.$file);
237        }
238        $tpl->fetch('db:'.$file);
239        return true;
240    }
241    return false;
242}
243
244/**
245 * Clear the module cache
246 *
247 * @param   int $mid    Module ID
248 * @return
249 **/
250function xoops_template_clear_module_cache($mid)
251{
252    $block_arr =& XoopsBlock::getByModule($mid);
253    $count = count($block_arr);
254    if ($count > 0) {
255        $xoopsTpl = new XoopsTpl();
256        $xoopsTpl->xoops_setCaching(2);
257        for ($i = 0; $i < $count; $i++) {
258            if ($block_arr[$i]->getVar('template') != '') {
259                $xoopsTpl->clear_cache('db:'.$block_arr[$i]->getVar('template'), 'blk_'.$block_arr[$i]->getVar('bid'));
260            }
261        }
262    }
263}
264?>
Note: See TracBrowser for help on using the repository browser.