-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathproxy.php
More file actions
116 lines (94 loc) · 3.37 KB
/
Copy pathproxy.php
File metadata and controls
116 lines (94 loc) · 3.37 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
require __DIR__ . '/vendor/autoload.php';
use Dotenv\Dotenv;
use GuzzleHttp\Client;
if (file_exists(__DIR__ . '/.env')) {
Dotenv::createImmutable(__DIR__)->safeLoad();
}
$mediaUrl = $_POST['file'] ?? $_GET['file'] ?? null;
if (!$mediaUrl || !filter_var($mediaUrl, FILTER_VALIDATE_URL)) {
http_response_code(400);
echo json_encode(['status' => false, 'message' => 'Invalid or missing media URL parameter.'], JSON_PRETTY_PRINT);
exit;
}
// Validate host domain for security (only allow Instagram & Facebook CDN URLs)
$parsed = parse_url($mediaUrl);
$host = strtolower($parsed['host'] ?? '');
$allowedHostSuffixes = [
'cdninstagram.com',
'fbcdn.net',
'instagram.com',
'igcdn.com',
];
$isAllowed = false;
foreach ($allowedHostSuffixes as $suffix) {
if ($host === $suffix || str_ends_with($host, '.' . $suffix)) {
$isAllowed = true;
break;
}
}
if (!$isAllowed) {
http_response_code(403);
echo json_encode(['status' => false, 'message' => 'Access denied: URL domain is not a valid Instagram media provider.'], JSON_PRETTY_PRINT);
exit;
}
function cleanupTempDir(string $dir, int $maxAgeSeconds = 3600): void
{
if (!is_dir($dir)) {
return;
}
$now = time();
$files = glob($dir . '/*');
if ($files) {
foreach ($files as $file) {
if (is_file($file) && ($now - filemtime($file) > $maxAgeSeconds)) {
@unlink($file);
}
}
}
}
function mediaProxy(string $url): void
{
$tempDir = __DIR__ . '/temp';
if (!is_dir($tempDir)) {
mkdir($tempDir, 0755, true);
}
cleanupTempDir($tempDir);
// Generate safe, deterministic extension and filename hash
$extension = 'jpg';
$pathWithoutQuery = strtok($url, '?');
$ext = pathinfo($pathWithoutQuery, PATHINFO_EXTENSION);
if (in_array(strtolower($ext), ['mp4', 'webm', 'jpg', 'jpeg', 'png', 'webp'])) {
$extension = strtolower($ext);
}
$fileName = md5($url) . '.' . $extension;
$filePath = $tempDir . '/' . $fileName;
$relativePath = 'temp/' . $fileName;
if (!file_exists($filePath)) {
$proxyHost = $_ENV['PROXY_HOST'] ?? getenv('PROXY_HOST') ?? '';
$proxyPort = $_ENV['PROXY_PORT'] ?? getenv('PROXY_PORT') ?? '';
$proxy = (!empty($proxyHost) && !empty($proxyPort)) ? "http://{$proxyHost}:{$proxyPort}" : null;
$clientOptions = [
'timeout' => 20.0,
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
],
'sink' => $filePath,
];
if ($proxy) {
$clientOptions['proxy'] = $proxy;
}
try {
$client = new Client();
$client->request('GET', $url, $clientOptions);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['status' => false, 'message' => 'Failed to proxy media file: ' . $e->getMessage()], JSON_PRETTY_PRINT);
exit;
}
}
echo json_encode(['status' => true, 'path' => $relativePath], JSON_PRETTY_PRINT);
}
mediaProxy($mediaUrl);