<?php
namespace App\Controller;
use App\Entity\Parametre\Utilisateur;
use App\Helper\StringHelper;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* StaticController.
*
* @author John Doe
* @package App\Controller
*/
class StaticController extends AbstractAppController {
/** @var int */
const CKE_UPLOAD_ADAPTER_ACTIVITY = 1;
/** @var int */
const CKE_UPLOAD_ADAPTER_ACTUALITY_EXTRANET = 5;
/** @var int */
const CKE_UPLOAD_ADAPTER_CAMPAGNE_MAILING = 4;
/** @var int */
const CKE_UPLOAD_ADAPTER_CLUB = 2;
/** @var int */
const CKE_UPLOAD_ADAPTER_DASHBOARD_COMPONENT = 6;
/** @var int */
const CKE_UPLOAD_ADAPTER_FACTURATION = 3;
/** @var string */
const STATIC_TOKEN_ENCRYPTION = "1df5bd80-5128-4729-9243-ebcbd49a1e5f";
/**
* Render image.
*
* @param $file
* @return BinaryFileResponse
* @throws \Exception
*/
public function activite($file) {
$image = \Kernel::getStoragePath() . "/association/activites/{$file}";
if (false === in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ["png", "jpg", "jpeg", "gif"]) || false === file_exists($image)) {
$image = null;
}
if (null === $image) {
return $this->favicon();
}
return new BinaryFileResponse($image, 200);
}
/**
* Render image.
*
* @param $file
* @return BinaryFileResponse
* @throws \Exception
*/
public function activiteInstance($file) {
$image = \Kernel::getStoragePath() . "/association/activites/instances/{$file}";
if (false === in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ["png", "jpg", "jpeg", "gif"]) || false === file_exists($image)) {
$image = null;
}
if (null === $image) {
return $this->favicon();
}
return new BinaryFileResponse($image, 200);
}
/**
* Render user avatar.
*
* @param $file
* @return BinaryFileResponse
* @throws \Exception
*/
public function avatar($file) {
$user = $this->getUser();
$avatar = null;
if (null !== $user) {
$userAvatar = \Kernel::getStoragePath() . "/avatars/{$file}";
if (true === in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ["png", "jpg", "jpeg", "gif"]) && true === file_exists($userAvatar)) {
$avatar = $userAvatar;
}
}
if (null === $avatar) {
return $this->favicon();
}
return new BinaryFileResponse($avatar, 200);
}
/**
* Render user avatar.
*
* @param $uid
* @return BinaryFileResponse
* @throws \Exception
*/
public function avatarUid($uid) {
// User was not connected.
if ($this->getUser() === null) {
return $this->favicon();
}
$user = $this->em()->getRepository(Utilisateur::class)->findOneBy([
"id" => $uid,
"platformId" => getenv("PLATFORM_ID"),
]);
$avatar = null;
if (null !== $user) {
$userAvatar = \Kernel::getStoragePath() . "/avatars/{$user->getAvatar()}";
if (true === in_array(strtolower(pathinfo($user->getAvatar(), PATHINFO_EXTENSION)), ["png", "jpg", "jpeg", "gif"]) && true === file_exists($userAvatar)) {
$avatar = $userAvatar;
}
}
if (null === $avatar) {
return $this->favicon();
}
return new BinaryFileResponse($avatar, 200);
}
/**
* Render application background login.
*/
public function bgLogin($identifier = null) {
// If platform identifier was defined.
if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
$identifier = StringHelper::decryptSalt($identifier, self::STATIC_TOKEN_ENCRYPTION);
if (false !== $identifier) {
putenv("PLATFORM_ID=" . explode(".", $identifier)[0]);
putenv("PLATFORM_CODE=" . explode(".", $identifier)[1]);
}
}
$filePath = \Kernel::getRoot() . "/public/assets/images/default-bg.jpg";
if (getenv("PLATFORM_CODE") !== false) {
$pathImage = $this->getAppParameter("LOGIN_BACKGROUND_IMAGE");
if (null !== $pathImage && true === file_exists(\Kernel::getStoragePath() . "/template/{$pathImage}")) {
$filePath = \Kernel::getStoragePath() . "/template/{$pathImage}";
}
}
header("Content-type: application/png");
return new BinaryFileResponse($filePath);
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Exception
*/
public function ckeditorUpload(Request $request) {
if ($this->getUser() === null) {
return $this->json([
"error" => "E01 - Cannot upload file.",
]);
}
$type = intval($request->request->get("type"));
if (false === array_key_exists($type, self::getCkEditorFolders())) {
return $this->json([
"error" => "E02 - Cannot upload file.",
]);
}
$folder = self::getCkEditorFolders()[$type];
/** @var UploadedFile $file */
$file = $request->files->get("upload");
if (null === $file) {
return $this->json([
"error" => "E03 - Cannot upload file.",
]);
}
$allowedExt = ["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp"];
if (false === in_array($file->getMimeType(), $allowedExt)) {
return $this->json([
"error" => "Le format de fichier n'est pas autorisé.",
]);
}
$finalPathFile = uniqid() . "." . $file->getClientOriginalExtension();
// Move file to datastore.
$file->move(
$folder,
$finalPathFile
);
return $this->json([
"url" => "/static/cke/{$type}/{$finalPathFile}",
]);
}
/**
* @param $type
* @param $file
* @return BinaryFileResponse
* @throws \Exception
*/
public function ckeditorView($type, $file) {
$type = intval($type);
if (false === array_key_exists($type, self::getCkEditorFolders())) {
throw new \Exception("BAD TYPE");
}
$folder = self::getCkEditorFolders()[$type];
if (false === file_exists($folder . "/" . $file)) {
throw new \Exception("File not found");
}
return new BinaryFileResponse($folder . "/" . $file, 200);
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
*/
public function ckeditorWrite(Request $request) {
return $this->render("Layout/write-email.html.twig");
}
/**
* @param Request $request
* @return BinaryFileResponse
* @throws \Exception
*/
public function downloadTmp(Request $request) {
$token = $request->query->get("token");
if (null === $token) {
throw new \Exception("File token was not defined.");
}
try {
$decode = StringHelper::decryptSalt(base64_decode($token), self::STATIC_TOKEN_ENCRYPTION);
} catch (\Exception $e) {
throw new \Exception("BAD TOKEN");
}
$filePath = \Kernel::getStoragePath() . "/tmp/{$decode}";
if (false === file_exists($filePath)) {
exit("Le fichier n'est plus disponible.");
}
$response = new BinaryFileResponse($filePath);
if ($request->query->has("force")) {
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
}
return $response;
}
/**
* Render application favicon.
*/
public function favicon() {
$filePath = \Kernel::getRoot() . "/public/assets/images/favicon.png";
if (getenv("PLATFORM_CODE") !== false) {
$favicon = $this->getAppParameter("APP_FAVICON");
if (null !== $favicon && true === file_exists(\Kernel::getStoragePath() . "/template/{$favicon}")) {
$filePath = \Kernel::getStoragePath() . "/template/{$favicon}";
}
}
header("Content-type: application/png");
return new BinaryFileResponse($filePath);
}
/**
* @return string[]
* @throws \Exception
*/
public static function getCkEditorFolders() {
return [
self::CKE_UPLOAD_ADAPTER_ACTIVITY => \Kernel::getStoragePath() . "/ckeditor/association/activites",
self::CKE_UPLOAD_ADAPTER_CLUB => \Kernel::getStoragePath() . "/ckeditor/association/clubs",
self::CKE_UPLOAD_ADAPTER_FACTURATION => \Kernel::getStoragePath() . "/ckeditor/association/facturation",
self::CKE_UPLOAD_ADAPTER_CAMPAGNE_MAILING => \Kernel::getStoragePath() . "/ckeditor/communication/campagne-mailing",
self::CKE_UPLOAD_ADAPTER_ACTUALITY_EXTRANET => \Kernel::getStoragePath() . "/ckeditor/association/actualites-extranet",
self::CKE_UPLOAD_ADAPTER_DASHBOARD_COMPONENT => \Kernel::getStoragePath() . "/ckeditor/dashboard/components",
];
}
/**
* Render application logo.
*/
public function logo($identifier = null) {
// If platform identifier was defined.
if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
$identifier = StringHelper::decryptSalt($identifier, self::STATIC_TOKEN_ENCRYPTION);
if (false !== $identifier) {
putenv("PLATFORM_ID=" . explode(".", $identifier)[0]);
putenv("PLATFORM_CODE=" . explode(".", $identifier)[1]);
}
}
$filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
if (getenv("PLATFORM_CODE") !== false) {
$logo = $this->getAppParameter("APP_LOGO");
if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/template/{$logo}")) {
$filePath = \Kernel::getStoragePath() . "/template/{$logo}";
}
}
header("Content-type: application/png");
return new BinaryFileResponse($filePath);
}
/**
* Render application logo carte adherent.
*/
public function logoCarteAdherent($identifier = null) {
// If platform identifier was defined.
if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
$identifier = StringHelper::decryptSalt($identifier, self::STATIC_TOKEN_ENCRYPTION);
if (false !== $identifier) {
putenv("PLATFORM_ID=" . explode(".", $identifier)[0]);
putenv("PLATFORM_CODE=" . explode(".", $identifier)[1]);
}
}
$filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
if (getenv("PLATFORM_CODE") !== false) {
$logo = $this->getAppParameter("ASSOCIATION_CARTE_ADHERENT_LOGO");
if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/association/general/{$logo}")) {
$filePath = \Kernel::getStoragePath() . "/association/general/{$logo}";
}
}
header("Content-type: application/png");
return new BinaryFileResponse($filePath);
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function pdfjsViewer(Request $request) {
return $this->render("Layout/pdfjs.html.twig", [
"fileUri" => $request->query->get("file"),
]);
}
/**
* @param $path
* @return BinaryFileResponse|Response
* @throws \Exception
*/
public function tmpDownload($path) {
$filePath = \Kernel::getStoragePath() . "/tmp/" . $path;
if (!file_exists($filePath)) {
return new Response("Ce fichier n'est plus disponible.");
}
$response = new BinaryFileResponse($filePath);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
$response->deleteFileAfterSend(true);
return $response;
}
}