-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.php
368 lines (342 loc) · 16.4 KB
/
upload.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<?php
/*
* @Author: Amirhossein Hosseinpour <https://amirhp.com>
* @Date Created: 2020/11/15 19:31:15
* @Last modified by: amirhp-com <[email protected]>
* @Last modified time: 2025/01/08 14:08:56
*/
@ini_set("display_errors", 1);
error_reporting(E_ERROR);
?>
<html>
<head>
<title>Upload File from URL to WebServer v14</title>
<style>
* {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
font-weight: 300;
font-size: 16px;
}
#progress {
font-size: 2rem;
margin: 1rem;
font-family: Calibria;
font-weight: 800;
}
form {
margin-top: 1rem;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
width: 100%
}
form>* {
align-self: center;
margin-bottom: 1rem;
min-width: 250px
}
form div>span {
font-size: small;
position: absolute;
top: -9px;
background: white;
display: block;
padding: 0 5px !important;
text-align: left;
left: 6px;
text-transform: lowercase;
font-family: arial;
color: #0060dfbd;
}
input:invalid {
background-image: linear-gradient(45deg, transparent, transparent 50%, #df0000 50%, #ff7a7a 100%) !important;
border-color: #df0000 !important;
}
input[required] {
outline: none !important;
background-image: linear-gradient(45deg, transparent, transparent 50%, #0060df 50%, #61a5ff 100%);
background-position: top right;
background-size: 1rem 1rem;
background-repeat: no-repeat
}
h1.aw {
font-weight: 900;
font-size: 3rem;
margin: 0;
color: #000;
}
span.dw {
color: #222;
font-size: 1.3rem;
display: block;
margin: 0 0 -4rem 0;
font-weight: normal;
}
body.uffutw {
text-transform: lowercase;
font-family: Calibri;
color: #222;
text-align: center;
margin-top: 5rem;
}
small.red-alert {
display: block;
width: 345px;
margin: auto;
}
.red-alert,
.red-alert a {
text-transform: uppercase;
font-weight: 800;
color: #e32121;
}
</style>
<link rel="icon" href="https://raw.githubusercontent.com/amirhp-com/upload-file-from-url-to-webserver/main/blackswan.png" sizes="32x32" />
<link rel="icon" href="https://raw.githubusercontent.com/amirhp-com/upload-file-from-url-to-webserver/main/blackswan.png" sizes="192x192" />
</head>
<body class="uffutw upload-file-from-url-to-webserver v-14">
<h1 class="aw">
<div style="background: url('https://raw.githubusercontent.com/amirhp-com/upload-file-from-url-to-webserver/main/blackswan.png') no-repeat center/contain;padding-top: 5rem;margin-bottom: 0.5rem;"></div>
<span class="dw">BlackSwan Upload File from URL to Web Server v.14</span><br>URL-Address to WebServer</h1>
<?php
if (isset($_GET["delete"]) && "true" == $_GET["delete"]) {
unlink(__FILE__);
die('<h3 class="red-alert" style="font-size: 1.5rem;">SELF-DESTRUCTION WAS SUCCESSFUL, BYE 👋!</h3>');
}
?>
<small>Developed by <a href="https://amirhp.com/" target="_blank">amirhp-com</a> | Give star on <a href="https://github.com/amirhp-com/upload-file-from-url-to-webserver" target="_blank">Github</a> | your_ip: <?= get_real_IP_address(); ?> [ <a href="./">root</a> / <a href="?r=<?= time(); ?>">new?</a> ]</small>
<br><br>
<?php
if (isset($_POST['url'], $_POST["name"])) {
set_time_limit(24 * 60 * 60);
$url = $_POST["url"];
$name = $_POST["name"];
$folder = $_POST["folder"] ?? "";
$folder = !empty($folder) ? rtrim(ltrim($folder, '/\\'), '/\\') : "";
ob_implicit_flush(true);
ob_start();
$server = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]";
echo "<script>
document.title = 'Uploading ...';
document.querySelector('h1').innerHTML += `
<div style=\"font-size: 0;margin-top: 3rem;\"><small style=\"font-size: 1rem;\">
Transferring <a href='$url' target='_blank'>Origin URL-Address</a> as <a href='{$server}/".(!empty($folder)?"{$folder}/":"")."{$name}' target='_blank'><strong>".(!empty($folder)?"{$folder}/":"")."{$name}</strong></a><br>
<span style=\"margin-top: 1rem;display: block;\">Please wait until process complete or Press ESC key to cancel</span></small></div>
<div id=\"progress\"></div>`;</script>";
$time = microtime(true);
$remote = fopen($url, 'r');
if (empty($folder)) { $folder = __DIR__; }
if (!is_dir($folder)) mkdir($folder, 0777, true);
$path_full = $folder . "/" . $name;
if (file_exists($path_full)) @unlink($path_full);
$local = fopen($path_full, 'w');
stream_context_set_default(array('http' => array('method' => 'HEAD')));
$headers = get_headers($url, true);
if (!$headers) {
echo "Force uploading fot NON-SSL Servers/Direct Admin hosts ...";
ob_end_flush();
ob_flush();
flush();
$ch = curl_init($url);
$fp = fopen($path_full, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<pre style='text-align: left; direction: ltr; border:1px solid gray; padding: 1rem; overflow: auto;'>" . print_r($fp, 1) . "</pre>";
die("<br>Upload complete!");
}
$headers = array_change_key_case(get_headers($url, 1));
$filesize = $headers['content-length'];
ob_end_flush(); ob_flush(); flush(); ob_start();
if ($filesize < 1) {
echo "<pre style='text-align: left; direction: ltr; border:1px solid #1e45cf; padding: 1rem; color: #1e45cf;'>" . print_r($headers, 1) . "</pre>";
echo "<pre style='text-align: left; direction: ltr; border:1px solid #c34f1e; padding: 1rem; color: #c34f1e;'>" . print_r($_SERVER, 1) . "</pre>";
exit;
}
$read_bytes = 0; $num = 0; $progress = 1;
$steps = floor($filesize / 2048) / 300 < 0 ? 3 : floor($filesize / 2048) / 300;
echo "<script>
document.title = 'Uploading " . sprintf("%'05.2f%%", $progress) . "';
document.querySelector('#progress').innerHTML = '( " . sprintf("%'05.2f%%", $progress) . " — " . human_filesize($filesize) . " )<br><small style=\"text-transform: capitalize;\">Elapsed Time: " . human_timing($time) . "</small>';
document.querySelector('body').style.backgroundImage = 'linear-gradient(to right, rgba(0, 223, 56, 0.18) $progress%, white $progress%)';
</script>";
while (!feof($remote)) {
$buffer = fread($remote, 2048);
fwrite($local, $buffer);
$read_bytes += 2048;
$num++;
if ($num > 4 && $steps > 1) {
if ($num % $steps == 0) {
$progress = min(100, 100 * $read_bytes / $filesize);
echo "<script>
document.title = 'Uploading " . sprintf("%'05.2f%%", $progress) . "';
document.querySelector('#progress').innerHTML = '( " . sprintf("%'05.2f%%", $progress) . " — " . human_filesize($read_bytes) . " / " . human_filesize($filesize) . " )<br><small style=\"text-transform: capitalize;\">Elapsed Time: " . human_timing($time) . "</small>';
document.querySelector('body').style.backgroundImage = 'linear-gradient(to right, rgba(0, 223, 56, 0.18) $progress%, white $progress%)';
</script>";
}
}
ob_end_flush(); ob_flush(); flush(); ob_start();
}
fclose($remote);
fclose($local);
$should_extract = "https://wordpress.org/latest.zip" == $url;
echo "<script>
document.title = 'Transferring Done!';
document.querySelector('#progress').innerHTML = '( 100.00% — " . human_filesize($filesize) . " / " . human_filesize($filesize) . " )';
document.querySelector('body').style.backgroundImage = 'linear-gradient(to right, rgba(0, 223, 56, 0.18) $progress%, white $progress%)';
document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Transferring done successfully in ".human_timing($time).($should_extract ? ", wait ..." : ".")."</small></div>';
</script>";
ob_end_flush(); ob_flush(); flush();
if ($should_extract) {
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⏳ PLEASE WAIT, Extracting Wordpress ZIP archive ...</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
$rootDir = __DIR__;
$zipFile = $path_full;
$tempDir = $rootDir . '/temp/wordpress';
$continue = false;
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
$zip->extractTo($rootDir . '/temp');
$zip->close();
$continue = true;
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ WordPress ZIP file extracted successfully.</small></div>';</script>";
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⏳ PLEASE WAIT, Moving wordpress files & folders to root ...</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
} else {
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Error: Failed to extract ZIP file ({$local}).</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
}
if ($continue) {
if (!is_dir($tempDir)) {
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Error: 'wordpress' folder not found in the ZIP file.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
}
// Step 3: Move files from the 'wordpress' folder to the root directory
$files = scandir($tempDir); $jf = 0; $jfl = 0;
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$source = $tempDir . '/' . $file;
$destination = $rootDir . '/' . $file;
// Move the file or directory
if (is_dir($source)) {
$jfl++; rename($source, $destination);
} else {
$jf++; rename($source, $destination);
}
}
}
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ <strong>".number_format($jf)."</strong> Files & <strong>".number_format($jfl)."</strong> Folders moved successfully to the root directory.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
// Step 4: Remove the temporary folder
$tempPath = $rootDir . '/temp';
if (is_dir($tempPath)) {
if (rmdir($tempPath)) {
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Temporary 'temp' folder removed successfully.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
} else {
// Fallback to recursive deletion if the folder isn't empty
rmdir_recursive($tempPath);
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Temporary 'temp' folder removed recursively.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
}
}else{
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">⛔️ Could not remove Temporary 'temp' folder.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
}
// Step 5: Optionally, delete the ZIP file
if (file_exists($zipFile)) {
@unlink($zipFile);
@unlink($folder . "/license.txt");
@unlink($folder . "/readme.html");
@unlink($folder . "/xmlrpc.php");
echo "<script>document.querySelector('h1').innerHTML += '<div style=\"margin-bottom: 0.5rem;\"><small style=\"font-size: 1rem;\">✅ Uploaded ZIP file deleted, extra WordPress files deleted.</small></div>';</script>";
ob_end_flush(); ob_flush(); flush(); ob_start();
}
}
}
ob_end_flush();ob_flush(); flush();
echo "<script>
document.querySelector('h1').innerHTML += `
<div style=\"font-size: 0;margin: 2rem; padding:1rem;\">
<small style=\"font-size: 1rem;\">
<a href=\"?r=" . time() . "\" style=\"background: #0060df;color: white;text-decoration: none; padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;\">Upload another file</a>
<a href=\"?delete=true\" style=\"background:#df0025;color: white;text-decoration: none; padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;\">Self Destruct?</a>
</small>
</div>`;</script>";
echo "all done ./";
exit;
}
?>
<form style="margin-top: 1rem;" name='upload' method='post' action="<?php echo strtok($_SERVER['REQUEST_URI'], '?'); ?>">
<div style='position: relative;'>
<span>Origin URL-Address</span>
<input type="url" id="url" onclick="this.select();" autofocus tabindex="1" required="required" name='url' style="min-width: 500px;padding: 0.5rem;border-radius: 3px;border: 1px solid #0060df;" value="https://wordpress.org/latest.zip" placeholder="set input url" />
</div>
<br>
<div style='position: relative;'>
<span>Destination Folder</span>
<input type="text" id="folder" tabindex="2" name='folder' style="min-width: 500px;padding: 0.5rem;border-radius: 3px;border: 1px solid #0060df;" value="" placeholder="docs/etc" />
</div>
<br>
<div style='position: relative;'>
<span>Destination Filename</span>
<input type="text" id="name" tabindex="3" required="required" name='name' style="min-width: 500px;padding: 0.5rem;border-radius: 3px;border: 1px solid #0060df;" value="wordpress_latest.zip" placeholder="set file name" />
</div>
<div class="form-wrapper">
<input type="submit" value="upload" tabindex="4" style="background: #0060df;color: white;padding: 0.5rem 2rem;border: none;border-radius: 3px;box-shadow: 0 0 8px -3px #00000069;margin: 0 1rem;cursor: pointer;">
</div>
</form>
<script type="text/javascript">
setTimeout(function() { document.querySelector("#url").select(); }, 200);
document.querySelector("#url").onchange = function() { document.querySelector("#name").value = document.querySelector("#url").value.split('/').pop(); };
document.querySelector("#url").onkeyup = function() { document.querySelector("#name").value = document.querySelector("#url").value.split('/').pop(); };
</script>
<small class="red-alert">DO NOT KEEP THIS FILE ON YOUR SERVER, DELETE IT AS SOON AS YOU'RE DONE. <a href='?delete=true'>[🗑️]</a></small>
</body>
</html>
<?php
function get_real_IP_address() {
if (!empty($_SERVER['GEOIP_ADDR'])) {
$ip = $_SERVER['GEOIP_ADDR'];
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
$ip = $_SERVER['HTTP_X_REAL_IP'];
} elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function human_filesize($bytes = 0, $decimals = 2) {
if (!$bytes || $bytes < 1) { return "ERR"; }
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%'05.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
function human_timing($time) {
$s = microtime(true) - $time;
$h = floor($s / 3600);
$s -= $h * 3600;
$m = floor($s / 60);
$s -= $m * 60;
return ($h > 0 ? "$h:" : "") . sprintf('%02d', $m) . ':' . sprintf('%02d', $s);
}
function rmdir_recursive($dir) {
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . '/' . $file;
is_dir($path) ? rmdir_recursive($path) : unlink($path);
}
return rmdir($dir);
}
/*##################################################
Lead Developer: [amirhp-com](https://amirhp.com/)
##################################################*/