diff options
author | Andrew Dolgov <[email protected]> | 2023-06-29 07:51:38 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2023-06-29 07:51:38 +0300 |
commit | 93df71ae1f2bba49c85a1b06b792726ba67ae426 (patch) | |
tree | a4f174ca6244603ec6d79c3dce45de764dfa7316 | |
parent | a1387a40ea48c04125d7f9c44ae6c1b2332a6535 (diff) |
add domain blacklist (imgur likes to 429 aggressively)
-rw-r--r-- | init.php | 40 |
1 files changed, 39 insertions, 1 deletions
@@ -4,6 +4,7 @@ class Api_Resize_Media extends Plugin { const MAX_WIDTH = 1024; const DEFAULT_QUALITY = 80; const IGNORE_SCHEMES = [ "magnet", "data" ]; + const DEFAULT_DOMAIN_BLACKLIST = [ "i.imgur.com" ]; /** @var array<int, string> */ private $article_link_cache = []; @@ -131,6 +132,14 @@ class Api_Resize_Media extends Plugin { return; } + $origin_domain = parse_url($url, PHP_URL_HOST); + $domain_blacklist = $this->host->get_array($this, "domain_blacklist", self::DEFAULT_DOMAIN_BLACKLIST); + + if (in_array($origin_domain, $domain_blacklist)) { + Debug::log("[api_resize_media] URL is blacklisted, skipping.", Debug::LOG_VERBOSE); + return; + } + $local_filename = sha1($url); $local_filename_flag = "$local_filename.api_resize-flag"; $quality = $this->host->get($this, "quality", self::DEFAULT_QUALITY); @@ -184,10 +193,23 @@ class Api_Resize_Media extends Plugin { public function api_resize() : void { $url = UrlHelper::validate($_REQUEST["url"]); + + // TODO: render error image using GD + if (!$url) + return; + $referrer = UrlHelper::validate($_REQUEST["referrer"] ?? ""); $width = (int) $_REQUEST["width"]; $force_stamp = sql_bool_to_bool($_REQUEST["force_stamp"]); + $origin_domain = parse_url($url, PHP_URL_HOST); + $domain_blacklist = $this->host->get_array($this, "domain_blacklist", self::DEFAULT_DOMAIN_BLACKLIST); + + if (in_array($origin_domain, $domain_blacklist)) { + header("Location: $url"); + return; + } + if ($width > self::MAX_WIDTH) $width = self::MAX_WIDTH; @@ -533,6 +555,7 @@ class Api_Resize_Media extends Plugin { $force_width = (int) $this->host->profile_get($this, "force_width", 0); $prepare_widths = implode(", ", $this->host->get_array($this, "prepare_widths")); + $domain_blacklist = implode(", ", $this->host->get_array($this, "domain_blacklist", self::DEFAULT_DOMAIN_BLACKLIST)); $quality = (int) $this->host->profile_get($this, "quality", self::DEFAULT_QUALITY); ?> @@ -573,6 +596,16 @@ class Api_Resize_Media extends Plugin { </fieldset> <fieldset class='prefs'> + <label><?= $this->__("Exclude domains:") ?></label> + <input dojoType='dijit.form.TextBox' + placeholder='example.com' + title="<?= $this->__("This is a global setting.") ?>" + name='domain_blacklist' value="<?= $domain_blacklist ?>"> + + <?= $this->__('(comma-separated list, disabled if empty)') ?> + </fieldset> + + <fieldset class='prefs'> <label><?= $this->__( "Output WEBP quality:") ?></label> <input dojoType='dijit.form.NumberSpinner' constraints='{min:0,max:100,places:0}' title="<?= $this->__("This setting is local to current preference profile.") ?>" @@ -591,16 +624,21 @@ class Api_Resize_Media extends Plugin { function save() : void { $force_width = (int) $_POST["force_width"]; $quality = (int) $_POST["quality"]; + $prepare_widths = array_map("intval", array_map("trim", explode(",", $_POST["prepare_widths"]))); + $domain_blacklist = array_map("trim", + explode(",", $_POST["domain_blacklist"])); + $this->host->set($this, "prepare_widths", $prepare_widths); + $this->host->set($this, "domain_blacklist", $domain_blacklist); $this->host->profile_set($this, "force_width", $force_width); $this->host->profile_set($this, "quality", $quality); - echo $this->T_sprintf("Data saved (%d, %d)", $force_width, $quality); + echo $this->T_sprintf("Configuration has been saved."); } function api_version() { |