|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +final class ImageCamoExtension extends Minz_Extension { |
| 6 | + // Defaults |
| 7 | + private const DEFAULT_CAMO_URL = 'https://your-camo-instance.example.com'; |
| 8 | + private const DEFAULT_HMAC_KEY = ''; |
| 9 | + private const DEFAULT_SCHEME_HTTP = true; |
| 10 | + private const DEFAULT_SCHEME_HTTPS = false; |
| 11 | + private const DEFAULT_SCHEME_DEFAULT = 'auto'; |
| 12 | + private const DEFAULT_SCHEME_INCLUDE = false; |
| 13 | + private const DEFAULT_ENCODING = 'base64'; // base64 or hex |
| 14 | + |
| 15 | + /** |
| 16 | + * @throws FreshRSS_Context_Exception |
| 17 | + */ |
| 18 | + #[\Override] |
| 19 | + public function init(): void { |
| 20 | + if (!FreshRSS_Context::hasSystemConf()) { |
| 21 | + throw new FreshRSS_Context_Exception('System configuration not initialised!'); |
| 22 | + } |
| 23 | + $this->registerHook('entry_before_display', [self::class, 'setImageProxyHook']); |
| 24 | + |
| 25 | + // Initialize defaults if not set |
| 26 | + $save = false; |
| 27 | + if (FreshRSS_Context::userConf()->attributeString('camo_proxy_url') == null) { |
| 28 | + FreshRSS_Context::userConf()->_attribute('camo_proxy_url', self::DEFAULT_CAMO_URL); |
| 29 | + $save = true; |
| 30 | + } |
| 31 | + if (FreshRSS_Context::userConf()->attributeString('camo_hmac_key') == null) { |
| 32 | + FreshRSS_Context::userConf()->_attribute('camo_hmac_key', self::DEFAULT_HMAC_KEY); |
| 33 | + $save = true; |
| 34 | + } |
| 35 | + if (FreshRSS_Context::userConf()->attributeBool('camo_scheme_http') === null) { |
| 36 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_http', self::DEFAULT_SCHEME_HTTP); |
| 37 | + $save = true; |
| 38 | + } |
| 39 | + if (FreshRSS_Context::userConf()->attributeBool('camo_scheme_https') === null) { |
| 40 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_https', self::DEFAULT_SCHEME_HTTPS); |
| 41 | + $save = true; |
| 42 | + } |
| 43 | + if (FreshRSS_Context::userConf()->attributeString('camo_scheme_default') === null) { |
| 44 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_default', self::DEFAULT_SCHEME_DEFAULT); |
| 45 | + $save = true; |
| 46 | + } |
| 47 | + if (FreshRSS_Context::userConf()->attributeBool('camo_scheme_include') === null) { |
| 48 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_include', self::DEFAULT_SCHEME_INCLUDE); |
| 49 | + $save = true; |
| 50 | + } |
| 51 | + if (FreshRSS_Context::userConf()->attributeString('camo_encoding') === null) { |
| 52 | + FreshRSS_Context::userConf()->_attribute('camo_encoding', self::DEFAULT_ENCODING); |
| 53 | + $save = true; |
| 54 | + } |
| 55 | + if ($save) { |
| 56 | + FreshRSS_Context::userConf()->save(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @throws FreshRSS_Context_Exception |
| 62 | + */ |
| 63 | + #[\Override] |
| 64 | + public function handleConfigureAction(): void { |
| 65 | + $this->registerTranslates(); |
| 66 | + |
| 67 | + if (Minz_Request::isPost()) { |
| 68 | + FreshRSS_Context::userConf()->_attribute('camo_proxy_url', Minz_Request::paramString('camo_proxy_url', plaintext: true) ?: self::DEFAULT_CAMO_URL); |
| 69 | + FreshRSS_Context::userConf()->_attribute('camo_hmac_key', Minz_Request::paramString('camo_hmac_key', plaintext: true) ?: self::DEFAULT_HMAC_KEY); |
| 70 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_http', Minz_Request::paramBoolean('camo_scheme_http')); |
| 71 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_https', Minz_Request::paramBoolean('camo_scheme_https')); |
| 72 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_default', Minz_Request::paramString('camo_scheme_default', plaintext: true) ?: self::DEFAULT_SCHEME_DEFAULT); |
| 73 | + FreshRSS_Context::userConf()->_attribute('camo_scheme_include', Minz_Request::paramBoolean('camo_scheme_include')); |
| 74 | + FreshRSS_Context::userConf()->_attribute('camo_encoding', Minz_Request::paramString('camo_encoding', plaintext: true) ?: self::DEFAULT_ENCODING); |
| 75 | + FreshRSS_Context::userConf()->save(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Generate camo signed URL |
| 81 | + * @throws FreshRSS_Context_Exception |
| 82 | + */ |
| 83 | + public static function getImageCamoUri(string $url): string { |
| 84 | + $parsed_url = parse_url($url); |
| 85 | + $scheme = $parsed_url['scheme'] ?? ''; |
| 86 | + |
| 87 | + // Check if we should proxy this scheme |
| 88 | + if ($scheme === 'http') { |
| 89 | + if (!FreshRSS_Context::userConf()->attributeBool('camo_scheme_http')) { |
| 90 | + return $url; |
| 91 | + } |
| 92 | + } elseif ($scheme === 'https') { |
| 93 | + if (!FreshRSS_Context::userConf()->attributeBool('camo_scheme_https')) { |
| 94 | + return $url; |
| 95 | + } |
| 96 | + } elseif ($scheme === '') { |
| 97 | + // Handle protocol-relative URLs |
| 98 | + $schemeDefault = FreshRSS_Context::userConf()->attributeString('camo_scheme_default'); |
| 99 | + if ($schemeDefault === 'auto') { |
| 100 | + $autoScheme = ((is_string($_SERVER['HTTPS'] ?? null) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https:' : 'http:'); |
| 101 | + if (FreshRSS_Context::userConf()->attributeBool('camo_scheme_include')) { |
| 102 | + $url = $autoScheme . $url; |
| 103 | + } |
| 104 | + } elseif (str_starts_with($schemeDefault ?? '', 'http')) { |
| 105 | + if (FreshRSS_Context::userConf()->attributeBool('camo_scheme_include')) { |
| 106 | + $url = $schemeDefault . ':' . $url; |
| 107 | + } |
| 108 | + } else { |
| 109 | + // Do not proxy unschemed URLs |
| 110 | + return $url; |
| 111 | + } |
| 112 | + } else { |
| 113 | + // Unknown/unsupported scheme |
| 114 | + return $url; |
| 115 | + } |
| 116 | + |
| 117 | + $hmacKey = FreshRSS_Context::userConf()->attributeString('camo_hmac_key'); |
| 118 | + $camoUrl = FreshRSS_Context::userConf()->attributeString('camo_proxy_url'); |
| 119 | + $encoding = FreshRSS_Context::userConf()->attributeString('camo_encoding') ?: 'base64'; |
| 120 | + |
| 121 | + if (empty($hmacKey) || empty($camoUrl)) { |
| 122 | + return $url; // Return original URL if configuration is incomplete |
| 123 | + } |
| 124 | + |
| 125 | + // Generate HMAC signature and encode URL according to camo format |
| 126 | + if ($encoding === 'hex') { |
| 127 | + return self::generateHexCamoUrl($hmacKey, $camoUrl, $url); |
| 128 | + } else { |
| 129 | + return self::generateBase64CamoUrl($hmacKey, $camoUrl, $url); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Generate Base64 encoded camo URL |
| 135 | + */ |
| 136 | + private static function generateBase64CamoUrl(string $hmacKey, string $camoUrl, string $imageUrl): string { |
| 137 | + // Generate HMAC-SHA1 |
| 138 | + $hmac = hash_hmac('sha1', $imageUrl, $hmacKey, true); |
| 139 | + |
| 140 | + // Base64 encode without padding (camo style) |
| 141 | + $b64Hmac = rtrim(strtr(base64_encode($hmac), '+/', '-_'), '='); |
| 142 | + $b64Url = rtrim(strtr(base64_encode($imageUrl), '+/', '-_'), '='); |
| 143 | + |
| 144 | + return rtrim($camoUrl, '/') . '/' . $b64Hmac . '/' . $b64Url; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Generate Hex encoded camo URL |
| 149 | + */ |
| 150 | + private static function generateHexCamoUrl(string $hmacKey, string $camoUrl, string $imageUrl): string { |
| 151 | + // Generate HMAC-SHA1 in hex |
| 152 | + $hexHmac = hash_hmac('sha1', $imageUrl, $hmacKey); |
| 153 | + $hexUrl = bin2hex($imageUrl); |
| 154 | + |
| 155 | + return rtrim($camoUrl, '/') . '/' . $hexHmac . '/' . $hexUrl; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param array<string> $matches |
| 160 | + * @throws FreshRSS_Context_Exception |
| 161 | + */ |
| 162 | + public static function getSrcSetUris(array $matches): string { |
| 163 | + return str_replace($matches[1], self::getImageCamoUri($matches[1]), $matches[0]); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @throws FreshRSS_Context_Exception |
| 168 | + */ |
| 169 | + public static function swapUris(string $content): string { |
| 170 | + if ($content === '') { |
| 171 | + return $content; |
| 172 | + } |
| 173 | + |
| 174 | + $doc = new DOMDocument(); |
| 175 | + libxml_use_internal_errors(true); // prevent tag soup errors from showing |
| 176 | + $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'); |
| 177 | + if (!is_string($content)) { |
| 178 | + return ''; |
| 179 | + } |
| 180 | + $doc->loadHTML($content); |
| 181 | + $imgs = $doc->getElementsByTagName('img'); |
| 182 | + foreach ($imgs as $img) { |
| 183 | + if (!($img instanceof DOMElement)) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + if ($img->hasAttribute('src')) { |
| 187 | + $src = $img->getAttribute('src'); |
| 188 | + $newSrc = self::getImageCamoUri($src); |
| 189 | + /* |
| 190 | + Due to the URL change, FreshRSS is not aware of already rendered enclosures. |
| 191 | + Adding data-xextension-imagecamo-original-src / srcset ensures that original URLs are present in the content for the renderer check FreshRSS_Entry->containsLink. |
| 192 | + */ |
| 193 | + $img->setAttribute('data-xextension-imagecamo-original-src', $src); |
| 194 | + $img->setAttribute('src', $newSrc); |
| 195 | + } |
| 196 | + if ($img->hasAttribute('srcset')) { |
| 197 | + $srcSet = $img->getAttribute('srcset'); |
| 198 | + $newSrcSet = preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/', fn (array $matches) => self::getSrcSetUris($matches), $srcSet); |
| 199 | + if ($newSrcSet != null) { |
| 200 | + $img->setAttribute('data-xextension-imagecamo-original-srcset', $srcSet); |
| 201 | + $img->setAttribute('srcset', $newSrcSet); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + $body = $doc->getElementsByTagName('body')->item(0); |
| 207 | + |
| 208 | + $output = $doc->saveHTML($body); |
| 209 | + if ($output === false) { |
| 210 | + return ''; |
| 211 | + } |
| 212 | + |
| 213 | + $output = preg_replace('/^<body>|<\/body>$/', '', $output) ?? ''; |
| 214 | + |
| 215 | + return $output; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @throws FreshRSS_Context_Exception |
| 220 | + */ |
| 221 | + public static function setImageProxyHook(FreshRSS_Entry $entry): FreshRSS_Entry { |
| 222 | + $entry->_content( |
| 223 | + self::swapUris($entry->content()) |
| 224 | + ); |
| 225 | + |
| 226 | + return $entry; |
| 227 | + } |
| 228 | +} |
0 commit comments