Issues (910)

framework/caching/FileCache.php (2 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\caching;
9
10
use Yii;
11
use yii\helpers\FileHelper;
12
13
/**
14
 * FileCache implements a cache component using files.
15
 *
16
 * For each data value being cached, FileCache will store it in a separate file.
17
 * The cache files are placed under [[cachePath]]. FileCache will perform garbage collection
18
 * automatically to remove expired cache files.
19
 *
20
 * Please refer to [[Cache]] for common cache operations that are supported by FileCache.
21
 *
22
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
23
 *
24
 * @author Qiang Xue <[email protected]>
25
 * @since 2.0
26
 */
27
class FileCache extends Cache
28
{
29
    /**
30
     * @var string a string prefixed to every cache key. This is needed when you store
31
     * cache data under the same [[cachePath]] for different applications to avoid
32
     * conflict.
33
     *
34
     * To ensure interoperability, only alphanumeric characters should be used.
35
     */
36
    public $keyPrefix = '';
37
    /**
38
     * @var string the directory to store cache files. You may use [path alias](guide:concept-aliases) here.
39
     * If not set, it will use the "cache" subdirectory under the application runtime path.
40
     */
41
    public $cachePath = '@runtime/cache';
42
    /**
43
     * @var string cache file suffix. Defaults to '.bin'.
44
     */
45
    public $cacheFileSuffix = '.bin';
46
    /**
47
     * @var int the level of sub-directories to store cache files. Defaults to 1.
48
     * If the system has huge number of cache files (e.g. one million), you may use a bigger value
49
     * (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
50
     * is not over burdened with a single directory having too many files.
51
     */
52
    public $directoryLevel = 1;
53
    /**
54
     * @var int the probability (parts per million) that garbage collection (GC) should be performed
55
     * when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
56
     * This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
57
     */
58
    public $gcProbability = 10;
59
    /**
60
     * @var int|null the permission to be set for newly created cache files.
61
     * This value will be used by PHP chmod() function. No umask will be applied.
62
     * If not set, the permission will be determined by the current environment.
63
     */
64
    public $fileMode;
65
    /**
66
     * @var int the permission to be set for newly created directories.
67
     * This value will be used by PHP chmod() function. No umask will be applied.
68
     * Defaults to 0775, meaning the directory is read-writable by owner and group,
69
     * but read-only for other users.
70
     */
71
    public $dirMode = 0775;
72
73
74
    /**
75
     * Initializes this component by ensuring the existence of the cache path.
76
     */
77 132
    public function init()
78
    {
79 132
        parent::init();
80 132
        $this->cachePath = Yii::getAlias($this->cachePath);
81 132
        if (!is_dir($this->cachePath)) {
82 1
            FileHelper::createDirectory($this->cachePath, $this->dirMode, true);
83
        }
84
    }
85
86
    /**
87
     * Checks whether a specified key exists in the cache.
88
     * This can be faster than getting the value from the cache if the data is big.
89
     * Note that this method does not check whether the dependency associated
90
     * with the cached data, if there is any, has changed. So a call to [[get]]
91
     * may return false while exists returns true.
92
     * @param mixed $key a key identifying the cached value. This can be a simple string or
93
     * a complex data structure consisting of factors representing the key.
94
     * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
95
     */
96 5
    public function exists($key)
97
    {
98 5
        $cacheFile = $this->getCacheFile($this->buildKey($key));
99
100 5
        return @filemtime($cacheFile) > time();
101
    }
102
103
    /**
104
     * Retrieves a value from cache with a specified key.
105
     * This is the implementation of the method declared in the parent class.
106
     * @param string $key a unique key identifying the cached value
107
     * @return string|false the value stored in cache, false if the value is not in the cache or expired.
108
     */
109 129
    protected function getValue($key)
110
    {
111 129
        $cacheFile = $this->getCacheFile($key);
112
113 129
        if (@filemtime($cacheFile) > time()) {
114 33
            $fp = @fopen($cacheFile, 'r');
115 33
            if ($fp !== false) {
116 33
                @flock($fp, LOCK_SH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for flock(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

116
                /** @scrutinizer ignore-unhandled */ @flock($fp, LOCK_SH);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
117 33
                $cacheValue = @stream_get_contents($fp);
118 33
                @flock($fp, LOCK_UN);
119 33
                @fclose($fp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fclose(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

119
                /** @scrutinizer ignore-unhandled */ @fclose($fp);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
120 33
                return $cacheValue;
121
            }
122
        }
123
124 121
        return false;
125
    }
126
127
    /**
128
     * Stores a value identified by a key in cache.
129
     * This is the implementation of the method declared in the parent class.
130
     *
131
     * @param string $key the key identifying the value to be cached
132
     * @param string $value the value to be cached. Other types (If you have disabled [[serializer]]) unable to get is
133
     * correct in [[getValue()]].
134
     * @param int $duration the number of seconds in which the cached value will expire. Fewer than or equal to 0 means 1 year expiration time.
135
     * @return bool true if the value is successfully stored into cache, false otherwise
136
     */
137 45
    protected function setValue($key, $value, $duration)
138
    {
139 45
        $this->gc();
140 45
        $cacheFile = $this->getCacheFile($key);
141 45
        if ($this->directoryLevel > 0) {
142 45
            @FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
143
        }
144
        // If ownership differs the touch call will fail, so we try to
145
        // rebuild the file from scratch by deleting it first
146
        // https://github.com/yiisoft/yii2/pull/16120
147 45
        if (is_file($cacheFile) && function_exists('posix_geteuid') && fileowner($cacheFile) !== posix_geteuid()) {
148
            @unlink($cacheFile);
149
        }
150 45
        if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
151 45
            if ($this->fileMode !== null) {
152
                @chmod($cacheFile, $this->fileMode);
153
            }
154 45
            if ($duration <= 0) {
155 36
                $duration = 31536000; // 1 year
156
            }
157
158 45
            return @touch($cacheFile, $duration + time());
159
        }
160
161
        $error = error_get_last();
162
        Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__);
163
        return false;
164
    }
165
166
    /**
167
     * Stores a value identified by a key into cache if the cache does not contain this key.
168
     * This is the implementation of the method declared in the parent class.
169
     *
170
     * @param string $key the key identifying the value to be cached
171
     * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) unable to get is
172
     * correct in [[getValue()]].
173
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
174
     * @return bool true if the value is successfully stored into cache, false otherwise
175
     */
176 3
    protected function addValue($key, $value, $duration)
177
    {
178 3
        $cacheFile = $this->getCacheFile($key);
179 3
        if (@filemtime($cacheFile) > time()) {
180 2
            return false;
181
        }
182
183 3
        return $this->setValue($key, $value, $duration);
184
    }
185
186
    /**
187
     * Deletes a value with the specified key from cache
188
     * This is the implementation of the method declared in the parent class.
189
     * @param string $key the key of the value to be deleted
190
     * @return bool if no error happens during deletion
191
     */
192 105
    protected function deleteValue($key)
193
    {
194 105
        $cacheFile = $this->getCacheFile($key);
195
196 105
        return @unlink($cacheFile);
197
    }
198
199
    /**
200
     * Returns the cache file path given the normalized cache key.
201
     * @param string $normalizedKey normalized cache key by [[buildKey]] method
202
     * @return string the cache file path
203
     */
204 130
    protected function getCacheFile($normalizedKey)
205
    {
206 130
        $cacheKey = $normalizedKey;
207
208 130
        if ($this->keyPrefix !== '') {
209
            // Remove key prefix to avoid generating constant directory levels
210 1
            $lenKeyPrefix = strlen($this->keyPrefix);
211 1
            $cacheKey = substr_replace($normalizedKey, '', 0, $lenKeyPrefix);
212
        }
213
214 130
        $cachePath = $this->cachePath;
215
216 130
        if ($this->directoryLevel > 0) {
217 130
            for ($i = 0; $i < $this->directoryLevel; ++$i) {
218 130
                if (($subDirectory = substr($cacheKey, $i + $i, 2)) !== false) {
219 130
                    $cachePath .= DIRECTORY_SEPARATOR . $subDirectory;
220
                }
221
            }
222
        }
223
224 130
        return $cachePath . DIRECTORY_SEPARATOR . $normalizedKey . $this->cacheFileSuffix;
225
    }
226
227
    /**
228
     * Deletes all values from cache.
229
     * This is the implementation of the method declared in the parent class.
230
     * @return bool whether the flush operation was successful.
231
     */
232 12
    protected function flushValues()
233
    {
234 12
        $this->gc(true, false);
235
236 12
        return true;
237
    }
238
239
    /**
240
     * Removes expired cache files.
241
     * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
242
     * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
243
     * @param bool $expiredOnly whether to removed expired cache files only.
244
     * If false, all cache files under [[cachePath]] will be removed.
245
     */
246 45
    public function gc($force = false, $expiredOnly = true)
247
    {
248 45
        if ($force || random_int(0, 1000000) < $this->gcProbability) {
249 12
            $this->gcRecursive($this->cachePath, $expiredOnly);
250
        }
251
    }
252
253
    /**
254
     * Recursively removing expired cache files under a directory.
255
     * This method is mainly used by [[gc()]].
256
     * @param string $path the directory under which expired cache files are removed.
257
     * @param bool $expiredOnly whether to only remove expired cache files. If false, all files
258
     * under `$path` will be removed.
259
     */
260 12
    protected function gcRecursive($path, $expiredOnly)
261
    {
262 12
        if (($handle = opendir($path)) !== false) {
263 12
            while (($file = readdir($handle)) !== false) {
264 12
                if (strncmp($file, '.', 1) === 0) {
265 12
                    continue;
266
                }
267 11
                $fullPath = $path . DIRECTORY_SEPARATOR . $file;
268 11
                if (is_dir($fullPath)) {
269 11
                    $this->gcRecursive($fullPath, $expiredOnly);
270 11
                    if (!$expiredOnly) {
271 11
                        if (!@rmdir($fullPath)) {
272
                            $error = error_get_last();
273 11
                            Yii::warning("Unable to remove directory '{$fullPath}': {$error['message']}", __METHOD__);
274
                        }
275
                    }
276 11
                } elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) {
277 11
                    if (!@unlink($fullPath)) {
278
                        $error = error_get_last();
279
                        Yii::warning("Unable to remove file '{$fullPath}': {$error['message']}", __METHOD__);
280
                    }
281
                }
282
            }
283 12
            closedir($handle);
284
        }
285
    }
286
}
287