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

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 function date;
  16. use function fstat;
  17. use function is_array;
  18. use Pimcore\Config;
  19. use Pimcore\Controller\Controller;
  20. use Pimcore\File;
  21. use Pimcore\Logger;
  22. use Pimcore\Model\Asset;
  23. use Pimcore\Model\Site;
  24. use Pimcore\Model\Tool\TmpStore;
  25. use Pimcore\Tool\Storage;
  26. use Symfony\Component\HttpFoundation\Cookie;
  27. use Symfony\Component\HttpFoundation\RedirectResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpFoundation\StreamedResponse;
  31. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  32. use function time;
  33. /**
  34.  * @internal
  35.  */
  36. class PublicServicesController extends Controller
  37. {
  38.     /**
  39.      * @param Request $request
  40.      *
  41.      * @return RedirectResponse|StreamedResponse
  42.      */
  43.     public function thumbnailAction(Request $request)
  44.     {
  45.         $assetId $request->get('assetId');
  46.         $thumbnailName $request->get('thumbnailName');
  47.         $filename $request->get('filename');
  48.         $requestedFileExtension strtolower(File::getFileExtension($filename));
  49.         $asset Asset::getById($assetId);
  50.         if ($asset) {
  51.             $prefix preg_replace('@^cache-buster\-[\d]+\/@'''$request->get('prefix'));
  52.             $prefix preg_replace('@' $asset->getId() . '/$@'''$prefix);
  53.             if ($asset->getPath() === ('/' $prefix)) {
  54.                 // we need to check the path as well, this is important in the case you have restricted the public access to
  55.                 // assets via rewrite rules
  56.                 try {
  57.                     $imageThumbnail null;
  58.                     $thumbnailStream null;
  59.                     // just check if the thumbnail exists -> throws exception otherwise
  60.                     $thumbnailConfig Asset\Image\Thumbnail\Config::getByName($thumbnailName);
  61.                     if (!$thumbnailConfig) {
  62.                         // check if there's an item in the TmpStore
  63.                         // remove an eventually existing cache-buster prefix first (eg. when using with a CDN)
  64.                         $pathInfo preg_replace('@^/cache-buster\-[\d]+@'''$request->getPathInfo());
  65.                         $deferredConfigId 'thumb_' $assetId '__' md5(urldecode($pathInfo));
  66.                         if ($thumbnailConfigItem TmpStore::get($deferredConfigId)) {
  67.                             $thumbnailConfig $thumbnailConfigItem->getData();
  68.                             TmpStore::delete($deferredConfigId);
  69.                             if (!$thumbnailConfig instanceof Asset\Image\Thumbnail\Config) {
  70.                                 throw new \Exception("Deferred thumbnail config file doesn't contain a valid \\Asset\\Image\\Thumbnail\\Config object");
  71.                             }
  72.                         }
  73.                     }
  74.                     if (!$thumbnailConfig) {
  75.                         throw $this->createNotFoundException("Thumbnail '" $thumbnailName "' file doesn't exist");
  76.                     }
  77.                     if (strcasecmp($thumbnailConfig->getFormat(), 'SOURCE') === 0) {
  78.                         $formatOverride $requestedFileExtension;
  79.                         if (in_array($requestedFileExtension, ['jpg''jpeg'])) {
  80.                             $formatOverride 'pjpeg';
  81.                         }
  82.                         $thumbnailConfig->setFormat($formatOverride);
  83.                     }
  84.                     if ($asset instanceof Asset\Video) {
  85.                         $time 1;
  86.                         if (preg_match("|~\-~time\-(\d+)\.|"$filename$matchesThumbs)) {
  87.                             $time = (int)$matchesThumbs[1];
  88.                         }
  89.                         $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$time);
  90.                         $thumbnailStream $imageThumbnail->getStream();
  91.                     } elseif ($asset instanceof Asset\Document) {
  92.                         $page 1;
  93.                         if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  94.                             $page = (int)$matchesThumbs[1];
  95.                         }
  96.                         $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  97.                         $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  98.                         $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$page);
  99.                         $thumbnailStream $imageThumbnail->getStream();
  100.                     } elseif ($asset instanceof Asset\Image) {
  101.                         //check if high res image is called
  102.                         preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  103.                         if (empty($matches) || !isset($matches[1])) {
  104.                             throw $this->createNotFoundException('Requested asset does not exist');
  105.                         }
  106.                         if (array_key_exists(2$matches) && $matches[2]) {
  107.                             $highResFactor = (float)str_replace(['@''x'], ''$matches[2]);
  108.                             $thumbnailConfig->setHighResolution($highResFactor);
  109.                         }
  110.                         // check if a media query thumbnail was requested
  111.                         if (preg_match("#~\-~media\-\-(.*)\-\-query#"$matches[1], $mediaQueryResult)) {
  112.                             $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  113.                         }
  114.                         $imageThumbnail $asset->getThumbnail($thumbnailConfig);
  115.                         $thumbnailStream $imageThumbnail->getStream();
  116.                     }
  117.                     if ($imageThumbnail && $thumbnailStream) {
  118.                         $pathReference $imageThumbnail->getPathReference();
  119.                         $actualFileExtension File::getFileExtension($pathReference['src']);
  120.                         if ($actualFileExtension !== $requestedFileExtension) {
  121.                             // create a copy/symlink to the file with the original file extension
  122.                             // this can be e.g. the case when the thumbnail is called as foo.png but the thumbnail config
  123.                             // is set to auto-optimized format so the resulting thumbnail can be jpeg
  124.                             $requestedFile preg_replace('/\.' $actualFileExtension '$/''.' $requestedFileExtension$pathReference['src']);
  125.                             Storage::get('thumbnail')->writeStream($requestedFile$thumbnailStream);
  126.                         }
  127.                         // set appropriate caching headers
  128.                         // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  129.                         $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  130.                         $headers = [
  131.                             'Cache-Control' => 'public, max-age=' $lifetime,
  132.                             'Expires' => date('D, d M Y H:i:s T'time() + $lifetime),
  133.                             'Content-Type' => $imageThumbnail->getMimeType(),
  134.                         ];
  135.                         $stats fstat($thumbnailStream);
  136.                         if (is_array($stats)) {
  137.                             $headers['Content-Length'] = $stats['size'];
  138.                         }
  139.                         $headers[AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER] = true;
  140.                         return new StreamedResponse(function () use ($thumbnailStream) {
  141.                             fpassthru($thumbnailStream);
  142.                         }, 200$headers);
  143.                     }
  144.                     throw new \Exception('Unable to generate thumbnail, see logs for details.');
  145.                 } catch (\Exception $e) {
  146.                     Logger::error($e->getMessage());
  147.                     return new RedirectResponse('/bundles/pimcoreadmin/img/filetype-not-supported.svg');
  148.                 }
  149.             }
  150.         }
  151.         throw $this->createNotFoundException('Asset not found');
  152.     }
  153.     /**
  154.      * @param Request $request
  155.      *
  156.      * @return Response
  157.      */
  158.     public function robotsTxtAction(Request $request)
  159.     {
  160.         // check for site
  161.         $domain \Pimcore\Tool::getHostname();
  162.         $site Site::getByDomain($domain);
  163.         $config Config::getRobotsConfig()->toArray();
  164.         $siteId 'default';
  165.         if ($site instanceof Site) {
  166.             $siteId $site->getId();
  167.         }
  168.         // send correct headers
  169.         header('Content-Type: text/plain; charset=utf8');
  170.         while (@ob_end_flush()) ;
  171.         // check for configured robots.txt in pimcore
  172.         $content '';
  173.         if (array_key_exists($siteId$config)) {
  174.             $content $config[$siteId];
  175.         }
  176.         if (empty($content)) {
  177.             // default behavior, allow robots to index everything
  178.             $content "User-agent: *\nDisallow:";
  179.         }
  180.         return new Response($contentResponse::HTTP_OK, [
  181.             'Content-Type' => 'text/plain',
  182.         ]);
  183.     }
  184.     /**
  185.      * @param Request $request
  186.      *
  187.      * @return Response
  188.      */
  189.     public function commonFilesAction(Request $request)
  190.     {
  191.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  192.     }
  193.     /**
  194.      * @param Request $request
  195.      *
  196.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  197.      */
  198.     public function customAdminEntryPointAction(Request $request)
  199.     {
  200.         $params $request->query->all();
  201.         if (isset($params['token'])) {
  202.             $url $this->generateUrl('pimcore_admin_login_check'$params);
  203.         } else {
  204.             $url $this->generateUrl('pimcore_admin_login'$params);
  205.         }
  206.         $redirect = new RedirectResponse($url);
  207.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  208.         if (!empty($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  209.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year'), '/'nullfalsetrue));
  210.         }
  211.         return $redirect;
  212.     }
  213. }