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

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 Enterprise License (PEL)
  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 PEL
  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.         $assetId $request->get('assetId');
  41.         $thumbnailName $request->get('thumbnailName');
  42.         $filename $request->get('filename');
  43.         $requestedFileExtension strtolower(File::getFileExtension($filename));
  44.         $asset Asset::getById($assetId);
  45.         $prefix preg_replace('@^cache-buster\-[\d]+\/@'''$request->get('prefix'));
  46.         if ($asset && $asset->getPath() == ('/' $prefix)) {
  47.             // we need to check the path as well, this is important in the case you have restricted the public access to
  48.             // assets via rewrite rules
  49.             try {
  50.                 $imageThumbnail null;
  51.                 $thumbnailFile null;
  52.                 $thumbnailConfig null;
  53.                 // just check if the thumbnail exists -> throws exception otherwise
  54.                 $thumbnailConfig Asset\Image\Thumbnail\Config::getByName($thumbnailName);
  55.                 if (!$thumbnailConfig) {
  56.                     // check if there's an item in the TmpStore
  57.                     // remove an eventually existing cache-buster prefix first (eg. when using with a CDN)
  58.                     $pathInfo preg_replace('@^/cache-buster\-[\d]+@'''$request->getPathInfo());
  59.                     $deferredConfigId 'thumb_' $assetId '__' md5(urldecode($pathInfo));
  60.                     if ($thumbnailConfigItem TmpStore::get($deferredConfigId)) {
  61.                         $thumbnailConfig $thumbnailConfigItem->getData();
  62.                         TmpStore::delete($deferredConfigId);
  63.                         if (!$thumbnailConfig instanceof Asset\Image\Thumbnail\Config) {
  64.                             throw new \Exception("Deferred thumbnail config file doesn't contain a valid \\Asset\\Image\\Thumbnail\\Config object");
  65.                         }
  66.                     }
  67.                 }
  68.                 if (!$thumbnailConfig) {
  69.                     throw $this->createNotFoundException("Thumbnail '" $thumbnailName "' file doesn't exist");
  70.                 }
  71.                 if (strcasecmp($thumbnailConfig->getFormat(), 'SOURCE') === 0) {
  72.                     $formatOverride $requestedFileExtension;
  73.                     if (in_array($requestedFileExtension, ['jpg''jpeg'])) {
  74.                         $formatOverride 'pjpeg';
  75.                     }
  76.                     $thumbnailConfig->setFormat($formatOverride);
  77.                 }
  78.                 if ($asset instanceof Asset\Video) {
  79.                     $time 1;
  80.                     if (preg_match("|~\-~time\-(\d+)\.|"$filename$matchesThumbs)) {
  81.                         $time = (int)$matchesThumbs[1];
  82.                     }
  83.                     $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$time);
  84.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  85.                 } elseif ($asset instanceof Asset\Document) {
  86.                     $page 1;
  87.                     if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  88.                         $page = (int)$matchesThumbs[1];
  89.                     }
  90.                     $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  91.                     $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  92.                     $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$page);
  93.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  94.                 } elseif ($asset instanceof Asset\Image) {
  95.                     //check if high res image is called
  96.                     preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  97.                     if (array_key_exists(2$matches) && $matches[2]) {
  98.                         $highResFactor = (float) str_replace(['@''x'], ''$matches[2]);
  99.                         $thumbnailConfig->setHighResolution($highResFactor);
  100.                     }
  101.                     // check if a media query thumbnail was requested
  102.                     if (preg_match("#~\-~media\-\-(.*)\-\-query#"$matches[1], $mediaQueryResult)) {
  103.                         $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  104.                     }
  105.                     $imageThumbnail $asset->getThumbnail($thumbnailConfig);
  106.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  107.                 }
  108.                 if ($imageThumbnail && $thumbnailFile && file_exists($thumbnailFile)) {
  109.                     $actualFileExtension File::getFileExtension($thumbnailFile);
  110.                     if ($actualFileExtension !== $requestedFileExtension) {
  111.                         // create a copy/symlink to the file with the original file extension
  112.                         // this can be e.g. the case when the thumbnail is called as foo.png but the thumbnail config
  113.                         // is set to auto-optimized format so the resulting thumbnail can be jpeg
  114.                         $requestedFile preg_replace('/\.' $actualFileExtension '$/''.' $requestedFileExtension$thumbnailFile);
  115.                         $linked is_link($requestedFile) || symlink($thumbnailFile$requestedFile);
  116.                         if (false === $linked) {
  117.                             // create a hard copy
  118.                             copy($thumbnailFile$requestedFile);
  119.                         }
  120.                     }
  121.                     // set appropriate caching headers
  122.                     // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  123.                     $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  124.                     $headers = [
  125.                         'Cache-Control' => 'public, max-age=' $lifetime,
  126.                         'Expires' => date('D, d M Y H:i:s T'time() + $lifetime),
  127.                         'Content-Type' => $imageThumbnail->getMimeType()
  128.                     ];
  129.                     // in certain cases where an event listener starts a session (e.g. when there's a firewall
  130.                     // configured for the entire site /*) the session event listener shouldn't modify the
  131.                     // cache control headers of this response
  132.                     if (defined('Symfony\Component\HttpKernel\EventListener\AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER')) {
  133.                         // this method of bypassing the session listener was introduced in Symfony 4, so we need
  134.                         // to check for the constant first
  135.                         $headers[AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER] = true;
  136.                     } else {
  137.                         // @TODO to be removed in Pimcore 7
  138.                         // Symfony 3.4 doesn't support bypassing the session listener, so we just remove it
  139.                         \Pimcore::getEventDispatcher()->removeSubscriber($sessionListener);
  140.                     }
  141.                     return new BinaryFileResponse($thumbnailFile200$headers);
  142.                 }
  143.             } catch (\Exception $e) {
  144.                 $message "Thumbnail with name '" $thumbnailName "' doesn't exist";
  145.                 Logger::error($message);
  146.                 throw $this->createNotFoundException($message$e);
  147.             }
  148.         } else {
  149.             throw $this->createNotFoundException('Asset not found');
  150.         }
  151.         throw $this->createNotFoundException('Unable to create image thumbnail');
  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.     public function hybridauthAction(Request $request)
  197.     {
  198.         \Pimcore\Tool\HybridAuth::process();
  199.     }
  200.     /**
  201.      * @param Request $request
  202.      *
  203.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  204.      */
  205.     public function qrcodeAction(Request $request)
  206.     {
  207.         $code Tool\Qrcode\Config::getByName($request->get('key'));
  208.         if ($code) {
  209.             $url $code->getUrl();
  210.             if ($code->getGoogleAnalytics()) {
  211.                 $glue '?';
  212.                 if (strpos($url'?')) {
  213.                     $glue '&';
  214.                 }
  215.                 $url .= $glue;
  216.                 $url .= 'utm_source=Mobile&utm_medium=QR-Code&utm_campaign=' $code->getName();
  217.             }
  218.             return $this->redirect($url);
  219.         } else {
  220.             Logger::error("called an QR code but '" $request->get('key') . ' is not a code in the system.');
  221.         }
  222.     }
  223.     /**
  224.      * @param Request $request
  225.      *
  226.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  227.      */
  228.     public function customAdminEntryPointAction(Request $request)
  229.     {
  230.         $url $this->generateUrl('pimcore_admin_login');
  231.         $redirect = new RedirectResponse($url);
  232.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  233.         if (isset($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  234.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year'), '/'nullfalsetrue));
  235.         }
  236.         return $redirect;
  237.     }
  238. }