-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_download.php
More file actions
94 lines (80 loc) · 2.52 KB
/
admin_download.php
File metadata and controls
94 lines (80 loc) · 2.52 KB
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
<?php
declare(strict_types=1);
$isHttps =
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https');
session_name('DropzoneAdminSession');
session_start([
'cookie_httponly' => true,
'cookie_secure' => $isHttps,
'cookie_samesite' => 'Lax',
]);
require_once 'inc/config.php';
require_once 'inc/helpers.php';
require_once 'inc/language.php';
define('UPLOADS_DIR', $uploadDir);
define('FILEDATA_JSON', $dataFile);
if (empty($_SESSION['logged_in'])) {
http_response_code(403);
exit('Forbidden');
}
function safe_uploads_join(string $rel): ?string {
$rel = ltrim($rel, '/\\');
$cand = realpath(UPLOADS_DIR . '/' . $rel);
$uploads = realpath(UPLOADS_DIR);
if ($cand && $uploads && str_starts_with($cand, $uploads)) return $cand;
$cand2 = UPLOADS_DIR . '/' . $rel;
$norm = str_replace(['//','\\'], ['/', '/'], $cand2);
if (strpos(realpath(dirname($norm)) ?: '', realpath(UPLOADS_DIR)) === 0) return $norm;
return null;
}
function pick_entry_by_path(array $data, string $path) {
foreach ($data as $e)
if ((string)($e['path'] ?? '') === $path) return $e;
return null;
}
function header_sane_filename(string $fn): string {
$base = basename($fn);
return str_replace(['"', "\r", "\n"], ['\'', '', ''], $base);
}
$path = trim((string)($_GET['path'] ?? ''));
if ($path === '') {
http_response_code(400);
exit('Missing path');
}
$data = read_json(FILEDATA_JSON, []);
$entry = pick_entry_by_path($data, $path);
if (!$entry) {
http_response_code(404);
exit('Not found');
}
$abs = safe_uploads_join($path);
if (!$abs || !is_file($abs)) {
http_response_code(404);
exit('File missing');
}
$filename = (string)($entry['name'] ?? basename($abs));
$size = filesize($abs);
$mime = 'application/octet-stream';
if (function_exists('mime_content_type')) {
$m = mime_content_type($abs);
if ($m) $mime = $m;
}
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header('Content-Disposition: attachment; filename="' . header_sane_filename($filename) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $size);
header('Cache-Control: private, no-transform, no-store, must-revalidate');
$fp = fopen($abs, 'rb');
if ($fp) {
while (!feof($fp)) {
echo fread($fp, 8192);
@ob_flush();
flush();
}
fclose($fp);
}
exit;
?>