src/Controller/ContactController.php line 30

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.                 return $this->redirectToRoute('contact');
  43.             } else {
  44.                 $response file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" $captcha_secret_key "&response=" $captcha "&remoteip=" $_SERVER['REMOTE_ADDR']);
  45.                 $response json_decode($response);
  46.                 if ($response->success === false) {
  47.                     $this->addFlash('contact.error''Erreur validation captcha !');
  48.                     return $this->redirectToRoute('contact');
  49.                 }
  50.             }
  51.             if ($response->success == true && $response->score <= 0.7) {
  52.                 $this->addFlash('contact.error''Score captcha trop faible !');
  53.                 return $this->redirectToRoute('contact');
  54.             }
  55.             $em->persist($contact);
  56.             $em->flush();
  57.             $email = (new Email())
  58.                 ->from($this->getParameter('mail_from'))
  59.                 ->to($this->getParameter('mail_to'))
  60.                 ->bcc('[email protected]')
  61.                 ->replyTo($contact->getEmail())
  62.                 ->subject("Vous avez un nouveau message via " $_SERVER['SERVER_NAME'])
  63.                 ->html($this->renderView('app/mail/contact.html.twig', array(
  64.                     'contact' => $contact,
  65.                     'base' => $_SERVER['SERVER_NAME'],
  66.                 )));
  67.             $mailer->send($email);
  68.             $this->addFlash('contact.success''contact.submitted');
  69.             return $this->redirectToRoute('contact');
  70.         }
  71.         return $this->render('app/pages/contact.html.twig', array(
  72.             'contact' => $contact,
  73.             'form' => $form->createView(),
  74.             'captcha_site_key' => $captcha_site_key,
  75.         ));
  76.     }
  77. }