Added feature #14666: Caching framework: Backport PhpFrontend from FLOW3 and refactor...
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_filebackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * A caching backend which stores cache entries in files
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @api
34 * @version $Id$
35 */
36 class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBackend implements t3lib_cache_backend_PhpCapableBackend {
37
38 const SEPARATOR = '^';
39
40 const EXPIRYTIME_FORMAT = 'YmdHis';
41 const EXPIRYTIME_LENGTH = 14;
42
43 const DATASIZE_DIGITS = 10;
44
45 /**
46 * @var string Directory where the files are stored
47 */
48 protected $cacheDirectory = '';
49
50 /**
51 * @var string Absolute path to root, usually document root of website
52 */
53 protected $root = '/';
54
55 /**
56 * Maximum allowed file path length in the current environment.
57 *
58 * @var integer
59 */
60 protected $maximumPathLength = null;
61
62 /**
63 * Constructs this backend
64 *
65 * @param mixed Configuration options - depends on the actual backend
66 */
67 public function __construct(array $options = array()) {
68 parent::__construct($options);
69
70 if (is_null($this->maximumPathLength)) {
71 $this->maximumPathLength = t3lib_div::getMaximumPathLength();
72 }
73 }
74
75 /**
76 * Sets a reference to the cache frontend which uses this backend and
77 * initializes the default cache directory
78 *
79 * @void
80 * @author Robert Lemke <robert@typo3.org>
81 */
82 public function setCache(t3lib_cache_frontend_frontend $cache) {
83 parent::setCache($cache);
84
85 if (empty($this->cacheDirectory)) {
86 $cacheDirectory = 'typo3temp/cache/';
87 try {
88 $this->setCacheDirectory($cacheDirectory);
89 } catch(t3lib_cache_Exception $exception) {
90 }
91 }
92 }
93
94 /**
95 * Sets the directory where the cache files are stored. By default it is
96 * assumed that the directory is below the TYPO3_DOCUMENT_ROOT. However, an
97 * absolute path can be selected, too.
98 *
99 * @param string The directory. If a relative path is given, it's assumed it's in TYPO3_DOCUMENT_ROOT. If an absolute path is given it is taken as is.
100 * @return void
101 * @throws t3lib_cache_Exception if the directory does not exist, is not writable or could not be created.
102 * @author Robert Lemke <robert@typo3.org>
103 * @author Ingo Renner <ingo@typo3.org>
104 */
105 public function setCacheDirectory($cacheDirectory) {
106 $documentRoot = PATH_site;
107
108 if (($open_basedir = ini_get('open_basedir'))) {
109 if (TYPO3_OS === 'WIN') {
110 $delimiter = ';';
111 $cacheDirectory = str_replace('\\', '/', $cacheDirectory);
112 if (!(preg_match('/[A-Z]:/', substr($cacheDirectory,0,2)))) {
113 $cacheDirectory = PATH_site . $cacheDirectory;
114 }
115 } else {
116 $delimiter = ':';
117 if ($cacheDirectory[0] != '/') {
118 // relative path to cache directory.
119 $cacheDirectory = PATH_site . $cacheDirectory;
120 }
121 }
122
123 $basedirs = explode($delimiter, $open_basedir);
124 $cacheDirectoryInBaseDir = FALSE;
125 foreach ($basedirs as $basedir) {
126 if (TYPO3_OS === 'WIN') {
127 $basedir = str_replace('\\', '/', $basedir);
128 }
129 if ($basedir[strlen($basedir) - 1] !== '/') {
130 $basedir .= '/';
131 }
132 if (t3lib_div::isFirstPartOfStr($cacheDirectory, $basedir)) {
133 $documentRoot = $basedir;
134 $cacheDirectory = str_replace($basedir, '', $cacheDirectory);
135 $cacheDirectoryInBaseDir = TRUE;
136 break;
137 }
138 }
139 if (!$cacheDirectoryInBaseDir) {
140 throw new t3lib_cache_Exception(
141 'Open_basedir restriction in effect. The directory "' . $cacheDirectory . '" is not in an allowed path.'
142 );
143 }
144 } else {
145 if ($cacheDirectory[0] == '/') {
146 // Absolute path to cache directory.
147 $documentRoot = '/';
148 }
149 if (TYPO3_OS === 'WIN') {
150 $documentRoot = '';
151 }
152 }
153
154 // After this point all paths have '/' as directory seperator
155 if ($cacheDirectory[strlen($cacheDirectory) - 1] !== '/') {
156 $cacheDirectory .= '/';
157 }
158 $cacheDirectory .= $this->cacheIdentifier;
159 if ($cacheDirectory[strlen($cacheDirectory) - 1] !== '/') {
160 $cacheDirectory .= '/';
161 }
162
163 if (!is_writable($documentRoot . $cacheDirectory)) {
164 t3lib_div::mkdir_deep(
165 $documentRoot,
166 $cacheDirectory
167 );
168 }
169 if (!is_dir($documentRoot . $cacheDirectory)) {
170 throw new t3lib_cache_Exception(
171 'The directory "' . $documentRoot . $cacheDirectory . '" does not exist.',
172 1203965199
173 );
174 }
175 if (!is_writable($documentRoot . $cacheDirectory)) {
176 throw new t3lib_cache_Exception(
177 'The directory "' . $documentRoot . $cacheDirectory . '" is not writable.',
178 1203965200
179 );
180 }
181
182 $this->root = $documentRoot;
183 $this->cacheDirectory = $cacheDirectory;
184 }
185
186 /**
187 * Returns the directory where the cache files are stored
188 *
189 * @return string Full path of the cache directory
190 * @author Robert Lemke <robert@typo3.org>
191 * @api
192 */
193 public function getCacheDirectory() {
194 return $this->root . $this->cacheDirectory;
195 }
196
197 /**
198 * Saves data in a cache file.
199 *
200 * @param string $entryIdentifier An identifier for this specific cache entry
201 * @param string $data The data to be stored
202 * @param array $tags Tags to associate with this cache entry
203 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
204 * @return void
205 * @throws t3lib_cache_Exception if the directory does not exist or is not writable or exceeds the maximum allowed path length, or if no cache frontend has been set.
206 * @throws t3lib_cache_exception_InvalidData if the data to bes stored is not a string.
207 * @author Robert Lemke <robert@typo3.org>
208 * @api
209 */
210 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
211 if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
212 throw new t3lib_cache_Exception(
213 'No cache frontend has been set yet via setCache().',
214 1204111375
215 );
216 }
217
218 if (!is_string($data)) {
219 throw new t3lib_cache_Exception_InvalidData(
220 'The specified data is of type "' . gettype($data) . '" but a string is expected.',
221 1204481674
222 );
223 }
224
225 $this->remove($entryIdentifier);
226
227 $temporaryCacheEntryPathAndFilename = $this->root . $this->cacheDirectory . uniqid() . '.temp';
228 if (strlen($temporaryCacheEntryPathAndFilename) > $this->maximumPathLength) {
229 throw new t3lib_cache_Exception(
230 'The length of the temporary cache file path "' . $temporaryCacheEntryPathAndFilename .
231 '" is ' . strlen($temporaryCacheEntryPathAndFilename) . ' characters long and exceeds the maximum path length of ' .
232 $this->maximumPathLength . '. Please consider setting the temporaryDirectoryBase option to a shorter path. ',
233 1248710426
234 );
235 }
236
237 $expiryTime = ($lifetime === NULL) ? 0 : ($GLOBALS['EXEC_TIME'] + $lifetime);
238 $metaData = str_pad($expiryTime, self::EXPIRYTIME_LENGTH) . implode(' ', $tags) . str_pad(strlen($data), self::DATASIZE_DIGITS);
239 $result = file_put_contents($temporaryCacheEntryPathAndFilename, $data . $metaData);
240
241 if ($result === FALSE) {
242 throw new t3lib_cache_exception(
243 'The temporary cache file "' . $temporaryCacheEntryPathAndFilename . '" could not be written.',
244 1204026251
245 );
246 }
247
248 $i = 0;
249 $cacheEntryPathAndFilename = $this->root . $this->cacheDirectory . $entryIdentifier;
250 // @TODO: Figure out why the heck this is done and maybe find a smarter solution, report to FLOW3
251 while (!rename($temporaryCacheEntryPathAndFilename, $cacheEntryPathAndFilename) && $i < 5) {
252 $i++;
253 }
254
255 // @FIXME: At least the result of rename() should be handled here, report to FLOW3
256 if ($result === FALSE) {
257 throw new t3lib_cache_exception(
258 'The cache file "' . $cacheEntryPathAndFilename . '" could not be written.',
259 1222361632
260 );
261 }
262 }
263
264 /**
265 * Loads data from a cache file.
266 *
267 * @param string $entryIdentifier An identifier which describes the cache entry to load
268 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
269 * @author Robert Lemke <robert@typo3.org>
270 * @author Karsten Dambekalns <karsten@typo3.org>
271 * @api
272 */
273 public function get($entryIdentifier) {
274 $pathAndFilename = $this->root . $this->cacheDirectory . $entryIdentifier;
275 if ($this->isCacheFileExpired($pathAndFilename)) {
276 return FALSE;
277 }
278 $dataSize = (integer)file_get_contents($pathAndFilename, NULL, NULL, filesize($pathAndFilename) - self::DATASIZE_DIGITS, self::DATASIZE_DIGITS);
279 return file_get_contents($pathAndFilename, NULL, NULL, 0, $dataSize);
280 }
281
282 /**
283 * Checks if a cache entry with the specified identifier exists.
284 *
285 * @param string $entryIdentifier Specifies the cache entry to remove
286 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
287 * @author Robert Lemke <robert@typo3.org>
288 * @api
289 */
290 public function has($entryIdentifier) {
291 return !$this->isCacheFileExpired($this->root . $this->cacheDirectory . $entryIdentifier);
292 }
293
294 /**
295 * Removes all cache entries matching the specified identifier.
296 * Usually this only affects one entry.
297 *
298 * @param string $entryIdentifier Specifies the cache entry to remove
299 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
300 * @author Robert Lemke <robert@typo3.org>
301 * @api
302 */
303 public function remove($entryIdentifier) {
304 $pathAndFilename = $this->root . $this->cacheDirectory . $entryIdentifier;
305 if (!file_exists($pathAndFilename)) {
306 return FALSE;
307 }
308 if (unlink($pathAndFilename) === FALSE) {
309 return FALSE;
310 }
311 return TRUE;
312 }
313
314 /**
315 * Finds and returns all cache entry identifiers which are tagged by the
316 * specified tag.
317 *
318 * @param string $searchedTag The tag to search for
319 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
320 * @author Robert Lemke <robert@typo3.org>
321 * @author Karsten Dambekalns <karsten@typo3.org>
322 * @api
323 */
324 public function findIdentifiersByTag($searchedTag) {
325 $entryIdentifiers = array();
326 $now = $GLOBALS['EXEC_TIME'];
327 for ($directoryIterator = t3lib_div::makeInstance('DirectoryIterator', $this->root . $this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
328 if ($directoryIterator->isDot()) {
329 continue;
330 }
331 $cacheEntryPathAndFilename = $directoryIterator->getPathname();
332 $index = (integer)file_get_contents($cacheEntryPathAndFilename, NULL, NULL, filesize($cacheEntryPathAndFilename) - self::DATASIZE_DIGITS, self::DATASIZE_DIGITS);
333 $metaData = file_get_contents($cacheEntryPathAndFilename, NULL, NULL, $index);
334
335 $expiryTime = (integer)substr($metaData, 0, self::EXPIRYTIME_LENGTH);
336 if ($expiryTime !== 0 && $expiryTime < $now) {
337 continue;
338 }
339 if (in_array($searchedTag, explode(' ', substr($metaData, self::EXPIRYTIME_LENGTH, -self::DATASIZE_DIGITS)))) {
340 $entryIdentifiers[] = $directoryIterator->getFilename();
341 }
342 }
343 return $entryIdentifiers;
344 }
345
346 /**
347 * Finds and returns all cache entry identifiers which are tagged by the
348 * specified tags.
349 *
350 * @param array $searchedTags Array of tags to search for
351 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
352 * @author Ingo Renner <ingo@typo3.org>
353 * @author Christian Kuhn <lolli@schwarzbu.ch>
354 * @api
355 */
356 public function findIdentifiersByTags(array $searchedTags) {
357 $entryIdentifiers = array();
358 for ($directoryIterator = t3lib_div::makeInstance('DirectoryIterator', $this->root . $this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
359 if ($directoryIterator->isDot()) {
360 continue;
361 }
362 $cacheEntryPathAndFilename = $directoryIterator->getPathname();
363 $index = (integer)file_get_contents($cacheEntryPathAndFilename, NULL, NULL, filesize($cacheEntryPathAndFilename) - self::DATASIZE_DIGITS, self::DATASIZE_DIGITS);
364 $metaData = file_get_contents($cacheEntryPathAndFilename, NULL, NULL, $index);
365
366 $expiryTime = (integer)substr($metaData, 0, self::EXPIRYTIME_LENGTH);
367 if ($expiryTime !== 0 && $expiryTime < $GLOBALS['EXEC_TIME']) {
368 continue;
369 }
370 if (in_array($searchedTags, explode(' ', substr($metaData, self::EXPIRYTIME_LENGTH, -self::DATASIZE_DIGITS)))) {
371 $entryIdentifiers[] = $directoryIterator->getFilename();
372 }
373 }
374 return $entryIdentifiers;
375 }
376
377 /**
378 * Removes all cache entries of this cache.
379 *
380 * @return void
381 * @author Robert Lemke <robert@typo3.org>
382 * @author Christian Kuhn <lolli@schwarzbu.ch>
383 * @api
384 */
385 public function flush() {
386 t3lib_div::rmdir($this->root . $this->cacheDirectory, TRUE);
387 }
388
389 /**
390 * Removes all cache entries of this cache which are tagged by the specified tag.
391 *
392 * @param string $tag The tag the entries must have
393 * @return void
394 * @author Ingo Renner <ingo@typo3.org>
395 * @api
396 */
397 public function flushByTag($tag) {
398 $identifiers = $this->findIdentifiersByTag($tag);
399 if (count($identifiers) === 0) {
400 return;
401 }
402
403 foreach ($identifiers as $entryIdentifier) {
404 $this->remove($entryIdentifier);
405 }
406 }
407
408 /**
409 * Removes all cache entries of this cache which are tagged by the specified tag.
410 *
411 * @param array $tags The tags the entries must have
412 * @return void
413 * @author Ingo Renner <ingo@typo3.org>
414 * @api
415 */
416 public function flushByTags(array $tags) {
417 foreach ($tags as $tag) {
418 $this->flushByTag($tag);
419 }
420 }
421
422 /**
423 * Checks if the given cache entry files are still valid or if their
424 * lifetime has exceeded.
425 *
426 * @param string $cacheEntryPathAndFilename
427 * @return boolean
428 * @author Robert Lemke <robert@typo3.org>
429 */
430 protected function isCacheFileExpired($cacheEntryPathAndFilename) {
431 if (!file_exists($cacheEntryPathAndFilename)) {
432 return TRUE;
433 }
434 $index = (integer)file_get_contents($cacheEntryPathAndFilename, NULL, NULL, filesize($cacheEntryPathAndFilename) - self::DATASIZE_DIGITS, self::DATASIZE_DIGITS);
435 $expiryTime = file_get_contents($cacheEntryPathAndFilename, NULL, NULL, $index, self::EXPIRYTIME_LENGTH);
436 return ($expiryTime != 0 && $expiryTime < $GLOBALS['EXEC_TIME']);
437 }
438
439 /**
440 * Does garbage collection
441 *
442 * @return void
443 * @author Karsten Dambekalns <karsten@typo3.org>
444 * @api
445 */
446 public function collectGarbage() {
447 if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
448 throw new t3lib_cache_Exception(
449 'Yet no cache frontend has been set via setCache().',
450 1222686150
451 );
452 }
453
454 $pattern = $this->root . $this->cacheDirectory . '*';
455 $filesFound = glob($pattern);
456
457 if (is_array($filesFound)) {
458 foreach ($filesFound as $cacheFilename) {
459 if ($this->isCacheFileExpired($cacheFilename)) {
460 $this->remove(basename($cacheFilename));
461 }
462 }
463 }
464 }
465
466 /**
467 * Tries to find the cache entry for the specified identifier.
468 * Usually only one cache entry should be found - if more than one exist, this
469 * is due to some error or crash.
470 *
471 * @param string $entryIdentifier The cache entry identifier
472 * @return mixed The file names (including path) as an array if one or more entries could be found, otherwise FALSE
473 * @author Robert Lemke <robert@typo3.org>
474 * @throws t3lib_cache_Exception if no frontend has been set
475 * @internal
476 */
477 protected function findCacheFilesByIdentifier($entryIdentifier) {
478 if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
479 throw new t3lib_cache_Exception(
480 'Yet no cache frontend has been set via setCache().',
481 1204111376
482 );
483 }
484
485 $pattern = $this->root . $this->cacheDirectory . $entryIdentifier;
486 $filesFound = glob($pattern);
487 if ($filesFound === FALSE || count($filesFound) === 0) {
488 return FALSE;
489 }
490
491 return $filesFound;
492 }
493
494 /**
495 * Loads PHP code from the cache and require_onces it right away.
496 *
497 * @param string $entryIdentifier An identifier which describes the cache entry to load
498 * @return mixed Potential return value from the include operation
499 * @api
500 */
501 public function requireOnce($entryIdentifier) {
502 $pathAndFilename = $this->root . $this->cacheDirectory . $entryIdentifier;
503 return ($this->isCacheFileExpired($pathAndFilename)) ? FALSE : require_once($pathAndFilename);
504 }
505 }
506
507
508 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_filebackend.php']) {
509 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_filebackend.php']);
510 }
511
512 ?>