From ad2431893c79834f0345db63d20acd2cc37308b6 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 10 Oct 2024 23:50:44 +0200 Subject: [PATCH] functions.php: add note on rejected ip for IP API --- inc/functions.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index cd83013d..7745c2e8 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1828,6 +1828,15 @@ function buildJavascript() { * @return bool True if the IP should be blocked. */ function checkIPAPI(string $ip): bool { + function add_note(string $ip, string $note) { + $query = prepare('INSERT INTO ``ip_notes`` VALUES (NULL, :ip, :mod, :time, :body)'); + $query->bindValue(':ip', $ip); + $query->bindValue(':mod', -1, PDO::PARAM_INT); + $query->bindValue(':time', time(), PDO::PARAM_INT); + $query->bindValue(':body', "IP API automatic note: $note"); + $query->execute() or error(db_error($query)); + } + global $config; if (!$config['ip_api']['iphub']['enabled']) { @@ -1918,10 +1927,28 @@ function checkIPAPI(string $ip): bool { return false; } - if (($config['ip_api']['ip_block'] == 1 && $ret['block'] == 1) - || $config['ip_api']['ip_block'] == 2 && $ret['block'] != 0 - || array_search($ret['isp'], $config['ip_api']['isp_blacklist']) !== false - || array_search($ret['asn'], $config['ip_api']['asn_blacklist']) !== false) { + $add_note_mode = (int)$config['ip_api']['add_note']; + + if (($config['ip_api']['ip_block'] == 1 && $ret['block'] == 1) || ($config['ip_api']['ip_block'] == 2 && $ret['block'] != 0)) { + if ($add_note_mode !== 0) { + add_note($ip, _('IP was rejected for being blacklisted by the API')); + } + error($config['error']['proxy']); + return true; + } + + if (array_search($ret['isp'], $config['ip_api']['isp_blacklist']) !== false) { + if ($add_note_mode !== 0) { + add_note($ip, _("IP was rejected because the \"{$ret['isp']}\" ISP is blacklisted.")); + } + error($config['error']['proxy']); + return true; + } + + if (array_search($ret['asn'], $config['ip_api']['asn_blacklist']) !== false) { + if ($add_note_mode !== 0) { + add_note($ip, _("IP was rejected because the \"{$ret['asn']}\" ASN is blacklisted.")); + } error($config['error']['proxy']); return true; }