1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Logger short summary. |
5
|
|
|
* |
6
|
|
|
* Logger description. |
7
|
|
|
* |
8
|
|
|
* @version 1.0 |
9
|
|
|
* @author stwalkerster |
10
|
|
|
*/ |
11
|
|
|
class Logger |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param PdoDatabase $database |
15
|
|
|
* @param DataObject $object |
16
|
|
|
* @param string $logAction |
17
|
|
|
* @param null $comment |
|
|
|
|
18
|
|
|
* @param User $user |
19
|
|
|
* @throws Exception |
20
|
|
|
*/ |
21
|
|
|
private static function createLogEntry(PdoDatabase $database, DataObject $object, $logAction, $comment = null, $user = null) |
22
|
|
|
{ |
23
|
|
|
if ($user == null) { |
24
|
|
|
$user = User::getCurrent(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$log = new Log(); |
28
|
|
|
$log->setDatabase($database); |
29
|
|
|
$log->setAction($logAction); |
30
|
|
|
$log->setObjectId($object->getId()); |
31
|
|
|
$log->setObjectType(get_class($object)); |
32
|
|
|
$log->setUser($user); |
33
|
|
|
$log->setComment($comment); |
34
|
|
|
$log->save(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function emailConfirmed(PdoDatabase $database, Request $object) |
38
|
|
|
{ |
39
|
|
|
self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
#region Users |
43
|
|
|
|
44
|
|
|
public static function approvedUser(PdoDatabase $database, User $object) |
45
|
|
|
{ |
46
|
|
|
self::createLogEntry($database, $object, "Approved"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function declinedUser(PdoDatabase $database, User $object, $comment) |
50
|
|
|
{ |
51
|
|
|
self::createLogEntry($database, $object, "Declined", $comment); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function suspendedUser(PdoDatabase $database, User $object, $comment) |
55
|
|
|
{ |
56
|
|
|
self::createLogEntry($database, $object, "Suspended", $comment); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function demotedUser(PdoDatabase $database, User $object, $comment) |
60
|
|
|
{ |
61
|
|
|
self::createLogEntry($database, $object, "Demoted", $comment); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function promotedUser(PdoDatabase $database, User $object) |
65
|
|
|
{ |
66
|
|
|
self::createLogEntry($database, $object, "Promoted"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function renamedUser(PdoDatabase $database, User $object, $comment) |
70
|
|
|
{ |
71
|
|
|
self::createLogEntry($database, $object, "Renamed", $comment); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function userPreferencesChange(PdoDatabase $database, User $object) |
75
|
|
|
{ |
76
|
|
|
self::createLogEntry($database, $object, "Prefchange"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
#endregion |
80
|
|
|
|
81
|
|
|
public static function interfaceMessageEdited(PdoDatabase $database, InterfaceMessage $object) |
82
|
|
|
{ |
83
|
|
|
self::createLogEntry($database, $object, "Edited"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
#region Welcome Templates |
87
|
|
|
|
88
|
|
|
public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
89
|
|
|
{ |
90
|
|
|
self::createLogEntry($database, $object, "CreatedTemplate"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
94
|
|
|
{ |
95
|
|
|
self::createLogEntry($database, $object, "EditedTemplate"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
99
|
|
|
{ |
100
|
|
|
self::createLogEntry($database, $object, "DeletedTemplate"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
#endregion |
104
|
|
|
|
105
|
|
|
#region Bans |
106
|
|
|
|
107
|
|
|
public static function banned(PdoDatabase $database, Ban $object, $reason) |
108
|
|
|
{ |
109
|
|
|
self::createLogEntry($database, $object, "Banned", $reason); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
113
|
|
|
{ |
114
|
|
|
self::createLogEntry($database, $object, "Unbanned", $reason); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
#endregion |
118
|
|
|
|
119
|
|
|
#region Requests |
120
|
|
|
|
121
|
|
|
public static function deferRequest(PdoDatabase $database, Request $object, $target) |
122
|
|
|
{ |
123
|
|
|
self::createLogEntry($database, $object, "Deferred to $target"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment) |
127
|
|
|
{ |
128
|
|
|
self::createLogEntry($database, $object, "Closed $target", $comment); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function reserve(PdoDatabase $database, Request $object) |
132
|
|
|
{ |
133
|
|
|
self::createLogEntry($database, $object, "Reserved"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function breakReserve(PdoDatabase $database, Request $object) |
137
|
|
|
{ |
138
|
|
|
self::createLogEntry($database, $object, "BreakReserve"); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public static function unreserve(PdoDatabase $database, Request $object) |
142
|
|
|
{ |
143
|
|
|
self::createLogEntry($database, $object, "Unreserved"); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public static function editComment(PdoDatabase $database, Comment $object) |
147
|
|
|
{ |
148
|
|
|
self::createLogEntry($database, $object->getRequestObject(), "EditComment-r"); |
149
|
|
|
self::createLogEntry($database, $object, "EditComment-c"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
153
|
|
|
{ |
154
|
|
|
self::createLogEntry($database, $object, "SendReserved"); |
155
|
|
|
self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public static function sentMail(PdoDatabase $database, Request $object, $comment) |
159
|
|
|
{ |
160
|
|
|
self::createLogEntry($database, $object, "SentMail", $comment); |
161
|
|
|
} |
162
|
|
|
#endregion |
163
|
|
|
|
164
|
|
|
#region Email templates |
165
|
|
|
|
166
|
|
|
public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
167
|
|
|
{ |
168
|
|
|
self::createLogEntry($database, $object, "CreatedEmail"); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
172
|
|
|
{ |
173
|
|
|
self::createLogEntry($database, $object, "EditedEmail"); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
#endregion |
177
|
|
|
|
178
|
|
|
#region Display |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Summary of getRequestLogs |
182
|
|
|
* @param int $requestId ID of the request to get logs for |
183
|
|
|
* @param PdoDatabase $db Database to use |
184
|
|
|
* @return array|bool |
185
|
|
|
*/ |
186
|
|
|
public static function getRequestLogs($requestId, PdoDatabase $db) |
187
|
|
|
{ |
188
|
|
|
$logStatement = $db->prepare( |
189
|
|
|
"SELECT * FROM log WHERE objecttype = 'Request' AND objectid = :requestid ORDER BY timestamp DESC"); |
190
|
|
|
|
191
|
|
|
$result = $logStatement->execute(array(":requestid" => $requestId)); |
192
|
|
|
if ($result) { |
193
|
|
|
$data = $logStatement->fetchAll(PDO::FETCH_CLASS, "Log"); |
194
|
|
|
|
195
|
|
|
/** @var Log $entry */ |
196
|
|
|
foreach ($data as $entry) { |
197
|
|
|
$entry->isNew = false; |
198
|
|
|
$entry->setDatabase($db); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $data; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Summary of getRequestLogsWithComments |
209
|
|
|
* @param int $requestId |
210
|
|
|
* @param PdoDatabase $db |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public static function getRequestLogsWithComments($requestId, PdoDatabase $db) |
214
|
|
|
{ |
215
|
|
|
$logs = self::getRequestLogs($requestId, $db); |
216
|
|
|
$comments = Comment::getForRequest($requestId, $db); |
217
|
|
|
|
218
|
|
|
$items = array_merge($logs, $comments); |
|
|
|
|
219
|
|
|
|
220
|
|
|
$sortKey = function(DataObject $item) |
221
|
|
|
{ |
222
|
|
|
if ($item instanceof Log) { |
223
|
|
|
return $item->getTimestamp(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if ($item instanceof Comment) { |
227
|
|
|
return $item->getTime(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return 0; |
231
|
|
|
}; |
232
|
|
|
|
233
|
|
|
do { |
234
|
|
|
$flag = false; |
235
|
|
|
|
236
|
|
|
$loopLimit = (count($items) - 1); |
237
|
|
|
for ($i = 0; $i < $loopLimit; $i++) { |
238
|
|
|
// are these two items out of order? |
239
|
|
|
if (strtotime($sortKey($items[$i])) > strtotime($sortKey($items[$i + 1]))) { |
240
|
|
|
// swap them |
241
|
|
|
$swap = $items[$i]; |
242
|
|
|
$items[$i] = $items[$i + 1]; |
243
|
|
|
$items[$i + 1] = $swap; |
244
|
|
|
|
245
|
|
|
// set a flag to say we've modified the array this time around |
246
|
|
|
$flag = true; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
while ($flag); |
251
|
|
|
|
252
|
|
|
return $items; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Summary of getLogDescription |
257
|
|
|
* @param Log $entry |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
public static function getLogDescription(Log $entry) |
261
|
|
|
{ |
262
|
|
|
$text = "Deferred to "; |
263
|
|
|
if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
264
|
|
|
// Deferred to a different queue |
265
|
|
|
// This is exactly what we want to display. |
266
|
|
|
return $entry->getAction(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$text = "Closed custom-n"; |
270
|
|
|
if ($entry->getAction() == $text) { |
271
|
|
|
// Custom-closed |
272
|
|
|
return "closed (custom reason - account not created)"; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$text = "Closed custom-y"; |
276
|
|
|
if ($entry->getAction() == $text) { |
277
|
|
|
// Custom-closed |
278
|
|
|
return "closed (custom reason - account created)"; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$text = "Closed 0"; |
282
|
|
|
if ($entry->getAction() == $text) { |
283
|
|
|
// Dropped the request - short-circuit the lookup |
284
|
|
|
return "dropped request"; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$text = "Closed "; |
288
|
|
|
if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
289
|
|
|
// Closed with a reason - do a lookup here. |
290
|
|
|
$id = substr($entry->getAction(), strlen($text)); |
291
|
|
|
/** @var EmailTemplate $template */ |
292
|
|
|
$template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
293
|
|
|
|
294
|
|
|
if ($template != false) { |
|
|
|
|
295
|
|
|
return "closed (" . $template->getName() . ")"; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// Fall back to the basic stuff |
301
|
|
|
$lookup = array( |
302
|
|
|
'Reserved' => 'reserved', |
303
|
|
|
'Email Confirmed' => 'email-confirmed', |
304
|
|
|
'Unreserved' => 'unreserved', |
305
|
|
|
'Approved' => 'approved', |
306
|
|
|
'Suspended' => 'suspended', |
307
|
|
|
'Banned' => 'banned', |
308
|
|
|
'Edited' => 'edited interface message', |
309
|
|
|
'Declined' => 'declined', |
310
|
|
|
'EditComment-c' => 'edited a comment', |
311
|
|
|
'EditComment-r' => 'edited a comment', |
312
|
|
|
'Unbanned' => 'unbanned', |
313
|
|
|
'Promoted' => 'promoted to tool admin', |
314
|
|
|
'BreakReserve' => 'forcibly broke the reservation', |
315
|
|
|
'Prefchange' => 'changed user preferences', |
316
|
|
|
'Renamed' => 'renamed', |
317
|
|
|
'Demoted' => 'demoted from tool admin', |
318
|
|
|
'ReceiveReserved' => 'received the reservation', |
319
|
|
|
'SendReserved' => 'sent the reservation', |
320
|
|
|
'EditedEmail' => 'edited email', |
321
|
|
|
'DeletedTemplate' => 'deleted template', |
322
|
|
|
'EditedTemplate' => 'edited template', |
323
|
|
|
'CreatedEmail' => 'created email', |
324
|
|
|
'CreatedTemplate' => 'created template', |
325
|
|
|
'SentMail' => 'sent an email to the requestor', |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
if (array_key_exists($entry->getAction(), $lookup)) { |
329
|
|
|
return $lookup[$entry->getAction()]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// OK, I don't know what this is. Fall back to something sane. |
333
|
|
|
return "performed an unknown action ({$entry->getAction()})"; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Summary of getLogs |
338
|
|
|
* @param mixed $userFilter |
339
|
|
|
* @param mixed $actionFilter |
340
|
|
|
* @param integer $limit |
341
|
|
|
* @param integer $offset |
342
|
|
|
* @return bool|array<string,string> |
343
|
|
|
*/ |
344
|
|
|
public static function getLogs($userFilter, $actionFilter, $limit = 100, $offset = 0) |
345
|
|
|
{ |
346
|
|
|
$database = gGetDb(); |
347
|
|
|
|
348
|
|
|
$whereClause = "(:userFilter = 0 OR user = :userid) AND (:actionFilter = 0 OR action = :action)"; |
349
|
|
|
$searchSqlStatement = "SELECT * FROM log WHERE $whereClause ORDER BY timestamp DESC LIMIT :limit OFFSET :offset;"; |
350
|
|
|
$countSqlStatement = "SELECT COUNT(1) FROM log WHERE $whereClause;"; |
351
|
|
|
|
352
|
|
|
$searchStatement = $database->prepare($searchSqlStatement); |
353
|
|
|
$countStatement = $database->prepare($countSqlStatement); |
354
|
|
|
|
355
|
|
|
$searchStatement->bindValue(":limit", $limit, PDO::PARAM_INT); |
356
|
|
|
$searchStatement->bindValue(":offset", $offset, PDO::PARAM_INT); |
357
|
|
|
|
358
|
|
|
if ($userFilter === false) { |
359
|
|
|
$searchStatement->bindValue(":userFilter", 0, PDO::PARAM_INT); |
360
|
|
|
$countStatement->bindValue(":userFilter", 0, PDO::PARAM_INT); |
361
|
|
|
$searchStatement->bindValue(":userid", 0, PDO::PARAM_INT); |
362
|
|
|
$countStatement->bindValue(":userid", 0, PDO::PARAM_INT); |
363
|
|
|
} |
364
|
|
|
else { |
365
|
|
|
$searchStatement->bindValue(":userFilter", 1, PDO::PARAM_INT); |
366
|
|
|
$countStatement->bindValue(":userFilter", 1, PDO::PARAM_INT); |
367
|
|
|
$searchStatement->bindValue(":userid", User::getByUsername($userFilter, $database)->getId(), PDO::PARAM_INT); |
368
|
|
|
$countStatement->bindValue(":userid", User::getByUsername($userFilter, $database)->getId(), PDO::PARAM_INT); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if ($actionFilter === false) { |
372
|
|
|
$searchStatement->bindValue(":actionFilter", 0, PDO::PARAM_INT); |
373
|
|
|
$countStatement->bindValue(":actionFilter", 0, PDO::PARAM_INT); |
374
|
|
|
$searchStatement->bindValue(":action", "", PDO::PARAM_STR); |
375
|
|
|
$countStatement->bindValue(":action", "", PDO::PARAM_STR); |
376
|
|
|
} |
377
|
|
|
else { |
378
|
|
|
$searchStatement->bindValue(":actionFilter", 1, PDO::PARAM_INT); |
379
|
|
|
$countStatement->bindValue(":actionFilter", 1, PDO::PARAM_INT); |
380
|
|
|
$searchStatement->bindValue(":action", $actionFilter, PDO::PARAM_STR); |
381
|
|
|
$countStatement->bindValue(":action", $actionFilter, PDO::PARAM_STR); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
if (!$countStatement->execute()) { |
385
|
|
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$count = $countStatement->fetchColumn(0); |
389
|
|
|
$countStatement->closeCursor(); |
390
|
|
|
|
391
|
|
|
if ($searchStatement->execute()) { |
392
|
|
|
$data = $searchStatement->fetchAll(PDO::FETCH_CLASS, "Log"); |
393
|
|
|
|
394
|
|
|
/** @var Log $entry */ |
395
|
|
|
foreach ($data as $entry) { |
396
|
|
|
$entry->setDatabase($database); |
397
|
|
|
$entry->isNew = false; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
$data['count'] = $count; |
401
|
|
|
|
402
|
|
|
return $data; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
return false; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Summary of getLogActions |
410
|
|
|
*/ |
411
|
|
|
public static function getLogActions() |
412
|
|
|
{ |
413
|
|
|
$database = gGetDb(); |
414
|
|
|
|
415
|
|
|
$lookup = array( |
416
|
|
|
'Reserved' => 'reserved', |
417
|
|
|
'Email Confirmed' => 'email-confirmed', |
418
|
|
|
'Unreserved' => 'unreserved', |
419
|
|
|
'Approved' => 'approved', |
420
|
|
|
'Suspended' => 'suspended', |
421
|
|
|
'Banned' => 'banned', |
422
|
|
|
'Edited' => 'edited interface message', |
423
|
|
|
'Declined' => 'declined', |
424
|
|
|
'EditComment-c' => 'edited a comment', |
425
|
|
|
'EditComment-r' => 'edited a comment', |
426
|
|
|
'Unbanned' => 'unbanned', |
427
|
|
|
'Promoted' => 'promoted to tool admin', |
428
|
|
|
'BreakReserve' => 'forcibly broke the reservation', |
429
|
|
|
'Prefchange' => 'changed user preferences', |
430
|
|
|
'Renamed' => 'renamed', |
431
|
|
|
'Demoted' => 'demoted from tool admin', |
432
|
|
|
'ReceiveReserved' => 'received the reservation', |
433
|
|
|
'SendReserved' => 'sent the reservation', |
434
|
|
|
'EditedEmail' => 'edited email', |
435
|
|
|
'DeletedTemplate' => 'deleted template', |
436
|
|
|
'EditedTemplate' => 'edited template', |
437
|
|
|
'CreatedEmail' => 'created email', |
438
|
|
|
'CreatedTemplate' => 'created template', |
439
|
|
|
'SentMail' => 'sent an email to the requestor', |
440
|
|
|
); |
441
|
|
|
|
442
|
|
|
$statement = $database->query("SELECT CONCAT('Closed ', id) as k, CONCAT('closed (',name,')') as v FROM emailtemplate;"); |
443
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
444
|
|
|
$lookup[$row['k']] = $row['v']; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $lookup; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
#endregion |
451
|
|
|
} |
452
|
|
|
|