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

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