Passed
Push — master ( 9f95b4...db9e7a )
by Tim
07:37
created

logFilter()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 3
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 9.5555
1
<?php
2
3
/**
4
 * @param \SimpleSAML\Module\logpeek\File\ReverseRead $objFile
5
 * @param string $tag
6
 * @param int $cut
7
 * @return array
8
 */
9
function logFilter(\SimpleSAML\Module\logpeek\File\ReverseRead $objFile, string $tag, int $cut): array
10
{
11
    if (!preg_match('/^[a-f0-9]{10}$/D', $tag)) {
12
        throw new Exception('Invalid search tag');
13
    }
14
    $i = 0;
15
    $results = [];
16
    $line = $objFile->getPreviousLine();
17
    while ($line !== false && ($i++ < $cut)) {
18
        if (strstr($line, '[' . $tag . ']')) {
19
            $results[] = $line;
20
        }
21
        $line = $objFile->getPreviousLine();
22
    }
23
    $results[] = 'Searched ' . $i . ' lines backward. ' . count($results) . ' lines found.';
24
    $results = array_reverse($results);
25
    return $results;
26
}
27
28
29
$config = \SimpleSAML\Configuration::getInstance();
30
$session = \SimpleSAML\Session::getSessionFromRequest();
31
32
\SimpleSAML\Utils\Auth::requireAdmin();
0 ignored issues
show
Bug Best Practice introduced by
The method SimpleSAML\Utils\Auth::requireAdmin() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
\SimpleSAML\Utils\Auth::/** @scrutinizer ignore-call */ 
33
                        requireAdmin();
Loading history...
33
34
$logpeekconfig = \SimpleSAML\Configuration::getConfig('module_logpeek.php');
35
$logfile = $logpeekconfig->getValue('logfile', '/var/simplesamlphp.log');
36
$blockSize = $logpeekconfig->getValue('blocksz', 8192);
37
38
$myLog = new \SimpleSAML\Module\logpeek\File\ReverseRead($logfile, $blockSize);
39
40
41
$results = [];
42
if (isset($_REQUEST['tag'])) {
43
    $results = logFilter($myLog, $_REQUEST['tag'], $logpeekconfig->getValue('lines', 500));
44
}
45
46
47
$fileModYear = intval(date("Y", $myLog->getFileMtime()));
48
$firstLine = $myLog->getFirstLine();
49
$firstTimeEpoch = \SimpleSAML\Module\logpeek\Syslog\ParseLine::getUnixTime($firstLine ?: '', $fileModYear);
50
$lastLine = $myLog->getLastLine();
51
$lastTimeEpoch = \SimpleSAML\Module\logpeek\Syslog\ParseLine::getUnixTime($lastLine ?: '', $fileModYear);
52
$fileSize = $myLog->getFileSize();
53
54
$t = new \SimpleSAML\XHTML\Template($config, 'logpeek:logpeek.twig');
55
$t->data['results'] = $results;
56
$t->data['trackid'] = $session->getTrackID();
57
$t->data['timestart'] = date(DATE_RFC822, $firstTimeEpoch ?: time());
58
$t->data['endtime'] = date(DATE_RFC822, $lastTimeEpoch ?: time());
59
$t->data['filesize'] = $fileSize;
60
61
$t->send();
62