diff options
author | floppydiskette <floppydisk@hyprcat.net> | 2024-12-27 18:47:40 +0000 |
---|---|---|
committer | floppydiskette <floppydisk@hyprcat.net> | 2024-12-27 18:47:40 +0000 |
commit | 3e97458ee28c79c16f7c792d96c01c44ea2356ed (patch) | |
tree | f661ecfc28ffc0a37e01420f91112daf13a8fa66 | |
parent | 0a8db68e42956d4d6644e501b8822186f89ec559 (diff) |
rudimentary "spam filter"v2024.12.27
-rw-r--r-- | app/Http/Controllers/GuestbookController.php | 14 | ||||
-rw-r--r-- | app/Http/Kernel.php | 1 | ||||
-rw-r--r-- | app/Http/Middleware/GuestbookValidate.php | 37 | ||||
-rw-r--r-- | config/app.php | 2 | ||||
-rw-r--r-- | resources/views/errors/guestbook-invalid.blade.php | 12 | ||||
-rw-r--r-- | routes/web.php | 1 |
6 files changed, 52 insertions, 15 deletions
diff --git a/app/Http/Controllers/GuestbookController.php b/app/Http/Controllers/GuestbookController.php index df726ef..3fd179b 100644 --- a/app/Http/Controllers/GuestbookController.php +++ b/app/Http/Controllers/GuestbookController.php @@ -27,21 +27,7 @@ class GuestbookController extends Controller { * @throws ValidationException */ public function addEntry(Request $request): RedirectResponse { - $this->validate($request, [ - 'name' => 'required', - 'message' => 'required' - ]); - - GuestbookEntry::insertGuestbookEntry($request); return back()->with('success', 'Entry submitted successfully!'); } - - public function banIP(string $addr) { - // TODO: Add banning system - // $matching_bans = DB::select('SELECT reason FROM guestbook__bans WHERE ip_address = ?', array($request->ip())); - // if (!empty($matching_bans)) { - // return view('errors.guestbook-ipban')->with('reason', $matching_bans[0]->reason); - // } - } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 4eab7b8..d6127bc 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -47,6 +47,7 @@ class Kernel extends HttpKernel protected $routeMiddleware = [ 'rate_limit' => \App\Http\Middleware\RateLimiter::class, + 'validator' => \App\Http\Middleware\GuestbookValidate::class, ]; diff --git a/app/Http/Middleware/GuestbookValidate.php b/app/Http/Middleware/GuestbookValidate.php new file mode 100644 index 0000000..b2218bb --- /dev/null +++ b/app/Http/Middleware/GuestbookValidate.php @@ -0,0 +1,37 @@ +<?php + +namespace App\Http\Middleware; + +use Closure; +use Illuminate\Http\Request; +use Symfony\Component\HttpFoundation\Response; + +class GuestbookValidate +{ + /** + * Handle an incoming request. + * + * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + */ + public function handle(Request $request, Closure $next): Response + { + if ( + !$request->validate([ + 'name' => 'required', + 'message' => 'required' + ]) || + $this->containsUrl($request->get('message')) || + $this->containsUrl($request->get('name')) + ) { + return response()->view('errors.guestbook-invalid', [], 400); + } + return $next($request); + } + + public function containsUrl($str) { + $matches = []; + $pattern = '/\b(?:https?|ftp|www)(:\/\/)*[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i'; + preg_match_all($pattern, $str, $matches); + return count($matches[0]); + } +} diff --git a/config/app.php b/config/app.php index 7a3c2bb..61bb148 100644 --- a/config/app.php +++ b/config/app.php @@ -5,7 +5,7 @@ use Illuminate\Support\ServiceProvider; return [ 'name' => env('APP_NAME', 'diskfloppy.me'), - 'version' => '2024.12.25', + 'version' => '2024.12.27', 'env' => env('APP_ENV', 'production'), 'debug' => (bool) env('APP_DEBUG', false), 'url' => env('APP_URL', 'http://localhost'), diff --git a/resources/views/errors/guestbook-invalid.blade.php b/resources/views/errors/guestbook-invalid.blade.php new file mode 100644 index 0000000..5fc6fbf --- /dev/null +++ b/resources/views/errors/guestbook-invalid.blade.php @@ -0,0 +1,12 @@ +<x-minimal> + <x-slot:title>Error dsdf!</x-slot:title> + <div class="page-container"> + <div> + <h1 style="margin-top: 0">Error 400: Invalid message!</h1> + <hr> + <p>Whoa there! Your form submission seems to contain a URL (or one of the fields was left blank)!</p> + <br> + Click <a href="/guestbook">here</a> to go back to the guestbook. + </div> + </div> +</x-minimal> diff --git a/routes/web.php b/routes/web.php index 8af223c..09837dd 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,4 +25,5 @@ Route::get('/guestbook', [GuestbookController::class, 'show']); Route::get('/music', [MusicController::class, 'show']); Route::get('/rosco', [RoscoController::class, 'show']); Route::post('/guestbook', [GuestbookController::class, 'addEntry']) + ->middleware('validator') ->middleware('rate_limit'); |