Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_utility.php
1 <?php
2
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  */
34
35 class Smarty_Internal_Utility {
36     protected $smarty;
37
38     function __construct($smarty)
39     {
40         $this->smarty = $smarty;
41     } 
42
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             if (strpos('/\\', substr($_dir, -1)) === false) {
64                 $_dir .= DS;
65             } 
66             $_compileDirs = new RecursiveDirectoryIterator($_dir);
67             $_compile = new RecursiveIteratorIterator($_compileDirs);
68             foreach ($_compile as $_fileinfo) {
69                 if (strpos($_fileinfo, '.svn') !== false) continue;
70                 $_file = $_fileinfo->getFilename();
71                 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
72                 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
73                    $_template_file = $_file;
74                 } else {
75                    $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
76                 }
77                 echo '<br>', $_dir, '---', $_template_file;
78                 flush();
79                 $_start_time = microtime(true);
80                 try {
81                     $_tpl = $this->smarty->createTemplate($_template_file,null,null,null,false);
82                     if ($_tpl->mustCompile()) {
83                         $_tpl->compileTemplateSource();
84                         echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
85                         flush();
86                     } else {
87                         echo ' is up to date';
88                         flush();
89                     }
90                 }
91                 catch (Exception $e) {
92                     echo 'Error: ', $e->getMessage(), "<br><br>";
93                     $_error_count++;
94                 } 
95                                 // free memory
96                 $this->smarty->template_objects = array();
97                 $_tpl->smarty->template_objects = array();
98                 $_tpl = null;
99                 if ($max_errors !== null && $_error_count == $max_errors) {
100                     echo '<br><br>too many errors';
101                     exit();
102                 } 
103             } 
104         } 
105         return $_count;
106     } 
107
108     /**
109      * Compile all config files
110      * 
111      * @param string $extension file extension
112      * @param bool $force_compile force all to recompile
113      * @param int $time_limit 
114      * @param int $max_errors 
115      * @return integer number of template files recompiled
116      */
117     function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
118     { 
119         // switch off time limit
120         if (function_exists('set_time_limit')) {
121             @set_time_limit($time_limit);
122         } 
123         $this->smarty->force_compile = $force_compile;
124         $_count = 0;
125         $_error_count = 0; 
126         // loop over array of template directories
127         foreach((array)$this->smarty->config_dir as $_dir) {
128             if (strpos('/\\', substr($_dir, -1)) === false) {
129                 $_dir .= DS;
130             } 
131             $_compileDirs = new RecursiveDirectoryIterator($_dir);
132             $_compile = new RecursiveIteratorIterator($_compileDirs);
133             foreach ($_compile as $_fileinfo) {
134                 if (strpos($_fileinfo, '.svn') !== false) continue;
135                 $_file = $_fileinfo->getFilename();
136                 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
137                 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
138                     $_config_file = $_file;
139                 } else {
140                     $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
141                 } 
142                 echo '<br>', $_dir, '---', $_config_file;
143                 flush();
144                 $_start_time = microtime(true);
145                 try {
146                     $_config = new Smarty_Internal_Config($_config_file, $this->smarty);
147                     if ($_config->mustCompile()) {
148                         $_config->compileConfigSource();
149                         echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
150                         flush();
151                     } else {
152                         echo ' is up to date';
153                         flush();
154                     } 
155                 } 
156                 catch (Exception $e) {
157                     echo 'Error: ', $e->getMessage(), "<br><br>";
158                     $_error_count++;
159                 } 
160                 if ($max_errors !== null && $_error_count == $max_errors) {
161                     echo '<br><br>too many errors';
162                     exit();
163                 } 
164             } 
165         } 
166         return $_count;
167     } 
168
169     /**
170      * Delete compiled template file
171      * 
172      * @param string $resource_name template name
173      * @param string $compile_id compile id
174      * @param integer $exp_time expiration time
175      * @return integer number of template files deleted
176      */
177     function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
178     {
179         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
180         $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
181         if (isset($resource_name)) {
182             $_resource_part_1 = $resource_name . '.php';
183             $_resource_part_2 = $resource_name . '.cache' . '.php';
184         } else {
185             $_resource_part = '';
186         } 
187         $_dir = $this->smarty->compile_dir;
188         if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
189             $_dir .= $_compile_id . $_dir_sep;
190         } 
191         if (isset($_compile_id)) {
192             $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
193         } 
194         $_count = 0;
195         $_compileDirs = new RecursiveDirectoryIterator($_dir);
196         $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
197         foreach ($_compile as $_file) {
198             if (strpos($_file, '.svn') !== false) continue;
199             if ($_file->isDir()) {
200                 if (!$_compile->isDot()) {
201                     // delete folder if empty
202                     @rmdir($_file->getPathname());
203                 } 
204             } else {
205                 if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
206                         (!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) ||
207                             (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
208                     if (isset($exp_time)) {
209                         if (time() - @filemtime($_file) >= $exp_time) {
210                             $_count += @unlink((string) $_file) ? 1 : 0;
211                         } 
212                     } else {
213                         $_count += @unlink((string) $_file) ? 1 : 0;
214                     } 
215                 } 
216             } 
217         } 
218         return $_count;
219     } 
220
221     /**
222      * Return array of tag/attributes of all tags used by an template
223      * 
224      * @param object $templae template object
225      * @return array of tag/attributes
226      */
227         function getTags(Smarty_Internal_Template $template) 
228         {
229                 $template->smarty->get_used_tags = true;
230                 $template->compileTemplateSource();
231                 return $template->compiler_object->used_tags;
232         }       
233         
234     function testInstall()
235     {
236         echo "<PRE>\n";
237
238         echo "Smarty Installation test...\n";
239
240         echo "Testing template directory...\n";
241
242         foreach((array)$this->smarty->template_dir as $template_dir) {
243             if (!is_dir($template_dir))
244                 echo "FAILED: $template_dir is not a directory.\n";
245             elseif (!is_readable($template_dir))
246                 echo "FAILED: $template_dir is not readable.\n";
247             else
248                 echo "$template_dir is OK.\n";
249         } 
250
251         echo "Testing compile directory...\n";
252
253         if (!is_dir($this->smarty->compile_dir))
254             echo "FAILED: {$this->smarty->compile_dir} is not a directory.\n";
255         elseif (!is_readable($this->smarty->compile_dir))
256             echo "FAILED: {$this->smarty->compile_dir} is not readable.\n";
257         elseif (!is_writable($this->smarty->compile_dir))
258             echo "FAILED: {$this->smarty->compile_dir} is not writable.\n";
259         else
260             echo "{$this->smarty->compile_dir} is OK.\n";
261
262         echo "Testing plugins directory...\n";
263
264         foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
265             if (!is_dir($plugin_dir))
266                 echo "FAILED: $plugin_dir is not a directory.\n";
267             elseif (!is_readable($plugin_dir))
268                 echo "FAILED: $plugin_dir is not readable.\n";
269             else
270                 echo "$plugin_dir is OK.\n";
271         } 
272
273         echo "Testing cache directory...\n";
274
275         if (!is_dir($this->smarty->cache_dir))
276             echo "FAILED: {$this->smarty->cache_dir} is not a directory.\n";
277         elseif (!is_readable($this->smarty->cache_dir))
278             echo "FAILED: {$this->smarty->cache_dir} is not readable.\n";
279         elseif (!is_writable($this->smarty->cache_dir))
280             echo "FAILED: {$this->smarty->cache_dir} is not writable.\n";
281         else
282             echo "{$this->smarty->cache_dir} is OK.\n";
283
284         echo "Testing configs directory...\n";
285
286         if (!is_dir($this->smarty->config_dir))
287             echo "FAILED: {$this->smarty->config_dir} is not a directory.\n";
288         elseif (!is_readable($this->smarty->config_dir))
289             echo "FAILED: {$this->smarty->config_dir} is not readable.\n";
290         else
291             echo "{$this->smarty->config_dir} is OK.\n";
292
293         echo "Tests complete.\n";
294
295         echo "</PRE>\n";
296
297         return true;
298     } 
299 }
300 ?>