]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_cacheresource_file.php
Merge pull request #1124 from tobiasd/duepuntoderivs
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_cacheresource_file.php
1 <?php
2 /**
3  * Smarty Internal Plugin CacheResource File
4  *
5  * @package    Smarty
6  * @subpackage Cacher
7  * @author     Uwe Tews
8  * @author     Rodney Rehm
9  */
10
11 /**
12  * This class does contain all necessary methods for the HTML cache on file system
13  * Implements the file system as resource for the HTML cache Version ussing nocache inserts.
14  *
15  * @package    Smarty
16  * @subpackage Cacher
17  */
18 class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
19 {
20     /**
21      * populate Cached Object with meta data from Resource
22      *
23      * @param Smarty_Template_Cached   $cached    cached object
24      * @param Smarty_Internal_Template $_template template object
25      *
26      * @return void
27      */
28     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
29     {
30         $_source_file_path = str_replace(':', '.', $_template->source->filepath);
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->source->uid;
34         // if use_sub_dirs, break file into directories
35         if ($_template->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 = $_template->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 = $_template->smarty->getCacheDir();
53         if ($_template->smarty->cache_locking) {
54             // create locking file name
55             // relative file name?
56             if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {
57                 $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;
58             } else {
59                 $_lock_dir = $_cache_dir;
60             }
61             $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock';
62         }
63         $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
64         $cached->timestamp = @filemtime($cached->filepath);
65         $cached->exists = !!$cached->timestamp;
66     }
67
68     /**
69      * populate Cached Object with timestamp and exists from Resource
70      *
71      * @param Smarty_Template_Cached $cached cached object
72      *
73      * @return void
74      */
75     public function populateTimestamp(Smarty_Template_Cached $cached)
76     {
77         $cached->timestamp = @filemtime($cached->filepath);
78         $cached->exists = !!$cached->timestamp;
79     }
80
81     /**
82      * Read the cached template and process its header
83      *
84      * @param Smarty_Internal_Template $_template template object
85      * @param Smarty_Template_Cached   $cached    cached object
86      *
87      * @return booleantrue or false if the cached content does not exist
88      */
89     public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
90     {
91         /** @var Smarty_Internal_Template $_smarty_tpl
92          * used in included file
93          */
94         $_smarty_tpl = $_template;
95
96         return @include $_template->cached->filepath;
97     }
98
99     /**
100      * Write the rendered template output to cache
101      *
102      * @param Smarty_Internal_Template $_template template object
103      * @param string                   $content   content to cache
104      *
105      * @return boolean success
106      */
107     public function writeCachedContent(Smarty_Internal_Template $_template, $content)
108     {
109         if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
110             $_template->cached->timestamp = @filemtime($_template->cached->filepath);
111             $_template->cached->exists = !!$_template->cached->timestamp;
112             if ($_template->cached->exists) {
113                 return true;
114             }
115         }
116
117         return false;
118     }
119
120     /**
121      * Empty cache
122      *
123      * @param Smarty  $smarty
124      * @param integer $exp_time expiration time (number of seconds, not timestamp)
125      *
126      * @return integer number of cache files deleted
127      */
128     public function clearAll(Smarty $smarty, $exp_time = null)
129     {
130         return $this->clear($smarty, null, null, null, $exp_time);
131     }
132
133     /**
134      * Empty cache for a specific template
135      *
136      * @param Smarty  $smarty
137      * @param string  $resource_name template name
138      * @param string  $cache_id      cache id
139      * @param string  $compile_id    compile id
140      * @param integer $exp_time      expiration time (number of seconds, not timestamp)
141      *
142      * @return integer number of cache files deleted
143      */
144     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
145     {
146         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
147         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
148         $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
149         $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
150         $_dir = realpath($smarty->getCacheDir()) . '/';
151         $_dir_length = strlen($_dir);
152         if (isset($_cache_id)) {
153             $_cache_id_parts = explode('|', $_cache_id);
154             $_cache_id_parts_count = count($_cache_id_parts);
155             if ($smarty->use_sub_dirs) {
156                 foreach ($_cache_id_parts as $id_part) {
157                     $_dir .= $id_part . DS;
158                 }
159             }
160         }
161         if (isset($resource_name)) {
162             $_save_stat = $smarty->caching;
163             $smarty->caching = true;
164             $tpl = new $smarty->template_class($resource_name, $smarty);
165             $smarty->caching = $_save_stat;
166
167             // remove from template cache
168             $tpl->source; // have the template registered before unset()
169             if ($smarty->allow_ambiguous_resources) {
170                 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
171             } else {
172                 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
173             }
174             if (isset($_templateId[150])) {
175                 $_templateId = sha1($_templateId);
176             }
177             unset($smarty->template_objects[$_templateId]);
178
179             if ($tpl->source->exists) {
180                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
181             } else {
182                 return 0;
183             }
184         }
185         $_count = 0;
186         $_time = time();
187         if (file_exists($_dir)) {
188             $_cacheDirs = new RecursiveDirectoryIterator($_dir);
189             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
190             foreach ($_cache as $_file) {
191                 if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
192                     continue;
193                 }
194                 // directory ?
195                 if ($_file->isDir()) {
196                     if (!$_cache->isDot()) {
197                         // delete folder if empty
198                         @rmdir($_file->getPathname());
199                     }
200                 } else {
201                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length)));
202                     $_parts_count = count($_parts);
203                     // check name
204                     if (isset($resource_name)) {
205                         if ($_parts[$_parts_count - 1] != $_resourcename_parts) {
206                             continue;
207                         }
208                     }
209                     // check compile id
210                     if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) {
211                         continue;
212                     }
213                     // check cache id
214                     if (isset($_cache_id)) {
215                         // count of cache id parts
216                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
217                         if ($_parts_count < $_cache_id_parts_count) {
218                             continue;
219                         }
220                         for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
221                             if ($_parts[$i] != $_cache_id_parts[$i]) {
222                                 continue 2;
223                             }
224                         }
225                     }
226                     // expired ?
227                     if (isset($exp_time)) {
228                         if ($exp_time < 0) {
229                             preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
230                             if ($_time < (@filemtime($_file) + $match[1])) {
231                                 continue;
232                             }
233                         } else {
234                             if ($_time - @filemtime($_file) < $exp_time) {
235                                 continue;
236                             }
237                         }
238                     }
239                     $_count += @unlink((string) $_file) ? 1 : 0;
240                 }
241             }
242         }
243
244         return $_count;
245     }
246
247     /**
248      * Check is cache is locked for this template
249      *
250      * @param Smarty                 $smarty Smarty object
251      * @param Smarty_Template_Cached $cached cached object
252      *
253      * @return boolean true or false if cache is locked
254      */
255     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
256     {
257         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
258             clearstatcache(true, $cached->lock_id);
259         } else {
260             clearstatcache();
261         }
262         $t = @filemtime($cached->lock_id);
263
264         return $t && (time() - $t < $smarty->locking_timeout);
265     }
266
267     /**
268      * Lock cache for this template
269      *
270      * @param Smarty                 $smarty Smarty object
271      * @param Smarty_Template_Cached $cached cached object
272      *
273      * @return bool|void
274      */
275     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
276     {
277         $cached->is_locked = true;
278         touch($cached->lock_id);
279     }
280
281     /**
282      * Unlock cache for this template
283      *
284      * @param Smarty                 $smarty Smarty object
285      * @param Smarty_Template_Cached $cached cached object
286      *
287      * @return bool|void
288      */
289     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
290     {
291         $cached->is_locked = false;
292         @unlink($cached->lock_id);
293     }
294 }