-
Notifications
You must be signed in to change notification settings - Fork 0
/
expurgate.php
249 lines (185 loc) · 5.86 KB
/
expurgate.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
define('CACHE_DIR', dirname(__FILE__) . '/cache');
$config_file = dirname(__FILE__) . '/expurgate-config.php';
if ( is_readable($config_file) ) {
include_once $config_file;
}
if ( !defined('MAX_AGE') ) {
define('MAX_AGE', 3600 * 24 * 7);
}
if ( !defined('MAX_CACHE_SIZE') ) {
define('MAX_CACHE_SIZE', 100 * 1024 * 1024);
}
if ( !defined('MAX_CACHE_FILES') ) {
define('MAX_CACHE_FILES', 1024);
}
if ( !defined('MAX_IMAGE_SIZE') ) {
define('MAX_IMAGE_SIZE', 500 * 1024);
}
// When you want to fetch an image and don't care where it comes from, use
// this. It'll fetch the image from the cache if it's cached, and from the
// remote server if not.
function get_image($url) {
$checksum = calculate_checksum($url);
if ( is_cached($checksum) ) {
return get_image_from_cache($checksum);
}
fetch_image($url);
return get_image_from_cache($checksum);
}
// Calculates a checksum for a given URL; this checksum is used as a unique
// identifier for this image -- when caching, for example.
function calculate_checksum($url) {
$key = get_key();
return hash_hmac('sha256', $url, $key);
}
// Given a URL and a checksum, will return true if the checksum is the valid
// one for that URL.
function validate_checksum($url, $checksum) {
$valid_checksum = calculate_checksum($url);
return $valid_checksum === $checksum;
}
// If a file called key.txt exists in the cache directory, its contents will
// be used as a key when generating checksums.
function get_key() {
$key_file = CACHE_DIR . '/key.txt';
if ( is_readable($key_file) ) {
return file_get_contents($key_file);
}
die("I can't find a key! I won't work without one.");
}
// Given a checksum, will return an absolute file path to the cache entry
// with that checksum (whether or not it exists).
function get_cache_filename($checksum) {
return CACHE_DIR . "/$checksum.cache.txt";
}
// Checks whether a cached version of the image already exists.
function is_cached($checksum) {
$cache_file = get_cache_filename($checksum);
$exists = file_exists($cache_file);
$is_readable = is_readable($cache_file);
return $exists && $is_readable;
}
// Reads a file from the store of cached images and outputs it to the browser,
// sending the appropriate headers.
function get_image_from_cache($checksum) {
$cache_file = get_cache_filename($checksum);
if ( !is_cached($checksum) ) {
error('Problem with cache image.');
}
$cache_entry = json_decode(file_get_contents($cache_file));
if ( empty($cache_entry->mime_type) || empty($cache_entry->image_data) ) {
error('Problem with cache image.');
}
$image_data = base64_decode($cache_entry->image_data);
header('Content-Type: ' . $cache_entry->mime_type);
echo $image_data;
}
// Fetches a remote image and stores it in the cache.
function fetch_image($url) {
if ( function_exists('curl_init') ) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_MAXREDIRS, 5);
curl_setopt($c, CURLOPT_TIMEOUT, 10);
$image_data = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
$mime_type = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
$image_size = curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD);
curl_close($c);
}
// TODO: fallback for non-cURL-enabled servers
if ( !in_array($status, array(200, 301, 302)) ) {
error('Invalid image.');
}
if ( $image_size > MAX_IMAGE_SIZE ) {
error('Image is too large.');
}
if ( empty($mime_type) || !preg_match('/^image\//', $mime_type) ) {
error('Invalid image type.');
}
if ( empty($image_data) ) {
error('Invalid image content.');
}
$checksum = calculate_checksum($url);
cache_image($image_data, $mime_type, $checksum);
}
// Stores an image in the cache.
function cache_image($image_data, $mime_type, $checksum) {
$cache_file = get_cache_filename($checksum);
$image_data = base64_encode($image_data);
$cache_entry = json_encode(compact('image_data', 'mime_type'));
return file_put_contents($cache_file, $cache_entry);
}
// Determines whether or not the cache needs purging -- if there are too many
// files, for example, or if they're too large in size.
function cache_is_full() {
$cache_files = get_cache_files();
$too_many_files = ( count($cache_files) > MAX_CACHE_FILES );
if ( $too_many_files ) {
return true;
}
$cache_size = array_reduce(
$cache_files,
function($size, $cache) {
$size += $cache->size;
return $size;
}
);
$too_big = ( $cache_size > MAX_CACHE_SIZE );
if ( $too_big ) {
return true;
}
}
// Removes one image from the cache. This means that, when the cache is full,
// we'll be adopting a one in, one out policy
function purge_cache() {
$cache_files = get_cache_files();
// For now, select a file at random.
$to_delete = $cache_files[array_rand($cache_files)];
unlink(CACHE_DIR . '/' . $to_delete->filename);
}
// Removes expired entries from the cache.
function purge_expired() {
$cache_files = get_cache_files();
array_walk(
$cache_files,
function($cache) {
if ( $cache->modified + MAX_AGE < time() ) {
unlink(CACHE_DIR . '/' . $cache->filename);
}
}
);
}
// Returns a list of all the cached files.
function get_cache_files() {
$files = glob(CACHE_DIR . '/*.cache.txt');
$cache_files = array();
foreach ( (array) $files as $file ) {
$cache_files[] = (object) array(
'filename' => basename($file),
'modified' => filemtime($file),
'size' => filesize($file)
);
}
return $cache_files;
}
function error($message) {
header('HTTP/1.0 404 Not Found');
header('Status: 404 Not Found');
die($message);
}
if ( empty($_GET['url']) || empty($_GET['checksum']) ) {
error('Could not find image.');
}
$url = $_GET['url'];
$checksum = $_GET['checksum'];
$is_valid = validate_checksum($url, $checksum);
if ( !$is_valid ) {
error('Could not find image.');
}
get_image($url);
purge_expired();