<?php
namespace App\Controller;
use App\Entity\ContactParticipationPlaceExpert;
use App\Entity\EmailBlacklist;
use App\Entity\PodcastPrivate;
use App\Repository\AccountingFirmRepository;
use App\Repository\AuthorizedDomainRepository;
use App\Repository\PodcastEpisodeRepository;
use App\Repository\PodcastPrivateAccountingFirmRepository;
use App\Repository\PodcastPrivateRepository;
use App\Services\WidgetMailing;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
use Qferrer\Mjml\Twig\MjmlExtension;
use Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class WidgetPodcastPrivateController extends AbstractController
{
protected MjmlExtension $mjmlRenderer;
protected Environment $twig;
public function __construct(MailerInterface $mailer, MjmlExtension $mjmlRenderer, Environment $twig)
{
$this->mjmlRenderer = $mjmlRenderer;
$this->twig = $twig;
}
#[Route('/check-podcast-private', name: 'w_podcast_private_check')]
public function ctrl_checkPodcastPrivate(Request $request, AccountingFirmRepository $repository, string $prefix = null): JsonResponse
{
$token = $request->query->get('token');
$host = $request->get('host');
if ($token && $host) {
$accountingFirm = $repository->findOneBy(['podcastPrivateToken' => $token]);
$isAuthorized = false;
if ($accountingFirm && in_array($host, $accountingFirm->getListAuthorizedDomains())) {
$isAuthorized = true;
}
$url = $isAuthorized ? $accountingFirm->getPodcastPrivateWidgetUrl() : null;
} else {
$accountingFirm = $repository->findOneBy(['host' => $host]);
$url = $accountingFirm ? $prefix . $this->generateUrl('w_podcast_private_get') : null;
}
return $this->json($url);
}
#[Route('/widget-podcast-private', name: 'w_podcast_private_get')]
public function ctrl_widgetPodcastPrivate(Request $request,
AccountingFirmRepository $repository,
AuthorizedDomainRepository $domains,
PodcastPrivateAccountingFirmRepository $podcastPrivateAccountingFirmRepository,
PodcastPrivateRepository $podcastPrivateRepository,
)
{
$token = $request->query->get('token');
$host = $request->get('host');
$podcastId = $request->get('pid');
$date = $request->get('date');
if ($host != "preview") {
$accountingFirm = $repository->findOneBy(['host' => $host]);
if (is_null($accountingFirm)) {
$domains = $domains->findBy(['name' => $host]);
if (is_null($domains)) {
throw new NotFoundHttpException();
}
foreach ($domains as $dom) {
$tmp_ac = $dom->getAccountingFirm();
if ($tmp_ac->getPodcastPrivateToken() == $token) {
$accountingFirm = $tmp_ac;
break;
}
}
}
if (is_null($accountingFirm)) {
throw new NotFoundHttpException();
}
}
$p = $request->getRequestUri();
$pos = strpos($p, '?');
if ($pos !== false) {
$p = substr($p, 0, $pos + 1) . str_replace('?', '&', substr($p, $pos + 1));
}
$route = parse_url($p);
if (isset($route['query'])) {
parse_str($route['query'], $query);
$page = isset($query['p']) ? $query['p'] : null;
if (!empty($page) && !is_null($page)) {
switch ($page) {
case 'list.php': // page listing
$episodes = $podcastPrivateRepository->findAllBeforeToday();
return $this->render('widget_podcast_private/list.html.twig', [
'cabinet' => $accountingFirm,
'episodes' => $episodes,
'origin' => "podcast_private"
]);
break;
default: // page detail
break;
}
}
}
if ($accountingFirm->getName() == "lagence.expert") {
$datetime = new \DateTime($date);
$date = $datetime->format('Y-m-15');
}
if ($podcastId != null) {
$podcastPrivate = $podcastPrivateRepository->findOneBy(['id' => $podcastId]);
} elseif ($date != null) {
$datetime = new \DateTime($date);
$datetime->format('Y-m-d');
$podcastPrivate = $podcastPrivateRepository->findLastOneBeforeDate($datetime);
} else {
$podcastPrivate = $podcastPrivateRepository->findLastOneBeforeToday();
}
$podcastPrivateAccountingFirm = $podcastPrivateAccountingFirmRepository->findOneBy(['accountingFirm' => $accountingFirm, 'podcastPrivate' => $podcastPrivate]);
if ($podcastPrivateAccountingFirm->getIdAusha()) {
$idAusha = $podcastPrivateAccountingFirm->getIdAusha();
}
$episodes = $podcastPrivateRepository->findAllBeforeToday();
$truncate = $podcastPrivate->getSubDescriptionTxt();
$chars = 550;
if (strlen($truncate) > $chars) {
$truncate = html_entity_decode($truncate) . " ";
$truncate = substr($truncate, 0, $chars);
$truncate = substr($truncate, 0, strrpos($truncate, ' '));
$truncate = $truncate . "...";
} else {
$truncate = null;
}
return $this->render('widget_podcast_private/content.html.twig', [
'cabinet' => $accountingFirm,
'podcast' => $podcastPrivate,
'idAusha' => $idAusha,
'episodes' => $episodes,
'truncate' => $truncate,
'origin' => "podcast_private"
]);
}
#[Route('/widget-podcast-private-band', name: 'w_podcast_private_get_band')]
public function ctrl_widgetPodcastPrivateBand(Request $request, AccountingFirmRepository $repository, AuthorizedDomainRepository $domains, PodcastPrivateAccountingFirmRepository $podcastPrivateAccountingFirmRepository, PodcastPrivateRepository $podcastPrivateRepository)
{
$token = $request->query->get('token');
$host = $request->get('host');
$aushaToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjczOTY4MjQ2MjU3NzJkYzA2M2ZkMTgyMzVlYzY4ZmFkZWE4MDNhMGI2NzI1NjQ1YzJkODEwM2E2YWYyMTMxZTJkZDZkYjU1NTMyMWQzM2NkIn0.eyJhdWQiOiIxIiwianRpIjoiNzM5NjgyNDYyNTc3MmRjMDYzZmQxODIzNWVjNjhmYWRlYTgwM2EwYjY3MjU2NDVjMmQ4MTAzYTZhZjIxMzFlMmRkNmRiNTU1MzIxZDMzY2QiLCJpYXQiOjE2NjMxNTQ4MTUsIm5iZiI6MTY2MzE1NDgxNSwiZXhwIjo3OTUyNDI4Nzk5LCJzdWIiOiIxMTMwNzEiLCJzY29wZXMiOlsic2hvd3MuaW5kZXhXaXRoR3JhbnRlZCIsInNob3dzLnNob3ciLCJzaG93cy5zaG93LmJ5X3B1YmxpY2lkIiwicGxheWxpc3RzLmluZGV4IiwicGxheWxpc3RzLnN0b3JlIiwicGxheWxpc3RzLnNob3ciLCJwbGF5bGl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wJBtOuYpsqqBou2YTEyYja8uNEQbHpOQT4gT7CJxa575kqKsrTr9tS2k19BPdlxtBoIPQHcVpY9tQ1_TJro72kbE1ooJICZyFYTdHHPX77H4LClYcN4TxHxp8jTp6hXt_BjZbwU7Kl2PIxFV1diK62idKCtBjd_tvnUIuh7sKZeGn-YwCYVZVWRblYmZQ3tmPyje1ZJsgnY_nZIZ1TBwzZf3ucuPRxJJ4vPc3dXkxvLtWUs3bLWnACHCjhRA7LfMNDIlAZoRKHKQI6UNuiE";
$client = new Client(['base_uri' => 'https://developers.ausha.co/v1/podcasts/']);
$headers = [
'Authorization' => 'Bearer ' . $aushaToken,
'accept' => 'application/json',
'content-type' => 'application/json',
];
if ($host != "preview") {
$accountingFirm = $repository->findOneBy(['host' => $host]);
if (is_null($accountingFirm)) {
$domains = $domains->findBy(['name' => $host]);
if (is_null($domains)) {
throw new NotFoundHttpException();
}
foreach ($domains as $dom) {
$tmp_ac = $dom->getAccountingFirm();
if ($tmp_ac->getPodcastPrivateToken() == $token) {
$accountingFirm = $tmp_ac;
break;
}
}
}
if (is_null($accountingFirm)) {
throw new NotFoundHttpException();
}
}
$podcastPrivate = $podcastPrivateRepository->findLastOneBeforeToday();
$podcastPrivateAccountingFirm = $podcastPrivateAccountingFirmRepository->findOneBy(['accountingFirm' => $accountingFirm, 'podcastPrivate' => $podcastPrivate]);
$idAusha = null;
if ($podcastPrivateAccountingFirm->getIdAusha()) {
$idAusha = $podcastPrivateAccountingFirm->getIdAusha();
}
$episodes = $podcastPrivateRepository->findThreeLatestBeforeToday();
return $this->render('widget_podcast_private/band.html.twig', [
'cabinet' => $accountingFirm,
'podcast' => $podcastPrivate,
'idAusha' => $idAusha,
'episodes' => $episodes,
'origin' => "podcast_private"
]);
}
#[Route(path: '/get-audio-ausha', name: 'w_podcast_private_get_audio_ausha')]
public function getAudioAusha(Request $request)
{
$aushaId = $request->query->get('aushaid');
$aushaToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1Ni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zdG9yZSIsInBvZGNhc3RzLnVwZGF0ZSIsInBvZGNhc3RzLmRlc3Ryb3kiLCJwb2RjYXN0cy5pbWFnZS5zdG9yZSIsInBvZGNhc3RzLmZpbGUuc3RvcmUiXX0.uWgOLN2_5LAKL9gB0Ij8V5PJjKrwRqAO_wVZ4l0vV73QPPF4MUDUzsYU-1hL4iFFPWanhg018EH_AGZvGdSdrZXqkzBPnqv6TxhL9aCVeayVwvgD-BQwPMJ3E7z27xREEhXH_r6f0d2t_J6SJexVh3AoT8fD2kUyFXh9B95ziWV-vULeHOqtWEZk01YctUyHLFWhIFzajgn8nSHb3hMYfbV4phapZqaegwCBSIxVPJz4yS3Ox0tbI5g-P6fAK5PlE1pKiPXabekYyUN4tPEmpMAQVURM3xcutBXUTMX0H6GCs0ih7KH3cI8SICVrldo3LkbAh8x4uuJJu2aNmv9qHpyxLi9wfmklyWMt0ROuf4dHPiDVLUfwmqnt9UEE4H5jP7hHcrMNCIRzerBgW0cCfKj0wJBtOuYpsqqBou2YTEyYja8uNEQbHpOQT4gT7CJxa575kqKsrTr9tS2k19BPdlxtBoIPQHcVpY9tQ1_TJro72kbE1ooJICZyFYTdHHPX77H4LClYcN4TxHxp8jTp6hXt_BjZbwU7Kl2PIxFV1diK62idKCtBjd_tvnUIuh7sKZeGn-YwCYVZVWRblYmZQ3tmPyje1ZJsgnY_nZIZ1TBwzZf3ucuPRxJJ4vPc3dXkxvLtWUs3bLWnACHCjhRA7LfMNDIlAZoRKHKQI6UNuiE";
$client = new Client(['base_uri' => 'https://developers.ausha.co/v1/podcasts/']);
$headers = [
'Authorization' => 'Bearer ' . $aushaToken,
'accept' => 'application/json',
'content-type' => 'application/json',
];
$ausha_mp3 = null;
$logFile = '../log_ausha.txt';
$origin = "PODCAST_PRIVATE_WidgetPodcastPrivateController";
$url = $aushaId;
$date = (new \DateTime())->format('Y-m-d H:i:s');
file_put_contents($logFile, "[$date] - origin => $origin - avant appel API\n", FILE_APPEND);
try {
$response = $client->request('GET', $aushaId, [
'headers' => $headers
]);
file_put_contents($logFile, "[$date] - origin => $origin - après appel API\n", FILE_APPEND);
if ($response->getStatusCode() === 200) {
file_put_contents($logFile, "succès à l'URL : $url\n", FILE_APPEND);
$ausha = json_decode($response->getBody()->getContents());
if ($ausha->data->audio_url) {
$ausha_mp3 = $ausha->data->audio_url;
}
}
} catch (\Exception $e) {
file_put_contents($logFile, "[$date] - origin => $origin - après appel API\n", FILE_APPEND);
file_put_contents($logFile, $e->getCode() . " - " . $e->getMessage() . "\n", FILE_APPEND);
}
return new JsonResponse([
'ausha_mp3' => $ausha_mp3
]);
}
#[Route('/embed', name: 'w_podcast_private_embed')]
public function embed()
{
$root = $this->getParameter('kernel.project_dir');
$path = $root . '/public/widgets/widget_podcast_private/widget_podcast_private.js';
return new BinaryFileResponse($path);
}
#[Route('/{filename}.{format}', name: 'w_podcast_private_files')]
public function podcastPrivate_files(string $filename, string $format)
{
$allowed = array('png', 'jpg', 'jpeg', 'gif', 'js', 'svg', 'eot', 'ttf', 'woff');
if (in_array($format, $allowed)) {
$root = $this->getParameter('kernel.project_dir');
$path = $root . '/public/widget_podcast_private/' . $filename . '.' . $format;
if (!file_exists($path)) {
$path = $root . '/public/widgets/widget_podcast_private/' . $filename . '.' . $format;
}
return new BinaryFileResponse($path);
}
return false;
}
#[Route('/sendParticipation', name: 'w_podcast_private_send_form')]
public function participation_send(Request $request, EntityManagerInterface $em, AccountingFirmRepository $accountingFirmRepository, WidgetMailing $mailing)
{
$email = $request->get('email');
$tel = $request->get('tel');
$firstname = $request->get('firstname');
$linkedin = $request->get('linkedin');
$expertise = $request->get('expertise');
$idCabinet = $request->get('cabinet');
$cabinet = $accountingFirmRepository->findById($idCabinet);
if (!empty($email) && !empty($firstname) && !empty($tel) && !empty($cabinet) && !empty($linkedin) && !empty($expertise)) {
// Vérifier si l'email est dans la blacklist
$blacklistRepo = $em->getRepository(EmailBlacklist::class);
if ($blacklistRepo->isEmailBlacklisted($email)) {
// Incrémenter le compteur de tentatives
$blacklistedEmail = $blacklistRepo->findByEmail($email);
if ($blacklistedEmail) {
$blacklistedEmail->incrementBlockCount();
$em->persist($blacklistedEmail);
$em->flush();
}
return new JsonResponse(['status' => 'error', 'message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer le message.'], 403);
}
$participationContact = new ContactParticipationPlaceExpert;
$participationContact->setEmail($email);
$participationContact->setPhone($tel);
$participationContact->setFirstname($firstname);
$participationContact->setLinkLinkedin($linkedin);
$participationContact->setExpertise($expertise);
$participationContact->setRgpd(true);
$participationContact->setAccountingFirm($cabinet);
$participationContact->setCreatedAt(new DateTimeImmutable());
$em->persist($participationContact);
$em->flush();
//send mail
$mailing->sendParticipationPalToAgenceExpert($cabinet, [ //Envoie à lagence.expert
'name' => $firstname,
'email' => $email,
'tel' => $tel,
'linkedin' => $linkedin,
'expertise' => $expertise,
]);
$mailing->sendParticipationPalToClient($cabinet, [ //Envoie au client
'name' => $firstname,
'email' => $email,
]);
return new JsonResponse('success');
}
return new JsonResponse('error');
}
}