File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Middleware/RateLimitMiddleware.php
Back
<?php /** * Rate Limit Middleware * Protects endpoints from abuse, spam, and brute-force attacks */ require_once __DIR__ . '/../Services/Security/RateLimiter.php'; require_once __DIR__ . '/../Helpers/Security.php'; require_once __DIR__ . '/../Core/Exceptions/RateLimitException.php'; class RateLimitMiddleware { private string $type; private int $maxAttempts; private int $decaySeconds; public function __construct(string $type = 'api_general', int $maxAttempts = 60, int $decaySeconds = 60) { $this->type = $type; $this->maxAttempts = $maxAttempts; $this->decaySeconds = $decaySeconds; $config = file_exists(__DIR__ . '/../../config/security.php') ? require __DIR__ . '/../../config/security.php' : []; if (isset($config['rate_limits'][$type])) { $this->maxAttempts = $config['rate_limits'][$type]['max']; $this->decaySeconds = $config['rate_limits'][$type]['decay']; } } public function handle(): void { $ip = Security::getClientIp(); $userId = $_SESSION['user_id'] ?? 'guest'; $key = "rl:{$this->type}:{$ip}:{$userId}"; if (RateLimiter::tooManyAttempts($key, $this->maxAttempts)) { $seconds = RateLimiter::availableIn($key); throw new RateLimitException($seconds, "คุณส่งคำขอถี่เกินไป กรุณารออีก {$seconds} วินาทีแล้วลองใหม่"); } RateLimiter::hit($key, $this->decaySeconds); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings