src/Controller/Admin/FileManagerController.php line 428

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use EADPlataforma\Error\ActionInvalidException;
  4. use EADPlataforma\Error\FieldException;
  5. use EADPlataforma\Error\NotFoundException;
  6. use EADPlataforma\Error\PermissionException;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  8. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use EADPlataforma\Entity\Library;
  14. use EADPlataforma\Entity\Lesson;
  15. use EADPlataforma\Entity\Session;
  16. use EADPlataforma\Enum\LibraryEnum;
  17. use EADPlataforma\Enum\LessonEnum;
  18. use EADPlataforma\Enum\AbstractEnum;
  19. use EADPlataforma\Enum\ServicesEnum;
  20. use EADPlataforma\Enum\ErrorEnum;
  21. /**
  22.  * @Route(
  23.  *      schemes         = {"http|https"}
  24.  * )
  25.  * @Cache(
  26.  *      maxage          = "0",
  27.  *      smaxage         = "0",
  28.  *      expires         = "now",
  29.  *      public          = false
  30.  * )
  31.  */
  32. class FileManagerController extends AbstractController {
  33.     public function getEntityClass(){
  34.         return;
  35.     }
  36.     /**
  37.      * @Route(
  38.      *      path          = "/stream/content/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
  39.      *      methods       = {"GET"},
  40.      *      name          = "getStremLibraryContent",
  41.      *      defaults      = { "sessionHash" = null, "lessonHash" = null }
  42.      * )
  43.      */
  44.     public function getStremLibraryContent(Request $request) {
  45.         $sessionHash $request->get('sessionHash');
  46.         $lessonHash $request->get('lessonHash');
  47.         $libraryRepository $this->em->getRepository(Library::class);
  48.         $libraryId $this->stringUtil->decodeHex($request->get('hash'), falsefalse);
  49.         $library $libraryRepository->findOneBy([
  50.             "id" => $libraryId,
  51.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  52.         ]);
  53.         $extension $library->getFileExtension();
  54.         if($extension != "pdf" || empty($sessionHash) || empty($lessonHash)){
  55.             if(strpos($request->headers->get('User-Agent'), 'Google AppsViewer') === false){
  56.                 //return $this->redirectToRoute('notFound');
  57.             }
  58.         }
  59.         $date strtotime(
  60.             $this->stringUtil->decodeHex($request->get('timeHash'), falsefalse)
  61.         );
  62.         $now strtotime(date('Y-m-d H:i:s'));
  63.         $diff = ($now $date);
  64.         if($diff 300){
  65.             return $this->redirectToRoute('notFound');
  66.         }
  67.         $user null;
  68.         if(!empty($sessionHash)){
  69.             $sessionHash $this->stringUtil->decodeHex($sessionHashfalsefalse);
  70.             $session $this->em->getRepository(Session::class)->findOneBy([
  71.                 "token" => $sessionHash,
  72.                 "deleted" => AbstractEnum::ITEM_NO_DELETED
  73.             ]);
  74.             
  75.             if(!$session || !$session->isValid()){
  76.                 return $this->redirectToRoute('notFound');
  77.             }
  78.             $user $session->getUser();
  79.         }
  80.         $info $libraryRepository->getContentInfo($librarytrue);
  81.         if(!empty($info->url)){
  82.             $hasModule $this->configuration->checkModuleIsAbleOnPlan(
  83.                 'lessonControlFunction'
  84.             );
  85.             $drmPdf $this->configuration->get("drm_pdf");
  86.             //check plan
  87.             if($hasModule && $drmPdf == LibraryEnum::YES){
  88.                 if($extension == "pdf" && !empty($sessionHash) && !empty($lessonHash)){
  89.                     $lessonId $this->stringUtil->decodeHex($lessonHashfalsefalse);
  90.                     $lesson $this->em->getRepository(lesson::class)->findOneBy([
  91.                         "id" => $lessonId,
  92.                         "deleted" => LessonEnum::ITEM_NO_DELETED
  93.                     ]);
  94.                     if($lesson && $lesson->getControlShowDocument() == LessonEnum::YES){
  95.                         $pdfService $this->generalService->getService('PdfService');
  96.                         $options $libraryRepository->getDRMOptions(
  97.                             $library->getTitle(), 
  98.                             $user
  99.                         );
  100.                         try{
  101.                             $pdfService->showWithDRM($info->url$options);
  102.                             exit;
  103.                         }catch(\Exception $e){
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.             return $this->redirect($info->url301);
  109.         }
  110.         return $this->redirectToRoute('notFound');
  111.     }
  112.     /**
  113.      * @Route(
  114.      *      path          = "/library/thumb/{id}",
  115.      *      methods       = {"GET"},
  116.      *      name          = "getThumbLibrary"
  117.      * )
  118.      */
  119.     public function getThumbLibrary(Request $request) {
  120.         $id $request->get('id');
  121.         $libraryRepository $this->em->getRepository(Library::class);
  122.         $library $libraryRepository->findOneBy([
  123.             "id" => $id,
  124.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  125.         ]);
  126.         if(!$library){
  127.             throw new NotFoundException("Library not found");
  128.         }
  129.         $url $libraryRepository->getCoverNew($library);
  130.         if(empty($url)){
  131.             throw new NotFoundException("Thumbnail link not found");
  132.         }
  133.         return $this->redirect($url301);
  134.     }
  135.     /**
  136.      * @Route(
  137.      *      path          = "/text/content/pdf/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
  138.      *      methods       = {"GET"},
  139.      *      name          = "getTextContentPDF"
  140.      * )
  141.      */
  142.     public function getTextContentPDF(Request $request) {
  143.         $sessionHash $request->get('sessionHash');
  144.         $lessonHash $request->get('lessonHash');
  145.         $libraryRepository $this->em->getRepository(Library::class);
  146.         $libraryId $this->stringUtil->decodeHex($request->get('hash'), falsefalse);
  147.         $library $libraryRepository->findOneBy([
  148.             "id" => $libraryId,
  149.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  150.         ]);
  151.         $date strtotime(
  152.             $this->stringUtil->decodeHex($request->get('timeHash'), falsefalse)
  153.         );
  154.         $now strtotime(date('Y-m-d H:i:s'));
  155.         $diff = ($now $date);
  156.         if($diff 300){
  157.             return $this->redirectToRoute('notFound');
  158.         }
  159.         if(!empty($sessionHash)){
  160.             $sessionHash $this->stringUtil->decodeHex($sessionHashfalsefalse);
  161.             $session $this->em->getRepository(Session::class)->findOneBy([
  162.                 "token" => $sessionHash,
  163.                 "deleted" => AbstractEnum::ITEM_NO_DELETED
  164.             ]);
  165.             
  166.             if(!$session || !$session->isValid()){
  167.                 return $this->redirectToRoute('notFound');
  168.             }
  169.         }
  170.         //check can see content
  171.         if($library->getType() == LibraryEnum::CONTENT_TEXT){
  172.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  173.             $drmPdf $this->configuration->get("drm_pdf");
  174.             //check plan
  175.             if($hasModule && $drmPdf == LibraryEnum::YES){
  176.                 $pdfService $this->generalService->getService('PdfService');
  177.                 $info $this->generalService->getServiceAccess(ServicesEnum::AWS_S3);
  178.                 $clientConnection $this->configuration->getClientConnection();
  179.                 $serverUser $clientConnection->getServerUser();
  180.                 $imageUrl "https:{$info->cdnLink}client/{$serverUser}";
  181.                 $newText str_replace('img src="''img src="' $imageUrl$library->getText());
  182.                 $newText str_replace('a href="''a href="' $imageUrl$newText);
  183.                 $data = [
  184.                     "title" => $library->getTitle(),
  185.                     "text" => html_entity_decode($newText)
  186.                 ];
  187.                 $pdfService->setFileName($library->getTitle());
  188.                 $pdfService->setTemplateBody("library_text_content");
  189.                 $pdfService->setData($data);
  190.                 return $pdfService->generate();
  191.             }
  192.         }
  193.         return $this->redirectToRoute('notFound');
  194.     }
  195.     /**
  196.      * @Route(
  197.      *      path          = "/text/content/pdf/drm/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
  198.      *      methods       = {"GET"},
  199.      *      name          = "getTextContentPDFDrm"
  200.      * )
  201.      */
  202.     public function getTextContentPDFDrm(Request $request) {
  203.         $hash $request->get('hash');
  204.         $timeHash $request->get('timeHash');
  205.         $sessionHash $request->get('sessionHash');
  206.         $lessonHash $request->get('lessonHash');
  207.         $libraryRepository $this->em->getRepository(Library::class);
  208.         $libraryId $this->stringUtil->decodeHex($hashfalsefalse);
  209.         $library $libraryRepository->findOneBy([
  210.             "id" => $libraryId,
  211.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  212.         ]);
  213.         $date strtotime(
  214.             $this->stringUtil->decodeHex($timeHashfalsefalse)
  215.         );
  216.         $now strtotime(date('Y-m-d H:i:s'));
  217.         $diff = ($now $date);
  218.         if($diff 300){
  219.             return $this->redirectToRoute('notFound');
  220.         }
  221.         $user null;
  222.         if(!empty($sessionHash)){
  223.             $sessionToken $this->stringUtil->decodeHex($sessionHashfalsefalse);
  224.             $session $this->em->getRepository(Session::class)->findOneBy([
  225.                 "token" => $sessionToken,
  226.                 "deleted" => AbstractEnum::ITEM_NO_DELETED
  227.             ]);
  228.             
  229.             if(!$session || !$session->isValid()){
  230.                 return $this->redirectToRoute('notFound');
  231.             }
  232.             $user $session->getUser();
  233.         }
  234.         //check can see content
  235.         if($library->getType() == LibraryEnum::CONTENT_TEXT){
  236.             $url "https://{$request->getHost()}/text/content/pdf/{$hash}/{$timeHash}/{$sessionHash}/{$lessonHash}";
  237.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  238.             $drmPdf $this->configuration->get("drm_pdf");
  239.             //check plan
  240.             if($hasModule && $drmPdf == LibraryEnum::YES){
  241.                 $pdfService $this->generalService->getService('PdfService');
  242.                 $options $libraryRepository->getDRMOptions(
  243.                     $library->getTitle(), 
  244.                     $user
  245.                 );
  246.                 try{
  247.                     $pdfService->showWithDRM($url$options);
  248.                     exit;
  249.                 }catch(\Exception $e){
  250.                 }
  251.             }
  252.         }
  253.         return $this->redirect($url301);
  254.     }
  255.     /**
  256.      * @Route(
  257.      *      path          = "/admin/preview/pdf/{libraryId}",
  258.      *      methods       = {"GET"},
  259.      *      name          = "getPreviewPDF"
  260.      * )
  261.      */
  262.     public function getPreviewPDF(Request $request) {
  263.         $libraryId $request->get('libraryId');
  264.         $libraryRepository $this->em->getRepository(Library::class);
  265.         $library $libraryRepository->findOneBy([
  266.             "id" => $libraryId,
  267.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  268.         ]);
  269.         if(!$library){
  270.             return $this->redirectToRoute('notFound');
  271.         }
  272.         //check can see content
  273.         $extension $library->getFileExtension();
  274.         if($extension != "pdf"){
  275.             return $this->redirectToRoute('notFound');
  276.         }
  277.         $info $libraryRepository->getContentInfo($librarytrue);
  278.         if(!empty($info->url)){
  279.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  280.             $drmPdf $this->configuration->get("drm_pdf");
  281.             //check plan
  282.             if($hasModule && $drmPdf == LibraryEnum::YES){
  283.                 $pdfService $this->generalService->getService('PdfService');
  284.                 $options $libraryRepository->getDRMOptions(
  285.                     $library->getTitle(), 
  286.                     $this->user
  287.                 );
  288.                 try{
  289.                     $pdfService->showWithDRM($info->url$options);
  290.                     exit;
  291.                 }catch(\Exception $e){
  292.                 }
  293.             }
  294.             return $this->redirect($info->url301);
  295.         }
  296.         return $this->redirectToRoute('notFound');
  297.     }
  298.     /**
  299.      * @Route(
  300.      *      path          = "/admin/sample/pdf",
  301.      *      methods       = {"GET"},
  302.      *      name          = "getSamplePDF",
  303.      * )
  304.      */
  305.     public function getSamplePDF(Request $request) {
  306.         $permission $this->userPermissionUtil->getPermission(
  307.             "course"
  308.             "course_configuration"
  309.             "allow_config"
  310.         );
  311.         if($this->userPermissionUtil->isLow($permission)){
  312.             return $this->redirectToRoute('notFound');
  313.         }
  314.         
  315.         //check plan
  316.         if(!$this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction')){
  317.             return $this->redirectToRoute('notFound');
  318.         }
  319.         if(!$this->user){
  320.             return $this->redirectToRoute('notFound');
  321.         }
  322.         $drmPdf $this->configuration->get("drm_pdf");
  323.         if($drmPdf == LibraryEnum::NO){
  324.             return $this->redirectToRoute('notFound');
  325.         }
  326.         $pdfService $this->generalService->getService('PdfService');
  327.         $libraryRepository $this->em->getRepository(Library::class);
  328.         $options $libraryRepository->getDRMOptions(
  329.             "ead-sample"
  330.             $this->user
  331.         );
  332.         $url "https://cdn.eadplataforma.app/assets/files/sample.pdf";
  333.         $pdfService->showWithDRM($url$options);
  334.         exit;
  335.     }
  336.     /**
  337.      * @Route(
  338.      *      path          = "/upload/{fileName}",
  339.      *      name          = "fileGetUpload",
  340.      *      methods       = {"GET"},
  341.      *      requirements  = {"fileName"=".+"}
  342.      * )
  343.      */
  344.     public function getFile(Request $request) {
  345.         $this->requestUtil->setRequest($request)->setData();
  346.         $fileName $request->get('fileName');
  347.         $stream AbstractEnum::NO;//$request->get('stream');
  348.         $option $request->get('option');
  349.         $fileNameOrigin $fileName;
  350.         $extension $this->fileService->getFileExtensionFromName($fileName);
  351.         if(!empty($option)){
  352.             $pathCrop AbstractEnum::PATH_CROP;
  353.             $fileName "{$pathCrop}/{$fileName}-{$option}.{$extension}";
  354.         }
  355.         $dir dirname($fileName);
  356.         if($dir != "lesson"){
  357.             $this->fileService->setFile($fileName);
  358.             $url $this->fileService->getFileUrlTemp(!empty($option) ? true false);
  359.             if(empty($url) && !empty($option)){
  360.                 $size $this->fileService->getImageOptionSize($option);
  361.                 $this->fileService->setFile($fileNameOrigin);
  362.                 $url $this->fileService->getFileUrlTemp(false);
  363.                 $hash rand() . md5(rand() . rand() . date('Y-m-d-H-i-s'));
  364.                 $fileTempEx "{$this->generalService->getPath()}/var/{$hash}.{$extension}";
  365.                 $this->fileService->createTempFileFromUrl("https:{$url}"$fileTempEx);
  366.                 if($extension != "gif"){
  367.                     $this->fileService->resizeFile($fileTempEx$size->width$size->height);
  368.                 }
  369.                 $this->fileService->createFile($fileTempEx$fileName);
  370.                 $this->fileService->setFile($fileName);
  371.                 unlink($fileTempEx);
  372.                 $url $this->fileService->getFileUrlTemp();
  373.             }
  374.             if($url){
  375.                 if(!empty($stream)){
  376.                     $response = new Response(file_get_contents($url));
  377.                     $disposition $response->headers->makeDisposition(
  378.                         ResponseHeaderBag::DISPOSITION_INLINE
  379.                         $this->fileService->getFileName()
  380.                     );
  381.                     // Set the content disposition
  382.                     $mime =  $this->fileService->getFileMimeTypeFromName($fileName);
  383.                     $response->headers->set('Content-Disposition'$disposition);
  384.                     $response->headers->set('Content-Type'$mime);
  385.                     return $response;
  386.                 }
  387.                 return $this->redirect($url301);
  388.             }
  389.         }
  390.         exit;
  391.     }
  392.     /**
  393.      * @Route(
  394.      *      path          = "/ead/player",
  395.      *      name          = "eadPlayer",
  396.      *      methods       = {"GET"},
  397.      * )
  398.      */
  399.     public function getEadPlayer(Request $request) {
  400.         return $this->redirect("https://dev.vdocipher.com/playerAssets/1.6.10/vdo.js"301);
  401.     }
  402.     /**
  403.      * @Route(
  404.      *      path          = "/ead/player/video/{id}",
  405.      *      name          = "eadPlayerVideo",
  406.      *      methods       = {"GET"},
  407.      * )
  408.      */
  409.     public function getEadPlayerVideo(Request $request) {
  410.         $libraryRepository $this->em->getRepository(Library::class);
  411.         $library $libraryRepository->findOneBy([
  412.             "id" => $request->get('id'),
  413.             "deleted" => LibraryEnum::ITEM_NO_DELETED,
  414.         ]);
  415.         $credentials $libraryRepository->getVideoCredentials($library);
  416.         $link "https://player.vdocipher.com/v2/?";
  417.         $data = [
  418.             "otp" => $credentials->otp,
  419.             "playbackInfo" => $credentials->playbackInfo,
  420.             "primaryColor" => $this->configuration->get('primary_color'),
  421.         ];
  422.         $link $link http_build_query($data);
  423.         return $this->redirect($link301);
  424.     }
  425.     /**
  426.      * @Route(
  427.      *      path          = "/ead/player/new",
  428.      *      name          = "eadPlayerNew",
  429.      *      methods       = {"GET"},
  430.      * )
  431.      */
  432.     public function getEadPlayerNew(Request $request) {
  433.         return $this->redirect("https://player.vdocipher.com/v2/api.js"301);
  434.     }
  435.     /**
  436.      * @Route(
  437.      *      path          = "/ead/captcha",
  438.      *      name          = "eadCaptcha",
  439.      *      methods       = {"GET"},
  440.      * )
  441.      */
  442.     public function getEadCaptcha(Request $request) {
  443.         $key $request->get('key');
  444.         $data $this->memcacheService->getData($key);
  445.        
  446.         $captchaService $this->generalService->getService('CaptchaService');
  447.         return $captchaService->generateCaptcha($data);
  448.     }
  449.     /**
  450.      * @Route(
  451.      *      path          = "/ead/captcha/reload/{key}",
  452.      *      name          = "eadCaptchaReload",
  453.      *      methods       = {"GET"},
  454.      * )
  455.      */
  456.     public function getEadCaptchaReload(Request $request) {
  457.         $key $request->get('key');
  458.        
  459.         $this->memcacheService->deleteData($key);
  460.         $preKey md5("captcha");
  461.         $newKey $preKey."_".md5($this->client->getDomainPrimary().date('Y-m-d H:i:s').$request->getClientIp());
  462.         $data $this->stringUtil->randomText(6);
  463.         $this->memcacheService->saveData($newKey$data60*60*24);
  464.         
  465.         return $this->eadResponse($newKey);
  466.     }
  467.     /**
  468.      * @Route(
  469.      *      path          = "admin/file/manager/image",
  470.      *      name          = "imgRegisterUpload",
  471.      *      methods       = {"POST"},
  472.      * )
  473.      */
  474.     public function registerImage(Request $request) {
  475.         $this->requestUtil->setRequest($request)->setData();
  476.         $file $this->requestUtil->getFile('file');
  477.         $file $this->fileService->setFile($file);
  478.         if($file){
  479.             $this->fileService->moveFile(AbstractEnum::PATH_OTHERS);
  480.         }
  481.         return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
  482.     }
  483.     /**
  484.      * @Route(
  485.      *      path          = "admin/file/manager/file",
  486.      *      name          = "fileRegisterUpload",
  487.      *      methods       = {"POST"},
  488.      * )
  489.      */
  490.     public function registerFile(Request $request) {
  491.         $this->requestUtil->setRequest($request)->setData();
  492.         $file $this->requestUtil->getFile('file');
  493.         $file $this->fileService->setFile($file);
  494.         if($file){
  495.             $this->fileService->moveFile(AbstractEnum::PATH_FILES);
  496.         }
  497.         return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
  498.     }
  499.     /**
  500.      * @Route(
  501.      *      path          = "admin/file/manager/delete",
  502.      *      name          = "fileDeleteUpload",
  503.      *      methods       = {"DELETE"}
  504.      * )
  505.      */
  506.     public function deleteFile(Request $request) {
  507.         $this->requestUtil->setRequest($request)->setData();
  508.         $file $this->requestUtil->getField('file');
  509.         if(!empty($file)){
  510.             $file explode(AbstractEnum::PATH_UPLOAD$file);
  511.             $file end($file);
  512.             $file urldecode($file);
  513.             $dir dirname($file);
  514.             
  515.             if($dir == AbstractEnum::PATH_FILES || $dir == AbstractEnum::PATH_OTHERS){
  516.                 $this->fileService->setFile($file);
  517.                 $this->fileService->deleteFile();
  518.             }
  519.         }
  520.         return $this->eadResponse([ "delete" => ]);
  521.     }
  522. }