@@ -23,229 +23,229 @@ |
||
23 | 23 | |
24 | 24 | class PageViewRequest extends InternalPageBase |
25 | 25 | { |
26 | - use RequestData; |
|
27 | - const PRIVATE_DATA_BARRIER = 'privateData'; |
|
28 | - const SET_BAN_BARRIER = 'setBan'; |
|
29 | - const STATUS_SYMBOL_OPEN = '☐'; |
|
30 | - const STATUS_SYMBOL_ACCEPTED = '☑'; |
|
31 | - const STATUS_SYMBOL_REJECTED = '☒'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Main function for this page, when no specific actions are called. |
|
35 | - * @throws ApplicationLogicException |
|
36 | - */ |
|
37 | - protected function main() |
|
38 | - { |
|
39 | - // set up csrf protection |
|
40 | - $this->assignCSRFToken(); |
|
41 | - |
|
42 | - // get some useful objects |
|
43 | - $database = $this->getDatabase(); |
|
44 | - $request = $this->getRequest($database, WebRequest::getInt('id')); |
|
45 | - $config = $this->getSiteConfiguration(); |
|
46 | - $currentUser = User::getCurrent($database); |
|
47 | - |
|
48 | - // Test we should be able to look at this request |
|
49 | - if ($config->getEmailConfirmationEnabled()) { |
|
50 | - if ($request->getEmailConfirm() !== 'Confirmed') { |
|
51 | - // Not allowed to look at this yet. |
|
52 | - throw new ApplicationLogicException('The email address has not yet been confirmed for this request.'); |
|
53 | - } |
|
54 | - } |
|
55 | - |
|
56 | - $this->setupBasicData($request, $config); |
|
57 | - |
|
58 | - $this->setupUsernameData($request); |
|
59 | - |
|
60 | - $this->setupTitle($request); |
|
61 | - |
|
62 | - $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
63 | - $this->setupGeneralData($database); |
|
64 | - |
|
65 | - $this->assign('requestDataCleared', false); |
|
66 | - if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
67 | - $this->assign('requestDataCleared', true); |
|
68 | - } |
|
69 | - |
|
70 | - $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
71 | - |
|
72 | - $this->setupLogData($request, $database); |
|
73 | - |
|
74 | - if ($allowedPrivateData) { |
|
75 | - $this->setTemplate('view-request/main-with-data.tpl'); |
|
76 | - $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
77 | - |
|
78 | - if ($currentUser->isCheckuser()) { |
|
79 | - $this->setTemplate('view-request/main-with-checkuser-data.tpl'); |
|
80 | - $this->setupCheckUserData($request); |
|
81 | - } |
|
82 | - } |
|
83 | - else { |
|
84 | - $this->setTemplate('view-request/main.tpl'); |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param Request $request |
|
90 | - */ |
|
91 | - protected function setupTitle(Request $request) |
|
92 | - { |
|
93 | - $statusSymbol = self::STATUS_SYMBOL_OPEN; |
|
94 | - if ($request->getStatus() === 'Closed') { |
|
95 | - if ($request->getWasCreated()) { |
|
96 | - $statusSymbol = self::STATUS_SYMBOL_ACCEPTED; |
|
97 | - } |
|
98 | - else { |
|
99 | - $statusSymbol = self::STATUS_SYMBOL_REJECTED; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Sets up data unrelated to the request, such as the email template information |
|
108 | - * |
|
109 | - * @param PdoDatabase $database |
|
110 | - */ |
|
111 | - protected function setupGeneralData(PdoDatabase $database) |
|
112 | - { |
|
113 | - $config = $this->getSiteConfiguration(); |
|
114 | - |
|
115 | - $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #'); |
|
116 | - |
|
117 | - $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
118 | - |
|
119 | - $this->assign('requestStates', $config->getRequestStates()); |
|
120 | - |
|
121 | - /** @var EmailTemplate $createdTemplate */ |
|
122 | - $createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database); |
|
123 | - |
|
124 | - $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != ''); |
|
125 | - $this->assign('createdJsQuestion', $createdTemplate->getJsquestion()); |
|
126 | - $this->assign('createdId', $createdTemplate->getId()); |
|
127 | - $this->assign('createdName', $createdTemplate->getName()); |
|
128 | - |
|
129 | - $createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database); |
|
130 | - $this->assign("createReasons", $createReasons); |
|
131 | - $declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
132 | - $this->assign("declineReasons", $declineReasons); |
|
133 | - |
|
134 | - $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database); |
|
135 | - $this->assign("allCreateReasons", $allCreateReasons); |
|
136 | - $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
137 | - $this->assign("allDeclineReasons", $allDeclineReasons); |
|
138 | - $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database); |
|
139 | - $this->assign("allOtherReasons", $allOtherReasons); |
|
140 | - |
|
141 | - $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
142 | - return User::getAllUsernames($database, true); |
|
143 | - }); |
|
144 | - } |
|
145 | - |
|
146 | - private function setupLogData(Request $request, PdoDatabase $database) |
|
147 | - { |
|
148 | - $currentUser = User::getCurrent($database); |
|
149 | - |
|
150 | - $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database); |
|
151 | - $requestLogs = array(); |
|
152 | - |
|
153 | - if (trim($request->getComment()) !== "") { |
|
154 | - $requestLogs[] = array( |
|
155 | - 'type' => 'comment', |
|
156 | - 'security' => 'user', |
|
157 | - 'userid' => null, |
|
158 | - 'user' => $request->getName(), |
|
159 | - 'entry' => null, |
|
160 | - 'time' => $request->getDate(), |
|
161 | - 'canedit' => false, |
|
162 | - 'id' => $request->getId(), |
|
163 | - 'comment' => $request->getComment(), |
|
164 | - ); |
|
165 | - } |
|
166 | - |
|
167 | - /** @var User[] $nameCache */ |
|
168 | - $nameCache = array(); |
|
169 | - |
|
170 | - $editableComments = $this->allowEditingComments($currentUser); |
|
171 | - |
|
172 | - /** @var Log|Comment $entry */ |
|
173 | - foreach ($logs as $entry) { |
|
174 | - // both log and comment have a 'user' field |
|
175 | - if (!array_key_exists($entry->getUser(), $nameCache)) { |
|
176 | - $entryUser = User::getById($entry->getUser(), $database); |
|
177 | - $nameCache[$entry->getUser()] = $entryUser; |
|
178 | - } |
|
179 | - |
|
180 | - if ($entry instanceof Comment) { |
|
181 | - $requestLogs[] = array( |
|
182 | - 'type' => 'comment', |
|
183 | - 'security' => $entry->getVisibility(), |
|
184 | - 'user' => $nameCache[$entry->getUser()]->getUsername(), |
|
185 | - 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
186 | - 'entry' => null, |
|
187 | - 'time' => $entry->getTime(), |
|
188 | - 'canedit' => ($editableComments || $entry->getUser() == $currentUser->getId()), |
|
189 | - 'id' => $entry->getId(), |
|
190 | - 'comment' => $entry->getComment(), |
|
191 | - ); |
|
192 | - } |
|
193 | - |
|
194 | - if ($entry instanceof Log) { |
|
195 | - $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0; |
|
196 | - $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()]; |
|
197 | - |
|
198 | - $requestLogs[] = array( |
|
199 | - 'type' => 'log', |
|
200 | - 'security' => 'user', |
|
201 | - 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
202 | - 'user' => $entryUser->getUsername(), |
|
203 | - 'entry' => LogHelper::getLogDescription($entry), |
|
204 | - 'time' => $entry->getTimestamp(), |
|
205 | - 'canedit' => false, |
|
206 | - 'id' => $entry->getId(), |
|
207 | - 'comment' => $entry->getComment(), |
|
208 | - ); |
|
209 | - } |
|
210 | - } |
|
211 | - |
|
212 | - $this->assign("requestLogs", $requestLogs); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @param Request $request |
|
217 | - */ |
|
218 | - protected function setupUsernameData(Request $request) |
|
219 | - { |
|
220 | - $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName()); |
|
221 | - |
|
222 | - $this->assign('requestIsBlacklisted', $blacklistData !== false); |
|
223 | - $this->assign('requestBlacklist', $blacklistData); |
|
224 | - |
|
225 | - try { |
|
226 | - $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName()); |
|
227 | - } |
|
228 | - catch (Exception $ex) { |
|
229 | - $spoofs = $ex->getMessage(); |
|
230 | - } |
|
231 | - |
|
232 | - $this->assign("spoofs", $spoofs); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param User $currentUser |
|
237 | - * |
|
238 | - * @return bool |
|
239 | - */ |
|
240 | - private function allowEditingComments(User $currentUser) |
|
241 | - { |
|
242 | - $editableComments = false; |
|
243 | - if ($currentUser->isAdmin() || $currentUser->isCheckuser()) { |
|
244 | - $editableComments = true; |
|
245 | - |
|
246 | - return $editableComments; |
|
247 | - } |
|
248 | - |
|
249 | - return $editableComments; |
|
250 | - } |
|
26 | + use RequestData; |
|
27 | + const PRIVATE_DATA_BARRIER = 'privateData'; |
|
28 | + const SET_BAN_BARRIER = 'setBan'; |
|
29 | + const STATUS_SYMBOL_OPEN = '☐'; |
|
30 | + const STATUS_SYMBOL_ACCEPTED = '☑'; |
|
31 | + const STATUS_SYMBOL_REJECTED = '☒'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Main function for this page, when no specific actions are called. |
|
35 | + * @throws ApplicationLogicException |
|
36 | + */ |
|
37 | + protected function main() |
|
38 | + { |
|
39 | + // set up csrf protection |
|
40 | + $this->assignCSRFToken(); |
|
41 | + |
|
42 | + // get some useful objects |
|
43 | + $database = $this->getDatabase(); |
|
44 | + $request = $this->getRequest($database, WebRequest::getInt('id')); |
|
45 | + $config = $this->getSiteConfiguration(); |
|
46 | + $currentUser = User::getCurrent($database); |
|
47 | + |
|
48 | + // Test we should be able to look at this request |
|
49 | + if ($config->getEmailConfirmationEnabled()) { |
|
50 | + if ($request->getEmailConfirm() !== 'Confirmed') { |
|
51 | + // Not allowed to look at this yet. |
|
52 | + throw new ApplicationLogicException('The email address has not yet been confirmed for this request.'); |
|
53 | + } |
|
54 | + } |
|
55 | + |
|
56 | + $this->setupBasicData($request, $config); |
|
57 | + |
|
58 | + $this->setupUsernameData($request); |
|
59 | + |
|
60 | + $this->setupTitle($request); |
|
61 | + |
|
62 | + $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
63 | + $this->setupGeneralData($database); |
|
64 | + |
|
65 | + $this->assign('requestDataCleared', false); |
|
66 | + if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
67 | + $this->assign('requestDataCleared', true); |
|
68 | + } |
|
69 | + |
|
70 | + $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
71 | + |
|
72 | + $this->setupLogData($request, $database); |
|
73 | + |
|
74 | + if ($allowedPrivateData) { |
|
75 | + $this->setTemplate('view-request/main-with-data.tpl'); |
|
76 | + $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
77 | + |
|
78 | + if ($currentUser->isCheckuser()) { |
|
79 | + $this->setTemplate('view-request/main-with-checkuser-data.tpl'); |
|
80 | + $this->setupCheckUserData($request); |
|
81 | + } |
|
82 | + } |
|
83 | + else { |
|
84 | + $this->setTemplate('view-request/main.tpl'); |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param Request $request |
|
90 | + */ |
|
91 | + protected function setupTitle(Request $request) |
|
92 | + { |
|
93 | + $statusSymbol = self::STATUS_SYMBOL_OPEN; |
|
94 | + if ($request->getStatus() === 'Closed') { |
|
95 | + if ($request->getWasCreated()) { |
|
96 | + $statusSymbol = self::STATUS_SYMBOL_ACCEPTED; |
|
97 | + } |
|
98 | + else { |
|
99 | + $statusSymbol = self::STATUS_SYMBOL_REJECTED; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Sets up data unrelated to the request, such as the email template information |
|
108 | + * |
|
109 | + * @param PdoDatabase $database |
|
110 | + */ |
|
111 | + protected function setupGeneralData(PdoDatabase $database) |
|
112 | + { |
|
113 | + $config = $this->getSiteConfiguration(); |
|
114 | + |
|
115 | + $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #'); |
|
116 | + |
|
117 | + $this->assign('defaultRequestState', $config->getDefaultRequestStateKey()); |
|
118 | + |
|
119 | + $this->assign('requestStates', $config->getRequestStates()); |
|
120 | + |
|
121 | + /** @var EmailTemplate $createdTemplate */ |
|
122 | + $createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database); |
|
123 | + |
|
124 | + $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != ''); |
|
125 | + $this->assign('createdJsQuestion', $createdTemplate->getJsquestion()); |
|
126 | + $this->assign('createdId', $createdTemplate->getId()); |
|
127 | + $this->assign('createdName', $createdTemplate->getName()); |
|
128 | + |
|
129 | + $createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database); |
|
130 | + $this->assign("createReasons", $createReasons); |
|
131 | + $declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
132 | + $this->assign("declineReasons", $declineReasons); |
|
133 | + |
|
134 | + $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database); |
|
135 | + $this->assign("allCreateReasons", $allCreateReasons); |
|
136 | + $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database); |
|
137 | + $this->assign("allDeclineReasons", $allDeclineReasons); |
|
138 | + $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database); |
|
139 | + $this->assign("allOtherReasons", $allOtherReasons); |
|
140 | + |
|
141 | + $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
142 | + return User::getAllUsernames($database, true); |
|
143 | + }); |
|
144 | + } |
|
145 | + |
|
146 | + private function setupLogData(Request $request, PdoDatabase $database) |
|
147 | + { |
|
148 | + $currentUser = User::getCurrent($database); |
|
149 | + |
|
150 | + $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database); |
|
151 | + $requestLogs = array(); |
|
152 | + |
|
153 | + if (trim($request->getComment()) !== "") { |
|
154 | + $requestLogs[] = array( |
|
155 | + 'type' => 'comment', |
|
156 | + 'security' => 'user', |
|
157 | + 'userid' => null, |
|
158 | + 'user' => $request->getName(), |
|
159 | + 'entry' => null, |
|
160 | + 'time' => $request->getDate(), |
|
161 | + 'canedit' => false, |
|
162 | + 'id' => $request->getId(), |
|
163 | + 'comment' => $request->getComment(), |
|
164 | + ); |
|
165 | + } |
|
166 | + |
|
167 | + /** @var User[] $nameCache */ |
|
168 | + $nameCache = array(); |
|
169 | + |
|
170 | + $editableComments = $this->allowEditingComments($currentUser); |
|
171 | + |
|
172 | + /** @var Log|Comment $entry */ |
|
173 | + foreach ($logs as $entry) { |
|
174 | + // both log and comment have a 'user' field |
|
175 | + if (!array_key_exists($entry->getUser(), $nameCache)) { |
|
176 | + $entryUser = User::getById($entry->getUser(), $database); |
|
177 | + $nameCache[$entry->getUser()] = $entryUser; |
|
178 | + } |
|
179 | + |
|
180 | + if ($entry instanceof Comment) { |
|
181 | + $requestLogs[] = array( |
|
182 | + 'type' => 'comment', |
|
183 | + 'security' => $entry->getVisibility(), |
|
184 | + 'user' => $nameCache[$entry->getUser()]->getUsername(), |
|
185 | + 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
186 | + 'entry' => null, |
|
187 | + 'time' => $entry->getTime(), |
|
188 | + 'canedit' => ($editableComments || $entry->getUser() == $currentUser->getId()), |
|
189 | + 'id' => $entry->getId(), |
|
190 | + 'comment' => $entry->getComment(), |
|
191 | + ); |
|
192 | + } |
|
193 | + |
|
194 | + if ($entry instanceof Log) { |
|
195 | + $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0; |
|
196 | + $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()]; |
|
197 | + |
|
198 | + $requestLogs[] = array( |
|
199 | + 'type' => 'log', |
|
200 | + 'security' => 'user', |
|
201 | + 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
|
202 | + 'user' => $entryUser->getUsername(), |
|
203 | + 'entry' => LogHelper::getLogDescription($entry), |
|
204 | + 'time' => $entry->getTimestamp(), |
|
205 | + 'canedit' => false, |
|
206 | + 'id' => $entry->getId(), |
|
207 | + 'comment' => $entry->getComment(), |
|
208 | + ); |
|
209 | + } |
|
210 | + } |
|
211 | + |
|
212 | + $this->assign("requestLogs", $requestLogs); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @param Request $request |
|
217 | + */ |
|
218 | + protected function setupUsernameData(Request $request) |
|
219 | + { |
|
220 | + $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName()); |
|
221 | + |
|
222 | + $this->assign('requestIsBlacklisted', $blacklistData !== false); |
|
223 | + $this->assign('requestBlacklist', $blacklistData); |
|
224 | + |
|
225 | + try { |
|
226 | + $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName()); |
|
227 | + } |
|
228 | + catch (Exception $ex) { |
|
229 | + $spoofs = $ex->getMessage(); |
|
230 | + } |
|
231 | + |
|
232 | + $this->assign("spoofs", $spoofs); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param User $currentUser |
|
237 | + * |
|
238 | + * @return bool |
|
239 | + */ |
|
240 | + private function allowEditingComments(User $currentUser) |
|
241 | + { |
|
242 | + $editableComments = false; |
|
243 | + if ($currentUser->isAdmin() || $currentUser->isCheckuser()) { |
|
244 | + $editableComments = true; |
|
245 | + |
|
246 | + return $editableComments; |
|
247 | + } |
|
248 | + |
|
249 | + return $editableComments; |
|
250 | + } |
|
251 | 251 | } |
252 | 252 | \ No newline at end of file |
@@ -100,7 +100,7 @@ |
||
100 | 100 | } |
101 | 101 | } |
102 | 102 | |
103 | - $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
|
103 | + $this->setHtmlTitle($statusSymbol.' #'.$request->getId()); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -18,127 +18,127 @@ |
||
18 | 18 | |
19 | 19 | class PageLog extends InternalPageBase |
20 | 20 | { |
21 | - /** |
|
22 | - * Main function for this page, when no specific actions are called. |
|
23 | - */ |
|
24 | - protected function main() |
|
25 | - { |
|
26 | - $this->setHtmlTitle('Logs'); |
|
27 | - |
|
28 | - $filterUser = WebRequest::getString('filterUser'); |
|
29 | - $filterAction = WebRequest::getString('filterAction'); |
|
30 | - |
|
31 | - $database = $this->getDatabase(); |
|
32 | - |
|
33 | - $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
34 | - return User::getAllUsernames($database); |
|
35 | - }); |
|
36 | - |
|
37 | - $limit = WebRequest::getInt('limit'); |
|
38 | - if ($limit === null) { |
|
39 | - $limit = 100; |
|
40 | - } |
|
41 | - |
|
42 | - $page = WebRequest::getInt('page'); |
|
43 | - if ($page === null) { |
|
44 | - $page = 1; |
|
45 | - } |
|
46 | - |
|
47 | - $offset = ($page - 1) * $limit; |
|
48 | - |
|
49 | - $logSearch = LogSearchHelper::get($database)->limit($limit, $offset); |
|
50 | - if ($filterUser !== null) { |
|
51 | - $logSearch->byUser(User::getByUsername($filterUser, $database)->getId()); |
|
52 | - } |
|
53 | - |
|
54 | - if ($filterAction !== null) { |
|
55 | - $logSearch->byAction($filterAction); |
|
56 | - } |
|
57 | - |
|
58 | - /** @var Log[] $logs */ |
|
59 | - $logs = $logSearch->getRecordCount($count)->fetch(); |
|
60 | - |
|
61 | - if ($count === 0) { |
|
62 | - $this->assign('logs', array()); |
|
63 | - $this->setTemplate('logs/main.tpl'); |
|
64 | - |
|
65 | - return; |
|
66 | - } |
|
67 | - |
|
68 | - list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration()); |
|
69 | - |
|
70 | - $this->setupPageData($page, $limit, $count); |
|
71 | - |
|
72 | - $this->assign("logs", $logData); |
|
73 | - $this->assign("users", $users); |
|
74 | - |
|
75 | - $this->assign("filterUser", $filterUser); |
|
76 | - $this->assign("filterAction", $filterAction); |
|
77 | - |
|
78 | - $this->assign('allLogActions', LogHelper::getLogActions($this->getDatabase())); |
|
79 | - |
|
80 | - $this->setTemplate("logs/main.tpl"); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Sets up the security for this page. If certain actions have different permissions, this should be reflected in |
|
85 | - * the return value from this function. |
|
86 | - * |
|
87 | - * If this page even supports actions, you will need to check the route |
|
88 | - * |
|
89 | - * @return SecurityConfiguration |
|
90 | - * @category Security-Critical |
|
91 | - */ |
|
92 | - protected function getSecurityConfiguration() |
|
93 | - { |
|
94 | - return $this->getSecurityManager()->configure()->asInternalPage(); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param int $page |
|
99 | - * @param int $limit |
|
100 | - * @param int $count |
|
101 | - */ |
|
102 | - protected function setupPageData($page, $limit, $count) |
|
103 | - { |
|
104 | - // The number of pages on the pager to show. Must be odd |
|
105 | - $pageLimit = 9; |
|
106 | - |
|
107 | - $pageData = array( |
|
108 | - // Can the user go to the previous page? |
|
109 | - 'canprev' => $page != 1, |
|
110 | - // Can the user go to the next page? |
|
111 | - 'cannext' => ($page * $limit) < $count, |
|
112 | - // Maximum page number |
|
113 | - 'maxpage' => ceil($count / $limit), |
|
114 | - // Limit to the number of pages to display |
|
115 | - 'pagelimit' => $pageLimit, |
|
116 | - ); |
|
117 | - |
|
118 | - // number of pages either side of the current to show |
|
119 | - $pageMargin = (($pageLimit - 1) / 2); |
|
120 | - |
|
121 | - // Calculate the number of pages either side to show - this is for situations like: |
|
122 | - // [1] [2] [[3]] [4] [5] [6] [7] [8] [9] - where you can't just use the page margin calculated |
|
123 | - $pageData['lowpage'] = max(1, $page - $pageMargin); |
|
124 | - $pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin); |
|
125 | - $pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1; |
|
126 | - |
|
127 | - if ($pageCount < $pageLimit) { |
|
128 | - if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) { |
|
129 | - $pageData['hipage'] = min($pageLimit, $pageData['maxpage']); |
|
130 | - } |
|
131 | - elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) { |
|
132 | - $pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - // Put the range of pages into the page data |
|
137 | - $pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']); |
|
138 | - |
|
139 | - $this->assign("pagedata", $pageData); |
|
140 | - |
|
141 | - $this->assign("limit", $limit); |
|
142 | - $this->assign("page", $page); |
|
143 | - } |
|
21 | + /** |
|
22 | + * Main function for this page, when no specific actions are called. |
|
23 | + */ |
|
24 | + protected function main() |
|
25 | + { |
|
26 | + $this->setHtmlTitle('Logs'); |
|
27 | + |
|
28 | + $filterUser = WebRequest::getString('filterUser'); |
|
29 | + $filterAction = WebRequest::getString('filterAction'); |
|
30 | + |
|
31 | + $database = $this->getDatabase(); |
|
32 | + |
|
33 | + $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) { |
|
34 | + return User::getAllUsernames($database); |
|
35 | + }); |
|
36 | + |
|
37 | + $limit = WebRequest::getInt('limit'); |
|
38 | + if ($limit === null) { |
|
39 | + $limit = 100; |
|
40 | + } |
|
41 | + |
|
42 | + $page = WebRequest::getInt('page'); |
|
43 | + if ($page === null) { |
|
44 | + $page = 1; |
|
45 | + } |
|
46 | + |
|
47 | + $offset = ($page - 1) * $limit; |
|
48 | + |
|
49 | + $logSearch = LogSearchHelper::get($database)->limit($limit, $offset); |
|
50 | + if ($filterUser !== null) { |
|
51 | + $logSearch->byUser(User::getByUsername($filterUser, $database)->getId()); |
|
52 | + } |
|
53 | + |
|
54 | + if ($filterAction !== null) { |
|
55 | + $logSearch->byAction($filterAction); |
|
56 | + } |
|
57 | + |
|
58 | + /** @var Log[] $logs */ |
|
59 | + $logs = $logSearch->getRecordCount($count)->fetch(); |
|
60 | + |
|
61 | + if ($count === 0) { |
|
62 | + $this->assign('logs', array()); |
|
63 | + $this->setTemplate('logs/main.tpl'); |
|
64 | + |
|
65 | + return; |
|
66 | + } |
|
67 | + |
|
68 | + list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration()); |
|
69 | + |
|
70 | + $this->setupPageData($page, $limit, $count); |
|
71 | + |
|
72 | + $this->assign("logs", $logData); |
|
73 | + $this->assign("users", $users); |
|
74 | + |
|
75 | + $this->assign("filterUser", $filterUser); |
|
76 | + $this->assign("filterAction", $filterAction); |
|
77 | + |
|
78 | + $this->assign('allLogActions', LogHelper::getLogActions($this->getDatabase())); |
|
79 | + |
|
80 | + $this->setTemplate("logs/main.tpl"); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Sets up the security for this page. If certain actions have different permissions, this should be reflected in |
|
85 | + * the return value from this function. |
|
86 | + * |
|
87 | + * If this page even supports actions, you will need to check the route |
|
88 | + * |
|
89 | + * @return SecurityConfiguration |
|
90 | + * @category Security-Critical |
|
91 | + */ |
|
92 | + protected function getSecurityConfiguration() |
|
93 | + { |
|
94 | + return $this->getSecurityManager()->configure()->asInternalPage(); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param int $page |
|
99 | + * @param int $limit |
|
100 | + * @param int $count |
|
101 | + */ |
|
102 | + protected function setupPageData($page, $limit, $count) |
|
103 | + { |
|
104 | + // The number of pages on the pager to show. Must be odd |
|
105 | + $pageLimit = 9; |
|
106 | + |
|
107 | + $pageData = array( |
|
108 | + // Can the user go to the previous page? |
|
109 | + 'canprev' => $page != 1, |
|
110 | + // Can the user go to the next page? |
|
111 | + 'cannext' => ($page * $limit) < $count, |
|
112 | + // Maximum page number |
|
113 | + 'maxpage' => ceil($count / $limit), |
|
114 | + // Limit to the number of pages to display |
|
115 | + 'pagelimit' => $pageLimit, |
|
116 | + ); |
|
117 | + |
|
118 | + // number of pages either side of the current to show |
|
119 | + $pageMargin = (($pageLimit - 1) / 2); |
|
120 | + |
|
121 | + // Calculate the number of pages either side to show - this is for situations like: |
|
122 | + // [1] [2] [[3]] [4] [5] [6] [7] [8] [9] - where you can't just use the page margin calculated |
|
123 | + $pageData['lowpage'] = max(1, $page - $pageMargin); |
|
124 | + $pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin); |
|
125 | + $pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1; |
|
126 | + |
|
127 | + if ($pageCount < $pageLimit) { |
|
128 | + if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) { |
|
129 | + $pageData['hipage'] = min($pageLimit, $pageData['maxpage']); |
|
130 | + } |
|
131 | + elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) { |
|
132 | + $pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + // Put the range of pages into the page data |
|
137 | + $pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']); |
|
138 | + |
|
139 | + $this->assign("pagedata", $pageData); |
|
140 | + |
|
141 | + $this->assign("limit", $limit); |
|
142 | + $this->assign("page", $page); |
|
143 | + } |
|
144 | 144 | } |
145 | 145 | \ No newline at end of file |
@@ -10,27 +10,27 @@ |
||
10 | 10 | |
11 | 11 | class IrcColourCode |
12 | 12 | { |
13 | - const BOLD = "\x02"; |
|
14 | - const ITALIC = "\x09"; |
|
15 | - const STRIKE = "\x13"; |
|
16 | - const UNDERLINE = "\x15"; |
|
17 | - const UNDERLINE2 = "\x1f"; |
|
18 | - const REVERSE = "\x16"; |
|
19 | - const RESET = "\x0f"; |
|
20 | - const WHITE = "\x0300"; |
|
21 | - const BLACK = "\x0301"; |
|
22 | - const DARK_BLUE = "\x0302"; |
|
23 | - const DARK_GREEN = "\x0303"; |
|
24 | - const RED = "\x0304"; |
|
25 | - const DARK_RED = "\x0305"; |
|
26 | - const DARK_VIOLET = "\x0306"; |
|
27 | - const ORANGE = "\x0307"; |
|
28 | - const YELLOW = "\x0308"; |
|
29 | - const LIGHT_GREEN = "\x0309"; |
|
30 | - const CYAN = "\x0310"; |
|
31 | - const LIGHT_CYAN = "\x0311"; |
|
32 | - const BLUE = "\x0312"; |
|
33 | - const VIOLET = "\x0313"; |
|
34 | - const DARK_GREY = "\x0314"; |
|
35 | - const LIGHT_GREY = "\x0315"; |
|
13 | + const BOLD = "\x02"; |
|
14 | + const ITALIC = "\x09"; |
|
15 | + const STRIKE = "\x13"; |
|
16 | + const UNDERLINE = "\x15"; |
|
17 | + const UNDERLINE2 = "\x1f"; |
|
18 | + const REVERSE = "\x16"; |
|
19 | + const RESET = "\x0f"; |
|
20 | + const WHITE = "\x0300"; |
|
21 | + const BLACK = "\x0301"; |
|
22 | + const DARK_BLUE = "\x0302"; |
|
23 | + const DARK_GREEN = "\x0303"; |
|
24 | + const RED = "\x0304"; |
|
25 | + const DARK_RED = "\x0305"; |
|
26 | + const DARK_VIOLET = "\x0306"; |
|
27 | + const ORANGE = "\x0307"; |
|
28 | + const YELLOW = "\x0308"; |
|
29 | + const LIGHT_GREEN = "\x0309"; |
|
30 | + const CYAN = "\x0310"; |
|
31 | + const LIGHT_CYAN = "\x0311"; |
|
32 | + const BLUE = "\x0312"; |
|
33 | + const VIOLET = "\x0313"; |
|
34 | + const DARK_GREY = "\x0314"; |
|
35 | + const LIGHT_GREY = "\x0315"; |
|
36 | 36 | } |
@@ -13,24 +13,24 @@ |
||
13 | 13 | |
14 | 14 | class NotIdentifiedException extends ReadableException |
15 | 15 | { |
16 | - /** |
|
17 | - * Returns a readable HTML error message that's displayable to the user using templates. |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function getReadableError() |
|
21 | - { |
|
22 | - if (!headers_sent()) { |
|
23 | - header("HTTP/1.1 403 Forbidden"); |
|
24 | - } |
|
16 | + /** |
|
17 | + * Returns a readable HTML error message that's displayable to the user using templates. |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function getReadableError() |
|
21 | + { |
|
22 | + if (!headers_sent()) { |
|
23 | + header("HTTP/1.1 403 Forbidden"); |
|
24 | + } |
|
25 | 25 | |
26 | - $this->setUpSmarty(); |
|
26 | + $this->setUpSmarty(); |
|
27 | 27 | |
28 | - // uck. We should still be able to access the database in this situation though. |
|
29 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
30 | - $currentUser = User::getCurrent($database); |
|
31 | - $this->assign('currentUser', $currentUser); |
|
32 | - $this->assign("loggedIn", (!$currentUser->isCommunityUser())); |
|
28 | + // uck. We should still be able to access the database in this situation though. |
|
29 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
30 | + $currentUser = User::getCurrent($database); |
|
31 | + $this->assign('currentUser', $currentUser); |
|
32 | + $this->assign("loggedIn", (!$currentUser->isCommunityUser())); |
|
33 | 33 | |
34 | - return $this->fetchTemplate("exception/not-identified.tpl"); |
|
35 | - } |
|
34 | + return $this->fetchTemplate("exception/not-identified.tpl"); |
|
35 | + } |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |
@@ -21,22 +21,22 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class ReadableException extends Exception |
23 | 23 | { |
24 | - use TemplateOutput; |
|
24 | + use TemplateOutput; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns a readable HTML error message that's displayable to the user using templates. |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - abstract public function getReadableError(); |
|
26 | + /** |
|
27 | + * Returns a readable HTML error message that's displayable to the user using templates. |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + abstract public function getReadableError(); |
|
31 | 31 | |
32 | - /** |
|
33 | - * @return SiteConfiguration |
|
34 | - */ |
|
35 | - protected function getSiteConfiguration() |
|
36 | - { |
|
37 | - // Uck. However, we have encountered an exception. |
|
38 | - global $siteConfiguration; |
|
32 | + /** |
|
33 | + * @return SiteConfiguration |
|
34 | + */ |
|
35 | + protected function getSiteConfiguration() |
|
36 | + { |
|
37 | + // Uck. However, we have encountered an exception. |
|
38 | + global $siteConfiguration; |
|
39 | 39 | |
40 | - return $siteConfiguration; |
|
41 | - } |
|
40 | + return $siteConfiguration; |
|
41 | + } |
|
42 | 42 | } |
43 | 43 | \ No newline at end of file |
@@ -21,13 +21,13 @@ |
||
21 | 21 | */ |
22 | 22 | class EnvironmentException extends Exception |
23 | 23 | { |
24 | - /** |
|
25 | - * EnvironmentException constructor. |
|
26 | - * |
|
27 | - * @param string $friendlyMessage |
|
28 | - */ |
|
29 | - public function __construct($friendlyMessage) |
|
30 | - { |
|
31 | - parent::__construct($friendlyMessage); |
|
32 | - } |
|
24 | + /** |
|
25 | + * EnvironmentException constructor. |
|
26 | + * |
|
27 | + * @param string $friendlyMessage |
|
28 | + */ |
|
29 | + public function __construct($friendlyMessage) |
|
30 | + { |
|
31 | + parent::__construct($friendlyMessage); |
|
32 | + } |
|
33 | 33 | } |
34 | 34 | \ No newline at end of file |
@@ -22,60 +22,60 @@ |
||
22 | 22 | */ |
23 | 23 | class AccessDeniedException extends ReadableException |
24 | 24 | { |
25 | - public function getReadableError() |
|
26 | - { |
|
27 | - if (!headers_sent()) { |
|
28 | - header("HTTP/1.1 403 Forbidden"); |
|
29 | - } |
|
25 | + public function getReadableError() |
|
26 | + { |
|
27 | + if (!headers_sent()) { |
|
28 | + header("HTTP/1.1 403 Forbidden"); |
|
29 | + } |
|
30 | 30 | |
31 | - $this->setUpSmarty(); |
|
31 | + $this->setUpSmarty(); |
|
32 | 32 | |
33 | - // uck. We should still be able to access the database in this situation though. |
|
34 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
35 | - $currentUser = User::getCurrent($database); |
|
36 | - $this->assign('currentUser', $currentUser); |
|
37 | - $this->assign("loggedIn", (!$currentUser->isCommunityUser())); |
|
33 | + // uck. We should still be able to access the database in this situation though. |
|
34 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
35 | + $currentUser = User::getCurrent($database); |
|
36 | + $this->assign('currentUser', $currentUser); |
|
37 | + $this->assign("loggedIn", (!$currentUser->isCommunityUser())); |
|
38 | 38 | |
39 | - if ($currentUser->isDeclined()) { |
|
40 | - $this->assign('htmlTitle', 'Account Declined'); |
|
41 | - $this->assign('declineReason', $this->getLogEntry('Declined', $currentUser, $database)); |
|
39 | + if ($currentUser->isDeclined()) { |
|
40 | + $this->assign('htmlTitle', 'Account Declined'); |
|
41 | + $this->assign('declineReason', $this->getLogEntry('Declined', $currentUser, $database)); |
|
42 | 42 | |
43 | - return $this->fetchTemplate("exception/account-declined.tpl"); |
|
44 | - } |
|
43 | + return $this->fetchTemplate("exception/account-declined.tpl"); |
|
44 | + } |
|
45 | 45 | |
46 | - if ($currentUser->isSuspended()) { |
|
47 | - $this->assign('htmlTitle', 'Account Suspended'); |
|
48 | - $this->assign('suspendReason', $this->getLogEntry('Suspended', $currentUser, $database)); |
|
46 | + if ($currentUser->isSuspended()) { |
|
47 | + $this->assign('htmlTitle', 'Account Suspended'); |
|
48 | + $this->assign('suspendReason', $this->getLogEntry('Suspended', $currentUser, $database)); |
|
49 | 49 | |
50 | - return $this->fetchTemplate("exception/account-suspended.tpl"); |
|
51 | - } |
|
50 | + return $this->fetchTemplate("exception/account-suspended.tpl"); |
|
51 | + } |
|
52 | 52 | |
53 | - if ($currentUser->isNewUser()) { |
|
54 | - $this->assign('htmlTitle', 'Account Pending'); |
|
53 | + if ($currentUser->isNewUser()) { |
|
54 | + $this->assign('htmlTitle', 'Account Pending'); |
|
55 | 55 | |
56 | - return $this->fetchTemplate("exception/account-new.tpl"); |
|
57 | - } |
|
56 | + return $this->fetchTemplate("exception/account-new.tpl"); |
|
57 | + } |
|
58 | 58 | |
59 | - return $this->fetchTemplate("exception/access-denied.tpl"); |
|
60 | - } |
|
59 | + return $this->fetchTemplate("exception/access-denied.tpl"); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param string $action |
|
64 | - * @param User $user |
|
65 | - * @param PdoDatabase $database |
|
66 | - * |
|
67 | - * @return null|string |
|
68 | - */ |
|
69 | - private function getLogEntry($action, User $user, PdoDatabase $database) |
|
70 | - { |
|
71 | - /** @var Log[] $logs */ |
|
72 | - $logs = LogSearchHelper::get($database) |
|
73 | - ->byAction($action) |
|
74 | - ->byObjectType('User') |
|
75 | - ->byObjectId($user->getId()) |
|
76 | - ->limit(1) |
|
77 | - ->fetch(); |
|
62 | + /** |
|
63 | + * @param string $action |
|
64 | + * @param User $user |
|
65 | + * @param PdoDatabase $database |
|
66 | + * |
|
67 | + * @return null|string |
|
68 | + */ |
|
69 | + private function getLogEntry($action, User $user, PdoDatabase $database) |
|
70 | + { |
|
71 | + /** @var Log[] $logs */ |
|
72 | + $logs = LogSearchHelper::get($database) |
|
73 | + ->byAction($action) |
|
74 | + ->byObjectType('User') |
|
75 | + ->byObjectId($user->getId()) |
|
76 | + ->limit(1) |
|
77 | + ->fetch(); |
|
78 | 78 | |
79 | - return $logs[0]->getComment(); |
|
80 | - } |
|
79 | + return $logs[0]->getComment(); |
|
80 | + } |
|
81 | 81 | } |
82 | 82 | \ No newline at end of file |
@@ -15,112 +15,112 @@ |
||
15 | 15 | |
16 | 16 | class PdoDatabase extends PDO |
17 | 17 | { |
18 | - /** |
|
19 | - * @var PdoDatabase[] |
|
20 | - */ |
|
21 | - private static $connections = array(); |
|
22 | - /** |
|
23 | - * @var bool True if a transaction is active |
|
24 | - */ |
|
25 | - protected $hasActiveTransaction = false; |
|
26 | - |
|
27 | - /** |
|
28 | - * Unless you're doing low-level work, this is not the function you want. |
|
29 | - * |
|
30 | - * @param string $connectionName |
|
31 | - * |
|
32 | - * @return PdoDatabase |
|
33 | - * @throws Exception |
|
34 | - */ |
|
35 | - public static function getDatabaseConnection($connectionName) |
|
36 | - { |
|
37 | - if (!isset(self::$connections[$connectionName])) { |
|
38 | - global $cDatabaseConfig; |
|
39 | - |
|
40 | - if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
41 | - throw new Exception("Database configuration not found for alias $connectionName"); |
|
42 | - } |
|
43 | - |
|
44 | - try { |
|
45 | - $databaseObject = new PdoDatabase( |
|
46 | - $cDatabaseConfig[$connectionName]["dsrcname"], |
|
47 | - $cDatabaseConfig[$connectionName]["username"], |
|
48 | - $cDatabaseConfig[$connectionName]["password"] |
|
49 | - ); |
|
50 | - } |
|
51 | - catch (PDOException $ex) { |
|
52 | - // wrap around any potential stack traces which may include passwords |
|
53 | - throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
54 | - } |
|
55 | - |
|
56 | - $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
57 | - |
|
58 | - // emulating prepared statements gives a performance boost on MySQL. |
|
59 | - // |
|
60 | - // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
61 | - // the prepared statements, so we're forced to turn this off for now. |
|
62 | - // -- stw 2014-02-11 |
|
63 | - $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
64 | - |
|
65 | - self::$connections[$connectionName] = $databaseObject; |
|
66 | - } |
|
67 | - |
|
68 | - return self::$connections[$connectionName]; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Determines if this connection has a transaction in progress or not |
|
73 | - * @return boolean true if there is a transaction in progress. |
|
74 | - */ |
|
75 | - public function hasActiveTransaction() |
|
76 | - { |
|
77 | - return $this->hasActiveTransaction; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Summary of beginTransaction |
|
82 | - * @return bool |
|
83 | - */ |
|
84 | - public function beginTransaction() |
|
85 | - { |
|
86 | - // Override the pre-existing method, which doesn't stop you from |
|
87 | - // starting transactions within transactions - which doesn't work and |
|
88 | - // will throw an exception. This eliminates the need to catch exceptions |
|
89 | - // all over the rest of the code |
|
90 | - if ($this->hasActiveTransaction) { |
|
91 | - return false; |
|
92 | - } |
|
93 | - else { |
|
94 | - // set the transaction isolation level for every transaction. |
|
95 | - $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
96 | - |
|
97 | - // start a new transaction, and return whether or not the start was |
|
98 | - // successful |
|
99 | - $this->hasActiveTransaction = parent::beginTransaction(); |
|
100 | - |
|
101 | - return $this->hasActiveTransaction; |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Commits the active transaction |
|
107 | - */ |
|
108 | - public function commit() |
|
109 | - { |
|
110 | - if ($this->hasActiveTransaction) { |
|
111 | - parent::commit(); |
|
112 | - $this->hasActiveTransaction = false; |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Rolls back a transaction |
|
118 | - */ |
|
119 | - public function rollBack() |
|
120 | - { |
|
121 | - if ($this->hasActiveTransaction) { |
|
122 | - parent::rollback(); |
|
123 | - $this->hasActiveTransaction = false; |
|
124 | - } |
|
125 | - } |
|
18 | + /** |
|
19 | + * @var PdoDatabase[] |
|
20 | + */ |
|
21 | + private static $connections = array(); |
|
22 | + /** |
|
23 | + * @var bool True if a transaction is active |
|
24 | + */ |
|
25 | + protected $hasActiveTransaction = false; |
|
26 | + |
|
27 | + /** |
|
28 | + * Unless you're doing low-level work, this is not the function you want. |
|
29 | + * |
|
30 | + * @param string $connectionName |
|
31 | + * |
|
32 | + * @return PdoDatabase |
|
33 | + * @throws Exception |
|
34 | + */ |
|
35 | + public static function getDatabaseConnection($connectionName) |
|
36 | + { |
|
37 | + if (!isset(self::$connections[$connectionName])) { |
|
38 | + global $cDatabaseConfig; |
|
39 | + |
|
40 | + if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
41 | + throw new Exception("Database configuration not found for alias $connectionName"); |
|
42 | + } |
|
43 | + |
|
44 | + try { |
|
45 | + $databaseObject = new PdoDatabase( |
|
46 | + $cDatabaseConfig[$connectionName]["dsrcname"], |
|
47 | + $cDatabaseConfig[$connectionName]["username"], |
|
48 | + $cDatabaseConfig[$connectionName]["password"] |
|
49 | + ); |
|
50 | + } |
|
51 | + catch (PDOException $ex) { |
|
52 | + // wrap around any potential stack traces which may include passwords |
|
53 | + throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
54 | + } |
|
55 | + |
|
56 | + $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
57 | + |
|
58 | + // emulating prepared statements gives a performance boost on MySQL. |
|
59 | + // |
|
60 | + // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
61 | + // the prepared statements, so we're forced to turn this off for now. |
|
62 | + // -- stw 2014-02-11 |
|
63 | + $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
64 | + |
|
65 | + self::$connections[$connectionName] = $databaseObject; |
|
66 | + } |
|
67 | + |
|
68 | + return self::$connections[$connectionName]; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Determines if this connection has a transaction in progress or not |
|
73 | + * @return boolean true if there is a transaction in progress. |
|
74 | + */ |
|
75 | + public function hasActiveTransaction() |
|
76 | + { |
|
77 | + return $this->hasActiveTransaction; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Summary of beginTransaction |
|
82 | + * @return bool |
|
83 | + */ |
|
84 | + public function beginTransaction() |
|
85 | + { |
|
86 | + // Override the pre-existing method, which doesn't stop you from |
|
87 | + // starting transactions within transactions - which doesn't work and |
|
88 | + // will throw an exception. This eliminates the need to catch exceptions |
|
89 | + // all over the rest of the code |
|
90 | + if ($this->hasActiveTransaction) { |
|
91 | + return false; |
|
92 | + } |
|
93 | + else { |
|
94 | + // set the transaction isolation level for every transaction. |
|
95 | + $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
96 | + |
|
97 | + // start a new transaction, and return whether or not the start was |
|
98 | + // successful |
|
99 | + $this->hasActiveTransaction = parent::beginTransaction(); |
|
100 | + |
|
101 | + return $this->hasActiveTransaction; |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Commits the active transaction |
|
107 | + */ |
|
108 | + public function commit() |
|
109 | + { |
|
110 | + if ($this->hasActiveTransaction) { |
|
111 | + parent::commit(); |
|
112 | + $this->hasActiveTransaction = false; |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Rolls back a transaction |
|
118 | + */ |
|
119 | + public function rollBack() |
|
120 | + { |
|
121 | + if ($this->hasActiveTransaction) { |
|
122 | + parent::rollback(); |
|
123 | + $this->hasActiveTransaction = false; |
|
124 | + } |
|
125 | + } |
|
126 | 126 | } |
@@ -50,7 +50,7 @@ |
||
50 | 50 | } |
51 | 51 | catch (PDOException $ex) { |
52 | 52 | // wrap around any potential stack traces which may include passwords |
53 | - throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
53 | + throw new EnvironmentException("Error connecting to database '$connectionName': ".$ex->getMessage()); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
@@ -13,22 +13,22 @@ discard block |
||
13 | 13 | |
14 | 14 | class ExceptionHandler |
15 | 15 | { |
16 | - /** |
|
17 | - * Global exception handler |
|
18 | - * |
|
19 | - * Smarty would be nice to use, but it COULD BE smarty that throws the errors. |
|
20 | - * Let's build something ourselves, and hope it works. |
|
21 | - * |
|
22 | - * @param $exception |
|
23 | - * |
|
24 | - * @category Security-Critical - has the potential to leak data when exception is thrown. |
|
25 | - */ |
|
26 | - public static function exceptionHandler(Exception $exception) |
|
27 | - { |
|
28 | - /** @global $siteConfiguration SiteConfiguration */ |
|
29 | - global $siteConfiguration; |
|
30 | - |
|
31 | - $errorDocument = <<<HTML |
|
16 | + /** |
|
17 | + * Global exception handler |
|
18 | + * |
|
19 | + * Smarty would be nice to use, but it COULD BE smarty that throws the errors. |
|
20 | + * Let's build something ourselves, and hope it works. |
|
21 | + * |
|
22 | + * @param $exception |
|
23 | + * |
|
24 | + * @category Security-Critical - has the potential to leak data when exception is thrown. |
|
25 | + */ |
|
26 | + public static function exceptionHandler(Exception $exception) |
|
27 | + { |
|
28 | + /** @global $siteConfiguration SiteConfiguration */ |
|
29 | + global $siteConfiguration; |
|
30 | + |
|
31 | + $errorDocument = <<<HTML |
|
32 | 32 | <!DOCTYPE html> |
33 | 33 | <html lang="en"><head> |
34 | 34 | <meta charset="utf-8"> |
@@ -49,77 +49,77 @@ discard block |
||
49 | 49 | </div></body></html> |
50 | 50 | HTML; |
51 | 51 | |
52 | - $errorData = self::getExceptionData($exception); |
|
53 | - $errorData['server'] = $_SERVER; |
|
54 | - $errorData['get'] = $_GET; |
|
55 | - $errorData['post'] = $_POST; |
|
56 | - |
|
57 | - $state = serialize($errorData); |
|
58 | - $errorId = sha1($state); |
|
59 | - |
|
60 | - // Save the error for later analysis |
|
61 | - file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
|
62 | - |
|
63 | - // clear and discard any content that's been saved to the output buffer |
|
64 | - if (ob_get_level() > 0) { |
|
65 | - ob_end_clean(); |
|
66 | - } |
|
67 | - |
|
68 | - // push error ID into the document. |
|
69 | - $message = str_replace('$1$', $errorId, $errorDocument); |
|
70 | - |
|
71 | - if ($siteConfiguration->getDebuggingTraceEnabled()) { |
|
72 | - ob_start(); |
|
73 | - var_dump($errorData); |
|
74 | - $textErrorData = ob_get_contents(); |
|
75 | - ob_end_clean(); |
|
76 | - |
|
77 | - $message = str_replace('$2$', $textErrorData, $message); |
|
78 | - } |
|
79 | - else { |
|
80 | - $message = str_replace('$2$', "", $message); |
|
81 | - } |
|
82 | - |
|
83 | - // While we *shouldn't* have sent headers by now due to the output buffering, PHPUnit does weird things. |
|
84 | - // This is "only" needed for the tests, but it's a good idea to wrap this anyway. |
|
85 | - if (!headers_sent()) { |
|
86 | - header('HTTP/1.1 500 Internal Server Error'); |
|
87 | - } |
|
88 | - |
|
89 | - // output the document |
|
90 | - print $message; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param int $errorSeverity The severity level of the exception. |
|
95 | - * @param string $errorMessage The Exception message to throw. |
|
96 | - * @param string $errorFile The filename where the exception is thrown. |
|
97 | - * @param int $errorLine The line number where the exception is thrown. |
|
98 | - * |
|
99 | - * @throws ErrorException |
|
100 | - */ |
|
101 | - public static function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine) |
|
102 | - { |
|
103 | - // call into the main exception handler above |
|
104 | - throw new ErrorException($errorMessage, 0, $errorSeverity, $errorFile, $errorLine); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param Exception $exception |
|
109 | - * |
|
110 | - * @return null|array |
|
111 | - */ |
|
112 | - private static function getExceptionData($exception) |
|
113 | - { |
|
114 | - if ($exception == null) { |
|
115 | - return null; |
|
116 | - } |
|
117 | - |
|
118 | - return array( |
|
119 | - 'exception' => get_class($exception), |
|
120 | - 'message' => $exception->getMessage(), |
|
121 | - 'stack' => $exception->getTraceAsString(), |
|
122 | - 'previous' => self::getExceptionData($exception->getPrevious()), |
|
123 | - ); |
|
124 | - } |
|
52 | + $errorData = self::getExceptionData($exception); |
|
53 | + $errorData['server'] = $_SERVER; |
|
54 | + $errorData['get'] = $_GET; |
|
55 | + $errorData['post'] = $_POST; |
|
56 | + |
|
57 | + $state = serialize($errorData); |
|
58 | + $errorId = sha1($state); |
|
59 | + |
|
60 | + // Save the error for later analysis |
|
61 | + file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
|
62 | + |
|
63 | + // clear and discard any content that's been saved to the output buffer |
|
64 | + if (ob_get_level() > 0) { |
|
65 | + ob_end_clean(); |
|
66 | + } |
|
67 | + |
|
68 | + // push error ID into the document. |
|
69 | + $message = str_replace('$1$', $errorId, $errorDocument); |
|
70 | + |
|
71 | + if ($siteConfiguration->getDebuggingTraceEnabled()) { |
|
72 | + ob_start(); |
|
73 | + var_dump($errorData); |
|
74 | + $textErrorData = ob_get_contents(); |
|
75 | + ob_end_clean(); |
|
76 | + |
|
77 | + $message = str_replace('$2$', $textErrorData, $message); |
|
78 | + } |
|
79 | + else { |
|
80 | + $message = str_replace('$2$', "", $message); |
|
81 | + } |
|
82 | + |
|
83 | + // While we *shouldn't* have sent headers by now due to the output buffering, PHPUnit does weird things. |
|
84 | + // This is "only" needed for the tests, but it's a good idea to wrap this anyway. |
|
85 | + if (!headers_sent()) { |
|
86 | + header('HTTP/1.1 500 Internal Server Error'); |
|
87 | + } |
|
88 | + |
|
89 | + // output the document |
|
90 | + print $message; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param int $errorSeverity The severity level of the exception. |
|
95 | + * @param string $errorMessage The Exception message to throw. |
|
96 | + * @param string $errorFile The filename where the exception is thrown. |
|
97 | + * @param int $errorLine The line number where the exception is thrown. |
|
98 | + * |
|
99 | + * @throws ErrorException |
|
100 | + */ |
|
101 | + public static function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine) |
|
102 | + { |
|
103 | + // call into the main exception handler above |
|
104 | + throw new ErrorException($errorMessage, 0, $errorSeverity, $errorFile, $errorLine); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param Exception $exception |
|
109 | + * |
|
110 | + * @return null|array |
|
111 | + */ |
|
112 | + private static function getExceptionData($exception) |
|
113 | + { |
|
114 | + if ($exception == null) { |
|
115 | + return null; |
|
116 | + } |
|
117 | + |
|
118 | + return array( |
|
119 | + 'exception' => get_class($exception), |
|
120 | + 'message' => $exception->getMessage(), |
|
121 | + 'stack' => $exception->getTraceAsString(), |
|
122 | + 'previous' => self::getExceptionData($exception->getPrevious()), |
|
123 | + ); |
|
124 | + } |
|
125 | 125 | } |
126 | 126 | \ No newline at end of file |
@@ -58,7 +58,7 @@ |
||
58 | 58 | $errorId = sha1($state); |
59 | 59 | |
60 | 60 | // Save the error for later analysis |
61 | - file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
|
61 | + file_put_contents($siteConfiguration->getErrorLog().'/'.$errorId.'.log', $state); |
|
62 | 62 | |
63 | 63 | // clear and discard any content that's been saved to the output buffer |
64 | 64 | if (ob_get_level() > 0) { |