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

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\Logger;
  16. use Pimcore\Model\Asset;
  17. use Pimcore\Model\Site;
  18. use Pimcore\Model\Tool;
  19. use Pimcore\Model\Tool\TmpStore;
  20. use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController;
  21. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. class PublicServicesController extends FrameworkController
  25. {
  26.     /**
  27.      * @param Request $request
  28.      *
  29.      * @return BinaryFileResponse
  30.      */
  31.     public function thumbnailAction(Request $request)
  32.     {
  33.         $assetId $request->get('assetId');
  34.         $thumbnailName $request->get('thumbnailName');
  35.         $filename $request->get('filename');
  36.         $asset Asset::getById($assetId);
  37.         if ($asset && $asset->getPath() == ('/' $request->get('prefix'))) {
  38.             // we need to check the path as well, this is important in the case you have restricted the public access to
  39.             // assets via rewrite rules
  40.             try {
  41.                 $page 1// default
  42.                 $thumbnailFile null;
  43.                 $thumbnailConfig null;
  44.                 //get page in case of an asset document (PDF, ...)
  45.                 if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  46.                     $page = (int)$matchesThumbs[1];
  47.                 }
  48.                 // just check if the thumbnail exists -> throws exception otherwise
  49.                 $thumbnailConfig Asset\Image\Thumbnail\Config::getByName($thumbnailName);
  50.                 if (!$thumbnailConfig) {
  51.                     // check if there's an item in the TmpStore
  52.                     // remove an eventually existing cache-buster prefix first (eg. when using with a CDN)
  53.                     $pathInfo preg_replace('@^/cache-buster\-[\d]+@'''$request->getPathInfo());
  54.                     $deferredConfigId 'thumb_' $assetId '__' md5(urldecode($pathInfo));
  55.                     if ($thumbnailConfigItem TmpStore::get($deferredConfigId)) {
  56.                         $thumbnailConfig $thumbnailConfigItem->getData();
  57.                         TmpStore::delete($deferredConfigId);
  58.                         if (!$thumbnailConfig instanceof Asset\Image\Thumbnail\Config) {
  59.                             throw new \Exception("Deferred thumbnail config file doesn't contain a valid \\Asset\\Image\\Thumbnail\\Config object");
  60.                         }
  61.                     }
  62.                 }
  63.                 if (!$thumbnailConfig) {
  64.                     throw $this->createNotFoundException("Thumbnail '" $thumbnailName "' file doesn't exist");
  65.                 }
  66.                 if ($asset instanceof Asset\Document) {
  67.                     $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  68.                     $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  69.                     $thumbnailFile $asset->getImageThumbnail($thumbnailConfig$page)->getFileSystemPath();
  70.                 } elseif ($asset instanceof Asset\Image) {
  71.                     //check if high res image is called
  72.                     preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  73.                     if (array_key_exists(2$matches) && $matches[2]) {
  74.                         $highResFactor = (float) str_replace(['@''x'], ''$matches[2]);
  75.                         $thumbnailConfig->setHighResolution($highResFactor);
  76.                     }
  77.                     // check if a media query thumbnail was requested
  78.                     if (preg_match("#~\-~([\d]+w)#"$matches[1], $mediaQueryResult)) {
  79.                         $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  80.                     }
  81.                     $thumbnailFile $asset->getThumbnail($thumbnailConfig)->getFileSystemPath();
  82.                 }
  83.                 if ($thumbnailFile && file_exists($thumbnailFile)) {
  84.                     // set appropriate caching headers
  85.                     // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  86.                     $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  87.                     return new BinaryFileResponse($thumbnailFile200, [
  88.                         'Cache-Control' => 'public, max-age=' $lifetime,
  89.                         'Expires' => date('D, d M Y H:i:s T'time() + $lifetime)
  90.                     ]);
  91.                 }
  92.             } catch (\Exception $e) {
  93.                 $message "Thumbnail with name '" $thumbnailName "' doesn't exist";
  94.                 Logger::error($message);
  95.                 throw $this->createNotFoundException($message$e);
  96.             }
  97.         } else {
  98.             throw $this->createNotFoundException('Asset not found');
  99.         }
  100.     }
  101.     /**
  102.      * @param $request
  103.      *
  104.      * @return Response
  105.      */
  106.     public function robotsTxtAction(Request $request)
  107.     {
  108.         // check for site
  109.         $site null;
  110.         try {
  111.             $domain = \Pimcore\Tool::getHostname();
  112.             $site Site::getByDomain($domain);
  113.         } catch (\Exception $e) {
  114.         }
  115.         $siteSuffix '-default';
  116.         if ($site instanceof Site) {
  117.             $siteSuffix '-' $site->getId();
  118.         }
  119.         // send correct headers
  120.         header('Content-Type: text/plain; charset=utf8');
  121.         while (@ob_end_flush()) ;
  122.         // check for configured robots.txt in pimcore
  123.         $content '';
  124.         $robotsPath PIMCORE_CONFIGURATION_DIRECTORY '/robots' $siteSuffix '.txt';
  125.         if (is_file($robotsPath)) {
  126.             $content file_get_contents($robotsPath);
  127.         }
  128.         if (empty($content)) {
  129.             // default behavior, allow robots to index everything
  130.             $content "User-agent: *\nDisallow:";
  131.         }
  132.         return new Response($content200);
  133.     }
  134.     /**
  135.      * @param Request $request
  136.      *
  137.      * @return Response
  138.      */
  139.     public function commonFilesAction(Request $request)
  140.     {
  141.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  142.     }
  143.     /**
  144.      * @param Request $request
  145.      */
  146.     public function hybridauthAction(Request $request)
  147.     {
  148.         \Pimcore\Tool\HybridAuth::process();
  149.     }
  150.     /**
  151.      * @param Request $request
  152.      *
  153.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  154.      */
  155.     public function qrcodeAction(Request $request)
  156.     {
  157.         $code Tool\Qrcode\Config::getByName($request->get('key'));
  158.         if ($code) {
  159.             $url $code->getUrl();
  160.             if ($code->getGoogleAnalytics()) {
  161.                 $glue '?';
  162.                 if (strpos($url'?')) {
  163.                     $glue '&';
  164.                 }
  165.                 $url .= $glue;
  166.                 $url .= 'utm_source=Mobile&utm_medium=QR-Code&utm_campaign=' $code->getName();
  167.             }
  168.             return $this->redirect($url);
  169.         } else {
  170.             Logger::error("called an QR code but '" $request->get('key') . ' is not a code in the system.');
  171.         }
  172.     }
  173. }