diff options
Diffstat (limited to 'classes/urlhelper.php')
-rw-r--r-- | classes/urlhelper.php | 103 |
1 files changed, 47 insertions, 56 deletions
diff --git a/classes/urlhelper.php b/classes/urlhelper.php index bb51f5d06..dc47f5ad8 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -10,31 +10,14 @@ class UrlHelper { "application/x-bittorrent" => [ "magnet" ], ]; - // TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ - /** @var string */ - static $fetch_last_error; - - /** @var int */ - static $fetch_last_error_code; - - /** @var string */ - static $fetch_last_error_content; - - /** @var string */ - static $fetch_last_content_type; - - /** @var string */ - static $fetch_last_modified; - - - /** @var string */ - static $fetch_effective_url; - - /** @var string */ - static $fetch_effective_ip_addr; - - /** @var bool */ - static $fetch_curl_used; + static string $fetch_last_error; + static int $fetch_last_error_code; + static string $fetch_last_error_content; + static string $fetch_last_content_type; + static string $fetch_last_modified; + static string $fetch_effective_url; + static string $fetch_effective_ip_addr; + static bool $fetch_curl_used; /** * @param array<string, string|int> $parts @@ -207,32 +190,26 @@ class UrlHelper { if ($nest > 10) return false; - if (version_compare(PHP_VERSION, '7.1.0', '>=')) { - $context_options = array( - 'http' => array( - 'header' => array( - 'Connection: close' - ), - 'method' => 'HEAD', - 'timeout' => $timeout, - 'protocol_version'=> 1.1) - ); - - if (Config::get(Config::HTTP_PROXY)) { - $context_options['http']['request_fulluri'] = true; - $context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY); - } + $context_options = array( + 'http' => array( + 'header' => array( + 'Connection: close' + ), + 'method' => 'HEAD', + 'timeout' => $timeout, + 'protocol_version'=> 1.1) + ); + + if (Config::get(Config::HTTP_PROXY)) { + $context_options['http']['request_fulluri'] = true; + $context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY); + } - $context = stream_context_create($context_options); + $context = stream_context_create($context_options); - // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0 - // @phpstan-ignore-next-line - $headers = get_headers($url, 0, $context); - } else { - // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0 - // @phpstan-ignore-next-line - $headers = get_headers($url, 0); - } + // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.4.0 + // @phpstan-ignore-next-line + $headers = get_headers($url, 0, $context); if (is_array($headers)) { $headers = array_reverse($headers); // last one is the correct one @@ -462,7 +439,11 @@ class UrlHelper { } if (!$contents) { - self::$fetch_last_error = curl_errno($ch) . " " . curl_error($ch); + if (curl_errno($ch) === 0) { + self::$fetch_last_error = 'Successful response, but no content was received.'; + } else { + self::$fetch_last_error = curl_errno($ch) . " " . curl_error($ch); + } curl_close($ch); return false; } @@ -543,6 +524,11 @@ class UrlHelper { $data = @file_get_contents($url, false, $context); + if ($data === false) { + self::$fetch_last_error = "'file_get_contents' failed."; + return false; + } + foreach ($http_response_header as $header) { if (strstr($header, ": ") !== false) { list ($key, $value) = explode(": ", $header); @@ -578,15 +564,20 @@ class UrlHelper { return false; } - $is_gzipped = RSSUtils::is_gzipped($data); + if ($data) { + $is_gzipped = RSSUtils::is_gzipped($data); - if ($is_gzipped && $data) { - $tmp = @gzdecode($data); + if ($is_gzipped) { + $tmp = @gzdecode($data); - if ($tmp) $data = $tmp; - } + if ($tmp) $data = $tmp; + } - return $data; + return $data; + } else { + self::$fetch_last_error = 'Successful response, but no content was received.'; + return false; + } } } |