pimcore/lib/Pimcore/Bundle/CoreBundle/Controller/PublicServicesController.php line 121

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