vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/MaintenancePageListener.php line 84

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\ResponseInjectionTrait;
  16. use Pimcore\Tool\Session;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. /**
  21.  * @internal
  22.  */
  23. class MaintenancePageListener
  24. {
  25.     use ResponseInjectionTrait;
  26.     /**
  27.      * @var string|null
  28.      */
  29.     protected $templateCode null;
  30.     /**
  31.      * @param KernelInterface $kernel
  32.      */
  33.     public function __construct(protected KernelInterface $kernel)
  34.     {
  35.     }
  36.     /**
  37.      * @param string $code
  38.      */
  39.     public function setTemplateCode($code): void
  40.     {
  41.         $this->templateCode $code;
  42.     }
  43.     /**
  44.      * @return string|null
  45.      */
  46.     public function getTemplateCode(): ?string
  47.     {
  48.         return $this->templateCode;
  49.     }
  50.     /**
  51.      * @param string $path
  52.      */
  53.     public function loadTemplateFromPath($path): void
  54.     {
  55.         $templateFile PIMCORE_PROJECT_ROOT $path;
  56.         if (file_exists($templateFile)) {
  57.             $this->setTemplateCode(file_get_contents($templateFile));
  58.         }
  59.     }
  60.     /**
  61.      * @param string $path
  62.      */
  63.     public function loadTemplateFromResource($path): void
  64.     {
  65.         $templateFile $this->kernel->locateResource($path);
  66.         if (file_exists($templateFile)) {
  67.             $this->setTemplateCode(file_get_contents($templateFile));
  68.         }
  69.     }
  70.     /**
  71.      * @param RequestEvent $event
  72.      */
  73.     public function onKernelRequest(RequestEvent $event): void
  74.     {
  75.         if (!$event->isMainRequest()) {
  76.             return;
  77.         }
  78.         $request $event->getRequest();
  79.         $maintenance false;
  80.         $file \Pimcore\Tool\Admin::getMaintenanceModeFile();
  81.         if (!is_file($file)) {
  82.             return;
  83.         }
  84.         $conf = include($file);
  85.         if (isset($conf['sessionId'])) {
  86.             $requestSessionId Session::getSessionId();
  87.             $maintenance true;
  88.             if ($conf['sessionId'] === $requestSessionId) {
  89.                 $maintenance false;
  90.             }
  91.         } else {
  92.             @unlink($file);
  93.         }
  94.         // do not activate the maintenance for the server itself
  95.         // this is to avoid problems with monitoring agents
  96.         $serverIps = ['127.0.0.1'];
  97.         if ($maintenance && !in_array($request->getClientIp(), $serverIps)) {
  98.             $response = new Response($this->getTemplateCode(), 503);
  99.             $event->setResponse($response);
  100.         }
  101.     }
  102. }