diff --git a/xExtension-ImageProxy/configure.phtml b/xExtension-ImageProxy/configure.phtml index c1aa7a7..0923d0a 100644 --- a/xExtension-ImageProxy/configure.phtml +++ b/xExtension-ImageProxy/configure.phtml @@ -7,26 +7,26 @@
- +
- image_proxy_scheme_http ? 'checked' : '' ?>> + image_proxy_scheme_http ? 'checked' : '' ?>>
- image_proxy_scheme_https ? 'checked' : '' ?>> + image_proxy_scheme_https ? 'checked' : '' ?>>
image_proxy_scheme_include ? 'checked' : '' ?>> + image_proxy_scheme_include ? 'checked' : '' ?>>
- image_proxy_url_encode ? 'checked' : '' ?>> + image_proxy_url_encode ? 'checked' : '' ?>>
diff --git a/xExtension-ImageProxy/extension.php b/xExtension-ImageProxy/extension.php index b5f1b4c..233bf5f 100644 --- a/xExtension-ImageProxy/extension.php +++ b/xExtension-ImageProxy/extension.php @@ -12,7 +12,7 @@ final class ImageProxyExtension extends Minz_Extension { private const URL_ENCODE = '1'; public function init(): void { - if (FreshRSS_Context::$system_conf === null) { + if (!FreshRSS_Context::hasSystemConf()) { throw new FreshRSS_Context_Exception('System configuration not initialised!'); } $this->registerHook( @@ -21,37 +21,37 @@ public function init(): void { ); // Defaults $save = false; - if (is_null(FreshRSS_Context::$user_conf->image_proxy_url)) { - FreshRSS_Context::$user_conf->image_proxy_url = self::PROXY_URL; + if (is_null(FreshRSS_Context::userConf()->image_proxy_url)) { + FreshRSS_Context::userConf()->image_proxy_url = self::PROXY_URL; $save = true; } - if (is_null(FreshRSS_Context::$user_conf->image_proxy_scheme_http)) { - FreshRSS_Context::$user_conf->image_proxy_scheme_http = self::SCHEME_HTTP; + if (is_null(FreshRSS_Context::userConf()->image_proxy_scheme_http)) { + FreshRSS_Context::userConf()->image_proxy_scheme_http = self::SCHEME_HTTP; $save = true; } - if (is_null(FreshRSS_Context::$user_conf->image_proxy_scheme_https)) { - FreshRSS_Context::$user_conf->image_proxy_scheme_https = self::SCHEME_HTTPS; + if (is_null(FreshRSS_Context::userConf()->image_proxy_scheme_https)) { + FreshRSS_Context::userConf()->image_proxy_scheme_https = self::SCHEME_HTTPS; // Legacy - if (!is_null(FreshRSS_Context::$user_conf->image_proxy_force)) { - FreshRSS_Context::$user_conf->image_proxy_scheme_https = FreshRSS_Context::$user_conf->image_proxy_force; - FreshRSS_Context::$user_conf->image_proxy_force = null; // Minz -> unset + if (!is_null(FreshRSS_Context::userConf()->image_proxy_force)) { + FreshRSS_Context::userConf()->image_proxy_scheme_https = FreshRSS_Context::userConf()->image_proxy_force; + FreshRSS_Context::userConf()->image_proxy_force = null; // Minz -> unset } $save = true; } - if (is_null(FreshRSS_Context::$user_conf->image_proxy_scheme_default)) { - FreshRSS_Context::$user_conf->image_proxy_scheme_default = self::SCHEME_DEFAULT; + if (is_null(FreshRSS_Context::userConf()->image_proxy_scheme_default)) { + FreshRSS_Context::userConf()->image_proxy_scheme_default = self::SCHEME_DEFAULT; $save = true; } - if (is_null(FreshRSS_Context::$user_conf->image_proxy_scheme_include)) { - FreshRSS_Context::$user_conf->image_proxy_scheme_include = self::SCHEME_INCLUDE; + if (is_null(FreshRSS_Context::userConf()->image_proxy_scheme_include)) { + FreshRSS_Context::userConf()->image_proxy_scheme_include = self::SCHEME_INCLUDE; $save = true; } - if (is_null(FreshRSS_Context::$user_conf->image_proxy_url_encode)) { - FreshRSS_Context::$user_conf->image_proxy_url_encode = self::URL_ENCODE; + if (is_null(FreshRSS_Context::userConf()->image_proxy_url_encode)) { + FreshRSS_Context::userConf()->image_proxy_url_encode = self::URL_ENCODE; $save = true; } if ($save) { - FreshRSS_Context::$user_conf->save(); + FreshRSS_Context::userConf()->save(); } } @@ -59,13 +59,13 @@ public function handleConfigureAction(): void { $this->registerTranslates(); if (Minz_Request::isPost()) { - FreshRSS_Context::$user_conf->image_proxy_url = Minz_Request::paramString('image_proxy_url', true) ?: self::PROXY_URL; - FreshRSS_Context::$user_conf->image_proxy_scheme_http = Minz_Request::paramString('image_proxy_scheme_http'); - FreshRSS_Context::$user_conf->image_proxy_scheme_https = Minz_Request::paramString('image_proxy_scheme_https'); - FreshRSS_Context::$user_conf->image_proxy_scheme_default = Minz_Request::paramString('image_proxy_scheme_default') ?: self::SCHEME_DEFAULT; - FreshRSS_Context::$user_conf->image_proxy_scheme_include = Minz_Request::paramString('image_proxy_scheme_include'); - FreshRSS_Context::$user_conf->image_proxy_url_encode = Minz_Request::paramString('image_proxy_url_encode'); - FreshRSS_Context::$user_conf->save(); + FreshRSS_Context::userConf()->image_proxy_url = Minz_Request::paramString('image_proxy_url', true) ?: self::PROXY_URL; + FreshRSS_Context::userConf()->image_proxy_scheme_http = Minz_Request::paramString('image_proxy_scheme_http'); + FreshRSS_Context::userConf()->image_proxy_scheme_https = Minz_Request::paramString('image_proxy_scheme_https'); + FreshRSS_Context::userConf()->image_proxy_scheme_default = Minz_Request::paramString('image_proxy_scheme_default') ?: self::SCHEME_DEFAULT; + FreshRSS_Context::userConf()->image_proxy_scheme_include = Minz_Request::paramString('image_proxy_scheme_include'); + FreshRSS_Context::userConf()->image_proxy_url_encode = Minz_Request::paramString('image_proxy_url_encode'); + FreshRSS_Context::userConf()->save(); } } @@ -73,27 +73,27 @@ public static function getProxyImageUri(string $url): string { $parsed_url = parse_url($url); $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] : null; if ($scheme === 'http') { - if (!FreshRSS_Context::$user_conf->image_proxy_scheme_http) { + if (!FreshRSS_Context::userConf()->image_proxy_scheme_http) { return $url; } - if (!FreshRSS_Context::$user_conf->image_proxy_scheme_include) { + if (!FreshRSS_Context::userConf()->image_proxy_scheme_include) { $url = substr($url, 7); // http:// } } elseif ($scheme === 'https') { - if (!FreshRSS_Context::$user_conf->image_proxy_scheme_https) { + if (!FreshRSS_Context::userConf()->image_proxy_scheme_https) { return $url; } - if (!FreshRSS_Context::$user_conf->image_proxy_scheme_include) { + if (!FreshRSS_Context::userConf()->image_proxy_scheme_include) { $url = substr($url, 8); // https:// } } elseif (empty($scheme)) { - if (FreshRSS_Context::$user_conf->image_proxy_scheme_default === 'auto') { - if (FreshRSS_Context::$user_conf->image_proxy_scheme_include) { + if (FreshRSS_Context::userConf()->image_proxy_scheme_default === 'auto') { + if (FreshRSS_Context::userConf()->image_proxy_scheme_include) { $url = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https:' : 'http:') . $url; } - } elseif (substr(FreshRSS_Context::$user_conf->image_proxy_scheme_default, 0, 4) === 'http') { - if (FreshRSS_Context::$user_conf->image_proxy_scheme_include) { - $url = FreshRSS_Context::$user_conf->image_proxy_scheme_default . ':' . $url; + } elseif (substr(FreshRSS_Context::userConf()->image_proxy_scheme_default, 0, 4) === 'http') { + if (FreshRSS_Context::userConf()->image_proxy_scheme_include) { + $url = FreshRSS_Context::userConf()->image_proxy_scheme_default . ':' . $url; } } else { // do not proxy unschemed ("//path/...") URLs return $url; @@ -101,19 +101,17 @@ public static function getProxyImageUri(string $url): string { } else { // unknown/unsupported (non-http) scheme return $url; } - if (FreshRSS_Context::$user_conf->image_proxy_url_encode) { + if (FreshRSS_Context::userConf()->image_proxy_url_encode) { $url = rawurlencode($url); } - return FreshRSS_Context::$user_conf->image_proxy_url . $url; + return FreshRSS_Context::userConf()->image_proxy_url . $url; } /** * @param array $matches - * @return array */ - public static function getSrcSetUris(array $matches): array { - $result = str_replace($matches[1], self::getProxyImageUri($matches[1]), $matches[0]); - return is_array($result) ? $result : [$result]; + public static function getSrcSetUris(array $matches): string { + return str_replace($matches[1], self::getProxyImageUri($matches[1]), $matches[0]); } public static function swapUris(string $content): string { @@ -131,8 +129,10 @@ public static function swapUris(string $content): string { $img->setAttribute('src', $newSrc); } if ($img->hasAttribute('srcset')) { - $newSrcSet = preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/', fn($matches) => self::getSrcSetUris($matches), $img->getAttribute('srcset')); - $img->setAttribute('srcset', $newSrcSet); + $newSrcSet = preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/', fn (array $matches) => self::getSrcSetUris($matches), $img->getAttribute('srcset')); + if ($newSrcSet != null) { + $img->setAttribute('srcset', $newSrcSet); + } } }