diff --git a/tests/ImageProxyTest.php b/tests/ImageProxyTest.php index ad07f6fa..db2834e5 100644 --- a/tests/ImageProxyTest.php +++ b/tests/ImageProxyTest.php @@ -67,6 +67,7 @@ public function run(): void { $this->testHtmlDocumentsAndTagSoup(); $this->testImageAttributes(); $this->testVideoPosters(); + $this->testHtmlEncodedUris(); $this->testUriSchemes(); $this->testExcludedDomains(); } catch (FreshRSS_Context_Exception $e) { @@ -188,6 +189,29 @@ private function testVideoPosters(): void { $this->assertSame(false, $videos[1]->hasAttribute('poster'), 'Videos without poster must remain without poster'); } + /** @throws FreshRSS_Context_Exception */ + private function testHtmlEncodedUris(): void { + $url = 'http://images.example/thumbnail.jpg?w=1&h=2'; + $this->assertSame( + self::PROXY_URL . $url, + ImageProxyExtension::getProxyImageHtmlUri($url), + 'HTML-encoded URLs must be proxied and remain HTML-encoded', + ); + $this->assertContains( + 'src="' . ImageProxyExtension::getProxyImageHtmlUri($url) . '"', + ImageProxyExtension::swapUris(''), + 'Proxied HTML-encoded URLs must be found as such in the proxied content', + ); + + $this->configure(encodeUrl: true); + $this->assertSame( + self::PROXY_URL . rawurlencode('http://images.example/thumbnail.jpg?w=1&h=2'), + ImageProxyExtension::getProxyImageHtmlUri($url), + 'HTML-encoded URLs must be decoded before being URL-encoded', + ); + $this->configure(); + } + /** @throws FreshRSS_Context_Exception */ private function testUriSchemes(): void { $this->assertSame( diff --git a/xExtension-ImageProxy/README.md b/xExtension-ImageProxy/README.md index 2a7bbce3..2e5a8562 100644 --- a/xExtension-ImageProxy/README.md +++ b/xExtension-ImageProxy/README.md @@ -6,6 +6,7 @@ To use it, upload this entire directory to the FreshRSS `./extensions` directory ## Changelog +* 1.1.1 Proxy the thumbnails shown in the list of articles * 1.1.0 Add a list of domains that are not proxied * 1.0.4 Respect the feed option to hide enclosures * 1.0.3 Proxy the poster images of videos diff --git a/xExtension-ImageProxy/extension.php b/xExtension-ImageProxy/extension.php index d7d97fa6..a20f3859 100644 --- a/xExtension-ImageProxy/extension.php +++ b/xExtension-ImageProxy/extension.php @@ -140,6 +140,14 @@ public static function getProxyImageUri(string $url): string { return FreshRSS_Context::userConf()->attributeString('image_proxy_url') . $url; } + /** + * Same as getProxyImageUri() for the HTML-encoded URLs that FreshRSS stores, e.g. the ones of thumbnails. + * @throws FreshRSS_Context_Exception + */ + public static function getProxyImageHtmlUri(string $url): string { + return htmlspecialchars(self::getProxyImageUri(htmlspecialchars_decode($url, ENT_QUOTES)), ENT_COMPAT, 'UTF-8'); + } + /** * @param array $matches * @throws FreshRSS_Context_Exception @@ -216,10 +224,43 @@ public static function setImageProxyHook(FreshRSS_Entry $entry): FreshRSS_Entry // The enclosures become part of the content, so only include them if the feed displays them // We most likely already have the feed object in cache, otherwise make a request $feed = FreshRSS_Category::findFeed(FreshRSS_Context::categories(), $entry->feedId()) ?? $entry->feed(); - $entry->_content( - self::swapUris($entry->content($feed?->attributeBoolean('display_enclosures') ?? true)) - ); + $withEnclosures = $feed?->attributeBoolean('display_enclosures') ?? true; + $entry->_content(self::swapUris($entry->content($withEnclosures))); + self::swapThumbnailUris($entry, $withEnclosures); return $entry; } + + /** + * Proxy the thumbnails of the entry and of its enclosures, which are shown in the list of articles. + * @throws FreshRSS_Context_Exception + */ + private static function swapThumbnailUris(FreshRSS_Entry $entry, bool $withEnclosures): void { + $thumbnail = $entry->attributeArray('thumbnail'); + if (is_string($thumbnail['url'] ?? null) && $thumbnail['url'] !== '') { + $url = self::getProxyImageHtmlUri($thumbnail['url']); + // When displaying the enclosures, FreshRSS appends the thumbnail to the content unless its URL is found there. + // The content set above already includes the thumbnail, so to avoid showing it twice, + // only proxy it if the proxied URL is found in the content as well (e.g. not if the content only links to it). + if (!$withEnclosures || preg_match('/([\'"])' . preg_quote($url, '/') . '\1/', $entry->content(false)) === 1) { + $thumbnail['url'] = $url; + $entry->_attribute('thumbnail', $thumbnail); + } + } + + // The thumbnails of the enclosures are not compared to the content, so they can always be proxied + $enclosures = $entry->attributeArray('enclosures') ?? []; + foreach ($enclosures as $key => $enclosure) { + if (is_array($enclosure) && is_array($enclosure['thumbnails'] ?? null)) { + $enclosure['thumbnails'] = array_map( + fn (mixed $src) => is_string($src) ? self::getProxyImageHtmlUri($src) : $src, + $enclosure['thumbnails'] + ); + $enclosures[$key] = $enclosure; + } + } + if ($enclosures !== []) { + $entry->_attribute('enclosures', $enclosures); + } + } } diff --git a/xExtension-ImageProxy/metadata.json b/xExtension-ImageProxy/metadata.json index 1d755b1c..5167ce3a 100644 --- a/xExtension-ImageProxy/metadata.json +++ b/xExtension-ImageProxy/metadata.json @@ -2,7 +2,7 @@ "name": "Image Proxy", "author": "Frans de Jonge", "description": "No insecure content warnings or disappearing images.", - "version": "1.1.0", + "version": "1.1.1", "entrypoint": "ImageProxy", "type": "user" }