-
Notifications
You must be signed in to change notification settings - Fork 1
/
imthumb-cache.class.php
233 lines (188 loc) · 5.3 KB
/
imthumb-cache.class.php
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
<?php
/**
* Image cache handler
*
* @package ImThumb
* @author Sam Pospischil <[email protected]>
* @since 2014-04-03
*/
class ImThumbCache
{
private static $defaults = array(
'cacheCleanPeriod' => 86400,
'cacheMaxAge' => 86400,
'cachePrefix' => 'timthumb',
'cacheSalt' => 'IOLUJN!(Y&)(TEHlsio(&*Y3978fgsdBBu', /* whatevs this gets overridden by requestHandler mostly */
'cacheSuffix' => '.timthumb.txt',
'cacheFilenameFormat' => false,
);
public $mtime;
protected $baseDir;
protected $cacheCleanPeriod;
protected $cacheMaxAge;
protected $cachePrefix;
protected $cacheSalt;
protected $cacheSuffix;
protected $cacheFilenameFormat;
protected $hasCache = false;
//----------------
public function __construct($baseDir, Array $options) {
$this->baseDir = $baseDir;
$options = array_merge(self::$defaults, $options);
// :IMPORTANT: do not ever allow external user input to find its way into these options as it would allow overriding random member variables
foreach ($options as $k => $v) {
$this->{$k} = $v;
}
}
public function load($src)
{
$this->initCacheDir();
$this->mtime = $this->getModTime($src);
$this->hasCache = $this->mtime !== false;
}
public function isCached($cache = null)
{
if ($cache !== null) {
$this->hasCache = $cache;
}
return $this->hasCache;
}
public function getDirectory()
{
return $this->baseDir;
}
//----------------
public function initCacheDir()
{
$cacheDir = $this->baseDir;
if (!$cacheDir) {
return;
}
if (!touch($cacheDir . '/index.html')) {
throw new ImThumbException("Could not create the index.html file - to fix this create an empty file named index.html file in the cache directory.", ImThumb::ERR_CACHE);
}
}
public function write(ImThumb $imageHandle)
{
if ($this->hasCache || !$this->baseDir) {
return false;
}
$this->initCacheDir();
$tempfile = tempnam($this->baseDir, 'imthumb_tmpimg_');
if (!$imageHandle->getImagick()->writeImage($tempfile)) {
throw new ImThumbException("Could not write image to temporary file", ImThumb::ERR_CACHE);
}
$cacheFile = $this->getCachePath($imageHandle);
$lockFile = $cacheFile . '.lock';
$fh = fopen($lockFile, 'w');
if (!$fh) {
throw new ImThumbException("Could not open the lockfile for writing an image", ImThumb::ERR_CACHE);
}
if (flock($fh, LOCK_EX)) {
@unlink($cacheFile);
rename($tempfile, $cacheFile);
flock($fh, LOCK_UN);
fclose($fh);
@unlink($lockFile);
} else {
fclose($fh);
@unlink($lockFile);
@unlink($tempfile);
throw new ImThumbException("Could not get a lock for writing", ImThumb::ERR_CACHE);
}
return true;
}
public function checkExpiredCaches()
{
$cacheDir = $this->baseDir;
$cacheExpiry = $this->cacheCleanPeriod;
if (!$cacheDir || !$cacheExpiry || $cacheExpiry < 0) {
return;
}
$lastCleanFile = $cacheDir . '/timthumb_cacheLastCleanTime.touch';
// If this is a new installation we need to create the file
if (!is_file($lastCleanFile)) {
if (!touch($lastCleanFile)) {
throw new ImThumbException("Could not create cache clean timestamp file.", ImThumb::ERR_CACHE);
}
}
// check for last auto-purge time
if (@filemtime($lastCleanFile) < (time() - $cacheExpiry)) {
// :NOTE: (from timthumb) Very slight race condition here, but worst case we'll have 2 or 3 servers cleaning the cache simultaneously once a day.
if (!touch($lastCleanFile)) {
$this->error("Could not create cache clean timestamp file.");
}
$maxAge = $this->cacheMaxAge;
$files = glob($this->cacheDirectory . '/*' . $this->cacheSuffix);
if ($files) {
$timeAgo = time() - $maxAge;
foreach ($files as $file) {
if (@filemtime($file) < $timeAgo) {
@unlink($file);
}
}
}
return true;
}
return false;
}
public function getImage(ImThumb $src)
{
$path = $this->getCachePath($src);
if ($path && file_exists($path)) {
return file_get_contents($path);
}
return null;
}
//----------------
protected function getModTime(ImThumb $src)
{
return @filemtime($this->getCachePath($src));
}
public function getCachePath(ImThumb $imageHandle, $src = null)
{
$cacheDir = $this->baseDir;
if (!$cacheDir) {
return false;
}
if (!$src) {
$src = $imageHandle->getSrc();
}
$extension = substr($src, strrpos($src, '.') + 1);
if ($this->cacheFilenameFormat) {
list($width, $height) = $imageHandle->getTargetSize();
return $cacheDir . '/' . str_replace(array(
'%filename%',
'%ext%',
'%w%',
'%h%',
'%q%',
'%a%',
'%zc%',
'%s%',
'%cc%',
'%ct%',
'%cr%',
'%filters%',
'%pjpg%',
'%upscale%',
), array(
basename($src, '.' . $extension),
$extension,
$width,
$height,
$imageHandle->param('quality'),
$imageHandle->param('align'),
$imageHandle->param('cropMode'),
$imageHandle->param('sharpen') ? 's' : '',
$imageHandle->param('canvasColor'),
$imageHandle->param('canvasTransparent') ? 't' : '',
$imageHandle->param('cropRect'),
$imageHandle->param('filters'),
$imageHandle->param('jpgProgressive') ? 'p' : '',
$imageHandle->param('upscale') ? '' : 'nu',
), $this->cacheFilenameFormat);
}
return $cacheDir . '/' . $this->cachePrefix . md5($this->cacheSalt . json_encode($imageHandle->params()) . ImThumb::VERSION) . $this->cacheSuffix;
}
}