Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit b499b59

Browse files
committed
Rework curl part of download.php
1 parent f7c1fac commit b499b59

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

Download.php

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<?php
2-
function getCURL(string $id, string $password) {
3-
# API Download URL
4-
$downloadURL = filter_input(INPUT_ENV, 'TMP_API_DOWNLOAD_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://api.tempfiles.download/download/?id=%1$s&p=%2$s']]);
5-
$d_url = sprintf($downloadURL, $id, $password);
6-
7-
$curl = curl_init();
8-
curl_setopt_array($curl, [
9-
CURLOPT_URL => $d_url,
10-
CURLOPT_RETURNTRANSFER => TRUE,
11-
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2,
12-
CURLOPT_CUSTOMREQUEST => 'GET',
13-
CURLOPT_HTTPHEADER => [
14-
"cache-control: no-cache"
15-
]
16-
]);
17-
return $curl;
2+
3+
function download($file_source, $file_target) {
4+
$rh = fopen($file_source, 'rb');
5+
$wh = fopen($file_target, 'w+b');
6+
if (!$rh || !$wh) {
7+
return false;
8+
}
9+
10+
while (!feof($rh)) {
11+
if (fwrite($wh, fread($rh, 4096)) === FALSE) {
12+
return false;
13+
}
14+
echo '';
15+
flush();
16+
}
17+
18+
fclose($rh);
19+
fclose($wh);
20+
21+
return true;
1822
}
1923

2024
function return404() {
@@ -28,16 +32,20 @@ function return404() {
2832
$id = filter_var($url[1]);
2933
$password = filter_input(INPUT_GET, "p");
3034

31-
$curl = getCURL($id, $password);
35+
# API Download URL
36+
$downloadURL = filter_input(INPUT_ENV, 'TMP_API_DOWNLOAD_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://api.tempfiles.download/download/?id=%1$s&p=%2$s']]);
37+
$d_url = sprintf($downloadURL, $id, $password);
38+
39+
$result = download($d_url, "/tmp/$id.tmp");
40+
if (!$result)
41+
return404();
3242

3343
// Execute cURL command and get response data
34-
$response = json_decode(curl_exec($curl));
44+
$response = json_decode(curl_exec(file_get_contents("/tmp/$id.tmp")));
45+
46+
unlink("/tmp/$id.tmp");
47+
3548

36-
$error = curl_error($curl);
37-
if ($error !== "") {
38-
error_log($error);
39-
return404();
40-
}
4149

4250
if ($response->data) {
4351

0 commit comments

Comments
 (0)