-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.php
More file actions
74 lines (58 loc) · 1.82 KB
/
proxy.php
File metadata and controls
74 lines (58 loc) · 1.82 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
<?php
if (!isset($_GET['uri'])) {
throw new \Exception('No URI given', 1527067951);
}
$allowedHosts = [
'*.ytimg.com',
'*.googleapis.com'
];
$uri = $_GET['uri'];
$host = parse_url($uri, PHP_URL_HOST);
$hostAllowed = false;
if ($host !== false) {
foreach ($allowedHosts as $allowedHost) {
if ($allowedHost === '*') {
$hostPattern = '/.+/';
} else {
$hostParts = explode('.', $allowedHost);
if (count($hostParts) < 2) {
continue;
}
$firstPart = array_shift($hostParts);
$lastPart = array_pop($hostParts);
array_walk($hostParts, function(&$val) {
$val = preg_quote($val, '/');
});
if ($lastPart === '*') {
$hostParts[] = '[^\.]+';
} else {
$hostParts[] = preg_quote($lastPart, '/');
}
if (count($hostParts) >= 2 && $firstPart === '*') {
$hostParts[0] = '(?:[^\.]+\.)*' . $hostParts[0];
} else {
array_unshift($hostParts, preg_quote($firstPart, '/'));
}
$hostPattern = '/' . implode('\.', $hostParts) . '/i';
}
if (preg_match($hostPattern, $host) === 1) {
$hostAllowed = true;
break;
}
}
}
if (!$hostAllowed) {
throw new \Exception('Disallowed host', 1527666194);
}
header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if ($contentType !== false && $contentType !== null) {
header('Content-Type: ' . $contentType);
}
curl_close($ch);
echo $body;