From 3d0b6f4880e7ddb6e9c571ee480f9872ac688bb6 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Thu, 31 Jul 2025 09:44:17 +0100 Subject: [PATCH] form fixes --- .../Controllers/ContactFormController.php | 33 ++++++++++--------- resources/views/emails/en/contact.blade.php | 2 +- resources/views/static/contact-us.blade.php | 3 +- routes/web.php | 4 ++- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/ContactFormController.php b/app/Http/Controllers/ContactFormController.php index 9f6a5edd9..0366b79ac 100644 --- a/app/Http/Controllers/ContactFormController.php +++ b/app/Http/Controllers/ContactFormController.php @@ -1,4 +1,5 @@ filled('website')) { + Log::warning('Spam detected: honeypot field filled', ['ip' => $request->ip()]); + abort(403, 'Spam detected.'); + } + $validated = $request->validate([ 'first_name' => 'required|string|max:255', 'last_name' => 'required|string|max:255', @@ -21,7 +28,7 @@ public function submit(Request $request) 'cf-turnstile-response' => env('TURNSTILE_SECRET_KEY') ? 'required' : 'nullable', ]); - // Validate Turnstile CAPTCHA if keys are present + // Verify CAPTCHA via Cloudflare Turnstile if (env('TURNSTILE_SECRET_KEY')) { $response = Http::asForm()->post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [ 'secret' => env('TURNSTILE_SECRET_KEY'), @@ -30,31 +37,27 @@ public function submit(Request $request) ]); if (!$response->json('success')) { - return redirect()->back() + return back() ->withInput() ->withErrors(['captcha' => 'CAPTCHA verification failed. Please try again.']); } } - Log::info('Contact form submitted', $validated); + Log::info('Contact form submitted', [ + 'ip' => $request->ip(), + 'name' => $validated['first_name'] . ' ' . $validated['last_name'], + 'email' => $validated['email'], + ]); $locale = app()->getLocale(); - $view = "emails.$locale.contact"; - - // Fallback to English view if the localized one doesn't exist - if (!view()->exists($view)) { - $view = 'emails.en.contact'; - } - - // Use email from .env, fallback to your address if missing - $recipientEmail = env('CONTACT_FORM_RECIPIENT_EMAIL', 'bernard@matrixinternet.ie'); + $view = view()->exists("emails.$locale.contact") ? "emails.$locale.contact" : 'emails.en.contact'; - Mail::send($view, ['data' => $validated], function ($message) use ($validated, $recipientEmail) { - $message->to($recipientEmail) + Mail::send($view, ['data' => $validated], function ($message) use ($validated) { + $message->to(env('CONTACT_FORM_RECIPIENT_EMAIL', 'bernard@matrixinternet.ie')) ->subject('New Contact Form Submission') ->replyTo($validated['email'], $validated['first_name'] . ' ' . $validated['last_name']); }); - return redirect()->back()->with('success', 'Thank you for contacting us.'); + return back()->with('success', 'Thank you for contacting us.'); } } diff --git a/resources/views/emails/en/contact.blade.php b/resources/views/emails/en/contact.blade.php index 0c0636825..bd10aa9ba 100644 --- a/resources/views/emails/en/contact.blade.php +++ b/resources/views/emails/en/contact.blade.php @@ -10,4 +10,4 @@

Subject: {{ ucfirst($data['subject']) }}

@endif

Message:

-

{{ nl2br(e($data['message'])) }}

+

{!! nl2br(e($data['message'])) !!}

diff --git a/resources/views/static/contact-us.blade.php b/resources/views/static/contact-us.blade.php index 0e828d284..486a054b6 100644 --- a/resources/views/static/contact-us.blade.php +++ b/resources/views/static/contact-us.blade.php @@ -160,7 +160,7 @@ class="w-full px-6 py-3 rounded-3xl border-2 border-[#A4B8D9] bg-white appearanc - +