51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
class RateLimiter {
|
|
private int $maxAttempts;
|
|
private int $timeWindow;
|
|
private CacheInterface $cache;
|
|
|
|
public function __construct(int $maxAttempts = 10, int $timeWindow = 60, ?CacheInterface $cache = null) {
|
|
$this->maxAttempts = $maxAttempts;
|
|
$this->timeWindow = $timeWindow;
|
|
$this->cache = $cache ?? new FileCache();
|
|
}
|
|
|
|
public function isAllowed(string $identifier): bool {
|
|
$key = 'ratelimit_' . md5($identifier);
|
|
$attempts = $this->cache->get($key) ?? [];
|
|
|
|
// Clean old attempts
|
|
$now = time();
|
|
$windowStart = $now - $this->timeWindow;
|
|
$attempts = array_filter($attempts, fn($time) => $time > $windowStart);
|
|
|
|
$attemptCount = count($attempts);
|
|
|
|
if ($attemptCount >= $this->maxAttempts) {
|
|
return false;
|
|
}
|
|
|
|
$attempts[] = $now;
|
|
$this->cache->set($key, $attempts, $this->timeWindow);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getRemainingAttempts(string $identifier): int {
|
|
$key = 'ratelimit_' . md5($identifier);
|
|
$attempts = $this->cache->get($key) ?? [];
|
|
|
|
// Clean old attempts
|
|
$now = time();
|
|
$windowStart = $now - $this->timeWindow;
|
|
$attempts = array_filter($attempts, fn($time) => $time > $windowStart);
|
|
|
|
return max(0, $this->maxAttempts - count($attempts));
|
|
}
|
|
|
|
public function reset(string $identifier): void {
|
|
$key = 'ratelimit_' . md5($identifier);
|
|
$this->cache->delete($key);
|
|
}
|
|
} |