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

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 = (int) $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.                         } elseif ($this->getParameter('pimcore.config')['assets']['image']['thumbnails']['status_cache']) {
  73.                             // Delete Thumbnail Name from Cache so the next call can generate a new TmpStore entry
  74.                             $asset->getDao()->deleteFromThumbnailCache($thumbnailName);
  75.                         }
  76.                     }
  77.                     if (!$thumbnailConfig) {
  78.                         throw $this->createNotFoundException("Thumbnail '" $thumbnailName "' file doesn't exist");
  79.                     }
  80.                     if (strcasecmp($thumbnailConfig->getFormat(), 'SOURCE') === 0) {
  81.                         $formatOverride $requestedFileExtension;
  82.                         if (in_array($requestedFileExtension, ['jpg''jpeg'])) {
  83.                             $formatOverride 'pjpeg';
  84.                         }
  85.                         $thumbnailConfig->setFormat($formatOverride);
  86.                     }
  87.                     if ($asset instanceof Asset\Video) {
  88.                         $time 1;
  89.                         if (preg_match("|~\-~time\-(\d+)\.|"$filename$matchesThumbs)) {
  90.                             $time = (int)$matchesThumbs[1];
  91.                         }
  92.                         $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$time);
  93.                         $thumbnailStream $imageThumbnail->getStream();
  94.                     } elseif ($asset instanceof Asset\Document) {
  95.                         $page 1;
  96.                         if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  97.                             $page = (int)$matchesThumbs[1];
  98.                         }
  99.                         $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  100.                         $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  101.                         $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$page);
  102.                         $thumbnailStream $imageThumbnail->getStream();
  103.                     } elseif ($asset instanceof Asset\Image) {
  104.                         //check if high res image is called
  105.                         preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  106.                         if (empty($matches) || !isset($matches[1])) {
  107.                             throw $this->createNotFoundException('Requested asset does not exist');
  108.                         }
  109.                         if (array_key_exists(2$matches) && $matches[2]) {
  110.                             $highResFactor = (float)str_replace(['@''x'], ''$matches[2]);
  111.                             $thumbnailConfig->setHighResolution($highResFactor);
  112.                         }
  113.                         // check if a media query thumbnail was requested
  114.                         if (preg_match("#~\-~media\-\-(.*)\-\-query#"$matches[1], $mediaQueryResult)) {
  115.                             $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  116.                         }
  117.                         $imageThumbnail $asset->getThumbnail($thumbnailConfig);
  118.                         $thumbnailStream $imageThumbnail->getStream();
  119.                     }
  120.                     if ($imageThumbnail && $thumbnailStream) {
  121.                         $pathReference $imageThumbnail->getPathReference();
  122.                         $actualFileExtension File::getFileExtension($pathReference['src']);
  123.                         if ($actualFileExtension !== $requestedFileExtension) {
  124.                             // create a copy/symlink to the file with the original file extension
  125.                             // this can be e.g. the case when the thumbnail is called as foo.png but the thumbnail config
  126.                             // is set to auto-optimized format so the resulting thumbnail can be jpeg
  127.                             $requestedFile preg_replace('/\.' $actualFileExtension '$/''.' $requestedFileExtension$pathReference['src']);
  128.                             Storage::get('thumbnail')->writeStream($requestedFile$thumbnailStream);
  129.                         }
  130.                         // set appropriate caching headers
  131.                         // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  132.                         $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  133.                         $headers = [
  134.                             'Cache-Control' => 'public, max-age=' $lifetime,
  135.                             'Expires' => date('D, d M Y H:i:s T'time() + $lifetime),
  136.                             'Content-Type' => $imageThumbnail->getMimeType(),
  137.                         ];
  138.                         $stats fstat($thumbnailStream);
  139.                         if (is_array($stats)) {
  140.                             $headers['Content-Length'] = $stats['size'];
  141.                         }
  142.                         $headers[AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER] = true;
  143.                         return new StreamedResponse(function () use ($thumbnailStream) {
  144.                             fpassthru($thumbnailStream);
  145.                         }, 200$headers);
  146.                     }
  147.                     throw new \Exception('Unable to generate thumbnail, see logs for details.');
  148.                 } catch (\Exception $e) {
  149.                     Logger::error($e->getMessage());
  150.                     return new RedirectResponse('/bundles/pimcoreadmin/img/filetype-not-supported.svg');
  151.                 }
  152.             }
  153.         }
  154.         throw $this->createNotFoundException('Asset not found');
  155.     }
  156.     /**
  157.      * @param Request $request
  158.      *
  159.      * @return Response
  160.      */
  161.     public function robotsTxtAction(Request $request)
  162.     {
  163.         // check for site
  164.         $domain \Pimcore\Tool::getHostname();
  165.         $site Site::getByDomain($domain);
  166.         $config Config::getRobotsConfig()->toArray();
  167.         $siteId 'default';
  168.         if ($site instanceof Site) {
  169.             $siteId $site->getId();
  170.         }
  171.         // send correct headers
  172.         header('Content-Type: text/plain; charset=utf8');
  173.         while (@ob_end_flush()) ;
  174.         // check for configured robots.txt in pimcore
  175.         $content '';
  176.         if (array_key_exists($siteId$config)) {
  177.             $content $config[$siteId];
  178.         }
  179.         if (empty($content)) {
  180.             // default behavior, allow robots to index everything
  181.             $content "User-agent: *\nDisallow:";
  182.         }
  183.         return new Response($contentResponse::HTTP_OK, [
  184.             'Content-Type' => 'text/plain',
  185.         ]);
  186.     }
  187.     /**
  188.      * @param Request $request
  189.      *
  190.      * @return Response
  191.      */
  192.     public function commonFilesAction(Request $request)
  193.     {
  194.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  195.     }
  196.     /**
  197.      * @param Request $request
  198.      *
  199.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  200.      */
  201.     public function customAdminEntryPointAction(Request $request)
  202.     {
  203.         $params $request->query->all();
  204.         if (isset($params['token'])) {
  205.             $url $this->generateUrl('pimcore_admin_login_check'$params);
  206.         } else {
  207.             $url $this->generateUrl('pimcore_admin_login'$params);
  208.         }
  209.         $redirect = new RedirectResponse($url);
  210.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  211.         if (!empty($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  212.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year'), '/'nullfalsetrue));
  213.         }
  214.         return $redirect;
  215.     }
  216. }