forked from matomo-org/plugin-VisitorGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogParser.php
80 lines (70 loc) · 1.77 KB
/
LogParser.php
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\VisitorGenerator;
class LogParser
{
private $file = array();
/**
* An absoulte path to a log file that should be parsed.
*
* @param string $file
*/
public function __construct($file)
{
$this->file = $file;
}
/**
* Get the raw log lines of all files. Will contain even empty lines and comments
*
* @return string[]
*/
public function getLogLines()
{
$logs = file($this->file);
return $logs;
}
/**
* Get all log lines separated into ip, time, url, referrer and user agent. Empty lines and comments won't be
* returned.
*
* @return array[]
*/
public function getParsedLogLines()
{
$parsedLines = array();
$lines = $this->getLogLines();
foreach ($lines as $line) {
$parsed = self::parseLogLine($line);
if (!empty($parsed)) {
$parsedLines[] = $parsed;
}
}
return $parsedLines;
}
/**
* Parses a single raw log line into ip, time, url, referrer and user agent. Returns an empty array if it is not a
* valid log line.
*
* @param string $log
* @return array
*/
public static function parseLogLine($log)
{
if (!preg_match('/^(\S+) \S+ \S+ \[(.*?)\] "GET (\S+.*?)" \d+ \d+ "(.*?)" "(.*?)"/', $log, $m)) {
return array();
}
return array(
'ip' => $m[1],
'time' => $m[2],
'url' => $m[3],
'referrer' => $m[4],
'ua' => $m[5],
);
}
}