src/Controller/WidgetPodcastPrivateController.php line 288

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ContactParticipationPlaceExpert;
  4. use App\Entity\EmailBlacklist;
  5. use App\Entity\PodcastPrivate;
  6. use App\Repository\AccountingFirmRepository;
  7. use App\Repository\AuthorizedDomainRepository;
  8. use App\Repository\PodcastEpisodeRepository;
  9. use App\Repository\PodcastPrivateAccountingFirmRepository;
  10. use App\Repository\PodcastPrivateRepository;
  11. use App\Services\WidgetMailing;
  12. use DateTimeImmutable;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use GuzzleHttp\Client;
  15. use Psr\Log\LoggerInterface;
  16. use Qferrer\Mjml\Twig\MjmlExtension;
  17. use Response;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. use Symfony\Component\Mailer\MailerInterface;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Twig\Environment;
  26. class WidgetPodcastPrivateController extends AbstractController
  27. {
  28.   protected MjmlExtension $mjmlRenderer;
  29.   protected Environment $twig;
  30.   public function __construct(MailerInterface $mailerMjmlExtension $mjmlRendererEnvironment $twig)
  31.   {
  32.     $this->mjmlRenderer $mjmlRenderer;
  33.     $this->twig $twig;
  34.   }
  35.   #[Route('/check-podcast-private'name'w_podcast_private_check')]
  36.   public function ctrl_checkPodcastPrivate(Request $requestAccountingFirmRepository $repositorystring $prefix null): JsonResponse
  37.   {
  38.     $token $request->query->get('token');
  39.     $host $request->get('host');
  40.     if ($token && $host) {
  41.       $accountingFirm $repository->findOneBy(['podcastPrivateToken' => $token]);
  42.       $isAuthorized false;
  43.       if ($accountingFirm && in_array($host$accountingFirm->getListAuthorizedDomains())) {
  44.         $isAuthorized true;
  45.       }
  46.       $url $isAuthorized $accountingFirm->getPodcastPrivateWidgetUrl() : null;
  47.     } else {
  48.       $accountingFirm $repository->findOneBy(['host' => $host]);
  49.       $url $accountingFirm $prefix $this->generateUrl('w_podcast_private_get') : null;
  50.     }
  51.     return $this->json($url);
  52.   }
  53.   #[Route('/widget-podcast-private'name'w_podcast_private_get')]
  54.   public function ctrl_widgetPodcastPrivate(Request $request,
  55.                                             AccountingFirmRepository $repository,
  56.                                             AuthorizedDomainRepository $domains,
  57.                                             PodcastPrivateAccountingFirmRepository $podcastPrivateAccountingFirmRepository,
  58.                                             PodcastPrivateRepository $podcastPrivateRepository,
  59.   )
  60.   {
  61.     $token $request->query->get('token');
  62.     $host $request->get('host');
  63.     $podcastId $request->get('pid');
  64.     $date $request->get('date');
  65.     if ($host != "preview") {
  66.       $accountingFirm $repository->findOneBy(['host' => $host]);
  67.       if (is_null($accountingFirm)) {
  68.         $domains $domains->findBy(['name' => $host]);
  69.         if (is_null($domains)) {
  70.           throw new NotFoundHttpException();
  71.         }
  72.         foreach ($domains as $dom) {
  73.           $tmp_ac $dom->getAccountingFirm();
  74.           if ($tmp_ac->getPodcastPrivateToken() == $token) {
  75.             $accountingFirm $tmp_ac;
  76.             break;
  77.           }
  78.         }
  79.       }
  80.       if (is_null($accountingFirm)) {
  81.         throw new NotFoundHttpException();
  82.       }
  83.     }
  84.     $p $request->getRequestUri();
  85.     $pos strpos($p'?');
  86.     if ($pos !== false) {
  87.       $p substr($p0$pos 1) . str_replace('?''&'substr($p$pos 1));
  88.     }
  89.     $route parse_url($p);
  90.     if (isset($route['query'])) {
  91.       parse_str($route['query'], $query);
  92.       $page = isset($query['p']) ? $query['p'] : null;
  93.       if (!empty($page) && !is_null($page)) {
  94.         switch ($page) {
  95.           case 'list.php'// page listing
  96.             $episodes $podcastPrivateRepository->findAllBeforeToday();
  97.             return $this->render('widget_podcast_private/list.html.twig', [
  98.               'cabinet' => $accountingFirm,
  99.               'episodes' => $episodes,
  100.               'origin' => "podcast_private"
  101.             ]);
  102.             break;
  103.           default: // page detail
  104.             break;
  105.         }
  106.       }
  107.     }
  108.     if ($accountingFirm->getName() == "lagence.expert") {
  109.         $datetime = new \DateTime($date);
  110.         $date $datetime->format('Y-m-15');
  111.     }
  112.     if ($podcastId != null) {
  113.       $podcastPrivate $podcastPrivateRepository->findOneBy(['id' => $podcastId]);
  114.     } elseif ($date != null) {
  115.       $datetime = new \DateTime($date);
  116.       $datetime->format('Y-m-d');
  117.       $podcastPrivate $podcastPrivateRepository->findLastOneBeforeDate($datetime);
  118.     } else {
  119.       $podcastPrivate $podcastPrivateRepository->findLastOneBeforeToday();
  120.     }
  121.     $podcastPrivateAccountingFirm $podcastPrivateAccountingFirmRepository->findOneBy(['accountingFirm' => $accountingFirm'podcastPrivate' => $podcastPrivate]);
  122.     if ($podcastPrivateAccountingFirm->getIdAusha()) {
  123.       $idAusha $podcastPrivateAccountingFirm->getIdAusha();
  124.     }
  125.     $episodes $podcastPrivateRepository->findAllBeforeToday();
  126.     $truncate $podcastPrivate->getSubDescriptionTxt();
  127.     $chars 550;
  128.     if (strlen($truncate) > $chars) {
  129.       $truncate html_entity_decode($truncate) . " ";
  130.       $truncate substr($truncate0$chars);
  131.       $truncate substr($truncate0strrpos($truncate' '));
  132.       $truncate $truncate "...";
  133.     } else {
  134.       $truncate null;
  135.     }
  136.     return $this->render('widget_podcast_private/content.html.twig', [
  137.       'cabinet' => $accountingFirm,
  138.       'podcast' => $podcastPrivate,
  139.       'idAusha' => $idAusha,
  140.       'episodes' => $episodes,
  141.       'truncate' => $truncate,
  142.       'origin' => "podcast_private"
  143.     ]);
  144.   }
  145.   #[Route('/widget-podcast-private-band'name'w_podcast_private_get_band')]
  146.   public function ctrl_widgetPodcastPrivateBand(Request $requestAccountingFirmRepository $repositoryAuthorizedDomainRepository $domainsPodcastPrivateAccountingFirmRepository $podcastPrivateAccountingFirmRepositoryPodcastPrivateRepository $podcastPrivateRepository)
  147.   {
  148.     $token $request->query->get('token');
  149.     $host $request->get('host');
  150.     $a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3RzLnNob3cuYnlfcHVibGljaWQiLCJwb2RjYXN0cy5zdG9yZSIsInBvZGNhc3RzLnVwZGF0ZSIsInBvZGNhc3RzLmRlc3Ryb3kiLCJwb2RjYXN0cy5pbWFnZS5zdG9yZSIsInBvZGNhc3RzLmZpbGUuc3RvcmUiXX0.uWgOLN2_5LAKL9gB0Ij8V5PJjKrwRqAO_wVZ4l0vV73QPPF4MUDUzsYU-1hL4iFFPWanhg018EH_AGZvGdSdrZXqkzBPnqv6TxhL9aCVeayVwvgD-BQwPMJ3E7z27xREEhXH_r6f0d2t_J6SJexVh3AoT8fD2kUyFXh9B95ziWV-vULeHOqtWEZk01YctUyHLFWhIFzajgn8nSHb3hMYfbV4phapZqaegwCBSIxVPJz4yS3Ox0tbI5g-P6fAK5PlE1pKiPXabekYyUN4tPEmpMAQVURM3xcutBXUTMX0H6GCs0ih7KH3cI8SICVrldo3LkbAh8x4uuJJu2aNmv9qHpyxLi9wfmklyWMt0ROuf4dHPiDVLUfwmqnt9UEE4H5jP7hHcrMNCIRzerBgW0cCfKj0wJBtOuYpsqqBou2YTEyYja8uNEQbHpOQT4gT7CJxa575kqKsrTr9tS2k19BPdlxtBoIPQHcVpY9tQ1_TJro72kbE1ooJICZyFYTdHHPX77H4LClYcN4TxHxp8jTp6hXt_BjZbwU7Kl2PIxFV1diK62idKCtBjd_tvnUIuh7sKZeGn-YwCYVZVWRblYmZQ3tmPyje1ZJsgnY_nZIZ1TBwzZf3ucuPRxJJ4vPc3dXkxvLtWUs3bLWnACHCjhRA7LfMNDIlAZoRKHKQI6UNuiE";
  151.     $client = new Client(['base_uri' => 'https://developers.ausha.co/v1/podcasts/']);
  152.     $headers = [
  153.       'Authorization' => 'Bearer ' $aushaToken,
  154.       'accept'        => 'application/json',
  155.       'content-type'  => 'application/json',
  156.     ];
  157.     if ($host != "preview") {
  158.       $accountingFirm $repository->findOneBy(['host' => $host]);
  159.       if (is_null($accountingFirm)) {
  160.         $domains $domains->findBy(['name' => $host]);
  161.         if (is_null($domains)) {
  162.           throw new NotFoundHttpException();
  163.         }
  164.         foreach ($domains as $dom) {
  165.           $tmp_ac $dom->getAccountingFirm();
  166.           if ($tmp_ac->getPodcastPrivateToken() == $token) {
  167.             $accountingFirm $tmp_ac;
  168.             break;
  169.           }
  170.         }
  171.       }
  172.       if (is_null($accountingFirm)) {
  173.         throw new NotFoundHttpException();
  174.       }
  175.     }
  176.     $podcastPrivate $podcastPrivateRepository->findLastOneBeforeToday();
  177.     $podcastPrivateAccountingFirm $podcastPrivateAccountingFirmRepository->findOneBy(['accountingFirm' => $accountingFirm'podcastPrivate' => $podcastPrivate]);
  178.     $idAusha null;
  179.     if ($podcastPrivateAccountingFirm->getIdAusha()) {
  180.       $idAusha $podcastPrivateAccountingFirm->getIdAusha();
  181.     }
  182.     $episodes $podcastPrivateRepository->findThreeLatestBeforeToday();
  183.     return $this->render('widget_podcast_private/band.html.twig', [
  184.       'cabinet' => $accountingFirm,
  185.       'podcast' => $podcastPrivate,
  186.       'idAusha' => $idAusha,
  187.       'episodes' => $episodes,
  188.       'origin' => "podcast_private"
  189.     ]);
  190.   }
  191.   #[Route(path'/get-audio-ausha'name'w_podcast_private_get_audio_ausha')]
  192.   public function getAudioAusha(Request $request)
  193.   {
  194.     $aushaId $request->query->get('aushaid');
  195.     $aushaToken "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjczOTY4MjQ2MjU3NzJkYzA2M2ZkMTgyMzVlYzY4ZmFkZWE4MDNhMGI2NzI1NjQ1YzJkODEwM2E2YWYyMTMxZTJkZDZkYjU1NTMyMWQzM2NkIn0.eyJhdWQiOiIxIiwianRpIjoiNzM5NjgyNDYyNTc3MmRjMDYzZmQxODIzNWVjNjhmYWRlYTgwM2EwYjY3MjU2NDVjMmQ4MTAzYTZhZjIxMzFlMmRkNmRiNTU1MzIxZDMzY2QiLCJpYXQiOjE2NjMxNTQ4MTUsIm5iZiI6MTY2MzE1NDgxNSwiZXhwIjo3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ajgn8nSHb3hMYfbV4phapZqaegwCBSIxVPJz4yS3Ox0tbI5g-P6fAK5PlE1pKiPXabekYyUN4tPEmpMAQVURM3xcutBXUTMX0H6GCs0ih7KH3cI8SICVrldo3LkbAh8x4uuJJu2aNmv9qHpyxLi9wfmklyWMt0ROuf4dHPiDVLUfwmqnt9UEE4H5jP7hHcrMNCIRzerBgW0cCfKj0wJBtOuYpsqqBou2YTEyYja8uNEQbHpOQT4gT7CJxa575kqKsrTr9tS2k19BPdlxtBoIPQHcVpY9tQ1_TJro72kbE1ooJICZyFYTdHHPX77H4LClYcN4TxHxp8jTp6hXt_BjZbwU7Kl2PIxFV1diK62idKCtBjd_tvnUIuh7sKZeGn-YwCYVZVWRblYmZQ3tmPyje1ZJsgnY_nZIZ1TBwzZf3ucuPRxJJ4vPc3dXkxvLtWUs3bLWnACHCjhRA7LfMNDIlAZoRKHKQI6UNuiE";
  196.     $client = new Client(['base_uri' => 'https://developers.ausha.co/v1/podcasts/']);
  197.     $headers = [
  198.       'Authorization' => 'Bearer ' $aushaToken,
  199.       'accept' => 'application/json',
  200.       'content-type' => 'application/json',
  201.     ];
  202.     $ausha_mp3 null;
  203.     $logFile '../log_ausha.txt';
  204.     $origin "PODCAST_PRIVATE_WidgetPodcastPrivateController";
  205.     $url $aushaId;
  206.     $date = (new \DateTime())->format('Y-m-d H:i:s');
  207.     file_put_contents($logFile"[$date] - origin => $origin - avant appel API\n"FILE_APPEND);
  208.     try {
  209.       $response $client->request('GET'$aushaId, [
  210.         'headers' => $headers
  211.       ]);
  212.       file_put_contents($logFile"[$date] - origin => $origin - après appel API\n"FILE_APPEND);
  213.       if ($response->getStatusCode() === 200) {
  214.         file_put_contents($logFile"succès à l'URL : $url\n"FILE_APPEND);
  215.         $ausha json_decode($response->getBody()->getContents());
  216.         if ($ausha->data->audio_url) {
  217.           $ausha_mp3 $ausha->data->audio_url;
  218.         }
  219.       }
  220.     } catch (\Exception $e) {
  221.       file_put_contents($logFile"[$date] - origin => $origin - après appel API\n"FILE_APPEND);
  222.       file_put_contents($logFile$e->getCode() . " - " $e->getMessage() . "\n"FILE_APPEND);
  223.     }
  224.     return new JsonResponse([
  225.       'ausha_mp3' => $ausha_mp3
  226.     ]);
  227.   }
  228.   #[Route('/embed'name'w_podcast_private_embed')]
  229.   public function embed()
  230.   {
  231.     $root $this->getParameter('kernel.project_dir');
  232.     $path $root '/public/widgets/widget_podcast_private/widget_podcast_private.js';
  233.     return new BinaryFileResponse($path);
  234.   }
  235.   #[Route('/{filename}.{format}'name'w_podcast_private_files')]
  236.   public function podcastPrivate_files(string $filenamestring $format)
  237.   {
  238.     $allowed = array('png''jpg''jpeg''gif''js''svg''eot''ttf''woff');
  239.     if (in_array($format$allowed)) {
  240.       $root $this->getParameter('kernel.project_dir');
  241.       $path $root '/public/widget_podcast_private/' $filename '.' $format;
  242.       if (!file_exists($path)) {
  243.         $path $root '/public/widgets/widget_podcast_private/' $filename '.' $format;
  244.       }
  245.       return new BinaryFileResponse($path);
  246.     }
  247.     return false;
  248.   }
  249.   #[Route('/sendParticipation'name'w_podcast_private_send_form')]
  250.   public function participation_send(Request $requestEntityManagerInterface $emAccountingFirmRepository $accountingFirmRepositoryWidgetMailing $mailing)
  251.   {
  252.     $email $request->get('email');
  253.     $tel $request->get('tel');
  254.     $firstname $request->get('firstname');
  255.     $linkedin $request->get('linkedin');
  256.     $expertise $request->get('expertise');
  257.     $idCabinet $request->get('cabinet');
  258.     $cabinet $accountingFirmRepository->findById($idCabinet);
  259.     if (!empty($email) && !empty($firstname) && !empty($tel) && !empty($cabinet) && !empty($linkedin) && !empty($expertise)) {
  260.       // Vérifier si l'email est dans la blacklist
  261.       $blacklistRepo $em->getRepository(EmailBlacklist::class);
  262.       if ($blacklistRepo->isEmailBlacklisted($email)) {
  263.         // Incrémenter le compteur de tentatives
  264.         $blacklistedEmail $blacklistRepo->findByEmail($email);
  265.         if ($blacklistedEmail) {
  266.           $blacklistedEmail->incrementBlockCount();
  267.           $em->persist($blacklistedEmail);
  268.           $em->flush();
  269.         }
  270.         return new JsonResponse(['status' => 'error''message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer le message.'], 403);
  271.       }
  272.       $participationContact = new ContactParticipationPlaceExpert;
  273.       $participationContact->setEmail($email);
  274.       $participationContact->setPhone($tel);
  275.       $participationContact->setFirstname($firstname);
  276.       $participationContact->setLinkLinkedin($linkedin);
  277.       $participationContact->setExpertise($expertise);
  278.       $participationContact->setRgpd(true);
  279.       $participationContact->setAccountingFirm($cabinet);
  280.       $participationContact->setCreatedAt(new DateTimeImmutable());
  281.       $em->persist($participationContact);
  282.       $em->flush();
  283.       //send mail
  284.       $mailing->sendParticipationPalToAgenceExpert($cabinet, [ //Envoie à lagence.expert
  285.         'name' => $firstname,
  286.         'email' => $email,
  287.         'tel' => $tel,
  288.         'linkedin' => $linkedin,
  289.         'expertise' => $expertise,
  290.       ]);
  291.       $mailing->sendParticipationPalToClient($cabinet, [ //Envoie au client
  292.         'name' => $firstname,
  293.         'email' => $email,
  294.       ]);
  295.       return new JsonResponse('success');
  296.     }
  297.     return new JsonResponse('error');
  298.   }
  299. }