Skip to content

Commit

Permalink
Update ImageProxy (#201)
Browse files Browse the repository at this point in the history
Partial follow-up of #185 (comment)
Fix regression in typing for `getSrcSetUris`()
  • Loading branch information
Alkarex authored Feb 29, 2024
1 parent 4e0155e commit 265fd0b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
12 changes: 6 additions & 6 deletions xExtension-ImageProxy/configure.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
<div class="form-group">
<label class="group-name" for="image_proxy_url"><?= _t('ext.imageproxy.proxy_url') ?></label>
<div class="group-controls">
<input type="url" name="image_proxy_url" id="image_proxy_url" value="<?= FreshRSS_Context::$user_conf->image_proxy_url ?>">
<input type="url" name="image_proxy_url" id="image_proxy_url" value="<?= FreshRSS_Context::userConf()->image_proxy_url ?>">
</div>
</div>
<div class="form-group">
<label class="group-name" for="image_proxy_scheme_http"><?= _t('ext.imageproxy.scheme_http') ?></label>
<div class="group-controls">
<input type="checkbox" name="image_proxy_scheme_http" id="image_proxy_scheme_http" value="1" <?= FreshRSS_Context::$user_conf->image_proxy_scheme_http ? 'checked' : '' ?>>
<input type="checkbox" name="image_proxy_scheme_http" id="image_proxy_scheme_http" value="1" <?= FreshRSS_Context::userConf()->image_proxy_scheme_http ? 'checked' : '' ?>>
</div>
</div>
<div class="form-group">
<label class="group-name" for="image_proxy_scheme_https"><?= _t('ext.imageproxy.scheme_https'); ?></label>
<div class="group-controls">
<input type="checkbox" name="image_proxy_scheme_https" id="image_proxy_scheme_https" value="1" <?= FreshRSS_Context::$user_conf->image_proxy_scheme_https ? 'checked' : '' ?>>
<input type="checkbox" name="image_proxy_scheme_https" id="image_proxy_scheme_https" value="1" <?= FreshRSS_Context::userConf()->image_proxy_scheme_https ? 'checked' : '' ?>>
</div>
</div>
<div class="form-group">
<label class="group-name" for="image_proxy_scheme_default"><?= _t('ext.imageproxy.scheme_default'); ?></label>
<div class="group-controls">
<select name="image_proxy_scheme_default" id="image_proxy_scheme_default">
<option value="<?= htmlentities(FreshRSS_Context::$user_conf->image_proxy_scheme_default ?? '') ?>" selected="selected"><?= htmlentities(FreshRSS_Context::$user_conf->image_proxy_scheme_default ?? '') ?></option>
<option value="<?= htmlentities(FreshRSS_Context::userConf()->image_proxy_scheme_default ?? '') ?>" selected="selected"><?= htmlentities(FreshRSS_Context::userConf()->image_proxy_scheme_default ?? '') ?></option>
<option value="-">-</option>
<option value="auto">auto</option>
<option value="http">http</option>
Expand All @@ -37,13 +37,13 @@
<div class="form-group">
<label class="group-name" for="image_proxy_scheme_include"><?= _t('ext.imageproxy.scheme_include'); ?></label>
<div class="group-controls">
<input type="checkbox" name="image_proxy_scheme_include" id="image_proxy_scheme_include" value="1" <?= FreshRSS_Context::$user_conf->image_proxy_scheme_include ? 'checked' : '' ?>>
<input type="checkbox" name="image_proxy_scheme_include" id="image_proxy_scheme_include" value="1" <?= FreshRSS_Context::userConf()->image_proxy_scheme_include ? 'checked' : '' ?>>
</div>
</div>
<div class="form-group">
<label class="group-name" for="image_proxy_url_encode"><?= _t('ext.imageproxy.url_encode'); ?></label>
<div class="group-controls">
<input type="checkbox" name="image_proxy_url_encode" id="image_proxy_url_encode" value="1" <?= FreshRSS_Context::$user_conf->image_proxy_url_encode ? 'checked' : '' ?>>
<input type="checkbox" name="image_proxy_url_encode" id="image_proxy_url_encode" value="1" <?= FreshRSS_Context::userConf()->image_proxy_url_encode ? 'checked' : '' ?>>
</div>
</div>

Expand Down
82 changes: 41 additions & 41 deletions xExtension-ImageProxy/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -21,99 +21,97 @@ 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();
}
}

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();
}
}

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;
}
} 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<string> $matches
* @return array<string>
*/
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 {
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 265fd0b

Please sign in to comment.