/
files.php
261 lines (224 loc) · 7.96 KB
/
files.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
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace Controller;
class Files extends \Controller
{
/**
* Require login to access files
*/
public function __construct()
{
$this->_requireLogin();
}
/**
* Force the framework to use the local filesystem cache method if possible
*/
protected function _useFileCache()
{
$f3 = \Base::instance();
$f3->set("CACHE", "folder=" . $f3->get("TEMP") . "cache/");
}
/**
* Send a file to the browser
*
* @param string $file
* @param string $mime
* @param string $filename
* @param bool $force
* @return int|bool
*/
protected function _sendFile($file, $mime = "", $filename = "", $force = true)
{
if (!is_file($file)) {
return false;
}
$size = filesize($file);
if (!$mime) {
$mime = \Web::instance()->mime($file);
}
header("Content-Type: $mime");
if ($force) {
if (!$filename) {
$filename = basename($file);
}
header("Content-Disposition: attachment; filename=\"$filename\"");
}
header("Accept-Ranges: bytes");
header("Content-Length: $size");
header("X-Powered-By: " . \Base::instance()->get("PACKAGE"));
readfile($file);
return $size;
}
/**
* GET /files/thumb/@size-@id.@format
*
* @param \Base $f3
* @param array $params
* @throws \Exception
*/
public function thumb($f3, $params)
{
$this->_useFileCache();
$cache = \Cache::instance();
// Abort on unusually large dimensions
if ($params["size"] > 1024) {
$f3->error(400);
return;
}
// Ensure proper content-type for JPEG images
if ($params["format"] == "jpg") {
$params["format"] = "jpeg";
}
// Output cached image if one exists
if ($f3->get("DEBUG") < 2) {
$hash = $f3->hash($f3->get('VERB') . " " . $f3->get('URI')) . ".thm";
if ($cache->exists($hash, $data)) {
header("Content-type: image/" . $params["format"]);
echo $data;
return;
}
}
$file = new \Model\Issue\File();
$file->load($params["id"]);
if (!$file->id) {
$f3->error(404);
return;
}
// Load 404 image if original file is missing
if (!is_file($file->disk_filename)) {
header("Content-Type: image/svg+xml");
readfile("img/mime/96/_404.svg");
return;
}
// Initialize thumbnail image
$img = new \Image($file->disk_filename);
// Render thumbnail directly if no alpha
$alpha = (imagecolorat($img->data(), 0, 0) & 0x7F000000) >> 24;
// 1.1 fits perfectly but crops shadow, so we compare width vs height
$size = intval($params["size"]) ?: 96;
if ($alpha) {
$thumbSize = $size;
} elseif ($img->width() > $img->height()) {
$thumbSize = round($size / 1.1);
} else {
$thumbSize = round($size / 1.2);
}
// Resize thumbnail
$img->resize($thumbSize, $thumbSize, false);
$tw = $img->width();
$th = $img->height();
$ox = round(($size - $tw) / 2);
$oy = round(($size - $th) / 2);
$fs = round($size / 24);
$fb = round($fs * 0.75);
// Initialize frame image
$frame = imagecreatetruecolor($size, $size);
imagesavealpha($frame, true);
$transparent = imagecolorallocatealpha($frame, 0, 0, 0, 127);
imagefill($frame, 0, 0, $transparent);
if (!$alpha) {
// Draw drop shadow
$cs = imagecolorallocatealpha($frame, 0, 0, 0, 120);
imagefilledrectangle($frame, $ox - $fb, $size - $oy + $fb, $size - $ox + $fb, $size - $oy + round($fb * 1.5), $cs);
imagefilledrectangle($frame, $ox - round($fb / 2), $size - $oy + $fb, $size - $ox + round($fb / 2), $size - $oy + round($fb * 1.625), $cs);
imagefilledrectangle($frame, $ox, $size - $oy + $fb, $size - $ox, $size - $oy + round($fb * 2), $cs);
// Draw frame
$c0 = imagecolorallocatealpha($frame, 193, 193, 193, 16);
imagefilledrectangle($frame, $ox - $fs, $oy - $fs, $size - $ox + $fs, $size - $oy + $fs, $c0);
$c1 = imagecolorallocate($frame, 243, 243, 243);
imagefilledrectangle($frame, $ox - $fb, $oy - $fb, $size - $ox + $fb, $size - $oy + $fb, $c1);
$c2 = imagecolorallocate($frame, 230, 230, 230);
// This is an incredibly stupid way of deprecating a parameter, thanks PHP.
if (PHP_VERSION_ID >= 80000) {
imagefilledpolygon($frame, array(
$size - $ox + $fb, $oy - $fb,
$size - $ox + $fb, $size - $oy + $fb,
$ox - $fb, $size - $oy + $fb,
), $c2);
} else {
imagefilledpolygon($frame, array(
$size - $ox + $fb, $oy - $fb,
$size - $ox + $fb, $size - $oy + $fb,
$ox - $fb, $size - $oy + $fb,
), 3, $c2);
}
}
// Copy thumbnail onto frame
imagecopy($frame, $img->data(), $ox, $oy, 0, 0, $tw, $th);
if (!$alpha) {
// Draw inner shadow thumbnail
$c3 = imagecolorallocatealpha($frame, 0, 0, 0, 100);
imagerectangle($frame, $ox, $oy, $size - $ox, $size - $oy, $c3);
}
// Render and cache image
ob_start();
call_user_func_array('image' . $params["format"], array($frame));
$data = ob_get_clean();
if ($f3->get("DEBUG") < 2) {
$cache->set($hash, $data, $f3->get("cache_expire.attachments"));
}
// Output image
header("Content-type: image/" . $params["format"]);
echo $data;
}
/**
* GET /avatar/@size-@id.@format
*
* @param \Base $f3
* @param array $params
* @throws \Exception
*/
public function avatar($f3, $params)
{
// Ensure proper content-type for JPEG images
if ($params["format"] == "jpg") {
$params["format"] = "jpeg";
}
$user = new \Model\User();
$user->load($params["id"]);
if ($user->avatar_filename && is_file("uploads/avatars/" . $user->avatar_filename)) {
// Use local file
$img = new \Image($user->avatar_filename, null, "uploads/avatars/");
$img->resize($params["size"], $params["size"]);
// Render and output image
header("Content-type: image/" . $params["format"]);
header("Cache-Control: private, max-age=" . (3600 / 2));
$img->render($params["format"]);
} else {
// Send user to Gravatar
header("Cache-Control: max-age=" . (3600 * 24));
$f3->reroute($f3->get("SCHEME") . ":" . \Helper\View::instance()->gravatar($user->email, $params["size"]), true);
}
}
/**
* GET /files/@id/@name
*
* @param \Base $f3
* @param array $params
* @throws \Exception
*/
public function file($f3, $params)
{
$file = new \Model\Issue\File();
$file->load($params["id"]);
if (!$file->id) {
$f3->error(404);
return;
}
$force = true;
if (
substr($file->content_type, 0, 5) == "image" ||
$file->content_type == "text/plain" ||
$file->content_type == "application/pdf" ||
in_array($file->content_type, ['video/mp4', 'video/webm'])
) {
$force = false;
}
// Force download of SVG images
if ($file->content_type == 'image/svg+xml') {
$force = true;
}
if (!$this->_sendFile($file->disk_filename, $file->content_type, $file->filename, $force)) {
$f3->error(404);
}
}
}