1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\statistics; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package SimpleSAMLphp |
9
|
|
|
*/ |
10
|
|
|
class LogParser |
11
|
|
|
{ |
12
|
|
|
/** @var integer */ |
13
|
|
|
private int $datestart; |
14
|
|
|
|
15
|
|
|
/** @var integer */ |
16
|
|
|
private int $datelength; |
17
|
|
|
|
18
|
|
|
/** @var integer */ |
19
|
|
|
private int $offset; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor |
24
|
|
|
* |
25
|
|
|
* @param integer $datestart At which char is the date starting |
26
|
|
|
* @param integer $datelength How many characters is the date (on the b |
27
|
|
|
* @param integer $offset At which char is the rest of the entries starting |
28
|
|
|
*/ |
29
|
|
|
public function __construct(int $datestart, int $datelength, int $offset) |
30
|
|
|
{ |
31
|
|
|
$this->datestart = $datestart; |
32
|
|
|
$this->datelength = $datelength; |
33
|
|
|
$this->offset = $offset; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $line |
39
|
|
|
* |
40
|
|
|
* @return integer |
41
|
|
|
*/ |
42
|
|
|
public function parseEpoch(string $line): int |
43
|
|
|
{ |
44
|
|
|
$epoch = strtotime(substr($line, 0, $this->datelength)); |
45
|
|
|
if ($epoch > time() + 2678400) { // 60 * 60 * 24 * 31 = 2678400 |
46
|
|
|
/** |
47
|
|
|
* More than a month in the future - probably caused by |
48
|
|
|
* the log files missing the year. |
49
|
|
|
* We will therefore subtrackt one year. |
50
|
|
|
*/ |
51
|
|
|
$hour = intval(gmdate('H', $epoch)); |
52
|
|
|
$minute = intval(gmdate('i', $epoch)); |
53
|
|
|
$second = intval(gmdate('s', $epoch)); |
54
|
|
|
$month = intval(gmdate('n', $epoch)); |
55
|
|
|
$day = intval(gmdate('j', $epoch)); |
56
|
|
|
$year = intval(gmdate('Y', $epoch)) - 1; |
57
|
|
|
$epoch = gmmktime($hour, $minute, $second, $month, $day, $year); |
58
|
|
|
} |
59
|
|
|
return intval($epoch); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $line |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function parseContent(string $line): array |
69
|
|
|
{ |
70
|
|
|
$contentstr = substr($line, $this->offset); |
71
|
|
|
$content = explode(' ', $contentstr); |
72
|
|
|
return $content; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|