From 9b8f92f2eb0b0b62525ba3c4f774cf040da64850 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Sat, 21 Dec 2024 17:14:31 +0200 Subject: [PATCH] Enhances add to whitelist --- app/classes/ratelimitrer.php | 39 +++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/app/classes/ratelimitrer.php b/app/classes/ratelimitrer.php index 9f002ef..5b96199 100644 --- a/app/classes/ratelimitrer.php +++ b/app/classes/ratelimitrer.php @@ -89,16 +89,37 @@ class RateLimiter { } // Add to whitelist - public function addToWhitelist($ip, $isNetwork = false, $description = '', $createdBy = 'system') { - $stmt = $this->db->prepare("INSERT INTO {$this->whitelistTable} - (ip_address, is_network, description, created_by) - VALUES (?, ?, ?, ?) - ON DUPLICATE KEY UPDATE - is_network = VALUES(is_network), - description = VALUES(description), - created_by = VALUES(created_by)"); + public function addToWhitelist($ip, $isNetwork = false, $description = '', $createdBy = 'system', $userId = null) { + try { + $stmt = $this->db->prepare("INSERT INTO {$this->whitelistTable} + (ip_address, is_network, description, created_by) + VALUES (?, ?, ?, ?) + ON DUPLICATE KEY UPDATE + is_network = VALUES(is_network), + description = VALUES(description), + created_by = VALUES(created_by)"); - return $stmt->execute([$ip, $isNetwork, $description, $createdBy]); + $result = $stmt->execute([$ip, $isNetwork, $description, $createdBy]); + + if ($result) { + $logMessage = sprintf( + 'IP Whitelist: Added %s "%s" by %s. Description: %s', + $isNetwork ? 'network' : 'IP', + $ip, + $createdBy, + $description + ); + $this->log->insertLog($userId ?? 0, $logMessage, 'system'); + } + + return $result; + + } catch (Exception $e) { + if ($userId) { + $this->log->insertLog($userId, "IP Whitelist: Failed to add {$ip}: " . $e->getMessage(), 'system'); + } + return false; + } } // Remove from whitelist