blacklist.class.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. class blacklist {
  3. private static $log_file = '../blacklist/ip_attempts.log';
  4. private static $blacklist_file = '../blacklist/ip.txt';
  5. private static $max_attempts = 5;
  6. private static $time_window = 10 * 60; // 10 minutes en secondes
  7. public static function execute(?string $_from = NULL) {
  8. self::check($_from);
  9. }
  10. public static function isValidIPv4() {
  11. return filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
  12. }
  13. private static function getFullUrl() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
  14. return $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  15. }
  16. private static function readBalcklist(){ // Charger les tentatives existantes
  17. $attempts = [];
  18. if (file_exists(self::$log_file)) {
  19. $lines = file(self::$log_file, FILE_IGNORE_NEW_LINES);
  20. foreach ($lines as $line) {
  21. list($ip, $timestamp) = explode(',', $line);
  22. $attempts[] = ['ip' => $ip, 'timestamp' => strtotime($timestamp)];
  23. }
  24. }
  25. return $attempts;
  26. }
  27. private static function checkBlacklist(string $_ip){ // Vérifier si l'IP est déjà blacklistée
  28. $blacklisted = FALSE;
  29. if (file_exists(self::$blacklist_file)) {
  30. $blacklisted_ips = file(self::$blacklist_file, FILE_IGNORE_NEW_LINES);
  31. $blacklisted = in_array($_ip, $blacklisted_ips);
  32. }
  33. return $blacklisted;
  34. }
  35. private static function addBalcklist(string $_ip){ // Ajouter une nouvelle tentative
  36. file_put_contents(self::$log_file, "$_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . self::getFullUrl() . "\n", FILE_APPEND);
  37. }
  38. private static function check(){ // Compter les tentatives récentes
  39. $now = time();
  40. $time_window = self::$time_window;
  41. $attempts = self::readBalcklist();
  42. $ip = $_SERVER['REMOTE_ADDR'];
  43. $blacklisted = self::checkBlacklist($ip);
  44. self::addBalcklist($ip);
  45. $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) {
  46. return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window;
  47. });
  48. if (count($recent_attempts) + 1 > self::$max_attempts && !self::checkBlacklist($ip)) {
  49. file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND);
  50. $blacklisted = TRUE;
  51. }
  52. if ($blacklisted == TRUE) {
  53. header('HTTP/1.0 401 Unauthorized');
  54. echo "Votre IP (" . $ip . ") a été blacklistée pour trop de tentatives.";
  55. exit();
  56. } else {
  57. echo "Votre IP est : " . $ip . ". Nombre de tentatives récentes : " . (count($recent_attempts) + 1);
  58. exit();
  59. }
  60. }
  61. public static function itIs(){ // Est-il blacklisté
  62. $ip = $_SERVER['REMOTE_ADDR'];
  63. if(self::checkBlacklist($ip)){
  64. echo "Votre IP (" . $ip . ") a été blacklistée pour trop de tentatives.";
  65. exit();
  66. }
  67. }
  68. }