In mail.php, we don't want to double encode fields so in send() create local variable...
[isso.git] / template_fs.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * File System Template System
24 * template_fs.php
25 *
26 * @package ISSO
27 */
28
29 $this->load('template', null);
30
31 /**
32 * File System Template System
33 *
34 * This framework merely replaces the template loading functions with
35 * file-system based ones. It has an optional caching system in which
36 * template data will remain stored in the database as long as the filesystem
37 * file is modified. To do this, pass a table name to setDatabaseCache() and make sure
38 * there's a DB module that has access to a table with this schema:
39 *
40 * CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
41 *
42 * @author Blue Static
43 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * @version $Revision$
45 * @package ISSO
46 *
47 */
48 class Template_FS extends Template
49 {
50 /**
51 * The path, from the path of the application, where templates are stored
52 * @var string
53 * @access private
54 */
55 var $templatedir = '';
56
57 /**
58 * The extension all the template files have
59 * @var string
60 * @access private
61 */
62 var $extension = 'tpl';
63
64 /**
65 * The database table name for the template cache
66 * @var string
67 * @access private
68 */
69 var $dbCacheTable = null;
70
71 // ###################################################################
72 /**
73 * Constructor
74 */
75 function __construct(&$registry)
76 {
77 $this->registry =& $registry;
78 }
79
80 // ###################################################################
81 /**
82 * (PHP 4) Constructor
83 */
84 function Template_FS(&$registry)
85 {
86 $this->__construct($registry);
87 }
88
89 // ###################################################################
90 /**
91 * Sets the template directory
92 *
93 * @access public
94 *
95 * @param string The template directory
96 */
97 function setTemplateDir($path)
98 {
99 $this->templatedir = $this->registry->fetch_sourcepath($path);
100 }
101
102 // ###################################################################
103 /**
104 * Sets the file extension
105 *
106 * @access public
107 *
108 * @param string File extension
109 */
110 function setExtension($ext)
111 {
112 $this->extension = $ext;
113 }
114
115 // ###################################################################
116 /**
117 * Sets the name of the table to access for the datbase cache
118 *
119 * @param string DB table name
120 */
121 function setDatabaseCache($table)
122 {
123 $this->dbCacheTable = $table;
124 }
125
126 // ###################################################################
127 /**
128 * Takes an array of template names, loads them, and then stores a
129 * parsed version for optimum speed.
130 *
131 * @access public
132 *
133 * @param array List of template names to be cached
134 */
135 function cache($namearray)
136 {
137 if (sizeof($this->cache) > 0)
138 {
139 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING);
140 }
141 else
142 {
143 $dbCache = array();
144 if ($this->dbCacheTable)
145 {
146 $db =& $this->registry->modules[ISSO_DB_LAYER];
147 $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')");
148 while ($tpl = $db->fetch_array($cache))
149 {
150 $time = filemtime($this->registry->apppath . $this->templatedir . $tpl['filename'] . '.' . $this->extension);
151 $template = $tpl['template'];
152 if ($time > $tpl['timestamp'])
153 {
154 $template = $this->_parse($this->_load($tpl['filename']));
155 $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escape_string($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'");
156 $tpl['template'] = $template;
157 }
158 $dbCache["$tpl[filename]"] = $template;
159 }
160 }
161 foreach ($namearray AS $name)
162 {
163 if (isset($dbCache["$name"]))
164 {
165 $template = $dbCache["$name"];
166 }
167 else
168 {
169 $template = $this->_parse($this->_load($name));
170 if ($this->dbCacheTable)
171 {
172 $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escape_string($template) . "', " . TIMENOW . ")");
173 }
174 }
175 $this->cache["$name"] = $template;
176 $this->usage["$name"] = 0;
177 }
178 }
179 }
180
181 // ###################################################################
182 /**
183 * Loads a template from the file system from the specified
184 * $templatedir with the file extension $extension
185 *
186 * @access private
187 *
188 * @param string The name of the template call
189 */
190 function _load($name)
191 {
192 $path = $this->registry->apppath . $this->templatedir . $name . '.' . $this->extension;
193 if (is_file($path))
194 {
195 if (($template = @file_get_contents($path)) !== false)
196 {
197 return $template;
198 }
199 else
200 {
201 trigger_error("Could not load the template '$path'", E_USER_ERROR);
202 exit;
203 }
204 }
205 else
206 {
207 trigger_error("Could not load the template '$path'", E_USER_ERROR);
208 exit;
209 }
210 }
211 }
212
213 /*=====================================================================*\
214 || ###################################################################
215 || # $HeadURL$
216 || # $Id$
217 || ###################################################################
218 \*=====================================================================*/
219 ?>