2023-07-16 00:49:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
class RateLimiter
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next): Response
|
|
|
|
{
|
2024-06-11 17:02:01 +00:00
|
|
|
if (auth()->check()) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
2023-07-16 00:49:09 +00:00
|
|
|
$ipAddress = $request->ip();
|
2023-07-29 17:03:13 +00:00
|
|
|
$cacheKey = 'rate_limit_'.$ipAddress;
|
2023-07-16 00:49:09 +00:00
|
|
|
|
|
|
|
if (Cache::has($cacheKey)) {
|
2023-07-29 17:03:13 +00:00
|
|
|
// If the cache key exists, the IP has submitted an entry within the last hour.
|
2023-07-16 14:30:11 +00:00
|
|
|
return response()->view('errors.guestbook-ratelimit', [], 429);
|
2023-07-16 00:49:09 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 17:03:13 +00:00
|
|
|
// Add the IP address to the cache and set the expiration time to one hour.
|
2023-07-20 02:24:35 +00:00
|
|
|
Cache::put($cacheKey, true, 3600);
|
2023-07-16 00:49:09 +00:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|