1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
<?php
# Lifter010: TODO
// +--------------------------------------------------------------------------+
// This file is part of Stud.IP
// StudipFileCache.class.php
//
//
//
// Copyright (c) 2007 André Noack <noack@data-quest.de>
// +--------------------------------------------------------------------------+
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
// +--------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// +--------------------------------------------------------------------------+
/**
* StudipCache implementation using files
*
* @package studip
* @subpackage cache
*
* @author André Noack <noack@data-quest.de>
* @version 2
*/
class StudipFileCache implements StudipSystemCache
{
use StudipCacheKeyTrait;
/**
* full path to cache directory
*
* @var string
*/
private $dir;
/**
* @return string A translateable display name for this cache class.
*/
public static function getDisplayName(): string
{
return _('Dateisystem');
}
/**
* without the 'dir' argument the cache path is taken from
* $CACHING_FILECACHE_PATH or is set to
* $TMP_PATH/studip_cache
*
* @param string the path to use
* @return void
* @throws exception if the directory does not exist or could not be
* created
*/
public function __construct($path = '')
{
$this->dir = $path
?: (Config::get()->SYSTEMCACHE['type'] == 'StudipFileCache' ?
Config::get()->SYSTEMCACHE['config']['path'] : '')
?: $GLOBALS['CACHING_FILECACHE_PATH']
?: ($GLOBALS['TMP_PATH'] . '/' . 'studip_cache');
$this->dir = rtrim($this->dir, '\\/') . '/';
if (!is_dir($this->dir) && !@mkdir($this->dir, 0700)) {
throw new Exception('Could not create directory: ' . $this->dir);
}
if (!is_writable($this->dir)) {
throw new Exception('Can not write to directory: ' . $this->dir);
}
}
/**
* get path to cache directory
*
* @return string
*/
public function getCacheDir()
{
return $this->dir;
}
/**
* expire cache item
*
* @see StudipCache::expire()
* @param string $arg
* @return void
*/
public function expire($arg)
{
$key = $this->getCacheKey($arg);
if ($file = $this->getPathAndFile($key)){
@unlink($file);
}
}
/**
* Expire all items from the cache.
*/
public function flush()
{
rmdirr($this->dir);
}
/**
* retrieve cache item from filesystem
* tests first if item is expired
*
* @see StudipCache::read()
* @param string $arg a cache key
* @return string|bool
*/
public function read($arg)
{
$key = $this->getCacheKey($arg);
if ($file = $this->check($key)){
$f = @fopen($file, 'rb');
if ($f) {
@flock($f, LOCK_SH);
$result = stream_get_contents($f);
@fclose($f);
}
return unserialize($result);
}
return false;
}
/**
* store data as cache item in filesystem
*
* @see StudipCache::write()
* @param string $arg a cache key
* @param mixed $content data to store
* @param int $expire expiry time in seconds, default 12h
* @return int|bool the number of bytes that were written to the file,
* or false on failure
*/
public function write($arg, $content, $expire = self::DEFAULT_EXPIRATION)
{
$key = $this->getCacheKey($arg);
$this->expire($key);
$file = $this->getPathAndFile($key, $expire);
return @file_put_contents($file, serialize($content), LOCK_EX);
}
/**
* checks if specified cache item is expired
* if expired the cache file is deleted
*
* @param string $key a cache key to check
* @return string|bool the path to the cache file or false if expired
*/
private function check($key)
{
if ($file = $this->getPathAndFile($key)){
list($id, $expire) = explode('-', basename($file));
if (time() < $expire) {
return $file;
} else {
@unlink($file);
}
}
return false;
}
/**
* get the full path to a cache file
*
* the cache files are organized in sub-folders named by
* the first two characters of the hashed cache key.
* the filename is constructed from the hashed cache key
* and the timestamp of expiration
*
* @param string $key a cache key
* @param int $expire expiry time in seconds
* @return string|bool full path to cache item or false on failure
*/
private function getPathAndFile($key, $expire = null)
{
$id = hash('md5', $key);
$path = $this->dir . mb_substr($id, 0, 2);
if (!is_dir($path) && !@mkdir($path, 0700)) {
throw new Exception('Could not create directory: ' . $path);
}
if (!is_null($expire)){
return $path . '/' . $id . '-' . (time() + $expire);
} else {
$files = @glob("{$path}/{$id}*");
if (count($files) > 0) {
return $files[0];
}
}
return false;
}
/**
* purges expired entries from the cache directory
*
* @param bool echo messages if set to false
* @return int the number of deleted files
*/
public function purge($be_quiet = true)
{
$now = time();
$deleted = 0;
foreach (@glob($this->dir . '*', GLOB_ONLYDIR) as $current_dir){
foreach (@glob("{$current_dir}/*") as $file){
list($id, $expire) = explode('-', basename($file));
if ($expire < $now) {
if (@unlink($file)) {
++$deleted;
if (!$be_quiet) {
echo "File: {$file} deleted.\n";
}
}
} else if (!$be_quiet){
echo "File: {$file} expires on " . strftime('%x %X', $expire) . "\n";
}
}
}
return $deleted;
}
/**
* Return statistics.
*
* @StudipSystemCache::getStats()
*
* @return array|array[]
*/
public function getStats(): array
{
return [
__CLASS__ => [
'name' => _('Anzahl Einträge'),
'value' => DBManager::get()->fetchColumn("SELECT COUNT(*) FROM `cache`")
]
];
}
/**
* Return the Vue component name and props that handle configuration.
*
* @see StudipSystemCache::getConfig()
*
* @return array
*/
public static function getConfig(): array
{
$currentCache = Config::get()->SYSTEMCACHE;
// Set default config for this cache
$currentConfig = [
'path' => $GLOBALS['TMP_PATH'] . '/studip_cache'
];
// If this cache is set as system cache, use config from global settings.
if ($currentCache['type'] == __CLASS__) {
$currentConfig = $currentCache['config'];
}
return [
'component' => 'FileCacheConfig',
'props' => $currentConfig
];
}
}
|