From 589abf27313806825423dc051b1f05a6850e4a0e Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Sun, 22 Dec 2024 17:21:24 +0200 Subject: [PATCH] Enhances remove from whitelist --- app/classes/ratelimitrer.php | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/app/classes/ratelimitrer.php b/app/classes/ratelimitrer.php index cc12ef3..7b35d2a 100644 --- a/app/classes/ratelimitrer.php +++ b/app/classes/ratelimitrer.php @@ -123,10 +123,37 @@ class RateLimiter { } // Remove from whitelist - public function removeFromWhitelist($ip) { - $stmt = $this->db->prepare("DELETE FROM {$this->whitelistTable} WHERE ip_address = ?"); + public function removeFromWhitelist($ip, $userId = null, $removedBy = 'system') { + try { + // Get IP details before removal for logging + $stmt = $this->db->prepare("SELECT * FROM {$this->whitelistTable} WHERE ip_address = ?"); + $stmt->execute([$ip]); + $ipDetails = $stmt->fetch(PDO::FETCH_ASSOC); - return $stmt->execute([$ip]); + // Remove the IP + $stmt = $this->db->prepare("DELETE FROM {$this->whitelistTable} WHERE ip_address = ?"); + + $result = $stmt->execute([$ip]); + + if ($result && $ipDetails) { + $logMessage = sprintf( + 'IP Whitelist: Removed %s "%s" by %s. Was added by: %s', + $ipDetails['is_network'] ? 'network' : 'IP', + $ip, + $removedBy, + $ipDetails['created_by'] + ); + $this->log->insertLog($userId ?? 0, $logMessage, 'system'); + } + + return $result; + + } catch (Exception $e) { + if ($userId) { + $this->log->insertLog($userId, "IP Whitelist: Failed to remove {$ip}: " . $e->getMessage(), 'system'); + } + return false; + } } public function getWhitelistedIps() {