From 8bdcd5b0864ee1e428e096d9e307eaf9ba0bcada Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Sat, 24 Feb 2024 21:52:00 +0100 Subject: [PATCH] feat(contact): Add hCaptcha integration, Discord alert for contact - Added hCaptcha validation to the contact form. - Integrated hCaptcha configuration in the application. - Implemented Discord alerts for new contact submissions with detailed content. --- .env.example | 3 + .../ContactSubmissionController.php | 15 +++ .../Requests/ContactSubmissionRequest.php | 13 +- config/HCaptcha.php | 10 ++ config/app.php | 2 + resources/views/contact/index.blade.php | 126 +++++++++++++----- 6 files changed, 131 insertions(+), 38 deletions(-) create mode 100644 config/HCaptcha.php diff --git a/.env.example b/.env.example index 9fb45a4..fd27e23 100644 --- a/.env.example +++ b/.env.example @@ -20,6 +20,9 @@ GTAG_MEASUREMENT_ID="G-XXXXXXXXXX" DISCORD_ALERT_WEBHOOK="https://discord.com/api/webhooks/000000000000000000/" HONEYPOT_NAME="honeypot" +HCAPTCHA_SECRET=secret-key +HCAPTCHA_SITEKEY=site-key + LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug diff --git a/app/Http/Controllers/ContactSubmissionController.php b/app/Http/Controllers/ContactSubmissionController.php index 46e063a..2009084 100644 --- a/app/Http/Controllers/ContactSubmissionController.php +++ b/app/Http/Controllers/ContactSubmissionController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Http\Requests\ContactSubmissionRequest; use App\Models\ContactSubmission; +use Spatie\DiscordAlerts\Facades\DiscordAlert; class ContactSubmissionController extends Controller { @@ -16,6 +17,20 @@ class ContactSubmissionController extends Controller { $contactSubmission = ContactSubmission::create($request->validated()); + $descriptionContent = "**Name**: {$contactSubmission->name}\n\n**Email**: {$contactSubmission->email}\n\n**Category**: {$contactSubmission->category->humanReadable()}\n\n**Subject**: {$contactSubmission->subject}\n\n**Message**: {$contactSubmission->message}"; + + if ($contactSubmission->discord) { + $descriptionContent .= "\n\n\n**Discord**: {$contactSubmission->discord}"; + } + + DiscordAlert::message("There is a new contact submission from {$contactSubmission->name} ({$contactSubmission->email}).", [ + [ + 'title' => "{$contactSubmission->category->humanReadable()} - {$contactSubmission->subject}", + 'description' => $descriptionContent, + 'color' => '#ff8a4c', + ] + ]); + return redirect()->route('contact.index')->with('success', 'Your message has been sent!'); } } diff --git a/app/Http/Requests/ContactSubmissionRequest.php b/app/Http/Requests/ContactSubmissionRequest.php index 28bf099..d20c1bf 100644 --- a/app/Http/Requests/ContactSubmissionRequest.php +++ b/app/Http/Requests/ContactSubmissionRequest.php @@ -11,10 +11,11 @@ class ContactSubmissionRequest extends FormRequest return [ 'name' => ['required', 'max:254'], 'email' => ['required', 'email', 'max:254'], - 'discord' => ['nullable', 'min:2', 'max:34'], + 'discord' => ['nullable', 'min:2', 'max:35'], 'category' => ['required', 'in:question,advertising,bug_report,feedback,other'], 'subject' => ['required', 'max:254'], 'message' => ['required', 'unique:contact_submissions', 'max:3500'], + 'h-captcha-response' => 'required|HCaptcha' ]; } @@ -22,4 +23,14 @@ class ContactSubmissionRequest extends FormRequest { return true; } + + public function messages(): array + { + return [ + 'h-captcha-response.required' => 'Please verify that you are not a robot.', + 'h-captcha-response.h_captcha' => 'Failed to validate captcha.', + 'category.in' => 'Invalid category.', + 'message.unique' => 'You have already submitted this message.', + ]; + } } diff --git a/config/HCaptcha.php b/config/HCaptcha.php new file mode 100644 index 0000000..a58a2fb --- /dev/null +++ b/config/HCaptcha.php @@ -0,0 +1,10 @@ + env('HCAPTCHA_SECRET'), + 'sitekey' => env('HCAPTCHA_SITEKEY'), + 'server-get-config' => false, + 'options' => [ + 'timeout' => 30, + ], +]; diff --git a/config/app.php b/config/app.php index dd44c6e..aa9c16b 100644 --- a/config/app.php +++ b/config/app.php @@ -170,6 +170,7 @@ return [ // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + Scyllaly\HCaptcha\HCaptchaServiceProvider::class, ])->toArray(), /* @@ -184,6 +185,7 @@ return [ */ 'aliases' => Facade::defaultAliases()->merge([ + 'HCaptcha' => Scyllaly\HCaptcha\Facades\HCaptcha::class, // 'Example' => App\Facades\Example::class, ])->toArray(), diff --git a/resources/views/contact/index.blade.php b/resources/views/contact/index.blade.php index 99de181..4ae37f9 100644 --- a/resources/views/contact/index.blade.php +++ b/resources/views/contact/index.blade.php @@ -1,44 +1,96 @@ @extends('layouts.app') @section('title', 'Heimerdinger.LoL • Contact') -@section('description', 'Contact Heimerdinger.LoL for any inquiries, feedback, or suggestions. We are always looking to - improve our website and content.') +@section('description', + 'Contact Heimerdinger.LoL for any inquiries, feedback, or suggestions. We are always looking to + improve our website and content.') + + @push('top_scripts') + {!! HCaptcha::renderJs() !!} + @endpush @section('content') -
- @csrf - @honeypot -
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- +
+

+ Get in Contact

+

* = required

- + @if ($errors->any()) +
+
+

There was an error with your submission

+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+
+ @endif + + @if (session('success')) +
+
+

Success

+

{{ session('success') }}

+
+
+ @endif + +
+ @csrf + @honeypot +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + {!! HCaptcha::display(['data-theme' => 'dark']) !!} +
+ +
+
@endsection