src/Controller/Admin/FileManagerController.php line 558

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.         $lessonRepository $this->em->getRepository(Lesson::class);
  152.         $lessonId $this->stringUtil->decodeHex($lessonHashfalsefalse);
  153.         $lesson $lessonRepository->findOneBy([
  154.             "id" => $lessonId,
  155.             "deleted" => LessonEnum::ITEM_NO_DELETED
  156.         ]);
  157.         $date strtotime(
  158.             $this->stringUtil->decodeHex($request->get('timeHash'), falsefalse)
  159.         );
  160.         $now strtotime(date('Y-m-d H:i:s'));
  161.         $diff = ($now $date);
  162.         if($diff 300){
  163.             return $this->redirectToRoute('notFound');
  164.         }
  165.         if(!empty($sessionHash)){
  166.             $sessionHash $this->stringUtil->decodeHex($sessionHashfalsefalse);
  167.             $session $this->em->getRepository(Session::class)->findOneBy([
  168.                 "token" => $sessionHash,
  169.                 "deleted" => AbstractEnum::ITEM_NO_DELETED
  170.             ]);
  171.             
  172.             if(!$session || !$session->isValid()){
  173.                 return $this->redirectToRoute('notFound');
  174.             }
  175.         }
  176.         //check can see content
  177.         if($library->getType() == LibraryEnum::CONTENT_TEXT){
  178.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  179.             $drmPdf $this->configuration->get("drm_pdf");
  180.             //check plan
  181.             if($hasModule && $drmPdf == LibraryEnum::YES){
  182.                 $pdfService $this->generalService->getService('PdfService');
  183.                 $info $this->generalService->getServiceAccess(ServicesEnum::AWS_S3);
  184.                 $clientConnection $this->configuration->getClientConnection();
  185.                 $serverUser $clientConnection->getServerUser();
  186.                 $imageUrl "https:{$info->cdnLink}client/{$serverUser}";
  187.                 $newText str_replace('img src="''img src="' $imageUrl$library->getText());
  188.                 $newText str_replace('a href="''a href="' $imageUrl$newText);
  189.                 $infoTag $lessonRepository->setLessonTag(
  190.                     $lesson
  191.                     $this->user
  192.                     $newText
  193.                 );
  194.                 $data = [
  195.                     "title" => $library->getTitle(),
  196.                     "text" => html_entity_decode($infoTag->content)
  197.                 ];
  198.                 $pdfService->setFileName($library->getTitle());
  199.                 $pdfService->setTemplateBody("library_text_content");
  200.                 $pdfService->setData($data);
  201.                 return $pdfService->generate();
  202.             }
  203.         }
  204.         return $this->redirectToRoute('notFound');
  205.     }
  206.     /**
  207.      * @Route(
  208.      *      path          = "/text/content/pdf/drm/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
  209.      *      methods       = {"GET"},
  210.      *      name          = "getTextContentPDFDrm"
  211.      * )
  212.      */
  213.     public function getTextContentPDFDrm(Request $request) {
  214.         $hash $request->get('hash');
  215.         $timeHash $request->get('timeHash');
  216.         $sessionHash $request->get('sessionHash');
  217.         $lessonHash $request->get('lessonHash');
  218.         $libraryRepository $this->em->getRepository(Library::class);
  219.         $libraryId $this->stringUtil->decodeHex($hashfalsefalse);
  220.         $library $libraryRepository->findOneBy([
  221.             "id" => $libraryId,
  222.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  223.         ]);
  224.         $date strtotime(
  225.             $this->stringUtil->decodeHex($timeHashfalsefalse)
  226.         );
  227.         $now strtotime(date('Y-m-d H:i:s'));
  228.         $diff = ($now $date);
  229.         if($diff 300){
  230.             return $this->redirectToRoute('notFound');
  231.         }
  232.         $user null;
  233.         if(!empty($sessionHash)){
  234.             $sessionToken $this->stringUtil->decodeHex($sessionHashfalsefalse);
  235.             $session $this->em->getRepository(Session::class)->findOneBy([
  236.                 "token" => $sessionToken,
  237.                 "deleted" => AbstractEnum::ITEM_NO_DELETED
  238.             ]);
  239.             
  240.             if(!$session || !$session->isValid()){
  241.                 return $this->redirectToRoute('notFound');
  242.             }
  243.             $user $session->getUser();
  244.         }
  245.         //check can see content
  246.         if($library->getType() == LibraryEnum::CONTENT_TEXT){
  247.             $url "https://{$request->getHost()}/text/content/pdf/{$hash}/{$timeHash}/{$sessionHash}/{$lessonHash}";
  248.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  249.             $drmPdf $this->configuration->get("drm_pdf");
  250.             //check plan
  251.             if($hasModule && $drmPdf == LibraryEnum::YES){
  252.                 $pdfService $this->generalService->getService('PdfService');
  253.                 $options $libraryRepository->getDRMOptions(
  254.                     $library->getTitle(), 
  255.                     $user
  256.                 );
  257.                 try{
  258.                     $pdfService->showWithDRM($url$options);
  259.                     exit;
  260.                 }catch(\Exception $e){
  261.                 }
  262.             }
  263.         }
  264.         return $this->redirect($url301);
  265.     }
  266.     /**
  267.      * @Route(
  268.      *      path          = "/admin/preview/pdf/{libraryId}",
  269.      *      methods       = {"GET"},
  270.      *      name          = "getPreviewPDF"
  271.      * )
  272.      */
  273.     public function getPreviewPDF(Request $request) {
  274.         $libraryId $request->get('libraryId');
  275.         $libraryRepository $this->em->getRepository(Library::class);
  276.         $library $libraryRepository->findOneBy([
  277.             "id" => $libraryId,
  278.             "deleted" => LibraryEnum::ITEM_NO_DELETED
  279.         ]);
  280.         if(!$library){
  281.             return $this->redirectToRoute('notFound');
  282.         }
  283.         //check can see content
  284.         $extension $library->getFileExtension();
  285.         if($extension != "pdf"){
  286.             return $this->redirectToRoute('notFound');
  287.         }
  288.         $info $libraryRepository->getContentInfo($librarytrue);
  289.         if(!empty($info->url)){
  290.             $hasModule $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
  291.             $drmPdf $this->configuration->get("drm_pdf");
  292.             //check plan
  293.             if($hasModule && $drmPdf == LibraryEnum::YES){
  294.                 $pdfService $this->generalService->getService('PdfService');
  295.                 $options $libraryRepository->getDRMOptions(
  296.                     $library->getTitle(), 
  297.                     $this->user
  298.                 );
  299.                 try{
  300.                     $pdfService->showWithDRM($info->url$options);
  301.                     exit;
  302.                 }catch(\Exception $e){
  303.                 }
  304.             }
  305.             return $this->redirect($info->url301);
  306.         }
  307.         return $this->redirectToRoute('notFound');
  308.     }
  309.     /**
  310.      * @Route(
  311.      *      path          = "/admin/sample/pdf",
  312.      *      methods       = {"GET"},
  313.      *      name          = "getSamplePDF",
  314.      * )
  315.      */
  316.     public function getSamplePDF(Request $request) {
  317.         $permission $this->userPermissionUtil->getPermission(
  318.             "course"
  319.             "course_configuration"
  320.             "allow_config"
  321.         );
  322.         if($this->userPermissionUtil->isLow($permission)){
  323.             return $this->redirectToRoute('notFound');
  324.         }
  325.         
  326.         //check plan
  327.         if(!$this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction')){
  328.             return $this->redirectToRoute('notFound');
  329.         }
  330.         if(!$this->user){
  331.             return $this->redirectToRoute('notFound');
  332.         }
  333.         $drmPdf $this->configuration->get("drm_pdf");
  334.         if($drmPdf == LibraryEnum::NO){
  335.             return $this->redirectToRoute('notFound');
  336.         }
  337.         $pdfService $this->generalService->getService('PdfService');
  338.         $libraryRepository $this->em->getRepository(Library::class);
  339.         $options $libraryRepository->getDRMOptions(
  340.             "ead-sample"
  341.             $this->user
  342.         );
  343.         $url "https://cdn.eadplataforma.app/assets/files/sample.pdf";
  344.         $pdfService->showWithDRM($url$options);
  345.         exit;
  346.     }
  347.     /**
  348.      * @Route(
  349.      *      path          = "/upload/{fileName}",
  350.      *      name          = "fileGetUpload",
  351.      *      methods       = {"GET"},
  352.      *      requirements  = {"fileName"=".+"}
  353.      * )
  354.      */
  355.     public function getFile(Request $request) {
  356.         $this->requestUtil->setRequest($request)->setData();
  357.         $fileName $request->get('fileName');
  358.         $stream AbstractEnum::NO;//$request->get('stream');
  359.         $option $request->get('option');
  360.         $fileNameOrigin $fileName;
  361.         $extension $this->fileService->getFileExtensionFromName($fileName);
  362.         if(!empty($option)){
  363.             $pathCrop AbstractEnum::PATH_CROP;
  364.             $fileName "{$pathCrop}/{$fileName}-{$option}.{$extension}";
  365.         }
  366.         $dir dirname($fileName);
  367.         if($dir != "lesson"){
  368.             $this->fileService->setFile($fileName);
  369.             $url $this->fileService->getFileUrlTemp(!empty($option) ? true false);
  370.             if(empty($url) && !empty($option)){
  371.                 $size $this->fileService->getImageOptionSize($option);
  372.                 $this->fileService->setFile($fileNameOrigin);
  373.                 $url $this->fileService->getFileUrlTemp(false);
  374.                 $hash rand() . md5(rand() . rand() . date('Y-m-d-H-i-s'));
  375.                 $fileTempEx "{$this->generalService->getPath()}/var/{$hash}.{$extension}";
  376.                 $this->fileService->createTempFileFromUrl("https:{$url}"$fileTempEx);
  377.                 if($extension != "gif"){
  378.                     $this->fileService->resizeFile($fileTempEx$size->width$size->height);
  379.                 }
  380.                 $this->fileService->createFile($fileTempEx$fileName);
  381.                 $this->fileService->setFile($fileName);
  382.                 unlink($fileTempEx);
  383.                 $url $this->fileService->getFileUrlTemp();
  384.             }
  385.             if($url){
  386.                 if(!empty($stream)){
  387.                     $response = new Response(file_get_contents($url));
  388.                     $disposition $response->headers->makeDisposition(
  389.                         ResponseHeaderBag::DISPOSITION_INLINE
  390.                         $this->fileService->getFileName()
  391.                     );
  392.                     // Set the content disposition
  393.                     $mime =  $this->fileService->getFileMimeTypeFromName($fileName);
  394.                     $response->headers->set('Content-Disposition'$disposition);
  395.                     $response->headers->set('Content-Type'$mime);
  396.                     return $response;
  397.                 }
  398.                 return $this->redirect($url301);
  399.             }
  400.         }
  401.         exit;
  402.     }
  403.     /**
  404.      * @Route(
  405.      *      path          = "/ead/player",
  406.      *      name          = "eadPlayer",
  407.      *      methods       = {"GET"},
  408.      * )
  409.      */
  410.     public function getEadPlayer(Request $request) {
  411.         return $this->redirect("https://dev.vdocipher.com/playerAssets/1.6.10/vdo.js"301);
  412.     }
  413.     /**
  414.      * @Route(
  415.      *      path          = "/ead/player/video/{id}",
  416.      *      name          = "eadPlayerVideo",
  417.      *      methods       = {"GET"},
  418.      * )
  419.      */
  420.     public function getEadPlayerVideo(Request $request) {
  421.         $libraryRepository $this->em->getRepository(Library::class);
  422.         $library $libraryRepository->findOneBy([
  423.             "id" => $request->get('id'),
  424.             "deleted" => LibraryEnum::ITEM_NO_DELETED,
  425.         ]);
  426.         $credentials $libraryRepository->getVideoCredentials($library);
  427.         $link "https://player.vdocipher.com/v2/?";
  428.         $data = [
  429.             "otp" => $credentials->otp,
  430.             "playbackInfo" => $credentials->playbackInfo,
  431.             "primaryColor" => $this->configuration->get('primary_color'),
  432.         ];
  433.         $link $link http_build_query($data);
  434.         return $this->redirect($link301);
  435.     }
  436.     /**
  437.      * @Route(
  438.      *      path          = "/ead/player/new",
  439.      *      name          = "eadPlayerNew",
  440.      *      methods       = {"GET"},
  441.      * )
  442.      */
  443.     public function getEadPlayerNew(Request $request) {
  444.         return $this->redirect("https://player.vdocipher.com/v2/api.js"301);
  445.     }
  446.     /**
  447.      * @Route(
  448.      *      path          = "/ead/captcha",
  449.      *      name          = "eadCaptcha",
  450.      *      methods       = {"GET"},
  451.      * )
  452.      */
  453.     public function getEadCaptcha(Request $request) {
  454.         $key $request->get('key');
  455.         $data $this->memcacheService->getData($key);
  456.        
  457.         $captchaService $this->generalService->getService('CaptchaService');
  458.         return $captchaService->generateCaptcha($data);
  459.     }
  460.     /**
  461.      * @Route(
  462.      *      path          = "/ead/captcha/reload/{key}",
  463.      *      name          = "eadCaptchaReload",
  464.      *      methods       = {"GET"},
  465.      * )
  466.      */
  467.     public function getEadCaptchaReload(Request $request) {
  468.         $key $request->get('key');
  469.        
  470.         $this->memcacheService->deleteData($key);
  471.         $preKey md5("captcha");
  472.         $newKey $preKey."_".md5($this->client->getDomainPrimary().date('Y-m-d H:i:s').$request->getClientIp());
  473.         $data $this->stringUtil->randomText(6);
  474.         $this->memcacheService->saveData($newKey$data60*60*24);
  475.         
  476.         return $this->eadResponse($newKey);
  477.     }
  478.     /**
  479.      * @Route(
  480.      *      path          = "admin/file/manager/image",
  481.      *      name          = "imgRegisterUpload",
  482.      *      methods       = {"POST"},
  483.      * )
  484.      */
  485.     public function registerImage(Request $request) {
  486.         $this->requestUtil->setRequest($request)->setData();
  487.         $file $this->requestUtil->getFile('file');
  488.         $file $this->fileService->setFile($file);
  489.         if($file){
  490.             $this->fileService->moveFile(AbstractEnum::PATH_OTHERS);
  491.         }
  492.         return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
  493.     }
  494.     /**
  495.      * @Route(
  496.      *      path          = "admin/file/manager/file",
  497.      *      name          = "fileRegisterUpload",
  498.      *      methods       = {"POST"},
  499.      * )
  500.      */
  501.     public function registerFile(Request $request) {
  502.         $this->requestUtil->setRequest($request)->setData();
  503.         $file $this->requestUtil->getFile('file');
  504.         $file $this->fileService->setFile($file);
  505.         if($file){
  506.             $this->fileService->moveFile(AbstractEnum::PATH_FILES);
  507.         }
  508.         return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
  509.     }
  510.     /**
  511.      * @Route(
  512.      *      path          = "admin/file/manager/delete",
  513.      *      name          = "fileDeleteUpload",
  514.      *      methods       = {"DELETE"}
  515.      * )
  516.      */
  517.     public function deleteFile(Request $request) {
  518.         $this->requestUtil->setRequest($request)->setData();
  519.         $file $this->requestUtil->getField('file');
  520.         if(!empty($file)){
  521.             $file explode(AbstractEnum::PATH_UPLOAD$file);
  522.             $file end($file);
  523.             $file urldecode($file);
  524.             $dir dirname($file);
  525.             
  526.             if($dir == AbstractEnum::PATH_FILES || $dir == AbstractEnum::PATH_OTHERS){
  527.                 $this->fileService->setFile($file);
  528.                 $this->fileService->deleteFile();
  529.             }
  530.         }
  531.         return $this->eadResponse([ "delete" => ]);
  532.     }
  533. }