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