vendor/contao/core-bundle/src/Controller/FrontendController.php line 90

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Controller;
  11. use Contao\CoreBundle\Cron\Cron;
  12. use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager;
  13. use Contao\FrontendIndex;
  14. use Contao\FrontendShare;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Core\Exception\LogoutException;
  20. /**
  21.  * @Route(defaults={"_scope" = "frontend", "_token_check" = true})
  22.  *
  23.  * @internal
  24.  */
  25. class FrontendController extends AbstractController
  26. {
  27.     public function indexAction(): Response
  28.     {
  29.         $this->initializeContaoFramework();
  30.         trigger_deprecation('contao/core-bundle''4.10''Using "Contao\FrontendController::indexAction()" has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.');
  31.         $controller = new FrontendIndex();
  32.         return $controller->run();
  33.     }
  34.     /**
  35.      * @Route("/_contao/cron", name="contao_frontend_cron")
  36.      */
  37.     public function cronAction(Request $requestCron $cron): Response
  38.     {
  39.         if ($request->isMethod(Request::METHOD_GET)) {
  40.             $cron->run(Cron::SCOPE_WEB);
  41.         }
  42.         return new Response(''Response::HTTP_NO_CONTENT);
  43.     }
  44.     /**
  45.      * @Route("/_contao/share", name="contao_frontend_share")
  46.      */
  47.     public function shareAction(): RedirectResponse
  48.     {
  49.         $this->initializeContaoFramework();
  50.         $controller = new FrontendShare();
  51.         return $controller->run();
  52.     }
  53.     /**
  54.      * Symfony will un-authenticate the user automatically by calling this route.
  55.      *
  56.      * @throws LogoutException
  57.      *
  58.      * @Route("/_contao/logout", name="contao_frontend_logout")
  59.      */
  60.     public function logoutAction(): void
  61.     {
  62.         throw new LogoutException('The user was not logged out correctly.');
  63.     }
  64.     /**
  65.      * Generates a 1px transparent PNG image uncacheable response.
  66.      *
  67.      * This route can be used to include e.g. a hidden <img> tag to force
  68.      * a request to the application. That way, cookies can be set even if
  69.      * the output is cached (used in the core if the "alwaysLoadFromCache"
  70.      * option is enabled to evaluate the RememberMe cookie and then set
  71.      * the session cookie).
  72.      *
  73.      * @Route("/_contao/check_cookies", name="contao_frontend_check_cookies")
  74.      */
  75.     public function checkCookiesAction(): Response
  76.     {
  77.         static $image 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
  78.         $response = new Response(base64_decode($imagetrue));
  79.         $response->setPrivate();
  80.         $response->headers->set('Content-Type''image/png');
  81.         $response->headers->addCacheControlDirective('no-store');
  82.         $response->headers->addCacheControlDirective('must-revalidate');
  83.         return $response;
  84.     }
  85.     /**
  86.      * Returns a script that makes sure a valid request token is filled into
  87.      * all forms if the "alwaysLoadFromCache" option is enabled.
  88.      *
  89.      * @Route("/_contao/request_token_script", name="contao_frontend_request_token_script")
  90.      */
  91.     public function requestTokenScriptAction(): Response
  92.     {
  93.         $token $this
  94.             ->get(ContaoCsrfTokenManager::class)
  95.             ->getToken($this->getParameter('contao.csrf_token_name'))
  96.             ->getValue()
  97.         ;
  98.         $token json_encode($token);
  99.         $response = new Response();
  100.         $response->setContent('document.querySelectorAll(\'input[name=REQUEST_TOKEN],input[name$="[REQUEST_TOKEN]"]\').forEach(function(i){i.value='.$token.'})');
  101.         $response->headers->set('Content-Type''application/javascript; charset=UTF-8');
  102.         $response->headers->addCacheControlDirective('no-store');
  103.         $response->headers->addCacheControlDirective('must-revalidate');
  104.         return $response;
  105.     }
  106.     public static function getSubscribedServices(): array
  107.     {
  108.         $services parent::getSubscribedServices();
  109.         $services[] = ContaoCsrfTokenManager::class;
  110.         return $services;
  111.     }
  112. }