src/Controller/Blog/HomepageController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Blog;
  3. use App\Entity\Pages\Pages;
  4. use App\Entity\Pages\PagesHasBlocks;
  5. use App\Form\Articles\ContactForm;
  6. use App\Services\Core\Translations;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Knp\Component\Pager\PaginatorInterface;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  24. use Symfony\Component\Routing\RouterInterface;
  25. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Contracts\HttpClient\HttpClientInterface;
  28. use App\Security\LoginFormAuthenticator;
  29. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  30. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  31. use Twig\Environment;
  32. use Symfony\Component\Mailer\Transport;
  33. use Symfony\Component\Mailer\Mailer;
  34. use Symfony\Component\Mime\Address;
  35. use Symfony\Component\Mime\Email;
  36. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  37. use Symfony\Component\Mime\Part\DataPart;
  38. class HomepageController extends AbstractController
  39. {
  40.     private $em;
  41.     private $paginator;
  42.     private $params;
  43.     private $passwordEncoder;
  44.     private $tokenGenerator;
  45.     private $httpClient;
  46.     private $us;
  47.     private $ts;
  48.     private $authenticator;
  49.     private $guardHandler;
  50.     private $mail;
  51.     private $twig;
  52.     public function __construct(EntityManagerInterface $em,
  53.                                 PaginatorInterface $paginator,
  54.                                 UserPasswordEncoderInterface $passwordEncoder,
  55.                                 TokenGeneratorInterface $tokenGenerator,
  56.                                 ParameterBagInterface $params,
  57.                                 HttpClientInterface $httpClient,
  58.                                 \App\Services\Core\Users $us,
  59.                                 Translations $translationService,
  60.                                 LoginFormAuthenticator $authenticator,
  61.                                 GuardAuthenticatorHandler $guardHandler,
  62.                                 \App\Services\Mailing\Mails $mail,
  63.                                 Environment $twig
  64.     ) {
  65.         $this->em = $em;
  66.         $this->paginator = $paginator;
  67.         $this->passwordEncoder = $passwordEncoder;
  68.         $this->tokenGenerator = $tokenGenerator;
  69.         $this->params = $params;
  70.         $this->httpClient = $httpClient;
  71.         $this->us = $us;
  72.         $this->ts = $translationService;
  73.         $this->authenticator = $authenticator;
  74.         $this->guardHandler = $guardHandler;
  75.         $this->mail = $mail;
  76.         $this->twig = $twig;
  77.     }
  78.     public function homepage(Request $request): Response
  79.     {
  80.         $locale = $request->getLocale();
  81.         $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => $locale]);
  82.         if($page == null) {
  83.             $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => "en"]);
  84.         }
  85.         $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod'],['sequence' => 'ASC']);
  86.         $page->setViews((int)$page->getViews() + 1);
  87.         $this->em->persist($page);
  88.         $this->em->flush();
  89.         return $this->render('vitrine/homepage.html.twig', [
  90.             'page' => $page,
  91.             'blocks' => $blocks
  92.         ]);
  93.     }
  94.     public function error(Request $request): Response
  95.     {
  96.         $locale = $request->getLocale();
  97.         return $this->render('vitrine/error.html.twig');
  98.     }
  99.     public function contact(Request $request): Response
  100.     {
  101.         $session = $request->getSession();
  102.         $user = $this->getUser();
  103.         $locale = $request->getLocale();
  104.         $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => $locale]);
  105.         if($page == null) {
  106.             $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => "fr"]);
  107.         }
  108.         $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
  109.         $page->setViews((int)$page->getViews() + 1);
  110.         $this->em->persist($page);
  111.         $this->em->flush();
  112.         $form = $this->createForm(ContactForm::class);
  113.         $form->handleRequest($request);
  114.         if ($form->isSubmitted() && $form->isValid()) {
  115.             $data = $request->request->all();
  116.             $data = $data['contact_form'];
  117.             // Honeypot antispam
  118.             if (!empty($data['website'])) {
  119.                 if ($locale == "fr") {
  120.                     return $this->redirectToRoute('contact');
  121.                 }
  122.                 return $this->redirectToRoute('locale_contact', ['_locale' => $locale]);
  123.             }
  124.             // reCAPTCHA v2 (optionnel via RECAPTCHA_ENABLED)
  125.             $recaptchaEnabled = filter_var($_ENV["RECAPTCHA_ENABLED"] ?? false, FILTER_VALIDATE_BOOLEAN);
  126.             if ($recaptchaEnabled) {
  127.                 $recaptchaResponse = $request->request->get('g-recaptcha-response');
  128.                 $captchaValid = false;
  129.                 if (!empty($recaptchaResponse)) {
  130.                     try {
  131.                         $verify = $this->httpClient->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
  132.                             'body' => [
  133.                                 'secret'   => $_ENV["RECAPTCHA_SECRET_KEY"],
  134.                                 'response' => $recaptchaResponse,
  135.                                 'remoteip' => $request->getClientIp(),
  136.                             ],
  137.                         ]);
  138.                         $result = $verify->toArray(false);
  139.                         $captchaValid = $result['success'] ?? false;
  140.                     } catch (\Throwable $e) {
  141.                         $captchaValid = false;
  142.                     }
  143.                 }
  144.                 if (!$captchaValid) {
  145.                     if ($locale == "fr") {
  146.                         $session->getFlashBag()->add('error', 'Veuillez valider le captcha');
  147.                         return $this->redirectToRoute('contact');
  148.                     }
  149.                     $session->getFlashBag()->add('error', 'Please validate the captcha');
  150.                     return $this->redirectToRoute('locale_contact', ['_locale' => $locale]);
  151.                 }
  152.             }
  153.             $emailFrom = $_ENV["CONTACTFORM_EMAILFROM"];
  154.             $emailTo = $_ENV["CONTACTFORM_EMAILTO"];
  155.             $replaceTitle = $_ENV["CONTACTFORM_SUBJECT"];
  156.             $dsn =  $_ENV["MAILER_DSN"];
  157.             $transport = Transport::fromDsn($dsn);
  158.             $mailer = new Mailer($transport);
  159.             $message = (new TemplatedEmail())
  160.                 ->from(new Address($emailFrom,$_ENV["CONTACTFORM_NAME"]))
  161.                 ->to($emailTo)
  162.                 ->subject($replaceTitle)
  163.                 ->html($this->twig->render("emails/contact.html.twig",[
  164.                     'name' => $data['name'],
  165.                     'lastname' => $data['lastname'],
  166.                     'email' => $data['email'],
  167.                     'phone' => $data['phone'],
  168.                     'enterprise' => $data['enterprise'],
  169.                     'message' => $data['message'],
  170.                 ]))
  171.             ;
  172.             $mailer->send($message);
  173.             if($locale == "fr") {
  174.                 $session->getFlashBag()->add('success', 'Mail envoyé');
  175.                 return $this->redirectToRoute('contact');
  176.             }
  177.             $session->getFlashBag()->add('success', 'Email sending');
  178.             return $this->redirectToRoute('locale_contact',['_locale' => $locale]);
  179.         }
  180.         return $this->render('vitrine/contact.html.twig', [
  181.             'page' => $page,
  182.             'blocks' => $blocks,
  183.             'form' => $form->createView()
  184.         ]);
  185.     }
  186. }