@@ -31,543 +31,543 @@ |
||
| 31 | 31 | |
| 32 | 32 | class LogHelper |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * @param int $requestId |
|
| 36 | - * |
|
| 37 | - * @return DataObject[] |
|
| 38 | - */ |
|
| 39 | - public static function getRequestLogsWithComments( |
|
| 40 | - $requestId, |
|
| 41 | - PdoDatabase $db, |
|
| 42 | - ISecurityManager $securityManager |
|
| 43 | - ): array { |
|
| 44 | - // FIXME: domains |
|
| 45 | - $logs = LogSearchHelper::get($db, 1)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 46 | - |
|
| 47 | - $currentUser = User::getCurrent($db); |
|
| 48 | - $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === ISecurityManager::ALLOWED; |
|
| 49 | - $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === ISecurityManager::ALLOWED; |
|
| 50 | - |
|
| 51 | - $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
|
| 52 | - |
|
| 53 | - $items = array_merge($logs, $comments); |
|
| 54 | - |
|
| 55 | - $sortKey = function(DataObject $item): int { |
|
| 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 | - public static function getLogDescription(Log $entry): string |
|
| 90 | - { |
|
| 91 | - $text = "Deferred to "; |
|
| 92 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 93 | - // Deferred to a different queue |
|
| 94 | - // This is exactly what we want to display. |
|
| 95 | - return $entry->getAction(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $text = "Closed custom-n"; |
|
| 99 | - if ($entry->getAction() == $text) { |
|
| 100 | - // Custom-closed |
|
| 101 | - return "closed (custom reason - account not created)"; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $text = "Closed custom-y"; |
|
| 105 | - if ($entry->getAction() == $text) { |
|
| 106 | - // Custom-closed |
|
| 107 | - return "closed (custom reason - account created)"; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $text = "Closed 0"; |
|
| 111 | - if ($entry->getAction() == $text) { |
|
| 112 | - // Dropped the request - short-circuit the lookup |
|
| 113 | - return "dropped request"; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $text = "Closed "; |
|
| 117 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 118 | - // Closed with a reason - do a lookup here. |
|
| 119 | - $id = substr($entry->getAction(), strlen($text)); |
|
| 120 | - /** @var EmailTemplate|false $template */ |
|
| 121 | - $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 122 | - |
|
| 123 | - if ($template !== false) { |
|
| 124 | - return 'closed (' . $template->getName() . ')'; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Fall back to the basic stuff |
|
| 129 | - $lookup = array( |
|
| 130 | - 'Reserved' => 'reserved', |
|
| 131 | - 'Email Confirmed' => 'email-confirmed', |
|
| 132 | - 'Manually Confirmed' => 'manually confirmed the request', |
|
| 133 | - 'Unreserved' => 'unreserved', |
|
| 134 | - 'Approved' => 'approved', |
|
| 135 | - 'DeactivatedUser' => 'deactivated user', |
|
| 136 | - 'RoleChange' => 'changed roles', |
|
| 137 | - 'GlobalRoleChange' => 'changed global roles', |
|
| 138 | - 'RequestedReactivation' => 'requested reactivation', |
|
| 139 | - 'Banned' => 'banned', |
|
| 140 | - 'Edited' => 'edited interface message', |
|
| 141 | - 'EditComment-c' => 'edited a comment', |
|
| 142 | - 'EditComment-r' => 'edited a comment', |
|
| 143 | - 'FlaggedComment' => 'flagged a comment', |
|
| 144 | - 'UnflaggedComment' => 'unflagged a comment', |
|
| 145 | - 'Unbanned' => 'unbanned', |
|
| 146 | - 'BanReplaced' => 'replaced ban', |
|
| 147 | - 'Promoted' => 'promoted to tool admin', |
|
| 148 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 149 | - 'Prefchange' => 'changed user preferences', |
|
| 150 | - 'Renamed' => 'renamed', |
|
| 151 | - 'Demoted' => 'demoted from tool admin', |
|
| 152 | - 'ReceiveReserved' => 'received the reservation', |
|
| 153 | - 'SendReserved' => 'sent the reservation', |
|
| 154 | - 'EditedEmail' => 'edited email', |
|
| 155 | - 'DeletedTemplate' => 'deleted template', |
|
| 156 | - 'EditedTemplate' => 'edited template', |
|
| 157 | - 'CreatedEmail' => 'created email', |
|
| 158 | - 'CreatedTemplate' => 'created template', |
|
| 159 | - 'SentMail' => 'sent an email to the requester', |
|
| 160 | - 'Registered' => 'registered a tool account', |
|
| 161 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 162 | - 'JobCompleted' => 'completed a background job', |
|
| 163 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 164 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
| 165 | - 'JobCancelled' => 'cancelled execution of a job', |
|
| 166 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 167 | - 'Hospitalised' => 'sent to the hospital', |
|
| 168 | - 'QueueCreated' => 'created a request queue', |
|
| 169 | - 'QueueEdited' => 'edited a request queue', |
|
| 170 | - 'DomainCreated' => 'created a domain', |
|
| 171 | - 'DomainEdited' => 'edited a domain', |
|
| 172 | - 'RequestFormCreated' => 'created a request form', |
|
| 173 | - 'RequestFormEdited' => 'edited a request form', |
|
| 174 | - ); |
|
| 175 | - |
|
| 176 | - if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 177 | - return $lookup[$entry->getAction()]; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // OK, I don't know what this is. Fall back to something sane. |
|
| 181 | - return "performed an unknown action ({$entry->getAction()})"; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - public static function getLogActions(PdoDatabase $database): array |
|
| 185 | - { |
|
| 186 | - $lookup = array( |
|
| 187 | - "Requests" => [ |
|
| 188 | - 'Reserved' => 'reserved', |
|
| 189 | - 'Email Confirmed' => 'email-confirmed', |
|
| 190 | - 'Manually Confirmed' => 'manually confirmed', |
|
| 191 | - 'Unreserved' => 'unreserved', |
|
| 192 | - 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 193 | - 'EditComment-r' => 'edited a comment (by request)', |
|
| 194 | - 'FlaggedComment' => 'flagged a comment', |
|
| 195 | - 'UnflaggedComment' => 'unflagged a comment', |
|
| 196 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 197 | - 'ReceiveReserved' => 'received the reservation', |
|
| 198 | - 'SendReserved' => 'sent the reservation', |
|
| 199 | - 'SentMail' => 'sent an email to the requester', |
|
| 200 | - 'Closed 0' => 'dropped request', |
|
| 201 | - 'Closed custom-y' => 'closed (custom reason - account created)', |
|
| 202 | - 'Closed custom-n' => 'closed (custom reason - account not created)', |
|
| 203 | - ], |
|
| 204 | - 'Users' => [ |
|
| 205 | - 'Approved' => 'approved', |
|
| 206 | - 'DeactivatedUser' => 'deactivated user', |
|
| 207 | - 'RoleChange' => 'changed roles', |
|
| 208 | - 'GlobalRoleChange' => 'changed global roles', |
|
| 209 | - 'Prefchange' => 'changed user preferences', |
|
| 210 | - 'Renamed' => 'renamed', |
|
| 211 | - 'Promoted' => 'promoted to tool admin', |
|
| 212 | - 'Demoted' => 'demoted from tool admin', |
|
| 213 | - 'Registered' => 'registered a tool account', |
|
| 214 | - 'RequestedReactivation' => 'requested reactivation', |
|
| 215 | - ], |
|
| 216 | - "Bans" => [ |
|
| 217 | - 'Banned' => 'banned', |
|
| 218 | - 'Unbanned' => 'unbanned', |
|
| 219 | - 'BanReplaced' => 'replaced ban', |
|
| 220 | - ], |
|
| 221 | - "Site notice" => [ |
|
| 222 | - 'Edited' => 'edited interface message', |
|
| 223 | - ], |
|
| 224 | - "Email close templates" => [ |
|
| 225 | - 'EditedEmail' => 'edited email', |
|
| 226 | - 'CreatedEmail' => 'created email', |
|
| 227 | - ], |
|
| 228 | - "Welcome templates" => [ |
|
| 229 | - 'DeletedTemplate' => 'deleted template', |
|
| 230 | - 'EditedTemplate' => 'edited template', |
|
| 231 | - 'CreatedTemplate' => 'created template', |
|
| 232 | - ], |
|
| 233 | - "Job queue" => [ |
|
| 234 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 235 | - 'JobCompleted' => 'completed a background job', |
|
| 236 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 237 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
| 238 | - 'JobCancelled' => 'cancelled execution of a job', |
|
| 239 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 240 | - 'Hospitalised' => 'sent to the hospital', |
|
| 241 | - ], |
|
| 242 | - "Request queues" => [ |
|
| 243 | - 'QueueCreated' => 'created a request queue', |
|
| 244 | - 'QueueEdited' => 'edited a request queue', |
|
| 245 | - ], |
|
| 246 | - "Domains" => [ |
|
| 247 | - 'DomainCreated' => 'created a domain', |
|
| 248 | - 'DomainEdited' => 'edited a domain', |
|
| 249 | - ], |
|
| 250 | - "Request forms" => [ |
|
| 251 | - 'RequestFormCreated' => 'created a request form', |
|
| 252 | - 'RequestFormEdited' => 'edited a request form', |
|
| 253 | - ], |
|
| 254 | - ); |
|
| 255 | - |
|
| 256 | - $databaseDrivenLogKeys = $database->query(<<<SQL |
|
| 34 | + /** |
|
| 35 | + * @param int $requestId |
|
| 36 | + * |
|
| 37 | + * @return DataObject[] |
|
| 38 | + */ |
|
| 39 | + public static function getRequestLogsWithComments( |
|
| 40 | + $requestId, |
|
| 41 | + PdoDatabase $db, |
|
| 42 | + ISecurityManager $securityManager |
|
| 43 | + ): array { |
|
| 44 | + // FIXME: domains |
|
| 45 | + $logs = LogSearchHelper::get($db, 1)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 46 | + |
|
| 47 | + $currentUser = User::getCurrent($db); |
|
| 48 | + $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === ISecurityManager::ALLOWED; |
|
| 49 | + $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === ISecurityManager::ALLOWED; |
|
| 50 | + |
|
| 51 | + $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
|
| 52 | + |
|
| 53 | + $items = array_merge($logs, $comments); |
|
| 54 | + |
|
| 55 | + $sortKey = function(DataObject $item): int { |
|
| 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 | + public static function getLogDescription(Log $entry): string |
|
| 90 | + { |
|
| 91 | + $text = "Deferred to "; |
|
| 92 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 93 | + // Deferred to a different queue |
|
| 94 | + // This is exactly what we want to display. |
|
| 95 | + return $entry->getAction(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $text = "Closed custom-n"; |
|
| 99 | + if ($entry->getAction() == $text) { |
|
| 100 | + // Custom-closed |
|
| 101 | + return "closed (custom reason - account not created)"; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $text = "Closed custom-y"; |
|
| 105 | + if ($entry->getAction() == $text) { |
|
| 106 | + // Custom-closed |
|
| 107 | + return "closed (custom reason - account created)"; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $text = "Closed 0"; |
|
| 111 | + if ($entry->getAction() == $text) { |
|
| 112 | + // Dropped the request - short-circuit the lookup |
|
| 113 | + return "dropped request"; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $text = "Closed "; |
|
| 117 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 118 | + // Closed with a reason - do a lookup here. |
|
| 119 | + $id = substr($entry->getAction(), strlen($text)); |
|
| 120 | + /** @var EmailTemplate|false $template */ |
|
| 121 | + $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 122 | + |
|
| 123 | + if ($template !== false) { |
|
| 124 | + return 'closed (' . $template->getName() . ')'; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Fall back to the basic stuff |
|
| 129 | + $lookup = array( |
|
| 130 | + 'Reserved' => 'reserved', |
|
| 131 | + 'Email Confirmed' => 'email-confirmed', |
|
| 132 | + 'Manually Confirmed' => 'manually confirmed the request', |
|
| 133 | + 'Unreserved' => 'unreserved', |
|
| 134 | + 'Approved' => 'approved', |
|
| 135 | + 'DeactivatedUser' => 'deactivated user', |
|
| 136 | + 'RoleChange' => 'changed roles', |
|
| 137 | + 'GlobalRoleChange' => 'changed global roles', |
|
| 138 | + 'RequestedReactivation' => 'requested reactivation', |
|
| 139 | + 'Banned' => 'banned', |
|
| 140 | + 'Edited' => 'edited interface message', |
|
| 141 | + 'EditComment-c' => 'edited a comment', |
|
| 142 | + 'EditComment-r' => 'edited a comment', |
|
| 143 | + 'FlaggedComment' => 'flagged a comment', |
|
| 144 | + 'UnflaggedComment' => 'unflagged a comment', |
|
| 145 | + 'Unbanned' => 'unbanned', |
|
| 146 | + 'BanReplaced' => 'replaced ban', |
|
| 147 | + 'Promoted' => 'promoted to tool admin', |
|
| 148 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 149 | + 'Prefchange' => 'changed user preferences', |
|
| 150 | + 'Renamed' => 'renamed', |
|
| 151 | + 'Demoted' => 'demoted from tool admin', |
|
| 152 | + 'ReceiveReserved' => 'received the reservation', |
|
| 153 | + 'SendReserved' => 'sent the reservation', |
|
| 154 | + 'EditedEmail' => 'edited email', |
|
| 155 | + 'DeletedTemplate' => 'deleted template', |
|
| 156 | + 'EditedTemplate' => 'edited template', |
|
| 157 | + 'CreatedEmail' => 'created email', |
|
| 158 | + 'CreatedTemplate' => 'created template', |
|
| 159 | + 'SentMail' => 'sent an email to the requester', |
|
| 160 | + 'Registered' => 'registered a tool account', |
|
| 161 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 162 | + 'JobCompleted' => 'completed a background job', |
|
| 163 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 164 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
| 165 | + 'JobCancelled' => 'cancelled execution of a job', |
|
| 166 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 167 | + 'Hospitalised' => 'sent to the hospital', |
|
| 168 | + 'QueueCreated' => 'created a request queue', |
|
| 169 | + 'QueueEdited' => 'edited a request queue', |
|
| 170 | + 'DomainCreated' => 'created a domain', |
|
| 171 | + 'DomainEdited' => 'edited a domain', |
|
| 172 | + 'RequestFormCreated' => 'created a request form', |
|
| 173 | + 'RequestFormEdited' => 'edited a request form', |
|
| 174 | + ); |
|
| 175 | + |
|
| 176 | + if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 177 | + return $lookup[$entry->getAction()]; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // OK, I don't know what this is. Fall back to something sane. |
|
| 181 | + return "performed an unknown action ({$entry->getAction()})"; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + public static function getLogActions(PdoDatabase $database): array |
|
| 185 | + { |
|
| 186 | + $lookup = array( |
|
| 187 | + "Requests" => [ |
|
| 188 | + 'Reserved' => 'reserved', |
|
| 189 | + 'Email Confirmed' => 'email-confirmed', |
|
| 190 | + 'Manually Confirmed' => 'manually confirmed', |
|
| 191 | + 'Unreserved' => 'unreserved', |
|
| 192 | + 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 193 | + 'EditComment-r' => 'edited a comment (by request)', |
|
| 194 | + 'FlaggedComment' => 'flagged a comment', |
|
| 195 | + 'UnflaggedComment' => 'unflagged a comment', |
|
| 196 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 197 | + 'ReceiveReserved' => 'received the reservation', |
|
| 198 | + 'SendReserved' => 'sent the reservation', |
|
| 199 | + 'SentMail' => 'sent an email to the requester', |
|
| 200 | + 'Closed 0' => 'dropped request', |
|
| 201 | + 'Closed custom-y' => 'closed (custom reason - account created)', |
|
| 202 | + 'Closed custom-n' => 'closed (custom reason - account not created)', |
|
| 203 | + ], |
|
| 204 | + 'Users' => [ |
|
| 205 | + 'Approved' => 'approved', |
|
| 206 | + 'DeactivatedUser' => 'deactivated user', |
|
| 207 | + 'RoleChange' => 'changed roles', |
|
| 208 | + 'GlobalRoleChange' => 'changed global roles', |
|
| 209 | + 'Prefchange' => 'changed user preferences', |
|
| 210 | + 'Renamed' => 'renamed', |
|
| 211 | + 'Promoted' => 'promoted to tool admin', |
|
| 212 | + 'Demoted' => 'demoted from tool admin', |
|
| 213 | + 'Registered' => 'registered a tool account', |
|
| 214 | + 'RequestedReactivation' => 'requested reactivation', |
|
| 215 | + ], |
|
| 216 | + "Bans" => [ |
|
| 217 | + 'Banned' => 'banned', |
|
| 218 | + 'Unbanned' => 'unbanned', |
|
| 219 | + 'BanReplaced' => 'replaced ban', |
|
| 220 | + ], |
|
| 221 | + "Site notice" => [ |
|
| 222 | + 'Edited' => 'edited interface message', |
|
| 223 | + ], |
|
| 224 | + "Email close templates" => [ |
|
| 225 | + 'EditedEmail' => 'edited email', |
|
| 226 | + 'CreatedEmail' => 'created email', |
|
| 227 | + ], |
|
| 228 | + "Welcome templates" => [ |
|
| 229 | + 'DeletedTemplate' => 'deleted template', |
|
| 230 | + 'EditedTemplate' => 'edited template', |
|
| 231 | + 'CreatedTemplate' => 'created template', |
|
| 232 | + ], |
|
| 233 | + "Job queue" => [ |
|
| 234 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 235 | + 'JobCompleted' => 'completed a background job', |
|
| 236 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 237 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
| 238 | + 'JobCancelled' => 'cancelled execution of a job', |
|
| 239 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 240 | + 'Hospitalised' => 'sent to the hospital', |
|
| 241 | + ], |
|
| 242 | + "Request queues" => [ |
|
| 243 | + 'QueueCreated' => 'created a request queue', |
|
| 244 | + 'QueueEdited' => 'edited a request queue', |
|
| 245 | + ], |
|
| 246 | + "Domains" => [ |
|
| 247 | + 'DomainCreated' => 'created a domain', |
|
| 248 | + 'DomainEdited' => 'edited a domain', |
|
| 249 | + ], |
|
| 250 | + "Request forms" => [ |
|
| 251 | + 'RequestFormCreated' => 'created a request form', |
|
| 252 | + 'RequestFormEdited' => 'edited a request form', |
|
| 253 | + ], |
|
| 254 | + ); |
|
| 255 | + |
|
| 256 | + $databaseDrivenLogKeys = $database->query(<<<SQL |
|
| 257 | 257 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v FROM emailtemplate |
| 258 | 258 | UNION ALL |
| 259 | 259 | SELECT CONCAT('Deferred to ', logname) AS k, CONCAT('deferred to ', displayname) AS v FROM requestqueue; |
| 260 | 260 | SQL |
| 261 | - ); |
|
| 262 | - foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 263 | - $lookup["Requests"][$row['k']] = $row['v']; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - return $lookup; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - public static function getObjectTypes(): array |
|
| 270 | - { |
|
| 271 | - return array( |
|
| 272 | - 'Ban' => 'Ban', |
|
| 273 | - 'Comment' => 'Comment', |
|
| 274 | - 'EmailTemplate' => 'Email template', |
|
| 275 | - 'JobQueue' => 'Job queue item', |
|
| 276 | - 'Request' => 'Request', |
|
| 277 | - 'SiteNotice' => 'Site notice', |
|
| 278 | - 'User' => 'User', |
|
| 279 | - 'WelcomeTemplate' => 'Welcome template', |
|
| 280 | - 'RequestQueue' => 'Request queue', |
|
| 281 | - 'Domain' => 'Domain', |
|
| 282 | - 'RequestForm' => 'Request form' |
|
| 283 | - ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * This returns an HTML representation of the object |
|
| 288 | - * |
|
| 289 | - * @param int $objectId |
|
| 290 | - * @param string $objectType |
|
| 291 | - * |
|
| 292 | - * @category Security-Critical |
|
| 293 | - */ |
|
| 294 | - private static function getObjectDescription( |
|
| 295 | - $objectId, |
|
| 296 | - $objectType, |
|
| 297 | - PdoDatabase $database, |
|
| 298 | - SiteConfiguration $configuration |
|
| 299 | - ): ?string { |
|
| 300 | - if ($objectType == '') { |
|
| 301 | - return null; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $baseurl = $configuration->getBaseUrl(); |
|
| 305 | - |
|
| 306 | - switch ($objectType) { |
|
| 307 | - case 'Ban': |
|
| 308 | - /** @var Ban $ban */ |
|
| 309 | - $ban = Ban::getById($objectId, $database); |
|
| 310 | - |
|
| 311 | - if ($ban === false) { |
|
| 312 | - return 'Ban #' . $objectId; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - return <<<HTML |
|
| 261 | + ); |
|
| 262 | + foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 263 | + $lookup["Requests"][$row['k']] = $row['v']; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + return $lookup; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + public static function getObjectTypes(): array |
|
| 270 | + { |
|
| 271 | + return array( |
|
| 272 | + 'Ban' => 'Ban', |
|
| 273 | + 'Comment' => 'Comment', |
|
| 274 | + 'EmailTemplate' => 'Email template', |
|
| 275 | + 'JobQueue' => 'Job queue item', |
|
| 276 | + 'Request' => 'Request', |
|
| 277 | + 'SiteNotice' => 'Site notice', |
|
| 278 | + 'User' => 'User', |
|
| 279 | + 'WelcomeTemplate' => 'Welcome template', |
|
| 280 | + 'RequestQueue' => 'Request queue', |
|
| 281 | + 'Domain' => 'Domain', |
|
| 282 | + 'RequestForm' => 'Request form' |
|
| 283 | + ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * This returns an HTML representation of the object |
|
| 288 | + * |
|
| 289 | + * @param int $objectId |
|
| 290 | + * @param string $objectType |
|
| 291 | + * |
|
| 292 | + * @category Security-Critical |
|
| 293 | + */ |
|
| 294 | + private static function getObjectDescription( |
|
| 295 | + $objectId, |
|
| 296 | + $objectType, |
|
| 297 | + PdoDatabase $database, |
|
| 298 | + SiteConfiguration $configuration |
|
| 299 | + ): ?string { |
|
| 300 | + if ($objectType == '') { |
|
| 301 | + return null; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $baseurl = $configuration->getBaseUrl(); |
|
| 305 | + |
|
| 306 | + switch ($objectType) { |
|
| 307 | + case 'Ban': |
|
| 308 | + /** @var Ban $ban */ |
|
| 309 | + $ban = Ban::getById($objectId, $database); |
|
| 310 | + |
|
| 311 | + if ($ban === false) { |
|
| 312 | + return 'Ban #' . $objectId; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + return <<<HTML |
|
| 316 | 316 | <a href="{$baseurl}/internal.php/bans/show?id={$objectId}">Ban #{$objectId}</a> |
| 317 | 317 | HTML; |
| 318 | - case 'EmailTemplate': |
|
| 319 | - /** @var EmailTemplate $emailTemplate */ |
|
| 320 | - $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 318 | + case 'EmailTemplate': |
|
| 319 | + /** @var EmailTemplate $emailTemplate */ |
|
| 320 | + $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 321 | 321 | |
| 322 | - if ($emailTemplate === false) { |
|
| 323 | - return 'Email Template #' . $objectId; |
|
| 324 | - } |
|
| 322 | + if ($emailTemplate === false) { |
|
| 323 | + return 'Email Template #' . $objectId; |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 326 | + $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 327 | 327 | |
| 328 | - return <<<HTML |
|
| 328 | + return <<<HTML |
|
| 329 | 329 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
| 330 | 330 | HTML; |
| 331 | - case 'SiteNotice': |
|
| 332 | - return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 333 | - case 'Request': |
|
| 334 | - /** @var Request $request */ |
|
| 335 | - $request = Request::getById($objectId, $database); |
|
| 331 | + case 'SiteNotice': |
|
| 332 | + return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 333 | + case 'Request': |
|
| 334 | + /** @var Request $request */ |
|
| 335 | + $request = Request::getById($objectId, $database); |
|
| 336 | 336 | |
| 337 | - if ($request === false) { |
|
| 338 | - return 'Request #' . $objectId; |
|
| 339 | - } |
|
| 337 | + if ($request === false) { |
|
| 338 | + return 'Request #' . $objectId; |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | - $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 341 | + $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 342 | 342 | |
| 343 | - return <<<HTML |
|
| 343 | + return <<<HTML |
|
| 344 | 344 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
| 345 | 345 | HTML; |
| 346 | - case 'User': |
|
| 347 | - /** @var User $user */ |
|
| 348 | - $user = User::getById($objectId, $database); |
|
| 349 | - |
|
| 350 | - // Some users were merged out of existence |
|
| 351 | - if ($user === false) { |
|
| 352 | - return 'User #' . $objectId; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 356 | - |
|
| 357 | - return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 358 | - case 'WelcomeTemplate': |
|
| 359 | - /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 360 | - $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 361 | - |
|
| 362 | - // some old templates have been completely deleted and lost to the depths of time. |
|
| 363 | - if ($welcomeTemplate === false) { |
|
| 364 | - return "Welcome template #{$objectId}"; |
|
| 365 | - } |
|
| 366 | - else { |
|
| 367 | - $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 368 | - |
|
| 369 | - return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 370 | - } |
|
| 371 | - case 'JobQueue': |
|
| 372 | - /** @var JobQueue $job */ |
|
| 373 | - $job = JobQueue::getById($objectId, $database); |
|
| 374 | - |
|
| 375 | - $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
| 376 | - |
|
| 377 | - if ($job === false) { |
|
| 378 | - return 'Job Queue Task #' . $objectId; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - $task = $job->getTask(); |
|
| 382 | - if (isset($taskDescriptions[$task])) { |
|
| 383 | - $description = $taskDescriptions[$task]; |
|
| 384 | - } |
|
| 385 | - else { |
|
| 386 | - $description = 'Unknown task'; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
| 390 | - case 'RequestQueue': |
|
| 391 | - /** @var RequestQueue $queue */ |
|
| 392 | - $queue = RequestQueue::getById($objectId, $database); |
|
| 393 | - |
|
| 394 | - if ($queue === false) { |
|
| 395 | - return "Request Queue #{$objectId}"; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
|
| 399 | - |
|
| 400 | - return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
|
| 401 | - case 'Domain': |
|
| 402 | - /** @var Domain $domain */ |
|
| 403 | - $domain = Domain::getById($objectId, $database); |
|
| 404 | - |
|
| 405 | - if ($domain === false) { |
|
| 406 | - return "Domain #{$objectId}"; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
|
| 410 | - return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
|
| 411 | - case 'RequestForm': |
|
| 412 | - /** @var RequestForm $queue */ |
|
| 413 | - $queue = RequestForm::getById($objectId, $database); |
|
| 414 | - |
|
| 415 | - if ($queue === false) { |
|
| 416 | - return "Request Form #{$objectId}"; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 420 | - |
|
| 421 | - return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
|
| 422 | - case 'Comment': |
|
| 423 | - /** @var Comment $comment */ |
|
| 424 | - $comment = Comment::getById($objectId, $database); |
|
| 425 | - /** @var Request $request */ |
|
| 426 | - $request = Request::getById($comment->getRequest(), $database); |
|
| 427 | - $requestName = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 428 | - |
|
| 429 | - return "<a href=\"{$baseurl}/internal.php/editComment?id={$objectId}\">Comment {$objectId}</a> on request <a href=\"{$baseurl}/internal.php/viewRequest?id={$comment->getRequest()}#comment-{$objectId}\">#{$comment->getRequest()} ({$requestName})</a>"; |
|
| 430 | - default: |
|
| 431 | - return '[' . $objectType . " " . $objectId . ']'; |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @param Log[] $logs |
|
| 437 | - * @throws Exception |
|
| 438 | - * |
|
| 439 | - * @returns User[] |
|
| 440 | - */ |
|
| 441 | - private static function loadUsersFromLogs(array $logs, PdoDatabase $database): array |
|
| 442 | - { |
|
| 443 | - $userIds = array(); |
|
| 444 | - |
|
| 445 | - foreach ($logs as $logEntry) { |
|
| 446 | - if (!$logEntry instanceof Log) { |
|
| 447 | - // if this happens, we've done something wrong with passing back the log data. |
|
| 448 | - throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - $user = $logEntry->getUser(); |
|
| 452 | - if ($user === -1) { |
|
| 453 | - continue; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - if (!array_search($user, $userIds)) { |
|
| 457 | - $userIds[] = $user; |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 462 | - $users[-1] = User::getCommunity()->getUsername(); |
|
| 463 | - |
|
| 464 | - return $users; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * @param Log[] $logs |
|
| 469 | - * |
|
| 470 | - * @throws Exception |
|
| 471 | - */ |
|
| 472 | - public static function prepareLogsForTemplate( |
|
| 473 | - array $logs, |
|
| 474 | - PdoDatabase $database, |
|
| 475 | - SiteConfiguration $configuration, |
|
| 476 | - ISecurityManager $securityManager |
|
| 477 | - ): array { |
|
| 478 | - $users = self::loadUsersFromLogs($logs, $database); |
|
| 479 | - $currentUser = User::getCurrent($database); |
|
| 480 | - |
|
| 481 | - $allowAccountLogSelf = $securityManager->allows('UserData', 'accountLogSelf', $currentUser) === ISecurityManager::ALLOWED; |
|
| 482 | - $allowAccountLog = $securityManager->allows('UserData', 'accountLog', $currentUser) === ISecurityManager::ALLOWED; |
|
| 483 | - |
|
| 484 | - $protectedLogActions = [ |
|
| 485 | - 'RequestedReactivation', |
|
| 486 | - 'DeactivatedUser', |
|
| 487 | - ]; |
|
| 488 | - |
|
| 489 | - $logData = array(); |
|
| 490 | - foreach ($logs as $logEntry) { |
|
| 491 | - $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 492 | - $database, $configuration); |
|
| 493 | - |
|
| 494 | - // initialise to sane default |
|
| 495 | - $comment = null; |
|
| 496 | - |
|
| 497 | - switch ($logEntry->getAction()) { |
|
| 498 | - case 'Renamed': |
|
| 499 | - $renameData = unserialize($logEntry->getComment()); |
|
| 500 | - $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 501 | - $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 502 | - $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 503 | - break; |
|
| 504 | - case 'RoleChange': |
|
| 505 | - case 'GlobalRoleChange': |
|
| 506 | - $roleChangeData = unserialize($logEntry->getComment()); |
|
| 507 | - |
|
| 508 | - $removed = array(); |
|
| 509 | - foreach ($roleChangeData['removed'] as $r) { |
|
| 510 | - $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - $added = array(); |
|
| 514 | - foreach ($roleChangeData['added'] as $r) { |
|
| 515 | - $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 519 | - |
|
| 520 | - $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 521 | - $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 522 | - break; |
|
| 523 | - case 'JobIssue': |
|
| 524 | - $jobIssueData = unserialize($logEntry->getComment()); |
|
| 525 | - $errorMessage = $jobIssueData['error']; |
|
| 526 | - $status = $jobIssueData['status']; |
|
| 527 | - |
|
| 528 | - $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
| 529 | - $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
| 530 | - break; |
|
| 531 | - case 'JobIssueRequest': |
|
| 532 | - case 'JobCompletedRequest': |
|
| 533 | - $jobData = unserialize($logEntry->getComment()); |
|
| 534 | - |
|
| 535 | - /** @var JobQueue $job */ |
|
| 536 | - $job = JobQueue::getById($jobData['job'], $database); |
|
| 537 | - $descs = JobQueue::getTaskDescriptions(); |
|
| 538 | - $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
| 539 | - break; |
|
| 540 | - |
|
| 541 | - case 'JobCompleted': |
|
| 542 | - break; |
|
| 543 | - |
|
| 544 | - default: |
|
| 545 | - $comment = $logEntry->getComment(); |
|
| 546 | - break; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - if (in_array($logEntry->getAction(), $protectedLogActions) && $logEntry->getObjectType() === 'User') { |
|
| 550 | - if ($allowAccountLog) { |
|
| 551 | - // do nothing, allowed to see all account logs |
|
| 552 | - } |
|
| 553 | - else if ($allowAccountLogSelf && $currentUser->getId() === $logEntry->getObjectId()) { |
|
| 554 | - // do nothing, allowed to see own account log |
|
| 555 | - } |
|
| 556 | - else { |
|
| 557 | - $comment = null; |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $logData[] = array( |
|
| 562 | - 'timestamp' => $logEntry->getTimestamp(), |
|
| 563 | - 'userid' => $logEntry->getUser(), |
|
| 564 | - 'username' => $users[$logEntry->getUser()], |
|
| 565 | - 'description' => self::getLogDescription($logEntry), |
|
| 566 | - 'objectdescription' => $objectDescription, |
|
| 567 | - 'comment' => $comment, |
|
| 568 | - ); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - return array($users, $logData); |
|
| 572 | - } |
|
| 346 | + case 'User': |
|
| 347 | + /** @var User $user */ |
|
| 348 | + $user = User::getById($objectId, $database); |
|
| 349 | + |
|
| 350 | + // Some users were merged out of existence |
|
| 351 | + if ($user === false) { |
|
| 352 | + return 'User #' . $objectId; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 356 | + |
|
| 357 | + return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 358 | + case 'WelcomeTemplate': |
|
| 359 | + /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 360 | + $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 361 | + |
|
| 362 | + // some old templates have been completely deleted and lost to the depths of time. |
|
| 363 | + if ($welcomeTemplate === false) { |
|
| 364 | + return "Welcome template #{$objectId}"; |
|
| 365 | + } |
|
| 366 | + else { |
|
| 367 | + $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 368 | + |
|
| 369 | + return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 370 | + } |
|
| 371 | + case 'JobQueue': |
|
| 372 | + /** @var JobQueue $job */ |
|
| 373 | + $job = JobQueue::getById($objectId, $database); |
|
| 374 | + |
|
| 375 | + $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
| 376 | + |
|
| 377 | + if ($job === false) { |
|
| 378 | + return 'Job Queue Task #' . $objectId; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + $task = $job->getTask(); |
|
| 382 | + if (isset($taskDescriptions[$task])) { |
|
| 383 | + $description = $taskDescriptions[$task]; |
|
| 384 | + } |
|
| 385 | + else { |
|
| 386 | + $description = 'Unknown task'; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
| 390 | + case 'RequestQueue': |
|
| 391 | + /** @var RequestQueue $queue */ |
|
| 392 | + $queue = RequestQueue::getById($objectId, $database); |
|
| 393 | + |
|
| 394 | + if ($queue === false) { |
|
| 395 | + return "Request Queue #{$objectId}"; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
|
| 399 | + |
|
| 400 | + return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
|
| 401 | + case 'Domain': |
|
| 402 | + /** @var Domain $domain */ |
|
| 403 | + $domain = Domain::getById($objectId, $database); |
|
| 404 | + |
|
| 405 | + if ($domain === false) { |
|
| 406 | + return "Domain #{$objectId}"; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
|
| 410 | + return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
|
| 411 | + case 'RequestForm': |
|
| 412 | + /** @var RequestForm $queue */ |
|
| 413 | + $queue = RequestForm::getById($objectId, $database); |
|
| 414 | + |
|
| 415 | + if ($queue === false) { |
|
| 416 | + return "Request Form #{$objectId}"; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 420 | + |
|
| 421 | + return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
|
| 422 | + case 'Comment': |
|
| 423 | + /** @var Comment $comment */ |
|
| 424 | + $comment = Comment::getById($objectId, $database); |
|
| 425 | + /** @var Request $request */ |
|
| 426 | + $request = Request::getById($comment->getRequest(), $database); |
|
| 427 | + $requestName = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 428 | + |
|
| 429 | + return "<a href=\"{$baseurl}/internal.php/editComment?id={$objectId}\">Comment {$objectId}</a> on request <a href=\"{$baseurl}/internal.php/viewRequest?id={$comment->getRequest()}#comment-{$objectId}\">#{$comment->getRequest()} ({$requestName})</a>"; |
|
| 430 | + default: |
|
| 431 | + return '[' . $objectType . " " . $objectId . ']'; |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @param Log[] $logs |
|
| 437 | + * @throws Exception |
|
| 438 | + * |
|
| 439 | + * @returns User[] |
|
| 440 | + */ |
|
| 441 | + private static function loadUsersFromLogs(array $logs, PdoDatabase $database): array |
|
| 442 | + { |
|
| 443 | + $userIds = array(); |
|
| 444 | + |
|
| 445 | + foreach ($logs as $logEntry) { |
|
| 446 | + if (!$logEntry instanceof Log) { |
|
| 447 | + // if this happens, we've done something wrong with passing back the log data. |
|
| 448 | + throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + $user = $logEntry->getUser(); |
|
| 452 | + if ($user === -1) { |
|
| 453 | + continue; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + if (!array_search($user, $userIds)) { |
|
| 457 | + $userIds[] = $user; |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 462 | + $users[-1] = User::getCommunity()->getUsername(); |
|
| 463 | + |
|
| 464 | + return $users; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * @param Log[] $logs |
|
| 469 | + * |
|
| 470 | + * @throws Exception |
|
| 471 | + */ |
|
| 472 | + public static function prepareLogsForTemplate( |
|
| 473 | + array $logs, |
|
| 474 | + PdoDatabase $database, |
|
| 475 | + SiteConfiguration $configuration, |
|
| 476 | + ISecurityManager $securityManager |
|
| 477 | + ): array { |
|
| 478 | + $users = self::loadUsersFromLogs($logs, $database); |
|
| 479 | + $currentUser = User::getCurrent($database); |
|
| 480 | + |
|
| 481 | + $allowAccountLogSelf = $securityManager->allows('UserData', 'accountLogSelf', $currentUser) === ISecurityManager::ALLOWED; |
|
| 482 | + $allowAccountLog = $securityManager->allows('UserData', 'accountLog', $currentUser) === ISecurityManager::ALLOWED; |
|
| 483 | + |
|
| 484 | + $protectedLogActions = [ |
|
| 485 | + 'RequestedReactivation', |
|
| 486 | + 'DeactivatedUser', |
|
| 487 | + ]; |
|
| 488 | + |
|
| 489 | + $logData = array(); |
|
| 490 | + foreach ($logs as $logEntry) { |
|
| 491 | + $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 492 | + $database, $configuration); |
|
| 493 | + |
|
| 494 | + // initialise to sane default |
|
| 495 | + $comment = null; |
|
| 496 | + |
|
| 497 | + switch ($logEntry->getAction()) { |
|
| 498 | + case 'Renamed': |
|
| 499 | + $renameData = unserialize($logEntry->getComment()); |
|
| 500 | + $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 501 | + $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 502 | + $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 503 | + break; |
|
| 504 | + case 'RoleChange': |
|
| 505 | + case 'GlobalRoleChange': |
|
| 506 | + $roleChangeData = unserialize($logEntry->getComment()); |
|
| 507 | + |
|
| 508 | + $removed = array(); |
|
| 509 | + foreach ($roleChangeData['removed'] as $r) { |
|
| 510 | + $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + $added = array(); |
|
| 514 | + foreach ($roleChangeData['added'] as $r) { |
|
| 515 | + $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 519 | + |
|
| 520 | + $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 521 | + $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 522 | + break; |
|
| 523 | + case 'JobIssue': |
|
| 524 | + $jobIssueData = unserialize($logEntry->getComment()); |
|
| 525 | + $errorMessage = $jobIssueData['error']; |
|
| 526 | + $status = $jobIssueData['status']; |
|
| 527 | + |
|
| 528 | + $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
| 529 | + $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
| 530 | + break; |
|
| 531 | + case 'JobIssueRequest': |
|
| 532 | + case 'JobCompletedRequest': |
|
| 533 | + $jobData = unserialize($logEntry->getComment()); |
|
| 534 | + |
|
| 535 | + /** @var JobQueue $job */ |
|
| 536 | + $job = JobQueue::getById($jobData['job'], $database); |
|
| 537 | + $descs = JobQueue::getTaskDescriptions(); |
|
| 538 | + $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
| 539 | + break; |
|
| 540 | + |
|
| 541 | + case 'JobCompleted': |
|
| 542 | + break; |
|
| 543 | + |
|
| 544 | + default: |
|
| 545 | + $comment = $logEntry->getComment(); |
|
| 546 | + break; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + if (in_array($logEntry->getAction(), $protectedLogActions) && $logEntry->getObjectType() === 'User') { |
|
| 550 | + if ($allowAccountLog) { |
|
| 551 | + // do nothing, allowed to see all account logs |
|
| 552 | + } |
|
| 553 | + else if ($allowAccountLogSelf && $currentUser->getId() === $logEntry->getObjectId()) { |
|
| 554 | + // do nothing, allowed to see own account log |
|
| 555 | + } |
|
| 556 | + else { |
|
| 557 | + $comment = null; |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $logData[] = array( |
|
| 562 | + 'timestamp' => $logEntry->getTimestamp(), |
|
| 563 | + 'userid' => $logEntry->getUser(), |
|
| 564 | + 'username' => $users[$logEntry->getUser()], |
|
| 565 | + 'description' => self::getLogDescription($logEntry), |
|
| 566 | + 'objectdescription' => $objectDescription, |
|
| 567 | + 'comment' => $comment, |
|
| 568 | + ); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + return array($users, $logData); |
|
| 572 | + } |
|
| 573 | 573 | } |
@@ -362,8 +362,7 @@ discard block |
||
| 362 | 362 | // some old templates have been completely deleted and lost to the depths of time. |
| 363 | 363 | if ($welcomeTemplate === false) { |
| 364 | 364 | return "Welcome template #{$objectId}"; |
| 365 | - } |
|
| 366 | - else { |
|
| 365 | + } else { |
|
| 367 | 366 | $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
| 368 | 367 | |
| 369 | 368 | return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
@@ -381,8 +380,7 @@ discard block |
||
| 381 | 380 | $task = $job->getTask(); |
| 382 | 381 | if (isset($taskDescriptions[$task])) { |
| 383 | 382 | $description = $taskDescriptions[$task]; |
| 384 | - } |
|
| 385 | - else { |
|
| 383 | + } else { |
|
| 386 | 384 | $description = 'Unknown task'; |
| 387 | 385 | } |
| 388 | 386 | |
@@ -549,11 +547,9 @@ discard block |
||
| 549 | 547 | if (in_array($logEntry->getAction(), $protectedLogActions) && $logEntry->getObjectType() === 'User') { |
| 550 | 548 | if ($allowAccountLog) { |
| 551 | 549 | // do nothing, allowed to see all account logs |
| 552 | - } |
|
| 553 | - else if ($allowAccountLogSelf && $currentUser->getId() === $logEntry->getObjectId()) { |
|
| 550 | + } else if ($allowAccountLogSelf && $currentUser->getId() === $logEntry->getObjectId()) { |
|
| 554 | 551 | // do nothing, allowed to see own account log |
| 555 | - } |
|
| 556 | - else { |
|
| 552 | + } else { |
|
| 557 | 553 | $comment = null; |
| 558 | 554 | } |
| 559 | 555 | } |
@@ -37,108 +37,108 @@ |
||
| 37 | 37 | |
| 38 | 38 | trait NavigationMenuAccessControl |
| 39 | 39 | { |
| 40 | - protected abstract function assign($name, $value); |
|
| 40 | + protected abstract function assign($name, $value); |
|
| 41 | 41 | |
| 42 | - protected abstract function getSecurityManager(): ISecurityManager; |
|
| 42 | + protected abstract function getSecurityManager(): ISecurityManager; |
|
| 43 | 43 | |
| 44 | - public abstract function getDomainAccessManager(): IDomainAccessManager; |
|
| 44 | + public abstract function getDomainAccessManager(): IDomainAccessManager; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param $currentUser |
|
| 48 | - */ |
|
| 49 | - protected function setupNavMenuAccess($currentUser) |
|
| 50 | - { |
|
| 51 | - $this->assign('nav__canRequests', $this->getSecurityManager() |
|
| 52 | - ->allows(PageMain::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 46 | + /** |
|
| 47 | + * @param $currentUser |
|
| 48 | + */ |
|
| 49 | + protected function setupNavMenuAccess($currentUser) |
|
| 50 | + { |
|
| 51 | + $this->assign('nav__canRequests', $this->getSecurityManager() |
|
| 52 | + ->allows(PageMain::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 53 | 53 | |
| 54 | - $this->assign('nav__canLogs', $this->getSecurityManager() |
|
| 55 | - ->allows(PageLog::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 56 | - $this->assign('nav__canUsers', $this->getSecurityManager() |
|
| 57 | - ->allows(StatsUsers::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 58 | - $this->assign('nav__canSearch', $this->getSecurityManager() |
|
| 59 | - ->allows(PageSearch::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 60 | - $this->assign('nav__canStats', $this->getSecurityManager() |
|
| 61 | - ->allows(StatsMain::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 54 | + $this->assign('nav__canLogs', $this->getSecurityManager() |
|
| 55 | + ->allows(PageLog::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 56 | + $this->assign('nav__canUsers', $this->getSecurityManager() |
|
| 57 | + ->allows(StatsUsers::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 58 | + $this->assign('nav__canSearch', $this->getSecurityManager() |
|
| 59 | + ->allows(PageSearch::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 60 | + $this->assign('nav__canStats', $this->getSecurityManager() |
|
| 61 | + ->allows(StatsMain::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 62 | 62 | |
| 63 | - $this->assign('nav__canBan', $this->getSecurityManager() |
|
| 64 | - ->allows(PageBan::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 65 | - $this->assign('nav__canEmailMgmt', $this->getSecurityManager() |
|
| 66 | - ->allows(PageEmailManagement::class, RoleConfigurationBase::MAIN, |
|
| 67 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 68 | - $this->assign('nav__canWelcomeMgmt', $this->getSecurityManager() |
|
| 69 | - ->allows(PageWelcomeTemplateManagement::class, RoleConfigurationBase::MAIN, |
|
| 70 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 71 | - $this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager() |
|
| 72 | - ->allows(PageSiteNotice::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 73 | - $this->assign('nav__canUserMgmt', $this->getSecurityManager() |
|
| 74 | - ->allows(PageUserManagement::class, RoleConfigurationBase::MAIN, |
|
| 75 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 76 | - $this->assign('nav__canJobQueue', $this->getSecurityManager() |
|
| 77 | - ->allows(PageJobQueue::class, RoleConfigurationBase::MAIN, |
|
| 78 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 79 | - $this->assign('nav__canDomainMgmt', $this->getSecurityManager() |
|
| 80 | - ->allows(PageDomainManagement::class, RoleConfigurationBase::MAIN, |
|
| 81 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 82 | - $this->assign('nav__canFlaggedComments', $this->getSecurityManager() |
|
| 83 | - ->allows(PageListFlaggedComments::class, RoleConfigurationBase::MAIN, |
|
| 84 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 85 | - $this->assign('nav__canQueueMgmt', $this->getSecurityManager() |
|
| 86 | - ->allows(PageQueueManagement::class, RoleConfigurationBase::MAIN, |
|
| 87 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 88 | - $this->assign('nav__canFormMgmt', $this->getSecurityManager() |
|
| 89 | - ->allows(PageRequestFormManagement::class, RoleConfigurationBase::MAIN, |
|
| 90 | - $currentUser) === ISecurityManager::ALLOWED); |
|
| 91 | - $this->assign('nav__canErrorLog', $this->getSecurityManager() |
|
| 92 | - ->allows(PageErrorLogViewer::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 63 | + $this->assign('nav__canBan', $this->getSecurityManager() |
|
| 64 | + ->allows(PageBan::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 65 | + $this->assign('nav__canEmailMgmt', $this->getSecurityManager() |
|
| 66 | + ->allows(PageEmailManagement::class, RoleConfigurationBase::MAIN, |
|
| 67 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 68 | + $this->assign('nav__canWelcomeMgmt', $this->getSecurityManager() |
|
| 69 | + ->allows(PageWelcomeTemplateManagement::class, RoleConfigurationBase::MAIN, |
|
| 70 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 71 | + $this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager() |
|
| 72 | + ->allows(PageSiteNotice::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 73 | + $this->assign('nav__canUserMgmt', $this->getSecurityManager() |
|
| 74 | + ->allows(PageUserManagement::class, RoleConfigurationBase::MAIN, |
|
| 75 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 76 | + $this->assign('nav__canJobQueue', $this->getSecurityManager() |
|
| 77 | + ->allows(PageJobQueue::class, RoleConfigurationBase::MAIN, |
|
| 78 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 79 | + $this->assign('nav__canDomainMgmt', $this->getSecurityManager() |
|
| 80 | + ->allows(PageDomainManagement::class, RoleConfigurationBase::MAIN, |
|
| 81 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 82 | + $this->assign('nav__canFlaggedComments', $this->getSecurityManager() |
|
| 83 | + ->allows(PageListFlaggedComments::class, RoleConfigurationBase::MAIN, |
|
| 84 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 85 | + $this->assign('nav__canQueueMgmt', $this->getSecurityManager() |
|
| 86 | + ->allows(PageQueueManagement::class, RoleConfigurationBase::MAIN, |
|
| 87 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 88 | + $this->assign('nav__canFormMgmt', $this->getSecurityManager() |
|
| 89 | + ->allows(PageRequestFormManagement::class, RoleConfigurationBase::MAIN, |
|
| 90 | + $currentUser) === ISecurityManager::ALLOWED); |
|
| 91 | + $this->assign('nav__canErrorLog', $this->getSecurityManager() |
|
| 92 | + ->allows(PageErrorLogViewer::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 93 | 93 | |
| 94 | - $this->assign('nav__canViewRequest', $this->getSecurityManager() |
|
| 95 | - ->allows(PageViewRequest::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 94 | + $this->assign('nav__canViewRequest', $this->getSecurityManager() |
|
| 95 | + ->allows(PageViewRequest::class, RoleConfigurationBase::MAIN, $currentUser) === ISecurityManager::ALLOWED); |
|
| 96 | 96 | |
| 97 | - $this->assign('nav__domainList', []); |
|
| 98 | - if ($this->getDomainAccessManager() !== null) { |
|
| 99 | - $this->assign('nav__domainList', $this->getDomainAccessManager()->getAllowedDomains($currentUser)); |
|
| 100 | - } |
|
| 101 | - } |
|
| 97 | + $this->assign('nav__domainList', []); |
|
| 98 | + if ($this->getDomainAccessManager() !== null) { |
|
| 99 | + $this->assign('nav__domainList', $this->getDomainAccessManager()->getAllowedDomains($currentUser)); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Sets up the badges to draw attention to issues on various admin pages. |
|
| 105 | - * |
|
| 106 | - * This function checks to see if a user can access the pages, and if so checks the count of problem areas. |
|
| 107 | - * If problem areas are found, a number greater than 0 will cause the badge to show up. |
|
| 108 | - * |
|
| 109 | - * @param User $currentUser The current user |
|
| 110 | - * @param PdoDatabase $database Database instance |
|
| 111 | - * |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) { |
|
| 115 | - // Set up some variables. |
|
| 116 | - // A size of 0 causes nothing to show up on the page (checked on navigation-menu.tpl) so leaving it 0 here is fine. |
|
| 117 | - $countOfFlagged = 0; |
|
| 118 | - $countOfJobQueue = 0; |
|
| 103 | + /** |
|
| 104 | + * Sets up the badges to draw attention to issues on various admin pages. |
|
| 105 | + * |
|
| 106 | + * This function checks to see if a user can access the pages, and if so checks the count of problem areas. |
|
| 107 | + * If problem areas are found, a number greater than 0 will cause the badge to show up. |
|
| 108 | + * |
|
| 109 | + * @param User $currentUser The current user |
|
| 110 | + * @param PdoDatabase $database Database instance |
|
| 111 | + * |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) { |
|
| 115 | + // Set up some variables. |
|
| 116 | + // A size of 0 causes nothing to show up on the page (checked on navigation-menu.tpl) so leaving it 0 here is fine. |
|
| 117 | + $countOfFlagged = 0; |
|
| 118 | + $countOfJobQueue = 0; |
|
| 119 | 119 | |
| 120 | - // Count of flagged comments: |
|
| 121 | - if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 122 | - // We want all flagged comments that haven't been acknowledged if we can visit the page. |
|
| 123 | - $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
|
| 124 | - } |
|
| 120 | + // Count of flagged comments: |
|
| 121 | + if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 122 | + // We want all flagged comments that haven't been acknowledged if we can visit the page. |
|
| 123 | + $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // Count of failed job queue changes: |
|
| 127 | - if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 128 | - // We want all failed jobs that haven't been acknowledged if we can visit the page. |
|
| 129 | - JobQueueSearchHelper::get($database, 1) // FIXME: domains |
|
| 130 | - ->statusIn([JobQueue::STATUS_FAILED]) |
|
| 131 | - ->notAcknowledged() |
|
| 132 | - ->getRecordCount($countOfJobQueue); |
|
| 133 | - } |
|
| 126 | + // Count of failed job queue changes: |
|
| 127 | + if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 128 | + // We want all failed jobs that haven't been acknowledged if we can visit the page. |
|
| 129 | + JobQueueSearchHelper::get($database, 1) // FIXME: domains |
|
| 130 | + ->statusIn([JobQueue::STATUS_FAILED]) |
|
| 131 | + ->notAcknowledged() |
|
| 132 | + ->getRecordCount($countOfJobQueue); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - // To generate the main badge, add both up. |
|
| 136 | - // If we add more badges in the future, don't forget to add them here! |
|
| 137 | - $countOfAll = $countOfFlagged + $countOfJobQueue; |
|
| 135 | + // To generate the main badge, add both up. |
|
| 136 | + // If we add more badges in the future, don't forget to add them here! |
|
| 137 | + $countOfAll = $countOfFlagged + $countOfJobQueue; |
|
| 138 | 138 | |
| 139 | - // Set badge variables |
|
| 140 | - $this->assign("nav__numFlaggedComments", $countOfFlagged); |
|
| 141 | - $this->assign("nav__numJobQueueFailed", $countOfJobQueue); |
|
| 142 | - $this->assign("nav__numAdmin", $countOfAll); |
|
| 143 | - } |
|
| 139 | + // Set badge variables |
|
| 140 | + $this->assign("nav__numFlaggedComments", $countOfFlagged); |
|
| 141 | + $this->assign("nav__numJobQueueFailed", $countOfJobQueue); |
|
| 142 | + $this->assign("nav__numAdmin", $countOfAll); |
|
| 143 | + } |
|
| 144 | 144 | } |
@@ -118,13 +118,13 @@ |
||
| 118 | 118 | $countOfJobQueue = 0; |
| 119 | 119 | |
| 120 | 120 | // Count of flagged comments: |
| 121 | - if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 121 | + if ($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 122 | 122 | // We want all flagged comments that haven't been acknowledged if we can visit the page. |
| 123 | 123 | $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // Count of failed job queue changes: |
| 127 | - if($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 127 | + if ($this->barrierTest(RoleConfigurationBase::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 128 | 128 | // We want all failed jobs that haven't been acknowledged if we can visit the page. |
| 129 | 129 | JobQueueSearchHelper::get($database, 1) // FIXME: domains |
| 130 | 130 | ->statusIn([JobQueue::STATUS_FAILED]) |
@@ -27,350 +27,350 @@ |
||
| 27 | 27 | |
| 28 | 28 | trait RequestData |
| 29 | 29 | { |
| 30 | - /** @return SiteConfiguration */ |
|
| 31 | - protected abstract function getSiteConfiguration(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array Array of IP address classed as 'private' by RFC1918. |
|
| 35 | - */ |
|
| 36 | - protected static $rfc1918ips = array( |
|
| 37 | - "10.0.0.0" => "10.255.255.255", |
|
| 38 | - "172.16.0.0" => "172.31.255.255", |
|
| 39 | - "192.168.0.0" => "192.168.255.255", |
|
| 40 | - "169.254.0.0" => "169.254.255.255", |
|
| 41 | - "127.0.0.0" => "127.255.255.255", |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Gets a request object |
|
| 46 | - * |
|
| 47 | - * @param PdoDatabase $database The database connection |
|
| 48 | - * @param int|null $requestId The ID of the request to retrieve |
|
| 49 | - * |
|
| 50 | - * @return Request |
|
| 51 | - * @throws ApplicationLogicException |
|
| 52 | - */ |
|
| 53 | - protected function getRequest(PdoDatabase $database, $requestId) |
|
| 54 | - { |
|
| 55 | - if ($requestId === null) { |
|
| 56 | - throw new ApplicationLogicException("No request specified"); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $request = Request::getById($requestId, $database); |
|
| 60 | - if ($request === false || !is_a($request, Request::class)) { |
|
| 61 | - throw new ApplicationLogicException('Could not load the requested request!'); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - return $request; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Returns a value stating whether the user is allowed to see private data or not |
|
| 69 | - * |
|
| 70 | - * @param Request $request |
|
| 71 | - * @param User $currentUser |
|
| 72 | - * |
|
| 73 | - * @return bool |
|
| 74 | - * @category Security-Critical |
|
| 75 | - */ |
|
| 76 | - protected function isAllowedPrivateData(Request $request, User $currentUser) |
|
| 77 | - { |
|
| 78 | - // Test the main security barrier for private data access using SecurityManager |
|
| 79 | - if ($this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')) { |
|
| 80 | - // Tool admins/check-users can always see private data |
|
| 81 | - return true; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // reserving user is allowed to see the data |
|
| 85 | - if ($currentUser->getId() === $request->getReserved() |
|
| 86 | - && $request->getReserved() !== null |
|
| 87 | - && $this->barrierTest('seePrivateDataWhenReserved', $currentUser, 'RequestData') |
|
| 88 | - ) { |
|
| 89 | - return true; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // user has the reveal hash |
|
| 93 | - if (WebRequest::getString('hash') === $request->getRevealHash() |
|
| 94 | - && $this->barrierTest('seePrivateDataWithHash', $currentUser, 'RequestData') |
|
| 95 | - ) { |
|
| 96 | - return true; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // nope. Not allowed. |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Tests the security barrier for a specified action. |
|
| 105 | - * |
|
| 106 | - * Don't use within templates |
|
| 107 | - * |
|
| 108 | - * @param string $action |
|
| 109 | - * |
|
| 110 | - * @param User $user |
|
| 111 | - * @param null|string $pageName |
|
| 112 | - * |
|
| 113 | - * @return bool |
|
| 114 | - * @category Security-Critical |
|
| 115 | - */ |
|
| 116 | - abstract protected function barrierTest($action, User $user, $pageName = null); |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Gets the name of the route that has been passed from the request router. |
|
| 120 | - * @return string |
|
| 121 | - */ |
|
| 122 | - abstract protected function getRouteName(); |
|
| 123 | - |
|
| 124 | - abstract protected function getSecurityManager(): ISecurityManager; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Sets the name of the template this page should display. |
|
| 128 | - * |
|
| 129 | - * @param string $name |
|
| 130 | - */ |
|
| 131 | - abstract protected function setTemplate($name); |
|
| 132 | - |
|
| 133 | - /** @return IXffTrustProvider */ |
|
| 134 | - abstract protected function getXffTrustProvider(); |
|
| 135 | - |
|
| 136 | - /** @return ILocationProvider */ |
|
| 137 | - abstract protected function getLocationProvider(); |
|
| 138 | - |
|
| 139 | - /** @return IRDnsProvider */ |
|
| 140 | - abstract protected function getRdnsProvider(); |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Assigns a Smarty variable |
|
| 144 | - * |
|
| 145 | - * @param array|string $name the template variable name(s) |
|
| 146 | - * @param mixed $value the value to assign |
|
| 147 | - */ |
|
| 148 | - abstract protected function assign($name, $value); |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param int|null $requestReservationId |
|
| 152 | - * @param PdoDatabase $database |
|
| 153 | - * @param User $currentUser |
|
| 154 | - */ |
|
| 155 | - protected function setupReservationDetails($requestReservationId, PdoDatabase $database, User $currentUser) |
|
| 156 | - { |
|
| 157 | - $requestIsReserved = $requestReservationId !== null; |
|
| 158 | - $this->assign('requestIsReserved', $requestIsReserved); |
|
| 159 | - $this->assign('requestIsReservedByMe', false); |
|
| 160 | - |
|
| 161 | - if ($requestIsReserved) { |
|
| 162 | - $this->assign('requestReservedByName', User::getById($requestReservationId, $database)->getUsername()); |
|
| 163 | - $this->assign('requestReservedById', $requestReservationId); |
|
| 164 | - |
|
| 165 | - if ($requestReservationId === $currentUser->getId()) { |
|
| 166 | - $this->assign('requestIsReservedByMe', true); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class)); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Adds private request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 175 | - * |
|
| 176 | - * @param Request $request |
|
| 177 | - * @param SiteConfiguration $configuration |
|
| 178 | - */ |
|
| 179 | - protected function setupPrivateData( |
|
| 180 | - $request, |
|
| 181 | - SiteConfiguration $configuration |
|
| 182 | - ) { |
|
| 183 | - $xffProvider = $this->getXffTrustProvider(); |
|
| 184 | - |
|
| 185 | - $this->assign('requestEmail', $request->getEmail()); |
|
| 186 | - $emailDomain = explode("@", $request->getEmail())[1]; |
|
| 187 | - $this->assign("emailurl", $emailDomain); |
|
| 188 | - $this->assign('commonEmailDomain', in_array(strtolower($emailDomain), $configuration->getCommonEmailDomains()) |
|
| 189 | - || $request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()); |
|
| 190 | - |
|
| 191 | - $trustedIp = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 192 | - $this->assign('requestTrustedIp', $trustedIp); |
|
| 193 | - $this->assign('requestRealIp', $request->getIp()); |
|
| 194 | - $this->assign('requestForwardedIp', $request->getForwardedIp()); |
|
| 195 | - |
|
| 196 | - $trustedIpLocation = $this->getLocationProvider()->getIpLocation($trustedIp); |
|
| 197 | - $this->assign('requestTrustedIpLocation', $trustedIpLocation); |
|
| 198 | - |
|
| 199 | - $this->assign('requestHasForwardedIp', $request->getForwardedIp() !== null); |
|
| 200 | - |
|
| 201 | - $this->setupForwardedIpData($request); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Adds related request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 206 | - * |
|
| 207 | - * @param Request $request |
|
| 208 | - * @param SiteConfiguration $configuration |
|
| 209 | - * @param PdoDatabase $database |
|
| 210 | - */ |
|
| 211 | - protected function setupRelatedRequests( |
|
| 212 | - Request $request, |
|
| 213 | - SiteConfiguration $configuration, |
|
| 214 | - PdoDatabase $database) |
|
| 215 | - { |
|
| 216 | - $this->assign('canSeeRelatedRequests', true); |
|
| 217 | - |
|
| 218 | - // TODO: Do we want to return results from other domains? |
|
| 219 | - $relatedEmailRequests = RequestSearchHelper::get($database, null) |
|
| 220 | - ->byEmailAddress($request->getEmail()) |
|
| 221 | - ->withConfirmedEmail() |
|
| 222 | - ->excludingPurgedData($configuration) |
|
| 223 | - ->excludingRequest($request->getId()) |
|
| 224 | - ->fetch(); |
|
| 225 | - |
|
| 226 | - $this->assign('requestRelatedEmailRequestsCount', count($relatedEmailRequests)); |
|
| 227 | - $this->assign('requestRelatedEmailRequests', $relatedEmailRequests); |
|
| 228 | - |
|
| 229 | - $trustedIp = $this->getXffTrustProvider()->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 230 | - |
|
| 231 | - // TODO: Do we want to return results from other domains? |
|
| 232 | - $relatedIpRequests = RequestSearchHelper::get($database, null) |
|
| 233 | - ->byIp($trustedIp) |
|
| 234 | - ->withConfirmedEmail() |
|
| 235 | - ->excludingPurgedData($configuration) |
|
| 236 | - ->excludingRequest($request->getId()) |
|
| 237 | - ->fetch(); |
|
| 238 | - |
|
| 239 | - $this->assign('requestRelatedIpRequestsCount', count($relatedIpRequests)); |
|
| 240 | - $this->assign('requestRelatedIpRequests', $relatedIpRequests); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Adds checkuser request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 245 | - * |
|
| 246 | - * @param Request $request |
|
| 247 | - */ |
|
| 248 | - protected function setupCheckUserData(Request $request) |
|
| 249 | - { |
|
| 250 | - $this->assign('requestUserAgent', $request->getUserAgent()); |
|
| 251 | - |
|
| 252 | - $data = \Waca\DataObjects\RequestData::getForRequest($request->getId(), $request->getDatabase(), \Waca\DataObjects\RequestData::TYPE_CLIENTHINT); |
|
| 253 | - $this->assign('requestClientHints', $data); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Sets up the basic data for this request, and adds it to Smarty |
|
| 258 | - * |
|
| 259 | - * @param Request $request |
|
| 260 | - * @param SiteConfiguration $config |
|
| 261 | - */ |
|
| 262 | - protected function setupBasicData(Request $request, SiteConfiguration $config) |
|
| 263 | - { |
|
| 264 | - $this->assign('requestId', $request->getId()); |
|
| 265 | - $this->assign('updateVersion', $request->getUpdateVersion()); |
|
| 266 | - $this->assign('requestName', $request->getName()); |
|
| 267 | - $this->assign('requestDate', $request->getDate()); |
|
| 268 | - $this->assign('requestStatus', $request->getStatus()); |
|
| 269 | - |
|
| 270 | - $this->assign('requestQueue', null); |
|
| 271 | - if ($request->getQueue() !== null) { |
|
| 272 | - /** @var RequestQueue $queue */ |
|
| 273 | - $queue = RequestQueue::getById($request->getQueue(), $this->getDatabase()); |
|
| 274 | - $this->assign('requestQueue', $queue->getHeader()); |
|
| 275 | - $this->assign('requestQueueApiName', $queue->getApiName()); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $this->assign('canPreviewForm', $this->barrierTest('view', User::getCurrent($this->getDatabase()), PageRequestFormManagement::class)); |
|
| 279 | - $this->assign('originForm', $request->getOriginFormObject()); |
|
| 280 | - |
|
| 281 | - $isClosed = $request->getStatus() === RequestStatus::CLOSED || $request->getStatus() === RequestStatus::JOBQUEUE; |
|
| 282 | - $this->assign('requestIsClosed', $isClosed); |
|
| 30 | + /** @return SiteConfiguration */ |
|
| 31 | + protected abstract function getSiteConfiguration(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array Array of IP address classed as 'private' by RFC1918. |
|
| 35 | + */ |
|
| 36 | + protected static $rfc1918ips = array( |
|
| 37 | + "10.0.0.0" => "10.255.255.255", |
|
| 38 | + "172.16.0.0" => "172.31.255.255", |
|
| 39 | + "192.168.0.0" => "192.168.255.255", |
|
| 40 | + "169.254.0.0" => "169.254.255.255", |
|
| 41 | + "127.0.0.0" => "127.255.255.255", |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Gets a request object |
|
| 46 | + * |
|
| 47 | + * @param PdoDatabase $database The database connection |
|
| 48 | + * @param int|null $requestId The ID of the request to retrieve |
|
| 49 | + * |
|
| 50 | + * @return Request |
|
| 51 | + * @throws ApplicationLogicException |
|
| 52 | + */ |
|
| 53 | + protected function getRequest(PdoDatabase $database, $requestId) |
|
| 54 | + { |
|
| 55 | + if ($requestId === null) { |
|
| 56 | + throw new ApplicationLogicException("No request specified"); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $request = Request::getById($requestId, $database); |
|
| 60 | + if ($request === false || !is_a($request, Request::class)) { |
|
| 61 | + throw new ApplicationLogicException('Could not load the requested request!'); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + return $request; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Returns a value stating whether the user is allowed to see private data or not |
|
| 69 | + * |
|
| 70 | + * @param Request $request |
|
| 71 | + * @param User $currentUser |
|
| 72 | + * |
|
| 73 | + * @return bool |
|
| 74 | + * @category Security-Critical |
|
| 75 | + */ |
|
| 76 | + protected function isAllowedPrivateData(Request $request, User $currentUser) |
|
| 77 | + { |
|
| 78 | + // Test the main security barrier for private data access using SecurityManager |
|
| 79 | + if ($this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')) { |
|
| 80 | + // Tool admins/check-users can always see private data |
|
| 81 | + return true; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // reserving user is allowed to see the data |
|
| 85 | + if ($currentUser->getId() === $request->getReserved() |
|
| 86 | + && $request->getReserved() !== null |
|
| 87 | + && $this->barrierTest('seePrivateDataWhenReserved', $currentUser, 'RequestData') |
|
| 88 | + ) { |
|
| 89 | + return true; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // user has the reveal hash |
|
| 93 | + if (WebRequest::getString('hash') === $request->getRevealHash() |
|
| 94 | + && $this->barrierTest('seePrivateDataWithHash', $currentUser, 'RequestData') |
|
| 95 | + ) { |
|
| 96 | + return true; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // nope. Not allowed. |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Tests the security barrier for a specified action. |
|
| 105 | + * |
|
| 106 | + * Don't use within templates |
|
| 107 | + * |
|
| 108 | + * @param string $action |
|
| 109 | + * |
|
| 110 | + * @param User $user |
|
| 111 | + * @param null|string $pageName |
|
| 112 | + * |
|
| 113 | + * @return bool |
|
| 114 | + * @category Security-Critical |
|
| 115 | + */ |
|
| 116 | + abstract protected function barrierTest($action, User $user, $pageName = null); |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Gets the name of the route that has been passed from the request router. |
|
| 120 | + * @return string |
|
| 121 | + */ |
|
| 122 | + abstract protected function getRouteName(); |
|
| 123 | + |
|
| 124 | + abstract protected function getSecurityManager(): ISecurityManager; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Sets the name of the template this page should display. |
|
| 128 | + * |
|
| 129 | + * @param string $name |
|
| 130 | + */ |
|
| 131 | + abstract protected function setTemplate($name); |
|
| 132 | + |
|
| 133 | + /** @return IXffTrustProvider */ |
|
| 134 | + abstract protected function getXffTrustProvider(); |
|
| 135 | + |
|
| 136 | + /** @return ILocationProvider */ |
|
| 137 | + abstract protected function getLocationProvider(); |
|
| 138 | + |
|
| 139 | + /** @return IRDnsProvider */ |
|
| 140 | + abstract protected function getRdnsProvider(); |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Assigns a Smarty variable |
|
| 144 | + * |
|
| 145 | + * @param array|string $name the template variable name(s) |
|
| 146 | + * @param mixed $value the value to assign |
|
| 147 | + */ |
|
| 148 | + abstract protected function assign($name, $value); |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param int|null $requestReservationId |
|
| 152 | + * @param PdoDatabase $database |
|
| 153 | + * @param User $currentUser |
|
| 154 | + */ |
|
| 155 | + protected function setupReservationDetails($requestReservationId, PdoDatabase $database, User $currentUser) |
|
| 156 | + { |
|
| 157 | + $requestIsReserved = $requestReservationId !== null; |
|
| 158 | + $this->assign('requestIsReserved', $requestIsReserved); |
|
| 159 | + $this->assign('requestIsReservedByMe', false); |
|
| 160 | + |
|
| 161 | + if ($requestIsReserved) { |
|
| 162 | + $this->assign('requestReservedByName', User::getById($requestReservationId, $database)->getUsername()); |
|
| 163 | + $this->assign('requestReservedById', $requestReservationId); |
|
| 164 | + |
|
| 165 | + if ($requestReservationId === $currentUser->getId()) { |
|
| 166 | + $this->assign('requestIsReservedByMe', true); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class)); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Adds private request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 175 | + * |
|
| 176 | + * @param Request $request |
|
| 177 | + * @param SiteConfiguration $configuration |
|
| 178 | + */ |
|
| 179 | + protected function setupPrivateData( |
|
| 180 | + $request, |
|
| 181 | + SiteConfiguration $configuration |
|
| 182 | + ) { |
|
| 183 | + $xffProvider = $this->getXffTrustProvider(); |
|
| 184 | + |
|
| 185 | + $this->assign('requestEmail', $request->getEmail()); |
|
| 186 | + $emailDomain = explode("@", $request->getEmail())[1]; |
|
| 187 | + $this->assign("emailurl", $emailDomain); |
|
| 188 | + $this->assign('commonEmailDomain', in_array(strtolower($emailDomain), $configuration->getCommonEmailDomains()) |
|
| 189 | + || $request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()); |
|
| 190 | + |
|
| 191 | + $trustedIp = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 192 | + $this->assign('requestTrustedIp', $trustedIp); |
|
| 193 | + $this->assign('requestRealIp', $request->getIp()); |
|
| 194 | + $this->assign('requestForwardedIp', $request->getForwardedIp()); |
|
| 195 | + |
|
| 196 | + $trustedIpLocation = $this->getLocationProvider()->getIpLocation($trustedIp); |
|
| 197 | + $this->assign('requestTrustedIpLocation', $trustedIpLocation); |
|
| 198 | + |
|
| 199 | + $this->assign('requestHasForwardedIp', $request->getForwardedIp() !== null); |
|
| 200 | + |
|
| 201 | + $this->setupForwardedIpData($request); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Adds related request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 206 | + * |
|
| 207 | + * @param Request $request |
|
| 208 | + * @param SiteConfiguration $configuration |
|
| 209 | + * @param PdoDatabase $database |
|
| 210 | + */ |
|
| 211 | + protected function setupRelatedRequests( |
|
| 212 | + Request $request, |
|
| 213 | + SiteConfiguration $configuration, |
|
| 214 | + PdoDatabase $database) |
|
| 215 | + { |
|
| 216 | + $this->assign('canSeeRelatedRequests', true); |
|
| 217 | + |
|
| 218 | + // TODO: Do we want to return results from other domains? |
|
| 219 | + $relatedEmailRequests = RequestSearchHelper::get($database, null) |
|
| 220 | + ->byEmailAddress($request->getEmail()) |
|
| 221 | + ->withConfirmedEmail() |
|
| 222 | + ->excludingPurgedData($configuration) |
|
| 223 | + ->excludingRequest($request->getId()) |
|
| 224 | + ->fetch(); |
|
| 225 | + |
|
| 226 | + $this->assign('requestRelatedEmailRequestsCount', count($relatedEmailRequests)); |
|
| 227 | + $this->assign('requestRelatedEmailRequests', $relatedEmailRequests); |
|
| 228 | + |
|
| 229 | + $trustedIp = $this->getXffTrustProvider()->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
| 230 | + |
|
| 231 | + // TODO: Do we want to return results from other domains? |
|
| 232 | + $relatedIpRequests = RequestSearchHelper::get($database, null) |
|
| 233 | + ->byIp($trustedIp) |
|
| 234 | + ->withConfirmedEmail() |
|
| 235 | + ->excludingPurgedData($configuration) |
|
| 236 | + ->excludingRequest($request->getId()) |
|
| 237 | + ->fetch(); |
|
| 238 | + |
|
| 239 | + $this->assign('requestRelatedIpRequestsCount', count($relatedIpRequests)); |
|
| 240 | + $this->assign('requestRelatedIpRequests', $relatedIpRequests); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Adds checkuser request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
|
| 245 | + * |
|
| 246 | + * @param Request $request |
|
| 247 | + */ |
|
| 248 | + protected function setupCheckUserData(Request $request) |
|
| 249 | + { |
|
| 250 | + $this->assign('requestUserAgent', $request->getUserAgent()); |
|
| 251 | + |
|
| 252 | + $data = \Waca\DataObjects\RequestData::getForRequest($request->getId(), $request->getDatabase(), \Waca\DataObjects\RequestData::TYPE_CLIENTHINT); |
|
| 253 | + $this->assign('requestClientHints', $data); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Sets up the basic data for this request, and adds it to Smarty |
|
| 258 | + * |
|
| 259 | + * @param Request $request |
|
| 260 | + * @param SiteConfiguration $config |
|
| 261 | + */ |
|
| 262 | + protected function setupBasicData(Request $request, SiteConfiguration $config) |
|
| 263 | + { |
|
| 264 | + $this->assign('requestId', $request->getId()); |
|
| 265 | + $this->assign('updateVersion', $request->getUpdateVersion()); |
|
| 266 | + $this->assign('requestName', $request->getName()); |
|
| 267 | + $this->assign('requestDate', $request->getDate()); |
|
| 268 | + $this->assign('requestStatus', $request->getStatus()); |
|
| 269 | + |
|
| 270 | + $this->assign('requestQueue', null); |
|
| 271 | + if ($request->getQueue() !== null) { |
|
| 272 | + /** @var RequestQueue $queue */ |
|
| 273 | + $queue = RequestQueue::getById($request->getQueue(), $this->getDatabase()); |
|
| 274 | + $this->assign('requestQueue', $queue->getHeader()); |
|
| 275 | + $this->assign('requestQueueApiName', $queue->getApiName()); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $this->assign('canPreviewForm', $this->barrierTest('view', User::getCurrent($this->getDatabase()), PageRequestFormManagement::class)); |
|
| 279 | + $this->assign('originForm', $request->getOriginFormObject()); |
|
| 280 | + |
|
| 281 | + $isClosed = $request->getStatus() === RequestStatus::CLOSED || $request->getStatus() === RequestStatus::JOBQUEUE; |
|
| 282 | + $this->assign('requestIsClosed', $isClosed); |
|
| 283 | 283 | $isHospital = $request->getStatus() === RequestStatus::HOSPITAL; |
| 284 | 284 | $this->assign('requestIsHospital', $isHospital); |
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Sets up the forwarded IP data for this request and adds it to Smarty |
|
| 289 | - * |
|
| 290 | - * @param Request $request |
|
| 291 | - */ |
|
| 292 | - protected function setupForwardedIpData(Request $request) |
|
| 293 | - { |
|
| 294 | - if ($request->getForwardedIp() !== null) { |
|
| 295 | - $requestProxyData = array(); // Initialize array to store data to be output in Smarty template. |
|
| 296 | - $proxyIndex = 0; |
|
| 297 | - |
|
| 298 | - // Assuming [client] <=> [proxy1] <=> [proxy2] <=> [proxy3] <=> [us], we will see an XFF header of [client], |
|
| 299 | - // [proxy1], [proxy2], and our actual IP will be [proxy3] |
|
| 300 | - $proxies = explode(",", $request->getForwardedIp()); |
|
| 301 | - $proxies[] = $request->getIp(); |
|
| 302 | - |
|
| 303 | - // Origin is the supposed "client" IP. |
|
| 304 | - $origin = $proxies[0]; |
|
| 305 | - $this->assign("forwardedOrigin", $origin); |
|
| 306 | - |
|
| 307 | - // We step through the servers in reverse order, from closest to furthest |
|
| 308 | - $proxies = array_reverse($proxies); |
|
| 309 | - |
|
| 310 | - // By default, we have trust, because the first in the chain is now REMOTE_ADDR, which is hardest to spoof. |
|
| 311 | - $trust = true; |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @var int $index The zero-based index of the proxy. |
|
| 315 | - * @var string $proxyData The proxy IP address (although possibly not!) |
|
| 316 | - */ |
|
| 317 | - foreach ($proxies as $index => $proxyData) { |
|
| 318 | - $proxyAddress = trim($proxyData); |
|
| 319 | - $requestProxyData[$proxyIndex]['ip'] = $proxyAddress; |
|
| 320 | - |
|
| 321 | - // get data on this IP. |
|
| 322 | - $thisProxyIsTrusted = $this->getXffTrustProvider()->isTrusted($proxyAddress); |
|
| 323 | - |
|
| 324 | - $proxyIsInPrivateRange = $this->getXffTrustProvider() |
|
| 325 | - ->ipInRange(self::$rfc1918ips, $proxyAddress); |
|
| 326 | - |
|
| 327 | - if (!$proxyIsInPrivateRange) { |
|
| 328 | - $proxyReverseDns = $this->getRdnsProvider()->getReverseDNS($proxyAddress); |
|
| 329 | - $proxyLocation = $this->getLocationProvider()->getIpLocation($proxyAddress); |
|
| 330 | - } |
|
| 331 | - else { |
|
| 332 | - // this is going to fail, so why bother trying? |
|
| 333 | - $proxyReverseDns = false; |
|
| 334 | - $proxyLocation = false; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // current trust chain status BEFORE this link |
|
| 338 | - $preLinkTrust = $trust; |
|
| 339 | - |
|
| 340 | - // is *this* link trusted? Note, this will be true even if there is an untrusted link before this! |
|
| 341 | - $requestProxyData[$proxyIndex]['trustedlink'] = $thisProxyIsTrusted; |
|
| 342 | - |
|
| 343 | - // set the trust status of the chain to this point |
|
| 344 | - $trust = $trust & $thisProxyIsTrusted; |
|
| 345 | - |
|
| 346 | - // If this is the origin address, and the chain was trusted before this point, then we can trust |
|
| 347 | - // the origin. |
|
| 348 | - if ($preLinkTrust && $proxyAddress == $origin) { |
|
| 349 | - // if this is the origin, then we are at the last point in the chain. |
|
| 350 | - // @todo: this is probably the cause of some bugs when an IP appears twice - we're missing a check |
|
| 351 | - // to see if this is *really* the last in the chain, rather than just the same IP as it. |
|
| 352 | - $trust = true; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - $requestProxyData[$proxyIndex]['trust'] = $trust; |
|
| 356 | - |
|
| 357 | - $requestProxyData[$proxyIndex]['rdnsfailed'] = $proxyReverseDns === false; |
|
| 358 | - $requestProxyData[$proxyIndex]['rdns'] = $proxyReverseDns; |
|
| 359 | - $requestProxyData[$proxyIndex]['routable'] = !$proxyIsInPrivateRange; |
|
| 360 | - |
|
| 361 | - $requestProxyData[$proxyIndex]['location'] = $proxyLocation; |
|
| 362 | - |
|
| 363 | - if ($proxyReverseDns === $proxyAddress && $proxyIsInPrivateRange === false) { |
|
| 364 | - $requestProxyData[$proxyIndex]['rdns'] = null; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - $showLinks = (!$trust || $proxyAddress == $origin) && !$proxyIsInPrivateRange; |
|
| 368 | - $requestProxyData[$proxyIndex]['showlinks'] = $showLinks; |
|
| 369 | - |
|
| 370 | - $proxyIndex++; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $this->assign("requestProxyData", $requestProxyData); |
|
| 374 | - } |
|
| 375 | - } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Sets up the forwarded IP data for this request and adds it to Smarty |
|
| 289 | + * |
|
| 290 | + * @param Request $request |
|
| 291 | + */ |
|
| 292 | + protected function setupForwardedIpData(Request $request) |
|
| 293 | + { |
|
| 294 | + if ($request->getForwardedIp() !== null) { |
|
| 295 | + $requestProxyData = array(); // Initialize array to store data to be output in Smarty template. |
|
| 296 | + $proxyIndex = 0; |
|
| 297 | + |
|
| 298 | + // Assuming [client] <=> [proxy1] <=> [proxy2] <=> [proxy3] <=> [us], we will see an XFF header of [client], |
|
| 299 | + // [proxy1], [proxy2], and our actual IP will be [proxy3] |
|
| 300 | + $proxies = explode(",", $request->getForwardedIp()); |
|
| 301 | + $proxies[] = $request->getIp(); |
|
| 302 | + |
|
| 303 | + // Origin is the supposed "client" IP. |
|
| 304 | + $origin = $proxies[0]; |
|
| 305 | + $this->assign("forwardedOrigin", $origin); |
|
| 306 | + |
|
| 307 | + // We step through the servers in reverse order, from closest to furthest |
|
| 308 | + $proxies = array_reverse($proxies); |
|
| 309 | + |
|
| 310 | + // By default, we have trust, because the first in the chain is now REMOTE_ADDR, which is hardest to spoof. |
|
| 311 | + $trust = true; |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @var int $index The zero-based index of the proxy. |
|
| 315 | + * @var string $proxyData The proxy IP address (although possibly not!) |
|
| 316 | + */ |
|
| 317 | + foreach ($proxies as $index => $proxyData) { |
|
| 318 | + $proxyAddress = trim($proxyData); |
|
| 319 | + $requestProxyData[$proxyIndex]['ip'] = $proxyAddress; |
|
| 320 | + |
|
| 321 | + // get data on this IP. |
|
| 322 | + $thisProxyIsTrusted = $this->getXffTrustProvider()->isTrusted($proxyAddress); |
|
| 323 | + |
|
| 324 | + $proxyIsInPrivateRange = $this->getXffTrustProvider() |
|
| 325 | + ->ipInRange(self::$rfc1918ips, $proxyAddress); |
|
| 326 | + |
|
| 327 | + if (!$proxyIsInPrivateRange) { |
|
| 328 | + $proxyReverseDns = $this->getRdnsProvider()->getReverseDNS($proxyAddress); |
|
| 329 | + $proxyLocation = $this->getLocationProvider()->getIpLocation($proxyAddress); |
|
| 330 | + } |
|
| 331 | + else { |
|
| 332 | + // this is going to fail, so why bother trying? |
|
| 333 | + $proxyReverseDns = false; |
|
| 334 | + $proxyLocation = false; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // current trust chain status BEFORE this link |
|
| 338 | + $preLinkTrust = $trust; |
|
| 339 | + |
|
| 340 | + // is *this* link trusted? Note, this will be true even if there is an untrusted link before this! |
|
| 341 | + $requestProxyData[$proxyIndex]['trustedlink'] = $thisProxyIsTrusted; |
|
| 342 | + |
|
| 343 | + // set the trust status of the chain to this point |
|
| 344 | + $trust = $trust & $thisProxyIsTrusted; |
|
| 345 | + |
|
| 346 | + // If this is the origin address, and the chain was trusted before this point, then we can trust |
|
| 347 | + // the origin. |
|
| 348 | + if ($preLinkTrust && $proxyAddress == $origin) { |
|
| 349 | + // if this is the origin, then we are at the last point in the chain. |
|
| 350 | + // @todo: this is probably the cause of some bugs when an IP appears twice - we're missing a check |
|
| 351 | + // to see if this is *really* the last in the chain, rather than just the same IP as it. |
|
| 352 | + $trust = true; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + $requestProxyData[$proxyIndex]['trust'] = $trust; |
|
| 356 | + |
|
| 357 | + $requestProxyData[$proxyIndex]['rdnsfailed'] = $proxyReverseDns === false; |
|
| 358 | + $requestProxyData[$proxyIndex]['rdns'] = $proxyReverseDns; |
|
| 359 | + $requestProxyData[$proxyIndex]['routable'] = !$proxyIsInPrivateRange; |
|
| 360 | + |
|
| 361 | + $requestProxyData[$proxyIndex]['location'] = $proxyLocation; |
|
| 362 | + |
|
| 363 | + if ($proxyReverseDns === $proxyAddress && $proxyIsInPrivateRange === false) { |
|
| 364 | + $requestProxyData[$proxyIndex]['rdns'] = null; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + $showLinks = (!$trust || $proxyAddress == $origin) && !$proxyIsInPrivateRange; |
|
| 368 | + $requestProxyData[$proxyIndex]['showlinks'] = $showLinks; |
|
| 369 | + |
|
| 370 | + $proxyIndex++; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $this->assign("requestProxyData", $requestProxyData); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | 376 | } |
@@ -16,20 +16,20 @@ |
||
| 16 | 16 | |
| 17 | 17 | trait LogEntryLookup |
| 18 | 18 | { |
| 19 | - protected function getLogEntry(string $action, User $user, PdoDatabase $database): ?string |
|
| 20 | - { |
|
| 21 | - /** @var Log[] $logs */ |
|
| 22 | - $logs = LogSearchHelper::get($database, null) |
|
| 23 | - ->byAction($action) |
|
| 24 | - ->byObjectType('User') |
|
| 25 | - ->byObjectId($user->getId()) |
|
| 26 | - ->limit(1) |
|
| 27 | - ->fetch(); |
|
| 19 | + protected function getLogEntry(string $action, User $user, PdoDatabase $database): ?string |
|
| 20 | + { |
|
| 21 | + /** @var Log[] $logs */ |
|
| 22 | + $logs = LogSearchHelper::get($database, null) |
|
| 23 | + ->byAction($action) |
|
| 24 | + ->byObjectType('User') |
|
| 25 | + ->byObjectId($user->getId()) |
|
| 26 | + ->limit(1) |
|
| 27 | + ->fetch(); |
|
| 28 | 28 | |
| 29 | - if (count($logs) > 0) { |
|
| 30 | - return $logs[0]->getComment(); |
|
| 31 | - } |
|
| 29 | + if (count($logs) > 0) { |
|
| 30 | + return $logs[0]->getComment(); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - return null; |
|
| 34 | - } |
|
| 33 | + return null; |
|
| 34 | + } |
|
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -24,232 +24,232 @@ |
||
| 24 | 24 | |
| 25 | 25 | abstract class InternalPageBase extends PageBase |
| 26 | 26 | { |
| 27 | - use NavigationMenuAccessControl; |
|
| 28 | - |
|
| 29 | - /** @var ITypeAheadHelper */ |
|
| 30 | - private $typeAheadHelper; |
|
| 31 | - private ISecurityManager $securityManager; |
|
| 32 | - /** @var IBlacklistHelper */ |
|
| 33 | - private $blacklistHelper; |
|
| 34 | - |
|
| 35 | - private IDomainAccessManager $domainAccessManager; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @return ITypeAheadHelper |
|
| 39 | - */ |
|
| 40 | - public function getTypeAheadHelper() |
|
| 41 | - { |
|
| 42 | - return $this->typeAheadHelper; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ITypeAheadHelper $typeAheadHelper |
|
| 47 | - */ |
|
| 48 | - public function setTypeAheadHelper(ITypeAheadHelper $typeAheadHelper) |
|
| 49 | - { |
|
| 50 | - $this->typeAheadHelper = $typeAheadHelper; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Runs the page code |
|
| 55 | - * |
|
| 56 | - * @throws Exception |
|
| 57 | - * @category Security-Critical |
|
| 58 | - */ |
|
| 59 | - final public function execute() |
|
| 60 | - { |
|
| 61 | - if ($this->getRouteName() === null) { |
|
| 62 | - throw new Exception("Request is unrouted."); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if ($this->getSiteConfiguration() === null) { |
|
| 66 | - throw new Exception("Page has no configuration!"); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->setupPage(); |
|
| 70 | - |
|
| 71 | - $this->touchUserLastActive(); |
|
| 72 | - |
|
| 73 | - $currentUser = User::getCurrent($this->getDatabase()); |
|
| 74 | - |
|
| 75 | - // Hey, this is also a security barrier, in addition to the below. Separated out for readability. |
|
| 76 | - if (!$this->isProtectedPage()) { |
|
| 77 | - // This page is /not/ a protected page, as such we can just run it. |
|
| 78 | - $this->runPage(); |
|
| 79 | - |
|
| 80 | - return; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Security barrier. |
|
| 84 | - // |
|
| 85 | - // This code essentially doesn't care if the user is logged in or not, as the security manager hides all that |
|
| 86 | - // away for us |
|
| 87 | - $securityResult = $this->getSecurityManager()->allows(get_called_class(), $this->getRouteName(), $currentUser); |
|
| 88 | - if ($securityResult === ISecurityManager::ALLOWED) { |
|
| 89 | - // We're allowed to run the page, so let's run it. |
|
| 90 | - $this->runPage(); |
|
| 91 | - } |
|
| 92 | - else { |
|
| 93 | - $this->handleAccessDenied($securityResult); |
|
| 94 | - |
|
| 95 | - // Send the headers |
|
| 96 | - $this->sendResponseHeaders(); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Performs final tasks needed before rendering the page. |
|
| 102 | - */ |
|
| 103 | - final public function finalisePage() |
|
| 104 | - { |
|
| 105 | - parent::finalisePage(); |
|
| 106 | - |
|
| 107 | - $database = $this->getDatabase(); |
|
| 108 | - $currentUser = User::getCurrent($database); |
|
| 109 | - |
|
| 110 | - // Load in the badges for the navbar |
|
| 111 | - $this->setUpNavBarBadges($currentUser, $database); |
|
| 112 | - |
|
| 113 | - if ($this->barrierTest('viewSiteNotice', User::getCurrent($database), 'GlobalInfo')) { |
|
| 114 | - $siteNotice = SiteNotice::get($this->getDatabase()); |
|
| 115 | - $siteNoticeHash = sha1($siteNotice); |
|
| 116 | - |
|
| 117 | - if (WebRequest::testSiteNoticeCookieValue($siteNoticeHash)) { |
|
| 118 | - $this->assign('siteNoticeState', 'd-none'); |
|
| 119 | - } |
|
| 120 | - else { |
|
| 121 | - $this->assign('siteNoticeState', 'd-block'); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $this->assign('siteNoticeText', $siteNotice); |
|
| 125 | - $this->assign('siteNoticeVersion', $siteNoticeHash); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ($this->barrierTest('viewOnlineUsers', User::getCurrent($database), 'GlobalInfo')) { |
|
| 129 | - $sql = 'SELECT * FROM user WHERE lastactive > DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 5 MINUTE);'; |
|
| 130 | - $statement = $database->query($sql); |
|
| 131 | - $activeUsers = $statement->fetchAll(PDO::FETCH_CLASS, User::class); |
|
| 132 | - $this->assign('onlineusers', $activeUsers); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $this->setupNavMenuAccess($currentUser); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Configures whether the page respects roles or not. You probably want this to return true. |
|
| 140 | - * |
|
| 141 | - * Set to false for public pages. You probably want this to return true. |
|
| 142 | - * |
|
| 143 | - * This defaults to true unless you explicitly set it to false. Setting it to false means anybody can do anything |
|
| 144 | - * on this page, so you probably want this to return true. |
|
| 145 | - * |
|
| 146 | - * @return bool |
|
| 147 | - * @category Security-Critical |
|
| 148 | - */ |
|
| 149 | - protected function isProtectedPage() |
|
| 150 | - { |
|
| 151 | - return true; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - protected function handleAccessDenied($denyReason) |
|
| 155 | - { |
|
| 156 | - $currentUser = User::getCurrent($this->getDatabase()); |
|
| 157 | - |
|
| 158 | - // Not allowed to access this resource. |
|
| 159 | - // Firstly, let's check if we're even logged in. |
|
| 160 | - if ($currentUser->isCommunityUser()) { |
|
| 161 | - // Not logged in, redirect to login page |
|
| 162 | - WebRequest::setPostLoginRedirect(); |
|
| 163 | - $this->redirect("login"); |
|
| 164 | - |
|
| 165 | - return; |
|
| 166 | - } |
|
| 167 | - else { |
|
| 168 | - // Decide whether this was a rights failure, or an identification failure. |
|
| 169 | - |
|
| 170 | - if ($denyReason === ISecurityManager::ERROR_NOT_IDENTIFIED) { |
|
| 171 | - // Not identified |
|
| 172 | - throw new NotIdentifiedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
|
| 173 | - } |
|
| 174 | - elseif ($denyReason === ISecurityManager::ERROR_DENIED) { |
|
| 175 | - // Nope, plain old access denied |
|
| 176 | - throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
|
| 177 | - } |
|
| 178 | - else { |
|
| 179 | - throw new Exception('Unknown response from security manager.'); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Tests the security barrier for a specified action. |
|
| 186 | - * |
|
| 187 | - * Don't use within templates |
|
| 188 | - * |
|
| 189 | - * @param string $action |
|
| 190 | - * |
|
| 191 | - * @param User $user |
|
| 192 | - * @param null|string $pageName |
|
| 193 | - * |
|
| 194 | - * @return bool |
|
| 195 | - * @category Security-Critical |
|
| 196 | - */ |
|
| 197 | - final public function barrierTest($action, User $user, $pageName = null) |
|
| 198 | - { |
|
| 199 | - $page = get_called_class(); |
|
| 200 | - if ($pageName !== null) { |
|
| 201 | - $page = $pageName; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $securityResult = $this->getSecurityManager()->allows($page, $action, $user); |
|
| 205 | - |
|
| 206 | - return $securityResult === ISecurityManager::ALLOWED; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Updates the lastactive timestamp |
|
| 211 | - */ |
|
| 212 | - private function touchUserLastActive() |
|
| 213 | - { |
|
| 214 | - if (WebRequest::getSessionUserId() !== null) { |
|
| 215 | - $query = 'UPDATE user SET lastactive = CURRENT_TIMESTAMP() WHERE id = :id;'; |
|
| 216 | - $this->getDatabase()->prepare($query)->execute(array(":id" => WebRequest::getSessionUserId())); |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - public function getSecurityManager(): ISecurityManager |
|
| 221 | - { |
|
| 222 | - return $this->securityManager; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function setSecurityManager(ISecurityManager $securityManager) |
|
| 226 | - { |
|
| 227 | - $this->securityManager = $securityManager; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @return IBlacklistHelper |
|
| 232 | - */ |
|
| 233 | - public function getBlacklistHelper() |
|
| 234 | - { |
|
| 235 | - return $this->blacklistHelper; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @param IBlacklistHelper $blacklistHelper |
|
| 240 | - */ |
|
| 241 | - public function setBlacklistHelper(IBlacklistHelper $blacklistHelper) |
|
| 242 | - { |
|
| 243 | - $this->blacklistHelper = $blacklistHelper; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - public function getDomainAccessManager(): IDomainAccessManager |
|
| 247 | - { |
|
| 248 | - return $this->domainAccessManager; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - public function setDomainAccessManager(IDomainAccessManager $domainAccessManager): void |
|
| 252 | - { |
|
| 253 | - $this->domainAccessManager = $domainAccessManager; |
|
| 254 | - } |
|
| 27 | + use NavigationMenuAccessControl; |
|
| 28 | + |
|
| 29 | + /** @var ITypeAheadHelper */ |
|
| 30 | + private $typeAheadHelper; |
|
| 31 | + private ISecurityManager $securityManager; |
|
| 32 | + /** @var IBlacklistHelper */ |
|
| 33 | + private $blacklistHelper; |
|
| 34 | + |
|
| 35 | + private IDomainAccessManager $domainAccessManager; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @return ITypeAheadHelper |
|
| 39 | + */ |
|
| 40 | + public function getTypeAheadHelper() |
|
| 41 | + { |
|
| 42 | + return $this->typeAheadHelper; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ITypeAheadHelper $typeAheadHelper |
|
| 47 | + */ |
|
| 48 | + public function setTypeAheadHelper(ITypeAheadHelper $typeAheadHelper) |
|
| 49 | + { |
|
| 50 | + $this->typeAheadHelper = $typeAheadHelper; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Runs the page code |
|
| 55 | + * |
|
| 56 | + * @throws Exception |
|
| 57 | + * @category Security-Critical |
|
| 58 | + */ |
|
| 59 | + final public function execute() |
|
| 60 | + { |
|
| 61 | + if ($this->getRouteName() === null) { |
|
| 62 | + throw new Exception("Request is unrouted."); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if ($this->getSiteConfiguration() === null) { |
|
| 66 | + throw new Exception("Page has no configuration!"); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->setupPage(); |
|
| 70 | + |
|
| 71 | + $this->touchUserLastActive(); |
|
| 72 | + |
|
| 73 | + $currentUser = User::getCurrent($this->getDatabase()); |
|
| 74 | + |
|
| 75 | + // Hey, this is also a security barrier, in addition to the below. Separated out for readability. |
|
| 76 | + if (!$this->isProtectedPage()) { |
|
| 77 | + // This page is /not/ a protected page, as such we can just run it. |
|
| 78 | + $this->runPage(); |
|
| 79 | + |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Security barrier. |
|
| 84 | + // |
|
| 85 | + // This code essentially doesn't care if the user is logged in or not, as the security manager hides all that |
|
| 86 | + // away for us |
|
| 87 | + $securityResult = $this->getSecurityManager()->allows(get_called_class(), $this->getRouteName(), $currentUser); |
|
| 88 | + if ($securityResult === ISecurityManager::ALLOWED) { |
|
| 89 | + // We're allowed to run the page, so let's run it. |
|
| 90 | + $this->runPage(); |
|
| 91 | + } |
|
| 92 | + else { |
|
| 93 | + $this->handleAccessDenied($securityResult); |
|
| 94 | + |
|
| 95 | + // Send the headers |
|
| 96 | + $this->sendResponseHeaders(); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Performs final tasks needed before rendering the page. |
|
| 102 | + */ |
|
| 103 | + final public function finalisePage() |
|
| 104 | + { |
|
| 105 | + parent::finalisePage(); |
|
| 106 | + |
|
| 107 | + $database = $this->getDatabase(); |
|
| 108 | + $currentUser = User::getCurrent($database); |
|
| 109 | + |
|
| 110 | + // Load in the badges for the navbar |
|
| 111 | + $this->setUpNavBarBadges($currentUser, $database); |
|
| 112 | + |
|
| 113 | + if ($this->barrierTest('viewSiteNotice', User::getCurrent($database), 'GlobalInfo')) { |
|
| 114 | + $siteNotice = SiteNotice::get($this->getDatabase()); |
|
| 115 | + $siteNoticeHash = sha1($siteNotice); |
|
| 116 | + |
|
| 117 | + if (WebRequest::testSiteNoticeCookieValue($siteNoticeHash)) { |
|
| 118 | + $this->assign('siteNoticeState', 'd-none'); |
|
| 119 | + } |
|
| 120 | + else { |
|
| 121 | + $this->assign('siteNoticeState', 'd-block'); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $this->assign('siteNoticeText', $siteNotice); |
|
| 125 | + $this->assign('siteNoticeVersion', $siteNoticeHash); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ($this->barrierTest('viewOnlineUsers', User::getCurrent($database), 'GlobalInfo')) { |
|
| 129 | + $sql = 'SELECT * FROM user WHERE lastactive > DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 5 MINUTE);'; |
|
| 130 | + $statement = $database->query($sql); |
|
| 131 | + $activeUsers = $statement->fetchAll(PDO::FETCH_CLASS, User::class); |
|
| 132 | + $this->assign('onlineusers', $activeUsers); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $this->setupNavMenuAccess($currentUser); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Configures whether the page respects roles or not. You probably want this to return true. |
|
| 140 | + * |
|
| 141 | + * Set to false for public pages. You probably want this to return true. |
|
| 142 | + * |
|
| 143 | + * This defaults to true unless you explicitly set it to false. Setting it to false means anybody can do anything |
|
| 144 | + * on this page, so you probably want this to return true. |
|
| 145 | + * |
|
| 146 | + * @return bool |
|
| 147 | + * @category Security-Critical |
|
| 148 | + */ |
|
| 149 | + protected function isProtectedPage() |
|
| 150 | + { |
|
| 151 | + return true; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + protected function handleAccessDenied($denyReason) |
|
| 155 | + { |
|
| 156 | + $currentUser = User::getCurrent($this->getDatabase()); |
|
| 157 | + |
|
| 158 | + // Not allowed to access this resource. |
|
| 159 | + // Firstly, let's check if we're even logged in. |
|
| 160 | + if ($currentUser->isCommunityUser()) { |
|
| 161 | + // Not logged in, redirect to login page |
|
| 162 | + WebRequest::setPostLoginRedirect(); |
|
| 163 | + $this->redirect("login"); |
|
| 164 | + |
|
| 165 | + return; |
|
| 166 | + } |
|
| 167 | + else { |
|
| 168 | + // Decide whether this was a rights failure, or an identification failure. |
|
| 169 | + |
|
| 170 | + if ($denyReason === ISecurityManager::ERROR_NOT_IDENTIFIED) { |
|
| 171 | + // Not identified |
|
| 172 | + throw new NotIdentifiedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
|
| 173 | + } |
|
| 174 | + elseif ($denyReason === ISecurityManager::ERROR_DENIED) { |
|
| 175 | + // Nope, plain old access denied |
|
| 176 | + throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
|
| 177 | + } |
|
| 178 | + else { |
|
| 179 | + throw new Exception('Unknown response from security manager.'); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Tests the security barrier for a specified action. |
|
| 186 | + * |
|
| 187 | + * Don't use within templates |
|
| 188 | + * |
|
| 189 | + * @param string $action |
|
| 190 | + * |
|
| 191 | + * @param User $user |
|
| 192 | + * @param null|string $pageName |
|
| 193 | + * |
|
| 194 | + * @return bool |
|
| 195 | + * @category Security-Critical |
|
| 196 | + */ |
|
| 197 | + final public function barrierTest($action, User $user, $pageName = null) |
|
| 198 | + { |
|
| 199 | + $page = get_called_class(); |
|
| 200 | + if ($pageName !== null) { |
|
| 201 | + $page = $pageName; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $securityResult = $this->getSecurityManager()->allows($page, $action, $user); |
|
| 205 | + |
|
| 206 | + return $securityResult === ISecurityManager::ALLOWED; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Updates the lastactive timestamp |
|
| 211 | + */ |
|
| 212 | + private function touchUserLastActive() |
|
| 213 | + { |
|
| 214 | + if (WebRequest::getSessionUserId() !== null) { |
|
| 215 | + $query = 'UPDATE user SET lastactive = CURRENT_TIMESTAMP() WHERE id = :id;'; |
|
| 216 | + $this->getDatabase()->prepare($query)->execute(array(":id" => WebRequest::getSessionUserId())); |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + public function getSecurityManager(): ISecurityManager |
|
| 221 | + { |
|
| 222 | + return $this->securityManager; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function setSecurityManager(ISecurityManager $securityManager) |
|
| 226 | + { |
|
| 227 | + $this->securityManager = $securityManager; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @return IBlacklistHelper |
|
| 232 | + */ |
|
| 233 | + public function getBlacklistHelper() |
|
| 234 | + { |
|
| 235 | + return $this->blacklistHelper; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @param IBlacklistHelper $blacklistHelper |
|
| 240 | + */ |
|
| 241 | + public function setBlacklistHelper(IBlacklistHelper $blacklistHelper) |
|
| 242 | + { |
|
| 243 | + $this->blacklistHelper = $blacklistHelper; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + public function getDomainAccessManager(): IDomainAccessManager |
|
| 247 | + { |
|
| 248 | + return $this->domainAccessManager; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + public function setDomainAccessManager(IDomainAccessManager $domainAccessManager): void |
|
| 252 | + { |
|
| 253 | + $this->domainAccessManager = $domainAccessManager; |
|
| 254 | + } |
|
| 255 | 255 | } |
@@ -88,8 +88,7 @@ discard block |
||
| 88 | 88 | if ($securityResult === ISecurityManager::ALLOWED) { |
| 89 | 89 | // We're allowed to run the page, so let's run it. |
| 90 | 90 | $this->runPage(); |
| 91 | - } |
|
| 92 | - else { |
|
| 91 | + } else { |
|
| 93 | 92 | $this->handleAccessDenied($securityResult); |
| 94 | 93 | |
| 95 | 94 | // Send the headers |
@@ -116,8 +115,7 @@ discard block |
||
| 116 | 115 | |
| 117 | 116 | if (WebRequest::testSiteNoticeCookieValue($siteNoticeHash)) { |
| 118 | 117 | $this->assign('siteNoticeState', 'd-none'); |
| 119 | - } |
|
| 120 | - else { |
|
| 118 | + } else { |
|
| 121 | 119 | $this->assign('siteNoticeState', 'd-block'); |
| 122 | 120 | } |
| 123 | 121 | |
@@ -163,19 +161,16 @@ discard block |
||
| 163 | 161 | $this->redirect("login"); |
| 164 | 162 | |
| 165 | 163 | return; |
| 166 | - } |
|
| 167 | - else { |
|
| 164 | + } else { |
|
| 168 | 165 | // Decide whether this was a rights failure, or an identification failure. |
| 169 | 166 | |
| 170 | 167 | if ($denyReason === ISecurityManager::ERROR_NOT_IDENTIFIED) { |
| 171 | 168 | // Not identified |
| 172 | 169 | throw new NotIdentifiedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
| 173 | - } |
|
| 174 | - elseif ($denyReason === ISecurityManager::ERROR_DENIED) { |
|
| 170 | + } elseif ($denyReason === ISecurityManager::ERROR_DENIED) { |
|
| 175 | 171 | // Nope, plain old access denied |
| 176 | 172 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
| 177 | - } |
|
| 178 | - else { |
|
| 173 | + } else { |
|
| 179 | 174 | throw new Exception('Unknown response from security manager.'); |
| 180 | 175 | } |
| 181 | 176 | } |
@@ -13,23 +13,23 @@ |
||
| 13 | 13 | |
| 14 | 14 | abstract class TextApiPageBase extends ApiPageBase implements IRoutedTask |
| 15 | 15 | { |
| 16 | - final protected function main() |
|
| 17 | - { |
|
| 18 | - if (headers_sent()) { |
|
| 19 | - throw new ApiException('Headers have already been sent - this indicates a bug in the application!'); |
|
| 20 | - } |
|
| 16 | + final protected function main() |
|
| 17 | + { |
|
| 18 | + if (headers_sent()) { |
|
| 19 | + throw new ApiException('Headers have already been sent - this indicates a bug in the application!'); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - try { |
|
| 23 | - $responseData = $this->runApiPage(); |
|
| 24 | - } |
|
| 25 | - catch (ApiException $ex) { |
|
| 26 | - $responseData = $ex->getMessage(); |
|
| 27 | - } |
|
| 22 | + try { |
|
| 23 | + $responseData = $this->runApiPage(); |
|
| 24 | + } |
|
| 25 | + catch (ApiException $ex) { |
|
| 26 | + $responseData = $ex->getMessage(); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - header('Content-Type: text/plain'); |
|
| 29 | + header('Content-Type: text/plain'); |
|
| 30 | 30 | |
| 31 | - ob_end_clean(); |
|
| 32 | - print($responseData); |
|
| 33 | - ob_start(); |
|
| 34 | - } |
|
| 31 | + ob_end_clean(); |
|
| 32 | + print($responseData); |
|
| 33 | + ob_start(); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -18,14 +18,14 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface IIdentificationVerifier |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 23 | - * |
|
| 24 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 25 | - * |
|
| 26 | - * @return bool |
|
| 27 | - * @throws EnvironmentException |
|
| 28 | - * @category Security-Critical |
|
| 29 | - */ |
|
| 30 | - public function isUserIdentified(string $onWikiName): bool; |
|
| 21 | + /** |
|
| 22 | + * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 23 | + * |
|
| 24 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 25 | + * |
|
| 26 | + * @return bool |
|
| 27 | + * @throws EnvironmentException |
|
| 28 | + * @category Security-Critical |
|
| 29 | + */ |
|
| 30 | + public function isUserIdentified(string $onWikiName): bool; |
|
| 31 | 31 | } |
| 32 | 32 | \ No newline at end of file |
@@ -22,47 +22,47 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | class CountAction extends XmlApiPageBase implements IXmlApiAction |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * The target user |
|
| 27 | - * @var User $user |
|
| 28 | - */ |
|
| 29 | - private $user; |
|
| 25 | + /** |
|
| 26 | + * The target user |
|
| 27 | + * @var User $user |
|
| 28 | + */ |
|
| 29 | + private $user; |
|
| 30 | 30 | |
| 31 | - public function executeApiAction(DOMElement $apiDocument) |
|
| 32 | - { |
|
| 33 | - $username = WebRequest::getString('user'); |
|
| 34 | - if ($username === null) { |
|
| 35 | - throw new ApiException("Please specify a username"); |
|
| 36 | - } |
|
| 31 | + public function executeApiAction(DOMElement $apiDocument) |
|
| 32 | + { |
|
| 33 | + $username = WebRequest::getString('user'); |
|
| 34 | + if ($username === null) { |
|
| 35 | + throw new ApiException("Please specify a username"); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - $userElement = $this->document->createElement("user"); |
|
| 39 | - $userElement->setAttribute("name", $username); |
|
| 40 | - $apiDocument->appendChild($userElement); |
|
| 38 | + $userElement = $this->document->createElement("user"); |
|
| 39 | + $userElement->setAttribute("name", $username); |
|
| 40 | + $apiDocument->appendChild($userElement); |
|
| 41 | 41 | |
| 42 | - $user = User::getByUsername($username, $this->getDatabase()); |
|
| 42 | + $user = User::getByUsername($username, $this->getDatabase()); |
|
| 43 | 43 | |
| 44 | - if ($user === false) { |
|
| 45 | - $userElement->setAttribute("missing", "true"); |
|
| 44 | + if ($user === false) { |
|
| 45 | + $userElement->setAttribute("missing", "true"); |
|
| 46 | 46 | |
| 47 | - return $apiDocument; |
|
| 48 | - } |
|
| 47 | + return $apiDocument; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - $this->user = $user; |
|
| 50 | + $this->user = $user; |
|
| 51 | 51 | |
| 52 | - $userElement->setAttribute("level", $this->user->getStatus()); |
|
| 53 | - $userElement->setAttribute("created", $this->getAccountsCreated()); |
|
| 52 | + $userElement->setAttribute("level", $this->user->getStatus()); |
|
| 53 | + $userElement->setAttribute("created", $this->getAccountsCreated()); |
|
| 54 | 54 | |
| 55 | - $userElement->setAttribute("today", $this->getToday()); |
|
| 55 | + $userElement->setAttribute("today", $this->getToday()); |
|
| 56 | 56 | |
| 57 | - // Let the IRC bot handle the result of this. |
|
| 58 | - $this->fetchAdminData($userElement); |
|
| 57 | + // Let the IRC bot handle the result of this. |
|
| 58 | + $this->fetchAdminData($userElement); |
|
| 59 | 59 | |
| 60 | - return $apiDocument; |
|
| 61 | - } |
|
| 60 | + return $apiDocument; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - private function getAccountsCreated() |
|
| 64 | - { |
|
| 65 | - $query = <<<QUERY |
|
| 63 | + private function getAccountsCreated() |
|
| 64 | + { |
|
| 65 | + $query = <<<QUERY |
|
| 66 | 66 | SELECT COUNT(*) AS count |
| 67 | 67 | FROM log |
| 68 | 68 | LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action |
@@ -73,17 +73,17 @@ discard block |
||
| 73 | 73 | AND user.username = :username; |
| 74 | 74 | QUERY; |
| 75 | 75 | |
| 76 | - $statement = $this->getDatabase()->prepare($query); |
|
| 77 | - $statement->execute(array(":username" => $this->user->getUsername(), ":created" => EmailTemplate::ACTION_CREATED)); |
|
| 78 | - $result = $statement->fetchColumn(); |
|
| 79 | - $statement->closeCursor(); |
|
| 76 | + $statement = $this->getDatabase()->prepare($query); |
|
| 77 | + $statement->execute(array(":username" => $this->user->getUsername(), ":created" => EmailTemplate::ACTION_CREATED)); |
|
| 78 | + $result = $statement->fetchColumn(); |
|
| 79 | + $statement->closeCursor(); |
|
| 80 | 80 | |
| 81 | - return $result; |
|
| 82 | - } |
|
| 81 | + return $result; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - private function getToday() |
|
| 85 | - { |
|
| 86 | - $query = <<<QUERY |
|
| 84 | + private function getToday() |
|
| 85 | + { |
|
| 86 | + $query = <<<QUERY |
|
| 87 | 87 | SELECT |
| 88 | 88 | COUNT(*) AS count |
| 89 | 89 | FROM log |
@@ -95,75 +95,75 @@ discard block |
||
| 95 | 95 | AND user.username = :username; |
| 96 | 96 | QUERY; |
| 97 | 97 | |
| 98 | - $statement = $this->getDatabase()->prepare($query); |
|
| 99 | - $statement->bindValue(":username", $this->user->getUsername()); |
|
| 100 | - $statement->bindValue(":date", date('Y-m-d') . "%"); |
|
| 101 | - $statement->bindValue(":created", EmailTemplate::ACTION_CREATED); |
|
| 102 | - $statement->execute(); |
|
| 103 | - $today = $statement->fetchColumn(); |
|
| 104 | - $statement->closeCursor(); |
|
| 98 | + $statement = $this->getDatabase()->prepare($query); |
|
| 99 | + $statement->bindValue(":username", $this->user->getUsername()); |
|
| 100 | + $statement->bindValue(":date", date('Y-m-d') . "%"); |
|
| 101 | + $statement->bindValue(":created", EmailTemplate::ACTION_CREATED); |
|
| 102 | + $statement->execute(); |
|
| 103 | + $today = $statement->fetchColumn(); |
|
| 104 | + $statement->closeCursor(); |
|
| 105 | 105 | |
| 106 | - return $today; |
|
| 107 | - } |
|
| 106 | + return $today; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - private function fetchAdminData(DOMElement $userElement) |
|
| 110 | - { |
|
| 111 | - $query = "SELECT COUNT(*) AS count FROM log WHERE log.user = :userid AND log.action = :action;"; |
|
| 109 | + private function fetchAdminData(DOMElement $userElement) |
|
| 110 | + { |
|
| 111 | + $query = "SELECT COUNT(*) AS count FROM log WHERE log.user = :userid AND log.action = :action;"; |
|
| 112 | 112 | |
| 113 | - $statement = $this->getDatabase()->prepare($query); |
|
| 114 | - $statement->bindValue(":userid", $this->user->getId()); |
|
| 113 | + $statement = $this->getDatabase()->prepare($query); |
|
| 114 | + $statement->bindValue(":userid", $this->user->getId()); |
|
| 115 | 115 | |
| 116 | - // Each entry is in the form [ database string, attribute name ] |
|
| 117 | - // and it happens to be that the attribute is just the lower case form of the database value |
|
| 118 | - $actions = [ |
|
| 119 | - ['Promoted', 'promoted'], |
|
| 120 | - ['Approved', 'approved'], |
|
| 121 | - ['Demoted', 'demoted'], |
|
| 122 | - ['Renamed', 'renamed'], |
|
| 123 | - ['Edited', 'edited'], |
|
| 124 | - ['Prefchange', 'prefchange'], |
|
| 125 | - ['DeactivatedUser', 'deactivateduser'], |
|
| 126 | - ]; |
|
| 127 | - foreach ($actions as $action) { |
|
| 128 | - $dbValue = $action[0]; |
|
| 129 | - $attributeName = $action[1]; |
|
| 116 | + // Each entry is in the form [ database string, attribute name ] |
|
| 117 | + // and it happens to be that the attribute is just the lower case form of the database value |
|
| 118 | + $actions = [ |
|
| 119 | + ['Promoted', 'promoted'], |
|
| 120 | + ['Approved', 'approved'], |
|
| 121 | + ['Demoted', 'demoted'], |
|
| 122 | + ['Renamed', 'renamed'], |
|
| 123 | + ['Edited', 'edited'], |
|
| 124 | + ['Prefchange', 'prefchange'], |
|
| 125 | + ['DeactivatedUser', 'deactivateduser'], |
|
| 126 | + ]; |
|
| 127 | + foreach ($actions as $action) { |
|
| 128 | + $dbValue = $action[0]; |
|
| 129 | + $attributeName = $action[1]; |
|
| 130 | 130 | |
| 131 | - $statement->bindValue(":action", $dbValue); |
|
| 132 | - $statement->execute(); |
|
| 133 | - $attributeValue = $statement->fetchColumn(); |
|
| 134 | - $userElement->setAttribute($attributeName, $attributeValue); |
|
| 135 | - $statement->closeCursor(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Combine all three actions affecting Welcome templates into one count. |
|
| 139 | - $combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
| 131 | + $statement->bindValue(":action", $dbValue); |
|
| 132 | + $statement->execute(); |
|
| 133 | + $attributeValue = $statement->fetchColumn(); |
|
| 134 | + $userElement->setAttribute($attributeName, $attributeValue); |
|
| 135 | + $statement->closeCursor(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Combine all three actions affecting Welcome templates into one count. |
|
| 139 | + $combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
| 140 | 140 | SELECT |
| 141 | 141 | COUNT(*) AS count |
| 142 | 142 | FROM log |
| 143 | 143 | WHERE log.user = :userid |
| 144 | 144 | AND log.action IN ('CreatedTemplate', 'EditedTemplate', 'DeletedTemplate'); |
| 145 | 145 | SQL |
| 146 | - ); |
|
| 146 | + ); |
|
| 147 | 147 | |
| 148 | - $combinedquery->bindValue(":userid", $this->user->getId()); |
|
| 149 | - $combinedquery->execute(); |
|
| 150 | - $dtc = $combinedquery->fetchColumn(); |
|
| 151 | - $userElement->setAttribute("welctempchange", $dtc); |
|
| 152 | - $combinedquery->closeCursor(); |
|
| 148 | + $combinedquery->bindValue(":userid", $this->user->getId()); |
|
| 149 | + $combinedquery->execute(); |
|
| 150 | + $dtc = $combinedquery->fetchColumn(); |
|
| 151 | + $userElement->setAttribute("welctempchange", $dtc); |
|
| 152 | + $combinedquery->closeCursor(); |
|
| 153 | 153 | |
| 154 | - // Combine both actions affecting Email templates into one count. |
|
| 155 | - $combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
| 154 | + // Combine both actions affecting Email templates into one count. |
|
| 155 | + $combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
| 156 | 156 | SELECT COUNT(*) AS count |
| 157 | 157 | FROM log |
| 158 | 158 | WHERE log.user = :userid |
| 159 | 159 | AND log.action IN ('CreatedEmail', 'EditedEmail'); |
| 160 | 160 | SQL |
| 161 | - ); |
|
| 162 | - |
|
| 163 | - $combinedquery->bindValue(":userid", $this->user->getId()); |
|
| 164 | - $combinedquery->execute(); |
|
| 165 | - $cec = $combinedquery->fetchColumn(); |
|
| 166 | - $userElement->setAttribute("emailtempchange", $cec); |
|
| 167 | - $combinedquery->closeCursor(); |
|
| 168 | - } |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + $combinedquery->bindValue(":userid", $this->user->getId()); |
|
| 164 | + $combinedquery->execute(); |
|
| 165 | + $cec = $combinedquery->fetchColumn(); |
|
| 166 | + $userElement->setAttribute("emailtempchange", $cec); |
|
| 167 | + $combinedquery->closeCursor(); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -18,57 +18,57 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class MetricsAction extends TextApiPageBase implements IApiAction |
| 20 | 20 | { |
| 21 | - private array $metrics = []; |
|
| 21 | + private array $metrics = []; |
|
| 22 | 22 | |
| 23 | - private function defineMetric(string $name, string $help, string $type = 'gauge'): void |
|
| 24 | - { |
|
| 25 | - $this->metrics[$name] = ['help' => $help, 'type' => $type, 'values' => []]; |
|
| 26 | - } |
|
| 23 | + private function defineMetric(string $name, string $help, string $type = 'gauge'): void |
|
| 24 | + { |
|
| 25 | + $this->metrics[$name] = ['help' => $help, 'type' => $type, 'values' => []]; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - private function setMetric(string $name, array $labels = [], int $value = 0): void |
|
| 29 | - { |
|
| 30 | - $calculatedLabel = ''; |
|
| 28 | + private function setMetric(string $name, array $labels = [], int $value = 0): void |
|
| 29 | + { |
|
| 30 | + $calculatedLabel = ''; |
|
| 31 | 31 | |
| 32 | - if (count($labels) > 0) { |
|
| 33 | - ksort($labels); |
|
| 32 | + if (count($labels) > 0) { |
|
| 33 | + ksort($labels); |
|
| 34 | 34 | |
| 35 | - $labelData = []; |
|
| 36 | - foreach ($labels as $label => $labelValue) { |
|
| 37 | - $labelData[] = $label . '="' . $labelValue . '"'; |
|
| 38 | - } |
|
| 35 | + $labelData = []; |
|
| 36 | + foreach ($labels as $label => $labelValue) { |
|
| 37 | + $labelData[] = $label . '="' . $labelValue . '"'; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - $calculatedLabel = '{' . implode(',', $labelData) . '}'; |
|
| 41 | - } |
|
| 40 | + $calculatedLabel = '{' . implode(',', $labelData) . '}'; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $this->metrics[$name]['values'][$calculatedLabel] = $value; |
|
| 44 | - } |
|
| 43 | + $this->metrics[$name]['values'][$calculatedLabel] = $value; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function runApiPage(): string |
|
| 47 | - { |
|
| 48 | - $this->defineMetric('acc_users', 'Number of users'); |
|
| 49 | - $statement = $this->getDatabase()->query('SELECT status, COUNT(*) AS count FROM user GROUP BY status;'); |
|
| 46 | + public function runApiPage(): string |
|
| 47 | + { |
|
| 48 | + $this->defineMetric('acc_users', 'Number of users'); |
|
| 49 | + $statement = $this->getDatabase()->query('SELECT status, COUNT(*) AS count FROM user GROUP BY status;'); |
|
| 50 | 50 | |
| 51 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 52 | - $this->setMetric('acc_users', ['status' => $row['status']], $row['count']); |
|
| 53 | - } |
|
| 54 | - $statement->closeCursor(); |
|
| 51 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 52 | + $this->setMetric('acc_users', ['status' => $row['status']], $row['count']); |
|
| 53 | + } |
|
| 54 | + $statement->closeCursor(); |
|
| 55 | 55 | |
| 56 | - $this->defineMetric('acc_active_domain_users', 'Number of active users in each domain'); |
|
| 57 | - $statement = $this->getDatabase()->query(' |
|
| 56 | + $this->defineMetric('acc_active_domain_users', 'Number of active users in each domain'); |
|
| 57 | + $statement = $this->getDatabase()->query(' |
|
| 58 | 58 | SELECT d.shortname, COUNT(1) AS count FROM userdomain ud |
| 59 | 59 | INNER JOIN user u ON ud.user = u.id |
| 60 | 60 | INNER JOIN domain d on ud.domain = d.id |
| 61 | 61 | WHERE u.status = \'Active\' |
| 62 | 62 | GROUP BY d.shortname;'); |
| 63 | 63 | |
| 64 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 65 | - $this->setMetric('acc_active_domain_users', ['domain' => $row['shortname']], $row['count']); |
|
| 66 | - } |
|
| 67 | - $statement->closeCursor(); |
|
| 64 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 65 | + $this->setMetric('acc_active_domain_users', ['domain' => $row['shortname']], $row['count']); |
|
| 66 | + } |
|
| 67 | + $statement->closeCursor(); |
|
| 68 | 68 | |
| 69 | 69 | |
| 70 | - $this->defineMetric('acc_active_domain_roles', 'Number of active users in each role'); |
|
| 71 | - $statement = $this->getDatabase()->query(' |
|
| 70 | + $this->defineMetric('acc_active_domain_roles', 'Number of active users in each role'); |
|
| 71 | + $statement = $this->getDatabase()->query(' |
|
| 72 | 72 | SELECT coalesce(d.shortname, \'\') AS domain, ur.role, COUNT(1) AS count |
| 73 | 73 | FROM userrole ur |
| 74 | 74 | INNER JOIN user u ON ur.user = u.id |
@@ -76,53 +76,53 @@ discard block |
||
| 76 | 76 | WHERE u.status = \'Active\' AND ur.role <> \'user\' |
| 77 | 77 | GROUP BY d.shortname, ur.role;'); |
| 78 | 78 | |
| 79 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 80 | - $this->setMetric('acc_active_domain_roles', ['domain' => $row['domain'], 'role' => $row['role']], $row['count']); |
|
| 81 | - } |
|
| 82 | - $statement->closeCursor(); |
|
| 79 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 80 | + $this->setMetric('acc_active_domain_roles', ['domain' => $row['domain'], 'role' => $row['role']], $row['count']); |
|
| 81 | + } |
|
| 82 | + $statement->closeCursor(); |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - $this->defineMetric('acc_active_domain_bans', 'Number of active bans in each domain'); |
|
| 86 | - $statement = $this->getDatabase()->query(' |
|
| 85 | + $this->defineMetric('acc_active_domain_bans', 'Number of active bans in each domain'); |
|
| 86 | + $statement = $this->getDatabase()->query(' |
|
| 87 | 87 | SELECT coalesce(d.shortname, \'\') AS domain, COUNT(1) AS count |
| 88 | 88 | FROM ban b LEFT JOIN domain d ON b.domain = d.id |
| 89 | 89 | WHERE (b.duration > UNIX_TIMESTAMP() OR b.duration is null) AND b.active = 1 |
| 90 | 90 | GROUP BY d.shortname;'); |
| 91 | 91 | |
| 92 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 93 | - $this->setMetric('acc_active_domain_bans', ['domain' => $row['domain'], 'role' => $row['role']], $row['count']); |
|
| 94 | - } |
|
| 95 | - $statement->closeCursor(); |
|
| 92 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 93 | + $this->setMetric('acc_active_domain_bans', ['domain' => $row['domain'], 'role' => $row['role']], $row['count']); |
|
| 94 | + } |
|
| 95 | + $statement->closeCursor(); |
|
| 96 | 96 | |
| 97 | 97 | |
| 98 | - $this->defineMetric('acc_queued_requests', 'Number of requests in each queue'); |
|
| 99 | - $statement = $this->getDatabase()->query(' |
|
| 98 | + $this->defineMetric('acc_queued_requests', 'Number of requests in each queue'); |
|
| 99 | + $statement = $this->getDatabase()->query(' |
|
| 100 | 100 | SELECT r.status, d.shortname, rq.header, COUNT(1) as count FROM request r |
| 101 | 101 | INNER JOIN domain d on r.domain = d.id |
| 102 | 102 | LEFT JOIN waca.requestqueue rq ON r.queue = rq.id |
| 103 | 103 | WHERE r.status <> \'Closed\' AND r.emailconfirm = \'Confirmed\' |
| 104 | 104 | GROUP BY r.status, d.shortname, rq.header;'); |
| 105 | 105 | |
| 106 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 107 | - $this->setMetric('acc_queued_requests', ['status' => $row['status'], 'shortname' => $row['shortname'], 'queue' => $row['header']], $row['count']); |
|
| 108 | - } |
|
| 109 | - $statement->closeCursor(); |
|
| 106 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 107 | + $this->setMetric('acc_queued_requests', ['status' => $row['status'], 'shortname' => $row['shortname'], 'queue' => $row['header']], $row['count']); |
|
| 108 | + } |
|
| 109 | + $statement->closeCursor(); |
|
| 110 | 110 | |
| 111 | - return $this->writeMetrics(); |
|
| 112 | - } |
|
| 111 | + return $this->writeMetrics(); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - private function writeMetrics() : string |
|
| 115 | - { |
|
| 116 | - $data = ''; |
|
| 114 | + private function writeMetrics() : string |
|
| 115 | + { |
|
| 116 | + $data = ''; |
|
| 117 | 117 | |
| 118 | - foreach ($this->metrics as $name => $metricData) { |
|
| 119 | - $data .= "# HELP {$name} {$metricData['help']}\n"; |
|
| 120 | - $data .= "# TYPE {$name} {$metricData['type']}\n"; |
|
| 121 | - foreach ($metricData['values'] as $label => $value) { |
|
| 122 | - $data .= "{$name}{$label} {$value}\n"; |
|
| 123 | - } |
|
| 124 | - } |
|
| 118 | + foreach ($this->metrics as $name => $metricData) { |
|
| 119 | + $data .= "# HELP {$name} {$metricData['help']}\n"; |
|
| 120 | + $data .= "# TYPE {$name} {$metricData['type']}\n"; |
|
| 121 | + foreach ($metricData['values'] as $label => $value) { |
|
| 122 | + $data .= "{$name}{$label} {$value}\n"; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - return $data; |
|
| 127 | - } |
|
| 126 | + return $data; |
|
| 127 | + } |
|
| 128 | 128 | } |