vendor/artgris/filemanager-bundle/Helpers/FileManager.php line 49

Open in your IDE?
  1. <?php
  2. namespace Artgris\Bundle\FileManagerBundle\Helpers;
  3. use Artgris\Bundle\FileManagerBundle\Event\FileManagerEvents;
  4. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. use Symfony\Component\Routing\RouterInterface;
  10. /**
  11.  * @author Arthur Gribet <a.gribet@gmail.com>
  12.  */
  13. class FileManager
  14. {
  15.     const VIEW_THUMBNAIL 'thumbnail';
  16.     const VIEW_LIST 'list';
  17.     private $queryParameters;
  18.     private $kernelRoute;
  19.     private $router;
  20.     private $configuration;
  21.     private $webDir;
  22.     /**
  23.      * @var EventDispatcherInterface
  24.      */
  25.     private $dispatcher;
  26.     /**
  27.      * FileManager constructor.
  28.      *
  29.      * @param $queryParameters
  30.      * @param $configuration
  31.      * @param $kernelRoute
  32.      * @param $webDir
  33.      *
  34.      * @internal param $basePath
  35.      */
  36.     public function __construct($queryParameters$configuration$kernelRouteRouterInterface $routerEventDispatcherInterface $dispatcher$webDir)
  37.     {
  38.         $this->queryParameters $queryParameters;
  39.         $this->configuration $configuration;
  40.         $this->kernelRoute $kernelRoute;
  41.         $this->router $router;
  42.         $this->dispatcher $dispatcher;
  43.         // Check Security
  44.         $this->checkSecurity();
  45.         $this->webDir $webDir;
  46.     }
  47.     public function getDirName()
  48.     {
  49.         return \dirname($this->getBasePath());
  50.     }
  51.     public function getBaseName()
  52.     {
  53.         return basename($this->getBasePath());
  54.     }
  55.     public function getRegex()
  56.     {
  57.         if (isset($this->configuration['regex'])) {
  58.             return '/' $this->configuration['regex'] . '/i';
  59.         }
  60.         switch ($this->getType()) {
  61.             case 'media':
  62.                 return '/\.(mp4|ogg|webm)$/i';
  63.                 break;
  64.             case 'image':
  65.                 return '/\.(gif|png|jpe?g|svg)$/i';
  66.             case 'file':
  67.             default:
  68.                 return '/.+$/i';
  69.         }
  70.     }
  71.     public function getCurrentRoute()
  72.     {
  73.         return urldecode($this->getRoute());
  74.     }
  75.     public function getCurrentPath()
  76.     {
  77.         return realpath($this->getBasePath() . $this->getCurrentRoute());
  78.     }
  79.     // parent url
  80.     public function getParent()
  81.     {
  82.         $queryParentParameters $this->queryParameters;
  83.         $parentRoute \dirname($this->getCurrentRoute());
  84.         if (\DIRECTORY_SEPARATOR !== $parentRoute) {
  85.             $queryParentParameters['route'] = \dirname($this->getCurrentRoute());
  86.         } else {
  87.             unset($queryParentParameters['route']);
  88.         }
  89.         $parentRoute $this->router->generate('file_manager'$queryParentParameters);
  90.         return $this->getRoute() ? $parentRoute null;
  91.     }
  92.     public function getImagePath()
  93.     {
  94.         $baseUrl $this->getBaseUrl();
  95.         if ($baseUrl) {
  96.             return $baseUrl $this->getCurrentRoute() . '/';
  97.         }
  98.         return false;
  99.     }
  100.     private function getBaseUrl()
  101.     {
  102.         $webPath '../' $this->webDir;
  103.         $dirl = new \SplFileInfo($this->getConfiguration()['dir']);
  104.         $base $dirl->getPathname();
  105.         if (=== mb_strpos($base$webPath)) {
  106.             return mb_substr($basemb_strlen($webPath));
  107.         }
  108.         return false;
  109.     }
  110.     private function checkSecurity()
  111.     {
  112.         if (!isset($this->configuration['dir'])) {
  113.             throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR'Please define a "dir" parameter in your config.yml');
  114.         }
  115.         $dir $this->configuration['dir'];
  116.         $fileSystem = new Filesystem();
  117.         $exist $fileSystem->exists($dir);
  118.         if (false === $exist) {
  119.             throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR'Directory does not exist.');
  120.         }
  121.         $currentPath $this->getCurrentPath();
  122.         // check Path security
  123.         if (false === $currentPath || !== mb_strpos($currentPath$this->getBasePath())) {
  124.             throw new HttpException(Response::HTTP_UNAUTHORIZED'You are not allowed to access this folder.');
  125.         }
  126.         $event = new GenericEvent($this, ['path' => $currentPath]);
  127.         $this->dispatcher->dispatch($eventFileManagerEvents::POST_CHECK_SECURITY);
  128.     }
  129.     public function getModule()
  130.     {
  131.         return isset($this->getQueryParameters()['module']) ? $this->getQueryParameters()['module'] : null;
  132.     }
  133.     public function getType()
  134.     {
  135.         return $this->mergeConfAndQuery('type');
  136.     }
  137.     /**
  138.      * @param null $type
  139.      */
  140.     public function setType($type)
  141.     {
  142.         $this->type $type;
  143.     }
  144.     public function getRoute()
  145.     {
  146.         return isset($this->getQueryParameters()['route']) && '/' !== $this->getQueryParameters()['route'] ? $this->getQueryParameters()['route'] : null;
  147.     }
  148.     /**
  149.      * @return bool|string
  150.      */
  151.     public function getBasePath()
  152.     {
  153.         return realpath($this->getConfiguration()['dir']);
  154.     }
  155.     /**
  156.      * @return mixed
  157.      */
  158.     public function getQueryParameters()
  159.     {
  160.         return $this->queryParameters;
  161.     }
  162.     /**
  163.      * @param mixed $queryParameters
  164.      */
  165.     public function setQueryParameters($queryParameters)
  166.     {
  167.         $this->queryParameters $queryParameters;
  168.     }
  169.     /**
  170.      * @return mixed
  171.      */
  172.     public function getKernelRoute()
  173.     {
  174.         return $this->kernelRoute;
  175.     }
  176.     /**
  177.      * @param mixed $kernelRoute
  178.      */
  179.     public function setKernelRoute($kernelRoute)
  180.     {
  181.         $this->kernelRoute $kernelRoute;
  182.     }
  183.     /**
  184.      * @return RouterInterface
  185.      */
  186.     public function getRouter()
  187.     {
  188.         return $this->router;
  189.     }
  190.     public function setRouter(RouterInterface $router)
  191.     {
  192.         $this->router $router;
  193.     }
  194.     /**
  195.      * @return mixed
  196.      */
  197.     public function getConfiguration()
  198.     {
  199.         return $this->configuration;
  200.     }
  201.     /**
  202.      * @param mixed $configuration
  203.      */
  204.     public function setConfiguration($configuration)
  205.     {
  206.         $this->configuration $configuration;
  207.     }
  208.     public function getTree()
  209.     {
  210.         return $this->mergeQueryAndConf('tree'true);
  211.     }
  212.     public function getView()
  213.     {
  214.         return $this->mergeQueryAndConf('view''list');
  215.     }
  216.     public function getQueryParameter($parameter)
  217.     {
  218.         return isset($this->getQueryParameters()[$parameter]) ? $this->getQueryParameters()[$parameter] : null;
  219.     }
  220.     public function getConfigurationParameter($parameter)
  221.     {
  222.         return isset($this->getConfiguration()[$parameter]) ? $this->getConfiguration()[$parameter] : null;
  223.     }
  224.     private function mergeQueryAndConf($parameter$default null)
  225.     {
  226.         if (null !== $this->getQueryParameter($parameter)) {
  227.             return $this->getQueryParameter($parameter);
  228.         }
  229.         if (null !== $this->getConfigurationParameter($parameter)) {
  230.             return $this->getConfigurationParameter($parameter);
  231.         }
  232.         return $default;
  233.     }
  234.     private function mergeConfAndQuery($parameter$default null)
  235.     {
  236.         if (null !== $this->getConfigurationParameter($parameter)) {
  237.             return $this->getConfigurationParameter($parameter);
  238.         }
  239.         if (null !== $this->getQueryParameter($parameter)) {
  240.             return $this->getQueryParameter($parameter);
  241.         }
  242.         return $default;
  243.     }
  244. }