$ip, 'timestamp' => strtotime($timestamp)]; } } return $attempts; } /** * Vérifie si une IP est déjà blacklistée. * * @param string $_ip L'adresse IP à vérifier. * @return bool TRUE si l'IP est blacklistée, FALSE sinon. */ private static function checkBlacklist(string $_ip) { $blacklisted = FALSE; if (file_exists(self::$blacklist_file)) { $blacklisted_ips = file(self::$blacklist_file, FILE_IGNORE_NEW_LINES); $blacklisted = in_array($_ip, $blacklisted_ips); } return $blacklisted; } /** * Ajoute une tentative pour une IP dans le fichier de log. * * @param string $_ip L'adresse IP à ajouter. * @return void */ private static function addBlacklist(string $_ip) { file_put_contents(self::$log_file, "$_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . self::getFullUrl() . "\n", FILE_APPEND); } /** * Vérifie si une IP doit être blacklistée et effectue les actions nécessaires. * * @return array|null Retourne un tableau d'erreur ou NULL si aucune action n'est nécessaire. */ private static function check() { if (self::isBlacklistExtention()) { $now = time(); $time_window = self::$time_window; $attempts = self::readBlacklist(); $ip = $_SERVER['REMOTE_ADDR']; // Vérifie si l'IP est déjà blacklistée $blacklisted = self::checkBlacklist($ip); // Ajoute une tentative self::addBlacklist($ip); // Filtre les tentatives récentes $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) { return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window; }); // Si trop de tentatives, ajoute l'IP à la blacklist if (count($recent_attempts) + 1 > self::$max_attempts && !$blacklisted) { file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND); $blacklisted = true; } // Redirection si blacklisté if ($blacklisted) { header("HTTP/1.1 403 Forbidden"); header("Location: /noAccess.php"); exit(); } else { return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."]; } } else { return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."]; } } /** * Vérifie si l'utilisateur actuel est blacklisté. * * @return void */ public static function itIs() { if (self::checkBlacklist($_SERVER['REMOTE_ADDR'])) { header("Location: /noAccess.php"); exit(); } return NULL; } }