404.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. $visitor_ip = $_SERVER['REMOTE_ADDR'];
  4. if(!blacklistisValidIPv4($visitor_ip)){
  5. $log_file = '../blacklist/ip_attempts.log';
  6. $blacklist_file = '../blacklist/ip.txt';
  7. $max_attempts = 5;
  8. $time_window = 10 * 60; // 10 minutes en secondes
  9. // Charger les tentatives existantes
  10. $attempts = [];
  11. if (file_exists($log_file)) {
  12. $lines = file($log_file, FILE_IGNORE_NEW_LINES);
  13. foreach ($lines as $line) {
  14. list($ip, $timestamp) = explode(',', $line);
  15. $attempts[] = ['ip' => $ip, 'timestamp' => strtotime($timestamp)];
  16. }
  17. }
  18. // Vérifier si l'IP est déjà blacklistée
  19. $blacklisted = false;
  20. if (file_exists($blacklist_file)) {
  21. $blacklisted_ips = file($blacklist_file, FILE_IGNORE_NEW_LINES);
  22. $blacklisted = in_array($visitor_ip, $blacklisted_ips);
  23. }
  24. // Ajouter une nouvelle tentative
  25. file_put_contents($log_file, "$visitor_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . getFullUrl() . "\n", FILE_APPEND);
  26. // Compter les tentatives récentes
  27. $now = time();
  28. $recent_attempts = array_filter($attempts, function ($attempt) use ($visitor_ip, $now, $time_window) {
  29. return $attempt['ip'] === $visitor_ip && ($now - $attempt['timestamp']) <= $time_window;
  30. });
  31. if (count($recent_attempts) + 1 > $max_attempts && !$blacklisted) {
  32. file_put_contents($blacklist_file, "$visitor_ip\n", FILE_APPEND);
  33. $blacklisted = true;
  34. }
  35. if ($blacklisted) {
  36. header('HTTP/1.0 401 Unauthorized');
  37. echo "Votre IP ($visitor_ip) a été blacklistée pour trop de tentatives.";
  38. exit();
  39. } else {
  40. echo "Votre IP est : $visitor_ip. Nombre de tentatives récentes : " . (count($recent_attempts) + 1);
  41. }
  42. } else {
  43. echo json_encode([["error" => "404"]]);
  44. }
  45. */
  46. error_reporting(E_ALL);
  47. ini_set('display_errors', 'On');
  48. require_once "../core/class/blacklist.class.php";
  49. new blacklist;
  50. if(blacklist::isValidIPv4()){
  51. blacklist::execute("404");
  52. } else {
  53. echo json_encode([["error" => "404"]]);
  54. }
  55. ?>