From 22804181ae1b333e8d25edc3bbcf381730a29939 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 18 Jun 2021 12:29:51 +0200 Subject: [PATCH] Use Guzzle to download the SFS index see #4281 --- .../status/BlacklistStatus.class.php | 95 +++++++++---------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/wcfsetup/install/files/lib/data/blacklist/status/BlacklistStatus.class.php b/wcfsetup/install/files/lib/data/blacklist/status/BlacklistStatus.class.php index eb1bf117fd..494d59cd4f 100644 --- a/wcfsetup/install/files/lib/data/blacklist/status/BlacklistStatus.class.php +++ b/wcfsetup/install/files/lib/data/blacklist/status/BlacklistStatus.class.php @@ -2,13 +2,12 @@ namespace wcf\data\blacklist\status; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Psr7\Request; +use Psr\Http\Client\ClientExceptionInterface; use wcf\data\DatabaseObject; -use wcf\system\exception\HTTPNotFoundException; -use wcf\system\exception\HTTPServerErrorException; -use wcf\system\exception\HTTPUnauthorizedException; use wcf\system\exception\SystemException; -use wcf\util\exception\HTTPException; -use wcf\util\HTTPRequest; +use wcf\system\io\HttpFactory; use wcf\util\JSON; /** @@ -88,60 +87,60 @@ class BlacklistStatus extends DatabaseObject * @return string|null * @throws SystemException */ - public static function getNextDelta(array $status) + public static function getNextDelta(array $status, ?ClientInterface $client = null) { + if (!$client) { + $client = HttpFactory::makeClientWithTimeout(5); + } + // Fetch the index file to determine the oldest possible value that can be retrieved. - $request = new HTTPRequest('https://assets.woltlab.com/blacklist/index.json'); + $request = new Request( + 'GET', + 'https://assets.woltlab.com/blacklist/index.json' + ); + try { - $request->execute(); - } catch (SystemException $e) { - if ( - $e instanceof HTTPNotFoundException - || $e instanceof HTTPUnauthorizedException - || $e instanceof HTTPServerErrorException - || $e instanceof HTTPException - ) { - \wcf\functions\exception\logThrowable($e); - - return null; - } + $response = $client->send($request); + } catch (ClientExceptionInterface $e) { + \wcf\functions\exception\logThrowable($e); - throw $e; + return null; } - $response = $request->getReply(); - if ($response['statusCode'] == 200) { - $data = JSON::decode($response['body']); - if (\is_array($data)) { - $deltas = ['delta1', 'delta2', 'delta3', 'delta4']; - - // The array is ordered from "now" to "14 days ago". - foreach (\array_reverse($data) as $entry) { - $date = $entry['date']; - if (isset($status[$date])) { - $dateStatus = $status[$date]; - if ($dateStatus->isComplete()) { - continue; - } + if ($response->getStatusCode() !== 200) { + return null; + } - foreach ($deltas as $delta) { - if ($entry['files'][$delta] && !$dateStatus->{$delta}) { - return "{$date}/{$delta}.json"; - } + $data = JSON::decode((string)$response->getBody()); + if (\is_array($data)) { + $deltas = ['delta1', 'delta2', 'delta3', 'delta4']; + + // The array is ordered from "now" to "14 days ago". + foreach (\array_reverse($data) as $entry) { + $date = $entry['date']; + if (isset($status[$date])) { + $dateStatus = $status[$date]; + if ($dateStatus->isComplete()) { + continue; + } + + foreach ($deltas as $delta) { + if ($entry['files'][$delta] && !$dateStatus->{$delta}) { + return "{$date}/{$delta}.json"; } - } else { - foreach ($deltas as $delta) { - if ($entry['files'][$delta]) { - return "{$date}/{$delta}.json"; - } + } + } else { + foreach ($deltas as $delta) { + if ($entry['files'][$delta]) { + return "{$date}/{$delta}.json"; } } - - // The `full.json` file is not considered for now, because it is very unlikely that none of the - // delta files are available. Also, it's significant larger than the delta updates and we cannot - // reliably predict if we're able to import it at all: slow hosts or max_execution_time almost - // exhausted by other cronjobs. } + + // The `full.json` file is not considered for now, because it is very unlikely that none of the + // delta files are available. Also, it's significant larger than the delta updates and we cannot + // reliably predict if we're able to import it at all: slow hosts or max_execution_time almost + // exhausted by other cronjobs. } } -- 2.20.1