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

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
  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)
  40.     {
  41.         $this->templateCode $code;
  42.     }
  43.     /**
  44.      * @return string
  45.      */
  46.     public function getTemplateCode()
  47.     {
  48.         return $this->templateCode;
  49.     }
  50.     /**
  51.      * @param string $path
  52.      */
  53.     public function loadTemplateFromResource($path)
  54.     {
  55.         $templateFile $this->kernel->locateResource($path);
  56.         if (file_exists($templateFile)) {
  57.             $this->setTemplateCode(file_get_contents($templateFile));
  58.         }
  59.     }
  60.     /**
  61.      * @param RequestEvent $event
  62.      */
  63.     public function onKernelRequest(RequestEvent $event)
  64.     {
  65.         if (!$event->isMainRequest()) {
  66.             return;
  67.         }
  68.         $request $event->getRequest();
  69.         $maintenance false;
  70.         $file \Pimcore\Tool\Admin::getMaintenanceModeFile();
  71.         if (!is_file($file)) {
  72.             return;
  73.         }
  74.         $conf = include($file);
  75.         if (isset($conf['sessionId'])) {
  76.             $requestSessionId Session::getSessionId();
  77.             $maintenance true;
  78.             if ($conf['sessionId'] === $requestSessionId) {
  79.                 $maintenance false;
  80.             }
  81.         } else {
  82.             @unlink($file);
  83.         }
  84.         // do not activate the maintenance for the server itself
  85.         // this is to avoid problems with monitoring agents
  86.         $serverIps = ['127.0.0.1'];
  87.         if ($maintenance && !in_array($request->getClientIp(), $serverIps)) {
  88.             $response = new Response($this->getTemplateCode(), 503);
  89.             $event->setResponse($response);
  90.         }
  91.     }
  92. }