From 35e28350bdada9e93e4b9bc5b185560998bc72e2 Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 26 Aug 2024 15:19:17 +0200 Subject: [PATCH] Change spaces to tabs --- system/functions.php | 2 +- system/src/RateLimit.php | 185 +++++++++++++++++++-------------------- 2 files changed, 93 insertions(+), 94 deletions(-) diff --git a/system/functions.php b/system/functions.php index 70c0b93e..f177d997 100644 --- a/system/functions.php +++ b/system/functions.php @@ -1041,7 +1041,7 @@ function load_config_lua($filename) return $result; } -function str_replace_first($search, $replace, $subject) { +function str_replace_first($search,$replace, $subject) { $pos = strpos($subject, $search); if ($pos !== false) { return substr_replace($subject, $replace, $pos, strlen($search)); diff --git a/system/src/RateLimit.php b/system/src/RateLimit.php index 8a90e476..40fe4c90 100644 --- a/system/src/RateLimit.php +++ b/system/src/RateLimit.php @@ -6,116 +6,115 @@ namespace MyAAC; class RateLimit { + public string $key; + public int $max_attempts; + public int $ttl; + public $enabled = false; + protected array $data; - public string $key; - public int $max_attempts; - public int $ttl; - public $enabled = false; - protected array $data; + public function __construct(string $key, int $max_attempts, int $ttl) + { + $this->key = $key; + $this->max_attempts = $max_attempts; + $this->ttl = $ttl; + } - public function __construct(string $key, int $max_attempts, int $ttl) - { - $this->key = $key; - $this->max_attempts = $max_attempts; - $this->ttl = $ttl; - } + public function attempts(string $ip): int + { + if (!$this->enabled) { + return 0; + } - public function attempts(string $ip): int - { - if (!$this->enabled) { - return 0; - } + if (isset($this->data[$ip]['attempts'])) { + return $this->data[$ip]['attempts']; + } - if (isset($this->data[$ip]['attempts'])) { - return $this->data[$ip]['attempts']; - } + return 0; + } - return 0; - } + public function exceeded(string $ip): bool { + if (!$this->enabled) { + return false; + } - public function exceeded(string $ip): bool { - if (!$this->enabled) { - return false; - } + return $this->attempts($ip) > $this->max_attempts; + } - return $this->attempts($ip) > $this->max_attempts; - } + public function increment(string $ip): bool + { + global $cache; + if ($this->enabled && $cache->enabled()) { + if (isset($this->data[$ip]['attempts']) && isset($this->data[$ip]['last'])) { + $this->data[$ip]['attempts']++; + $this->data[$ip]['last'] = time(); + } else { + $this->data[$ip] = [ + 'attempts' => 1, + 'last' => time(), + ]; + } - public function increment(string $ip): bool - { - global $cache; - if ($this->enabled && $cache->enabled()) { - if (isset($this->data[$ip]['attempts']) && isset($this->data[$ip]['last'])) { - $this->data[$ip]['attempts']++; - $this->data[$ip]['last'] = time(); - } else { - $this->data[$ip] = [ - 'attempts' => 1, - 'last' => time(), - ]; - } + $this->save(); + } - $this->save(); - } + return false; + } - return false; - } + public function reset(string $ip): void + { + if (!$this->enabled) { + return; + } - public function reset(string $ip): void - { - if (!$this->enabled) { - return; - } + if (isset($this->data[$ip])) { + unset($this->data[$ip]); + } - if (isset($this->data[$ip])) { - unset($this->data[$ip]); - } + $this->save(); + } - $this->save(); - } + public function save(): void + { + global $cache; + if (!$this->enabled) { + return; + } - public function save(): void - { - global $cache; - if (!$this->enabled) { - return; - } + $data = $this->data; + $cache->set($this->key, serialize($data), $this->ttl * 60); + } - $data = $this->data; - $cache->set($this->key, serialize($data), $this->ttl * 60); - } + public function load(): void + { + global $cache; + if (!$this->enabled) { + return; + } - public function load(): void - { - global $cache; - if (!$this->enabled) { - return; - } + $data = []; + if ($this->enabled && $cache->enabled()) { + $tmp = ''; + if ($cache->fetch($this->key, $tmp)) { + $data = unserialize($tmp); + $to_remove = []; + foreach ($data as $ip => $t) { + if (time() - $t['last'] >= ($this->ttl * 60)) { + $to_remove[] = $ip; + } + } - $data = []; - if ($this->enabled && $cache->enabled()) { - $tmp = ''; - if ($cache->fetch($this->key, $tmp)) { - $data = unserialize($tmp); - $to_remove = []; - foreach ($data as $ip => $t) { - if (time() - $t['last'] >= ($this->ttl * 60)) { - $to_remove[] = $ip; - } - } + if (count($to_remove)) { + foreach ($to_remove as $ip) { + unset($data[$ip]); + } - if (count($to_remove)) { - foreach ($to_remove as $ip) { - unset($data[$ip]); - } + $this->save(); + } + } else { + $data = []; + } + } - $this->save(); - } - } else { - $data = []; - } - } - - $this->data = $data; - } + $this->data = $data; + } }