Code

Updated smarty
[gosa.git] / gosa-core / include / smarty / sysplugins / smarty_internal_utility.php
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   
37     protected $smarty;
39     function __construct($smarty) {
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         function _get_time()
55         {
56             $_mtime = microtime();
57             $_mtime = explode(" ", $_mtime);
58             return (double)($_mtime[1]) + (double)($_mtime[0]);
59         } 
60         // set default directory
61         if ($dir_name === null) {
62             $dir_name = $this->smarty->template_dir;
63         } 
64         // switch off time limit
65         if (function_exists('set_time_limit')) {
66             @set_time_limit($time_limit);
67         } 
68         $this->smarty->force_compile = $force_compile;
69         $_count = 0;
70         $_error_count = 0; 
71         // loop over array of template directories
72         foreach((array)$this->smarty->template_dir as $_dir) {
73             $_compileDirs = new RecursiveDirectoryIterator($_dir);
74             $_compile = new RecursiveIteratorIterator($_compileDirs);
75             foreach ($_compile as $_fileinfo) {
76                 if (strpos($_fileinfo, '.svn') !== false) continue;
77                 $_file = $_fileinfo->getFilename();
78                 if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
79                 if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
80                     $_template_file = $_file;
81                 } else {
82                     $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . '\\' . $_file;
83                 } 
84                 echo '<br>', $_dir, '---', $_template_file;
85                 flush();
86                 $_start_time = _get_time();
87                 try {
88                     $_tpl = $this->smarty->createTemplate($_template_file);
89                     $_tpl->getCompiledTemplate();
90                 } 
91                 catch (Exception $e) {
92                     echo 'Error: ', $e->getMessage(), "<br><br>";
93                     $_error_count++;
94                 } 
95                 echo ' done in  ', _get_time() - $_start_time, ' seconds';
96                 if ($max_errors !== null && $_error_count == $max_errors) {
97                     echo '<br><br>too many errors';
98                     exit();
99                 } 
100             } 
101         } 
102         return $_count;
103     } 
105     /**
106     * Delete compiled template file
107     * 
108     * @param string $resource_name template name
109     * @param string $compile_id compile id
110     * @param integer $exp_time expiration time
111     * @return integer number of template files deleted
112     */
113     function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
114     {
115         $_compile_id =  isset($compile_id) ? preg_replace('![^\w\|]+!','_',$compile_id) : null;
116         $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
117         if (isset($resource_name)) {
118             $_resource_part_1 = $resource_name . '.php';
119             $_resource_part_2 = $resource_name . '.cache' . '.php';
120         } else {
121             $_resource_part = '';
122         } 
123         $_dir = $this->smarty->compile_dir;
124         if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
125             $_dir .= $_compile_id . $_dir_sep;
126         } 
127         if (isset($_compile_id)) {
128             $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
129         } 
130         $_count = 0;
131         $_compileDirs = new RecursiveDirectoryIterator($_dir);
132         $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
133         foreach ($_compile as $_file) {
134             if (strpos($_file, '.svn') !== false) continue;
135             if ($_file->isDir()) {
136                 if (!$_compile->isDot()) {
137                     // delete folder if empty
138                     @rmdir($_file->getPathname());
139                 } 
140             } else {
141                 if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
142                         (!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) ||
143                             (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
144                     if (isset($exp_time)) {
145                         if (time() - @filemtime($_file) >= $exp_time) {
146                             $_count += @unlink((string) $_file) ? 1 : 0;
147                         } 
148                     } else {
149                         $_count += @unlink((string) $_file) ? 1 : 0;
150                     } 
151                 } 
152             } 
153         } 
154         return $_count;
155     }
156     
157     function testInstall()
158     {
159         echo "<PRE>\n";
160     
161         echo "Smarty Installation test...\n";
162     
163         echo "Testing template directory...\n";
164     
165         foreach((array)$this->smarty->template_dir as $template_dir) {
166             if (!is_dir($template_dir))
167                 echo "FAILED: $template_dir is not a directory.\n";
168             elseif (!is_readable($template_dir))
169                 echo "FAILED: $template_dir is not readable.\n";
170             else
171                 echo "$template_dir is OK.\n";
172         } 
173     
174         echo "Testing compile directory...\n";
175     
176         if (!is_dir($this->smarty->compile_dir))
177             echo "FAILED: $this->smarty->compile_dir is not a directory.\n";
178         elseif (!is_readable($this->smarty->compile_dir))
179             echo "FAILED: $this->smarty->compile_dir is not readable.\n";
180         elseif (!is_writable($this->smarty->compile_dir))
181             echo "FAILED: $this->smarty->compile_dir is not writable.\n";
182         else
183             echo "{$this->smarty->compile_dir} is OK.\n";
184     
185         echo "Testing plugins directory...\n";
186     
187         foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
188             if (!is_dir($plugin_dir))
189                 echo "FAILED: $plugin_dir is not a directory.\n";
190             elseif (!is_readable($plugin_dir))
191                 echo "FAILED: $plugin_dir is not readable.\n";
192             else
193                 echo "$plugin_dir is OK.\n";
194         } 
195     
196         echo "Testing cache directory...\n";
197     
198         if (!is_dir($this->smarty->cache_dir))
199             echo "FAILED: $this->smarty->cache_dir is not a directory.\n";
200         elseif (!is_readable($this->smarty->cache_dir))
201             echo "FAILED: $this->smarty->cache_dir is not readable.\n";
202         elseif (!is_writable($this->smarty->cache_dir))
203             echo "FAILED: $this->smarty->cache_dir is not writable.\n";
204         else
205             echo "{$this->smarty->cache_dir} is OK.\n";
206     
207         echo "Testing configs directory...\n";
208     
209         if (!is_dir($this->smarty->config_dir))
210             echo "FAILED: $this->smarty->config_dir is not a directory.\n";
211         elseif (!is_readable($this->smarty->config_dir))
212             echo "FAILED: $this->smarty->config_dir is not readable.\n";
213         else
214             echo "{$this->smarty->config_dir} is OK.\n";
215     
216         echo "Tests complete.\n";
217     
218         echo "</PRE>\n";
219     
220         return true;
221     } 
222