<?php
namespace App\Controller\ThemesWebsite\Blog;
use App\Entity\Core\Users;
use App\Entity\Fiches\Articles;
use App\Entity\Fiches\Interactions;
use App\Entity\Pages\Contents;
use App\Entity\Pages\Pages;
use App\Entity\Pages\PagesHasBlocks;
use App\Entity\Pages\SecureContent;
use App\Entity\Pages\SimulationContent;
use App\Entity\Pages\SimulationContentCategories;
use App\Entity\Pages\SimulationContentHasCheck;
use App\Form\Fiches\InteractionsAdminForm;
use App\Form\Fiches\InteractionsForm;
use App\Form\Fiches\InteractionsSimpleForm;
use App\Form\Pages\BeforeSecureContentsForm;
use App\Services\EncryptionService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Gestion des pages
*/
class PagesController extends AbstractController
{
private $em;
private $us;
private $es;
private $decryptionLogger;
public function __construct(EntityManagerInterface $em,
\App\Services\Core\Users $us,
EncryptionService $es,
LoggerInterface $decryptionLogger
){
$this->em = $em;
$this->us = $us;
$this->es = $es;
$this->decryptionLogger = $decryptionLogger;
}
public function search(Request $request)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$session = $request->getSession();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'search']);
$searchTerm = "";
if(isset($_GET['search'])) {
$searchTerm = $_GET['search'];
}
$articles = $this->em->getRepository(\App\Entity\Articles\Articles::class)->getSearch("fr",$searchTerm);
$contents = $this->em->getRepository(Contents::class)->getSearch($searchTerm);
$pages = $this->em->getRepository(Pages::class)->getSearch($searchTerm);
$premiums = $this->em->getRepository(Articles::class)->getSearch($searchTerm);
return $this->render('themesWebsite/blog'.$themeSelection.'/search.html.twig',[
'page' => $page,
'pages' => $pages,
'search' => $searchTerm,
'articles' => $articles,
'contents' => $contents,
'premiums' => $premiums
]);
}
/**
* 1er niveau
* @param Request $request
* @param $folderslug
* @return mixed
*/
public function fiche(Request $request, $folderslug)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$session = $request->getSession();
// Mode TEST !
if($_ENV['APP_ENV'] !== "prod") {
if($folderslug === "test") {
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'test']);
return $this->render('themesWebsite/blog'.$themeSelection.'/page_test.html.twig',['page' => $page]);
}
}
// Simulation de projets
$contentSC = $this->em->getRepository(SimulationContent::class)->findOneBy(['identifiant' => $folderslug]);
if ($contentSC) {
$checks = $this->em->getRepository(SimulationContentHasCheck::class)->findBy(['simulationContent' => $contentSC->getId()]);
$categories = $this->em->getRepository(SimulationContentCategories::class)->findBy(['simulationContent' => $contentSC->getId()]);
$categoriesOn = $this->em->getRepository(SimulationContentCategories::class)->findBy(['simulationContent' => $contentSC->getId(),'onQuotation' => true, 'details' => true]);
$categoriesOff = $this->em->getRepository(SimulationContentCategories::class)->findBy(['simulationContent' => $contentSC->getId(),'onQuotation' => false, 'details' => true]);
return $this->render('themesWebsite/blog'.$themeSelection.'/page_simulation.html.twig', [
'content' => $contentSC,
'checks' => $checks,
'categories' => $categories,
'categoriesOn' => $categoriesOn,
'categoriesOff' => $categoriesOff
]);
}
// Contenu chiffré.
$contentS = $this->em->getRepository(SecureContent::class)->findOneBy(['identifiant' => $folderslug]);
if ($contentS) {
$attemptKey = 'decrypt_attempts_' . $folderslug;
$maxAttempts = 5;
$lockoutTime = 60; // Temps de verrouillage en secondes (1 minute)
// Vérifiez si l'utilisateur est temporairement verrouillé
if ($session->has($attemptKey . '_lockout_time') && time() < $session->get($attemptKey . '_lockout_time')) {
return $this->render('themesWebsite/blog'.$themeSelection.'/secure/lockout.html.twig', [
'lockout_time' => $session->get($attemptKey . '_lockout_time') - time(),
'content' => $contentS,
'folderslug' => $folderslug
]);
}
$attempts = $session->get($attemptKey, 0);
if ($attempts >= $maxAttempts) {
// Verrouillez l'accès pour un certain temps
$session->set($attemptKey . '_lockout_time', time() + $lockoutTime);
$session->remove($attemptKey); // Réinitialisez le compteur de tentatives
return $this->render('themesWebsite/blog'.$themeSelection.'/secure/lockout.html.twig', [
'lockout_time' => $lockoutTime,
'content' => $contentS,
'folderslug' => $folderslug
]);
}
$form = $this->createForm(BeforeSecureContentsForm::class, $contentS);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$data = $form->getData();
$data = $request->request->all();
$dataM = $data['before_secure_contents_form'];
$key = $dataM['identifiantKey'];
try {
$keyDecrypt = $this->es->decrypt($contentS->getIdentifiantKey(), $key);
if ($keyDecrypt === "mirtillo") {
$descriptionDecrypt = "";
if(!empty($contentS->getDescription())) {
$descriptionDecrypt = $this->es->decrypt($contentS->getDescription(), $key);
}
$descriptionContentDecrypt = "";
if(!empty($contentS->getDescriptionContent())) {
$descriptionContentDecrypt = $this->es->decrypt($contentS->getDescriptionContent(), $key);
}
$session->remove($attemptKey); // Réinitialisez le compteur en cas de succès
return $this->render('themesWebsite/blog'.$themeSelection.'/secure/content.html.twig', [
'content' => $contentS,
'descriptionContent' => $descriptionContentDecrypt,
'description' => $descriptionDecrypt
]);
} else {
throw new \Exception("Decryption failed");
}
} catch (\Exception $e) {
// En cas d'échec, incrémentez le compteur
$attempts++;
$session->set($attemptKey, $attempts);
// Journalisation de l'échec
$this->decryptionLogger->warning('Tentative de décryptage échouée', [
'user_id' => $this->getUser() ? $this->getUser()->getId() : 'anonyme',
'ip' => $request->getClientIp(),
'folderslug' => $folderslug,
'timestamp' => time(),
'tentative' => $attempts
]);
}
return $this->redirectToRoute('pages_fiche', ['folderslug' => $folderslug]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/secure/key_content.html.twig', [
'form' => $form->createView(),
'content' => $contentS
]);
}
// Contenu netlinking.
$content = $this->em->getRepository(Contents::class)->findOneBy(['folderSlug' => $folderslug, 'status' => 'ONLINE']);
if($content) {
return $this->render('themesWebsite/blog'.$themeSelection.'/page_content.html.twig',[
'page' => $content
]);
}
$premiumContent = $this->em->getRepository(Articles::class)->findOneBy(['folderSlug' => $folderslug, 'status' => 'ONLINE']);
if($premiumContent) {
$user = $this->getUser();
$interactions = $this->em->getRepository(Interactions::class)->getThread($premiumContent->getId(),$user);
$countInteractions = $this->em->getRepository(Interactions::class)->countThreads($premiumContent->getId());
$questions = $this->em->getRepository(Interactions::class)->getQuestions($premiumContent->getId());
$intObj = new Interactions();
$intObj->setUser($user);
$intObj->setArticle($premiumContent);
$intObj->setLocked(false);
// Interactions
$formInteractions = $this->createForm(InteractionsForm::class,$intObj);
$formInteractions->handleRequest($request);
if ($formInteractions->isSubmitted() && $formInteractions->isValid()) {
$this->em->persist($intObj);
$this->em->flush();
return $this->redirectToRoute('pages_fiche',['folderslug' => $folderslug]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/premium/content.html.twig',[
'page' => $premiumContent,
'fiche' => $premiumContent,
'formInteractions' => $formInteractions->createView(),
'interactions' => $interactions,
'countInteractions' => $countInteractions,
'questions' => $questions
]);
}
$lang = "fr";
$page = $this->em->getRepository(Pages::class)->getPage($lang,$folderslug);
if(!$page) {
return $this->redirectToRoute('homepage');
}
$user = $this->getUser();
if($user == null) {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
} else {
$grant = $this->em->getRepository(Users::class)->userHasRole($user->getId(),"ROLE_SUPER_ADMIN");
if($grant == "0") {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
}
}
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
return $this->render('themesWebsite/blog'.$themeSelection.'/page.html.twig',[
'page' => $page,
'blocks' => $blocks
]);
}
/**
* 2ème niveau
* @param Request $request
* @param $folderslug
* @param $folderslug2
* @return mixed
*/
public function fiche2(Request $request, $folderslug, $folderslug2)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$content = $this->em->getRepository(Contents::class)->findOneBy(['folderSlug' => $folderslug, 'folderSlug2' => $folderslug2, 'status' => 'ONLINE']);
if($content) {
return $this->render('themesWebsite/blog'.$themeSelection.'/page_content.html.twig',[
'page' => $content
]);
}
$premiumContent = $this->em->getRepository(Articles::class)->findOneBy(['folderSlug' => $folderslug, 'folderSlug2' => $folderslug2, 'status' => 'ONLINE']);
if($premiumContent) {
$user = $this->getUser();
$interactions = $this->em->getRepository(Interactions::class)->getThread($premiumContent->getId(),$user);
$countInteractions = $this->em->getRepository(Interactions::class)->countThreads($premiumContent->getId());
$questions = $this->em->getRepository(Interactions::class)->getQuestions($premiumContent->getId());
$intObj = new Interactions();
$intObj->setUser($user);
$intObj->setArticle($premiumContent);
$intObj->setLocked(false);
// Interactions
$formInteractions = $this->createForm(InteractionsForm::class,$intObj);
$formInteractions->handleRequest($request);
if ($formInteractions->isSubmitted() && $formInteractions->isValid()) {
$this->em->persist($intObj);
$this->em->flush();
return $this->redirectToRoute('pages_fiche',['folderslug' => $folderslug]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/premium/content.html.twig',[
'page' => $premiumContent,
'fiche' => $premiumContent,
'formInteractions' => $formInteractions->createView(),
'interactions' => $interactions,
'countInteractions' => $countInteractions,
'questions' => $questions
]);
}
$lang = "fr";
$page = $this->em->getRepository(Pages::class)->getPage($lang,$folderslug,$folderslug2);
if(!$page) {
return $this->redirectToRoute('homepage');
}
$user = $this->getUser();
if($user == null) {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
} else {
$grant = $this->em->getRepository(Users::class)->userHasRole($user->getId(),"ROLE_SUPER_ADMIN");
if($grant == "0") {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
}
}
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
return $this->render('themesWebsite/blog'.$themeSelection.'/page.html.twig',[
'page' => $page,
'blocks' => $blocks
]);
}
/**
* 3ème niveau
* @param Request $request
* @param $folderslug
* @param $folderslug2
* @param $folderslug3
* @return mixed
*/
public function fiche3(Request $request, $folderslug, $folderslug2, $folderslug3)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$content = $this->em->getRepository(Contents::class)->findOneBy(['folderSlug' => $folderslug, 'folderSlug2' => $folderslug2, 'folderSlug3' => $folderslug3, 'status' => 'ONLINE']);
if($content) {
return $this->render('themesWebsite/blog'.$themeSelection.'/page_content.html.twig',[
'page' => $content
]);
}
$premiumContent = $this->em->getRepository(Articles::class)->findOneBy(['folderSlug' => $folderslug, 'folderSlug2' => $folderslug2, 'folderSlug3' => $folderslug3, 'status' => 'ONLINE']);
if($premiumContent) {
$user = $this->getUser();
$interactions = $this->em->getRepository(Interactions::class)->getThread($premiumContent->getId(),$user);
$countInteractions = $this->em->getRepository(Interactions::class)->countThreads($premiumContent->getId());
$questions = $this->em->getRepository(Interactions::class)->getQuestions($premiumContent->getId());
$intObj = new Interactions();
$intObj->setUser($user);
$intObj->setArticle($premiumContent);
$intObj->setLocked(false);
// Interactions
$formInteractions = $this->createForm(InteractionsForm::class,$intObj);
$formInteractions->handleRequest($request);
if ($formInteractions->isSubmitted() && $formInteractions->isValid()) {
$this->em->persist($intObj);
$this->em->flush();
return $this->redirectToRoute('pages_fiche',['folderslug' => $folderslug]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/premium/content.html.twig',[
'page' => $premiumContent,
'fiche' => $premiumContent,
'formInteractions' => $formInteractions->createView(),
'interactions' => $interactions,
'countInteractions' => $countInteractions,
'questions' => $questions
]);
}
$lang = "fr";
$page = $this->em->getRepository(Pages::class)->getPage($lang,$folderslug,$folderslug2,$folderslug3);
if(!$page) {
return $this->redirectToRoute('homepage');
}
$user = $this->getUser();
if($user == null) {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
} else {
$grant = $this->em->getRepository(Users::class)->userHasRole($user->getId(),"ROLE_SUPER_ADMIN");
if($grant == "0") {
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
}
}
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
return $this->render('themesWebsite/blog'.$themeSelection.'/page.html.twig',[
'page' => $page,
'blocks' => $blocks
]);
}
/**
* Redirection vers la page cible de la fiche
* @param Request $request
* @param Articles $fiche
* @param Pages $page
* @return mixed
*/
public function redirFiche(Request $request, Articles $fiche)
{
$folderslug = $fiche->getFolderSlug();
$folderslug2 = $fiche->getFolderSlug2();
$folderslug3 = $fiche->getFolderSlug3();
if(!empty($folderslug) and !empty($folderslug2) and !empty($folderslug3)) {
return $this->redirectToRoute('pages_fiche3',['folderslug' => $folderslug, 'folderslug2' => $folderslug2, 'folderslug3' => $folderslug3]);
} elseif(!empty($folderslug) and !empty($folderslug2) and empty($folderslug3)) {
return $this->redirectToRoute('pages_fiche2',['folderslug' => $folderslug, 'folderslug2' => $folderslug2]);
} elseif(!empty($folderslug) and empty($folderslug2) and empty($folderslug3)) {
return $this->redirectToRoute('pages_fiche',['folderslug' => $folderslug]);
}
return $this->redirectToRoute('homepage');
}
/**
* Interactions
* @param Request $request
* @param Articles $fiche
* @param Pages $page
* @param Interactions $interaction
* @return mixed
*/
public function interactions(Request $request, Articles $fiche, Interactions $interaction)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$user = $this->getUser();
$role = $this->us->hasRoles($user->getRoles(), "ROLE_SUPER_ADMIN");
// Vérification si l'utilisateur et privée
if($interaction->getPersonal() == true) {
// Vérification si l'utilisateur admin
if ($role === false) {
// Vérification de l'utilisateur du post
if($interaction->getUser() != $user) {
die('ici');
return $this->redirectToRoute('homepage');
}
}
}
$interactions = $this->em->getRepository(Interactions::class)->findBy(['interaction' => $interaction],['createdAt' => 'ASC']);
$intObj = new Interactions();
$intObj->setUser($user);
$intObj->setArticle($fiche);
$intObj->setLocked(false);
$intObj->setPersonal($interaction->getPersonal());
$intObj->setInteraction($interaction);
$form = $this->createForm(InteractionsSimpleForm::class,$intObj);
if ($role == true) {
$form = $this->createForm(InteractionsAdminForm::class,$intObj);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($intObj);
$this->em->flush();
return $this->redirectToRoute('pages_interactions',['fiche' => $fiche->getId(),'interaction' => $interaction->getId()]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/premium/interactions.html.twig',[
'page' => $fiche,
'fiche' => $fiche,
'subject' => $interaction,
'interactions' => $interactions,
'formInteractions' => $form->createView(),
]);
}
/**
* ADMIN - Modifier une interaction
* @param Request $request
* @param Articles $fiche
* @param Pages $page
* @param Interactions $interaction
* @return mixed
*/
public function editInteraction(Request $request, Articles $fiche, Interactions $interaction)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$user = $this->getUser();
$role = $this->us->hasRoles($user->getRoles(), "ROLE_SUPER_ADMIN");
if ($role != true) {
return $this->redirectToRoute('homepage');
}
$form = $this->createForm(InteractionsAdminForm::class,$interaction);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($interaction);
$this->em->flush();
return $this->redirectToRoute('pages_interactions',['fiche' => $fiche->getId(),'interaction' => $interaction->getId()]);
}
return $this->render('themesWebsite/blog'.$themeSelection.'/premium/edit_interaction.html.twig',[
'page' => $fiche,
'fiche' => $fiche,
'interaction' => $interaction,
'formInteractions' => $form->createView(),
]);
}
}