-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathembed.php
59 lines (43 loc) · 1.27 KB
/
embed.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
$file = @$_GET['file'] ?: '';
if (!$file) {
http_response_code(400);
die('No file specified.');
}
$pos = strrpos($file, '/');
if ($pos) $file = substr($url, $pos);
$regex = "/^[^\/.]+\.(png|jpe?g|gif|webp)$/";
if (!preg_match($regex, $file)) {
http_response_code(400);
die('Invalid filename.');
}
if (!file_exists($file)) {
http_response_code(404);
die('File not found.');
}
function human_filesize($bytes, $decimals = 2) {
$sz = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
$size = human_filesize(filesize($file));
$url = 'http'.(isset($_SERVER['HTTPS']) ? 's' : '')."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($url, $_SERVER['PHP_SELF'])) {
$url = substr($url, 0, strrpos($url, '/')) . "/$file";
}
$url = explode('?', $url)[0].'?raw';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?=$file?></title>
<meta name="theme-color" content="#7289DA">
<meta name="twitter:card" content="summary_large_image">
<meta property="og:title" content="<?="$file ($size)"?>" />
<meta property="og:image" content="<?=$url?>" />
</head>
<body>
Metadata set, check HTML <head> for details.
</body>
</html>