summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2023-05-08 15:44:30 +0200
committerSimon Rettberg2023-05-08 15:44:30 +0200
commit9d1ab886b470e2ae029e35e05b8ae4ab229f238d (patch)
tree2db57e9a77346ecf055b01042f0239b48fe40522 /inc
parent[backup] i18n: Fix typo (diff)
downloadslx-admin-9d1ab886b470e2ae029e35e05b8ae4ab229f238d.tar.gz
slx-admin-9d1ab886b470e2ae029e35e05b8ae4ab229f238d.tar.xz
slx-admin-9d1ab886b470e2ae029e35e05b8ae4ab229f238d.zip
[inc/Download] Clean up code
Diffstat (limited to 'inc')
-rw-r--r--inc/download.inc.php47
1 files changed, 7 insertions, 40 deletions
diff --git a/inc/download.inc.php b/inc/download.inc.php
index 3986bc40..10eac188 100644
--- a/inc/download.inc.php
+++ b/inc/download.inc.php
@@ -13,7 +13,7 @@ class Download
* Return file handle to header file
* @return false|resource
*/
- private static function initCurl(string $url, int $timeout, &$head)
+ private static function initCurl(string $url, int $timeout)
{
if (self::$curlHandle === false) {
self::$curlHandle = curl_init();
@@ -30,42 +30,19 @@ class Download
curl_setopt(self::$curlHandle, CURLOPT_URL, $url);
- $tmpfile = tempnam('/tmp/', 'bwlp-');
- $head = fopen($tmpfile, 'w+b');
- unlink($tmpfile);
- if ($head === false)
- ErrorHandler::traceError("Could not open temporary head file $tmpfile for writing.");
- curl_setopt(self::$curlHandle, CURLOPT_WRITEHEADER, $head);
return self::$curlHandle;
}
/**
- * Read 10kb from the given file handle, seek to 0 first,
- * close the file after reading. Returns data read
- */
- private static function getContents($fh)
- {
- fseek($fh, 0, SEEK_SET);
- $data = fread($fh, 10000);
- fclose($fh);
- return $data;
- }
-
- /**
* Download file, obey given timeout in seconds
* Return data on success, false on failure
*/
public static function asString(string $url, int $timeout, &$code)
{
- $ch = self::initCurl($url, $timeout, $head);
+ $ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
- $head = self::getContents($head);
- if (preg_match_all('#^HTTP/\d+\.\d+ (\d+) #m', $head, $out)) {
- $code = (int) array_pop($out[1]);
- } else {
- $code = 999;
- }
+ $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
return $data;
}
@@ -88,17 +65,12 @@ class Download
$string .= $k . '=' . urlencode($v);
}
}
- $ch = self::initCurl($url, $timeout, $head);
+ $ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
$data = curl_exec($ch);
- $head = self::getContents($head);
- if (preg_match_all('#^HTTP/\d+\.\d+ (\d+) #m', $head, $out)) {
- $code = (int) array_pop($out[1]);
- } else {
- $code = 999;
- }
+ $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
return $data;
}
@@ -116,20 +88,15 @@ class Download
$fh = fopen($target, 'wb');
if ($fh === false)
ErrorHandler::traceError("Could not open $target for writing.");
- $ch = self::initCurl($url, $timeout, $head);
+ $ch = self::initCurl($url, $timeout);
curl_setopt($ch, CURLOPT_FILE, $fh);
$res = curl_exec($ch);
- $head = self::getContents($head);
+ $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
fclose($fh);
if ($res === false) {
@unlink($target);
return false;
}
- if (preg_match_all('#^HTTP/\d+\.\d+ (\d+) #m', $head, $out)) {
- $code = (int) array_pop($out[1]);
- } else {
- $code = '999 ' . curl_error($ch);
- }
return true;
}