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

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\Logger;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Model\Site;
  20. use Pimcore\Model\Tool;
  21. use Pimcore\Model\Tool\TmpStore;
  22. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  23. use Symfony\Component\HttpFoundation\Cookie;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. class PublicServicesController extends Controller
  28. {
  29.     /**
  30.      * @param Request $request
  31.      *
  32.      * @return BinaryFileResponse
  33.      */
  34.     public function thumbnailAction(Request $request)
  35.     {
  36.         $assetId $request->get('assetId');
  37.         $thumbnailName $request->get('thumbnailName');
  38.         $filename $request->get('filename');
  39.         $asset Asset::getById($assetId);
  40.         $prefix preg_replace('@^cache-buster\-[\d]+\/@'''$request->get('prefix'));
  41.         if ($asset && $asset->getPath() == ('/' $prefix)) {
  42.             // we need to check the path as well, this is important in the case you have restricted the public access to
  43.             // assets via rewrite rules
  44.             try {
  45.                 $page 1// default
  46.                 $imageThumbnail null;
  47.                 $thumbnailFile null;
  48.                 $thumbnailConfig null;
  49.                 //get page in case of an asset document (PDF, ...)
  50.                 if (preg_match("|~\-~page\-(\d+)\.|"$filename$matchesThumbs)) {
  51.                     $page = (int)$matchesThumbs[1];
  52.                 }
  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 ($asset instanceof Asset\Document) {
  72.                     $thumbnailConfig->setName(preg_replace("/\-[\d]+/"''$thumbnailConfig->getName()));
  73.                     $thumbnailConfig->setName(str_replace('document_'''$thumbnailConfig->getName()));
  74.                     $imageThumbnail $asset->getImageThumbnail($thumbnailConfig$page);
  75.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  76.                 } elseif ($asset instanceof Asset\Image) {
  77.                     //check if high res image is called
  78.                     preg_match("@([^\@]+)(\@[0-9.]+x)?\.([a-zA-Z]{2,5})@"$filename$matches);
  79.                     if (array_key_exists(2$matches) && $matches[2]) {
  80.                         $highResFactor = (float) str_replace(['@''x'], ''$matches[2]);
  81.                         $thumbnailConfig->setHighResolution($highResFactor);
  82.                     }
  83.                     // check if a media query thumbnail was requested
  84.                     if (preg_match("#~\-~([\d]+w)#"$matches[1], $mediaQueryResult)) {
  85.                         $thumbnailConfig->selectMedia($mediaQueryResult[1]);
  86.                     }
  87.                     $imageThumbnail $asset->getThumbnail($thumbnailConfig);
  88.                     $thumbnailFile $imageThumbnail->getFileSystemPath();
  89.                 }
  90.                 if ($imageThumbnail && $thumbnailFile && file_exists($thumbnailFile)) {
  91.                     // set appropriate caching headers
  92.                     // see also: https://github.com/pimcore/pimcore/blob/1931860f0aea27de57e79313b2eb212dcf69ef13/.htaccess#L86-L86
  93.                     $lifetime 86400 7// 1 week lifetime, same as direct delivery in .htaccess
  94.                     return new BinaryFileResponse($thumbnailFile200, [
  95.                         'Cache-Control' => 'public, max-age=' $lifetime,
  96.                         'Expires' => date('D, d M Y H:i:s T'time() + $lifetime),
  97.                         'Content-Type' => $imageThumbnail->getMimeType()
  98.                     ]);
  99.                 }
  100.             } catch (\Exception $e) {
  101.                 $message "Thumbnail with name '" $thumbnailName "' doesn't exist";
  102.                 Logger::error($message);
  103.                 throw $this->createNotFoundException($message$e);
  104.             }
  105.         } else {
  106.             throw $this->createNotFoundException('Asset not found');
  107.         }
  108.     }
  109.     /**
  110.      * @param $request
  111.      *
  112.      * @return Response
  113.      */
  114.     public function robotsTxtAction(Request $request)
  115.     {
  116.         // check for site
  117.         $domain = \Pimcore\Tool::getHostname();
  118.         $site Site::getByDomain($domain);
  119.         $config Config::getRobotsConfig()->toArray();
  120.         $siteId 'default';
  121.         if ($site instanceof Site) {
  122.             $siteId $site->getId();
  123.         }
  124.         // send correct headers
  125.         header('Content-Type: text/plain; charset=utf8');
  126.         while (@ob_end_flush()) ;
  127.         // check for configured robots.txt in pimcore
  128.         $content '';
  129.         if (array_key_exists($siteId$config)) {
  130.             $content $config[$siteId];
  131.         }
  132.         if (empty($content)) {
  133.             // default behavior, allow robots to index everything
  134.             $content "User-agent: *\nDisallow:";
  135.         }
  136.         return new Response($contentResponse::HTTP_OK, [
  137.             'Content-Type' => 'text/plain'
  138.         ]);
  139.     }
  140.     /**
  141.      * @param Request $request
  142.      *
  143.      * @return Response
  144.      */
  145.     public function commonFilesAction(Request $request)
  146.     {
  147.         return new Response("HTTP/1.1 404 Not Found\nFiltered by common files filter"404);
  148.     }
  149.     /**
  150.      * @param Request $request
  151.      */
  152.     public function hybridauthAction(Request $request)
  153.     {
  154.         \Pimcore\Tool\HybridAuth::process();
  155.     }
  156.     /**
  157.      * @param Request $request
  158.      *
  159.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  160.      */
  161.     public function qrcodeAction(Request $request)
  162.     {
  163.         $code Tool\Qrcode\Config::getByName($request->get('key'));
  164.         if ($code) {
  165.             $url $code->getUrl();
  166.             if ($code->getGoogleAnalytics()) {
  167.                 $glue '?';
  168.                 if (strpos($url'?')) {
  169.                     $glue '&';
  170.                 }
  171.                 $url .= $glue;
  172.                 $url .= 'utm_source=Mobile&utm_medium=QR-Code&utm_campaign=' $code->getName();
  173.             }
  174.             return $this->redirect($url);
  175.         } else {
  176.             Logger::error("called an QR code but '" $request->get('key') . ' is not a code in the system.');
  177.         }
  178.     }
  179.     /**
  180.      * @param Request $request
  181.      *
  182.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  183.      */
  184.     public function customAdminEntryPointAction(Request $request)
  185.     {
  186.         $url $this->generateUrl('pimcore_admin_login');
  187.         $redirect = new RedirectResponse($url);
  188.         $customAdminPathIdentifier $this->getParameter('pimcore_admin.custom_admin_path_identifier');
  189.         if (isset($customAdminPathIdentifier) && $request->cookies->get('pimcore_custom_admin') != $customAdminPathIdentifier) {
  190.             $redirect->headers->setCookie(new Cookie('pimcore_custom_admin'$customAdminPathIdentifierstrtotime('+1 year'), '/'nullfalsetrue));
  191.         }
  192.         return $redirect;
  193.     }
  194. }