$ip, 'timestamp' => strtotime($timestamp)]; } } return $attempts; } private static function checkBlacklist(string $_ip){ // Vérifier si l'IP est déjà blacklistée $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; } private static function addBalcklist(string $_ip){ // Ajouter une nouvelle tentative file_put_contents(self::$log_file, "$_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . self::getFullUrl() . "\n", FILE_APPEND); } private static function check(){ // Compter les tentatives récentes if(self::isBlacklistExtention()){ $now = time(); $time_window = self::$time_window; $attempts = self::readBalcklist(); $ip = $_SERVER['REMOTE_ADDR']; $blacklisted = self::checkBlacklist($ip); self::addBalcklist($ip); $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) { return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window; }); if (count($recent_attempts) + 1 > self::$max_attempts && !self::checkBlacklist($ip)) { file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND); $blacklisted = TRUE; } if ($blacklisted == TRUE) { return "Votre IP a été blacklistée pour trop de tentatives."; exit(); } else { return "Nombre de tentatives récentes pour votre IP

" . (count($recent_attempts) + 1)."

"; } } else { return "La page que vous cherchez n'existe pas."; } } public static function itIs(){ // Est-il blacklisté $ip = $_SERVER['REMOTE_ADDR']; if(self::checkBlacklist($ip)){ echo "Votre IP (" . $ip . ") a été blacklistée pour trop de tentatives."; exit(); } } }