File: LogController.php

package info (click to toggle)
icingaweb2-module-audit 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: php: 270; sh: 54; makefile: 15
file content (76 lines) | stat: -rw-r--r-- 2,635 bytes parent folder | download
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
<?php

/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Audit\Controllers;

use DateTime;
use DateTimeZone;
use Icinga\Data\ConfigObject;
use Icinga\Protocol\File\FileReader;
use ipl\Html\Html;
use ipl\Html\HtmlString;
use ipl\Html\Table;
use ipl\Web\Compat\CompatController;
use ipl\Web\Widget\EmptyStateBar;

class LogController extends CompatController
{
    public function indexAction(): void
    {
        $this->assertPermission('audit/log');

        if ($this->Config()->get('log', 'type') !== 'file') {
            $this->httpNotFound('Page not found');
        }

        $this->addTitleTab($this->translate('Audit Log'));

        $file = $this->Config()->get('log', 'path', '/var/log/icingaweb2/audit.log');

        if (! @file_exists($file)) {
            $this->addContent(new EmptyStateBar($this->translate('No activity has been recorded yet.')));

            return;
        }

        $resource = new FileReader(new ConfigObject([
            'filename'  => $file,
            'fields'    => '/(?<!.)' // ^ can't handle multilines, don't ask *me* why this works
                . '(?<datetime>[0-9]{4}(?:-[0-9]{2}){2}'                    // date
                . 'T[0-9]{2}(?::[0-9]{2}){2}(?:[\+\-][0-9]{2}:[0-9]{2})?)'  // time
                . ' - (?<identity>.+)'                                      // identity
                . ' - (?<type>.+)'                                          // type
                . ' - (?<message>.+)'                                       // message
                . '(?!.)/msSU' // $ can't handle multilines, don't ...
        ]));

        $query = $resource->select()->order('DESC');

        $this->setupPaginationControl($query);
        $this->addControl(HtmlString::create((string) $this->view->paginator));

        $this->setupLimitControl();
        $this->addControl(Html::tag(
            'div',
            ['class' => 'sort-controls-container'],
            HtmlString::create((string) $this->view->limiter)
        ));

        $table = new Table();
        $table->addAttributes(['class' => 'action']);

        /** @var object{datetime: string, type: string, identity: string, message: string} $row */
        foreach ($query as $row) {
            $time = new DateTime($row->datetime);
            $time->setTimezone(new DateTimeZone(date_default_timezone_get()));
            $table->add([
                [$time->format('d.m. H:i'), Html::tag('br'), $row->type],
                $row->identity,
                nl2br(trim($row->message), false),
            ]);
        }

        $this->addContent($table);
    }
}