Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_cacheresource_file.php
1 <?php
2
3 /**
4  * Smarty Internal Plugin CacheResource File
5  * 
6  * Implements the file system as resource for the HTML cache
7  * Version ussing nocache inserts
8  * 
9  * @package Smarty
10  * @subpackage Cacher
11  * @author Uwe Tews 
12  */
13
14 /**
15  * This class does contain all necessary methods for the HTML cache on file system
16  */
17 class Smarty_Internal_CacheResource_File {
18     function __construct($smarty)
19     {
20         $this->smarty = $smarty;
21     } 
22     /**
23      * Returns the filepath of the cached template output
24      * 
25      * @param object $_template current template
26      * @return string the cache filepath
27      */
28     public function getCachedFilepath($_template)
29     {
30         $_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
31         $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
32         $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
33         $_filepath = $_template->templateUid; 
34         // if use_sub_dirs, break file into directories
35         if ($this->smarty->use_sub_dirs) {
36             $_filepath = substr($_filepath, 0, 2) . DS
37              . substr($_filepath, 2, 2) . DS
38              . substr($_filepath, 4, 2) . DS
39              . $_filepath;
40         } 
41         $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
42         if (isset($_cache_id)) {
43             $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
44         } else {
45             $_cache_id = '';
46         } 
47         if (isset($_compile_id)) {
48             $_compile_id = $_compile_id . $_compile_dir_sep;
49         } else {
50             $_compile_id = '';
51         } 
52         $_cache_dir = $this->smarty->cache_dir;
53         if (strpos('/\\', substr($_cache_dir, -1)) === false) {
54             $_cache_dir .= DS;
55         } 
56         return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
57     } 
58
59     /**
60      * Returns the timpestamp of the cached template output
61      * 
62      * @param object $_template current template
63      * @return integer |booelan the template timestamp or false if the file does not exist
64      */
65     public function getCachedTimestamp($_template)
66     { 
67         // return @filemtime ($_template->getCachedFilepath());
68         return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
69     } 
70
71     /**
72      * Returns the cached template output
73      * 
74      * @param object $_template current template
75      * @return string |booelan the template content or false if the file does not exist
76      */
77     public function getCachedContents($_template, $no_render = false)
78     {
79         if (!$no_render) {
80                 ob_start();
81         }
82         $_smarty_tpl = $_template;
83         include $_template->getCachedFilepath();
84         if ($no_render) {
85                 return null;
86         } else {
87           return ob_get_clean();
88         }
89     } 
90
91     /**
92      * Writes the rendered template output to cache file
93      * 
94      * @param object $_template current template
95      * @return boolean status
96      */
97     public function writeCachedContent($_template, $content)
98     {
99         if (!$_template->resource_object->isEvaluated) {
100             if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
101                 $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
102                 return true;
103             } 
104         } 
105         return false;
106     } 
107
108     /**
109      * Empty cache folder
110      * 
111      * @param integer $exp_time expiration time
112      * @return integer number of cache files deleted
113      */
114     public function clearAll($exp_time = null)
115     {
116         return $this->clear(null, null, null, $exp_time);
117     } 
118     /**
119      * Empty cache for a specific template
120      * 
121      * @param string $resource_name template name
122      * @param string $cache_id cache id
123      * @param string $compile_id compile id
124      * @param integer $exp_time expiration time
125      * @return integer number of cache files deleted
126      */
127     public function clear($resource_name, $cache_id, $compile_id, $exp_time)
128     {
129         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
130         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
131         $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
132         $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
133         $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
134         $_dir_length = strlen($_dir);
135         if (isset($_cache_id)) {
136             $_cache_id_parts = explode('|', $_cache_id);
137             $_cache_id_parts_count = count($_cache_id_parts);
138             if ($this->smarty->use_sub_dirs) {
139                 foreach ($_cache_id_parts as $id_part) {
140                     $_dir .= $id_part . DS;
141                 } 
142             } 
143         } 
144         if (isset($resource_name)) {
145             $_save_stat = $this->smarty->caching;
146             $this->smarty->caching = true;
147             $tpl = new $this->smarty->template_class($resource_name, $this->smarty); 
148             // remove from template cache
149             unset($this->smarty->template_objects[crc32($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
150             $this->smarty->caching = $_save_stat;
151             if ($tpl->isExisting()) {
152                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->getCachedFilepath()));
153             } else {
154                 return 0;
155             } 
156         } 
157         $_count = 0;
158         if (file_exists($_dir)) {
159             $_cacheDirs = new RecursiveDirectoryIterator($_dir);
160             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
161             foreach ($_cache as $_file) {
162                 if (strpos($_file, '.svn') !== false) continue; 
163                 // directory ?
164                 if ($_file->isDir()) {
165                     if (!$_cache->isDot()) {
166                         // delete folder if empty
167                         @rmdir($_file->getPathname());
168                     } 
169                 } else {
170                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
171                     $_parts_count = count($_parts); 
172                     // check name
173                     if (isset($resource_name)) {
174                         if ($_parts[$_parts_count-1] != $_resourcename_parts) {
175                             continue;
176                         } 
177                     } 
178                     // check compile id
179                     if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
180                         continue;
181                     } 
182                     // check cache id
183                     if (isset($_cache_id)) {
184                         // count of cache id parts
185                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
186                         if ($_parts_count < $_cache_id_parts_count) {
187                             continue;
188                         } 
189                         for ($i = 0; $i < $_cache_id_parts_count; $i++) {
190                             if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
191                         } 
192                     } 
193                     // expired ?
194                     if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
195                         continue;
196                     } 
197                     $_count += @unlink((string) $_file) ? 1 : 0;
198                 } 
199             } 
200         } 
201         return $_count;
202     } 
203
204
205 ?>