vendor/pimcore/pimcore/bundles/CoreBundle/Controller/PublicServicesController.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\Controller;
  15. use Pimcore\Config;
  16. use Pimcore\Controller\Controller;
  17. use Pimcore\File;
  18. use Pimcore\Logger;
  19. use Pimcore\Model\Asset;
  20. use Pimcore\Model\Site;
  21. use Pimcore\Model\Tool;
  22. use Pimcore\Model\Tool\TmpStore;
  23. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  24. use Symfony\Component\HttpFoundation\Cookie;
  25. use Symfony\Component\HttpFoundation\RedirectResponse;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  29. use Symfony\Component\HttpKernel\EventListener\SessionListener;
  30. class PublicServicesController extends Controller
  31. {
  32.     /**
  33.      * @param Request $request
  34.      * @param SessionListener $sessionListener
  35.      *
  36.      * @return BinaryFileResponse
  37.      */
  38.     public function thumbnailAction(Request $requestSessionListener $sessionListener)
  39.     {
  40.         $errorImage PIMCORE_WEB_ROOT '/bundles/pimcoreadmin/img/filetype-not-supported.svg';
  41.         $assetId $request->get('assetId');
  42.         $thumbnailName $request->get('thumbnailName');
  43.         $filename $request->get('filename');
  44.         $requestedFileExtension strtolower(File::getFileExtension($filename));
  45.         $asset Asset::getById($assetId);
  46.         $prefix preg_replace('@^cache-buster\-[\d]+\/@'''$request->get('prefix'));
  47.         if ($asset && $asset->getPath() == ('/' $prefix)) {
  48.             // we need to check the path as well, this is important in the case you have restricted the public access to
  49.             // assets via rewrite rules
  50.             try {
  51.                 $imageThumbnail null;
  52.                 $thumbnailFile null;
  53.                 $thumbnailConfig null;
  54.                 // just check if the thumbnail exists -> throws exception otherwise
  55.                 $thumbnailConfig Asset\Image\Thumbnail\Config::getByName($thumbnailName);
  56.                 if (!$thumbnailConfig) {
  57.                     // check if there's an item in the TmpStore
  58.                     // remove an eventually existing cache-buster prefix first (eg. when using with a CDN)
  59.                     $pathInfo preg_replace('@^/cache-buster\-[\d]+@'''$request->getPathInfo());
  60.                     $deferredConfigId 'thumb_' $assetId '__' md5(urldecode($pathInfo));
  61.                     if ($thumbnailConfigItem TmpStore::get($deferredConfigId)) {
  62.                         $thumbnailConfig $thumbnailConfigItem->getData();
  63.                         TmpStore::delete($deferredConfigId);
  64.                         if (!$thumbnailConfig instanceof Asset\Image\Thumbnail\Config) {
  65.                             throw new \Exception("Deferred thumbnail config file doesn't contain a valid \\Asset\\Image\\Thumbnail\\Config object");
  66.                         }
  67.                     }
  68.                 }
  69.                 if (!$thumbnailConfig) {
  70.                     throw $this->createNotFoundException("Thumbnail '" $thumbnailName "' file doesn't exist");
  71.                 }
  72.                 if (strcasecmp($thumbnailConfig->getFormat(), 'SOURCE') === 0) {
  73.                     $formatOverride $requestedFileExtension;
  74.                     if (in_array($requestedFileExtension, ['jpg''jpeg'])) {
  75.                         $formatOverride 'pjpeg';
  76.                     }
  77.                     $thumbnailConfig->setFormat($formatOverride);
  78.                 }
  79.                 if ($asset instanceof Asset\Video) {
  80.                     $time 1;
  81.                     if (preg_match("|~\-~time\-(\d+)\.|"$filename$matchesThumbs)) {
  82.                         $time = (int)$matchesThumbs[1];
  83.                     }
  84.                     $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$time);
  85.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  86.                 } elseif ($asset instanceof Asset\Document) {
  87.                     $page 1;
  88.                     if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  89.                         $page = (int)$matchesThumbs[1];
  90.                     }
  91.                     $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  92.                     $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  93.                     $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$page);
  94.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  95.                 } elseif ($asset instanceof Asset\Image) {
  96.                     //check if high res image is called
  97.                     preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  98.                     if (array_key_exists(2$matches) && $matches[2]) {
  99.                         $highResFactor = (float) str_replace(['@''x'], ''$matches[2]);
  100.                         $thumbnailConfig->setHighResolution($highResFactor);
  101.                     }
  102.                     // check if a media query thumbnail was requested
  103.                     if (preg_match("#~\-~media\-\-(.*)\-\-query#"$matches[1], $mediaQueryResult)) {
  104.                         $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  105.                     }
  106.                     $imageThumbnail $asset->getThumbnail($thumbnailConfig);
  107.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  108.                 }
  109.                 if ($imageThumbnail && $thumbnailFile && file_exists($thumbnailFile)) {
  110.                     $actualFileExtension File::getFileExtension($thumbnailFile);
  111.                     if ($actualFileExtension !== $requestedFileExtension && $thumbnailFile != $errorImage) {
  112.                         // create a copy/symlink to the file with the original file extension
  113.                         // this can be e.g. the case when the thumbnail is called as foo.png but the thumbnail config
  114.                         // is set to auto-optimized format so the resulting thumbnail can be jpeg
  115.                         $requestedFile preg_replace('/\.' $actualFileExtension '$/''.' $requestedFileExtension$thumbnailFile);
  116.                         $linked is_link($requestedFile) || symlink($thumbnailFile$requestedFile);
  117.                         if (false === $linked) {
  118.                             // create a hard copy
  119.                             copy($thumbnailFile$requestedFile);
  120.                         }
  121.                     }
  122.                     // set appropriate caching headers
  123.                     // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  124.                     $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  125.                     $headers = [
  126.                         'Cache-Control' => 'public, max-age=' $lifetime,
  127.                         'Expires' => date('D, d M Y H:i:s T'time() + $lifetime),
  128.                         'Content-Type' => $imageThumbnail->getMimeType(),
  129.                     ];
  130.                     // in certain cases where an event listener starts a session (e.g. when there's a firewall
  131.                     // configured for the entire site /*) the session event listener shouldn't modify the
  132.                     // cache control headers of this response
  133.                     if (defined('Symfony\Component\HttpKernel\EventListener\AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER')) {
  134.                         // this method of bypassing the session listener was introduced in Symfony 4, so we need
  135.                         // to check for the constant first
  136.                         $headers[AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER] = true;
  137.                     } else {
  138.                         // @TODO to be removed in Pimcore 10
  139.                         // Symfony 3.4 doesn't support bypassing the session listener, so we just remove it
  140.                         \Pimcore::getEventDispatcher()->removeSubscriber($sessionListener);
  141.                     }
  142.                     return new BinaryFileResponse($thumbnailFile200$headers);
  143.                 }
  144.             } catch (\Exception $e) {
  145.                 $message "Thumbnail with name '" $thumbnailName "' doesn't exist";
  146.                 Logger::error($message);
  147.                 throw $this->createNotFoundException($message$e);
  148.             }
  149.         } else {
  150.             throw $this->createNotFoundException('Asset not found');
  151.         }
  152.         throw $this->createNotFoundException('Unable to create image thumbnail');
  153.     }
  154.     /**
  155.      * @param Request $request
  156.      *
  157.      * @return Response
  158.      */
  159.     public function robotsTxtAction(Request $request)
  160.     {
  161.         // check for site
  162.         $domain = \Pimcore\Tool::getHostname();
  163.         $site Site::getByDomain($domain);
  164.         $config Config::getRobotsConfig()->toArray();
  165.         $siteId 'default';
  166.         if ($site instanceof Site) {
  167.             $siteId $site->getId();
  168.         }
  169.         // send correct headers
  170.         header('Content-Type: text/plain; charset=utf8');
  171.         while (@ob_end_flush()) ;
  172.         // check for configured robots.txt in pimcore
  173.         $content '';
  174.         if (array_key_exists($siteId$config)) {
  175.             $content $config[$siteId];
  176.         }
  177.         if (empty($content)) {
  178.             // default behavior, allow robots to index everything
  179.             $content "User-agent: *\nDisallow:";
  180.         }
  181.         return new Response($contentResponse::HTTP_OK, [
  182.             'Content-Type' => 'text/plain',
  183.         ]);
  184.     }
  185.     /**
  186.      * @param Request $request
  187.      *
  188.      * @return Response
  189.      */
  190.     public function commonFilesAction(Request $request)
  191.     {
  192.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  193.     }
  194.     /**
  195.      * @param Request $request
  196.      *
  197.      * @deprecated
  198.      */
  199.     public function hybridauthAction(Request $request)
  200.     {
  201.         \Pimcore\Tool\HybridAuth::process();
  202.     }
  203.     /**
  204.      * @deprecated
  205.      *
  206.      * @param Request $request
  207.      *
  208.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  209.      */
  210.     public function qrcodeAction(Request $request)
  211.     {
  212.         $code Tool\Qrcode\Config::getByName($request->get('key'));
  213.         if ($code) {
  214.             $url $code->getUrl();
  215.             if ($code->getGoogleAnalytics()) {
  216.                 $glue '?';
  217.                 if (strpos($url'?')) {
  218.                     $glue '&';
  219.                 }
  220.                 $url .= $glue;
  221.                 $url .= 'utm_source=Mobile&utm_medium=QR-Code&utm_campaign=' $code->getName();
  222.             }
  223.             return $this->redirect($url);
  224.         } else {
  225.             Logger::error("called an QR code but '" $request->get('key') . ' is not a code in the system.');
  226.         }
  227.     }
  228.     /**
  229.      * @param Request $request
  230.      *
  231.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  232.      */
  233.     public function customAdminEntryPointAction(Request $request)
  234.     {
  235.         $params $request->query->all();
  236.         if (isset($params['token'])) {
  237.             $url $this->generateUrl('pimcore_admin_login_check'$params);
  238.         } else {
  239.             $url $this->generateUrl('pimcore_admin_login'$params);
  240.         }
  241.         $redirect = new RedirectResponse($url);
  242.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  243.         if (!empty($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  244.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year'), '/'nullfalsetrue));
  245.         }
  246.         return $redirect;
  247.     }
  248. }