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

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 Pimcore\Config;
  17. use Pimcore\Controller\Controller;
  18. use Pimcore\File;
  19. use Pimcore\Logger;
  20. use Pimcore\Model\Asset;
  21. use Pimcore\Model\Site;
  22. use Pimcore\Model\Tool\TmpStore;
  23. use Pimcore\Tool\Storage;
  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\HttpFoundation\StreamedResponse;
  29. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  30. use function time;
  31. /**
  32.  * @internal
  33.  */
  34. class PublicServicesController extends Controller
  35. {
  36.     /**
  37.      * @param Request $request
  38.      *
  39.      * @return RedirectResponse|StreamedResponse
  40.      */
  41.     public function thumbnailAction(Request $request)
  42.     {
  43.         $assetId = (int) $request->get('assetId');
  44.         $thumbnailName $request->get('thumbnailName');
  45.         $filename $request->get('filename');
  46.         $requestedFileExtension strtolower(File::getFileExtension($filename));
  47.         $asset Asset::getById($assetId);
  48.         if ($asset) {
  49.             $prefix preg_replace('@^cache-buster\-[\d]+\/@'''$request->get('prefix'));
  50.             $prefix preg_replace('@' $asset->getId() . '/$@'''$prefix);
  51.             if ($asset->getPath() === ('/' $prefix)) {
  52.                 // we need to check the path as well, this is important in the case you have restricted the public access to
  53.                 // assets via rewrite rules
  54.                 try {
  55.                     $imageThumbnail null;
  56.                     $thumbnailStream null;
  57.                     // just check if the thumbnail exists -> throws exception otherwise
  58.                     $thumbnailConfig Asset\Image\Thumbnail\Config::getByName($thumbnailName);
  59.                     if (!$thumbnailConfig) {
  60.                         // check if there's an item in the TmpStore
  61.                         // remove an eventually existing cache-buster prefix first (eg. when using with a CDN)
  62.                         $pathInfo preg_replace('@^/cache-buster\-[\d]+@'''$request->getPathInfo());
  63.                         $deferredConfigId 'thumb_' $assetId '__' md5(urldecode($pathInfo));
  64.                         if ($thumbnailConfigItem TmpStore::get($deferredConfigId)) {
  65.                             $thumbnailConfig $thumbnailConfigItem->getData();
  66.                             TmpStore::delete($deferredConfigId);
  67.                             if (!$thumbnailConfig instanceof Asset\Image\Thumbnail\Config) {
  68.                                 throw new \Exception("Deferred thumbnail config file doesn't contain a valid \\Asset\\Image\\Thumbnail\\Config object");
  69.                             }
  70.                         } elseif ($this->getParameter('pimcore.config')['assets']['image']['thumbnails']['status_cache']) {
  71.                             // Delete Thumbnail Name from Cache so the next call can generate a new TmpStore entry
  72.                             $asset->getDao()->deleteFromThumbnailCache($thumbnailName);
  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.                             'Content-Length' => $imageThumbnail->getFileSize(),
  136.                         ];
  137.                         $headers[AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER] = true;
  138.                         return new StreamedResponse(function () use ($thumbnailStream) {
  139.                             fpassthru($thumbnailStream);
  140.                         }, 200$headers);
  141.                     }
  142.                     throw new \Exception('Unable to generate thumbnail, see logs for details.');
  143.                 } catch (\Exception $e) {
  144.                     Logger::error($e->getMessage());
  145.                     return new RedirectResponse('/bundles/pimcoreadmin/img/filetype-not-supported.svg');
  146.                 }
  147.             }
  148.         }
  149.         throw $this->createNotFoundException('Asset not found');
  150.     }
  151.     /**
  152.      * @param Request $request
  153.      *
  154.      * @return Response
  155.      */
  156.     public function robotsTxtAction(Request $request)
  157.     {
  158.         // check for site
  159.         $domain \Pimcore\Tool::getHostname();
  160.         $site Site::getByDomain($domain);
  161.         $config Config::getRobotsConfig()->toArray();
  162.         $siteId 'default';
  163.         if ($site instanceof Site) {
  164.             $siteId $site->getId();
  165.         }
  166.         // send correct headers
  167.         header('Content-Type: text/plain; charset=utf8');
  168.         while (@ob_end_flush()) ;
  169.         // check for configured robots.txt in pimcore
  170.         $content '';
  171.         if (array_key_exists($siteId$config)) {
  172.             $content $config[$siteId];
  173.         }
  174.         if (empty($content)) {
  175.             // default behavior, allow robots to index everything
  176.             $content "User-agent: *\nDisallow:";
  177.         }
  178.         return new Response($contentResponse::HTTP_OK, [
  179.             'Content-Type' => 'text/plain',
  180.         ]);
  181.     }
  182.     /**
  183.      * @param Request $request
  184.      *
  185.      * @return Response
  186.      */
  187.     public function commonFilesAction(Request $request)
  188.     {
  189.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  190.     }
  191.     /**
  192.      * @param Request $request
  193.      *
  194.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  195.      */
  196.     public function customAdminEntryPointAction(Request $request)
  197.     {
  198.         $params $request->query->all();
  199.         if (isset($params['token'])) {
  200.             $url $this->generateUrl('pimcore_admin_login_check'$params);
  201.         } else {
  202.             $url $this->generateUrl('pimcore_admin_login'$params);
  203.         }
  204.         $redirect = new RedirectResponse($url);
  205.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  206.         if (!empty($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  207.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year')));
  208.         }
  209.         return $redirect;
  210.     }
  211. }