From 70ba6c5b53fef4ddee85574612cf241a0e4f842b Mon Sep 17 00:00:00 2001 From: Eugene Molotov Date: Wed, 7 Sep 2022 06:02:23 +0500 Subject: [PATCH] [VkBridge] Manually handle redirects (#3017) Some redirects are legit, some redirects lead to "Too many requests" page --- bridges/VkBridge.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/bridges/VkBridge.php b/bridges/VkBridge.php index b6bf1b94..13fad14a 100644 --- a/bridges/VkBridge.php +++ b/bridges/VkBridge.php @@ -420,8 +420,30 @@ class VkBridge extends BridgeAbstract private function getContents() { $header = ['Accept-language: en', 'Cookie: remixlang=3']; + $redirects = 0; + $uri = $this->getURI(); - return getContents($this->getURI(), $header); + while ($redirects < 2) { + $response = getContents($uri, $header, [CURLOPT_FOLLOWLOCATION => false], true); + + if (in_array($response['code'], [200, 304])) { + return $response['content']; + } + + $uri = urljoin(self::URI, $response['header']['location'][0]); + + if (str_contains($uri, '/429.html')) { + returnServerError('VK responded "Too many requests"'); + } + + if (!preg_match('#^https?://vk.com/(club|public)#', $uri)) { + returnServerError('Unexpected redirect location'); + } + + $redirects++; + } + + returnServerError('Too many redirects, while retreving content from VK'); } protected function appendVideo($video_title, $video_link, &$content_suffix, array &$post_videos)