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