-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebAnalyzer.php
313 lines (260 loc) · 9.51 KB
/
WebAnalyzer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
* @package Localzet WebAnalyzer library
* @link https://github.com/localzet/WebAnalyzer
*
* @author Ivan Zorin <[email protected]>
* @copyright Copyright (c) 2018-2024 Zorin Projects S.P.
* @license https://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For any questions, please contact <[email protected]>
*/
namespace localzet;
use localzet\WebAnalyzer\Analyser;
use localzet\WebAnalyzer\Cache;
use localzet\WebAnalyzer\Model;
use Psr\Cache\InvalidArgumentException;
class WebAnalyzer
{
use Cache;
use Analyser\Header,
Analyser\Derive,
Analyser\Corrections,
Analyser\Camouflage,
Analyser\Location,
Analyser\Network;
/**
* @var Model\Browser $browser Информация о браузере
*/
public Model\Browser $browser;
/**
* @var Model\Engine $engine Информация о движке рендеринга
*/
public Model\Engine $engine;
/**
* @var Model\Os $os Информация об операционной системе
*/
public Model\Os $os;
/**
* @var Model\Device $device Информация об устройстве
*/
public Model\Device $device;
/**
* @var Model\Location $location Информация о местоположении
*/
public Model\Location $location;
/**
* @var Model\Network $network Информация о сети
*/
public Model\Network $network;
/**
* @var bool $camouflage Является ли браузер замаскированным под другой браузер
*/
public bool $camouflage = false;
/**
* @var int[] $features Массив функций
*/
public array $features = [];
/**
* @var object $options Опции анализа
*/
public object $options;
/**
* @var array $headers Заголовки HTTP
*/
private array $headers = [];
/**
* Конструктор класса WebAnalyzer
*
* @param array|null $headers Заголовки для анализа
* @param array|null $options Опции ['cache', 'cacheExpires', 'detectBots']
* @throws InvalidArgumentException
*/
public function __construct(?array $headers = null, ?array $options = [])
{
$this->browser = new Model\Browser();
$this->engine = new Model\Engine();
$this->os = new Model\Os();
$this->device = new Model\Device();
$this->location = new Model\Location();
$this->network = new Model\Network();
if ($headers) {
$cache = $this->analyseWithCache($headers, $options);
if ($cache && $cache->isHit()) {
$this->applyCachedData($cache->get());
} else {
$this->headers = $headers;
$this->options = (object)$options;
$this
->analyseHeaders()
->analyseLocation()
->analyseNetwork()
->deriveInformation()
->applyCorrections()
->detectCamouflage()
->deriveDeviceSubType();
if ($cache) {
$this->cache->save(
$cache
->expiresAfter($this->expires)
->set($this->retrieveCachedData())
);
}
}
}
}
private function isX()
{
$arguments = func_get_args();
$x = $arguments[0];
if (count($arguments) < 2) {
return false;
}
if (empty($this->$x->name)) {
return false;
}
if ($this->$x->name != $arguments[1]) {
return false;
}
if (count($arguments) >= 4) {
if (empty($this->$x->version)) {
return false;
}
if (!$this->$x->version->is($arguments[2], $arguments[3])) {
return false;
}
}
return true;
}
public function isBrowser()
{
$arguments = func_get_args();
array_unshift($arguments, 'browser');
return call_user_func_array([$this, 'isX'], $arguments);
}
public function isEngine()
{
$arguments = func_get_args();
array_unshift($arguments, 'engine');
return call_user_func_array([$this, 'isX'], $arguments);
}
public function isOs()
{
$arguments = func_get_args();
array_unshift($arguments, 'os');
return call_user_func_array([$this, 'isX'], $arguments);
}
public function isDevice($model)
{
return (!empty($this->device->series) && $this->device->series == $model) || (!empty($this->device->model) && $this->device->model == $model);
}
public function getType()
{
return $this->device->type . (!empty($this->device->subtype) ? ':' . $this->device->subtype : '');
}
public function isType()
{
$arguments = func_get_args();
$count = count($arguments);
for ($a = 0; $a < $count; $a++) {
if (str_contains($arguments[$a], ':')) {
list($type, $subtype) = explode(':', $arguments[$a]);
if ($type == $this->device->type && $subtype == $this->device->subtype) {
return true;
}
} else {
if ($arguments[$a] == $this->device->type) {
return true;
}
}
}
return false;
}
public function isMobile()
{
return $this->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable');
}
public function isDetected()
{
return $this->browser->isDetected() || $this->os->isDetected() || $this->engine->isDetected() || $this->device->isDetected();
}
public function toString(): string
{
$prefix = $this->camouflage ? 'an unknown browser that imitates ' : '';
$browser = $this->browser->toString();
$os = $this->os->toString();
$engine = $this->engine->toString();
$device = $this->device->toString();
if (empty($device) && empty($os) && $this->device->type == 'television') {
$device = 'television';
}
if (empty($device) && $this->device->type == 'emulator') {
$device = 'emulator';
}
if (!empty($browser) && !empty($os) && !empty($device)) {
return $prefix . $browser . ' on ' . $device . ' running ' . $os;
}
if (!empty($browser) && empty($os) && !empty($device)) {
return $prefix . $browser . ' on ' . $device;
}
if (!empty($browser) && !empty($os) && empty($device)) {
return $prefix . $browser . ' on ' . $os;
}
if (empty($browser) && !empty($os) && !empty($device)) {
return $prefix . $device . ' running ' . $os;
}
if (!empty($browser) && empty($os) && empty($device)) {
return $prefix . $browser;
}
if (empty($browser) && empty($os) && !empty($device)) {
return $prefix . $device;
}
if ($this->device->type == 'desktop' && !empty($os) && !empty($engine) && empty($device)) {
return 'an unknown browser based on ' . $engine . ' running on ' . $os;
}
if ($this->browser->stock && !empty($os) && empty($device)) {
return $os;
}
if ($this->browser->stock && !empty($engine) && empty($device)) {
return 'an unknown browser based on ' . $engine;
}
if ($this->device->type == 'bot') {
return 'an unknown bot';
}
return 'an unknown browser';
}
public function toJavaScript(): string
{
return "this.browser = new Browser({ " . $this->browser->toJavaScript() . " });\n" .
"this.engine = new Engine({ " . $this->engine->toJavaScript() . " });\n" .
"this.os = new Os({ " . $this->os->toJavaScript() . " });\n" .
"this.device = new Device({ " . $this->device->toJavaScript() . " });\n" .
"this.camouflage = " . ($this->camouflage ? 'true' : 'false') . ";\n" .
"this.features = " . json_encode($this->features) . ";\n";
}
public function toArray(): array
{
return [
'browser' => $this->browser->toArray(),
'engine' => $this->engine->toArray(),
'os' => $this->os->toArray(),
'device' => $this->device->toArray(),
'location' => $this->location->toArray(),
'network' => $this->network->toArray(),
'camouflage' => $this->camouflage,
];
}
}