-
Notifications
You must be signed in to change notification settings - Fork 0
/
camo.php
196 lines (166 loc) · 4.37 KB
/
camo.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
<?php
// Shared key
define('SERVER_KEY', '24FEEDFACEDEADBEEFCAFE');
/**
* Get requested image URL
* @param string $key Used shared key
* @return string Image URL
*/
function get_camo_requested_url($key)
{
$requestedUrl = null;
if(isset($_SERVER['PATH_INFO']) || !empty($_SERVER['REDIRECT_URL'])) {
$path = explode('/', isset($_SERVER['PATH_INFO']) ?
$_SERVER['PATH_INFO'] : $_SERVER['REDIRECT_URL']);
$hash = $path[1];
if(!empty($_GET['url'])) {
$url = $_GET['url'];
}
elseif(!empty($path[2])) {
$url = hex2bin($path[2]);
}
if(!empty($hash) && !empty($url)) {
$check = hash_hmac('sha1', $url, $key);
if($hash === $check) {
$requestedUrl = $url;
}
}
}
return $requestedUrl;
}
/**
* Get headers received from client
* @param array $names Header keys to retreive
* @return array Headers
*/
function get_request_headers(array $names)
{
$headers = array();
foreach ($names as $name) {
$serverCode = strtoupper($name);
$serverCode = str_replace('-', '_', $serverCode);
if(!empty($_SERVER['HTTP_' . $serverCode])) {
$headers[$name] = $_SERVER['HTTP_' . $serverCode];
}
}
return $headers;
}
/**
* Open HTTP stream
* @param string $url Stream URI
* @param array|null $headers Headers to send
* @return Resource HTTP Stream
*/
function open_http_stream($url, array $headers = null)
{
$stream = null;
if(is_null($headers)) {
$headers = array();
}
$sentHeaders = array('http' => array('header' =>
headers_key_to_value($headers)
));
$context = stream_context_create($sentHeaders);
$stream = fopen($url, 'r', false, $context);
return $stream;
}
/**
* Transform key-value associated array to header formatted rows
* @param array $headers Array by 'Key' => 'value'
* @return array Array by 'Key: value'
*/
function headers_key_to_value(array $headers)
{
return array_map(
function($k, $v) { return $k.': '.$v; },
array_keys($headers), $headers
);
}
/**
* Get HTTP headers returned by stream
* @param Resource $stream HTTP stream
* @return array HTTP headers
*/
function get_http_stream_headers($stream)
{
$headers = array();
$metadata = stream_get_meta_data($stream);
$status = $metadata['wrapper_data'][0];
$statusSplitted = explode(' ', $status);
$statusCode = intval($statusSplitted[1]);
$headers['Status'] = $status;
$headers['Status-Code'] = $statusCode;
foreach ($metadata['wrapper_data'] as $line) {
$row = explode(': ', $line);
if(count($row) > 1) {
$headers[array_shift($row)] = implode(': ', $row);
}
}
return $headers;
}
/**
* Send headers to client
* @param array $headers HTTP headers
* @param array|null $filters Headers keys to send (if available in $headers)
*/
function pass_headers(array $headers, array $filters = null)
{
if (!is_null($filters)) {
$headers = array_filter($headers, function($k) use ($filters) {
return in_array($k, $filters);
}, ARRAY_FILTER_USE_KEY);
}
$sentHeaders = headers_key_to_value($headers);
foreach ($sentHeaders as $header) {
header($header);
}
}
$url = get_camo_requested_url(SERVER_KEY);
if(!empty($url)) {
// Custom headers
$sentHeaders = array(
'Accept' => 'image/*',
'Connection' => 'close',
'X-Frame-Options' => 'deny',
'X-XSS-Protection' => '1; mode=block',
'X-Content-Type-Options' => 'nosniff',
'Content-Security-Policy' => 'default-src \'none\'; img-src data:; style-src \'unsafe-inline\''
);
$receivedHeaders = get_request_headers(array(
'Accept-Encoding', 'If-Modified-Since', 'If-None-Match'
));
$sentHeaders = array_merge($sentHeaders, $receivedHeaders);
// Get HTTP request
$stream = open_http_stream($url, $sentHeaders);
$headers = get_http_stream_headers($stream);
// If status is OK, returns the content and headers
if($headers['Status-Code'] === 200) {
if(strpos($headers['Content-Type'], 'image/') === 0) {
$headers['Cache-Control'] = 'public, max-age=31536000';
pass_headers($headers, array(
'Content-Type',
'Cache-Control',
'ETag',
'Expires',
'Last-Modified',
'Content-Encoding'
));
fpassthru($stream);
}
else {
header('HTTP/1.1 403 Forbidden');
readfile('403.html');
}
}
// Otherwise, only returns status header
else {
header($headers['Status']);
}
fclose($stream);
}
// If we cannot retreive the requested URL
else {
header('HTTP/1.1 404 Not Found');
echo 'Error 404 - Not found';
// readfile('404.html');
}