-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMaintenance.php
More file actions
317 lines (269 loc) · 9.21 KB
/
Maintenance.php
File metadata and controls
317 lines (269 loc) · 9.21 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace FriendsOfREDAXO\Maintenance;
use rex;
use rex_addon;
use rex_article;
use rex_backend_login;
use rex_clang;
use rex_extension;
use rex_extension_point;
use rex_fragment;
use rex_login;
use rex_response;
use rex_user;
use rex_yrewrite;
use function in_array;
use const FILTER_VALIDATE_IP;
use const FILTER_VALIDATE_URL;
/**
* Class Maintenance.
* @package FriendsOfREDAXO\Maintenance
*/
class Maintenance
{
private static ?rex_addon $addon = null;
/**
* Gets the addon instance.
*/
private static function getAddOn(): rex_addon
{
if (null === self::$addon) {
self::$addon = rex_addon::get('maintenance');
}
return self::$addon;
}
/**
* Checks if a URL is valid.
* @api
*/
public function checkUrl(string $url): ?bool
{
if ('' !== $url) {
if (false === filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
return true;
}
return null;
}
/**
* Checks if an IP address is valid.
* @api
*/
public function checkIp(string $ip): ?bool
{
if ('' !== $ip && false === filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
return true;
}
/**
* Checks if the current IP is allowed.
* @api
*/
public static function isIpAllowed(): bool
{
$ip = rex_server('REMOTE_ADDR', 'string', '');
$allowedIps = (string) self::getConfig('allowed_ips', '');
if ('' !== $allowedIps) {
$allowedIpsArray = explode(',', $allowedIps);
return in_array($ip, $allowedIpsArray, true);
}
return false;
}
/**
* Checks if the current host is allowed.
* @api
*/
public static function isHostAllowed(): bool
{
$host = rex_server('HTTP_HOST', 'string', '');
$allowedHosts = (string) self::getConfig('allowed_domains', '');
if ('' !== $allowedHosts) {
$allowedHostsArray = explode(',', $allowedHosts);
return in_array($host, $allowedHostsArray, true);
}
return false;
}
/**
* Checks if the current YRewrite domain is allowed.
* @api
*/
public static function isYrewriteDomainAllowed(): bool
{
if ($ydomain = rex_yrewrite::getDomainByArticleId(rex_article::getCurrentId(), rex_clang::getCurrentId())) {
$yrewrite_domain = $ydomain->getHost();
$allowedDomains = (string) self::getConfig('allowed_yrewrite_domains', '');
if ('' !== $allowedDomains) {
$allowedDomainsArray = explode('|', $allowedDomains);
return in_array($yrewrite_domain, $allowedDomainsArray, true);
}
}
return false;
}
/**
* Checks if the maintenance secret is valid.
* @api
*/
public static function isSecretAllowed(): bool
{
$config_secret = (string) self::getConfig('maintenance_secret', '');
// Check if already logged in with the correct secret
if ('' !== $config_secret && rex_session('maintenance_secret', 'string', '') === $config_secret) {
return true;
}
$maintenance_secret = rex_request('maintenance_secret', 'string', '');
$authentification_mode = (string) self::getConfig('authentification_mode', '');
// Authentifizierung prüfen - für URL-Parameter und auch bei leerem Modus
$authentification_mode = (string) self::getConfig('authentification_mode', '');
if (('' === $authentification_mode || 'URL' === $authentification_mode || 'password' === $authentification_mode)
&& '' !== $config_secret
&& $maintenance_secret === $config_secret) {
rex_set_session('maintenance_secret', $maintenance_secret);
return true;
}
rex_unset_session('maintenance_secret');
return false;
}
/**
* Checks if the current user is allowed.
* @api
*/
public static function isUserAllowed(): bool
{
rex_backend_login::createUser();
$user = rex::getUser();
// Admins always have access, regardless of settings
if ($user instanceof rex_user && $user->isAdmin()) {
return true;
}
// Check if the REDAXO user should be blocked
$block_frontend_rex_user = (bool) self::getConfig('block_frontend_rex_user', false);
// If the user is logged in and should not be blocked, allow access
if ($user instanceof rex_user && !$block_frontend_rex_user) {
return true;
}
return false;
}
/**
* Checks frontend access and shows maintenance page if necessary.
*/
public static function checkFrontend(): void
{
rex_login::startSession();
// If the IP address is allowed, do not block the request
if (self::isIpAllowed()) {
return;
}
// If YRewrite is installed and the domain is allowed, do not block the request
if (rex_addon::get('yrewrite')->isAvailable() && self::isYrewriteDomainAllowed()) {
return;
}
// If the host is allowed, do not block the request
if (self::isHostAllowed()) {
return;
}
// If the secret/password is correct, do not block the request
if (self::isSecretAllowed()) {
return;
}
// If the user is allowed (admin or non-blocked editor), do not block the request
if (self::isUserAllowed()) {
return;
}
// If the sitemap is requested, do not block the request
$REQUEST_URI = rex_server('REQUEST_URI', 'string', '');
if (true === str_contains($REQUEST_URI, 'sitemap.xml')) {
return;
}
// EP to allow media files
$media = rex_get('rex_media_file', 'string', '');
$media_unblock = [];
$media_unblocklist = rex_extension::registerPoint(new rex_extension_point('MAINTENANCE_MEDIA_UNBLOCK_LIST', $media_unblock));
if (in_array($media, $media_unblocklist, true)) {
return;
}
// Block everything that has not been allowed so far as chosen in the settings
$redirect_url = (string) self::getConfig('redirect_frontend_to_url', '');
$responsecode = (int) self::getConfig('http_response_code', 503);
$mpage = new rex_fragment();
if ('' !== $redirect_url) {
rex_response::setStatus(rex_response::HTTP_MOVED_TEMPORARILY);
rex_response::sendRedirect($redirect_url);
}
header('HTTP/1.1 ' . $responsecode);
exit($mpage->parse('maintenance/frontend.php'));
}
/**
* Checks backend access and shows maintenance page if necessary.
*/
public static function checkBackend(): void
{
if (rex::getUser() instanceof rex_user && !rex::getUser()->isAdmin() && !rex::getImpersonator()) {
$redirect_url = (string) self::getConfig('redirect_backend_to_url', '');
if ('' !== $redirect_url) {
rex_response::sendRedirect($redirect_url);
}
$mpage = new rex_fragment();
$responsecode = (int) self::getConfig('http_response_code', 503);
header('HTTP/1.1 ' . $responsecode);
exit($mpage->parse('maintenance/backend.php'));
}
}
/**
* Sets maintenance mode indicators in backend.
*/
public static function setIndicators(): void
{
$page = self::getAddOn()->getProperty('page');
if (self::getBoolConfig('block_backend', false)) {
$page['title'] .= ' <span class="label label-info pull-right">B</span>';
$page['icon'] .= ' fa-toggle-on block_backend';
self::getAddOn()->setProperty('page', $page);
}
if (self::getBoolConfig('block_frontend', false)) {
$page['title'] .= ' <span class="label label-danger pull-right">F</span>';
$page['icon'] .= ' fa-toggle-on block_frontend';
}
self::getAddOn()->setProperty('page', $page);
}
/**
* Shows maintenance announcement.
* @api
*/
public static function showAnnouncement(): void
{
echo self::getAnnouncement();
}
/**
* Gets maintenance announcement if within announcement period.
* @api
*/
public static function getAnnouncement(): string
{
$start_date = (string) self::getConfig('announcement_start_date', '');
if ('' !== $start_date) {
$start = strtotime($start_date);
$end = strtotime((string) self::getConfig('announcement_end_date', ''));
$now = time();
if ($start && $end && $now >= $start && $now <= $end) {
return (string) self::getConfig('announcement', '');
}
}
return '';
}
/**
* Gets config value with type casting.
*/
private static function getConfig(string $key, mixed $default = null): mixed
{
return self::getAddOn()->getConfig($key, $default);
}
/**
* Gets boolean config value.
*/
private static function getBoolConfig(string $key, bool $default = false): bool
{
return (bool) self::getConfig($key, $default);
}
}