forked from SloppySyntax/koken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl.php
59 lines (49 loc) · 1.2 KB
/
dl.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
<?php
$path = $_GET['src'];
if (preg_match('~/storage/originals/([a-z0-9]{2}/[a-z0-9]{2}/.*)$~', $path, $matches))
{
$base = dirname(__FILE__) . '/storage/originals/';
$full_path = $base . $matches[1];
if (!file_exists($full_path))
{
header('HTTP/1.1 404 Not Found');
exit;
}
$realbase = realpath($base);
$realfile = realpath($full_path);
if (!$realfile || strpos($realfile, $realbase) !== 0)
{
header('HTTP/1.1 403 Forbidden');
exit;
}
$name = basename($path);
$info = pathinfo($name);
$ext = $info['extension'];
header("Content-Disposition: attachment; filename=$name");
switch(strtolower($ext)) {
case 'jpg':
$ct = 'image/jpeg';
break;
case 'gif':
$ct = 'image/gif';
break;
case 'png':
$ct = 'image/png';
break;
default:
$ct = 'application/octet-stream';
break;
}
header('Content-type: ' . $ct);
header('Content-length: ' . filesize($full_path));
$disabled_functions = explode(',', ini_get('disable_functions'));
if (is_callable('readfile') && !in_array('readfile', $disabled_functions)) {
readfile($full_path);
} else {
die(file_get_contents($full_path));
}
}
else
{
header('HTTP/1.1 403 Forbidden');
}