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
|
|
|
public function getRequest() |
101
|
|
|
{ |
102
|
|
|
return $this->request; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
public function setMethod($method) |
106
|
|
|
{ |
107
|
3 |
|
$this->method = $method; |
108
|
3 |
|
} |
109
|
|
|
|
110
|
|
|
public function getMethod() |
111
|
|
|
{ |
112
|
|
|
return $this->method; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function setUrl($url) |
116
|
|
|
{ |
117
|
3 |
|
$this->url = $url; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
public function getUrl() |
121
|
|
|
{ |
122
|
|
|
return $this->url; |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
public function setRequest($request) |
126
|
|
|
{ |
127
|
3 |
|
$request = str_replace(['http://'.$this->domain.'/', 'https://'.$this->domain.'/'], '/', $request); |
128
|
3 |
|
list($method, $url, $httpVersion) = explode(' ', $request); |
129
|
3 |
|
$this->setMethod($method); |
130
|
3 |
|
$this->setUrl($url); |
131
|
3 |
|
$this->setHttpVersion($httpVersion); |
132
|
3 |
|
} |
133
|
|
|
|
134
|
|
|
public function getHttpVersion() |
135
|
|
|
{ |
136
|
|
|
return $this->httpVersion; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
public function setHttpVersion($httpVersion) |
140
|
|
|
{ |
141
|
3 |
|
$this->httpVersion = $httpVersion; |
142
|
3 |
|
} |
143
|
|
|
|
144
|
|
|
public function getStatus() |
145
|
|
|
{ |
146
|
|
|
return $this->status; |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
public function setStatus($status) |
150
|
|
|
{ |
151
|
3 |
|
$this->status = $status; |
152
|
3 |
|
} |
153
|
|
|
|
154
|
|
|
public function getResponseBytes() |
155
|
|
|
{ |
156
|
|
|
return $this->responseBytes; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
public function setResponseBytes($responseBytes) |
160
|
|
|
{ |
161
|
3 |
|
$this->responseBytes = $responseBytes; |
162
|
3 |
|
} |
163
|
|
|
|
164
|
|
|
public function getReferer() |
165
|
|
|
{ |
166
|
|
|
return $this->referer; |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
protected function isInternalReferer($referer) |
170
|
|
|
{ |
171
|
3 |
|
return 0 === strpos($referer, 'http://'.$this->domain) || 0 === strpos($referer, 'https://'.$this->domain); |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
public function setHeaderReferer($referer) |
175
|
|
|
{ |
176
|
3 |
|
if (!empty($referer) && '-' != $referer) { |
177
|
3 |
|
$this->setReferer($referer); |
178
|
|
|
} |
179
|
3 |
|
} |
180
|
|
|
|
181
|
3 |
|
public function setReferer($referer) |
182
|
|
|
{ |
183
|
3 |
|
$referer = $this->isInternalReferer($referer) ? null : $referer; |
184
|
|
|
|
185
|
3 |
|
$this->referer = $referer; |
186
|
3 |
|
} |
187
|
|
|
|
188
|
3 |
|
public function getUseragent() |
189
|
|
|
{ |
190
|
3 |
|
return $this->useragent; |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
public function setHeaderUserAgent($useragent) |
194
|
|
|
{ |
195
|
3 |
|
$this->setUserAgent($useragent); |
196
|
3 |
|
} |
197
|
|
|
|
198
|
3 |
|
public function setUseragent($useragent) |
199
|
|
|
{ |
200
|
3 |
|
$this->useragent = $useragent; |
201
|
3 |
|
} |
202
|
|
|
|
203
|
|
|
public function getHit() |
204
|
|
|
{ |
205
|
|
|
return $this->hit ?? 1; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function incrementHit() |
209
|
|
|
{ |
210
|
|
|
$this->hit++; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function ipAdressNumber($dotted) |
214
|
|
|
{ |
215
|
|
|
$dotted = preg_split('/[.]+/', $dotted); |
216
|
|
|
|
217
|
|
|
return (float) ($dotted[0] * 16777216) + ($dotted[1] * 65536) + ($dotted[2] * 256) + ($dotted[3]); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|