<?php
namespace App\Controller;
use Api\Client\ClientBuilder;
use Api\Client\Sdk;
use App\Entity\Contact;
use App\Entity\Spam;
use App\Service\Mailer;
use Doctrine\ORM\EntityManagerInterface;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\VarDumper\VarDumper;
/**
* Default controller.
*
* @Route("/contact", requirements={"_locale" = "en|fr"})
*/
class ContactController extends AbstractController
{
/**
* @Route("/", name="contact", methods={"GET", "POST"}, options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function contactAction(Request $request, MailerInterface $mailer, LoggerInterface $logger, EntityManagerInterface $em)
{
$captcha_site_key = $this->getParameter('captcha_site_key');
$captcha_secret_key = $this->getParameter('captcha_secret_key');
$contact = new Contact();
$form = $this->createForm('App\Form\ContactType', $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
} else {
$captcha = false;
}
if (!$captcha) {
$this->addFlash('contact.error', 'Erreur validation captcha !');
return $this->redirectToRoute('contact');
} else {
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $captcha_secret_key . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$response = json_decode($response);
if ($response->success === false) {
$this->addFlash('contact.error', 'Erreur validation captcha !');
return $this->redirectToRoute('contact');
} else {
if ($response->success == true && $response->score <= 0.5) {
$this->addFlash('contact.error', 'Score captcha trop faible !');
return $this->redirectToRoute('contact');
}
$spam_exist = $em->getRepository(Spam::class)->findOneBy(['email' => $contact->getEmail()]) instanceof Spam;
if ($spam_exist) {
$this->addFlash('contact.error', 'contact.spam');
return $this->redirectToRoute('contact');
}
$clientBuilder = new ClientBuilder();
$clientBuilder->addPlugin(new HeaderDefaultsPlugin([
'Accept' => 'application/json',
'apiKey' => $this->getParameter('spw_api_key'),
]));
$sdk = new Sdk($clientBuilder);
try {
$isSpam = $sdk->blacklist()->isSpam($contact->getEmail());
if ($isSpam) {
$this->addFlash('contact.error', 'contact.spam');
return $this->redirectToRoute('contact');
}
} catch (\Api\Client\HttpClient\Exception|\Exception $exception) {
}
$em->persist($contact);
$em->flush();
$email = (new Email())
->from($this->getParameter('mail_from'))
->to($this->getParameter('mail_to'))
->bcc('[email protected]')
->replyTo($contact->getEmail())
->subject("Vous avez un nouveau message via " . $_SERVER['SERVER_NAME'])
->html($this->renderView('app/mail/contact.html.twig', array(
'contact' => $contact,
'base' => $_SERVER['SERVER_NAME'],
)));
$mailer->send($email);
return $this->render('app/pages/contact_submitted.html.twig');
}
}
}
return $this->render('app/pages/contact.html.twig', array(
'contact' => $contact,
'form' => $form->createView(),
'captcha_site_key' => $captcha_site_key,
));
}
}