-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcors-proxy.php
64 lines (51 loc) · 1.91 KB
/
cors-proxy.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
<?php
session_start();
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
die('Unauthorized request');
}
if (isset($_GET['url'])) {
$url = filter_var($_GET['url'], FILTER_SANITIZE_URL);
// Check if the URL is for the short URL API and append the signature
if (strpos($url, 'https://s.sternenlabor.de/public-api.php?action=shorturl&format=json') === 0) {
$signature = 'efaf50b091';
$url .= '&signature=' . $signature;
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid URL']);
exit;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // Get headers
curl_setopt($ch, CURLOPT_NOBODY, false); // Also get body
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
http_response_code(500);
echo json_encode(['error' => 'Request Error:' . curl_error($ch)]);
} else {
// Set the HTTP status code
http_response_code($httpCode);
// Separate headers and body
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
// Pass through the headers
$headers = explode("\r\n", $header);
foreach ($headers as $header) {
if (!empty($header) && strpos($header, ':') !== false) {
header($header);
}
}
// Output the body
echo $body;
}
curl_close($ch);
} else {
http_response_code(400);
echo json_encode(['error' => 'No URL provided']);
}