|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace hexydec\agentzero; |
|
4
|
|
|
|
|
5
|
|
|
class agentzero { |
|
6
|
|
|
|
|
7
|
|
|
// ua string |
|
8
|
|
|
public readonly string $string; |
|
9
|
|
|
|
|
10
|
|
|
// categories |
|
11
|
|
|
public readonly ?string $type; |
|
12
|
|
|
public readonly ?string $category; |
|
13
|
|
|
|
|
14
|
|
|
// device |
|
15
|
|
|
public readonly ?string $vendor; |
|
16
|
|
|
public readonly ?string $device; |
|
17
|
|
|
public readonly ?string $model; |
|
18
|
|
|
public readonly ?string $build; |
|
19
|
|
|
public readonly ?int $ram; |
|
20
|
|
|
|
|
21
|
|
|
// architecture |
|
22
|
|
|
public readonly ?string $processor; |
|
23
|
|
|
public readonly ?string $architecture; |
|
24
|
|
|
public readonly ?int $bits; |
|
25
|
|
|
public readonly ?string $cpu; |
|
26
|
|
|
public readonly ?int $cpuclock; |
|
27
|
|
|
|
|
28
|
|
|
// platform |
|
29
|
|
|
public readonly ?string $kernel; |
|
30
|
|
|
public readonly ?string $platform; |
|
31
|
|
|
public readonly ?string $platformversion; |
|
32
|
|
|
|
|
33
|
|
|
// browser |
|
34
|
|
|
public readonly ?string $engine; |
|
35
|
|
|
public readonly ?string $engineversion; |
|
36
|
|
|
public readonly ?string $browser; |
|
37
|
|
|
public readonly ?string $browserversion; |
|
38
|
|
|
public readonly ?string $language; |
|
39
|
|
|
|
|
40
|
|
|
// app |
|
41
|
|
|
public readonly ?string $app; |
|
42
|
|
|
public readonly ?string $appname; |
|
43
|
|
|
public readonly ?string $appversion; |
|
44
|
|
|
public readonly ?string $framework; |
|
45
|
|
|
public readonly ?string $frameworkversion; |
|
46
|
|
|
public readonly ?string $url; |
|
47
|
|
|
|
|
48
|
|
|
// network |
|
49
|
|
|
public readonly ?string $nettype; |
|
50
|
|
|
public readonly ?string $proxy; |
|
51
|
|
|
|
|
52
|
|
|
// screen |
|
53
|
|
|
public readonly ?int $width; |
|
54
|
|
|
public readonly ?int $height; |
|
55
|
|
|
public readonly ?int $dpi; |
|
56
|
|
|
public readonly ?float $density; |
|
57
|
|
|
public readonly ?bool $darkmode; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructs a new AgentZero object, private because it can only be created internally |
|
61
|
|
|
* |
|
62
|
|
|
* @param \stdClass $data A stdClass object containing the UA details |
|
63
|
|
|
*/ |
|
64
|
97 |
|
private function __construct(string $ua, \stdClass $data) { |
|
65
|
97 |
|
$this->string = $ua; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// categories |
|
68
|
97 |
|
$this->type = $data->type ?? null; |
|
|
|
|
|
|
69
|
97 |
|
$this->category = $data->category ?? null; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
// device |
|
72
|
97 |
|
$this->vendor = $data->vendor ?? null; |
|
|
|
|
|
|
73
|
97 |
|
$this->device = $data->device ?? null; |
|
|
|
|
|
|
74
|
97 |
|
$this->model = $data->model ?? null; |
|
|
|
|
|
|
75
|
97 |
|
$this->build = $data->build ?? null; |
|
|
|
|
|
|
76
|
97 |
|
$this->ram = $data->ram ?? null; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
// architecture |
|
79
|
97 |
|
$this->processor = $data->processor ?? null; |
|
|
|
|
|
|
80
|
97 |
|
$this->architecture = $data->architecture ?? null; |
|
|
|
|
|
|
81
|
97 |
|
$this->bits = $data->bits ?? null; |
|
|
|
|
|
|
82
|
97 |
|
$this->cpu = $data->cpu ?? null; |
|
|
|
|
|
|
83
|
97 |
|
$this->cpuclock = $data->cpuclock ?? null; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
// platform |
|
86
|
97 |
|
$this->kernel = $data->kernel ?? null; |
|
|
|
|
|
|
87
|
97 |
|
$this->platform = $data->platform ?? null; |
|
|
|
|
|
|
88
|
97 |
|
$this->platformversion = $data->platformversion ?? null; |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
// browser |
|
91
|
97 |
|
$this->engine = $data->engine ?? null; |
|
|
|
|
|
|
92
|
97 |
|
$this->engineversion = $data->engineversion ?? null; |
|
|
|
|
|
|
93
|
97 |
|
$this->browser = $data->browser ?? null; |
|
|
|
|
|
|
94
|
97 |
|
$this->browserversion = $data->browserversion ?? null; |
|
|
|
|
|
|
95
|
97 |
|
$this->language = $data->language ?? null; |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
// app |
|
98
|
97 |
|
$this->app = $data->app ?? null; |
|
|
|
|
|
|
99
|
97 |
|
$this->appname = $data->appname ?? null; |
|
|
|
|
|
|
100
|
97 |
|
$this->appversion = $data->appversion ?? null; |
|
|
|
|
|
|
101
|
97 |
|
$this->framework = $data->framework ?? null; |
|
|
|
|
|
|
102
|
97 |
|
$this->frameworkversion = $data->frameworkversion ?? null; |
|
|
|
|
|
|
103
|
97 |
|
$this->url = $data->url ?? null; |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
// network |
|
106
|
97 |
|
$this->nettype = $data->nettype ?? null; |
|
|
|
|
|
|
107
|
97 |
|
$this->proxy = $data->proxy ?? null; |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
// screen |
|
110
|
97 |
|
$this->width = $data->width ?? null; |
|
|
|
|
|
|
111
|
97 |
|
$this->height = $data->height ?? null; |
|
|
|
|
|
|
112
|
97 |
|
$this->dpi = $data->dpi ?? null; |
|
|
|
|
|
|
113
|
97 |
|
$this->density = $data->density ?? null; |
|
|
|
|
|
|
114
|
97 |
|
$this->darkmode = $data->darkmode ?? null; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Retrieves calculated properties |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $key The name of the property to retrieve |
|
121
|
|
|
* @return string|int|null The requested property or null if it doesn't exist |
|
122
|
|
|
*/ |
|
123
|
|
|
public function __get(string $key) : string|int|null { |
|
124
|
|
|
switch ($key) { |
|
125
|
|
|
case 'host': |
|
126
|
|
|
if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) { |
|
127
|
|
|
return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host; |
|
128
|
|
|
} |
|
129
|
|
|
return null; |
|
130
|
|
|
case 'browsermajorversion': |
|
131
|
|
|
case 'enginemajorversion': |
|
132
|
|
|
case 'platformmajorversion': |
|
133
|
|
|
case 'appmajorversion': |
|
134
|
|
|
$item = \str_replace('major', '', $key); |
|
135
|
|
|
$value = $this->{$item} ?? null; |
|
136
|
|
|
return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789'))); |
|
137
|
|
|
} |
|
138
|
|
|
return $this->{$key} ?? null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Extracts tokens from a UA string |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $ua The User Agent string to be tokenised |
|
145
|
|
|
* @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly |
|
146
|
|
|
* @param array<string> $ignore An array of tokens that can be ignored in the UA string |
|
147
|
|
|
* @return false|array<int,string> An array of tokens, or false if no tokens could be extracted |
|
148
|
|
|
*/ |
|
149
|
97 |
|
protected static function getTokens(string $ua, array $single, array $ignore) : array|false { |
|
150
|
|
|
|
|
151
|
|
|
// prepare regexp |
|
152
|
97 |
|
$single = \implode('|', \array_map('\\preg_quote', $single)); |
|
153
|
97 |
|
$pattern = '/\{[^}]++\}|[^()\[\];,\/ _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/ _-](?:\/[^;,()\[\] ]++)?|[0-9]/i'; |
|
154
|
|
|
|
|
155
|
|
|
// split up ua string |
|
156
|
97 |
|
if (\preg_match_all($pattern, $ua, $match)) { |
|
157
|
|
|
|
|
158
|
|
|
// userland token processing |
|
159
|
97 |
|
$tokens = []; |
|
160
|
97 |
|
foreach ($match[0] AS $key => $item) { |
|
161
|
97 |
|
$lower = \mb_strtolower($item); |
|
162
|
|
|
|
|
163
|
|
|
// special case for handling like |
|
164
|
97 |
|
if (\str_starts_with($lower, 'like ')) { |
|
165
|
|
|
|
|
166
|
|
|
// chop off words up to a useful token e.g. Platform/Version |
|
167
|
76 |
|
if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) { |
|
168
|
76 |
|
$tokens[$key] = \mb_substr($item, $pos + 1); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// check token is not ignored |
|
172
|
97 |
|
} elseif (!\in_array($lower, $ignore)) { |
|
173
|
97 |
|
$tokens[$key] = $item; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// rekey and return |
|
178
|
97 |
|
return \array_values($tokens); |
|
179
|
|
|
} |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Parses a User Agent string |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $ua The User Agent string to be parsed |
|
187
|
|
|
* @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed |
|
188
|
|
|
*/ |
|
189
|
97 |
|
public static function parse(string $ua) : agentzero|false { |
|
190
|
97 |
|
if (($config = config::get()) === null) { |
|
191
|
|
|
|
|
192
|
97 |
|
} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) { |
|
193
|
|
|
|
|
194
|
|
|
// extract UA info |
|
195
|
97 |
|
$browser = new \stdClass(); |
|
196
|
97 |
|
$tokenslower = \array_map('mb_strtolower', $tokens); |
|
197
|
97 |
|
foreach ($config['match'] AS $key => $item) { |
|
198
|
97 |
|
$item->match($browser, $key, $tokens, $tokenslower); |
|
199
|
|
|
} |
|
200
|
97 |
|
return new agentzero($ua, $browser); |
|
201
|
|
|
} |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
} |