<?phpnamespace App\Controller\Vitrine;use App\Entity\Core\Ebook;use App\Entity\Core\EbookHasRegistrations;use App\Form\Core\EbookRegistrationForm;use App\Services\Core\Core;use App\Services\Core\Users;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Vich\UploaderBundle\Templating\Helper\UploaderHelper;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\JsonResponse;/** * Gestion des ebooks */class EbooksController extends AbstractController{ private $em; private $us; public function __construct (EntityManagerInterface $em, Users $us) { $this->em = $em; $this->us = $us; } /** * Affichage du formulaire * @param Request $request * @param Users $us * @param Ebook $ebook * @return Response */ public function view(Request $request,Ebook $ebook): Response { $themeSelection = $_ENV['THEME_BLOG']; $randomPassword = $this->us->randomPasswordSecurised(50).uniqid(); $registration = new EbookHasRegistrations(); $form = $this->createForm(EbookRegistrationForm::class, $registration); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $registration->setEbook($ebook); $registration->setDownloaded(false); $registration->setOpenedAt(null); $registration->setSendedAt(new \DateTime('now')); $registration->setCodeRegistration($randomPassword); $this->em->persist($registration); $this->em->flush(); return $this->redirectToRoute('visitors_ebooks_sv',['identifiant' => $randomPassword]); } return $this->render('vitrine/'.$themeSelection.'/ebooks/sourcecode.html.twig',[ 'ebook' => $ebook, 'form' => $form->createView() ]); } /** * Visualisation des informations d'un ebook (pour le télécharger) * @param Request $request * @param $identifiant * @return mixed */ public function save(Request $request, $identifiant) { $themeSelection = $_ENV['THEME_BLOG']; $registration = $this->em->getRepository(EbookHasRegistrations::class)->findOneBy(['codeRegistration' => $identifiant]); if($registration == null) { return $this->redirectToRoute('homepage'); } $ebook = $registration->getEbook(); return $this->render('vitrine/'.$themeSelection.'/ebooks/page_afterregistered.html.twig',[ 'ebook' => $ebook, 'identifiant' => $identifiant, ]); } /** * Télécharger un Ebook en fonction de l'identifiant * @param Request $request * @param UploaderHelper $helper * @param $identifiant * @return Response */ public function download(Request $request, UploaderHelper $helper, $identifiant) { $registration = $this->em->getRepository(EbookHasRegistrations::class)->findOneBy(['codeRegistration' => $identifiant]); if($registration == null) { return $this->redirectToRoute('homepage'); } $registration->setDownloaded(true); $this->em->persist($registration); $this->em->flush(); $ebook = $registration->getEbook(); $path = $_ENV["PATH_FOLDER"]; $pathname = $path.$helper->asset($ebook); $contentType = mime_content_type($pathname); if (ob_get_level()) ob_end_clean(); header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header("Content-type: ".$contentType); header("Content-Disposition: inline; filename=".$contentType); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); clearstatcache(); readfile($pathname); clearstatcache(); return new Response(); }}