From 9d1ab886b470e2ae029e35e05b8ae4ab229f238d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 8 May 2023 15:44:30 +0200 Subject: [inc/Download] Clean up code --- inc/download.inc.php | 47 +++++++---------------------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) (limited to 'inc') 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; } -- cgit v1.2.3-55-g7522