1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Pages; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use SmartyException; |
13
|
|
|
use Waca\DataObjects\Ban; |
14
|
|
|
use Waca\DataObjects\Domain; |
15
|
|
|
use Waca\DataObjects\Request; |
16
|
|
|
use Waca\DataObjects\RequestQueue; |
17
|
|
|
use Waca\DataObjects\User; |
18
|
|
|
use Waca\Exceptions\AccessDeniedException; |
19
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
20
|
|
|
use Waca\Helpers\BanHelper; |
21
|
|
|
use Waca\Helpers\Logger; |
22
|
|
|
use Waca\Helpers\SearchHelpers\UserSearchHelper; |
23
|
|
|
use Waca\SessionAlert; |
24
|
|
|
use Waca\Tasks\InternalPageBase; |
25
|
|
|
use Waca\WebRequest; |
26
|
|
|
|
27
|
|
|
class PageBan extends InternalPageBase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Main function for this page, when no specific actions are called. |
31
|
|
|
*/ |
32
|
|
|
protected function main(): void |
33
|
|
|
{ |
34
|
|
|
$this->assignCSRFToken(); |
35
|
|
|
$this->setHtmlTitle('Bans'); |
36
|
|
|
|
37
|
|
|
$database = $this->getDatabase(); |
38
|
|
|
$currentDomain = Domain::getCurrent($database); |
39
|
|
|
$bans = Ban::getActiveBans($database, $currentDomain->getId()); |
40
|
|
|
|
41
|
|
|
$this->setupBanList($bans); |
42
|
|
|
|
43
|
|
|
$this->assign('isFiltered', false); |
44
|
|
|
$this->setTemplate('bans/main.tpl'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function show(): void |
48
|
|
|
{ |
49
|
|
|
$this->assignCSRFToken(); |
50
|
|
|
$this->setHtmlTitle('Bans'); |
51
|
|
|
|
52
|
|
|
$rawIdList = WebRequest::getString('id'); |
53
|
|
|
if ($rawIdList === null) { |
54
|
|
|
$this->redirect('bans'); |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$idList = explode(',', $rawIdList); |
60
|
|
|
|
61
|
|
|
$database = $this->getDatabase(); |
62
|
|
|
$currentDomain = Domain::getCurrent($database); |
63
|
|
|
$bans = Ban::getByIdList($idList, $database, $currentDomain->getId()); |
64
|
|
|
|
65
|
|
|
$this->setupBanList($bans); |
66
|
|
|
$this->assign('isFiltered', true); |
67
|
|
|
$this->setTemplate('bans/main.tpl'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Entry point for the ban set action |
72
|
|
|
* @throws SmartyException |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
protected function set(): void |
76
|
|
|
{ |
77
|
|
|
$this->setHtmlTitle('Bans'); |
78
|
|
|
|
79
|
|
|
// dual-mode action |
80
|
|
|
if (WebRequest::wasPosted()) { |
81
|
|
|
try { |
82
|
|
|
$this->handlePostMethodForSetBan(); |
83
|
|
|
} |
84
|
|
|
catch (ApplicationLogicException $ex) { |
85
|
|
|
SessionAlert::error($ex->getMessage()); |
86
|
|
|
$this->redirect("bans", "set"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
else { |
90
|
|
|
$this->handleGetMethodForSetBan(); |
91
|
|
|
|
92
|
|
|
$user = User::getCurrent($this->getDatabase()); |
93
|
|
|
$banType = WebRequest::getString('type'); |
94
|
|
|
$banRequest = WebRequest::getInt('request'); |
95
|
|
|
|
96
|
|
|
// if the parameters are null, skip loading a request. |
97
|
|
|
if ($banType !== null && $banRequest !== null && $banRequest !== 0) { |
98
|
|
|
$this->preloadFormForRequest($banRequest, $banType, $user); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function replace(): void |
104
|
|
|
{ |
105
|
|
|
$this->setHtmlTitle('Bans'); |
106
|
|
|
|
107
|
|
|
$database = $this->getDatabase(); |
108
|
|
|
$domain = Domain::getCurrent($database); |
109
|
|
|
|
110
|
|
|
// dual-mode action |
111
|
|
|
if (WebRequest::wasPosted()) { |
112
|
|
|
try { |
113
|
|
|
$originalBanId = WebRequest::postInt('replaceBanId'); |
114
|
|
|
$originalBanUpdateVersion = WebRequest::postInt('replaceBanUpdateVersion'); |
115
|
|
|
|
116
|
|
|
$originalBan = Ban::getActiveId($originalBanId, $database, $domain->getId()); |
117
|
|
|
|
118
|
|
|
if ($originalBan === false) { |
119
|
|
|
throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Discard original ban; we're replacing it. |
123
|
|
|
$originalBan->setUpdateVersion($originalBanUpdateVersion); |
124
|
|
|
$originalBan->setActive(false); |
125
|
|
|
$originalBan->save(); |
126
|
|
|
|
127
|
|
|
Logger::banReplaced($database, $originalBan); |
128
|
|
|
|
129
|
|
|
// Proceed as normal to save the new ban. |
130
|
|
|
$this->handlePostMethodForSetBan(); |
131
|
|
|
} |
132
|
|
|
catch (ApplicationLogicException $ex) { |
133
|
|
|
$database->rollback(); |
134
|
|
|
SessionAlert::error($ex->getMessage()); |
135
|
|
|
$this->redirect("bans", "set"); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
else { |
139
|
|
|
$this->handleGetMethodForSetBan(); |
140
|
|
|
|
141
|
|
|
$user = User::getCurrent($database); |
142
|
|
|
$originalBanId = WebRequest::getString('id'); |
143
|
|
|
|
144
|
|
|
$originalBan = Ban::getActiveId($originalBanId, $database, $domain->getId()); |
145
|
|
|
|
146
|
|
|
if ($originalBan === false) { |
147
|
|
|
throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($originalBan->getName() !== null) { |
151
|
|
|
if (!$this->barrierTest('name', $user, 'BanType')) { |
152
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
153
|
|
|
$this->redirect("bans", "set"); |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->assign('banName', $originalBan->getName()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($originalBan->getEmail() !== null) { |
161
|
|
|
if (!$this->barrierTest('email', $user, 'BanType')) { |
162
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
163
|
|
|
$this->redirect("bans", "set"); |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->assign('banEmail', $originalBan->getEmail()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($originalBan->getUseragent() !== null) { |
171
|
|
|
if (!$this->barrierTest('useragent', $user, 'BanType')) { |
172
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
173
|
|
|
$this->redirect("bans", "set"); |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->assign('banUseragent', $originalBan->getUseragent()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($originalBan->getIp() !== null) { |
181
|
|
|
if (!$this->barrierTest('ip', $user, 'BanType')) { |
182
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
183
|
|
|
$this->redirect("bans", "set"); |
184
|
|
|
return; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->assign('banIP', $originalBan->getIp() . '/' . $originalBan->getIpMask()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$banIsGlobal = $originalBan->getDomain() === null; |
191
|
|
|
if ($banIsGlobal) { |
192
|
|
|
if (!$this->barrierTest('global', $user, 'BanType')) { |
193
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
194
|
|
|
$this->redirect("bans", "set"); |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (!$this->barrierTest($originalBan->getVisibility(), $user, 'BanVisibility')) { |
200
|
|
|
SessionAlert::error("You are not allowed to set this type of ban."); |
201
|
|
|
$this->redirect("bans", "set"); |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->assign('banGlobal', $banIsGlobal); |
206
|
|
|
$this->assign('banVisibility', $originalBan->getVisibility()); |
207
|
|
|
|
208
|
|
|
if ($originalBan->getDuration() !== null) { |
209
|
|
|
$this->assign('banDuration', date('c', $originalBan->getDuration())); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->assign('banReason', $originalBan->getReason()); |
213
|
|
|
$this->assign('banAction', $originalBan->getAction()); |
214
|
|
|
$this->assign('banQueue', $originalBan->getTargetQueue()); |
215
|
|
|
|
216
|
|
|
$this->assign('replaceBanId', $originalBan->getId()); |
217
|
|
|
$this->assign('replaceBanUpdateVersion', $originalBan->getUpdateVersion()); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Entry point for the ban remove action |
223
|
|
|
* |
224
|
|
|
* @throws AccessDeniedException |
225
|
|
|
* @throws ApplicationLogicException |
226
|
|
|
* @throws SmartyException |
227
|
|
|
*/ |
228
|
|
|
protected function remove(): void |
229
|
|
|
{ |
230
|
|
|
$this->setHtmlTitle('Bans'); |
231
|
|
|
|
232
|
|
|
$ban = $this->getBanForUnban(); |
233
|
|
|
|
234
|
|
|
$banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); |
235
|
|
|
if (!$banHelper->canUnban($ban)) { |
236
|
|
|
// triggered when a user tries to unban a ban they can't see the entirety of. |
237
|
|
|
// there's no UI way to get to this, so a raw exception is fine. |
238
|
|
|
throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// dual mode |
242
|
|
|
if (WebRequest::wasPosted()) { |
243
|
|
|
$this->validateCSRFToken(); |
244
|
|
|
$unbanReason = WebRequest::postString('unbanreason'); |
245
|
|
|
|
246
|
|
|
if ($unbanReason === null || trim($unbanReason) === "") { |
247
|
|
|
SessionAlert::error('No unban reason specified'); |
248
|
|
|
$this->redirect("bans", "remove", array('id' => $ban->getId())); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// set optimistic locking from delete form page load |
252
|
|
|
$updateVersion = WebRequest::postInt('updateversion'); |
253
|
|
|
$ban->setUpdateVersion($updateVersion); |
254
|
|
|
|
255
|
|
|
$database = $this->getDatabase(); |
256
|
|
|
$ban->setActive(false); |
257
|
|
|
$ban->save(); |
258
|
|
|
|
259
|
|
|
Logger::unbanned($database, $ban, $unbanReason); |
260
|
|
|
|
261
|
|
|
SessionAlert::quick('Disabled ban.'); |
262
|
|
|
$this->getNotificationHelper()->unbanned($ban, $unbanReason); |
263
|
|
|
|
264
|
|
|
$this->redirect('bans'); |
265
|
|
|
} |
266
|
|
|
else { |
267
|
|
|
$this->assignCSRFToken(); |
268
|
|
|
$this->assign('ban', $ban); |
269
|
|
|
$this->setTemplate('bans/unban.tpl'); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Retrieves the requested ban duration from the WebRequest |
275
|
|
|
* |
276
|
|
|
* @throws ApplicationLogicException |
277
|
|
|
*/ |
278
|
|
|
private function getBanDuration(): ?int |
279
|
|
|
{ |
280
|
|
|
$duration = WebRequest::postString('duration'); |
281
|
|
|
if ($duration === "other") { |
282
|
|
|
$duration = strtotime(WebRequest::postString('otherduration')); |
|
|
|
|
283
|
|
|
|
284
|
|
|
if (!$duration) { |
285
|
|
|
throw new ApplicationLogicException('Invalid ban time'); |
286
|
|
|
} |
287
|
|
|
elseif (time() > $duration) { |
288
|
|
|
throw new ApplicationLogicException('Ban time has already expired!'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return $duration; |
292
|
|
|
} |
293
|
|
|
elseif ($duration === "-1") { |
294
|
|
|
return null; |
295
|
|
|
} |
296
|
|
|
else { |
297
|
|
|
return WebRequest::postInt('duration') + time(); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Handles the POST method on the set action |
303
|
|
|
* |
304
|
|
|
* @throws ApplicationLogicException |
305
|
|
|
* @throws Exception |
306
|
|
|
*/ |
307
|
|
|
private function handlePostMethodForSetBan() |
308
|
|
|
{ |
309
|
|
|
$this->validateCSRFToken(); |
310
|
|
|
$database = $this->getDatabase(); |
311
|
|
|
$user = User::getCurrent($database); |
312
|
|
|
$currentDomain = Domain::getCurrent($database); |
313
|
|
|
|
314
|
|
|
// Checks whether there is a reason entered for ban. |
315
|
|
|
$reason = WebRequest::postString('banreason'); |
316
|
|
|
if ($reason === null || trim($reason) === "") { |
317
|
|
|
throw new ApplicationLogicException('You must specify a ban reason'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// ban targets |
321
|
|
|
list($targetName, $targetIp, $targetEmail, $targetUseragent) = $this->getRawBanTargets($user); |
322
|
|
|
|
323
|
|
|
$visibility = $this->getBanVisibility(); |
324
|
|
|
|
325
|
|
|
// Validate ban duration |
326
|
|
|
$duration = $this->getBanDuration(); |
327
|
|
|
|
328
|
|
|
$action = WebRequest::postString('banAction') ?? Ban::ACTION_NONE; |
329
|
|
|
|
330
|
|
|
$global = WebRequest::postBoolean('banGlobal'); |
331
|
|
|
if (!$this->barrierTest('global', $user, 'BanType')) { |
332
|
|
|
$global = false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
if ($action === Ban::ACTION_DEFER && $global) { |
336
|
|
|
throw new ApplicationLogicException("Cannot set a global ban in defer-to-queue mode."); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// handle CIDR ranges |
340
|
|
|
$targetMask = null; |
341
|
|
|
if ($targetIp !== null) { |
342
|
|
|
list($targetIp, $targetMask) = $this->splitCidrRange($targetIp); |
343
|
|
|
$this->validateIpBan($targetIp, $targetMask, $user, $action); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); |
347
|
|
|
|
348
|
|
|
$bansByTarget = $banHelper->getBansByTarget( |
349
|
|
|
$targetName, |
350
|
|
|
$targetEmail, |
351
|
|
|
$targetIp, |
352
|
|
|
$targetMask, |
353
|
|
|
$targetUseragent, |
354
|
|
|
$currentDomain->getId()); |
355
|
|
|
|
356
|
|
|
if (count($bansByTarget) > 0) { |
357
|
|
|
throw new ApplicationLogicException('This target is already banned!'); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$ban = new Ban(); |
361
|
|
|
$ban->setDatabase($database); |
362
|
|
|
$ban->setActive(true); |
363
|
|
|
|
364
|
|
|
$ban->setName($targetName); |
365
|
|
|
$ban->setIp($targetIp, $targetMask); |
366
|
|
|
$ban->setEmail($targetEmail); |
367
|
|
|
$ban->setUseragent($targetUseragent); |
368
|
|
|
|
369
|
|
|
$ban->setUser($user->getId()); |
370
|
|
|
$ban->setReason($reason); |
371
|
|
|
$ban->setDuration($duration); |
372
|
|
|
$ban->setVisibility($visibility); |
373
|
|
|
|
374
|
|
|
$ban->setDomain($global ? null : $currentDomain->getId()); |
375
|
|
|
|
376
|
|
|
$ban->setAction($action); |
377
|
|
|
if ($ban->getAction() === Ban::ACTION_DEFER) { |
378
|
|
|
//FIXME: domains |
379
|
|
|
$queue = RequestQueue::getByApiName($database, WebRequest::postString('banActionTarget'), 1); |
|
|
|
|
380
|
|
|
if ($queue === false) { |
381
|
|
|
throw new ApplicationLogicException("Unknown target queue"); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
if (!$queue->isEnabled()) { |
385
|
|
|
throw new ApplicationLogicException("Target queue is not enabled"); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$ban->setTargetQueue($queue->getId()); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$ban->save(); |
392
|
|
|
|
393
|
|
|
Logger::banned($database, $ban, $reason); |
394
|
|
|
|
395
|
|
|
$this->getNotificationHelper()->banned($ban); |
396
|
|
|
SessionAlert::quick('Ban has been set.'); |
397
|
|
|
|
398
|
|
|
$this->redirect('bans'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Handles the GET method on the set action |
403
|
|
|
* @throws Exception |
404
|
|
|
*/ |
405
|
|
|
private function handleGetMethodForSetBan() |
406
|
|
|
{ |
407
|
|
|
$this->setTemplate('bans/banform.tpl'); |
408
|
|
|
$this->assignCSRFToken(); |
409
|
|
|
|
410
|
|
|
$this->assign('maxIpRange', $this->getSiteConfiguration()->getBanMaxIpRange()); |
411
|
|
|
$this->assign('maxIpBlockRange', $this->getSiteConfiguration()->getBanMaxIpBlockRange()); |
412
|
|
|
|
413
|
|
|
$this->assign('banVisibility', 'user'); |
414
|
|
|
$this->assign('banGlobal', false); |
415
|
|
|
$this->assign('banQueue', false); |
416
|
|
|
$this->assign('banAction', Ban::ACTION_BLOCK); |
417
|
|
|
$this->assign('banDuration', ''); |
418
|
|
|
$this->assign('banReason', ''); |
419
|
|
|
|
420
|
|
|
$this->assign('banEmail', ''); |
421
|
|
|
$this->assign('banIP', ''); |
422
|
|
|
$this->assign('banName', ''); |
423
|
|
|
$this->assign('banUseragent', ''); |
424
|
|
|
|
425
|
|
|
$this->assign('replaceBanId', null); |
426
|
|
|
|
427
|
|
|
|
428
|
|
|
|
429
|
|
|
$database = $this->getDatabase(); |
430
|
|
|
|
431
|
|
|
$user = User::getCurrent($database); |
432
|
|
|
$this->setupSecurity($user); |
433
|
|
|
|
434
|
|
|
$queues = RequestQueue::getEnabledQueues($database); |
435
|
|
|
|
436
|
|
|
$this->assign('requestQueues', $queues); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Finds the Ban object referenced in the WebRequest if it is valid |
441
|
|
|
* |
442
|
|
|
* @return Ban |
443
|
|
|
* @throws ApplicationLogicException |
444
|
|
|
*/ |
445
|
|
|
private function getBanForUnban(): Ban |
446
|
|
|
{ |
447
|
|
|
$banId = WebRequest::getInt('id'); |
448
|
|
|
if ($banId === null || $banId === 0) { |
449
|
|
|
throw new ApplicationLogicException("The ban ID appears to be missing. This is probably a bug."); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$database = $this->getDatabase(); |
453
|
|
|
$this->setupSecurity(User::getCurrent($database)); |
454
|
|
|
$currentDomain = Domain::getCurrent($database); |
455
|
|
|
$ban = Ban::getActiveId($banId, $database, $currentDomain->getId()); |
456
|
|
|
|
457
|
|
|
if ($ban === false) { |
458
|
|
|
throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return $ban; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Sets up Smarty variables for access control |
466
|
|
|
*/ |
467
|
|
|
private function setupSecurity(User $user): void |
468
|
|
|
{ |
469
|
|
|
$this->assign('canSeeIpBan', $this->barrierTest('ip', $user, 'BanType')); |
470
|
|
|
$this->assign('canSeeNameBan', $this->barrierTest('name', $user, 'BanType')); |
471
|
|
|
$this->assign('canSeeEmailBan', $this->barrierTest('email', $user, 'BanType')); |
472
|
|
|
$this->assign('canSeeUseragentBan', $this->barrierTest('useragent', $user, 'BanType')); |
473
|
|
|
|
474
|
|
|
$this->assign('canGlobalBan', $this->barrierTest('global', $user, 'BanType')); |
475
|
|
|
|
476
|
|
|
$this->assign('canSeeUserVisibility', $this->barrierTest('user', $user, 'BanVisibility')); |
477
|
|
|
$this->assign('canSeeAdminVisibility', $this->barrierTest('admin', $user, 'BanVisibility')); |
478
|
|
|
$this->assign('canSeeCheckuserVisibility', $this->barrierTest('checkuser', $user, 'BanVisibility')); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Validates that the provided IP is acceptable for a ban of this type |
483
|
|
|
* |
484
|
|
|
* @param string $targetIp IP address |
485
|
|
|
* @param int $targetMask CIDR prefix length |
486
|
|
|
* @param User $user User performing the ban |
487
|
|
|
* @param string $action Ban action to take |
488
|
|
|
* |
489
|
|
|
* @throws ApplicationLogicException |
490
|
|
|
*/ |
491
|
|
|
private function validateIpBan(string $targetIp, int $targetMask, User $user, string $action): void |
492
|
|
|
{ |
493
|
|
|
// validate this is an IP |
494
|
|
|
if (!filter_var($targetIp, FILTER_VALIDATE_IP)) { |
495
|
|
|
throw new ApplicationLogicException("Not a valid IP address"); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
$canLargeIpBan = $this->barrierTest('ip-largerange', $user, 'BanType'); |
499
|
|
|
$maxIpBlockRange = $this->getSiteConfiguration()->getBanMaxIpBlockRange(); |
500
|
|
|
$maxIpRange = $this->getSiteConfiguration()->getBanMaxIpRange(); |
501
|
|
|
|
502
|
|
|
// validate CIDR ranges |
503
|
|
|
if (filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
504
|
|
|
if ($targetMask < 0 || $targetMask > 128) { |
505
|
|
|
throw new ApplicationLogicException("CIDR mask out of range for IPv6"); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
// prevent setting the ban if: |
509
|
|
|
// * the user isn't allowed to set large bans, AND |
510
|
|
|
// * the ban is a drop or a block (preventing human review of the request), AND |
511
|
|
|
// * the mask is too wide-reaching |
512
|
|
|
if (!$canLargeIpBan && ($action == Ban::ACTION_BLOCK || $action == Ban::ACTION_DROP) && $targetMask < $maxIpBlockRange[6]) { |
513
|
|
|
throw new ApplicationLogicException("The requested IP range for this ban is too wide for the block/drop action."); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
if (!$canLargeIpBan && $targetMask < $maxIpRange[6]) { |
517
|
|
|
throw new ApplicationLogicException("The requested IP range for this ban is too wide."); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
if (filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
522
|
|
|
if ($targetMask < 0 || $targetMask > 32) { |
523
|
|
|
throw new ApplicationLogicException("CIDR mask out of range for IPv4"); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if (!$canLargeIpBan && ($action == Ban::ACTION_BLOCK || $action == Ban::ACTION_DROP) && $targetMask < $maxIpBlockRange[4]) { |
527
|
|
|
throw new ApplicationLogicException("The IP range for this ban is too wide for the block/drop action."); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
if (!$canLargeIpBan && $targetMask < $maxIpRange[4]) { |
531
|
|
|
throw new ApplicationLogicException("The requested IP range for this ban is too wide."); |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$squidIpList = $this->getSiteConfiguration()->getSquidList(); |
536
|
|
|
if (in_array($targetIp, $squidIpList)) { |
537
|
|
|
throw new ApplicationLogicException("This IP address is on the protected list of proxies, and cannot be banned."); |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Configures a ban list template for display |
543
|
|
|
* |
544
|
|
|
* @param Ban[] $bans |
545
|
|
|
*/ |
546
|
|
|
private function setupBanList(array $bans): void |
547
|
|
|
{ |
548
|
|
|
$userIds = array_map(fn(Ban $entry) => $entry->getUser(), $bans); |
549
|
|
|
$userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); |
550
|
|
|
|
551
|
|
|
$domainIds = array_filter(array_unique(array_map(fn(Ban $entry) => $entry->getDomain(), $bans))); |
552
|
|
|
$domains = []; |
553
|
|
|
foreach ($domainIds as $d) { |
554
|
|
|
if ($d === null) { |
555
|
|
|
continue; |
556
|
|
|
} |
557
|
|
|
$domains[$d] = Domain::getById($d, $this->getDatabase()); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$this->assign('domains', $domains); |
561
|
|
|
|
562
|
|
|
$user = User::getCurrent($this->getDatabase()); |
563
|
|
|
$this->assign('canSet', $this->barrierTest('set', $user)); |
564
|
|
|
$this->assign('canRemove', $this->barrierTest('remove', $user)); |
565
|
|
|
|
566
|
|
|
$this->setupSecurity($user); |
567
|
|
|
|
568
|
|
|
$this->assign('usernames', $userList); |
569
|
|
|
$this->assign('activebans', $bans); |
570
|
|
|
|
571
|
|
|
$banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); |
572
|
|
|
$this->assign('banHelper', $banHelper); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Converts a plain IP or CIDR mask into an IP and a CIDR suffix |
577
|
|
|
* |
578
|
|
|
* @param string $targetIp IP or CIDR range |
579
|
|
|
* |
580
|
|
|
* @return array |
581
|
|
|
*/ |
582
|
|
|
private function splitCidrRange(string $targetIp): array |
583
|
|
|
{ |
584
|
|
|
if (strpos($targetIp, '/') !== false) { |
585
|
|
|
$ipParts = explode('/', $targetIp, 2); |
586
|
|
|
$targetIp = $ipParts[0]; |
587
|
|
|
$targetMask = (int)$ipParts[1]; |
588
|
|
|
} |
589
|
|
|
else { |
590
|
|
|
// Default the CIDR range based on the IP type |
591
|
|
|
$targetMask = filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 128 : 32; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
return array($targetIp, $targetMask); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Returns the validated ban visibility from WebRequest |
599
|
|
|
* |
600
|
|
|
* @throws ApplicationLogicException |
601
|
|
|
*/ |
602
|
|
|
private function getBanVisibility(): string |
603
|
|
|
{ |
604
|
|
|
$visibility = WebRequest::postString('banVisibility'); |
605
|
|
|
if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { |
606
|
|
|
throw new ApplicationLogicException('Invalid ban visibility'); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
return $visibility; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Returns array of [username, ip, email, ua] as ban targets from WebRequest, |
614
|
|
|
* filtered for whether the user is allowed to set bans including those types. |
615
|
|
|
* |
616
|
|
|
* @return string[] |
617
|
|
|
* @throws ApplicationLogicException |
618
|
|
|
*/ |
619
|
|
|
private function getRawBanTargets(User $user): array |
620
|
|
|
{ |
621
|
|
|
$targetName = WebRequest::postString('banName'); |
622
|
|
|
$targetIp = WebRequest::postString('banIP'); |
623
|
|
|
$targetEmail = WebRequest::postString('banEmail'); |
624
|
|
|
$targetUseragent = WebRequest::postString('banUseragent'); |
625
|
|
|
|
626
|
|
|
// check the user is allowed to use provided targets |
627
|
|
|
if (!$this->barrierTest('name', $user, 'BanType')) { |
628
|
|
|
$targetName = null; |
629
|
|
|
} |
630
|
|
|
if (!$this->barrierTest('ip', $user, 'BanType')) { |
631
|
|
|
$targetIp = null; |
632
|
|
|
} |
633
|
|
|
if (!$this->barrierTest('email', $user, 'BanType')) { |
634
|
|
|
$targetEmail = null; |
635
|
|
|
} |
636
|
|
|
if (!$this->barrierTest('useragent', $user, 'BanType')) { |
637
|
|
|
$targetUseragent = null; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
// Checks whether there is a target entered to ban. |
641
|
|
|
if ($targetName === null && $targetIp === null && $targetEmail === null && $targetUseragent === null) { |
642
|
|
|
throw new ApplicationLogicException('You must specify a target to be banned'); |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
return array($targetName, $targetIp, $targetEmail, $targetUseragent); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
private function preloadFormForRequest(int $banRequest, string $banType, User $user): void |
649
|
|
|
{ |
650
|
|
|
$database = $this->getDatabase(); |
651
|
|
|
|
652
|
|
|
// Attempt to resolve the correct target |
653
|
|
|
/** @var Request|false $request */ |
654
|
|
|
$request = Request::getById($banRequest, $database); |
655
|
|
|
if ($request === false) { |
|
|
|
|
656
|
|
|
$this->assign('bantarget', ''); |
657
|
|
|
|
658
|
|
|
return; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
switch ($banType) { |
662
|
|
|
case 'EMail': |
663
|
|
|
if ($this->barrierTest('email', $user, 'BanType')) { |
664
|
|
|
$this->assign('banEmail', $request->getEmail()); |
665
|
|
|
} |
666
|
|
|
break; |
667
|
|
|
case 'IP': |
668
|
|
|
if ($this->barrierTest('ip', $user, 'BanType')) { |
669
|
|
|
$trustedIp = $this->getXffTrustProvider()->getTrustedClientIp( |
670
|
|
|
$request->getIp(), |
671
|
|
|
$request->getForwardedIp()); |
672
|
|
|
|
673
|
|
|
$this->assign('banIP', $trustedIp); |
674
|
|
|
} |
675
|
|
|
break; |
676
|
|
|
case 'Name': |
677
|
|
|
if ($this->barrierTest('name', $user, 'BanType')) { |
678
|
|
|
$this->assign('banName', $request->getName()); |
679
|
|
|
} |
680
|
|
|
break; |
681
|
|
|
case 'UA': |
682
|
|
|
if ($this->barrierTest('useragent', $user, 'BanType')) { |
683
|
|
|
$this->assign('banUseragent', $request->getEmail()); |
684
|
|
|
} |
685
|
|
|
break; |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
|