-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
185 lines (160 loc) · 5.53 KB
/
index.php
File metadata and controls
185 lines (160 loc) · 5.53 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
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
<?php
// RemoteDownloaderPHP — https://github.com/BaseMax/RemoteDownloaderPHP
// Developed by Max Base (Seyyed Ali Mohammadiyeh)
// === CONFIGURATION ===
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
ini_set('zlib.output_compression', 'Off');
ob_implicit_flush(true);
while (ob_get_level()) ob_end_flush();
define('LOG_FILE', __DIR__ . '/downloader.log');
define('USER_AGENT', 'RemoteDownloaderPHP/1.1');
define('DOWNLOAD_DIR', __DIR__ . '/downloads');
if (!is_dir(DOWNLOAD_DIR)) {
mkdir(DOWNLOAD_DIR, 0755, true);
}
function log_message($msg) {
file_put_contents(LOG_FILE, "[" . date('Y-m-d H:i:s') . "] $msg\n", FILE_APPEND);
}
function sanitize_filename($name) {
return preg_replace('/[^a-zA-Z0-9\._-]/', '_', basename($name));
}
function get_remote_headers($url) {
$head = @get_headers($url, 1);
if (!$head || (!isset($head[0]) || !preg_match('/HTTP\/\d\.\d\s(200|301|302)/', $head[0]))) {
return false;
}
return $head;
}
// === INPUT HANDLING ===
$url = trim($_REQUEST['url'] ?? '');
$confirm = isset($_POST['confirm']);
$customName = trim($_POST['filename'] ?? '');
if (!$url) {
echo <<<HTML
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>RemoteDownloaderPHP</title>
<style>
body{font-family:sans-serif;padding:2em;}
input,button{padding:0.5em;width:90%;margin:0.5em 0;}
</style></head><body>
<h1>RemoteDownloaderPHP</h1>
<form method="POST">
<input name="url" placeholder="https://example.com/file.zip" required />
<input name="filename" placeholder="Optional custom filename" />
<button type="submit">Check & Continue</button>
</form>
<footer style="margin-top:3em; font-size:0.8em; color:#666; text-align:center;">
<hr>
<p>🔗 <a href="https://github.com/BaseMax/RemoteDownloaderPHP" target="_blank" rel="noopener noreferrer">RemoteDownloaderPHP on GitHub</a></p>
</footer>
</body></html>
HTML;
exit;
}
// === FETCH HEADERS ===
$headers = get_remote_headers($url);
if (!$headers) {
echo "❌ Invalid or unreachable URL.";
log_message("Invalid URL: $url");
exit;
}
$type = $headers['Content-Type'] ?? 'unknown';
if (is_array($type)) {
$type = end($type);
}
$lengthBytes = $headers['Content-Length'] ?? null;
if (is_array($lengthBytes)) {
$lengthBytes = end($lengthBytes);
}
$sizeReadable = $lengthBytes ? round($lengthBytes / 1024 / 1024, 2) . ' MB' : 'unknown';
$acceptRanges = (isset($headers['Accept-Ranges']) && stripos($headers['Accept-Ranges'], 'bytes') !== false);
$finalFilename = sanitize_filename($customName ?: basename(parse_url($url, PHP_URL_PATH)) ?: 'downloaded_file');
if (!$confirm) {
echo <<<HTML
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Confirm Download</title>
<style>
body{font-family:sans-serif;padding:2em;}
button{padding:0.5em 1em;margin-top:1em;}
</style>
</head><body>
<h2>Confirm Remote Download</h2>
<p><strong>URL:</strong> {$url}</p>
<p><strong>Type:</strong> {$type}</p>
<p><strong>Size:</strong> {$sizeReadable}</p>
<p><strong>Resume Supported:</strong> {$acceptRanges}</p>
<p><strong>Filename:</strong> {$finalFilename}</p>
<form method="POST">
<input type="hidden" name="url" value="{$url}" />
<input type="hidden" name="filename" value="{$finalFilename}" />
<input type="hidden" name="confirm" value="1" />
<button type="submit">Start Download</button>
</form>
<footer style="margin-top:3em; font-size:0.8em; color:#666; text-align:center;">
<hr>
<p>🔗 <a href="https://github.com/BaseMax/RemoteDownloaderPHP" target="_blank" rel="noopener noreferrer">RemoteDownloaderPHP on GitHub</a></p>
</footer>
</body></html>
HTML;
exit;
}
// === INITIATE DOWNLOAD TO SERVER ===
log_message("Download started from $url as $finalFilename");
$filePath = DOWNLOAD_DIR . '/' . $finalFilename;
$fp = fopen($filePath, 'wb');
if (!$fp) {
echo "❌ Cannot open file for writing: $filePath";
log_message("Cannot open file for writing: $filePath");
exit;
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_TIMEOUT => 0,
CURLOPT_FILE => $fp,
CURLOPT_BUFFERSIZE => 1024 * 1024,
CURLOPT_USERAGENT => USER_AGENT,
CURLOPT_FAILONERROR => true
]);
curl_exec($ch);
if (curl_errno($ch)) {
$err = curl_error($ch);
log_message("❌ cURL error: $err");
fclose($fp);
$incompletePath = DOWNLOAD_DIR . '/not-complete-' . basename($filePath);
if (file_exists($filePath)) {
rename($filePath, $incompletePath);
log_message("Renamed incomplete file to: " . basename($incompletePath));
}
curl_close($ch);
exit;
} else {
log_message("✅ Download completed: $finalFilename");
}
curl_close($ch);
fclose($fp);
// === SHOW SUCCESS PAGE ===
echo <<<HTML
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Download Complete</title>
<style>
body{font-family:sans-serif;padding:2em;}
a.button {
display: inline-block; padding: 0.5em 1em; margin-top: 1em;
background: #28a745; color: white; text-decoration: none; border-radius: 4px;
}
</style>
</head><body>
<h2>Download Completed Successfully</h2>
<p>File saved on server as: <strong>{$finalFilename}</strong></p>
<p><a href="downloads/{$finalFilename}" class="button" target="_blank" rel="noopener noreferrer">Download file from server</a></p>
<footer style="margin-top:3em; font-size:0.8em; color:#666; text-align:center;">
<hr>
<p>🔗 <a href="https://github.com/BaseMax/RemoteDownloaderPHP" target="_blank" rel="noopener noreferrer">RemoteDownloaderPHP on GitHub</a></p>
</footer>
</body></html>
HTML;
exit;