summaryrefslogtreecommitdiffstats
path: root/inc/download.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/download.inc.php')
-rw-r--r--inc/download.inc.php76
1 files changed, 27 insertions, 49 deletions
diff --git a/inc/download.inc.php b/inc/download.inc.php
index 39f8e2e2..8358eaa3 100644
--- a/inc/download.inc.php
+++ b/inc/download.inc.php
@@ -1,67 +1,53 @@
<?php
+declare(strict_types=1);
+
class Download
{
+ /**
+ * @var false|resource
+ */
private static $curlHandle = false;
/**
* Common initialization for download and downloadToFile
* Return file handle to header file
+ * @return false|resource
*/
- private static function initCurl($url, $timeout, &$head)
+ private static function initCurl(string $url, int $timeout)
{
if (self::$curlHandle === false) {
self::$curlHandle = curl_init();
if (self::$curlHandle === false) {
- Util::traceError('Could not initialize cURL');
+ ErrorHandler::traceError('Could not initialize cURL');
}
curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT, ceil($timeout / 2));
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $timeout);
curl_setopt(self::$curlHandle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt(self::$curlHandle, CURLOPT_AUTOREFERER, true);
- curl_setopt(self::$curlHandle, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$curlHandle, CURLOPT_MAXREDIRS, 6);
+ curl_setopt(self::$curlHandle, CURLOPT_ACCEPT_ENCODING, '');
+ curl_setopt(self::$curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) SLX-Admin/1.0');
+ curl_setopt(self::$curlHandle, CURLOPT_PROTOCOLS, CURLPROTO_FTP | CURLPROTO_FTPS | CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
curl_setopt(self::$curlHandle, CURLOPT_URL, $url);
- $tmpfile = tempnam('/tmp/', 'bwlp-');
- $head = fopen($tmpfile, 'w+b');
- unlink($tmpfile);
- if ($head === false)
- Util::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($url, $timeout, &$code)
+ public static function asString(string $url, int $timeout, ?int &$code)
{
- $ch = self::initCurl($url, $timeout, $head);
+ $ch = self::initCurl($url, $timeout);
+ curl_setopt($ch, CURLOPT_FILE, null);
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;
}
@@ -71,9 +57,10 @@ class Download
* @param string $url URL to fetch
* @param array|false $params POST params to set in body, list of key-value-pairs
* @param int $timeout timeout in seconds
- * @param int $code HTTP response code, or 999 on error
+ * @param ?int $code HTTP response code, or 999 on error
+ * @return string|false
*/
- public static function asStringPost($url, $params, $timeout, &$code)
+ public static function asStringPost(string $url, $params, int $timeout, ?int &$code)
{
$string = '';
if (is_array($params)) {
@@ -84,17 +71,13 @@ class Download
$string .= $k . '=' . urlencode($v);
}
}
- $ch = self::initCurl($url, $timeout, $head);
+ $ch = self::initCurl($url, $timeout);
+ curl_setopt($ch, CURLOPT_FILE, null);
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;
}
@@ -104,28 +87,23 @@ class Download
* @param string $target destination path to download file to
* @param string $url URL of file to download
* @param int $timeout timeout in seconds
- * @param int $code HTTP status code passed out by reference
- * @return boolean
+ * @param ?int $code HTTP status code passed out by reference
*/
- public static function toFile($target, $url, $timeout, &$code)
+ public static function toFile(string $target, string $url, int $timeout, ?int &$code): bool
{
$fh = fopen($target, 'wb');
if ($fh === false)
- Util::traceError("Could not open $target for writing.");
- $ch = self::initCurl($url, $timeout, $head);
+ ErrorHandler::traceError("Could not open $target for writing.");
+ $ch = self::initCurl($url, $timeout);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
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;
}