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

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