Skip to content
Merged

Dev #2927

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions app/Http/Controllers/ContactFormController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
Expand All @@ -10,6 +11,12 @@ class ContactFormController extends Controller
{
public function submit(Request $request)
{
// Honeypot field: bot filled
if ($request->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',
Expand All @@ -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'),
Expand All @@ -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.');
}
}
2 changes: 1 addition & 1 deletion resources/views/emails/en/contact.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
<p><strong>Subject:</strong> {{ ucfirst($data['subject']) }}</p>
@endif
<p><strong>Message:</strong></p>
<p>{{ nl2br(e($data['message'])) }}</p>
<p>{!! nl2br(e($data['message'])) !!}</p>
3 changes: 2 additions & 1 deletion resources/views/static/contact-us.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class="w-full px-6 py-3 rounded-3xl border-2 border-[#A4B8D9] bg-white appearanc
<textarea id="message" name="message" rows="6" required aria-required="true" placeholder="Enter message"
class="mt-2 px-6 py-3 w-full bg-white rounded-3xl border-2 border-[#A4B8D9] text-slate-600 placeholder-slate-200 focus:outline-none focus:ring-2 focus:ring-primary resize-none">{{ old('message') }}</textarea>
</div>

<!-- Terms -->
<label class="flex gap-2 items-start mt-10 cursor-pointer lg:items-center">
<input type="checkbox" id="terms" name="terms" required aria-required="true"
Expand All @@ -176,6 +176,7 @@ class="w-8 h-8 bg-white rounded-lg border-2 border-blue-800 text-primary focus:r
<div class="flex justify-center mt-6">
<div class="cf-turnstile"></div>
<input type="hidden" id="cf-turnstile-response" name="cf-turnstile-response" />
<input type="text" name="website" style="display:none;" tabindex="-1" autocomplete="off">
</div>

<!-- Submit -->
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@
'/training/making-and-coding',
[StaticPageController::class, 'static']
)->name('training.module-22'); */
Route::post('/contact-submit', [ContactFormController::class, 'submit'])->name('contact.submit');
Route::post('/contact-submit', [ContactFormController::class, 'submit'])
->middleware('throttle:5,1') // 5 requests per minute per IP
->name('contact.submit');
Route::get('/resources/CodingAtHome', [CodingAtHomeController::class, 'show'])->name(
'coding@home'
);
Expand Down
Loading