1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\LogsAnalyzer; |
4
|
|
|
|
5
|
|
|
class LogLine |
6
|
|
|
{ |
7
|
|
|
protected $ip; |
8
|
|
|
protected $logname; |
9
|
|
|
protected $user; |
10
|
|
|
protected $date; |
11
|
|
|
protected $time; |
12
|
|
|
protected $domain; |
13
|
|
|
protected $method; |
14
|
|
|
protected $url; |
15
|
|
|
protected $httpVersion; |
16
|
|
|
protected $status; |
17
|
|
|
protected $responseBytes; |
18
|
|
|
protected $referer; |
19
|
|
|
protected $useragent; |
20
|
|
|
|
21
|
2 |
|
public function __construct(?string $domain = null) |
22
|
|
|
{ |
23
|
2 |
|
$this->domain = $domain; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
public function getKeys() |
27
|
|
|
{ |
28
|
|
|
return array_keys(get_object_vars($this)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function get() |
32
|
|
|
{ |
33
|
|
|
return array_values(get_object_vars($this)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getIp() |
37
|
|
|
{ |
38
|
|
|
return $this->ip; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public function setHost($ip) |
42
|
|
|
{ |
43
|
2 |
|
$this->setIp($ip); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
2 |
|
public function setIp($ip) |
47
|
|
|
{ |
48
|
2 |
|
$this->ip = $ip; |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
public function getLogname() |
52
|
|
|
{ |
53
|
|
|
return $this->logname; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function setLogname($logname) |
57
|
|
|
{ |
58
|
2 |
|
if ('-' != $logname) { |
59
|
|
|
$this->logname = $logname; |
60
|
|
|
} |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
public function getUser() |
64
|
|
|
{ |
65
|
|
|
return $this->user; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function setUser($user) |
69
|
|
|
{ |
70
|
2 |
|
if ('-' != $user) { |
71
|
|
|
$this->user = $user; |
72
|
|
|
} |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
public function getTime() |
76
|
|
|
{ |
77
|
|
|
return $this->time; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function setTime($time) |
81
|
|
|
{ |
82
|
2 |
|
list($date, $time) = explode(':', $time, 2); |
83
|
2 |
|
$this->time = $time; |
84
|
|
|
|
85
|
2 |
|
$date = \DateTime::createFromFormat('j/M/Y', $date); |
86
|
2 |
|
$this->date = $date->format('Ymd'); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
public function getDomain() |
90
|
|
|
{ |
91
|
|
|
return $this->domain; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setDomain($domain) |
95
|
|
|
{ |
96
|
|
|
$this->domain = $domain; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getRequest() |
100
|
|
|
{ |
101
|
|
|
return $this->request; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function setMethod($method) |
105
|
|
|
{ |
106
|
2 |
|
$this->method = $method; |
107
|
2 |
|
} |
108
|
|
|
|
109
|
2 |
|
public function setUrl($url) |
110
|
|
|
{ |
111
|
2 |
|
$this->url = $url; |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
public function setRequest($request) |
115
|
|
|
{ |
116
|
2 |
|
$request = str_replace(['http://'.$this->domain.'/', 'https://'.$this->domain.'/'], '/', $request); |
117
|
2 |
|
list($method, $url, $httpVersion) = explode(' ', $request); |
118
|
2 |
|
$this->setMethod($method); |
119
|
2 |
|
$this->setUrl($url); |
120
|
2 |
|
$this->setHttpVersion($httpVersion); |
121
|
2 |
|
} |
122
|
|
|
|
123
|
|
|
public function getHttpVersion() |
124
|
|
|
{ |
125
|
|
|
return $this->httpVersion; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
public function setHttpVersion($httpVersion) |
129
|
|
|
{ |
130
|
2 |
|
$this->httpVersion = $httpVersion; |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
public function getStatus() |
134
|
|
|
{ |
135
|
|
|
return $this->status; |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function setStatus($status) |
139
|
|
|
{ |
140
|
2 |
|
$this->status = $status; |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
public function getResponseBytes() |
144
|
|
|
{ |
145
|
|
|
return $this->responseBytes; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
public function setResponseBytes($responseBytes) |
149
|
|
|
{ |
150
|
2 |
|
$this->responseBytes = $responseBytes; |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
public function getReferer() |
154
|
|
|
{ |
155
|
|
|
return $this->referer; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
protected function isInternalReferer($referer) |
159
|
|
|
{ |
160
|
2 |
|
return 0 === strpos($referer, 'http://'.$this->domain) || 0 === strpos($referer, 'https://'.$this->domain); |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
public function setHeaderReferer($referer) |
164
|
|
|
{ |
165
|
2 |
|
if (!empty($referer) && '-' != $referer) { |
166
|
2 |
|
$this->setReferer($referer); |
167
|
|
|
} |
168
|
2 |
|
} |
169
|
|
|
|
170
|
2 |
|
public function setReferer($referer) |
171
|
|
|
{ |
172
|
2 |
|
$referer = $this->isInternalReferer($referer) ? null : $referer; |
173
|
|
|
|
174
|
2 |
|
$this->referer = $referer; |
175
|
2 |
|
} |
176
|
|
|
|
177
|
2 |
|
public function getUseragent() |
178
|
|
|
{ |
179
|
2 |
|
return $this->useragent; |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
public function setHeaderUserAgent($useragent) |
183
|
|
|
{ |
184
|
2 |
|
$this->setUserAgent($useragent); |
185
|
2 |
|
} |
186
|
|
|
|
187
|
2 |
|
public function setUseragent($useragent) |
188
|
|
|
{ |
189
|
2 |
|
$this->useragent = $useragent; |
190
|
2 |
|
} |
191
|
|
|
|
192
|
|
|
public function ipAdressNumber($dotted) |
193
|
|
|
{ |
194
|
|
|
$dotted = preg_split('/[.]+/', $dotted); |
195
|
|
|
|
196
|
|
|
return (float) ($dotted[0] * 16777216) + ($dotted[1] * 65536) + ($dotted[2] * 256) + ($dotted[3]); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|