1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
<?php
/**
* LogEvent
* model class for table log_events
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Peter Thienel <thienel@data-quest.de>
* @copyright 2013 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 3.0
* @property string event_id database column
* @property string id alias column for event_id
* @property string user_id database column
* @property string action_id database column
* @property string affected_range_id database column
* @property string coaffected_range_id database column
* @property string info database column
* @property string dbg_info database column
* @property string mkdate database column
* @property LogAction action belongs_to LogAction
* @property User user belongs_to User
*/
class LogEvent extends SimpleORMap implements PrivacyObject
{
protected static function configure($config = [])
{
$config['db_table'] = 'log_events';
$config['belongs_to']['action'] = [
'class_name' => LogAction::class,
'foreign_key' => 'action_id',
];
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id',
];
parent::configure($config);
}
protected $formatted_text = '';
/**
* Returns the number of log events counted by actions as an array where
* the ey is the action id and the value is the number of events for
* this action.
*
* @return array Number of loge events for all actions
*/
public static function countByActions()
{
$query = "SELECT action_id, COUNT(*) FROM log_events GROUP BY action_id";
$statement = DBManager::get()->query($query);
return $statement->fetchGrouped(PDO::FETCH_COLUMN);
}
/**
* Deletes all expired events.
*
* @return int Number of deleted events.
*/
public static function deleteExpired()
{
$sql = "DELETE log_events
FROM log_events
JOIN log_actions USING(action_id)
WHERE log_actions.expires > 0
AND log_events.mkdate + log_actions.expires < UNIX_TIMESTAMP()";
return DBManager::get()->exec($sql);
}
/**
* Returns the formatted log event. Fills the action template with data
* of this event.
*
* @return string The formatted log event.
*/
public function formatEvent()
{
$text = $this->formatObject();
$patterns = [
'/%affected/',
'/%coaffected/',
'/%info/',
'/%dbg_info/'
];
$replacements = [
$this->affected_range_id,
$this->coaffected_range_id,
htmlReady($this->info),
htmlReady($this->dbg_info)
];
$replace_callbacks = [
'/%sem\(%affected\)/',
'/%sem\(%coaffected\)/',
'/%studyarea\(%affected\)/',
'/%studyarea\(%coaffected\)/',
'/%res\(%affected\)/',
'/%res\(%coaffected\)/',
'/%inst\(%affected\)/',
'/%inst\(%coaffected\)/',
'/%user\(%affected\)/',
'/%user\(%coaffected\)/',
'/%user/',
'/%singledate\(%affected\)/',
'/%semester\(%coaffected\)/',
'/%plugin\(%coaffected\)/',
'/%group\(%coaffected\)/',
];
$text = preg_replace_callback($replace_callbacks, [$this, 'formatCallback'], $text);
return preg_replace($patterns, $replacements, $text);
}
/**
* @param $m
* @return string
*/
protected function formatCallback($m)
{
$map = [
'sem' => 'Seminar',
'res' => 'Resource',
'inst' => 'Institute',
'user' => 'Username',
'group' => 'Statusgruppe'
];
$ret = '';
if (preg_match_all('/%([a-z]+)/', $m[0], $found)) {
if (isset($found[1][0])) {
$funcname = 'format' . (isset($map[$found[1][0]]) ? $map[$found[1][0]] : $found[1][0]);
if (isset($found[1][1])) {
$param = $found[1][1] . '_range_id';
} else {
$param = 'user_id';
}
if (is_callable([$this, $funcname])) {
$ret = call_user_func([$this, $funcname], $param);
}
}
}
return $ret;
}
/**
* Returns the name of the resource for the resource id found in the
* given field or the resource id if the resource is unknown.
*
* @param string $field The name of the table field.
* @return string The name of the resource or resource id.
*/
protected function formatResource($field)
{
$resource = Resource::find($this->$field);
if ($resource && $resource->name) {
return sprintf(
'<a href="%s">%s</a>',
Resource::getLinkForAction('show', $resource->id),
htmlReady($resource->name)
);
}
return $this->$field;
}
/**
* Returns the name of the user with the id found in the given field.
*
* @param string $field The name of the table field.
* @return string The name of the user.
*/
protected function formatUsername($field)
{
return '<a href="' . URLHelper::getLink('dispatch.php/admin/user/edit/'
. $this->$field) . '">' . htmlReady(get_fullname($this->$field))
. '</a>';
}
/**
* Returns the name of the seminar for the id found in the given
* field or the id if the seminar is unknown.
*
* @param string $field The name of the table field.
* @return string The name of seminar or the id.
*/
protected function formatSeminar($field)
{
$course = Course::find($this->$field);
if (!$course) {
return $this->$field;
}
return sprintf(
'<a href="%s">%s %s (%s)</a>',
URLHelper::getLink(
'dispatch.php/course/details',
['sem_id' => $course->getId()]
),
htmlReady($course->VeranstaltungsNummer),
htmlReady(my_substr($course->name, 0, 100)),
htmlReady($course->start_semester->name)
);
}
/**
* Returns the name of the institute for the id found in the given
* field or the id if the institute is unknown.
*
* @param string $field The name of the table field.
* @return string The name of institute or the id.
*/
protected function formatInstitute($field)
{
$institute = Institute::find($this->$field);
if (!$institute) {
return $this->$field;
}
return sprintf(
'<a href="%s">%s</a>',
URLHelper::getLink(
'dispatch.php/institute/overview',
['auswahl' => $institute->getId()]
),
htmlReady(my_substr($institute->name, 0, 100))
);
}
/**
* Returns the name of the study area for the id found in the given
* field or the id if the study area is unknown.
*
* @param string $field The name of the table field.
* @return string The name of seminar or the id.
*/
protected function formatStudyarea($field)
{
$study_area = StudipStudyArea::find($this->$field);
if (!$study_area) {
return $this->$field;
}
return '<em>' . htmlReady($study_area->getPath(' > ')) . '</em>';
}
/**
* Returns the singledate for the id found in the given field.
*
* @param string $field The name of the table field.
* @return string The singledate.
*/
protected function formatSingledate($field)
{
$termin = new SingleDate($this->$field);
return '<em>' . $termin->toString() . '</em>';
}
/**
* Returns the name of the plugin for the id found in the given
* field or the id if the plugin is unknown.
*
* @param string $field The name of the table field.
* @return string The name of plugin or the id.
*/
protected function formatPlugin($field)
{
$plugin_manager = PluginManager::getInstance();
$plugin_info = $plugin_manager->getPluginInfoById($this->$field);
return $plugin_info ? '<em>' . htmlReady($plugin_info['name']) . '</em>' : $this->$field;
}
/**
* Returns the name of the semester for the id found in the given
* field or the id if the seminar is unknown.
*
* @param string $field The name of the table field.
* @return string The name of semester or the id.
*/
protected function formatSemester($field)
{
$all_semester = Semester::findAllVisible(false);
foreach ($all_semester as $val) {
if (!empty($val['beginn']) && ($val['beginn'] == $this->$field)) {
return '<em>' . htmlReady($val['name']) . '</em>';
}
}
return htmlReady($this->$field);
}
/**
* Returns the name of the statusgroup for the id found in the given
* field or the id if the group is unknown.
*
* @param string $field The name of the table field.
* @return string The name of statusgruppe or the id.
*/
protected function formatStatusgruppe($field)
{
$group = Statusgruppen::find($this->$field);
if (!$group) {
return $this->$field;
}
$course = Course::find($group->range_id);
return sprintf(
'<a href="%s">%s</a>',
URLHelper::getLink('dispatch.php/course/statusgroups', [
'contentbox_open' => $group->getId()
]),
htmlReady($group->name. ($course ? " (VA: ".$course->name.")" : ""))
);
}
protected function formatObject()
{
if ($this->action) {
switch ($this->action->type) {
case 'plugin':
$plugin_manager = PluginManager::getInstance();
$plugin_info = $plugin_manager->getPluginInfo($this->action->class);
$class_name = $plugin_info['class'];
$plugin = $plugin_manager->getPlugin($class_name);
if ($plugin instanceof Loggable) {
return $class_name::logFormat($this);
}
break;
case 'file':
if (file_exists($this->action->filename)) {
require_once $this->action->filename;
$class_name = $this->action->class;
if ($class_name && in_array(Loggable::class, class_implements($class_name))) {
return $class_name::logFormat($this);
}
}
break;
case 'core':
$class_name = $this->action->class;
if ($class_name && in_array(Loggable::class, class_implements($class_name))) {
return $class_name::logFormat($this);
}
break;
}
}
return $this->action->info_template;
}
/**
* Export available data of a given user into a storage object
* (an instance of the StoredUserData class) for that user.
*
* @param StoredUserData $storage object to store data into
*/
public static function exportUserData(StoredUserData $storage)
{
$user_id = $storage->user_id;
$templates = [];
$query = "SELECT *
FROM log_events
WHERE :user_id IN (user_id, affected_range_id, coaffected_range_id)";
$log = DBManager::get()->fetchAll($query, [':user_id' => $user_id]);
foreach ($log as $pos => $event) {
if (!array_key_exists($event['action_id'], $templates)) {
$log_action = LogAction::find($event["action_id"]);
$templates[$event['action_id']] = $log_action->info_template;
}
$template = $templates[$event['action_id']];
$log_event = LogEvent::find($event['event_id']);
// anonymize
$was_censored = false;
if ($event['user_id'] && substr_count($template, '%user ') && $event['user_id'] !== $user_id) {
$event['user_id'] = preg_replace('/[^w]/', '#', $event['user_id']);
$log_event->user_id = $event['user_id'];
$was_censored = true;
}
if ($event['affected_range_id'] && substr_count($template, '%user(%affected)') && $event['affected_range_id'] !== $user_id) {
$event['affected_range_id'] = preg_replace('/[^w]/', '#', $event['affected_range_id']);
$log_event->affected_range_id = $event['affected_range_id'];
$was_censored = true;
}
if ($event['coaffected_range_id'] && substr_count($template, '%user(%coaffected)') && $event['coaffected_range_id'] !== $user_id) {
$event['coaffected_range_id'] = preg_replace('/[^w]/', '#', $event['coaffected_range_id']);
$log_event->coaffected_range_id = $event['coaffected_range_id'];
$was_censored = true;
}
//censore possible info
if ($was_censored) {
if (!empty($event['info'])) {
$event['info'] = preg_replace('/[^w]/', '#', $event['info']);
}
if (!empty($event["dbg_info"])) {
$event['dbg_info'] = preg_replace('/[^w]/', '#', $event['dbg_info']);
}
}
$a['readable_entry'] = html_entity_decode(strip_tags(str_replace('<br>', PHP_EOL, $log_event->formatEvent())));
$log[$pos]= array_merge($a, $event);
}
if ($log) {
$storage->addTabularData(_('Logs'), 'log_events', $log);
}
}
}
|