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(); |
|
|
|
|
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
|
|
|
|