app/Plugin/TabaHtmlEditor2/Controller/AdminController.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) SPREAD WORKS Inc. All Rights Reserved.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Plugin\TabaHtmlEditor2\Controller;
  9. use Eccube\Controller\AbstractController;
  10. use Plugin\TabaHtmlEditor2\Common\Constants;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  15. /**
  16.  * 管理画面用コントローラー
  17.  *
  18.  * @Route(Plugin\TabaHtmlEditor2\Common\Constants::ADMIN_URI_PREFIX,name=Plugin\TabaHtmlEditor2\Common\Constants::ADMIN_BIND_PREFIX)
  19.  */
  20. class AdminController extends AbstractController
  21. {
  22.     /**
  23.      * コンストラクタ
  24.      */
  25.     public function __construct()
  26.     {
  27.     }
  28.     /**
  29.      * 各種ファイルを出力します。
  30.      *
  31.      * @param Request $request
  32.      * @param string $file
  33.      * @throws NotFoundHttpException
  34.      * @return BinaryFileResponse
  35.      *
  36.      * @Route("/assets/{file}",name="_assets",requirements={"file"="[a-zA-Z0-9-_/\s.]+"})
  37.      */
  38.     public function assets(Request $request,$file) {
  39.         if ($this->container->has('profiler')) $this->container->get('profiler')->disable();
  40.         if (strpos($file,'..')) {
  41.             log_critical("ディレクトリトラバーサル攻撃の可能性があります。 [FILE] " $file);
  42.             throw new NotFoundHttpException();
  43.         }
  44.         $file Constants::TEMPLATE_PATH DIRECTORY_SEPARATOR .  "admin" DIRECTORY_SEPARATOR "assets" .  DIRECTORY_SEPARATOR $file;
  45.         if (file_exists($this->eccubeConfig['plugin_realdir'] . DIRECTORY_SEPARATOR $file)) {
  46.             log_debug("[ASSETS] [FILE] " $file);
  47.             // 拡張子によりMIMEを設定します。
  48.             $suffixes explode(".",$file);
  49.             $suffix end($suffixes);
  50.             $suffix_def = array(
  51.                 "jpeg" => "image/jpg",
  52.                 "jpg" => "image/jpg",
  53.                 "gif" => "image/gif",
  54.                 "png" => "image/png",
  55.                 "svg" => "image/svg+xml",
  56.                 "js" => "application/x-javascript",
  57.                 "css" => "text/css",
  58.                 "html" => "text/html",
  59.                 "map" => "application/json",
  60.             );
  61.             if (in_array($suffix,array_keys($suffix_def))) {
  62.                 $fileObject = new \SplFileInfo($this->eccubeConfig['plugin_realdir'] . DIRECTORY_SEPARATOR $file);
  63.                 $response = new BinaryFileResponse($fileObject);
  64.                 $response->headers->set('Content-Type',$suffix_def[$suffix]);
  65.                 // キャッシュするヘッダーを出力する設定をします
  66.                 if ($this->container->has(Constants::CONTAINER_KEY_NAME)) {
  67.                     $this->container->get(Constants::CONTAINER_KEY_NAME)->set(Constants::HTTP_CACHE_STATUS,true);
  68.                 }
  69.                 return $response;
  70.             } else {
  71.                 throw new NotFoundHttpException();
  72.             }
  73.         } else {
  74.             throw new NotFoundHttpException();
  75.         }
  76.     }
  77. }