src/App/Controller/StaticController.php line 293

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Parametre\Utilisateur;
  4. use App\Helper\StringHelper;
  5. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  10. /**
  11.  * StaticController.
  12.  *
  13.  * @author John Doe
  14.  * @package App\Controller
  15.  */
  16. class StaticController extends AbstractAppController {
  17.     /** @var int */
  18.     const CKE_UPLOAD_ADAPTER_ACTIVITY 1;
  19.     /** @var int */
  20.     const CKE_UPLOAD_ADAPTER_ACTUALITY_EXTRANET 5;
  21.     /** @var int */
  22.     const CKE_UPLOAD_ADAPTER_CAMPAGNE_MAILING 4;
  23.     /** @var int */
  24.     const CKE_UPLOAD_ADAPTER_CLUB 2;
  25.     /** @var int */
  26.     const CKE_UPLOAD_ADAPTER_DASHBOARD_COMPONENT 6;
  27.     /** @var int */
  28.     const CKE_UPLOAD_ADAPTER_FACTURATION 3;
  29.     /** @var string */
  30.     const STATIC_TOKEN_ENCRYPTION "1df5bd80-5128-4729-9243-ebcbd49a1e5f";
  31.     /**
  32.      * Render image.
  33.      *
  34.      * @param $file
  35.      * @return BinaryFileResponse
  36.      * @throws \Exception
  37.      */
  38.     public function activite($file) {
  39.         $image = \Kernel::getStoragePath() . "/association/activites/{$file}";
  40.         if (false === in_array(strtolower(pathinfo($filePATHINFO_EXTENSION)), ["png""jpg""jpeg""gif"]) || false === file_exists($image)) {
  41.             $image null;
  42.         }
  43.         if (null === $image) {
  44.             return $this->favicon();
  45.         }
  46.         return new BinaryFileResponse($image200);
  47.     }
  48.     /**
  49.      * Render image.
  50.      *
  51.      * @param $file
  52.      * @return BinaryFileResponse
  53.      * @throws \Exception
  54.      */
  55.     public function activiteInstance($file) {
  56.         $image = \Kernel::getStoragePath() . "/association/activites/instances/{$file}";
  57.         if (false === in_array(strtolower(pathinfo($filePATHINFO_EXTENSION)), ["png""jpg""jpeg""gif"]) || false === file_exists($image)) {
  58.             $image null;
  59.         }
  60.         if (null === $image) {
  61.             return $this->favicon();
  62.         }
  63.         return new BinaryFileResponse($image200);
  64.     }
  65.     /**
  66.      * Render user avatar.
  67.      *
  68.      * @param $file
  69.      * @return BinaryFileResponse
  70.      * @throws \Exception
  71.      */
  72.     public function avatar($file) {
  73.         $user   $this->getUser();
  74.         $avatar null;
  75.         if (null !== $user) {
  76.             $userAvatar = \Kernel::getStoragePath() . "/avatars/{$file}";
  77.             if (true === in_array(strtolower(pathinfo($filePATHINFO_EXTENSION)), ["png""jpg""jpeg""gif"]) && true === file_exists($userAvatar)) {
  78.                 $avatar $userAvatar;
  79.             }
  80.         }
  81.         if (null === $avatar) {
  82.             return $this->favicon();
  83.         }
  84.         return new BinaryFileResponse($avatar200);
  85.     }
  86.     /**
  87.      * Render user avatar.
  88.      *
  89.      * @param $uid
  90.      * @return BinaryFileResponse
  91.      * @throws \Exception
  92.      */
  93.     public function avatarUid($uid) {
  94.         // User was not connected.
  95.         if ($this->getUser() === null) {
  96.             return $this->favicon();
  97.         }
  98.         $user $this->em()->getRepository(Utilisateur::class)->findOneBy([
  99.             "id"         => $uid,
  100.             "platformId" => getenv("PLATFORM_ID"),
  101.         ]);
  102.         $avatar null;
  103.         if (null !== $user) {
  104.             $userAvatar = \Kernel::getStoragePath() . "/avatars/{$user->getAvatar()}";
  105.             if (true === in_array(strtolower(pathinfo($user->getAvatar(), PATHINFO_EXTENSION)), ["png""jpg""jpeg""gif"]) && true === file_exists($userAvatar)) {
  106.                 $avatar $userAvatar;
  107.             }
  108.         }
  109.         if (null === $avatar) {
  110.             return $this->favicon();
  111.         }
  112.         return new BinaryFileResponse($avatar200);
  113.     }
  114.     /**
  115.      * Render application background login.
  116.      */
  117.     public function bgLogin($identifier null) {
  118.         // If platform identifier was defined.
  119.         if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
  120.             $identifier StringHelper::decryptSalt($identifierself::STATIC_TOKEN_ENCRYPTION);
  121.             if (false !== $identifier) {
  122.                 putenv("PLATFORM_ID=" explode("."$identifier)[0]);
  123.                 putenv("PLATFORM_CODE=" explode("."$identifier)[1]);
  124.             }
  125.         }
  126.         $filePath = \Kernel::getRoot() . "/public/assets/images/default-bg.jpg";
  127.         if (getenv("PLATFORM_CODE") !== false) {
  128.             $pathImage $this->getAppParameter("LOGIN_BACKGROUND_IMAGE");
  129.             if (null !== $pathImage && true === file_exists(\Kernel::getStoragePath() . "/template/{$pathImage}")) {
  130.                 $filePath = \Kernel::getStoragePath() . "/template/{$pathImage}";
  131.             }
  132.         }
  133.         header("Content-type: application/png");
  134.         return new BinaryFileResponse($filePath);
  135.     }
  136.     /**
  137.      * @param Request $request
  138.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  139.      * @throws \Exception
  140.      */
  141.     public function ckeditorUpload(Request $request) {
  142.         if ($this->getUser() === null) {
  143.             return $this->json([
  144.                 "error" => "E01 - Cannot upload file.",
  145.             ]);
  146.         }
  147.         $type intval($request->request->get("type"));
  148.         if (false === array_key_exists($typeself::getCkEditorFolders())) {
  149.             return $this->json([
  150.                 "error" => "E02 - Cannot upload file.",
  151.             ]);
  152.         }
  153.         $folder self::getCkEditorFolders()[$type];
  154.         /** @var UploadedFile $file */
  155.         $file $request->files->get("upload");
  156.         if (null === $file) {
  157.             return $this->json([
  158.                 "error" => "E03 - Cannot upload file.",
  159.             ]);
  160.         }
  161.         $allowedExt = ["image/png""image/jpg""image/jpeg""image/gif""image/webp"];
  162.         if (false === in_array($file->getMimeType(), $allowedExt)) {
  163.             return $this->json([
  164.                 "error" => "Le format de fichier n'est pas autorisé.",
  165.             ]);
  166.         }
  167.         $finalPathFile uniqid() . "." $file->getClientOriginalExtension();
  168.         // Move file to datastore.
  169.         $file->move(
  170.             $folder,
  171.             $finalPathFile
  172.         );
  173.         return $this->json([
  174.             "url" => "/static/cke/{$type}/{$finalPathFile}",
  175.         ]);
  176.     }
  177.     /**
  178.      * @param $type
  179.      * @param $file
  180.      * @return BinaryFileResponse
  181.      * @throws \Exception
  182.      */
  183.     public function ckeditorView($type$file) {
  184.         $type intval($type);
  185.         if (false === array_key_exists($typeself::getCkEditorFolders())) {
  186.             throw new \Exception("BAD TYPE");
  187.         }
  188.         $folder self::getCkEditorFolders()[$type];
  189.         if (false === file_exists($folder "/" $file)) {
  190.             throw new \Exception("File not found");
  191.         }
  192.         return new BinaryFileResponse($folder "/" $file200);
  193.     }
  194.     /**
  195.      * @param Request $request
  196.      * @return \Symfony\Component\HttpFoundation\Response
  197.      * @throws \Exception
  198.      */
  199.     public function ckeditorWrite(Request $request) {
  200.         return $this->render("Layout/write-email.html.twig");
  201.     }
  202.     /**
  203.      * @param Request $request
  204.      * @return BinaryFileResponse
  205.      * @throws \Exception
  206.      */
  207.     public function downloadTmp(Request $request) {
  208.         $token $request->query->get("token");
  209.         if (null === $token) {
  210.             throw new \Exception("File token was not defined.");
  211.         }
  212.         try {
  213.             $decode StringHelper::decryptSalt(base64_decode($token), self::STATIC_TOKEN_ENCRYPTION);
  214.         } catch (\Exception $e) {
  215.             throw new \Exception("BAD TOKEN");
  216.         }
  217.         $filePath = \Kernel::getStoragePath() . "/tmp/{$decode}";
  218.         if (false === file_exists($filePath)) {
  219.             exit("Le fichier n'est plus disponible.");
  220.         }
  221.         $response = new BinaryFileResponse($filePath);
  222.         if ($request->query->has("force")) {
  223.             $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  224.         }
  225.         return $response;
  226.     }
  227.     /**
  228.      * Render application favicon.
  229.      */
  230.     public function favicon() {
  231.         $filePath = \Kernel::getRoot() . "/public/assets/images/favicon.png";
  232.         if (getenv("PLATFORM_CODE") !== false) {
  233.             $favicon $this->getAppParameter("APP_FAVICON");
  234.             if (null !== $favicon && true === file_exists(\Kernel::getStoragePath() . "/template/{$favicon}")) {
  235.                 $filePath = \Kernel::getStoragePath() . "/template/{$favicon}";
  236.             }
  237.         }
  238.         header("Content-type: application/png");
  239.         return new BinaryFileResponse($filePath);
  240.     }
  241.     /**
  242.      * @return string[]
  243.      * @throws \Exception
  244.      */
  245.     public static function getCkEditorFolders() {
  246.         return [
  247.             self::CKE_UPLOAD_ADAPTER_ACTIVITY            => \Kernel::getStoragePath() . "/ckeditor/association/activites",
  248.             self::CKE_UPLOAD_ADAPTER_CLUB                => \Kernel::getStoragePath() . "/ckeditor/association/clubs",
  249.             self::CKE_UPLOAD_ADAPTER_FACTURATION         => \Kernel::getStoragePath() . "/ckeditor/association/facturation",
  250.             self::CKE_UPLOAD_ADAPTER_CAMPAGNE_MAILING    => \Kernel::getStoragePath() . "/ckeditor/communication/campagne-mailing",
  251.             self::CKE_UPLOAD_ADAPTER_ACTUALITY_EXTRANET  => \Kernel::getStoragePath() . "/ckeditor/association/actualites-extranet",
  252.             self::CKE_UPLOAD_ADAPTER_DASHBOARD_COMPONENT => \Kernel::getStoragePath() . "/ckeditor/dashboard/components",
  253.         ];
  254.     }
  255.     /**
  256.      * Render application logo.
  257.      */
  258.     public function logo($identifier null) {
  259.         // If platform identifier was defined.
  260.         if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
  261.             $identifier StringHelper::decryptSalt($identifierself::STATIC_TOKEN_ENCRYPTION);
  262.             if (false !== $identifier) {
  263.                 putenv("PLATFORM_ID=" explode("."$identifier)[0]);
  264.                 putenv("PLATFORM_CODE=" explode("."$identifier)[1]);
  265.             }
  266.         }
  267.         $filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
  268.         if (getenv("PLATFORM_CODE") !== false) {
  269.             $logo $this->getAppParameter("APP_LOGO");
  270.             if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/template/{$logo}")) {
  271.                 $filePath = \Kernel::getStoragePath() . "/template/{$logo}";
  272.             }
  273.         }
  274.         header("Content-type: application/png");
  275.         return new BinaryFileResponse($filePath);
  276.     }
  277.     /**
  278.      * Render application logo carte adherent.
  279.      */
  280.     public function logoCarteAdherent($identifier null) {
  281.         // If platform identifier was defined.
  282.         if (getenv("PLATFORM_CODE") === false && null !== $identifier) {
  283.             $identifier StringHelper::decryptSalt($identifierself::STATIC_TOKEN_ENCRYPTION);
  284.             if (false !== $identifier) {
  285.                 putenv("PLATFORM_ID=" explode("."$identifier)[0]);
  286.                 putenv("PLATFORM_CODE=" explode("."$identifier)[1]);
  287.             }
  288.         }
  289.         $filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
  290.         if (getenv("PLATFORM_CODE") !== false) {
  291.             $logo $this->getAppParameter("ASSOCIATION_CARTE_ADHERENT_LOGO");
  292.             if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/association/general/{$logo}")) {
  293.                 $filePath = \Kernel::getStoragePath() . "/association/general/{$logo}";
  294.             }
  295.         }
  296.         header("Content-type: application/png");
  297.         return new BinaryFileResponse($filePath);
  298.     }
  299.     /**
  300.      * @return \Symfony\Component\HttpFoundation\Response
  301.      */
  302.     public function pdfjsViewer(Request $request) {
  303.         return $this->render("Layout/pdfjs.html.twig", [
  304.             "fileUri" => $request->query->get("file"),
  305.         ]);
  306.     }
  307.     /**
  308.      * @param $path
  309.      * @return BinaryFileResponse|Response
  310.      * @throws \Exception
  311.      */
  312.     public function tmpDownload($path) {
  313.         $filePath = \Kernel::getStoragePath() . "/tmp/" $path;
  314.         if (!file_exists($filePath)) {
  315.             return new Response("Ce fichier n'est plus disponible.");
  316.         }
  317.         $response = new BinaryFileResponse($filePath);
  318.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  319.         $response->deleteFileAfterSend(true);
  320.         return $response;
  321.     }
  322. }