vendor/lexik/maintenance-bundle/Listener/MaintenanceListener.php line 152

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\MaintenanceBundle\Listener;
  3. use Lexik\Bundle\MaintenanceBundle\Drivers\DriverFactory;
  4. use Lexik\Bundle\MaintenanceBundle\Exception\ServiceUnavailableException;
  5. use Symfony\Component\HttpKernel\HttpKernelInterface;
  6. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  7. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  8. use Symfony\Component\HttpFoundation\IpUtils;
  9. /**
  10.  * Listener to decide if user can access to the site
  11.  *
  12.  * @package LexikMaintenanceBundle
  13.  * @author  Gilles Gauthier <g.gauthier@lexik.fr>
  14.  */
  15. class MaintenanceListener
  16. {
  17.     /**
  18.      * Service driver factory
  19.      *
  20.      * @var \Lexik\Bundle\MaintenanceBundle\Drivers\DriverFactory
  21.      */
  22.     protected $driverFactory;
  23.     /**
  24.      * Authorized data
  25.      *
  26.      * @var array
  27.      */
  28.     protected $authorizedIps;
  29.     /**
  30.      * @var null|String
  31.      */
  32.     protected $path;
  33.     /**
  34.      * @var null|String
  35.      */
  36.     protected $host;
  37.     /**
  38.      * @var array|null
  39.      */
  40.     protected $ips;
  41.     /**
  42.      * @var array
  43.      */
  44.     protected $query;
  45.     /**
  46.      * @var array
  47.      */
  48.     protected $cookie;
  49.     /**
  50.      * @var null|String
  51.      */
  52.     protected $route;
  53.     /**
  54.      * @var array
  55.      */
  56.     protected $attributes;
  57.     /**
  58.      * @var Int|null
  59.      */
  60.     protected $http_code;
  61.     /**
  62.      * @var null|String
  63.      */
  64.     protected $http_status;
  65.     /**
  66.      * @var null|String
  67.      */
  68.     protected $http_exception_message;
  69.     /**
  70.      * @var bool
  71.      */
  72.     protected $handleResponse false;
  73.     /**
  74.      * @var bool
  75.      */
  76.     protected $debug;
  77.     /**
  78.      * Constructor Listener
  79.      *
  80.      * Accepts a driver factory, and several arguments to be compared against the
  81.      * incoming request.
  82.      * When the maintenance mode is enabled, the request will be allowed to bypass
  83.      * it if at least one of the provided arguments is not empty and matches the
  84.      *  incoming request.
  85.      *
  86.      * @param DriverFactory $driverFactory The driver factory
  87.      * @param String $path A regex for the path
  88.      * @param String $host A regex for the host
  89.      * @param array $ips The list of IP addresses
  90.      * @param array $query Query arguments
  91.      * @param array $cookie Cookies
  92.      * @param String $route Route name
  93.      * @param array $attributes Attributes
  94.      * @param Int $http_code http status code for response
  95.      * @param String $http_status http status message for response
  96.      * @param null $http_exception_message http response page exception message
  97.      * @param bool $debug
  98.      */
  99.     public function __construct(
  100.         DriverFactory $driverFactory,
  101.         $path null,
  102.         $host null,
  103.         $ips null,
  104.         $query = array(),
  105.         $cookie = array(),
  106.         $route null,
  107.         $attributes = array(),
  108.         $http_code null,
  109.         $http_status null,
  110.         $http_exception_message null,
  111.         $debug false
  112.     ) {
  113.         $this->driverFactory $driverFactory;
  114.         $this->path $path;
  115.         $this->host $host;
  116.         $this->ips $ips;
  117.         $this->query $query;
  118.         $this->cookie $cookie;
  119.         $this->route $route;
  120.         $this->attributes $attributes;
  121.         $this->http_code $http_code;
  122.         $this->http_status $http_status;
  123.         $this->http_exception_message $http_exception_message;
  124.         $this->debug $debug;
  125.     }
  126.     /**
  127.      * @param GetResponseEvent $event GetResponseEvent
  128.      *
  129.      * @return void
  130.      *
  131.      * @throws ServiceUnavailableException
  132.      */
  133.     public function onKernelRequest(GetResponseEvent $event)
  134.     {
  135.         if(!$event->isMasterRequest()){
  136.             return;
  137.         }
  138.         $request $event->getRequest();
  139.         if (is_array($this->query)) {
  140.             foreach ($this->query as $key => $pattern) {
  141.                 if (!empty($pattern) && preg_match('{'.$pattern.'}'$request->get($key))) {
  142.                     return;
  143.                 }
  144.             }
  145.         }
  146.         if (is_array($this->cookie)) {
  147.             foreach ($this->cookie as $key => $pattern) {
  148.                 if (!empty($pattern) && preg_match('{'.$pattern.'}'$request->cookies->get($key))) {
  149.                     return;
  150.                 }
  151.             }
  152.         }
  153.         if (is_array($this->attributes)) {
  154.             foreach ($this->attributes as $key => $pattern) {
  155.                 if (!empty($pattern) && preg_match('{'.$pattern.'}'$request->attributes->get($key))) {
  156.                     return;
  157.                 }
  158.             }
  159.         }
  160.         if (null !== $this->path && !empty($this->path) && preg_match('{'.$this->path.'}'rawurldecode($request->getPathInfo()))) {
  161.             return;
  162.         }
  163.         if (null !== $this->host && !empty($this->host) && preg_match('{'.$this->host.'}i'$request->getHost())) {
  164.             return;
  165.         }
  166.         if (count((array) $this->ips) !== && $this->checkIps($request->getClientIp(), $this->ips)) {
  167.             return;
  168.         }
  169.         $route $request->get('_route');
  170.         if (null !== $this->route && preg_match('{'.$this->route.'}'$route)  || (true === $this->debug && '_' === $route[0])) {
  171.             return;
  172.         }
  173.         // Get driver class defined in your configuration
  174.         $driver $this->driverFactory->getDriver();
  175.         if ($driver->decide() && HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
  176.             $this->handleResponse true;
  177.             throw new ServiceUnavailableException($this->http_exception_message);
  178.         }
  179.     }
  180.     /**
  181.      * Rewrites the http code of the response
  182.      *
  183.      * @param FilterResponseEvent $event FilterResponseEvent
  184.      * @return void
  185.      */
  186.     public function onKernelResponse(FilterResponseEvent $event)
  187.     {
  188.         if ($this->handleResponse && $this->http_code !== null) {
  189.             $response $event->getResponse();
  190.             $response->setStatusCode($this->http_code$this->http_status);
  191.         }
  192.     }
  193.     /**
  194.      * Checks if the requested ip is valid.
  195.      *
  196.      * @param string       $requestedIp
  197.      * @param string|array $ips
  198.      * @return boolean
  199.      */
  200.     protected function checkIps($requestedIp$ips)
  201.     {
  202.         $ips = (array) $ips;
  203.         $valid false;
  204.         $i 0;
  205.         while ($i<count($ips) && !$valid) {
  206.             $valid IpUtils::checkIp($requestedIp$ips[$i]);
  207.             $i++;
  208.         }
  209.         return $valid;
  210.     }
  211. }