| 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\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Waca\DataObjects\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Waca\Exceptions\AccessDeniedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Waca\Exceptions\ApplicationLogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Waca\Helpers\BanHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Waca\Helpers\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Waca\Helpers\SearchHelpers\UserSearchHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Waca\SessionAlert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Waca\Tasks\InternalPageBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Waca\WebRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class PageBan extends InternalPageBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Main function for this page, when no specific actions are called. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected function main() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assignCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->setHtmlTitle('Bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $bans = Ban::getActiveBans($this->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->setupBanList($bans); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->setTemplate('bans/main.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected function show() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->assignCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->setHtmlTitle('Bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $rawIdList = WebRequest::getString('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if ($rawIdList === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->redirect('bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $idList = explode(',',$rawIdList); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $bans = Ban::getByIdList($idList, $this->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->setupBanList($bans); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->setTemplate('bans/showbans.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Entry point for the ban set action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws SmartyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function set() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->setHtmlTitle('Bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // dual-mode action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (WebRequest::wasPosted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $this->handlePostMethodForSetBan(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             catch (ApplicationLogicException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 SessionAlert::error($ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $this->redirect("bans", "set"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->handleGetMethodForSetBan(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Entry point for the ban remove action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @throws AccessDeniedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @throws SmartyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     protected function remove() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->setHtmlTitle('Bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $ban = $this->getBanForUnban(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if (!$banHelper->canUnban($ban)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             // triggered when a user tries to unban a ban they can't see the entirety of. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // there's no UI way to get to this, so a raw exception is fine. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             throw new AccessDeniedException($this->getSecurityManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // dual mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (WebRequest::wasPosted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $this->validateCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $unbanReason = WebRequest::postString('unbanreason'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             if ($unbanReason === null || trim($unbanReason) === "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 SessionAlert::error('No unban reason specified'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $this->redirect("bans", "remove", array('id' => $ban->getId())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             // set optimistic locking from delete form page load | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $updateVersion = WebRequest::postInt('updateversion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $ban->setUpdateVersion($updateVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $ban->setActive(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $ban->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             Logger::unbanned($database, $ban, $unbanReason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             SessionAlert::quick('Disabled ban.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->getNotificationHelper()->unbanned($ban, $unbanReason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->redirect('bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $this->assignCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $this->assign('ban', $ban); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $this->setTemplate('bans/unban.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     private function getBanDuration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $duration = WebRequest::postString('duration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if ($duration === "other") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $duration = strtotime(WebRequest::postString('otherduration')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             if (!$duration) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 throw new ApplicationLogicException('Invalid ban time'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             elseif (time() > $duration) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 throw new ApplicationLogicException('Ban time has already expired!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return $duration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         elseif ($duration === "-1") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $duration = WebRequest::postInt('duration') + time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             return $duration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Handles the POST method on the set action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     private function handlePostMethodForSetBan() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $this->validateCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $user = User::getCurrent($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         // Checks whether there is a reason entered for ban. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $reason = WebRequest::postString('banreason'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         if ($reason === null || trim($reason) === "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             throw new ApplicationLogicException('You must specify a ban reason'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         // ban targets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $targetName = WebRequest::postString('banName'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $targetIp = WebRequest::postString('banIP'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $targetEmail = WebRequest::postString('banEmail'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $targetUseragent = WebRequest::postString('banUseragent'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $targetMask = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         // check the user is allowed to use provided targets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         if(!$this->barrierTest('name', $user, 'BanType')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             $targetName = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         if(!$this->barrierTest('ip', $user, 'BanType')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             $targetIp = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         if(!$this->barrierTest('email', $user, 'BanType')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             $targetEmail = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if(!$this->barrierTest('useragent', $user, 'BanType')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             $targetUseragent = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         // Checks whether there is a target entered to ban. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         if ($targetName === null && $targetIp === null && $targetEmail === null && $targetUseragent === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             throw new ApplicationLogicException('You must specify a target to be banned'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $visibility = WebRequest::postString('banVisibility'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |             throw new ApplicationLogicException('Invalid ban visibility'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         // Validate ban duration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         $duration = $this->getBanDuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         // handle CIDR ranges | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         if ($targetIp !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             if (strpos($targetIp, '/') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |                 $ipParts = explode('/', $targetIp, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |                 $targetIp = $ipParts[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |                 $targetMask = $ipParts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                 $targetMask = filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 128 : 32; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             $this->validateIpBan($targetIp, $targetMask); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         $banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         if (count($banHelper->getBansByTarget($targetName, $targetEmail, $targetIp, $targetMask, $targetUseragent)) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             throw new ApplicationLogicException('This target is already banned!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         $ban = new Ban(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         $ban->setDatabase($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         $ban->setActive(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         $ban->setName($targetName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         $ban->setIp($targetIp, $targetMask); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         $ban->setEmail($targetEmail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         $ban->setUseragent($targetUseragent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         $ban->setUser($user->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         $ban->setReason($reason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         $ban->setDuration($duration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         $ban->setVisibility($visibility); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         $ban->setAction(WebRequest::postString('banAction')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         if ($ban->getAction() === Ban::ACTION_DEFER) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             $ban->setActionTarget(WebRequest::postString('banActionTarget')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $ban->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         Logger::banned($database, $ban, $reason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         $this->getNotificationHelper()->banned($ban); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         SessionAlert::quick('Ban has been set.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         $this->redirect('bans'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |      * Handles the GET method on the set action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 267 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |     protected function handleGetMethodForSetBan() | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |         $this->setTemplate('bans/banform.tpl'); | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |         $this->assignCSRFToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |         $database = $this->getDatabase(); | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 275 |  |  |         $user = User::getCurrent($database); | 
            
                                                                        
                            
            
                                    
            
            
                | 276 |  |  |         $this->setupSecurity($user); | 
            
                                                                        
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 278 |  |  |         $this->assign('requestStates', $this->getSiteConfiguration()->getRequestStates()); | 
            
                                                                        
                            
            
                                    
            
            
                | 279 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |         $banType = WebRequest::getString('type'); | 
            
                                                                        
                            
            
                                    
            
            
                | 281 |  |  |         $banRequest = WebRequest::getInt('request'); | 
            
                                                                        
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 283 |  |  |         // if the parameters are null, skip loading a request. | 
            
                                                                        
                            
            
                                    
            
            
                | 284 |  |  |         if ($banType === null || $banRequest === null || $banRequest === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 285 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 286 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 288 |  |  |         // Attempt to resolve the correct target | 
            
                                                                        
                            
            
                                    
            
            
                | 289 |  |  |         /** @var Request $request */ | 
            
                                                                        
                            
            
                                    
            
            
                | 290 |  |  |         $request = Request::getById($banRequest, $database); | 
            
                                                                        
                            
            
                                    
            
            
                | 291 |  |  |         if ($request === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 292 |  |  |             $this->assign('bantarget', ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 294 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 295 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 296 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 297 |  |  |         switch ($banType) { | 
            
                                                                        
                            
            
                                    
            
            
                | 298 |  |  |             case 'EMail': | 
            
                                                                        
                            
            
                                    
            
            
                | 299 |  |  |                 if ($this->barrierTest('email', $user, 'BanType')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 300 |  |  |                     $this->assign('banEmail', $request->getEmail()); | 
            
                                                                        
                            
            
                                    
            
            
                | 301 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 302 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 303 |  |  |             case 'IP': | 
            
                                                                        
                            
            
                                    
            
            
                | 304 |  |  |                 if ($this->barrierTest('ip', $user, 'BanType')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 305 |  |  |                     $this->assign('banIP', $this->getXffTrustProvider() | 
            
                                                                        
                            
            
                                    
            
            
                | 306 |  |  |                         ->getTrustedClientIp($request->getIp(), $request->getForwardedIp())); | 
            
                                                                        
                            
            
                                    
            
            
                | 307 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 308 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 309 |  |  |             case 'Name': | 
            
                                                                        
                            
            
                                    
            
            
                | 310 |  |  |                 if ($this->barrierTest('username', $user, 'BanType')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 311 |  |  |                     $this->assign('banName', $request->getEmail()); | 
            
                                                                        
                            
            
                                    
            
            
                | 312 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 313 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 314 |  |  |             case 'UA': | 
            
                                                                        
                            
            
                                    
            
            
                | 315 |  |  |                 if ($this->barrierTest('useragent', $user, 'BanType')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 316 |  |  |                     $this->assign('banUseragent', $request->getEmail()); | 
            
                                                                        
                            
            
                                    
            
            
                | 317 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 318 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |      * @return Ban | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |     private function getBanForUnban() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         $banId = WebRequest::getInt('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |         if ($banId === null || $banId === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |             throw new ApplicationLogicException("The ban ID appears to be missing. This is probably a bug."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         $this->setupSecurity(User::getCurrent($database)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |         $ban = Ban::getActiveId($banId, $database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |         if ($ban === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |             throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |         return $ban; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |      * @param $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |     protected function setupSecurity($user): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |         $this->assign('canSeeIpBan', $this->barrierTest('ip', $user, 'BanType')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |         $this->assign('canSeeNameBan', $this->barrierTest('name', $user, 'BanType')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |         $this->assign('canSeeEmailBan', $this->barrierTest('email', $user, 'BanType')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |         $this->assign('canSeeUseragentBan', $this->barrierTest('useragent', $user, 'BanType')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |         $this->assign('canSeeUserVisibility', $this->barrierTest('user', $user, 'BanVisibility')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |         $this->assign('canSeeAdminVisibility', $this->barrierTest('admin', $user, 'BanVisibility')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |         $this->assign('canSeeCheckuserVisibility', $this->barrierTest('checkuser', $user, 'BanVisibility')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |      * @param string $targetIp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |      * @param        $targetMask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |     private function validateIpBan(string $targetIp, $targetMask): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |         // validate this is an IP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |         if (!filter_var($targetIp, FILTER_VALIDATE_IP)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |             throw new ApplicationLogicException("Not a valid IP address"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |         // validate CIDR ranges | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |         if (filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && ($targetMask < 0 || $targetMask > 128)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |             throw new ApplicationLogicException("CIDR mask out of range for IPv6"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         if (filter_var($targetIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && ($targetMask < 0 || $targetMask > 32)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |             throw new ApplicationLogicException("CIDR mask out of range for IPv4"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |         $squidIpList = $this->getSiteConfiguration()->getSquidList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |         if (in_array($targetIp, $squidIpList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |             throw new ApplicationLogicException("This IP address is on the protected list of proxies, and cannot be banned."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |      * @param array $bans | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |     protected function setupBanList(array $bans): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |         $userIds = array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |             function(Ban $entry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |                 return $entry->getUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |             $bans); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |         $userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |         $user = User::getCurrent($this->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |         $this->assign('canSet', $this->barrierTest('set', $user)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |         $this->assign('canRemove', $this->barrierTest('remove', $user)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |         $this->setupSecurity($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |         $this->assign('usernames', $userList); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |         $this->assign('activebans', $bans); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |         $banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), $this->getSecurityManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |         $this->assign('banHelper', $banHelper); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 411 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 412 |  |  |  |