src/Controller/WidgetRGPDController.php line 114

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AccountingFirm;
  4. use App\Entity\EmailBlacklist;
  5. use App\Entity\RgpdContact;
  6. use App\Repository\AccountingFirmRepository;
  7. use App\Repository\AuthorizedDomainRepository;
  8. use App\Services\WidgetMailing;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Qferrer\Mjml\Twig\MjmlExtension;
  11. use Response;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Twig\Environment;
  20. class WidgetRGPDController extends AbstractController
  21. {
  22.   protected MjmlExtension $mjmlRenderer;
  23.   protected Environment $twig;
  24.   public function __construct(MailerInterface $mailerMjmlExtension $mjmlRendererEnvironment $twig)
  25.     {
  26.         $this->mjmlRenderer $mjmlRenderer;
  27.         $this->twig $twig;
  28.     }
  29.   #[Route('/check-rgpd'name'w_rgpd_check')]
  30.   public function ctrl_checkRgpd(Request $requestAccountingFirmRepository $repositorystring $prefix null): JsonResponse
  31.   {
  32.     $token $request->query->get('token');
  33.     $host $request->get('host');
  34.     if ($token && $host) {
  35.       $accountingFirm $repository->findOneBy(['rgpdToken' => $token]);
  36.       $isAuthorized false;
  37.       if ($accountingFirm && in_array($host$accountingFirm->getListAuthorizedDomains())) {
  38.         $isAuthorized true;
  39.       }
  40.       $url $isAuthorized $accountingFirm->getRgpdWidgetUrl() : null;
  41.     } else {
  42.       $accountingFirm $repository->findOneBy(['host' => $host]);
  43.       $url $accountingFirm $prefix $this->generateUrl('w_rgpd_get') : null;
  44.     }
  45.     return $this->json($url);
  46.   }
  47.   #[Route('/widget-rgpd'name'w_rgpd_get')]
  48.   public function ctrl_widgetRgpd(Request $requestAccountingFirmRepository $repositoryAuthorizedDomainRepository $domains)
  49.   {
  50.     $token $request->query->get('token');
  51.     $host $request->get('host');
  52.     if ($host != "preview") {
  53.       $accountingFirm $repository->findOneBy(['host' => $host]);
  54.       if (is_null($accountingFirm)) {
  55.         $domains $domains->findBy(['name' => $host]);
  56.         if (is_null($domains)) {
  57.           throw new NotFoundHttpException();
  58.         }
  59.         foreach ($domains as $dom) {
  60.           $tmp_ac $dom->getAccountingFirm();
  61.           if ($tmp_ac->getRgpdToken() == $token) {
  62.             $accountingFirm $tmp_ac;
  63.             break;
  64.           }
  65.         }
  66.       }
  67.       if (is_null($accountingFirm)) {
  68.         throw new NotFoundHttpException();
  69.       }
  70.     }
  71.     return $this->render('widget_rgpd/content.html.twig', [
  72.       'cabinet' => $accountingFirm,
  73.     ]);
  74.   }
  75.   #[Route('/embed'name'w_rgpd_embed')]
  76.   public function embed()
  77.   {
  78.     $root $this->getParameter('kernel.project_dir');
  79.     $path $root '/public/widgets/rgpd/widget_rgpd.js';
  80.     return new BinaryFileResponse($path);
  81.   }
  82.   #[Route('/{filename}.{format}'name'w_rgpd_files')]
  83.   public function rgpd_files(string $filenamestring $format)
  84.   {
  85.       $allowed = array('png''jpg''jpeg''gif''js''svg''eot''ttf''woff');
  86.       if (in_array($format$allowed)) {
  87.           $root $this->getParameter('kernel.project_dir');
  88.           $path $root '/public/rgpd/' $filename '.' $format;
  89.           //dd(file_exists($path), $path, $request->getRequestFormat());
  90.           return new BinaryFileResponse($path);
  91.       }
  92.       return false;
  93.   }
  94.   #[Route('/send'name'rgpd_send_form')]
  95.   public function rgpd_send(Request $requestEntityManagerInterface $emAccountingFirmRepository $accountingFirmRepositoryWidgetMailing $mailing)
  96.   {
  97.     $email $request->get('email');
  98.     $tel $request->get('tel');
  99.     $name $request->get('name');
  100.     $idCabinet $request->get('cabinet');
  101.     $cabinet $accountingFirmRepository->findById($idCabinet);
  102.     if (!empty($email) && !empty($name) && !empty($tel) && !empty($cabinet)) {
  103.       // Vérifier si l'email est dans la blacklist
  104.       $blacklistRepo $em->getRepository(EmailBlacklist::class);
  105.       if ($blacklistRepo->isEmailBlacklisted($email)) {
  106.         // Incrémenter le compteur de tentatives
  107.         $blacklistedEmail $blacklistRepo->findByEmail($email);
  108.         if ($blacklistedEmail) {
  109.           $blacklistedEmail->incrementBlockCount();
  110.           $em->persist($blacklistedEmail);
  111.           $em->flush();
  112.         }
  113.         return new JsonResponse(['status' => 'error''message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer le message.'], 403);
  114.       }
  115.       $rgpdContact = new RgpdContact;
  116.       $rgpdContact->setEmail($email);
  117.       $rgpdContact->setPhone($tel);
  118.       $rgpdContact->setName($name);
  119.       $rgpdContact->setAccountingFirm($cabinet);
  120.       $rgpdContact->updateTimestamps();
  121.       $em->persist($rgpdContact);
  122.       $em->flush();
  123.       //send mail
  124.       $mailing->sendRgpdClient($cabinet, [ //Envoie au client
  125.         'name' => $name,
  126.         'email' => $email,
  127.       ]);
  128.       if ($cabinet->isRgpdExpertExterneUse()) {  // si le cabinet utilise son propre expert
  129.         $mailing->sendRgpdAgenceExpertExterne($cabinet, [ // envoie à lagence.expert
  130.           'name' => $name,
  131.           'email' => $email,
  132.           'tel' => $tel,
  133.         ]);
  134.         $mailing->sendRgpdExpertExterne($cabinet, [ //envoie à l'expert du cabinet
  135.           'name' => $name,
  136.           'email' => $email,
  137.           'tel' => $tel,
  138.         ]);
  139.       } else { //si c'est un partenaire de lagence.expert
  140.         $mailing->sendRgpdAgenceExpertInterne($cabinet, [  // envoie à lagence.expert
  141.           'name' => $name,
  142.           'email' => $email,
  143.           'tel' => $tel,
  144.         ]);
  145.         $mailing->sendRgpdExpertInterne($cabinet, [ // envoie a partenaire de lagence.expert
  146.           'name' => $name,
  147.           'email' => $email,
  148.           'tel' => $tel,
  149.         ]);
  150.         $mailing->sendRgpdCabinetInterne($cabinet, [ // envoie au cabinet concerné
  151.           'name' => $name,
  152.           'email' => $email,
  153.           'tel' => $tel,
  154.         ]);
  155.       }
  156.       return new JsonResponse('success');
  157.     }
  158.     return new JsonResponse('error');
  159.   }
  160. }