GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/app/Livewire/Pages/Contact.php
<?php

namespace App\Livewire\Pages;

use Livewire\Component;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;

class Contact extends Component
{
    public $email;
    public $name;
    public $message;
    public $honey;
    public $recaptcha;

    protected $rules = [
        'name' => 'required|string|max:255',
        'email' => 'required|valid_email_domain|email|max:255|unique:contacts,email',
        'message' => 'required|string|max:255',
        'recaptcha' => 'required',
        'honey' => 'nullable'
    ];

    public function contactUs()
    {
        $this->validate();
        try {

            if ($this->honey) {
                session()->flash('error', 'Spam detected.');
                return;
            }

            $response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
                'secret' => config('recaptcha.api_secret_key'),
                'response' => $this->recaptcha
            ]);
            $responseBody = $response->json();

            if (!$responseBody['success']) {
                session()->flash('error', 'reCAPTCHA verification failed. Please try again.');
                return;
            }

            // Process the contact form submission
            // Save to DB
            // Inside your method after validation and reCAPTCHA verification
            $contact = DB::table('contacts')->insert([
                'email' => $this->email,
                'name' => $this->name,
                'message' => $this->message,
                'honey' => $this->honey,
                'recaptcha' => $this->recaptcha,
                'created_at' => now(),
            ]);

            // Check if the insertion was successful
            if ($contact) {
                session()->flash('success', 'Thank you. We will get back to you');
                return redirect()->to('/contact');
            } else {
                session()->flash('error', 'Failed to create contact record.');
            }
        } catch (\Throwable $th) {
            session()->flash('error', $th->getMessage());
            $this->addError('message', $th->getMessage());

        }
    }

    public function render()
    {
        return view('livewire.pages.contact');
    }
}