<?php
namespace EADPlataforma\Controller\Admin;
use EADPlataforma\Error\ActionInvalidException;
use EADPlataforma\Error\FieldException;
use EADPlataforma\Error\NotFoundException;
use EADPlataforma\Error\PermissionException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\Library;
use EADPlataforma\Entity\Lesson;
use EADPlataforma\Entity\Session;
use EADPlataforma\Enum\LibraryEnum;
use EADPlataforma\Enum\LessonEnum;
use EADPlataforma\Enum\AbstractEnum;
use EADPlataforma\Enum\ServicesEnum;
use EADPlataforma\Enum\ErrorEnum;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class FileManagerController extends AbstractController {
public function getEntityClass(){
return;
}
/**
* @Route(
* path = "/stream/content/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
* methods = {"GET"},
* name = "getStremLibraryContent",
* defaults = { "sessionHash" = null, "lessonHash" = null }
* )
*/
public function getStremLibraryContent(Request $request) {
$sessionHash = $request->get('sessionHash');
$lessonHash = $request->get('lessonHash');
$libraryRepository = $this->em->getRepository(Library::class);
$libraryId = $this->stringUtil->decodeHex($request->get('hash'), false, false);
$library = $libraryRepository->findOneBy([
"id" => $libraryId,
"deleted" => LibraryEnum::ITEM_NO_DELETED
]);
$extension = $library->getFileExtension();
if($extension != "pdf" || empty($sessionHash) || empty($lessonHash)){
if(strpos($request->headers->get('User-Agent'), 'Google AppsViewer') === false){
//return $this->redirectToRoute('notFound');
}
}
$date = strtotime(
$this->stringUtil->decodeHex($request->get('timeHash'), false, false)
);
$now = strtotime(date('Y-m-d H:i:s'));
$diff = ($now - $date);
if($diff > 300){
return $this->redirectToRoute('notFound');
}
$user = null;
if(!empty($sessionHash)){
$sessionHash = $this->stringUtil->decodeHex($sessionHash, false, false);
$session = $this->em->getRepository(Session::class)->findOneBy([
"token" => $sessionHash,
"deleted" => AbstractEnum::ITEM_NO_DELETED
]);
if(!$session || !$session->isValid()){
return $this->redirectToRoute('notFound');
}
$user = $session->getUser();
}
$info = $libraryRepository->getContentInfo($library, true);
if(!empty($info->url)){
$hasModule = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$drmPdf = $this->configuration->get("drm_pdf");
//check plan
if($hasModule && $drmPdf == LibraryEnum::YES){
if($extension == "pdf" && !empty($sessionHash) && !empty($lessonHash)){
$lessonId = $this->stringUtil->decodeHex($lessonHash, false, false);
$lesson = $this->em->getRepository(lesson::class)->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
if($lesson && $lesson->getControlShowDocument() == LessonEnum::YES){
$pdfService = $this->generalService->getService('PdfService');
$options = $libraryRepository->getDRMOptions(
$library->getTitle(),
$user
);
try{
$pdfService->showWithDRM($info->url, $options);
exit;
}catch(\Exception $e){
}
}
}
}
return $this->redirect($info->url, 301);
}
return $this->redirectToRoute('notFound');
}
/**
* @Route(
* path = "/library/thumb/{id}",
* methods = {"GET"},
* name = "getThumbLibrary"
* )
*/
public function getThumbLibrary(Request $request) {
$id = $request->get('id');
$libraryRepository = $this->em->getRepository(Library::class);
$library = $libraryRepository->findOneBy([
"id" => $id,
"deleted" => LibraryEnum::ITEM_NO_DELETED
]);
if(!$library){
throw new NotFoundException("Library not found");
}
$url = $libraryRepository->getCoverNew($library);
if(empty($url)){
throw new NotFoundException("Thumbnail link not found");
}
return $this->redirect($url, 301);
}
/**
* @Route(
* path = "/text/content/pdf/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
* methods = {"GET"},
* name = "getTextContentPDF"
* )
*/
public function getTextContentPDF(Request $request) {
$sessionHash = $request->get('sessionHash');
$lessonHash = $request->get('lessonHash');
$libraryRepository = $this->em->getRepository(Library::class);
$libraryId = $this->stringUtil->decodeHex($request->get('hash'), false, false);
$library = $libraryRepository->findOneBy([
"id" => $libraryId,
"deleted" => LibraryEnum::ITEM_NO_DELETED
]);
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonId = $this->stringUtil->decodeHex($lessonHash, false, false);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
$date = strtotime(
$this->stringUtil->decodeHex($request->get('timeHash'), false, false)
);
$now = strtotime(date('Y-m-d H:i:s'));
$diff = ($now - $date);
if($diff > 300){
return $this->redirectToRoute('notFound');
}
if(!empty($sessionHash)){
$sessionHash = $this->stringUtil->decodeHex($sessionHash, false, false);
$session = $this->em->getRepository(Session::class)->findOneBy([
"token" => $sessionHash,
"deleted" => AbstractEnum::ITEM_NO_DELETED
]);
if(!$session || !$session->isValid()){
return $this->redirectToRoute('notFound');
}
}
//check can see content
if($library->getType() == LibraryEnum::CONTENT_TEXT){
$hasModule = $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
$drmPdf = $this->configuration->get("drm_pdf");
//check plan
if($hasModule && $drmPdf == LibraryEnum::YES){
$pdfService = $this->generalService->getService('PdfService');
$info = $this->generalService->getServiceAccess(ServicesEnum::AWS_S3);
$clientConnection = $this->configuration->getClientConnection();
$serverUser = $clientConnection->getServerUser();
$imageUrl = "https:{$info->cdnLink}client/{$serverUser}";
$newText = str_replace('img src="', 'img src="' . $imageUrl, $library->getText());
$newText = str_replace('a href="', 'a href="' . $imageUrl, $newText);
$infoTag = $lessonRepository->setLessonTag(
$lesson,
$this->user,
$newText
);
$data = [
"title" => $library->getTitle(),
"text" => html_entity_decode($infoTag->content)
];
$pdfService->setFileName($library->getTitle());
$pdfService->setTemplateBody("library_text_content");
$pdfService->setData($data);
return $pdfService->generate();
}
}
return $this->redirectToRoute('notFound');
}
/**
* @Route(
* path = "/text/content/pdf/drm/{hash}/{timeHash}/{sessionHash}/{lessonHash}",
* methods = {"GET"},
* name = "getTextContentPDFDrm"
* )
*/
public function getTextContentPDFDrm(Request $request) {
$hash = $request->get('hash');
$timeHash = $request->get('timeHash');
$sessionHash = $request->get('sessionHash');
$lessonHash = $request->get('lessonHash');
$libraryRepository = $this->em->getRepository(Library::class);
$libraryId = $this->stringUtil->decodeHex($hash, false, false);
$library = $libraryRepository->findOneBy([
"id" => $libraryId,
"deleted" => LibraryEnum::ITEM_NO_DELETED
]);
$date = strtotime(
$this->stringUtil->decodeHex($timeHash, false, false)
);
$now = strtotime(date('Y-m-d H:i:s'));
$diff = ($now - $date);
if($diff > 300){
return $this->redirectToRoute('notFound');
}
$user = null;
if(!empty($sessionHash)){
$sessionToken = $this->stringUtil->decodeHex($sessionHash, false, false);
$session = $this->em->getRepository(Session::class)->findOneBy([
"token" => $sessionToken,
"deleted" => AbstractEnum::ITEM_NO_DELETED
]);
if(!$session || !$session->isValid()){
return $this->redirectToRoute('notFound');
}
$user = $session->getUser();
}
//check can see content
if($library->getType() == LibraryEnum::CONTENT_TEXT){
$url = "https://{$request->getHost()}/text/content/pdf/{$hash}/{$timeHash}/{$sessionHash}/{$lessonHash}";
$hasModule = $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
$drmPdf = $this->configuration->get("drm_pdf");
//check plan
if($hasModule && $drmPdf == LibraryEnum::YES){
$pdfService = $this->generalService->getService('PdfService');
$options = $libraryRepository->getDRMOptions(
$library->getTitle(),
$user
);
try{
$pdfService->showWithDRM($url, $options);
exit;
}catch(\Exception $e){
}
}
}
return $this->redirect($url, 301);
}
/**
* @Route(
* path = "/admin/preview/pdf/{libraryId}",
* methods = {"GET"},
* name = "getPreviewPDF"
* )
*/
public function getPreviewPDF(Request $request) {
$libraryId = $request->get('libraryId');
$libraryRepository = $this->em->getRepository(Library::class);
$library = $libraryRepository->findOneBy([
"id" => $libraryId,
"deleted" => LibraryEnum::ITEM_NO_DELETED
]);
if(!$library){
return $this->redirectToRoute('notFound');
}
//check can see content
$extension = $library->getFileExtension();
if($extension != "pdf"){
return $this->redirectToRoute('notFound');
}
$info = $libraryRepository->getContentInfo($library, true);
if(!empty($info->url)){
$hasModule = $this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction');
$drmPdf = $this->configuration->get("drm_pdf");
//check plan
if($hasModule && $drmPdf == LibraryEnum::YES){
$pdfService = $this->generalService->getService('PdfService');
$options = $libraryRepository->getDRMOptions(
$library->getTitle(),
$this->user
);
try{
$pdfService->showWithDRM($info->url, $options);
exit;
}catch(\Exception $e){
}
}
return $this->redirect($info->url, 301);
}
return $this->redirectToRoute('notFound');
}
/**
* @Route(
* path = "/admin/sample/pdf",
* methods = {"GET"},
* name = "getSamplePDF",
* )
*/
public function getSamplePDF(Request $request) {
$permission = $this->userPermissionUtil->getPermission(
"course",
"course_configuration",
"allow_config"
);
if($this->userPermissionUtil->isLow($permission)){
return $this->redirectToRoute('notFound');
}
//check plan
if(!$this->configuration->checkModuleIsAbleOnPlan('lessonControlFunction')){
return $this->redirectToRoute('notFound');
}
if(!$this->user){
return $this->redirectToRoute('notFound');
}
$drmPdf = $this->configuration->get("drm_pdf");
if($drmPdf == LibraryEnum::NO){
return $this->redirectToRoute('notFound');
}
$pdfService = $this->generalService->getService('PdfService');
$libraryRepository = $this->em->getRepository(Library::class);
$options = $libraryRepository->getDRMOptions(
"ead-sample",
$this->user
);
$url = "https://cdn.eadplataforma.app/assets/files/sample.pdf";
$pdfService->showWithDRM($url, $options);
exit;
}
/**
* @Route(
* path = "/upload/{fileName}",
* name = "fileGetUpload",
* methods = {"GET"},
* requirements = {"fileName"=".+"}
* )
*/
public function getFile(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$fileName = $request->get('fileName');
$stream = AbstractEnum::NO;//$request->get('stream');
$option = $request->get('option');
$fileNameOrigin = $fileName;
$extension = $this->fileService->getFileExtensionFromName($fileName);
if(!empty($option)){
$pathCrop = AbstractEnum::PATH_CROP;
$fileName = "{$pathCrop}/{$fileName}-{$option}.{$extension}";
}
$dir = dirname($fileName);
if($dir != "lesson"){
$this->fileService->setFile($fileName);
$url = $this->fileService->getFileUrlTemp(!empty($option) ? true : false);
if(empty($url) && !empty($option)){
$size = $this->fileService->getImageOptionSize($option);
$this->fileService->setFile($fileNameOrigin);
$url = $this->fileService->getFileUrlTemp(false);
$hash = rand() . md5(rand() . rand() . date('Y-m-d-H-i-s'));
$fileTempEx = "{$this->generalService->getPath()}/var/{$hash}.{$extension}";
$this->fileService->createTempFileFromUrl("https:{$url}", $fileTempEx);
if($extension != "gif"){
$this->fileService->resizeFile($fileTempEx, $size->width, $size->height);
}
$this->fileService->createFile($fileTempEx, $fileName);
$this->fileService->setFile($fileName);
unlink($fileTempEx);
$url = $this->fileService->getFileUrlTemp();
}
if($url){
if(!empty($stream)){
$response = new Response(file_get_contents($url));
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$this->fileService->getFileName()
);
// Set the content disposition
$mime = $this->fileService->getFileMimeTypeFromName($fileName);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', $mime);
return $response;
}
return $this->redirect($url, 301);
}
}
exit;
}
/**
* @Route(
* path = "/ead/player",
* name = "eadPlayer",
* methods = {"GET"},
* )
*/
public function getEadPlayer(Request $request) {
return $this->redirect("https://dev.vdocipher.com/playerAssets/1.6.10/vdo.js", 301);
}
/**
* @Route(
* path = "/ead/player/video/{id}",
* name = "eadPlayerVideo",
* methods = {"GET"},
* )
*/
public function getEadPlayerVideo(Request $request) {
$libraryRepository = $this->em->getRepository(Library::class);
$library = $libraryRepository->findOneBy([
"id" => $request->get('id'),
"deleted" => LibraryEnum::ITEM_NO_DELETED,
]);
$credentials = $libraryRepository->getVideoCredentials($library);
$link = "https://player.vdocipher.com/v2/?";
$data = [
"otp" => $credentials->otp,
"playbackInfo" => $credentials->playbackInfo,
"primaryColor" => $this->configuration->get('primary_color'),
];
$link = $link . http_build_query($data);
return $this->redirect($link, 301);
}
/**
* @Route(
* path = "/ead/player/new",
* name = "eadPlayerNew",
* methods = {"GET"},
* )
*/
public function getEadPlayerNew(Request $request) {
return $this->redirect("https://player.vdocipher.com/v2/api.js", 301);
}
/**
* @Route(
* path = "/ead/captcha",
* name = "eadCaptcha",
* methods = {"GET"},
* )
*/
public function getEadCaptcha(Request $request) {
$key = $request->get('key');
$data = $this->memcacheService->getData($key);
$captchaService = $this->generalService->getService('CaptchaService');
return $captchaService->generateCaptcha($data);
}
/**
* @Route(
* path = "/ead/captcha/reload/{key}",
* name = "eadCaptchaReload",
* methods = {"GET"},
* )
*/
public function getEadCaptchaReload(Request $request) {
$key = $request->get('key');
$this->memcacheService->deleteData($key);
$preKey = md5("captcha");
$newKey = $preKey."_".md5($this->client->getDomainPrimary().date('Y-m-d H:i:s').$request->getClientIp());
$data = $this->stringUtil->randomText(6);
$this->memcacheService->saveData($newKey, $data, 60*60*24);
return $this->eadResponse($newKey);
}
/**
* @Route(
* path = "admin/file/manager/image",
* name = "imgRegisterUpload",
* methods = {"POST"},
* )
*/
public function registerImage(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$file = $this->requestUtil->getFile('file');
$file = $this->fileService->setFile($file);
if($file){
$this->fileService->moveFile(AbstractEnum::PATH_OTHERS);
}
return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
}
/**
* @Route(
* path = "admin/file/manager/file",
* name = "fileRegisterUpload",
* methods = {"POST"},
* )
*/
public function registerFile(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$file = $this->requestUtil->getFile('file');
$file = $this->fileService->setFile($file);
if($file){
$this->fileService->moveFile(AbstractEnum::PATH_FILES);
}
return $this->json([ "link" => $this->fileService->getFileLocalPathName() ]);
}
/**
* @Route(
* path = "admin/file/manager/delete",
* name = "fileDeleteUpload",
* methods = {"DELETE"}
* )
*/
public function deleteFile(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$file = $this->requestUtil->getField('file');
if(!empty($file)){
$file = explode(AbstractEnum::PATH_UPLOAD, $file);
$file = end($file);
$file = urldecode($file);
$dir = dirname($file);
if($dir == AbstractEnum::PATH_FILES || $dir == AbstractEnum::PATH_OTHERS){
$this->fileService->setFile($file);
$this->fileService->deleteFile();
}
}
return $this->eadResponse([ "delete" => 1 ]);
}
}