2 namespace TYPO3\CMS\Core\Cache\Backend
;
5 * This script belongs to the FLOW3 framework. *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
11 * The TYPO3 project - inspiring people to share! *
14 * A caching backend which stores cache entries in files, but does not support or
15 * care about expiry times and tags.
19 class SimpleFileBackend
extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend
implements \TYPO3\CMS\Core\Cache\Backend\PhpCapableBackendInterface
{
21 const SEPARATOR
= '^';
22 const EXPIRYTIME_FORMAT
= 'YmdHis';
23 const EXPIRYTIME_LENGTH
= 14;
24 const DATASIZE_DIGITS
= 10;
26 * Directory where the files are stored
30 protected $cacheDirectory = '';
33 * TYPO3 v4 note: This variable is only available in v5
34 * Temporary path to cache directory before setCache() was called. It is
35 * set by setCacheDirectory() and used in setCache() method which calls
36 * the directory creation if needed. The variable is not used afterwards,
37 * the final cache directory path is stored in $this->cacheDirectory then.
39 * @var string Temporary path to cache directory
41 protected $temporaryCacheDirectory = '';
44 * A file extension to use for each cache entry.
48 protected $cacheEntryFileExtension = '';
53 protected $cacheEntryIdentifiers = array();
58 protected $frozen = FALSE
;
61 * If the extension "igbinary" is installed, use it for increased performance.
62 * Caching the result of extension_loaded() here is faster than calling extension_loaded() multiple times.
66 protected $useIgBinary = FALSE
;
69 * Initializes this cache frontend
73 public function initializeObject() {
74 $this->useIgBinary
= extension_loaded('igbinary');
78 * Sets a reference to the cache frontend which uses this backend and
79 * initializes the default cache directory.
81 * TYPO3 v4 note: This method is different between TYPO3 v4 and FLOW3
82 * because the Environment class to get the path to a temporary directory
83 * does not exist in v4.
85 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache The cache frontend
86 * @throws \TYPO3\CMS\Core\Cache\Exception
89 public function setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
$cache) {
90 parent
::setCache($cache);
91 if (empty($this->temporaryCacheDirectory
)) {
92 // If no cache directory was given with cacheDirectory
93 // configuration option, set it to a path below typo3temp/
94 $temporaryCacheDirectory = PATH_site
. 'typo3temp/';
96 $temporaryCacheDirectory = $this->temporaryCacheDirectory
;
98 $codeOrData = $cache instanceof \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend ?
'Code' : 'Data';
99 $finalCacheDirectory = $temporaryCacheDirectory . 'Cache/' . $codeOrData . '/' . $this->cacheIdentifier
. '/';
100 if (!is_dir($finalCacheDirectory)) {
101 $this->createFinalCacheDirectory($finalCacheDirectory);
103 unset($this->temporaryCacheDirectory
);
104 $this->cacheDirectory
= $finalCacheDirectory;
105 $this->cacheEntryFileExtension
= $cache instanceof \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend ?
'.php' : '';
106 if (strlen($this->cacheDirectory
) +
23 > \TYPO3\CMS\Core\Utility\GeneralUtility
::getMaximumPathLength()) {
107 throw new \TYPO3\CMS\Core\Cache\
Exception('The length of the temporary cache file path "' . $this->cacheDirectory
. '" exceeds the ' . 'maximum path length of ' . (\TYPO3\CMS\Core\Utility\GeneralUtility
::getMaximumPathLength() - 23) . '. Please consider ' . 'setting the temporaryDirectoryBase option to a shorter path.', 1248710426);
112 * Sets the directory where the cache files are stored. By default it is
113 * assumed that the directory is below the TYPO3_DOCUMENT_ROOT. However, an
114 * absolute path can be selected, too.
116 * This method does not exist in FLOW3 anymore, but it is needed in
117 * TYPO3 v4 to enable a cache path outside of document root. The final
118 * cache path is checked and created in createFinalCachDirectory(),
119 * called by setCache() method, which is done _after_ the cacheDirectory
120 * option was handled.
122 * @param string $cacheDirectory The cache base directory. If a relative path
124 * @throws \TYPO3\CMS\Core\Cache\Exception if the directory is not within allowed
126 public function setCacheDirectory($cacheDirectory) {
127 // Skip handling if directory is a stream ressource
128 // This is used by unit tests with vfs:// directoryies
129 if (strpos($cacheDirectory, '://')) {
130 $this->temporaryCacheDirectory
= $cacheDirectory;
133 $documentRoot = PATH_site
;
134 if ($open_basedir = ini_get('open_basedir')) {
135 if (TYPO3_OS
=== 'WIN') {
137 $cacheDirectory = str_replace('\\', '/', $cacheDirectory);
138 if (!preg_match('/[A-Z]:/', substr($cacheDirectory, 0, 2))) {
139 $cacheDirectory = PATH_site
. $cacheDirectory;
143 if ($cacheDirectory[0] != '/') {
144 // relative path to cache directory.
145 $cacheDirectory = PATH_site
. $cacheDirectory;
148 $basedirs = explode($delimiter, $open_basedir);
149 $cacheDirectoryInBaseDir = FALSE
;
150 foreach ($basedirs as $basedir) {
151 if (TYPO3_OS
=== 'WIN') {
152 $basedir = str_replace('\\', '/', $basedir);
154 if ($basedir[strlen($basedir) - 1] !== '/') {
157 if (\TYPO3\CMS\Core\Utility\GeneralUtility
::isFirstPartOfStr($cacheDirectory, $basedir)) {
158 $documentRoot = $basedir;
159 $cacheDirectory = str_replace($basedir, '', $cacheDirectory);
160 $cacheDirectoryInBaseDir = TRUE
;
164 if (!$cacheDirectoryInBaseDir) {
165 throw new \TYPO3\CMS\Core\Cache\
Exception('Open_basedir restriction in effect. The directory "' . $cacheDirectory . '" is not in an allowed path.');
168 if ($cacheDirectory[0] == '/') {
169 // Absolute path to cache directory.
172 if (TYPO3_OS
=== 'WIN') {
173 if (substr($cacheDirectory, 0, strlen($documentRoot)) === $documentRoot) {
178 // After this point all paths have '/' as directory seperator
179 if ($cacheDirectory[strlen($cacheDirectory) - 1] !== '/') {
180 $cacheDirectory .= '/';
182 $this->temporaryCacheDirectory
= $documentRoot . $cacheDirectory . $this->cacheIdentifier
. '/';
186 * Create the final cache directory if it does not exist. This method
187 * exists in TYPO3 v4 only.
189 * @param string $finalCacheDirectory Absolute path to final cache directory
191 * @throws \TYPO3\CMS\Core\Cache\Exception If directory is not writable after creation
193 protected function createFinalCacheDirectory($finalCacheDirectory) {
195 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir_deep($finalCacheDirectory);
196 } catch (\RuntimeException
$e) {
197 throw new \TYPO3\CMS\Core\Cache\
Exception('The directory "' . $finalCacheDirectory . '" can not be created.', 1303669848, $e);
199 if (!is_writable($finalCacheDirectory)) {
200 throw new \TYPO3\CMS\Core\Cache\
Exception('The directory "' . $finalCacheDirectory . '" is not writable.', 1203965200);
205 * Returns the directory where the cache files are stored
207 * @return string Full path of the cache directory
210 public function getCacheDirectory() {
211 return $this->cacheDirectory
;
215 * Saves data in a cache file.
217 * @param string $entryIdentifier An identifier for this specific cache entry
218 * @param string $data The data to be stored
219 * @param array $tags Tags to associate with this cache entry
220 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
222 * @throws \TYPO3\CMS\Core\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.
223 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if the data to bes stored is not a string.
224 * @throws \InvalidArgumentException
227 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL
) {
228 if (!is_string($data)) {
229 throw new \TYPO3\CMS\Core\Cache\Exception\
InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1334756734);
231 if ($entryIdentifier !== basename($entryIdentifier)) {
232 throw new \
InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756735);
234 if ($entryIdentifier === '') {
235 throw new \
InvalidArgumentException('The specified entry identifier must not be empty.', 1334756736);
237 $temporaryCacheEntryPathAndFilename = $this->cacheDirectory
. uniqid() . '.temp';
238 $result = file_put_contents($temporaryCacheEntryPathAndFilename, $data);
239 \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($temporaryCacheEntryPathAndFilename);
240 if ($result === FALSE
) {
241 throw new \TYPO3\CMS\Core\Cache\
Exception('The temporary cache file "' . $temporaryCacheEntryPathAndFilename . '" could not be written.', 1334756737);
243 $cacheEntryPathAndFilename = $this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
;
244 rename($temporaryCacheEntryPathAndFilename, $cacheEntryPathAndFilename);
248 * Loads data from a cache file.
250 * @param string $entryIdentifier An identifier which describes the cache entry to load
251 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
252 * @throws \InvalidArgumentException If identifier is invalid
255 public function get($entryIdentifier) {
256 if ($entryIdentifier !== basename($entryIdentifier)) {
257 throw new \
InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756877);
259 $pathAndFilename = $this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
;
260 if (!file_exists($pathAndFilename)) {
263 return file_get_contents($pathAndFilename);
267 * Checks if a cache entry with the specified identifier exists.
269 * @param string $entryIdentifier
270 * @return boolean TRUE if such an entry exists, FALSE if not
271 * @throws \InvalidArgumentException
274 public function has($entryIdentifier) {
275 if ($entryIdentifier !== basename($entryIdentifier)) {
276 throw new \
InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756878);
278 return file_exists($this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
);
282 * Removes all cache entries matching the specified identifier.
283 * Usually this only affects one entry.
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 * @throws \InvalidArgumentException
290 public function remove($entryIdentifier) {
291 if ($entryIdentifier !== basename($entryIdentifier)) {
292 throw new \
InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756960);
294 if ($entryIdentifier === '') {
295 throw new \
InvalidArgumentException('The specified entry identifier must not be empty.', 1334756961);
298 unlink($this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
);
299 } catch (\Exception
$e) {
306 * Removes all cache entries of this cache.
311 public function flush() {
312 \TYPO3\CMS\Core\Utility\GeneralUtility
::flushDirectory($this->cacheDirectory
);
313 $this->createFinalCacheDirectory($this->cacheDirectory
);
317 * Checks if the given cache entry files are still valid or if their
318 * lifetime has exceeded.
320 * @param string $cacheEntryPathAndFilename
324 protected function isCacheFileExpired($cacheEntryPathAndFilename) {
325 return file_exists($cacheEntryPathAndFilename) === FALSE
;
334 public function collectGarbage() {
339 * Tries to find the cache entry for the specified identifier.
341 * @param string $entryIdentifier The cache entry identifier
342 * @return mixed The file names (including path) as an array if one or more entries could be found, otherwise FALSE
344 protected function findCacheFilesByIdentifier($entryIdentifier) {
345 $pathAndFilename = $this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
;
346 return file_exists($pathAndFilename) ?
array($pathAndFilename) : FALSE
;
350 * Loads PHP code from the cache and require_onces it right away.
352 * @param string $entryIdentifier An identifier which describes the cache entry to load
353 * @return mixed Potential return value from the include operation
354 * @throws \InvalidArgumentException
357 public function requireOnce($entryIdentifier) {
358 $pathAndFilename = $this->cacheDirectory
. $entryIdentifier . $this->cacheEntryFileExtension
;
359 if ($entryIdentifier !== basename($entryIdentifier)) {
360 throw new \
InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1282073037);
362 return file_exists($pathAndFilename) ?
require_once $pathAndFilename : FALSE
;