From a076c28a306d9f9c9e30e3e9a7b4c37e6a18dc5c Mon Sep 17 00:00:00 2001
From: Yasen Pramatarov <yasen@lindeas.com>
Date: Fri, 3 Jan 2025 18:08:59 +0200
Subject: [PATCH] Cleanup expired IP entries

---
 app/classes/ratelimitrer.php | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/app/classes/ratelimitrer.php b/app/classes/ratelimitrer.php
index 76b10ca..7e8481c 100644
--- a/app/classes/ratelimitrer.php
+++ b/app/classes/ratelimitrer.php
@@ -310,6 +310,25 @@ class RateLimiter {
         return $stmt->fetchAll(PDO::FETCH_ASSOC);
     }
 
+    public function cleanupExpiredEntries() {
+        try {
+            // Remove expired blacklist entries
+            $stmt = $this->db->prepare("DELETE FROM {$this->blacklistTable}
+                WHERE expiry_time IS NOT NULL AND expiry_time < NOW()");
+            $stmt->execute();
+
+            // Clean old login attempts
+            $stmt = $this->db->prepare("DELETE FROM {$this->tableName}
+                WHERE attempted_at < DATE_SUB(NOW(), INTERVAL ? MINUTE)");
+            $stmt->execute([$this->decayMinutes]);
+
+            return true;
+        } catch (Exception $e) {
+            $this->log->insertLog(0, "Failed to cleanup expired entries: " . $e->getMessage(), 'system');
+            return false;
+        }
+    }
+
     public function attempt($username, $ipAddress) {
         // Skip rate limiting for whitelisted IPs
         if ($this->isIpWhitelisted($ipAddress)) {