Total Complexity | 104 |
Total Lines | 659 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like PageBan often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageBan, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
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() |
||
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 |
||
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 |
||
686 | } |
||
687 | } |
||
688 | } |
||
689 |