aboutsummaryrefslogtreecommitdiff
path: root/app/Http/Middleware/GuestbookValidate.php
diff options
context:
space:
mode:
authorfloppydiskette <floppydisk@hyprcat.net>2024-12-27 18:47:40 +0000
committerfloppydiskette <floppydisk@hyprcat.net>2024-12-27 18:47:40 +0000
commit3e97458ee28c79c16f7c792d96c01c44ea2356ed (patch)
treef661ecfc28ffc0a37e01420f91112daf13a8fa66 /app/Http/Middleware/GuestbookValidate.php
parent0a8db68e42956d4d6644e501b8822186f89ec559 (diff)
rudimentary "spam filter"v2024.12.27
Diffstat (limited to 'app/Http/Middleware/GuestbookValidate.php')
-rw-r--r--app/Http/Middleware/GuestbookValidate.php37
1 files changed, 37 insertions, 0 deletions
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]);
+ }
+}