1 <?php
3 /**
4 * Project: Smarty: the PHP compiling template engine
5 * File: smarty_internal_utility.php
6 * SVN: $Id: $
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * For questions, help, comments, discussion, etc., please join the
23 * Smarty mailing list. Send a blank e-mail to
24 * smarty-discussion-subscribe@googlegroups.com
25 *
26 * @link http://www.smarty.net/
27 * @copyright 2008 New Digital Group, Inc.
28 * @author Monte Ohrt <monte at ohrt dot com>
29 * @author Uwe Tews
30 * @package Smarty
31 * @subpackage PluginsInternal
32 * @version 3-SVN$Rev: 3286 $
33 */
35 class Smarty_Internal_Utility {
36 protected $smarty;
38 function __construct($smarty)
39 {
40 $this->smarty = $smarty;
41 }
43 /**
44 * Compile all template files
45 *
46 * @param string $extension file extension
47 * @param bool $force_compile force all to recompile
48 * @param int $time_limit
49 * @param int $max_errors
50 * @return integer number of template files recompiled
51 */
52 function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
53 {
54 // switch off time limit
55 if (function_exists('set_time_limit')) {
56 @set_time_limit($time_limit);
57 }
58 $this->smarty->force_compile = $force_compile;
59 $_count = 0;
60 $_error_count = 0;
61 // loop over array of template directories
62 foreach((array)$this->smarty->template_dir as $_dir) {
63 $_compileDirs = new RecursiveDirectoryIterator($_dir);
64 $_compile = new RecursiveIteratorIterator($_compileDirs);
65 foreach ($_compile as $_fileinfo) {
66 if (strpos($_fileinfo, '.svn') !== false) continue;
67 $_file = $_fileinfo->getFilename();
68 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
69 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
70 $_template_file = $_file;
71 } else {
72 $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
73 }
74 echo '<br>', $_dir, '---', $_template_file;
75 flush();
76 $_start_time = $this->_get_time();
77 try {
78 $_tpl = $this->smarty->createTemplate($_template_file);
79 if ($_tpl->mustCompile()) {
80 $_tpl->compileTemplateSource();
81 echo ' compiled in ', $this->_get_time() - $_start_time, ' seconds';
82 flush();
83 } else {
84 echo ' is up to date';
85 flush();
86 }
87 }
88 catch (Exception $e) {
89 echo 'Error: ', $e->getMessage(), "<br><br>";
90 $_error_count++;
91 }
92 if ($max_errors !== null && $_error_count == $max_errors) {
93 echo '<br><br>too many errors';
94 exit();
95 }
96 }
97 }
98 return $_count;
99 }
101 /**
102 * Compile all config files
103 *
104 * @param string $extension file extension
105 * @param bool $force_compile force all to recompile
106 * @param int $time_limit
107 * @param int $max_errors
108 * @return integer number of template files recompiled
109 */
110 function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
111 {
112 // switch off time limit
113 if (function_exists('set_time_limit')) {
114 @set_time_limit($time_limit);
115 }
116 $this->smarty->force_compile = $force_compile;
117 $_count = 0;
118 $_error_count = 0;
119 // loop over array of template directories
120 foreach((array)$this->smarty->config_dir as $_dir) {
121 $_compileDirs = new RecursiveDirectoryIterator($_dir);
122 $_compile = new RecursiveIteratorIterator($_compileDirs);
123 foreach ($_compile as $_fileinfo) {
124 if (strpos($_fileinfo, '.svn') !== false) continue;
125 $_file = $_fileinfo->getFilename();
126 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
127 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
128 $_config_file = $_file;
129 } else {
130 $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
131 }
132 echo '<br>', $_dir, '---', $_config_file;
133 flush();
134 $_start_time = $this->_get_time();
135 try {
136 $_config = new Smarty_Internal_Config($_config_file, $this->smarty);
137 if ($_config->mustCompile()) {
138 $_config->compileConfigSource();
139 echo ' compiled in ', $this->_get_time() - $_start_time, ' seconds';
140 flush();
141 } else {
142 echo ' is up to date';
143 flush();
144 }
145 }
146 catch (Exception $e) {
147 echo 'Error: ', $e->getMessage(), "<br><br>";
148 $_error_count++;
149 }
150 if ($max_errors !== null && $_error_count == $max_errors) {
151 echo '<br><br>too many errors';
152 exit();
153 }
154 }
155 }
156 return $_count;
157 }
159 /**
160 * Delete compiled template file
161 *
162 * @param string $resource_name template name
163 * @param string $compile_id compile id
164 * @param integer $exp_time expiration time
165 * @return integer number of template files deleted
166 */
167 function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
168 {
169 $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
170 $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
171 if (isset($resource_name)) {
172 $_resource_part_1 = $resource_name . '.php';
173 $_resource_part_2 = $resource_name . '.cache' . '.php';
174 } else {
175 $_resource_part = '';
176 }
177 $_dir = $this->smarty->compile_dir;
178 if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
179 $_dir .= $_compile_id . $_dir_sep;
180 }
181 if (isset($_compile_id)) {
182 $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
183 }
184 $_count = 0;
185 $_compileDirs = new RecursiveDirectoryIterator($_dir);
186 $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
187 foreach ($_compile as $_file) {
188 if (strpos($_file, '.svn') !== false) continue;
189 if ($_file->isDir()) {
190 if (!$_compile->isDot()) {
191 // delete folder if empty
192 @rmdir($_file->getPathname());
193 }
194 } else {
195 if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
196 (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
197 (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
198 if (isset($exp_time)) {
199 if (time() - @filemtime($_file) >= $exp_time) {
200 $_count += @unlink((string) $_file) ? 1 : 0;
201 }
202 } else {
203 $_count += @unlink((string) $_file) ? 1 : 0;
204 }
205 }
206 }
207 }
208 return $_count;
209 }
211 function testInstall()
212 {
213 echo "<PRE>\n";
215 echo "Smarty Installation test...\n";
217 echo "Testing template directory...\n";
219 foreach((array)$this->smarty->template_dir as $template_dir) {
220 if (!is_dir($template_dir))
221 echo "FAILED: $template_dir is not a directory.\n";
222 elseif (!is_readable($template_dir))
223 echo "FAILED: $template_dir is not readable.\n";
224 else
225 echo "$template_dir is OK.\n";
226 }
228 echo "Testing compile directory...\n";
230 if (!is_dir($this->smarty->compile_dir))
231 echo "FAILED: {$this->smarty->compile_dir} is not a directory.\n";
232 elseif (!is_readable($this->smarty->compile_dir))
233 echo "FAILED: {$this->smarty->compile_dir} is not readable.\n";
234 elseif (!is_writable($this->smarty->compile_dir))
235 echo "FAILED: {$this->smarty->compile_dir} is not writable.\n";
236 else
237 echo "{$this->smarty->compile_dir} is OK.\n";
239 echo "Testing plugins directory...\n";
241 foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
242 if (!is_dir($plugin_dir))
243 echo "FAILED: $plugin_dir is not a directory.\n";
244 elseif (!is_readable($plugin_dir))
245 echo "FAILED: $plugin_dir is not readable.\n";
246 else
247 echo "$plugin_dir is OK.\n";
248 }
250 echo "Testing cache directory...\n";
252 if (!is_dir($this->smarty->cache_dir))
253 echo "FAILED: {$this->smarty->cache_dir} is not a directory.\n";
254 elseif (!is_readable($this->smarty->cache_dir))
255 echo "FAILED: {$this->smarty->cache_dir} is not readable.\n";
256 elseif (!is_writable($this->smarty->cache_dir))
257 echo "FAILED: {$this->smarty->cache_dir} is not writable.\n";
258 else
259 echo "{$this->smarty->cache_dir} is OK.\n";
261 echo "Testing configs directory...\n";
263 if (!is_dir($this->smarty->config_dir))
264 echo "FAILED: {$this->smarty->config_dir} is not a directory.\n";
265 elseif (!is_readable($this->smarty->config_dir))
266 echo "FAILED: {$this->smarty->config_dir} is not readable.\n";
267 else
268 echo "{$this->smarty->config_dir} is OK.\n";
270 echo "Tests complete.\n";
272 echo "</PRE>\n";
274 return true;
275 }
276 /**
277 * Get Micro Time
278 *
279 * @return double micro time
280 */
281 function _get_time()
282 {
283 $_mtime = microtime();
284 $_mtime = explode(" ", $_mtime);
285 return (double)($_mtime[1]) + (double)($_mtime[0]);
286 }
287 }