@@ -21,310 +21,310 @@ |
||
| 21 | 21 | |
| 22 | 22 | class PageBan extends InternalPageBase |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Main function for this page, when no specific actions are called. |
|
| 26 | - */ |
|
| 27 | - protected function main() |
|
| 28 | - { |
|
| 29 | - $this->assignCSRFToken(); |
|
| 30 | - |
|
| 31 | - $this->setHtmlTitle('Bans'); |
|
| 32 | - |
|
| 33 | - $bans = Ban::getActiveBans(null, $this->getDatabase()); |
|
| 34 | - |
|
| 35 | - $userIds = array_map( |
|
| 36 | - function(Ban $entry) { |
|
| 37 | - return $entry->getUser(); |
|
| 38 | - }, |
|
| 39 | - $bans); |
|
| 40 | - $userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); |
|
| 41 | - |
|
| 42 | - $user = User::getCurrent($this->getDatabase()); |
|
| 43 | - $this->assign('canSet', $this->barrierTest('set', $user)); |
|
| 44 | - $this->assign('canRemove', $this->barrierTest('remove', $user)); |
|
| 45 | - |
|
| 46 | - $this->assign('usernames', $userList); |
|
| 47 | - $this->assign('activebans', $bans); |
|
| 48 | - $this->setTemplate('bans/banlist.tpl'); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Entry point for the ban set action |
|
| 53 | - */ |
|
| 54 | - protected function set() |
|
| 55 | - { |
|
| 56 | - $this->setHtmlTitle('Bans'); |
|
| 57 | - |
|
| 58 | - // dual-mode action |
|
| 59 | - if (WebRequest::wasPosted()) { |
|
| 60 | - try { |
|
| 61 | - $this->handlePostMethodForSetBan(); |
|
| 62 | - } |
|
| 63 | - catch (ApplicationLogicException $ex) { |
|
| 64 | - SessionAlert::error($ex->getMessage()); |
|
| 65 | - $this->redirect("bans", "set"); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - else { |
|
| 69 | - $this->handleGetMethodForSetBan(); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Entry point for the ban remove action |
|
| 75 | - */ |
|
| 76 | - protected function remove() |
|
| 77 | - { |
|
| 78 | - $this->setHtmlTitle('Bans'); |
|
| 79 | - |
|
| 80 | - $ban = $this->getBanForUnban(); |
|
| 81 | - |
|
| 82 | - // dual mode |
|
| 83 | - if (WebRequest::wasPosted()) { |
|
| 84 | - $this->validateCSRFToken(); |
|
| 85 | - $unbanReason = WebRequest::postString('unbanreason'); |
|
| 86 | - |
|
| 87 | - if ($unbanReason === null || trim($unbanReason) === "") { |
|
| 88 | - SessionAlert::error('No unban reason specified'); |
|
| 89 | - $this->redirect("bans", "remove", array('id' => $ban->getId())); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // set optimistic locking from delete form page load |
|
| 93 | - $updateVersion = WebRequest::postInt('updateversion'); |
|
| 94 | - $ban->setUpdateVersion($updateVersion); |
|
| 95 | - |
|
| 96 | - $database = $this->getDatabase(); |
|
| 97 | - $ban->setActive(false); |
|
| 98 | - $ban->save(); |
|
| 99 | - |
|
| 100 | - Logger::unbanned($database, $ban, $unbanReason); |
|
| 101 | - |
|
| 102 | - SessionAlert::quick('Disabled ban.'); |
|
| 103 | - $this->getNotificationHelper()->unbanned($ban, $unbanReason); |
|
| 104 | - |
|
| 105 | - $this->redirect('bans'); |
|
| 106 | - } |
|
| 107 | - else { |
|
| 108 | - $this->assignCSRFToken(); |
|
| 109 | - $this->assign('ban', $ban); |
|
| 110 | - $this->setTemplate('bans/unban.tpl'); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @throws ApplicationLogicException |
|
| 116 | - */ |
|
| 117 | - private function getBanDuration() |
|
| 118 | - { |
|
| 119 | - $duration = WebRequest::postString('duration'); |
|
| 120 | - if ($duration === "other") { |
|
| 121 | - $duration = strtotime(WebRequest::postString('otherduration')); |
|
| 122 | - |
|
| 123 | - if (!$duration) { |
|
| 124 | - throw new ApplicationLogicException('Invalid ban time'); |
|
| 125 | - } |
|
| 126 | - elseif (time() > $duration) { |
|
| 127 | - throw new ApplicationLogicException('Ban time has already expired!'); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $duration; |
|
| 131 | - } |
|
| 132 | - elseif ($duration === "-1") { |
|
| 133 | - $duration = -1; |
|
| 134 | - |
|
| 135 | - return $duration; |
|
| 136 | - } |
|
| 137 | - else { |
|
| 138 | - $duration = WebRequest::postInt('duration') + time(); |
|
| 139 | - |
|
| 140 | - return $duration; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param string $type |
|
| 146 | - * @param string $target |
|
| 147 | - * |
|
| 148 | - * @throws ApplicationLogicException |
|
| 149 | - */ |
|
| 150 | - private function validateBanType($type, $target) |
|
| 151 | - { |
|
| 152 | - switch ($type) { |
|
| 153 | - case 'IP': |
|
| 154 | - $this->validateIpBan($target); |
|
| 155 | - |
|
| 156 | - return; |
|
| 157 | - case 'Name': |
|
| 158 | - // No validation needed here. |
|
| 159 | - return; |
|
| 160 | - case 'EMail': |
|
| 161 | - $this->validateEmailBanTarget($target); |
|
| 162 | - |
|
| 163 | - return; |
|
| 164 | - default: |
|
| 165 | - throw new ApplicationLogicException("Unknown ban type"); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Handles the POST method on the set action |
|
| 171 | - * |
|
| 172 | - * @throws ApplicationLogicException |
|
| 173 | - * @throws Exception |
|
| 174 | - */ |
|
| 175 | - private function handlePostMethodForSetBan() |
|
| 176 | - { |
|
| 177 | - $this->validateCSRFToken(); |
|
| 178 | - $reason = WebRequest::postString('banreason'); |
|
| 179 | - $target = WebRequest::postString('target'); |
|
| 180 | - |
|
| 181 | - // Checks whether there is a reason entered for ban. |
|
| 182 | - if ($reason === null || trim($reason) === "") { |
|
| 183 | - throw new ApplicationLogicException('You must specify a ban reason'); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - // Checks whether there is a target entered to ban. |
|
| 187 | - if ($target === null || trim($target) === "") { |
|
| 188 | - throw new ApplicationLogicException('You must specify a target to be banned'); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - // Validate ban duration |
|
| 192 | - $duration = $this->getBanDuration(); |
|
| 193 | - |
|
| 194 | - // Validate ban type & target for that type |
|
| 195 | - $type = WebRequest::postString('type'); |
|
| 196 | - $this->validateBanType($type, $target); |
|
| 197 | - |
|
| 198 | - $database = $this->getDatabase(); |
|
| 199 | - |
|
| 200 | - if (count(Ban::getActiveBans($target, $database)) > 0) { |
|
| 201 | - throw new ApplicationLogicException('This target is already banned!'); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $ban = new Ban(); |
|
| 205 | - $ban->setDatabase($database); |
|
| 206 | - $ban->setActive(true); |
|
| 207 | - $ban->setType($type); |
|
| 208 | - $ban->setTarget($target); |
|
| 209 | - $ban->setUser(User::getCurrent($database)->getId()); |
|
| 210 | - $ban->setReason($reason); |
|
| 211 | - $ban->setDuration($duration); |
|
| 212 | - |
|
| 213 | - $ban->save(); |
|
| 214 | - |
|
| 215 | - Logger::banned($database, $ban, $reason); |
|
| 216 | - |
|
| 217 | - $this->getNotificationHelper()->banned($ban); |
|
| 218 | - SessionAlert::quick('Ban has been set.'); |
|
| 219 | - |
|
| 220 | - $this->redirect('bans'); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Handles the GET method on the set action |
|
| 225 | - */ |
|
| 226 | - protected function handleGetMethodForSetBan() |
|
| 227 | - { |
|
| 228 | - $this->setTemplate('bans/banform.tpl'); |
|
| 229 | - $this->assignCSRFToken(); |
|
| 230 | - |
|
| 231 | - $banType = WebRequest::getString('type'); |
|
| 232 | - $banTarget = WebRequest::getInt('request'); |
|
| 233 | - |
|
| 234 | - $database = $this->getDatabase(); |
|
| 235 | - |
|
| 236 | - // if the parameters are null, skip loading a request. |
|
| 237 | - if ($banType === null |
|
| 238 | - || !in_array($banType, array('IP', 'Name', 'EMail')) |
|
| 239 | - || $banTarget === null |
|
| 240 | - || $banTarget === 0 |
|
| 241 | - ) { |
|
| 242 | - $this->assign('bantarget', ''); |
|
| 243 | - $this->assign('bantype', ''); |
|
| 244 | - |
|
| 245 | - return; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Set the ban type, which the user has indicated. |
|
| 249 | - $this->assign('bantype', $banType); |
|
| 250 | - |
|
| 251 | - // Attempt to resolve the correct target |
|
| 252 | - /** @var Request $request */ |
|
| 253 | - $request = Request::getById($banTarget, $database); |
|
| 254 | - if ($request === false) { |
|
| 255 | - $this->assign('bantarget', ''); |
|
| 256 | - |
|
| 257 | - return; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $realTarget = ''; |
|
| 261 | - switch ($banType) { |
|
| 262 | - case 'EMail': |
|
| 263 | - $realTarget = $request->getEmail(); |
|
| 264 | - break; |
|
| 265 | - case 'IP': |
|
| 266 | - $xffProvider = $this->getXffTrustProvider(); |
|
| 267 | - $realTarget = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 268 | - break; |
|
| 269 | - case 'Name': |
|
| 270 | - $realTarget = $request->getName(); |
|
| 271 | - break; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - $this->assign('bantarget', $realTarget); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Validates an IP ban target |
|
| 279 | - * |
|
| 280 | - * @param string $target |
|
| 281 | - * |
|
| 282 | - * @throws ApplicationLogicException |
|
| 283 | - */ |
|
| 284 | - private function validateIpBan($target) |
|
| 285 | - { |
|
| 286 | - $squidIpList = $this->getSiteConfiguration()->getSquidList(); |
|
| 287 | - |
|
| 288 | - if (filter_var($target, FILTER_VALIDATE_IP) === false) { |
|
| 289 | - throw new ApplicationLogicException('Invalid target - IP address expected.'); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (in_array($target, $squidIpList)) { |
|
| 293 | - throw new ApplicationLogicException("This IP address is on the protected list of proxies, and cannot be banned."); |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Validates an email address as a ban target |
|
| 299 | - * |
|
| 300 | - * @param string $target |
|
| 301 | - * |
|
| 302 | - * @throws ApplicationLogicException |
|
| 303 | - */ |
|
| 304 | - private function validateEmailBanTarget($target) |
|
| 305 | - { |
|
| 306 | - if (filter_var($target, FILTER_VALIDATE_EMAIL) !== $target) { |
|
| 307 | - throw new ApplicationLogicException('Invalid target - email address expected.'); |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @return Ban |
|
| 313 | - * @throws ApplicationLogicException |
|
| 314 | - */ |
|
| 315 | - private function getBanForUnban() |
|
| 316 | - { |
|
| 317 | - $banId = WebRequest::getInt('id'); |
|
| 318 | - if ($banId === null || $banId === 0) { |
|
| 319 | - throw new ApplicationLogicException("The ban ID appears to be missing. This is probably a bug."); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - $ban = Ban::getActiveId($banId, $this->getDatabase()); |
|
| 323 | - |
|
| 324 | - if ($ban === false) { |
|
| 325 | - throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return $ban; |
|
| 329 | - } |
|
| 24 | + /** |
|
| 25 | + * Main function for this page, when no specific actions are called. |
|
| 26 | + */ |
|
| 27 | + protected function main() |
|
| 28 | + { |
|
| 29 | + $this->assignCSRFToken(); |
|
| 30 | + |
|
| 31 | + $this->setHtmlTitle('Bans'); |
|
| 32 | + |
|
| 33 | + $bans = Ban::getActiveBans(null, $this->getDatabase()); |
|
| 34 | + |
|
| 35 | + $userIds = array_map( |
|
| 36 | + function(Ban $entry) { |
|
| 37 | + return $entry->getUser(); |
|
| 38 | + }, |
|
| 39 | + $bans); |
|
| 40 | + $userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); |
|
| 41 | + |
|
| 42 | + $user = User::getCurrent($this->getDatabase()); |
|
| 43 | + $this->assign('canSet', $this->barrierTest('set', $user)); |
|
| 44 | + $this->assign('canRemove', $this->barrierTest('remove', $user)); |
|
| 45 | + |
|
| 46 | + $this->assign('usernames', $userList); |
|
| 47 | + $this->assign('activebans', $bans); |
|
| 48 | + $this->setTemplate('bans/banlist.tpl'); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Entry point for the ban set action |
|
| 53 | + */ |
|
| 54 | + protected function set() |
|
| 55 | + { |
|
| 56 | + $this->setHtmlTitle('Bans'); |
|
| 57 | + |
|
| 58 | + // dual-mode action |
|
| 59 | + if (WebRequest::wasPosted()) { |
|
| 60 | + try { |
|
| 61 | + $this->handlePostMethodForSetBan(); |
|
| 62 | + } |
|
| 63 | + catch (ApplicationLogicException $ex) { |
|
| 64 | + SessionAlert::error($ex->getMessage()); |
|
| 65 | + $this->redirect("bans", "set"); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + else { |
|
| 69 | + $this->handleGetMethodForSetBan(); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Entry point for the ban remove action |
|
| 75 | + */ |
|
| 76 | + protected function remove() |
|
| 77 | + { |
|
| 78 | + $this->setHtmlTitle('Bans'); |
|
| 79 | + |
|
| 80 | + $ban = $this->getBanForUnban(); |
|
| 81 | + |
|
| 82 | + // dual mode |
|
| 83 | + if (WebRequest::wasPosted()) { |
|
| 84 | + $this->validateCSRFToken(); |
|
| 85 | + $unbanReason = WebRequest::postString('unbanreason'); |
|
| 86 | + |
|
| 87 | + if ($unbanReason === null || trim($unbanReason) === "") { |
|
| 88 | + SessionAlert::error('No unban reason specified'); |
|
| 89 | + $this->redirect("bans", "remove", array('id' => $ban->getId())); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // set optimistic locking from delete form page load |
|
| 93 | + $updateVersion = WebRequest::postInt('updateversion'); |
|
| 94 | + $ban->setUpdateVersion($updateVersion); |
|
| 95 | + |
|
| 96 | + $database = $this->getDatabase(); |
|
| 97 | + $ban->setActive(false); |
|
| 98 | + $ban->save(); |
|
| 99 | + |
|
| 100 | + Logger::unbanned($database, $ban, $unbanReason); |
|
| 101 | + |
|
| 102 | + SessionAlert::quick('Disabled ban.'); |
|
| 103 | + $this->getNotificationHelper()->unbanned($ban, $unbanReason); |
|
| 104 | + |
|
| 105 | + $this->redirect('bans'); |
|
| 106 | + } |
|
| 107 | + else { |
|
| 108 | + $this->assignCSRFToken(); |
|
| 109 | + $this->assign('ban', $ban); |
|
| 110 | + $this->setTemplate('bans/unban.tpl'); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @throws ApplicationLogicException |
|
| 116 | + */ |
|
| 117 | + private function getBanDuration() |
|
| 118 | + { |
|
| 119 | + $duration = WebRequest::postString('duration'); |
|
| 120 | + if ($duration === "other") { |
|
| 121 | + $duration = strtotime(WebRequest::postString('otherduration')); |
|
| 122 | + |
|
| 123 | + if (!$duration) { |
|
| 124 | + throw new ApplicationLogicException('Invalid ban time'); |
|
| 125 | + } |
|
| 126 | + elseif (time() > $duration) { |
|
| 127 | + throw new ApplicationLogicException('Ban time has already expired!'); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $duration; |
|
| 131 | + } |
|
| 132 | + elseif ($duration === "-1") { |
|
| 133 | + $duration = -1; |
|
| 134 | + |
|
| 135 | + return $duration; |
|
| 136 | + } |
|
| 137 | + else { |
|
| 138 | + $duration = WebRequest::postInt('duration') + time(); |
|
| 139 | + |
|
| 140 | + return $duration; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param string $type |
|
| 146 | + * @param string $target |
|
| 147 | + * |
|
| 148 | + * @throws ApplicationLogicException |
|
| 149 | + */ |
|
| 150 | + private function validateBanType($type, $target) |
|
| 151 | + { |
|
| 152 | + switch ($type) { |
|
| 153 | + case 'IP': |
|
| 154 | + $this->validateIpBan($target); |
|
| 155 | + |
|
| 156 | + return; |
|
| 157 | + case 'Name': |
|
| 158 | + // No validation needed here. |
|
| 159 | + return; |
|
| 160 | + case 'EMail': |
|
| 161 | + $this->validateEmailBanTarget($target); |
|
| 162 | + |
|
| 163 | + return; |
|
| 164 | + default: |
|
| 165 | + throw new ApplicationLogicException("Unknown ban type"); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Handles the POST method on the set action |
|
| 171 | + * |
|
| 172 | + * @throws ApplicationLogicException |
|
| 173 | + * @throws Exception |
|
| 174 | + */ |
|
| 175 | + private function handlePostMethodForSetBan() |
|
| 176 | + { |
|
| 177 | + $this->validateCSRFToken(); |
|
| 178 | + $reason = WebRequest::postString('banreason'); |
|
| 179 | + $target = WebRequest::postString('target'); |
|
| 180 | + |
|
| 181 | + // Checks whether there is a reason entered for ban. |
|
| 182 | + if ($reason === null || trim($reason) === "") { |
|
| 183 | + throw new ApplicationLogicException('You must specify a ban reason'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + // Checks whether there is a target entered to ban. |
|
| 187 | + if ($target === null || trim($target) === "") { |
|
| 188 | + throw new ApplicationLogicException('You must specify a target to be banned'); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + // Validate ban duration |
|
| 192 | + $duration = $this->getBanDuration(); |
|
| 193 | + |
|
| 194 | + // Validate ban type & target for that type |
|
| 195 | + $type = WebRequest::postString('type'); |
|
| 196 | + $this->validateBanType($type, $target); |
|
| 197 | + |
|
| 198 | + $database = $this->getDatabase(); |
|
| 199 | + |
|
| 200 | + if (count(Ban::getActiveBans($target, $database)) > 0) { |
|
| 201 | + throw new ApplicationLogicException('This target is already banned!'); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $ban = new Ban(); |
|
| 205 | + $ban->setDatabase($database); |
|
| 206 | + $ban->setActive(true); |
|
| 207 | + $ban->setType($type); |
|
| 208 | + $ban->setTarget($target); |
|
| 209 | + $ban->setUser(User::getCurrent($database)->getId()); |
|
| 210 | + $ban->setReason($reason); |
|
| 211 | + $ban->setDuration($duration); |
|
| 212 | + |
|
| 213 | + $ban->save(); |
|
| 214 | + |
|
| 215 | + Logger::banned($database, $ban, $reason); |
|
| 216 | + |
|
| 217 | + $this->getNotificationHelper()->banned($ban); |
|
| 218 | + SessionAlert::quick('Ban has been set.'); |
|
| 219 | + |
|
| 220 | + $this->redirect('bans'); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Handles the GET method on the set action |
|
| 225 | + */ |
|
| 226 | + protected function handleGetMethodForSetBan() |
|
| 227 | + { |
|
| 228 | + $this->setTemplate('bans/banform.tpl'); |
|
| 229 | + $this->assignCSRFToken(); |
|
| 230 | + |
|
| 231 | + $banType = WebRequest::getString('type'); |
|
| 232 | + $banTarget = WebRequest::getInt('request'); |
|
| 233 | + |
|
| 234 | + $database = $this->getDatabase(); |
|
| 235 | + |
|
| 236 | + // if the parameters are null, skip loading a request. |
|
| 237 | + if ($banType === null |
|
| 238 | + || !in_array($banType, array('IP', 'Name', 'EMail')) |
|
| 239 | + || $banTarget === null |
|
| 240 | + || $banTarget === 0 |
|
| 241 | + ) { |
|
| 242 | + $this->assign('bantarget', ''); |
|
| 243 | + $this->assign('bantype', ''); |
|
| 244 | + |
|
| 245 | + return; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Set the ban type, which the user has indicated. |
|
| 249 | + $this->assign('bantype', $banType); |
|
| 250 | + |
|
| 251 | + // Attempt to resolve the correct target |
|
| 252 | + /** @var Request $request */ |
|
| 253 | + $request = Request::getById($banTarget, $database); |
|
| 254 | + if ($request === false) { |
|
| 255 | + $this->assign('bantarget', ''); |
|
| 256 | + |
|
| 257 | + return; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $realTarget = ''; |
|
| 261 | + switch ($banType) { |
|
| 262 | + case 'EMail': |
|
| 263 | + $realTarget = $request->getEmail(); |
|
| 264 | + break; |
|
| 265 | + case 'IP': |
|
| 266 | + $xffProvider = $this->getXffTrustProvider(); |
|
| 267 | + $realTarget = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 268 | + break; |
|
| 269 | + case 'Name': |
|
| 270 | + $realTarget = $request->getName(); |
|
| 271 | + break; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + $this->assign('bantarget', $realTarget); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Validates an IP ban target |
|
| 279 | + * |
|
| 280 | + * @param string $target |
|
| 281 | + * |
|
| 282 | + * @throws ApplicationLogicException |
|
| 283 | + */ |
|
| 284 | + private function validateIpBan($target) |
|
| 285 | + { |
|
| 286 | + $squidIpList = $this->getSiteConfiguration()->getSquidList(); |
|
| 287 | + |
|
| 288 | + if (filter_var($target, FILTER_VALIDATE_IP) === false) { |
|
| 289 | + throw new ApplicationLogicException('Invalid target - IP address expected.'); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (in_array($target, $squidIpList)) { |
|
| 293 | + throw new ApplicationLogicException("This IP address is on the protected list of proxies, and cannot be banned."); |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Validates an email address as a ban target |
|
| 299 | + * |
|
| 300 | + * @param string $target |
|
| 301 | + * |
|
| 302 | + * @throws ApplicationLogicException |
|
| 303 | + */ |
|
| 304 | + private function validateEmailBanTarget($target) |
|
| 305 | + { |
|
| 306 | + if (filter_var($target, FILTER_VALIDATE_EMAIL) !== $target) { |
|
| 307 | + throw new ApplicationLogicException('Invalid target - email address expected.'); |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @return Ban |
|
| 313 | + * @throws ApplicationLogicException |
|
| 314 | + */ |
|
| 315 | + private function getBanForUnban() |
|
| 316 | + { |
|
| 317 | + $banId = WebRequest::getInt('id'); |
|
| 318 | + if ($banId === null || $banId === 0) { |
|
| 319 | + throw new ApplicationLogicException("The ban ID appears to be missing. This is probably a bug."); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + $ban = Ban::getActiveId($banId, $this->getDatabase()); |
|
| 323 | + |
|
| 324 | + if ($ban === false) { |
|
| 325 | + throw new ApplicationLogicException("The specified ban is not currently active, or doesn't exist."); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return $ban; |
|
| 329 | + } |
|
| 330 | 330 | } |
@@ -18,77 +18,77 @@ |
||
| 18 | 18 | |
| 19 | 19 | class PageExpandedRequestList extends InternalPageBase |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Main function for this page, when no specific actions are called. |
|
| 23 | - * @return void |
|
| 24 | - * @todo This is very similar to the PageMain code, we could probably generalise this somehow |
|
| 25 | - */ |
|
| 26 | - protected function main() |
|
| 27 | - { |
|
| 28 | - $config = $this->getSiteConfiguration(); |
|
| 21 | + /** |
|
| 22 | + * Main function for this page, when no specific actions are called. |
|
| 23 | + * @return void |
|
| 24 | + * @todo This is very similar to the PageMain code, we could probably generalise this somehow |
|
| 25 | + */ |
|
| 26 | + protected function main() |
|
| 27 | + { |
|
| 28 | + $config = $this->getSiteConfiguration(); |
|
| 29 | 29 | |
| 30 | - $requestedStatus = WebRequest::getString('status'); |
|
| 31 | - $requestStates = $config->getRequestStates(); |
|
| 30 | + $requestedStatus = WebRequest::getString('status'); |
|
| 31 | + $requestStates = $config->getRequestStates(); |
|
| 32 | 32 | |
| 33 | - if ($requestedStatus !== null && isset($requestStates[$requestedStatus])) { |
|
| 33 | + if ($requestedStatus !== null && isset($requestStates[$requestedStatus])) { |
|
| 34 | 34 | |
| 35 | - $this->assignCSRFToken(); |
|
| 35 | + $this->assignCSRFToken(); |
|
| 36 | 36 | |
| 37 | - $database = $this->getDatabase(); |
|
| 37 | + $database = $this->getDatabase(); |
|
| 38 | 38 | |
| 39 | - if ($config->getEmailConfirmationEnabled()) { |
|
| 40 | - $query = "SELECT * FROM request WHERE status = :type AND emailconfirm = 'Confirmed';"; |
|
| 41 | - $totalQuery = "SELECT COUNT(id) FROM request WHERE status = :type AND emailconfirm = 'Confirmed';"; |
|
| 42 | - } |
|
| 43 | - else { |
|
| 44 | - $query = "SELECT * FROM request WHERE status = :type;"; |
|
| 45 | - $totalQuery = "SELECT COUNT(id) FROM request WHERE status = :type;"; |
|
| 46 | - } |
|
| 39 | + if ($config->getEmailConfirmationEnabled()) { |
|
| 40 | + $query = "SELECT * FROM request WHERE status = :type AND emailconfirm = 'Confirmed';"; |
|
| 41 | + $totalQuery = "SELECT COUNT(id) FROM request WHERE status = :type AND emailconfirm = 'Confirmed';"; |
|
| 42 | + } |
|
| 43 | + else { |
|
| 44 | + $query = "SELECT * FROM request WHERE status = :type;"; |
|
| 45 | + $totalQuery = "SELECT COUNT(id) FROM request WHERE status = :type;"; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - $statement = $database->prepare($query); |
|
| 48 | + $statement = $database->prepare($query); |
|
| 49 | 49 | |
| 50 | - $totalRequestsStatement = $database->prepare($totalQuery); |
|
| 50 | + $totalRequestsStatement = $database->prepare($totalQuery); |
|
| 51 | 51 | |
| 52 | - $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
| 52 | + $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
| 53 | 53 | |
| 54 | - $type = $requestedStatus; |
|
| 54 | + $type = $requestedStatus; |
|
| 55 | 55 | |
| 56 | - $statement->bindValue(":type", $type); |
|
| 57 | - $statement->execute(); |
|
| 56 | + $statement->bindValue(":type", $type); |
|
| 57 | + $statement->execute(); |
|
| 58 | 58 | |
| 59 | - $requests = $statement->fetchAll(PDO::FETCH_CLASS, Request::class); |
|
| 59 | + $requests = $statement->fetchAll(PDO::FETCH_CLASS, Request::class); |
|
| 60 | 60 | |
| 61 | - /** @var Request $req */ |
|
| 62 | - foreach ($requests as $req) { |
|
| 63 | - $req->setDatabase($database); |
|
| 64 | - } |
|
| 61 | + /** @var Request $req */ |
|
| 62 | + foreach ($requests as $req) { |
|
| 63 | + $req->setDatabase($database); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - $this->assign('requests', $requests); |
|
| 67 | - $this->assign('header', $type); |
|
| 66 | + $this->assign('requests', $requests); |
|
| 67 | + $this->assign('header', $type); |
|
| 68 | 68 | |
| 69 | - $totalRequestsStatement->bindValue(':type', $type); |
|
| 70 | - $totalRequestsStatement->execute(); |
|
| 71 | - $totalRequests = $totalRequestsStatement->fetchColumn(); |
|
| 72 | - $totalRequestsStatement->closeCursor(); |
|
| 73 | - $this->assign('totalRequests', $totalRequests); |
|
| 69 | + $totalRequestsStatement->bindValue(':type', $type); |
|
| 70 | + $totalRequestsStatement->execute(); |
|
| 71 | + $totalRequests = $totalRequestsStatement->fetchColumn(); |
|
| 72 | + $totalRequestsStatement->closeCursor(); |
|
| 73 | + $this->assign('totalRequests', $totalRequests); |
|
| 74 | 74 | |
| 75 | - $userIds = array_map( |
|
| 76 | - function(Request $entry) { |
|
| 77 | - return $entry->getReserved(); |
|
| 78 | - }, |
|
| 79 | - $requests |
|
| 80 | - ); |
|
| 75 | + $userIds = array_map( |
|
| 76 | + function(Request $entry) { |
|
| 77 | + return $entry->getReserved(); |
|
| 78 | + }, |
|
| 79 | + $requests |
|
| 80 | + ); |
|
| 81 | 81 | |
| 82 | - $userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); |
|
| 83 | - $this->assign('userlist', $userList); |
|
| 82 | + $userList = UserSearchHelper::get($this->getDatabase())->inIds($userIds)->fetchMap('username'); |
|
| 83 | + $this->assign('userlist', $userList); |
|
| 84 | 84 | |
| 85 | - $this->assign('requestLimitShowOnly', $config->getMiserModeLimit()); |
|
| 85 | + $this->assign('requestLimitShowOnly', $config->getMiserModeLimit()); |
|
| 86 | 86 | |
| 87 | - $currentUser = User::getCurrent($database); |
|
| 88 | - $this->assign('canBan', $this->barrierTest('set', $currentUser, PageBan::class)); |
|
| 89 | - $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class)); |
|
| 87 | + $currentUser = User::getCurrent($database); |
|
| 88 | + $this->assign('canBan', $this->barrierTest('set', $currentUser, PageBan::class)); |
|
| 89 | + $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class)); |
|
| 90 | 90 | |
| 91 | - $this->setTemplate('mainpage/expandedrequestlist.tpl'); |
|
| 92 | - } |
|
| 93 | - } |
|
| 91 | + $this->setTemplate('mainpage/expandedrequestlist.tpl'); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | 94 | } |
@@ -24,213 +24,213 @@ |
||
| 24 | 24 | |
| 25 | 25 | class PageViewRequest extends InternalPageBase |
| 26 | 26 | { |
| 27 | - use RequestData; |
|
| 28 | - const STATUS_SYMBOL_OPEN = '☐'; |
|
| 29 | - const STATUS_SYMBOL_ACCEPTED = '☑'; |
|
| 30 | - const STATUS_SYMBOL_REJECTED = '☒'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Main function for this page, when no specific actions are called. |
|
| 34 | - * @throws ApplicationLogicException |
|
| 35 | - */ |
|
| 36 | - protected function main() |
|
| 37 | - { |
|
| 38 | - // set up csrf protection |
|
| 39 | - $this->assignCSRFToken(); |
|
| 40 | - |
|
| 41 | - // get some useful objects |
|
| 42 | - $database = $this->getDatabase(); |
|
| 43 | - $request = $this->getRequest($database, WebRequest::getInt('id')); |
|
| 44 | - $config = $this->getSiteConfiguration(); |
|
| 45 | - $currentUser = User::getCurrent($database); |
|
| 46 | - |
|
| 47 | - // Test we should be able to look at this request |
|
| 48 | - if ($config->getEmailConfirmationEnabled()) { |
|
| 49 | - if ($request->getEmailConfirm() !== 'Confirmed') { |
|
| 50 | - // Not allowed to look at this yet. |
|
| 51 | - throw new ApplicationLogicException('The email address has not yet been confirmed for this request.'); |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->setupBasicData($request, $config); |
|
| 56 | - |
|
| 57 | - $this->setupUsernameData($request); |
|
| 58 | - |
|
| 59 | - $this->setupTitle($request); |
|
| 60 | - |
|
| 61 | - $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
| 62 | - $this->setupGeneralData($database); |
|
| 63 | - |
|
| 64 | - $this->assign('requestDataCleared', false); |
|
| 65 | - if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
| 66 | - $this->assign('requestDataCleared', true); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
| 70 | - |
|
| 71 | - $this->setupLogData($request, $database); |
|
| 72 | - |
|
| 73 | - if ($allowedPrivateData) { |
|
| 74 | - $this->setTemplate('view-request/main-with-data.tpl'); |
|
| 75 | - $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
| 76 | - |
|
| 77 | - $this->assign('canSetBan', $this->barrierTest('set', $currentUser, PageBan::class)); |
|
| 78 | - $this->assign('canSeeCheckuserData', $this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')); |
|
| 79 | - |
|
| 80 | - if ($this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')) { |
|
| 81 | - $this->setTemplate('view-request/main-with-checkuser-data.tpl'); |
|
| 82 | - $this->setupCheckUserData($request); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - else { |
|
| 86 | - $this->setTemplate('view-request/main.tpl'); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param Request $request |
|
| 92 | - */ |
|
| 93 | - protected function setupTitle(Request $request) |
|
| 94 | - { |
|
| 95 | - $statusSymbol = self::STATUS_SYMBOL_OPEN; |
|
| 96 | - if ($request->getStatus() === 'Closed') { |
|
| 97 | - if ($request->getWasCreated()) { |
|
| 98 | - $statusSymbol = self::STATUS_SYMBOL_ACCEPTED; |
|
| 99 | - } |
|
| 100 | - else { |
|
| 101 | - $statusSymbol = self::STATUS_SYMBOL_REJECTED; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Sets up data unrelated to the request, such as the email template information |
|
| 110 | - * |
|
| 111 | - * @param PdoDatabase $database |
|
| 112 | - */ |
|
| 113 | - protected function setupGeneralData(PdoDatabase $database) |
|
| 114 | - { |
|
| 115 | - $config = $this->getSiteConfiguration(); |
|
| 116 | - |
|
| 117 | - $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #'); |
|
| 118 | - |
|
| 119 | - $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
| 120 | - |
|
| 121 | - $this->assign('requestStates', $config->getRequestStates()); |
|
| 122 | - |
|
| 123 | - /** @var EmailTemplate $createdTemplate */ |
|
| 124 | - $createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database); |
|
| 125 | - |
|
| 126 | - $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != ''); |
|
| 127 | - $this->assign('createdJsQuestion', $createdTemplate->getJsquestion()); |
|
| 128 | - $this->assign('createdId', $createdTemplate->getId()); |
|
| 129 | - $this->assign('createdName', $createdTemplate->getName()); |
|
| 130 | - |
|
| 131 | - $createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database); |
|
| 132 | - $this->assign("createReasons", $createReasons); |
|
| 133 | - $declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
| 134 | - $this->assign("declineReasons", $declineReasons); |
|
| 135 | - |
|
| 136 | - $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database); |
|
| 137 | - $this->assign("allCreateReasons", $allCreateReasons); |
|
| 138 | - $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
| 139 | - $this->assign("allDeclineReasons", $allDeclineReasons); |
|
| 140 | - $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database); |
|
| 141 | - $this->assign("allOtherReasons", $allOtherReasons); |
|
| 142 | - |
|
| 143 | - $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
| 144 | - return UserSearchHelper::get($database)->byStatus('Active')->fetchColumn('username'); |
|
| 145 | - }); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - private function setupLogData(Request $request, PdoDatabase $database) |
|
| 149 | - { |
|
| 150 | - $currentUser = User::getCurrent($database); |
|
| 151 | - |
|
| 152 | - $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database, $this->getSecurityManager()); |
|
| 153 | - $requestLogs = array(); |
|
| 154 | - |
|
| 155 | - if (trim($request->getComment()) !== "") { |
|
| 156 | - $requestLogs[] = array( |
|
| 157 | - 'type' => 'comment', |
|
| 158 | - 'security' => 'user', |
|
| 159 | - 'userid' => null, |
|
| 160 | - 'user' => $request->getName(), |
|
| 161 | - 'entry' => null, |
|
| 162 | - 'time' => $request->getDate(), |
|
| 163 | - 'canedit' => false, |
|
| 164 | - 'id' => $request->getId(), |
|
| 165 | - 'comment' => $request->getComment(), |
|
| 166 | - ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** @var User[] $nameCache */ |
|
| 170 | - $nameCache = array(); |
|
| 171 | - |
|
| 172 | - $editableComments = $this->barrierTest('editOthers', $currentUser, PageEditComment::class); |
|
| 173 | - |
|
| 174 | - /** @var Log|Comment $entry */ |
|
| 175 | - foreach ($logs as $entry) { |
|
| 176 | - // both log and comment have a 'user' field |
|
| 177 | - if (!array_key_exists($entry->getUser(), $nameCache)) { |
|
| 178 | - $entryUser = User::getById($entry->getUser(), $database); |
|
| 179 | - $nameCache[$entry->getUser()] = $entryUser; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($entry instanceof Comment) { |
|
| 183 | - $requestLogs[] = array( |
|
| 184 | - 'type' => 'comment', |
|
| 185 | - 'security' => $entry->getVisibility(), |
|
| 186 | - 'user' => $nameCache[$entry->getUser()]->getUsername(), |
|
| 187 | - 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
| 188 | - 'entry' => null, |
|
| 189 | - 'time' => $entry->getTime(), |
|
| 190 | - 'canedit' => ($editableComments || $entry->getUser() == $currentUser->getId()), |
|
| 191 | - 'id' => $entry->getId(), |
|
| 192 | - 'comment' => $entry->getComment(), |
|
| 193 | - ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if ($entry instanceof Log) { |
|
| 197 | - $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0; |
|
| 198 | - $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()]; |
|
| 199 | - |
|
| 200 | - $requestLogs[] = array( |
|
| 201 | - 'type' => 'log', |
|
| 202 | - 'security' => 'user', |
|
| 203 | - 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
| 204 | - 'user' => $entryUser->getUsername(), |
|
| 205 | - 'entry' => LogHelper::getLogDescription($entry), |
|
| 206 | - 'time' => $entry->getTimestamp(), |
|
| 207 | - 'canedit' => false, |
|
| 208 | - 'id' => $entry->getId(), |
|
| 209 | - 'comment' => $entry->getComment(), |
|
| 210 | - ); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $this->assign("requestLogs", $requestLogs); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @param Request $request |
|
| 219 | - */ |
|
| 220 | - protected function setupUsernameData(Request $request) |
|
| 221 | - { |
|
| 222 | - $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName()); |
|
| 223 | - |
|
| 224 | - $this->assign('requestIsBlacklisted', $blacklistData !== false); |
|
| 225 | - $this->assign('requestBlacklist', $blacklistData); |
|
| 226 | - |
|
| 227 | - try { |
|
| 228 | - $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName()); |
|
| 229 | - } |
|
| 230 | - catch (Exception $ex) { |
|
| 231 | - $spoofs = $ex->getMessage(); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $this->assign("spoofs", $spoofs); |
|
| 235 | - } |
|
| 27 | + use RequestData; |
|
| 28 | + const STATUS_SYMBOL_OPEN = '☐'; |
|
| 29 | + const STATUS_SYMBOL_ACCEPTED = '☑'; |
|
| 30 | + const STATUS_SYMBOL_REJECTED = '☒'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Main function for this page, when no specific actions are called. |
|
| 34 | + * @throws ApplicationLogicException |
|
| 35 | + */ |
|
| 36 | + protected function main() |
|
| 37 | + { |
|
| 38 | + // set up csrf protection |
|
| 39 | + $this->assignCSRFToken(); |
|
| 40 | + |
|
| 41 | + // get some useful objects |
|
| 42 | + $database = $this->getDatabase(); |
|
| 43 | + $request = $this->getRequest($database, WebRequest::getInt('id')); |
|
| 44 | + $config = $this->getSiteConfiguration(); |
|
| 45 | + $currentUser = User::getCurrent($database); |
|
| 46 | + |
|
| 47 | + // Test we should be able to look at this request |
|
| 48 | + if ($config->getEmailConfirmationEnabled()) { |
|
| 49 | + if ($request->getEmailConfirm() !== 'Confirmed') { |
|
| 50 | + // Not allowed to look at this yet. |
|
| 51 | + throw new ApplicationLogicException('The email address has not yet been confirmed for this request.'); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->setupBasicData($request, $config); |
|
| 56 | + |
|
| 57 | + $this->setupUsernameData($request); |
|
| 58 | + |
|
| 59 | + $this->setupTitle($request); |
|
| 60 | + |
|
| 61 | + $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
| 62 | + $this->setupGeneralData($database); |
|
| 63 | + |
|
| 64 | + $this->assign('requestDataCleared', false); |
|
| 65 | + if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
| 66 | + $this->assign('requestDataCleared', true); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
| 70 | + |
|
| 71 | + $this->setupLogData($request, $database); |
|
| 72 | + |
|
| 73 | + if ($allowedPrivateData) { |
|
| 74 | + $this->setTemplate('view-request/main-with-data.tpl'); |
|
| 75 | + $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
| 76 | + |
|
| 77 | + $this->assign('canSetBan', $this->barrierTest('set', $currentUser, PageBan::class)); |
|
| 78 | + $this->assign('canSeeCheckuserData', $this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')); |
|
| 79 | + |
|
| 80 | + if ($this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')) { |
|
| 81 | + $this->setTemplate('view-request/main-with-checkuser-data.tpl'); |
|
| 82 | + $this->setupCheckUserData($request); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + else { |
|
| 86 | + $this->setTemplate('view-request/main.tpl'); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param Request $request |
|
| 92 | + */ |
|
| 93 | + protected function setupTitle(Request $request) |
|
| 94 | + { |
|
| 95 | + $statusSymbol = self::STATUS_SYMBOL_OPEN; |
|
| 96 | + if ($request->getStatus() === 'Closed') { |
|
| 97 | + if ($request->getWasCreated()) { |
|
| 98 | + $statusSymbol = self::STATUS_SYMBOL_ACCEPTED; |
|
| 99 | + } |
|
| 100 | + else { |
|
| 101 | + $statusSymbol = self::STATUS_SYMBOL_REJECTED; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Sets up data unrelated to the request, such as the email template information |
|
| 110 | + * |
|
| 111 | + * @param PdoDatabase $database |
|
| 112 | + */ |
|
| 113 | + protected function setupGeneralData(PdoDatabase $database) |
|
| 114 | + { |
|
| 115 | + $config = $this->getSiteConfiguration(); |
|
| 116 | + |
|
| 117 | + $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #'); |
|
| 118 | + |
|
| 119 | + $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
| 120 | + |
|
| 121 | + $this->assign('requestStates', $config->getRequestStates()); |
|
| 122 | + |
|
| 123 | + /** @var EmailTemplate $createdTemplate */ |
|
| 124 | + $createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database); |
|
| 125 | + |
|
| 126 | + $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != ''); |
|
| 127 | + $this->assign('createdJsQuestion', $createdTemplate->getJsquestion()); |
|
| 128 | + $this->assign('createdId', $createdTemplate->getId()); |
|
| 129 | + $this->assign('createdName', $createdTemplate->getName()); |
|
| 130 | + |
|
| 131 | + $createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database); |
|
| 132 | + $this->assign("createReasons", $createReasons); |
|
| 133 | + $declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
| 134 | + $this->assign("declineReasons", $declineReasons); |
|
| 135 | + |
|
| 136 | + $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database); |
|
| 137 | + $this->assign("allCreateReasons", $allCreateReasons); |
|
| 138 | + $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
| 139 | + $this->assign("allDeclineReasons", $allDeclineReasons); |
|
| 140 | + $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database); |
|
| 141 | + $this->assign("allOtherReasons", $allOtherReasons); |
|
| 142 | + |
|
| 143 | + $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
| 144 | + return UserSearchHelper::get($database)->byStatus('Active')->fetchColumn('username'); |
|
| 145 | + }); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + private function setupLogData(Request $request, PdoDatabase $database) |
|
| 149 | + { |
|
| 150 | + $currentUser = User::getCurrent($database); |
|
| 151 | + |
|
| 152 | + $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database, $this->getSecurityManager()); |
|
| 153 | + $requestLogs = array(); |
|
| 154 | + |
|
| 155 | + if (trim($request->getComment()) !== "") { |
|
| 156 | + $requestLogs[] = array( |
|
| 157 | + 'type' => 'comment', |
|
| 158 | + 'security' => 'user', |
|
| 159 | + 'userid' => null, |
|
| 160 | + 'user' => $request->getName(), |
|
| 161 | + 'entry' => null, |
|
| 162 | + 'time' => $request->getDate(), |
|
| 163 | + 'canedit' => false, |
|
| 164 | + 'id' => $request->getId(), |
|
| 165 | + 'comment' => $request->getComment(), |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** @var User[] $nameCache */ |
|
| 170 | + $nameCache = array(); |
|
| 171 | + |
|
| 172 | + $editableComments = $this->barrierTest('editOthers', $currentUser, PageEditComment::class); |
|
| 173 | + |
|
| 174 | + /** @var Log|Comment $entry */ |
|
| 175 | + foreach ($logs as $entry) { |
|
| 176 | + // both log and comment have a 'user' field |
|
| 177 | + if (!array_key_exists($entry->getUser(), $nameCache)) { |
|
| 178 | + $entryUser = User::getById($entry->getUser(), $database); |
|
| 179 | + $nameCache[$entry->getUser()] = $entryUser; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($entry instanceof Comment) { |
|
| 183 | + $requestLogs[] = array( |
|
| 184 | + 'type' => 'comment', |
|
| 185 | + 'security' => $entry->getVisibility(), |
|
| 186 | + 'user' => $nameCache[$entry->getUser()]->getUsername(), |
|
| 187 | + 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
| 188 | + 'entry' => null, |
|
| 189 | + 'time' => $entry->getTime(), |
|
| 190 | + 'canedit' => ($editableComments || $entry->getUser() == $currentUser->getId()), |
|
| 191 | + 'id' => $entry->getId(), |
|
| 192 | + 'comment' => $entry->getComment(), |
|
| 193 | + ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if ($entry instanceof Log) { |
|
| 197 | + $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0; |
|
| 198 | + $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()]; |
|
| 199 | + |
|
| 200 | + $requestLogs[] = array( |
|
| 201 | + 'type' => 'log', |
|
| 202 | + 'security' => 'user', |
|
| 203 | + 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
| 204 | + 'user' => $entryUser->getUsername(), |
|
| 205 | + 'entry' => LogHelper::getLogDescription($entry), |
|
| 206 | + 'time' => $entry->getTimestamp(), |
|
| 207 | + 'canedit' => false, |
|
| 208 | + 'id' => $entry->getId(), |
|
| 209 | + 'comment' => $entry->getComment(), |
|
| 210 | + ); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $this->assign("requestLogs", $requestLogs); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @param Request $request |
|
| 219 | + */ |
|
| 220 | + protected function setupUsernameData(Request $request) |
|
| 221 | + { |
|
| 222 | + $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName()); |
|
| 223 | + |
|
| 224 | + $this->assign('requestIsBlacklisted', $blacklistData !== false); |
|
| 225 | + $this->assign('requestBlacklist', $blacklistData); |
|
| 226 | + |
|
| 227 | + try { |
|
| 228 | + $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName()); |
|
| 229 | + } |
|
| 230 | + catch (Exception $ex) { |
|
| 231 | + $spoofs = $ex->getMessage(); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $this->assign("spoofs", $spoofs); |
|
| 235 | + } |
|
| 236 | 236 | } |
@@ -15,200 +15,200 @@ |
||
| 15 | 15 | |
| 16 | 16 | abstract class SearchHelperBase |
| 17 | 17 | { |
| 18 | - /** @var PdoDatabase */ |
|
| 19 | - protected $database; |
|
| 20 | - /** @var array */ |
|
| 21 | - protected $parameterList = array(); |
|
| 22 | - /** @var null|int */ |
|
| 23 | - private $limit = null; |
|
| 24 | - /** @var null|int */ |
|
| 25 | - private $offset = null; |
|
| 26 | - private $orderBy = null; |
|
| 27 | - /** |
|
| 28 | - * @var string The where clause. |
|
| 29 | - * |
|
| 30 | - * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
| 31 | - * that we use positional parameters instead of named parameters because we don't know many times different options |
|
| 32 | - * will be called (looking at excluding() here, but there's the option for others). |
|
| 33 | - */ |
|
| 34 | - protected $whereClause = ' WHERE 1 = 1'; |
|
| 35 | - /** @var string */ |
|
| 36 | - protected $table; |
|
| 37 | - protected $joinClause = ''; |
|
| 38 | - private $targetClass; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * SearchHelperBase constructor. |
|
| 42 | - * |
|
| 43 | - * @param PdoDatabase $database |
|
| 44 | - * @param string $table |
|
| 45 | - * @param $targetClass |
|
| 46 | - * @param null|string $order Order by clause, excluding ORDER BY. |
|
| 47 | - */ |
|
| 48 | - protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
| 49 | - { |
|
| 50 | - $this->database = $database; |
|
| 51 | - $this->table = $table; |
|
| 52 | - $this->orderBy = $order; |
|
| 53 | - $this->targetClass = $targetClass; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Finalises the database query, and executes it, returning a set of objects. |
|
| 58 | - * |
|
| 59 | - * @return DataObject[] |
|
| 60 | - */ |
|
| 61 | - public function fetch() |
|
| 62 | - { |
|
| 63 | - $statement = $this->getData(); |
|
| 64 | - |
|
| 65 | - /** @var DataObject[] $returnedObjects */ |
|
| 66 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 67 | - foreach ($returnedObjects as $req) { |
|
| 68 | - $req->setDatabase($this->database); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return $returnedObjects; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Finalises the database query, and executes it, returning only the requested column. |
|
| 76 | - * |
|
| 77 | - * @param string $column The required column |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - public function fetchColumn($column){ |
|
| 81 | - $statement = $this->getData(array($column)); |
|
| 82 | - |
|
| 83 | - return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function fetchMap($column){ |
|
| 87 | - $statement = $this->getData(array('id', $column)); |
|
| 88 | - |
|
| 89 | - $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 90 | - $map = array(); |
|
| 91 | - |
|
| 92 | - foreach ($data as $row) { |
|
| 93 | - $map[$row['id']] = $row[$column]; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return $map; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param int $count Returns the record count of the result set |
|
| 101 | - * |
|
| 102 | - * @return $this |
|
| 103 | - */ |
|
| 104 | - public function getRecordCount(&$count) |
|
| 105 | - { |
|
| 106 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 107 | - $query .= $this->joinClause . $this->whereClause; |
|
| 108 | - |
|
| 109 | - $statement = $this->database->prepare($query); |
|
| 110 | - $statement->execute($this->parameterList); |
|
| 111 | - |
|
| 112 | - $count = $statement->fetchColumn(0); |
|
| 113 | - $statement->closeCursor(); |
|
| 114 | - |
|
| 115 | - return $this; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Limits the results |
|
| 120 | - * |
|
| 121 | - * @param integer $limit |
|
| 122 | - * @param integer|null $offset |
|
| 123 | - * |
|
| 124 | - * @return $this |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - public function limit($limit, $offset = null) |
|
| 128 | - { |
|
| 129 | - $this->limit = $limit; |
|
| 130 | - $this->offset = $offset; |
|
| 131 | - |
|
| 132 | - return $this; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - private function applyLimit() |
|
| 136 | - { |
|
| 137 | - $clause = ''; |
|
| 138 | - if ($this->limit !== null) { |
|
| 139 | - $clause = ' LIMIT ?'; |
|
| 140 | - $this->parameterList[] = $this->limit; |
|
| 141 | - |
|
| 142 | - if ($this->offset !== null) { |
|
| 143 | - $clause .= ' OFFSET ?'; |
|
| 144 | - $this->parameterList[] = $this->offset; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $clause; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - private function applyOrder() |
|
| 152 | - { |
|
| 153 | - if ($this->orderBy !== null) { |
|
| 154 | - return ' ORDER BY ' . $this->orderBy; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return ''; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param array $columns |
|
| 162 | - * |
|
| 163 | - * @return PDOStatement |
|
| 164 | - */ |
|
| 165 | - private function getData($columns = array('*')) |
|
| 166 | - { |
|
| 167 | - $query = $this->buildQuery($columns); |
|
| 168 | - $query .= $this->applyOrder(); |
|
| 169 | - $query .= $this->applyLimit(); |
|
| 170 | - |
|
| 171 | - $statement = $this->database->prepare($query); |
|
| 172 | - $statement->execute($this->parameterList); |
|
| 173 | - |
|
| 174 | - return $statement; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param array $columns |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - */ |
|
| 182 | - protected function buildQuery($columns) |
|
| 183 | - { |
|
| 184 | - $colData = array(); |
|
| 185 | - foreach ($columns as $c) { |
|
| 186 | - $colData[] = 'origin.' . $c; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $query = 'SELECT /* SearchHelper */ ' . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
| 190 | - $query .= $this->joinClause . $this->whereClause; |
|
| 191 | - |
|
| 192 | - return $query; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - public function inIds($idList) { |
|
| 196 | - $this->inClause('id', $idList); |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - protected function inClause($column, $values) { |
|
| 201 | - if (count($values) === 0) { |
|
| 202 | - return; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
| 206 | - $valueCount = count($values); |
|
| 207 | - |
|
| 208 | - // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
| 209 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
| 210 | - |
|
| 211 | - $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
| 212 | - $this->parameterList = array_merge($this->parameterList, $values); |
|
| 213 | - } |
|
| 18 | + /** @var PdoDatabase */ |
|
| 19 | + protected $database; |
|
| 20 | + /** @var array */ |
|
| 21 | + protected $parameterList = array(); |
|
| 22 | + /** @var null|int */ |
|
| 23 | + private $limit = null; |
|
| 24 | + /** @var null|int */ |
|
| 25 | + private $offset = null; |
|
| 26 | + private $orderBy = null; |
|
| 27 | + /** |
|
| 28 | + * @var string The where clause. |
|
| 29 | + * |
|
| 30 | + * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
| 31 | + * that we use positional parameters instead of named parameters because we don't know many times different options |
|
| 32 | + * will be called (looking at excluding() here, but there's the option for others). |
|
| 33 | + */ |
|
| 34 | + protected $whereClause = ' WHERE 1 = 1'; |
|
| 35 | + /** @var string */ |
|
| 36 | + protected $table; |
|
| 37 | + protected $joinClause = ''; |
|
| 38 | + private $targetClass; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * SearchHelperBase constructor. |
|
| 42 | + * |
|
| 43 | + * @param PdoDatabase $database |
|
| 44 | + * @param string $table |
|
| 45 | + * @param $targetClass |
|
| 46 | + * @param null|string $order Order by clause, excluding ORDER BY. |
|
| 47 | + */ |
|
| 48 | + protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
| 49 | + { |
|
| 50 | + $this->database = $database; |
|
| 51 | + $this->table = $table; |
|
| 52 | + $this->orderBy = $order; |
|
| 53 | + $this->targetClass = $targetClass; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Finalises the database query, and executes it, returning a set of objects. |
|
| 58 | + * |
|
| 59 | + * @return DataObject[] |
|
| 60 | + */ |
|
| 61 | + public function fetch() |
|
| 62 | + { |
|
| 63 | + $statement = $this->getData(); |
|
| 64 | + |
|
| 65 | + /** @var DataObject[] $returnedObjects */ |
|
| 66 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 67 | + foreach ($returnedObjects as $req) { |
|
| 68 | + $req->setDatabase($this->database); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return $returnedObjects; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Finalises the database query, and executes it, returning only the requested column. |
|
| 76 | + * |
|
| 77 | + * @param string $column The required column |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + public function fetchColumn($column){ |
|
| 81 | + $statement = $this->getData(array($column)); |
|
| 82 | + |
|
| 83 | + return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function fetchMap($column){ |
|
| 87 | + $statement = $this->getData(array('id', $column)); |
|
| 88 | + |
|
| 89 | + $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 90 | + $map = array(); |
|
| 91 | + |
|
| 92 | + foreach ($data as $row) { |
|
| 93 | + $map[$row['id']] = $row[$column]; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return $map; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param int $count Returns the record count of the result set |
|
| 101 | + * |
|
| 102 | + * @return $this |
|
| 103 | + */ |
|
| 104 | + public function getRecordCount(&$count) |
|
| 105 | + { |
|
| 106 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 107 | + $query .= $this->joinClause . $this->whereClause; |
|
| 108 | + |
|
| 109 | + $statement = $this->database->prepare($query); |
|
| 110 | + $statement->execute($this->parameterList); |
|
| 111 | + |
|
| 112 | + $count = $statement->fetchColumn(0); |
|
| 113 | + $statement->closeCursor(); |
|
| 114 | + |
|
| 115 | + return $this; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Limits the results |
|
| 120 | + * |
|
| 121 | + * @param integer $limit |
|
| 122 | + * @param integer|null $offset |
|
| 123 | + * |
|
| 124 | + * @return $this |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + public function limit($limit, $offset = null) |
|
| 128 | + { |
|
| 129 | + $this->limit = $limit; |
|
| 130 | + $this->offset = $offset; |
|
| 131 | + |
|
| 132 | + return $this; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + private function applyLimit() |
|
| 136 | + { |
|
| 137 | + $clause = ''; |
|
| 138 | + if ($this->limit !== null) { |
|
| 139 | + $clause = ' LIMIT ?'; |
|
| 140 | + $this->parameterList[] = $this->limit; |
|
| 141 | + |
|
| 142 | + if ($this->offset !== null) { |
|
| 143 | + $clause .= ' OFFSET ?'; |
|
| 144 | + $this->parameterList[] = $this->offset; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $clause; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + private function applyOrder() |
|
| 152 | + { |
|
| 153 | + if ($this->orderBy !== null) { |
|
| 154 | + return ' ORDER BY ' . $this->orderBy; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return ''; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param array $columns |
|
| 162 | + * |
|
| 163 | + * @return PDOStatement |
|
| 164 | + */ |
|
| 165 | + private function getData($columns = array('*')) |
|
| 166 | + { |
|
| 167 | + $query = $this->buildQuery($columns); |
|
| 168 | + $query .= $this->applyOrder(); |
|
| 169 | + $query .= $this->applyLimit(); |
|
| 170 | + |
|
| 171 | + $statement = $this->database->prepare($query); |
|
| 172 | + $statement->execute($this->parameterList); |
|
| 173 | + |
|
| 174 | + return $statement; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param array $columns |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + */ |
|
| 182 | + protected function buildQuery($columns) |
|
| 183 | + { |
|
| 184 | + $colData = array(); |
|
| 185 | + foreach ($columns as $c) { |
|
| 186 | + $colData[] = 'origin.' . $c; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $query = 'SELECT /* SearchHelper */ ' . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
| 190 | + $query .= $this->joinClause . $this->whereClause; |
|
| 191 | + |
|
| 192 | + return $query; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + public function inIds($idList) { |
|
| 196 | + $this->inClause('id', $idList); |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + protected function inClause($column, $values) { |
|
| 201 | + if (count($values) === 0) { |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
| 206 | + $valueCount = count($values); |
|
| 207 | + |
|
| 208 | + // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
| 209 | + $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
| 210 | + |
|
| 211 | + $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
| 212 | + $this->parameterList = array_merge($this->parameterList, $values); |
|
| 213 | + } |
|
| 214 | 214 | } |
@@ -26,347 +26,347 @@ |
||
| 26 | 26 | |
| 27 | 27 | class LogHelper |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * Summary of getRequestLogsWithComments |
|
| 31 | - * |
|
| 32 | - * @param int $requestId |
|
| 33 | - * @param PdoDatabase $db |
|
| 34 | - * @param SecurityManager $securityManager |
|
| 35 | - * |
|
| 36 | - * @return \Waca\DataObject[] |
|
| 37 | - */ |
|
| 38 | - public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
| 39 | - { |
|
| 40 | - $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 41 | - |
|
| 42 | - $currentUser = User::getCurrent($db); |
|
| 43 | - $securityResult = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser); |
|
| 44 | - $showAllComments = $securityResult === SecurityManager::ALLOWED; |
|
| 45 | - |
|
| 46 | - $comments = Comment::getForRequest($requestId, $db, $showAllComments, $currentUser->getId()); |
|
| 47 | - |
|
| 48 | - $items = array_merge($logs, $comments); |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param DataObject $item |
|
| 52 | - * |
|
| 53 | - * @return int |
|
| 54 | - */ |
|
| 55 | - $sortKey = function(DataObject $item) { |
|
| 56 | - if ($item instanceof Log) { |
|
| 57 | - return $item->getTimestamp()->getTimestamp(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ($item instanceof Comment) { |
|
| 61 | - return $item->getTime()->getTimestamp(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - return 0; |
|
| 65 | - }; |
|
| 66 | - |
|
| 67 | - do { |
|
| 68 | - $flag = false; |
|
| 69 | - |
|
| 70 | - $loopLimit = (count($items) - 1); |
|
| 71 | - for ($i = 0; $i < $loopLimit; $i++) { |
|
| 72 | - // are these two items out of order? |
|
| 73 | - if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
| 74 | - // swap them |
|
| 75 | - $swap = $items[$i]; |
|
| 76 | - $items[$i] = $items[$i + 1]; |
|
| 77 | - $items[$i + 1] = $swap; |
|
| 78 | - |
|
| 79 | - // set a flag to say we've modified the array this time around |
|
| 80 | - $flag = true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - while ($flag); |
|
| 85 | - |
|
| 86 | - return $items; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Summary of getLogDescription |
|
| 91 | - * |
|
| 92 | - * @param Log $entry |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public static function getLogDescription(Log $entry) |
|
| 97 | - { |
|
| 98 | - $text = "Deferred to "; |
|
| 99 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 100 | - // Deferred to a different queue |
|
| 101 | - // This is exactly what we want to display. |
|
| 102 | - return $entry->getAction(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $text = "Closed custom-n"; |
|
| 106 | - if ($entry->getAction() == $text) { |
|
| 107 | - // Custom-closed |
|
| 108 | - return "closed (custom reason - account not created)"; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $text = "Closed custom-y"; |
|
| 112 | - if ($entry->getAction() == $text) { |
|
| 113 | - // Custom-closed |
|
| 114 | - return "closed (custom reason - account created)"; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $text = "Closed 0"; |
|
| 118 | - if ($entry->getAction() == $text) { |
|
| 119 | - // Dropped the request - short-circuit the lookup |
|
| 120 | - return "dropped request"; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $text = "Closed "; |
|
| 124 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 125 | - // Closed with a reason - do a lookup here. |
|
| 126 | - $id = substr($entry->getAction(), strlen($text)); |
|
| 127 | - /** @var EmailTemplate $template */ |
|
| 128 | - $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 129 | - |
|
| 130 | - if ($template != false) { |
|
| 131 | - return "closed (" . $template->getName() . ")"; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Fall back to the basic stuff |
|
| 136 | - $lookup = array( |
|
| 137 | - 'Reserved' => 'reserved', |
|
| 138 | - 'Email Confirmed' => 'email-confirmed', |
|
| 139 | - 'Unreserved' => 'unreserved', |
|
| 140 | - 'Approved' => 'approved', |
|
| 141 | - 'Suspended' => 'suspended', |
|
| 142 | - 'RoleChange' => 'changed roles', |
|
| 143 | - 'Banned' => 'banned', |
|
| 144 | - 'Edited' => 'edited interface message', |
|
| 145 | - 'Declined' => 'declined', |
|
| 146 | - 'EditComment-c' => 'edited a comment', |
|
| 147 | - 'EditComment-r' => 'edited a comment', |
|
| 148 | - 'Unbanned' => 'unbanned', |
|
| 149 | - 'Promoted' => 'promoted to tool admin', |
|
| 150 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 151 | - 'Prefchange' => 'changed user preferences', |
|
| 152 | - 'Renamed' => 'renamed', |
|
| 153 | - 'Demoted' => 'demoted from tool admin', |
|
| 154 | - 'ReceiveReserved' => 'received the reservation', |
|
| 155 | - 'SendReserved' => 'sent the reservation', |
|
| 156 | - 'EditedEmail' => 'edited email', |
|
| 157 | - 'DeletedTemplate' => 'deleted template', |
|
| 158 | - 'EditedTemplate' => 'edited template', |
|
| 159 | - 'CreatedEmail' => 'created email', |
|
| 160 | - 'CreatedTemplate' => 'created template', |
|
| 161 | - 'SentMail' => 'sent an email to the requestor', |
|
| 162 | - 'Registered' => 'registered a tool account', |
|
| 163 | - ); |
|
| 164 | - |
|
| 165 | - if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 166 | - return $lookup[$entry->getAction()]; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // OK, I don't know what this is. Fall back to something sane. |
|
| 170 | - return "performed an unknown action ({$entry->getAction()})"; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param PdoDatabase $database |
|
| 175 | - * |
|
| 176 | - * @return array |
|
| 177 | - */ |
|
| 178 | - public static function getLogActions(PdoDatabase $database) |
|
| 179 | - { |
|
| 180 | - $lookup = array( |
|
| 181 | - 'Reserved' => 'reserved', |
|
| 182 | - 'Email Confirmed' => 'email-confirmed', |
|
| 183 | - 'Unreserved' => 'unreserved', |
|
| 184 | - 'Approved' => 'approved', |
|
| 185 | - 'Suspended' => 'suspended', |
|
| 186 | - 'RoleChange' => 'changed roles', |
|
| 187 | - 'Banned' => 'banned', |
|
| 188 | - 'Edited' => 'edited interface message', |
|
| 189 | - 'Declined' => 'declined', |
|
| 190 | - 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 191 | - 'EditComment-r' => 'edited a comment (by request)', |
|
| 192 | - 'Unbanned' => 'unbanned', |
|
| 193 | - 'Promoted' => 'promoted to tool admin', |
|
| 194 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 195 | - 'Prefchange' => 'changed user preferences', |
|
| 196 | - 'Renamed' => 'renamed', |
|
| 197 | - 'Demoted' => 'demoted from tool admin', |
|
| 198 | - 'ReceiveReserved' => 'received the reservation', |
|
| 199 | - 'SendReserved' => 'sent the reservation', |
|
| 200 | - 'EditedEmail' => 'edited email', |
|
| 201 | - 'DeletedTemplate' => 'deleted template', |
|
| 202 | - 'EditedTemplate' => 'edited template', |
|
| 203 | - 'CreatedEmail' => 'created email', |
|
| 204 | - 'CreatedTemplate' => 'created template', |
|
| 205 | - 'SentMail' => 'sent an email to the requestor', |
|
| 206 | - 'Registered' => 'registered a tool account', |
|
| 207 | - 'Closed 0' => 'dropped request', |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - $statement = $database->query(<<<SQL |
|
| 29 | + /** |
|
| 30 | + * Summary of getRequestLogsWithComments |
|
| 31 | + * |
|
| 32 | + * @param int $requestId |
|
| 33 | + * @param PdoDatabase $db |
|
| 34 | + * @param SecurityManager $securityManager |
|
| 35 | + * |
|
| 36 | + * @return \Waca\DataObject[] |
|
| 37 | + */ |
|
| 38 | + public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
| 39 | + { |
|
| 40 | + $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 41 | + |
|
| 42 | + $currentUser = User::getCurrent($db); |
|
| 43 | + $securityResult = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser); |
|
| 44 | + $showAllComments = $securityResult === SecurityManager::ALLOWED; |
|
| 45 | + |
|
| 46 | + $comments = Comment::getForRequest($requestId, $db, $showAllComments, $currentUser->getId()); |
|
| 47 | + |
|
| 48 | + $items = array_merge($logs, $comments); |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param DataObject $item |
|
| 52 | + * |
|
| 53 | + * @return int |
|
| 54 | + */ |
|
| 55 | + $sortKey = function(DataObject $item) { |
|
| 56 | + if ($item instanceof Log) { |
|
| 57 | + return $item->getTimestamp()->getTimestamp(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ($item instanceof Comment) { |
|
| 61 | + return $item->getTime()->getTimestamp(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + return 0; |
|
| 65 | + }; |
|
| 66 | + |
|
| 67 | + do { |
|
| 68 | + $flag = false; |
|
| 69 | + |
|
| 70 | + $loopLimit = (count($items) - 1); |
|
| 71 | + for ($i = 0; $i < $loopLimit; $i++) { |
|
| 72 | + // are these two items out of order? |
|
| 73 | + if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
| 74 | + // swap them |
|
| 75 | + $swap = $items[$i]; |
|
| 76 | + $items[$i] = $items[$i + 1]; |
|
| 77 | + $items[$i + 1] = $swap; |
|
| 78 | + |
|
| 79 | + // set a flag to say we've modified the array this time around |
|
| 80 | + $flag = true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + while ($flag); |
|
| 85 | + |
|
| 86 | + return $items; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Summary of getLogDescription |
|
| 91 | + * |
|
| 92 | + * @param Log $entry |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public static function getLogDescription(Log $entry) |
|
| 97 | + { |
|
| 98 | + $text = "Deferred to "; |
|
| 99 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 100 | + // Deferred to a different queue |
|
| 101 | + // This is exactly what we want to display. |
|
| 102 | + return $entry->getAction(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $text = "Closed custom-n"; |
|
| 106 | + if ($entry->getAction() == $text) { |
|
| 107 | + // Custom-closed |
|
| 108 | + return "closed (custom reason - account not created)"; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $text = "Closed custom-y"; |
|
| 112 | + if ($entry->getAction() == $text) { |
|
| 113 | + // Custom-closed |
|
| 114 | + return "closed (custom reason - account created)"; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $text = "Closed 0"; |
|
| 118 | + if ($entry->getAction() == $text) { |
|
| 119 | + // Dropped the request - short-circuit the lookup |
|
| 120 | + return "dropped request"; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $text = "Closed "; |
|
| 124 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 125 | + // Closed with a reason - do a lookup here. |
|
| 126 | + $id = substr($entry->getAction(), strlen($text)); |
|
| 127 | + /** @var EmailTemplate $template */ |
|
| 128 | + $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 129 | + |
|
| 130 | + if ($template != false) { |
|
| 131 | + return "closed (" . $template->getName() . ")"; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Fall back to the basic stuff |
|
| 136 | + $lookup = array( |
|
| 137 | + 'Reserved' => 'reserved', |
|
| 138 | + 'Email Confirmed' => 'email-confirmed', |
|
| 139 | + 'Unreserved' => 'unreserved', |
|
| 140 | + 'Approved' => 'approved', |
|
| 141 | + 'Suspended' => 'suspended', |
|
| 142 | + 'RoleChange' => 'changed roles', |
|
| 143 | + 'Banned' => 'banned', |
|
| 144 | + 'Edited' => 'edited interface message', |
|
| 145 | + 'Declined' => 'declined', |
|
| 146 | + 'EditComment-c' => 'edited a comment', |
|
| 147 | + 'EditComment-r' => 'edited a comment', |
|
| 148 | + 'Unbanned' => 'unbanned', |
|
| 149 | + 'Promoted' => 'promoted to tool admin', |
|
| 150 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 151 | + 'Prefchange' => 'changed user preferences', |
|
| 152 | + 'Renamed' => 'renamed', |
|
| 153 | + 'Demoted' => 'demoted from tool admin', |
|
| 154 | + 'ReceiveReserved' => 'received the reservation', |
|
| 155 | + 'SendReserved' => 'sent the reservation', |
|
| 156 | + 'EditedEmail' => 'edited email', |
|
| 157 | + 'DeletedTemplate' => 'deleted template', |
|
| 158 | + 'EditedTemplate' => 'edited template', |
|
| 159 | + 'CreatedEmail' => 'created email', |
|
| 160 | + 'CreatedTemplate' => 'created template', |
|
| 161 | + 'SentMail' => 'sent an email to the requestor', |
|
| 162 | + 'Registered' => 'registered a tool account', |
|
| 163 | + ); |
|
| 164 | + |
|
| 165 | + if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 166 | + return $lookup[$entry->getAction()]; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // OK, I don't know what this is. Fall back to something sane. |
|
| 170 | + return "performed an unknown action ({$entry->getAction()})"; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param PdoDatabase $database |
|
| 175 | + * |
|
| 176 | + * @return array |
|
| 177 | + */ |
|
| 178 | + public static function getLogActions(PdoDatabase $database) |
|
| 179 | + { |
|
| 180 | + $lookup = array( |
|
| 181 | + 'Reserved' => 'reserved', |
|
| 182 | + 'Email Confirmed' => 'email-confirmed', |
|
| 183 | + 'Unreserved' => 'unreserved', |
|
| 184 | + 'Approved' => 'approved', |
|
| 185 | + 'Suspended' => 'suspended', |
|
| 186 | + 'RoleChange' => 'changed roles', |
|
| 187 | + 'Banned' => 'banned', |
|
| 188 | + 'Edited' => 'edited interface message', |
|
| 189 | + 'Declined' => 'declined', |
|
| 190 | + 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 191 | + 'EditComment-r' => 'edited a comment (by request)', |
|
| 192 | + 'Unbanned' => 'unbanned', |
|
| 193 | + 'Promoted' => 'promoted to tool admin', |
|
| 194 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 195 | + 'Prefchange' => 'changed user preferences', |
|
| 196 | + 'Renamed' => 'renamed', |
|
| 197 | + 'Demoted' => 'demoted from tool admin', |
|
| 198 | + 'ReceiveReserved' => 'received the reservation', |
|
| 199 | + 'SendReserved' => 'sent the reservation', |
|
| 200 | + 'EditedEmail' => 'edited email', |
|
| 201 | + 'DeletedTemplate' => 'deleted template', |
|
| 202 | + 'EditedTemplate' => 'edited template', |
|
| 203 | + 'CreatedEmail' => 'created email', |
|
| 204 | + 'CreatedTemplate' => 'created template', |
|
| 205 | + 'SentMail' => 'sent an email to the requestor', |
|
| 206 | + 'Registered' => 'registered a tool account', |
|
| 207 | + 'Closed 0' => 'dropped request', |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + $statement = $database->query(<<<SQL |
|
| 211 | 211 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v |
| 212 | 212 | FROM emailtemplate; |
| 213 | 213 | SQL |
| 214 | - ); |
|
| 215 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 216 | - $lookup[$row['k']] = $row['v']; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return $lookup; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * This returns a HTML |
|
| 224 | - * |
|
| 225 | - * @param string $objectId |
|
| 226 | - * @param string $objectType |
|
| 227 | - * @param PdoDatabase $database |
|
| 228 | - * @param SiteConfiguration $configuration |
|
| 229 | - * |
|
| 230 | - * @return null|string |
|
| 231 | - * @category Security-Critical |
|
| 232 | - */ |
|
| 233 | - private static function getObjectDescription( |
|
| 234 | - $objectId, |
|
| 235 | - $objectType, |
|
| 236 | - PdoDatabase $database, |
|
| 237 | - SiteConfiguration $configuration |
|
| 238 | - ) { |
|
| 239 | - if ($objectType == '') { |
|
| 240 | - return null; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - $baseurl = $configuration->getBaseUrl(); |
|
| 244 | - |
|
| 245 | - switch ($objectType) { |
|
| 246 | - case 'Ban': |
|
| 247 | - /** @var Ban $ban */ |
|
| 248 | - $ban = Ban::getById($objectId, $database); |
|
| 249 | - |
|
| 250 | - return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
|
| 251 | - case 'EmailTemplate': |
|
| 252 | - /** @var EmailTemplate $emailTemplate */ |
|
| 253 | - $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 254 | - $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 255 | - |
|
| 256 | - return <<<HTML |
|
| 214 | + ); |
|
| 215 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 216 | + $lookup[$row['k']] = $row['v']; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return $lookup; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * This returns a HTML |
|
| 224 | + * |
|
| 225 | + * @param string $objectId |
|
| 226 | + * @param string $objectType |
|
| 227 | + * @param PdoDatabase $database |
|
| 228 | + * @param SiteConfiguration $configuration |
|
| 229 | + * |
|
| 230 | + * @return null|string |
|
| 231 | + * @category Security-Critical |
|
| 232 | + */ |
|
| 233 | + private static function getObjectDescription( |
|
| 234 | + $objectId, |
|
| 235 | + $objectType, |
|
| 236 | + PdoDatabase $database, |
|
| 237 | + SiteConfiguration $configuration |
|
| 238 | + ) { |
|
| 239 | + if ($objectType == '') { |
|
| 240 | + return null; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + $baseurl = $configuration->getBaseUrl(); |
|
| 244 | + |
|
| 245 | + switch ($objectType) { |
|
| 246 | + case 'Ban': |
|
| 247 | + /** @var Ban $ban */ |
|
| 248 | + $ban = Ban::getById($objectId, $database); |
|
| 249 | + |
|
| 250 | + return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
|
| 251 | + case 'EmailTemplate': |
|
| 252 | + /** @var EmailTemplate $emailTemplate */ |
|
| 253 | + $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 254 | + $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 255 | + |
|
| 256 | + return <<<HTML |
|
| 257 | 257 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
| 258 | 258 | HTML; |
| 259 | - case 'SiteNotice': |
|
| 260 | - return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 261 | - case 'Request': |
|
| 262 | - /** @var Request $request */ |
|
| 263 | - $request = Request::getById($objectId, $database); |
|
| 264 | - $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 265 | - |
|
| 266 | - return <<<HTML |
|
| 259 | + case 'SiteNotice': |
|
| 260 | + return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 261 | + case 'Request': |
|
| 262 | + /** @var Request $request */ |
|
| 263 | + $request = Request::getById($objectId, $database); |
|
| 264 | + $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 265 | + |
|
| 266 | + return <<<HTML |
|
| 267 | 267 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
| 268 | 268 | HTML; |
| 269 | - case 'User': |
|
| 270 | - /** @var User $user */ |
|
| 271 | - $user = User::getById($objectId, $database); |
|
| 272 | - $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 273 | - |
|
| 274 | - return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 275 | - case 'WelcomeTemplate': |
|
| 276 | - /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 277 | - $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 278 | - |
|
| 279 | - // some old templates have been completely deleted and lost to the depths of time. |
|
| 280 | - if ($welcomeTemplate === false) { |
|
| 281 | - return "Welcome template #{$objectId}"; |
|
| 282 | - } |
|
| 283 | - else { |
|
| 284 | - $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 285 | - |
|
| 286 | - return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 287 | - } |
|
| 288 | - default: |
|
| 289 | - return '[' . $objectType . " " . $objectId . ']'; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param Log[] $logs |
|
| 295 | - * @param PdoDatabase $database |
|
| 296 | - * @param SiteConfiguration $configuration |
|
| 297 | - * |
|
| 298 | - * @return array |
|
| 299 | - * @throws Exception |
|
| 300 | - */ |
|
| 301 | - public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
| 302 | - { |
|
| 303 | - $userIds = array(); |
|
| 304 | - |
|
| 305 | - /** @var Log $logEntry */ |
|
| 306 | - foreach ($logs as $logEntry) { |
|
| 307 | - if (!$logEntry instanceof Log) { |
|
| 308 | - // if this happens, we've done something wrong with passing back the log data. |
|
| 309 | - throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $user = $logEntry->getUser(); |
|
| 313 | - if ($user === -1) { |
|
| 314 | - continue; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - if (!array_search($user, $userIds)) { |
|
| 318 | - $userIds[] = $user; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 323 | - $users[-1] = User::getCommunity()->getUsername(); |
|
| 324 | - |
|
| 325 | - $logData = array(); |
|
| 326 | - |
|
| 327 | - /** @var Log $logEntry */ |
|
| 328 | - foreach ($logs as $logEntry) { |
|
| 329 | - $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 330 | - $database, $configuration); |
|
| 331 | - |
|
| 332 | - if ($logEntry->getAction() === 'Renamed') { |
|
| 333 | - $renameData = unserialize($logEntry->getComment()); |
|
| 334 | - $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 335 | - $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 336 | - $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 337 | - } |
|
| 338 | - else if ($logEntry->getAction() === 'RoleChange') { |
|
| 339 | - $roleChangeData = unserialize($logEntry->getComment()); |
|
| 340 | - |
|
| 341 | - $removed = array(); |
|
| 342 | - foreach ($roleChangeData['removed'] as $r) { |
|
| 343 | - $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - $added = array(); |
|
| 347 | - foreach ($roleChangeData['added'] as $r) { |
|
| 348 | - $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 352 | - |
|
| 353 | - $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 354 | - $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 355 | - } |
|
| 356 | - else { |
|
| 357 | - $comment = $logEntry->getComment(); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $logData[] = array( |
|
| 361 | - 'timestamp' => $logEntry->getTimestamp(), |
|
| 362 | - 'userid' => $logEntry->getUser(), |
|
| 363 | - 'username' => $users[$logEntry->getUser()], |
|
| 364 | - 'description' => self::getLogDescription($logEntry), |
|
| 365 | - 'objectdescription' => $objectDescription, |
|
| 366 | - 'comment' => $comment, |
|
| 367 | - ); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return array($users, $logData); |
|
| 371 | - } |
|
| 269 | + case 'User': |
|
| 270 | + /** @var User $user */ |
|
| 271 | + $user = User::getById($objectId, $database); |
|
| 272 | + $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 273 | + |
|
| 274 | + return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 275 | + case 'WelcomeTemplate': |
|
| 276 | + /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 277 | + $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 278 | + |
|
| 279 | + // some old templates have been completely deleted and lost to the depths of time. |
|
| 280 | + if ($welcomeTemplate === false) { |
|
| 281 | + return "Welcome template #{$objectId}"; |
|
| 282 | + } |
|
| 283 | + else { |
|
| 284 | + $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 285 | + |
|
| 286 | + return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 287 | + } |
|
| 288 | + default: |
|
| 289 | + return '[' . $objectType . " " . $objectId . ']'; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param Log[] $logs |
|
| 295 | + * @param PdoDatabase $database |
|
| 296 | + * @param SiteConfiguration $configuration |
|
| 297 | + * |
|
| 298 | + * @return array |
|
| 299 | + * @throws Exception |
|
| 300 | + */ |
|
| 301 | + public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
| 302 | + { |
|
| 303 | + $userIds = array(); |
|
| 304 | + |
|
| 305 | + /** @var Log $logEntry */ |
|
| 306 | + foreach ($logs as $logEntry) { |
|
| 307 | + if (!$logEntry instanceof Log) { |
|
| 308 | + // if this happens, we've done something wrong with passing back the log data. |
|
| 309 | + throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $user = $logEntry->getUser(); |
|
| 313 | + if ($user === -1) { |
|
| 314 | + continue; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + if (!array_search($user, $userIds)) { |
|
| 318 | + $userIds[] = $user; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 323 | + $users[-1] = User::getCommunity()->getUsername(); |
|
| 324 | + |
|
| 325 | + $logData = array(); |
|
| 326 | + |
|
| 327 | + /** @var Log $logEntry */ |
|
| 328 | + foreach ($logs as $logEntry) { |
|
| 329 | + $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 330 | + $database, $configuration); |
|
| 331 | + |
|
| 332 | + if ($logEntry->getAction() === 'Renamed') { |
|
| 333 | + $renameData = unserialize($logEntry->getComment()); |
|
| 334 | + $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 335 | + $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 336 | + $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 337 | + } |
|
| 338 | + else if ($logEntry->getAction() === 'RoleChange') { |
|
| 339 | + $roleChangeData = unserialize($logEntry->getComment()); |
|
| 340 | + |
|
| 341 | + $removed = array(); |
|
| 342 | + foreach ($roleChangeData['removed'] as $r) { |
|
| 343 | + $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + $added = array(); |
|
| 347 | + foreach ($roleChangeData['added'] as $r) { |
|
| 348 | + $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 352 | + |
|
| 353 | + $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 354 | + $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 355 | + } |
|
| 356 | + else { |
|
| 357 | + $comment = $logEntry->getComment(); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $logData[] = array( |
|
| 361 | + 'timestamp' => $logEntry->getTimestamp(), |
|
| 362 | + 'userid' => $logEntry->getUser(), |
|
| 363 | + 'username' => $users[$logEntry->getUser()], |
|
| 364 | + 'description' => self::getLogDescription($logEntry), |
|
| 365 | + 'objectdescription' => $objectDescription, |
|
| 366 | + 'comment' => $comment, |
|
| 367 | + ); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return array($users, $logData); |
|
| 371 | + } |
|
| 372 | 372 | } |
@@ -20,242 +20,242 @@ |
||
| 20 | 20 | |
| 21 | 21 | class PageCloseRequest extends RequestActionBase |
| 22 | 22 | { |
| 23 | - protected function main() |
|
| 24 | - { |
|
| 25 | - $this->processClose(); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Main function for this page, when no specific actions are called. |
|
| 30 | - * @throws ApplicationLogicException |
|
| 31 | - */ |
|
| 32 | - final protected function processClose() |
|
| 33 | - { |
|
| 34 | - $this->checkPosted(); |
|
| 35 | - $database = $this->getDatabase(); |
|
| 36 | - |
|
| 37 | - $currentUser = User::getCurrent($database); |
|
| 38 | - $template = $this->getTemplate($database); |
|
| 39 | - $request = $this->getRequest($database); |
|
| 40 | - $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
| 41 | - |
|
| 42 | - if ($request->getStatus() === 'Closed') { |
|
| 43 | - throw new ApplicationLogicException('Request is already closed'); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - if ($this->confirmEmailAlreadySent($request, $template)) { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) { |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - if ($this->confirmAccountCreated($request, $template)) { |
|
| 55 | - return; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // I think we're good here... |
|
| 59 | - $request->setStatus('Closed'); |
|
| 60 | - $request->setReserved(null); |
|
| 61 | - |
|
| 62 | - Logger::closeRequest($database, $request, $template->getId(), null); |
|
| 63 | - |
|
| 64 | - $request->save(); |
|
| 65 | - |
|
| 66 | - // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
| 67 | - // be rolled back. |
|
| 68 | - |
|
| 69 | - $this->getNotificationHelper()->requestClosed($request, $template->getName()); |
|
| 70 | - SessionAlert::success("Request {$request->getId()} has been closed"); |
|
| 71 | - |
|
| 72 | - $this->sendMail($request, $template->getText(), $currentUser, false); |
|
| 73 | - |
|
| 74 | - $this->redirect(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param PdoDatabase $database |
|
| 79 | - * |
|
| 80 | - * @return EmailTemplate |
|
| 81 | - * @throws ApplicationLogicException |
|
| 82 | - */ |
|
| 83 | - protected function getTemplate(PdoDatabase $database) |
|
| 84 | - { |
|
| 85 | - $templateId = WebRequest::postInt('template'); |
|
| 86 | - if ($templateId === null) { |
|
| 87 | - throw new ApplicationLogicException('No template specified'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** @var EmailTemplate $template */ |
|
| 91 | - $template = EmailTemplate::getById($templateId, $database); |
|
| 92 | - if ($template === false || !$template->getActive()) { |
|
| 93 | - throw new ApplicationLogicException('Invalid or inactive template specified'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return $template; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param Request $request |
|
| 101 | - * @param EmailTemplate $template |
|
| 102 | - * |
|
| 103 | - * @return bool |
|
| 104 | - */ |
|
| 105 | - protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
| 106 | - { |
|
| 107 | - if ($this->checkEmailAlreadySent($request)) { |
|
| 108 | - $this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl'); |
|
| 109 | - |
|
| 110 | - return true; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - protected function checkEmailAlreadySent(Request $request) |
|
| 117 | - { |
|
| 118 | - if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) { |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - protected function checkReserveOverride(Request $request, User $currentUser) |
|
| 126 | - { |
|
| 127 | - $reservationId = $request->getReserved(); |
|
| 128 | - |
|
| 129 | - if ($reservationId !== 0 && $reservationId !== null) { |
|
| 130 | - if (!WebRequest::postBoolean('reserveOverride')) { |
|
| 131 | - if ($currentUser->getId() !== $reservationId) { |
|
| 132 | - return true; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param Request $request |
|
| 142 | - * @param EmailTemplate $template |
|
| 143 | - * @param User $currentUser |
|
| 144 | - * @param PdoDatabase $database |
|
| 145 | - * |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - protected function confirmReserveOverride( |
|
| 149 | - Request $request, |
|
| 150 | - EmailTemplate $template, |
|
| 151 | - User $currentUser, |
|
| 152 | - PdoDatabase $database |
|
| 153 | - ) { |
|
| 154 | - if ($this->checkReserveOverride($request, $currentUser)) { |
|
| 155 | - $this->assign('reserveUser', User::getById($request->getReserved(), $database)->getUsername()); |
|
| 156 | - $this->showConfirmation($request, $template, 'close-confirmations/reserve-override.tpl'); |
|
| 157 | - |
|
| 158 | - return true; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @param Request $request |
|
| 166 | - * @param EmailTemplate $template |
|
| 167 | - * |
|
| 168 | - * @return bool |
|
| 169 | - * @throws \Waca\Exceptions\CurlException |
|
| 170 | - */ |
|
| 171 | - protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
| 172 | - { |
|
| 173 | - if ($this->checkAccountCreated($request, $template)) { |
|
| 174 | - $this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl'); |
|
| 175 | - |
|
| 176 | - return true; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - protected function checkAccountCreated(Request $request, EmailTemplate $template) |
|
| 183 | - { |
|
| 184 | - if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) { |
|
| 185 | - $parameters = array( |
|
| 186 | - 'action' => 'query', |
|
| 187 | - 'list' => 'users', |
|
| 188 | - 'format' => 'php', |
|
| 189 | - 'ususers' => $request->getName(), |
|
| 190 | - ); |
|
| 191 | - |
|
| 192 | - $content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(), |
|
| 193 | - $parameters); |
|
| 194 | - |
|
| 195 | - $apiResult = unserialize($content); |
|
| 196 | - $exists = !isset($apiResult['query']['users']['0']['missing']); |
|
| 197 | - |
|
| 198 | - if (!$exists) { |
|
| 199 | - return true; |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param Request $request |
|
| 208 | - * @param string $mailText |
|
| 209 | - * @param User $currentUser |
|
| 210 | - * @param boolean $ccMailingList |
|
| 211 | - */ |
|
| 212 | - protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
|
| 213 | - { |
|
| 214 | - $headers = array( |
|
| 215 | - 'X-ACC-Request' => $request->getId(), |
|
| 216 | - 'X-ACC-UserID' => $currentUser->getId(), |
|
| 217 | - ); |
|
| 218 | - |
|
| 219 | - if ($ccMailingList) { |
|
| 220 | - $headers['Cc'] = '[email protected]'; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $helper = $this->getEmailHelper(); |
|
| 224 | - |
|
| 225 | - $emailSig = $currentUser->getEmailSig(); |
|
| 226 | - if ($emailSig !== '' || $emailSig !== null) { |
|
| 227 | - $emailSig = "\n\n" . $emailSig; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $subject = "RE: [ACC #{$request->getId()}] English Wikipedia Account Request"; |
|
| 231 | - $content = $mailText . $emailSig; |
|
| 232 | - |
|
| 233 | - $helper->sendMail($request->getEmail(), $subject, $content, $headers); |
|
| 234 | - |
|
| 235 | - $request->setEmailSent(true); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @param Request $request |
|
| 240 | - * @param EmailTemplate $template |
|
| 241 | - * @param string $templateName |
|
| 242 | - * |
|
| 243 | - * @throws Exception |
|
| 244 | - * @return void |
|
| 245 | - */ |
|
| 246 | - protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
|
| 247 | - { |
|
| 248 | - $this->assignCSRFToken(); |
|
| 249 | - |
|
| 250 | - $this->assign('request', $request->getId()); |
|
| 251 | - $this->assign('template', $template->getId()); |
|
| 252 | - |
|
| 253 | - $this->assign('updateversion', $request->getUpdateVersion()); |
|
| 254 | - |
|
| 255 | - $this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false'); |
|
| 256 | - $this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false'); |
|
| 257 | - $this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false'); |
|
| 258 | - |
|
| 259 | - $this->setTemplate($templateName); |
|
| 260 | - } |
|
| 23 | + protected function main() |
|
| 24 | + { |
|
| 25 | + $this->processClose(); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Main function for this page, when no specific actions are called. |
|
| 30 | + * @throws ApplicationLogicException |
|
| 31 | + */ |
|
| 32 | + final protected function processClose() |
|
| 33 | + { |
|
| 34 | + $this->checkPosted(); |
|
| 35 | + $database = $this->getDatabase(); |
|
| 36 | + |
|
| 37 | + $currentUser = User::getCurrent($database); |
|
| 38 | + $template = $this->getTemplate($database); |
|
| 39 | + $request = $this->getRequest($database); |
|
| 40 | + $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
| 41 | + |
|
| 42 | + if ($request->getStatus() === 'Closed') { |
|
| 43 | + throw new ApplicationLogicException('Request is already closed'); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + if ($this->confirmEmailAlreadySent($request, $template)) { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) { |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + if ($this->confirmAccountCreated($request, $template)) { |
|
| 55 | + return; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // I think we're good here... |
|
| 59 | + $request->setStatus('Closed'); |
|
| 60 | + $request->setReserved(null); |
|
| 61 | + |
|
| 62 | + Logger::closeRequest($database, $request, $template->getId(), null); |
|
| 63 | + |
|
| 64 | + $request->save(); |
|
| 65 | + |
|
| 66 | + // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
| 67 | + // be rolled back. |
|
| 68 | + |
|
| 69 | + $this->getNotificationHelper()->requestClosed($request, $template->getName()); |
|
| 70 | + SessionAlert::success("Request {$request->getId()} has been closed"); |
|
| 71 | + |
|
| 72 | + $this->sendMail($request, $template->getText(), $currentUser, false); |
|
| 73 | + |
|
| 74 | + $this->redirect(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param PdoDatabase $database |
|
| 79 | + * |
|
| 80 | + * @return EmailTemplate |
|
| 81 | + * @throws ApplicationLogicException |
|
| 82 | + */ |
|
| 83 | + protected function getTemplate(PdoDatabase $database) |
|
| 84 | + { |
|
| 85 | + $templateId = WebRequest::postInt('template'); |
|
| 86 | + if ($templateId === null) { |
|
| 87 | + throw new ApplicationLogicException('No template specified'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** @var EmailTemplate $template */ |
|
| 91 | + $template = EmailTemplate::getById($templateId, $database); |
|
| 92 | + if ($template === false || !$template->getActive()) { |
|
| 93 | + throw new ApplicationLogicException('Invalid or inactive template specified'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return $template; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param Request $request |
|
| 101 | + * @param EmailTemplate $template |
|
| 102 | + * |
|
| 103 | + * @return bool |
|
| 104 | + */ |
|
| 105 | + protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
| 106 | + { |
|
| 107 | + if ($this->checkEmailAlreadySent($request)) { |
|
| 108 | + $this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl'); |
|
| 109 | + |
|
| 110 | + return true; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + protected function checkEmailAlreadySent(Request $request) |
|
| 117 | + { |
|
| 118 | + if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) { |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + protected function checkReserveOverride(Request $request, User $currentUser) |
|
| 126 | + { |
|
| 127 | + $reservationId = $request->getReserved(); |
|
| 128 | + |
|
| 129 | + if ($reservationId !== 0 && $reservationId !== null) { |
|
| 130 | + if (!WebRequest::postBoolean('reserveOverride')) { |
|
| 131 | + if ($currentUser->getId() !== $reservationId) { |
|
| 132 | + return true; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param Request $request |
|
| 142 | + * @param EmailTemplate $template |
|
| 143 | + * @param User $currentUser |
|
| 144 | + * @param PdoDatabase $database |
|
| 145 | + * |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + protected function confirmReserveOverride( |
|
| 149 | + Request $request, |
|
| 150 | + EmailTemplate $template, |
|
| 151 | + User $currentUser, |
|
| 152 | + PdoDatabase $database |
|
| 153 | + ) { |
|
| 154 | + if ($this->checkReserveOverride($request, $currentUser)) { |
|
| 155 | + $this->assign('reserveUser', User::getById($request->getReserved(), $database)->getUsername()); |
|
| 156 | + $this->showConfirmation($request, $template, 'close-confirmations/reserve-override.tpl'); |
|
| 157 | + |
|
| 158 | + return true; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @param Request $request |
|
| 166 | + * @param EmailTemplate $template |
|
| 167 | + * |
|
| 168 | + * @return bool |
|
| 169 | + * @throws \Waca\Exceptions\CurlException |
|
| 170 | + */ |
|
| 171 | + protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
| 172 | + { |
|
| 173 | + if ($this->checkAccountCreated($request, $template)) { |
|
| 174 | + $this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl'); |
|
| 175 | + |
|
| 176 | + return true; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + protected function checkAccountCreated(Request $request, EmailTemplate $template) |
|
| 183 | + { |
|
| 184 | + if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) { |
|
| 185 | + $parameters = array( |
|
| 186 | + 'action' => 'query', |
|
| 187 | + 'list' => 'users', |
|
| 188 | + 'format' => 'php', |
|
| 189 | + 'ususers' => $request->getName(), |
|
| 190 | + ); |
|
| 191 | + |
|
| 192 | + $content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(), |
|
| 193 | + $parameters); |
|
| 194 | + |
|
| 195 | + $apiResult = unserialize($content); |
|
| 196 | + $exists = !isset($apiResult['query']['users']['0']['missing']); |
|
| 197 | + |
|
| 198 | + if (!$exists) { |
|
| 199 | + return true; |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param Request $request |
|
| 208 | + * @param string $mailText |
|
| 209 | + * @param User $currentUser |
|
| 210 | + * @param boolean $ccMailingList |
|
| 211 | + */ |
|
| 212 | + protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
|
| 213 | + { |
|
| 214 | + $headers = array( |
|
| 215 | + 'X-ACC-Request' => $request->getId(), |
|
| 216 | + 'X-ACC-UserID' => $currentUser->getId(), |
|
| 217 | + ); |
|
| 218 | + |
|
| 219 | + if ($ccMailingList) { |
|
| 220 | + $headers['Cc'] = '[email protected]'; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $helper = $this->getEmailHelper(); |
|
| 224 | + |
|
| 225 | + $emailSig = $currentUser->getEmailSig(); |
|
| 226 | + if ($emailSig !== '' || $emailSig !== null) { |
|
| 227 | + $emailSig = "\n\n" . $emailSig; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $subject = "RE: [ACC #{$request->getId()}] English Wikipedia Account Request"; |
|
| 231 | + $content = $mailText . $emailSig; |
|
| 232 | + |
|
| 233 | + $helper->sendMail($request->getEmail(), $subject, $content, $headers); |
|
| 234 | + |
|
| 235 | + $request->setEmailSent(true); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @param Request $request |
|
| 240 | + * @param EmailTemplate $template |
|
| 241 | + * @param string $templateName |
|
| 242 | + * |
|
| 243 | + * @throws Exception |
|
| 244 | + * @return void |
|
| 245 | + */ |
|
| 246 | + protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
|
| 247 | + { |
|
| 248 | + $this->assignCSRFToken(); |
|
| 249 | + |
|
| 250 | + $this->assign('request', $request->getId()); |
|
| 251 | + $this->assign('template', $template->getId()); |
|
| 252 | + |
|
| 253 | + $this->assign('updateversion', $request->getUpdateVersion()); |
|
| 254 | + |
|
| 255 | + $this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false'); |
|
| 256 | + $this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false'); |
|
| 257 | + $this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false'); |
|
| 258 | + |
|
| 259 | + $this->setTemplate($templateName); |
|
| 260 | + } |
|
| 261 | 261 | } |
@@ -30,224 +30,224 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class WebStart extends ApplicationBase |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @var IRequestRouter $requestRouter The request router to use. Note that different entry points have different |
|
| 35 | - * routers and hence different URL mappings |
|
| 36 | - */ |
|
| 37 | - private $requestRouter; |
|
| 38 | - /** |
|
| 39 | - * @var bool $isPublic Determines whether to use public interface objects or internal interface objects |
|
| 40 | - */ |
|
| 41 | - private $isPublic = false; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * WebStart constructor. |
|
| 45 | - * |
|
| 46 | - * @param SiteConfiguration $configuration The site configuration |
|
| 47 | - * @param IRequestRouter $router The request router to use |
|
| 48 | - */ |
|
| 49 | - public function __construct(SiteConfiguration $configuration, IRequestRouter $router) |
|
| 50 | - { |
|
| 51 | - parent::__construct($configuration); |
|
| 52 | - |
|
| 53 | - $this->requestRouter = $router; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param ITask $page |
|
| 58 | - * @param SiteConfiguration $siteConfiguration |
|
| 59 | - * @param PdoDatabase $database |
|
| 60 | - * @param PdoDatabase $notificationsDatabase |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - protected function setupHelpers( |
|
| 65 | - ITask $page, |
|
| 66 | - SiteConfiguration $siteConfiguration, |
|
| 67 | - PdoDatabase $database, |
|
| 68 | - PdoDatabase $notificationsDatabase = null |
|
| 69 | - ) { |
|
| 70 | - parent::setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase); |
|
| 71 | - |
|
| 72 | - if ($page instanceof PageBase) { |
|
| 73 | - $page->setTokenManager(new TokenManager()); |
|
| 74 | - |
|
| 75 | - if ($page instanceof InternalPageBase) { |
|
| 76 | - $page->setTypeAheadHelper(new TypeAheadHelper()); |
|
| 77 | - |
|
| 78 | - $identificationVerifier = new IdentificationVerifier($page->getHttpHelper(), $siteConfiguration, |
|
| 79 | - $database); |
|
| 80 | - $page->setIdentificationVerifier($identificationVerifier); |
|
| 81 | - |
|
| 82 | - $page->setSecurityManager(new SecurityManager($identificationVerifier, new RoleConfiguration())); |
|
| 83 | - |
|
| 84 | - if ($siteConfiguration->getTitleBlacklistEnabled()) { |
|
| 85 | - $page->setBlacklistHelper(new FakeBlacklistHelper()); |
|
| 86 | - } |
|
| 87 | - else { |
|
| 88 | - $page->setBlacklistHelper(new BlacklistHelper($page->getHttpHelper(), |
|
| 89 | - $siteConfiguration->getMediawikiWebServiceEndpoint())); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Application entry point. |
|
| 97 | - * |
|
| 98 | - * Sets up the environment and runs the application, performing any global cleanup operations when done. |
|
| 99 | - */ |
|
| 100 | - public function run() |
|
| 101 | - { |
|
| 102 | - try { |
|
| 103 | - if ($this->setupEnvironment()) { |
|
| 104 | - $this->main(); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - catch (EnvironmentException $ex) { |
|
| 108 | - ob_end_clean(); |
|
| 109 | - print Offline::getOfflineMessage($this->isPublic(), $ex->getMessage()); |
|
| 110 | - } |
|
| 111 | - catch (ReadableException $ex) { |
|
| 112 | - ob_end_clean(); |
|
| 113 | - print $ex->getReadableError(); |
|
| 114 | - } |
|
| 115 | - finally { |
|
| 116 | - $this->cleanupEnvironment(); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Environment setup |
|
| 122 | - * |
|
| 123 | - * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false |
|
| 124 | - * and shut down prematurely. |
|
| 125 | - * |
|
| 126 | - * @return bool |
|
| 127 | - * @throws EnvironmentException |
|
| 128 | - */ |
|
| 129 | - protected function setupEnvironment() |
|
| 130 | - { |
|
| 131 | - // initialise global exception handler |
|
| 132 | - set_exception_handler(array(ExceptionHandler::class, 'exceptionHandler')); |
|
| 133 | - set_error_handler(array(ExceptionHandler::class, 'errorHandler'), E_RECOVERABLE_ERROR); |
|
| 134 | - |
|
| 135 | - // start output buffering if necessary |
|
| 136 | - if (ob_get_level() === 0) { |
|
| 137 | - ob_start(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // initialise super-global providers |
|
| 141 | - WebRequest::setGlobalStateProvider(new GlobalStateProvider()); |
|
| 142 | - |
|
| 143 | - if (Offline::isOffline()) { |
|
| 144 | - print Offline::getOfflineMessage($this->isPublic()); |
|
| 145 | - ob_end_flush(); |
|
| 146 | - |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Call parent setup |
|
| 151 | - if (!parent::setupEnvironment()) { |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // Start up sessions |
|
| 156 | - Session::start(); |
|
| 157 | - |
|
| 158 | - // Check the user is allowed to be logged in still. This must be before we call any user-loading functions and |
|
| 159 | - // get the current user cached. |
|
| 160 | - // I'm not sure if this function call being here is particularly a good thing, but it's part of starting up a |
|
| 161 | - // session I suppose. |
|
| 162 | - $this->checkForceLogout(); |
|
| 163 | - |
|
| 164 | - // environment initialised! |
|
| 165 | - return true; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Main application logic |
|
| 170 | - */ |
|
| 171 | - protected function main() |
|
| 172 | - { |
|
| 173 | - // Get the right route for the request |
|
| 174 | - $page = $this->requestRouter->route(); |
|
| 175 | - |
|
| 176 | - $siteConfiguration = $this->getConfiguration(); |
|
| 177 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 178 | - |
|
| 179 | - if ($siteConfiguration->getIrcNotificationsEnabled()) { |
|
| 180 | - $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 181 | - } |
|
| 182 | - else { |
|
| 183 | - // @todo federated table here? |
|
| 184 | - $notificationsDatabase = $database; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - $this->setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase); |
|
| 188 | - |
|
| 189 | - /* @todo Remove this global statement! It's here for User.php, which does far more than it should. */ |
|
| 190 | - global $oauthHelper; |
|
| 191 | - $oauthHelper = $page->getOAuthHelper(); |
|
| 192 | - |
|
| 193 | - /* @todo Remove this global statement! It's here for Request.php, which does far more than it should. */ |
|
| 194 | - global $globalXffTrustProvider; |
|
| 195 | - $globalXffTrustProvider = $page->getXffTrustProvider(); |
|
| 196 | - |
|
| 197 | - // run the route code for the request. |
|
| 198 | - $page->execute(); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Any cleanup tasks should go here |
|
| 203 | - * |
|
| 204 | - * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
| 205 | - * This should *only* be for cleaning up, no logic should go here. |
|
| 206 | - */ |
|
| 207 | - protected function cleanupEnvironment() |
|
| 208 | - { |
|
| 209 | - // Clean up anything we splurged after sending the page. |
|
| 210 | - if (ob_get_level() > 0) { |
|
| 211 | - for ($i = ob_get_level(); $i > 0; $i--) { |
|
| 212 | - ob_end_clean(); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - private function checkForceLogout() |
|
| 218 | - { |
|
| 219 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 220 | - |
|
| 221 | - $sessionUserId = WebRequest::getSessionUserId(); |
|
| 222 | - iF ($sessionUserId === null) { |
|
| 223 | - return; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Note, User::getCurrent() caches it's result, which we *really* don't want to trigger. |
|
| 227 | - $currentUser = User::getById($sessionUserId, $database); |
|
| 228 | - |
|
| 229 | - if ($currentUser === false) { |
|
| 230 | - // Umm... this user has a session cookie with a userId set, but no user exists... |
|
| 231 | - Session::restart(); |
|
| 232 | - |
|
| 233 | - $currentUser = User::getCurrent($database); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if ($currentUser->getForceLogout()) { |
|
| 237 | - Session::restart(); |
|
| 238 | - |
|
| 239 | - $currentUser->setForceLogout(false); |
|
| 240 | - $currentUser->save(); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - public function isPublic() |
|
| 245 | - { |
|
| 246 | - return $this->isPublic; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function setPublic($isPublic) |
|
| 250 | - { |
|
| 251 | - $this->isPublic = $isPublic; |
|
| 252 | - } |
|
| 33 | + /** |
|
| 34 | + * @var IRequestRouter $requestRouter The request router to use. Note that different entry points have different |
|
| 35 | + * routers and hence different URL mappings |
|
| 36 | + */ |
|
| 37 | + private $requestRouter; |
|
| 38 | + /** |
|
| 39 | + * @var bool $isPublic Determines whether to use public interface objects or internal interface objects |
|
| 40 | + */ |
|
| 41 | + private $isPublic = false; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * WebStart constructor. |
|
| 45 | + * |
|
| 46 | + * @param SiteConfiguration $configuration The site configuration |
|
| 47 | + * @param IRequestRouter $router The request router to use |
|
| 48 | + */ |
|
| 49 | + public function __construct(SiteConfiguration $configuration, IRequestRouter $router) |
|
| 50 | + { |
|
| 51 | + parent::__construct($configuration); |
|
| 52 | + |
|
| 53 | + $this->requestRouter = $router; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param ITask $page |
|
| 58 | + * @param SiteConfiguration $siteConfiguration |
|
| 59 | + * @param PdoDatabase $database |
|
| 60 | + * @param PdoDatabase $notificationsDatabase |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + protected function setupHelpers( |
|
| 65 | + ITask $page, |
|
| 66 | + SiteConfiguration $siteConfiguration, |
|
| 67 | + PdoDatabase $database, |
|
| 68 | + PdoDatabase $notificationsDatabase = null |
|
| 69 | + ) { |
|
| 70 | + parent::setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase); |
|
| 71 | + |
|
| 72 | + if ($page instanceof PageBase) { |
|
| 73 | + $page->setTokenManager(new TokenManager()); |
|
| 74 | + |
|
| 75 | + if ($page instanceof InternalPageBase) { |
|
| 76 | + $page->setTypeAheadHelper(new TypeAheadHelper()); |
|
| 77 | + |
|
| 78 | + $identificationVerifier = new IdentificationVerifier($page->getHttpHelper(), $siteConfiguration, |
|
| 79 | + $database); |
|
| 80 | + $page->setIdentificationVerifier($identificationVerifier); |
|
| 81 | + |
|
| 82 | + $page->setSecurityManager(new SecurityManager($identificationVerifier, new RoleConfiguration())); |
|
| 83 | + |
|
| 84 | + if ($siteConfiguration->getTitleBlacklistEnabled()) { |
|
| 85 | + $page->setBlacklistHelper(new FakeBlacklistHelper()); |
|
| 86 | + } |
|
| 87 | + else { |
|
| 88 | + $page->setBlacklistHelper(new BlacklistHelper($page->getHttpHelper(), |
|
| 89 | + $siteConfiguration->getMediawikiWebServiceEndpoint())); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Application entry point. |
|
| 97 | + * |
|
| 98 | + * Sets up the environment and runs the application, performing any global cleanup operations when done. |
|
| 99 | + */ |
|
| 100 | + public function run() |
|
| 101 | + { |
|
| 102 | + try { |
|
| 103 | + if ($this->setupEnvironment()) { |
|
| 104 | + $this->main(); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + catch (EnvironmentException $ex) { |
|
| 108 | + ob_end_clean(); |
|
| 109 | + print Offline::getOfflineMessage($this->isPublic(), $ex->getMessage()); |
|
| 110 | + } |
|
| 111 | + catch (ReadableException $ex) { |
|
| 112 | + ob_end_clean(); |
|
| 113 | + print $ex->getReadableError(); |
|
| 114 | + } |
|
| 115 | + finally { |
|
| 116 | + $this->cleanupEnvironment(); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Environment setup |
|
| 122 | + * |
|
| 123 | + * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false |
|
| 124 | + * and shut down prematurely. |
|
| 125 | + * |
|
| 126 | + * @return bool |
|
| 127 | + * @throws EnvironmentException |
|
| 128 | + */ |
|
| 129 | + protected function setupEnvironment() |
|
| 130 | + { |
|
| 131 | + // initialise global exception handler |
|
| 132 | + set_exception_handler(array(ExceptionHandler::class, 'exceptionHandler')); |
|
| 133 | + set_error_handler(array(ExceptionHandler::class, 'errorHandler'), E_RECOVERABLE_ERROR); |
|
| 134 | + |
|
| 135 | + // start output buffering if necessary |
|
| 136 | + if (ob_get_level() === 0) { |
|
| 137 | + ob_start(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // initialise super-global providers |
|
| 141 | + WebRequest::setGlobalStateProvider(new GlobalStateProvider()); |
|
| 142 | + |
|
| 143 | + if (Offline::isOffline()) { |
|
| 144 | + print Offline::getOfflineMessage($this->isPublic()); |
|
| 145 | + ob_end_flush(); |
|
| 146 | + |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Call parent setup |
|
| 151 | + if (!parent::setupEnvironment()) { |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // Start up sessions |
|
| 156 | + Session::start(); |
|
| 157 | + |
|
| 158 | + // Check the user is allowed to be logged in still. This must be before we call any user-loading functions and |
|
| 159 | + // get the current user cached. |
|
| 160 | + // I'm not sure if this function call being here is particularly a good thing, but it's part of starting up a |
|
| 161 | + // session I suppose. |
|
| 162 | + $this->checkForceLogout(); |
|
| 163 | + |
|
| 164 | + // environment initialised! |
|
| 165 | + return true; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Main application logic |
|
| 170 | + */ |
|
| 171 | + protected function main() |
|
| 172 | + { |
|
| 173 | + // Get the right route for the request |
|
| 174 | + $page = $this->requestRouter->route(); |
|
| 175 | + |
|
| 176 | + $siteConfiguration = $this->getConfiguration(); |
|
| 177 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 178 | + |
|
| 179 | + if ($siteConfiguration->getIrcNotificationsEnabled()) { |
|
| 180 | + $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 181 | + } |
|
| 182 | + else { |
|
| 183 | + // @todo federated table here? |
|
| 184 | + $notificationsDatabase = $database; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + $this->setupHelpers($page, $siteConfiguration, $database, $notificationsDatabase); |
|
| 188 | + |
|
| 189 | + /* @todo Remove this global statement! It's here for User.php, which does far more than it should. */ |
|
| 190 | + global $oauthHelper; |
|
| 191 | + $oauthHelper = $page->getOAuthHelper(); |
|
| 192 | + |
|
| 193 | + /* @todo Remove this global statement! It's here for Request.php, which does far more than it should. */ |
|
| 194 | + global $globalXffTrustProvider; |
|
| 195 | + $globalXffTrustProvider = $page->getXffTrustProvider(); |
|
| 196 | + |
|
| 197 | + // run the route code for the request. |
|
| 198 | + $page->execute(); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Any cleanup tasks should go here |
|
| 203 | + * |
|
| 204 | + * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
| 205 | + * This should *only* be for cleaning up, no logic should go here. |
|
| 206 | + */ |
|
| 207 | + protected function cleanupEnvironment() |
|
| 208 | + { |
|
| 209 | + // Clean up anything we splurged after sending the page. |
|
| 210 | + if (ob_get_level() > 0) { |
|
| 211 | + for ($i = ob_get_level(); $i > 0; $i--) { |
|
| 212 | + ob_end_clean(); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + private function checkForceLogout() |
|
| 218 | + { |
|
| 219 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 220 | + |
|
| 221 | + $sessionUserId = WebRequest::getSessionUserId(); |
|
| 222 | + iF ($sessionUserId === null) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Note, User::getCurrent() caches it's result, which we *really* don't want to trigger. |
|
| 227 | + $currentUser = User::getById($sessionUserId, $database); |
|
| 228 | + |
|
| 229 | + if ($currentUser === false) { |
|
| 230 | + // Umm... this user has a session cookie with a userId set, but no user exists... |
|
| 231 | + Session::restart(); |
|
| 232 | + |
|
| 233 | + $currentUser = User::getCurrent($database); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if ($currentUser->getForceLogout()) { |
|
| 237 | + Session::restart(); |
|
| 238 | + |
|
| 239 | + $currentUser->setForceLogout(false); |
|
| 240 | + $currentUser->save(); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + public function isPublic() |
|
| 245 | + { |
|
| 246 | + return $this->isPublic; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function setPublic($isPublic) |
|
| 250 | + { |
|
| 251 | + $this->isPublic = $isPublic; |
|
| 252 | + } |
|
| 253 | 253 | } |