src/Controller/ContactController.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Api\Client\ClientBuilder;
  4. use Api\Client\Sdk;
  5. use App\Entity\Contact;
  6. use App\Entity\Spam;
  7. use App\Service\Mailer;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\VarDumper\VarDumper;
  17. /**
  18.  * Default controller.
  19.  *
  20.  * @Route("/contact", requirements={"_locale" = "en|fr"})
  21.  */
  22. class ContactController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/", name="contact", methods={"GET", "POST"}, options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
  26.      */
  27.     public function contactAction(Request $requestMailerInterface $mailerLoggerInterface $loggerEntityManagerInterface $em)
  28.     {
  29.         $captcha_site_key $this->getParameter('captcha_site_key');
  30.         $captcha_secret_key $this->getParameter('captcha_secret_key');
  31.         $contact = new Contact();
  32.         $form $this->createForm('App\Form\ContactType'$contact);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             if (isset($_POST['g-recaptcha-response'])) {
  36.                 $captcha $_POST['g-recaptcha-response'];
  37.             } else {
  38.                 $captcha false;
  39.             }
  40.             if (!$captcha) {
  41.                 $this->addFlash('contact.error''Erreur validation captcha !');
  42.                 
  43.                 return $this->redirectToRoute('contact');
  44.             } else {
  45.                 $response file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" $captcha_secret_key "&response=" $captcha "&remoteip=" $_SERVER['REMOTE_ADDR']);
  46.                 $response json_decode($response);
  47.                 if ($response->success === false) {
  48.                     $this->addFlash('contact.error''Erreur validation captcha !');
  49.                     return $this->redirectToRoute('contact');
  50.                 } else {
  51.                     if ($response->success == true && $response->score <= 0.5) {
  52.                         $this->addFlash('contact.error''Score captcha trop faible !');
  53.                         return $this->redirectToRoute('contact');
  54.                     }
  55.                     $spam_exist $em->getRepository(Spam::class)->findOneBy(['email' => $contact->getEmail()]) instanceof Spam;
  56.                     if ($spam_exist) {
  57.                         $this->addFlash('contact.error''contact.spam');
  58.                         
  59.                         return $this->redirectToRoute('contact');
  60.                     }
  61.                     $clientBuilder = new ClientBuilder();
  62.                     $clientBuilder->addPlugin(new HeaderDefaultsPlugin([
  63.                         'Accept' => 'application/json',
  64.                         'apiKey' => $this->getParameter('spw_api_key'),
  65.                     ]));
  66.                     $sdk = new Sdk($clientBuilder);
  67.                     try {
  68.                         $isSpam $sdk->blacklist()->isSpam($contact->getEmail());
  69.                         if ($isSpam) {
  70.                             $this->addFlash('contact.error''contact.spam');
  71.                             
  72.                             return $this->redirectToRoute('contact');
  73.                         }
  74.                     } catch (\Api\Client\HttpClient\Exception|\Exception $exception) {
  75.                     }
  76.                     $em->persist($contact);
  77.                     $em->flush();
  78.                     $email = (new Email())
  79.                         ->from($this->getParameter('mail_from'))
  80.                         ->to($this->getParameter('mail_to'))
  81.                         ->bcc('[email protected]')
  82.                         ->replyTo($contact->getEmail())
  83.                         ->subject("Vous avez un nouveau message via " $_SERVER['SERVER_NAME'])
  84.                         ->html($this->renderView('app/mail/contact.html.twig', array(
  85.                         'contact' => $contact,
  86.                         'base'    => $_SERVER['SERVER_NAME'],
  87.                     )));
  88.                     $mailer->send($email);
  89.                     
  90.                     return $this->render('app/pages/contact_submitted.html.twig');
  91.                 }
  92.             }
  93.         }
  94.         
  95.         return $this->render('app/pages/contact.html.twig', array(
  96.             'contact'          => $contact,
  97.             'form'             => $form->createView(),
  98.             'captcha_site_key' => $captcha_site_key,
  99.         ));
  100.     }
  101. }