<?php
namespace App\Controller;
use Swift_Mailer;
use App\Entity\Enquiry;
use App\Form\EnquiryType;
use App\Annotation\CmsComponent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Google\Cloud\RecaptchaEnterprise\V1\Event;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Google\Cloud\RecaptchaEnterprise\V1\Assessment;
use Google\Cloud\RecaptchaEnterprise\V1\CreateAssessmentRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Google\Cloud\RecaptchaEnterprise\V1\TokenProperties\InvalidReason;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
class EnquiryDefaultController extends AbstractController
{
private ?RecaptchaEnterpriseServiceClient $client = null;
private string $errorMessage = '';
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Swift_Mailer $mailer,
private readonly string $recaptcha_key,
private readonly string $recaptcha_project_id,
private readonly string $projectRoot
) {
}
/**
* @CmsComponent("WhatsApp Block", active=true, routeName="whatsapp_block")
*/
#[Route(path: '/whatsapp-block', name: 'whatsapp_block')]
public function whatsappBlock(): Response
{
return $this->render('@theme/enquiry/whatsapp-block.html.twig');
}
/**
* @CmsComponent("Embed Enquiry Form", active=true, routeName="embed_enquiry")
*/
#[Route(path: '/pcgc-enquiry', name: 'embed_enquiry')]
public function embedEnquiry(Request $request): Response
{
$enquiry = new Enquiry();
$enquiry->setSubject('TaurusHR Website Enquiry');
// $this->setTempData($enquiry);
$form = $this->createForm(EnquiryType::class, $enquiry);
$form->handleRequest($request);
$error = false;
$success = false;
$errorMessage = '';
if ($form->isSubmitted()) {
if ($this->spamChecksPass($request)) {
if ($form->isValid()) {
$this->em->persist($enquiry);
$this->em->flush();
$success = true;
$this->sendEmail($enquiry);
} else {
$error = true;
$errorMessage = 'Error - Check the form for errors';
}
} else {
$error = true;
$errorMessage = $this->errorMessage;
}
}
return $this->render('@theme/enquiry/enquiry.html.twig', [
'enquiry' => $enquiry,
'error' => $error,
'success' => $success,
'errorMessage' => $errorMessage,
'form' => $form->createView(),
]);
}
#[Route(path: '/email-test', name: 'email_test')]
public function emailTest(): Response
{
return $this->render('@theme/emails/enquiry-confirmed.html.twig');
}
private function spamChecksPass(Request $request): bool
{
// also test form execution time
try {
$posted = $request->request->All();
// check for captcha response
$recaptcha = $posted['g-recaptcha-response'] ?? null;
if (empty($recaptcha)) {
return false;
}
// check for form execution time
$loadedAt = (int) $posted['enquiry']['_loaded_at'] ?? 0;
if ($loadedAt && (time() - $loadedAt) < 3) {
return false;
}
// check for $honeypot field
$honeypot = $posted['enquiry']['website'] ?? null;
if (!empty($honeypot)) {
return false;
}
return $this->verifyRecaptcha($recaptcha);
} catch (\Throwable $th) {
return false;
}
}
private function verifyRecaptcha($token): bool
{
$this->errorMessage = '';
if (! $this->client) {
$this->client = new RecaptchaEnterpriseServiceClient([
'credentials' => $this->projectRoot . '/recaptcha-credentials.json',
]);
$projectName = $this->client->projectName($this->recaptcha_project_id);
}
// Set the properties of the event to be tracked.
$event = (new Event())
->setSiteKey($this->recaptcha_key)
->setToken($token);
$assessment = (new Assessment())
->setEvent($event);
$request = (new CreateAssessmentRequest())
->setParent($projectName)
->setAssessment($assessment);
try {
$response = $this->client->createAssessment($request);
if ($response->getTokenProperties()->getValid() == false) {
$this->errorMessage = InvalidReason::name($response->getTokenProperties()->getInvalidReason());
return false;
}
$score = $response->getRiskAnalysis()->getScore();
if ($score < 0.5) {
$this->errorMessage = 'Low score: ' . $score;
return false;
}
} catch (\Exception $e) {
return false;
}
return true;
}
private function sendEmail(Enquiry $enquiry): void
{
$message_to_client = (new \Swift_Message())
->setSubject('Enquiry Received via '.$this->getParameter('sitename').' website')
->setFrom($this->getParameter('email_norely'))
->setTo($this->getParameter('email_primary'))
->setBody(
$this->renderView('@theme/emails/enquiry-to-client.html.twig', ['enquiry' => $enquiry]),
'text/html'
)
;
$this->mailer->send($message_to_client);
$message_to_user = (new \Swift_Message())
->setSubject('Enquiry sent to '.$this->getParameter('sitename').' confirmed')
->setFrom($this->getParameter('email_norely'))
->setTo($enquiry->getEmail())
->setBody(
$this->renderView(
'@theme/emails/enquiry-confirmed.html.twig',
['enquiry' => $enquiry]
),
'text/html'
)
;
$this->mailer->send($message_to_user);
}
private function setTempData(Enquiry $enquiry): void
{
$enquiry->setName('Test Recaptcha');
$enquiry->setContactNumber('0400000000');
$enquiry->setEmail('dev@thetlb.co.uk');
$enquiry->setInterestedService('HR');
$enquiry->setMessage('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat.');
}
}