1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Storefront\Framework\Routing; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Framework\Log\Package; |
6
|
|
|
use Shopware\Core\Framework\Routing\MaintenanceModeResolver as CoreMaintenanceModeResolver; |
7
|
|
|
use Shopware\Core\Framework\Util\Json; |
8
|
|
|
use Shopware\Core\PlatformRequest; |
9
|
|
|
use Shopware\Core\SalesChannelRequest; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
|
13
|
|
|
#[Package('storefront')] |
14
|
|
|
class MaintenanceModeResolver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RequestStack |
18
|
|
|
*/ |
19
|
|
|
protected $requestStack; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CoreMaintenanceModeResolver |
23
|
|
|
*/ |
24
|
|
|
protected $maintenanceModeResolver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @internal |
28
|
|
|
*/ |
29
|
|
|
public function __construct(RequestStack $requestStack, CoreMaintenanceModeResolver $maintenanceModeResolver) |
30
|
|
|
{ |
31
|
|
|
$this->requestStack = $requestStack; |
32
|
|
|
$this->maintenanceModeResolver = $maintenanceModeResolver; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* shouldRedirect returns true, when the given request should be redirected to the maintenance page. |
37
|
|
|
* This would be the case, for example, when the maintenance mode is active and the client's IP address |
38
|
|
|
* is not listed in the maintenance mode whitelist. |
39
|
|
|
*/ |
40
|
|
|
public function shouldRedirect(Request $request): bool |
41
|
|
|
{ |
42
|
|
|
return $this->isSalesChannelRequest() |
43
|
|
|
&& !$request->attributes->getBoolean(PlatformRequest::ATTRIBUTE_IS_ALLOWED_IN_MAINTENANCE) |
44
|
|
|
&& !$this->isXmlHttpRequest($request) |
45
|
|
|
&& !$this->isErrorControllerRequest($request) |
46
|
|
|
&& $this->isMaintenanceRequest($request); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* shouldRedirectToShop returns true, when the given request to the maintenance page should be redirected to the shop. |
51
|
|
|
* This would be the case, for example, when the maintenance mode is not active or if it is active |
52
|
|
|
* the client's IP address is listed in the maintenance mode whitelist. |
53
|
|
|
*/ |
54
|
|
|
public function shouldRedirectToShop(Request $request): bool |
55
|
|
|
{ |
56
|
|
|
return !$this->isXmlHttpRequest($request) |
57
|
|
|
&& !$this->isErrorControllerRequest($request) |
58
|
|
|
&& !$this->isMaintenanceRequest($request); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function shouldBeCached(Request $request): bool |
62
|
|
|
{ |
63
|
|
|
return !$this->isMaintenanceModeActive() || !$this->isClientAllowed($request); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* isMaintenanceRequest returns true, when the maintenance mode is active and the client's IP address |
68
|
|
|
* is not listed in the maintenance mode whitelist. |
69
|
|
|
*/ |
70
|
|
|
public function isMaintenanceRequest(Request $request): bool |
71
|
|
|
{ |
72
|
|
|
return $this->isMaintenanceModeActive() && !$this->isClientAllowed($request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function isSalesChannelRequest(): bool |
76
|
|
|
{ |
77
|
|
|
$main = $this->requestStack->getMainRequest(); |
78
|
|
|
|
79
|
|
|
return (bool) $main?->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function isXmlHttpRequest(Request $request): bool |
83
|
|
|
{ |
84
|
|
|
return $request->isXmlHttpRequest(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function isErrorControllerRequest(Request $request): bool |
88
|
|
|
{ |
89
|
|
|
return $request->attributes->get('_route') === null |
90
|
|
|
&& $request->attributes->get('_controller') === 'error_controller'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function isMaintenanceModeActive(): bool |
94
|
|
|
{ |
95
|
|
|
$main = $this->requestStack->getMainRequest(); |
96
|
|
|
|
97
|
|
|
return (bool) $main?->attributes->get(SalesChannelRequest::ATTRIBUTE_SALES_CHANNEL_MAINTENANCE); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function isClientAllowed(Request $request): bool |
101
|
|
|
{ |
102
|
|
|
$main = $this->requestStack->getMainRequest(); |
103
|
|
|
$whitelist = $main?->attributes->get(SalesChannelRequest::ATTRIBUTE_SALES_CHANNEL_MAINTENANCE_IP_WHITLELIST) ?? ''; |
104
|
|
|
|
105
|
|
|
/** @var string[] $allowedIps */ |
106
|
|
|
$allowedIps = Json::decodeToList((string) $whitelist); |
107
|
|
|
|
108
|
|
|
return $this->maintenanceModeResolver->isClientAllowed($request, $allowedIps); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|