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
|
95 |
|
private function __construct(string $ua, \stdClass $data) { |
65
|
95 |
|
$this->string = $ua; |
|
|
|
|
66
|
|
|
|
67
|
|
|
// categories |
68
|
95 |
|
$this->type = $data->type ?? null; |
|
|
|
|
69
|
95 |
|
$this->category = $data->category ?? null; |
|
|
|
|
70
|
|
|
|
71
|
|
|
// device |
72
|
95 |
|
$this->vendor = $data->vendor ?? null; |
|
|
|
|
73
|
95 |
|
$this->device = $data->device ?? null; |
|
|
|
|
74
|
95 |
|
$this->model = $data->model ?? null; |
|
|
|
|
75
|
95 |
|
$this->build = $data->build ?? null; |
|
|
|
|
76
|
95 |
|
$this->ram = $data->ram ?? null; |
|
|
|
|
77
|
|
|
|
78
|
|
|
// architecture |
79
|
95 |
|
$this->processor = $data->processor ?? null; |
|
|
|
|
80
|
95 |
|
$this->architecture = $data->architecture ?? null; |
|
|
|
|
81
|
95 |
|
$this->bits = $data->bits ?? null; |
|
|
|
|
82
|
95 |
|
$this->cpu = $data->cpu ?? null; |
|
|
|
|
83
|
95 |
|
$this->cpuclock = $data->cpuclock ?? null; |
|
|
|
|
84
|
|
|
|
85
|
|
|
// platform |
86
|
95 |
|
$this->kernel = $data->kernel ?? null; |
|
|
|
|
87
|
95 |
|
$this->platform = $data->platform ?? null; |
|
|
|
|
88
|
95 |
|
$this->platformversion = $data->platformversion ?? null; |
|
|
|
|
89
|
|
|
|
90
|
|
|
// browser |
91
|
95 |
|
$this->engine = $data->engine ?? null; |
|
|
|
|
92
|
95 |
|
$this->engineversion = $data->engineversion ?? null; |
|
|
|
|
93
|
95 |
|
$this->browser = $data->browser ?? null; |
|
|
|
|
94
|
95 |
|
$this->browserversion = $data->browserversion ?? null; |
|
|
|
|
95
|
95 |
|
$this->language = $data->language ?? null; |
|
|
|
|
96
|
|
|
|
97
|
|
|
// app |
98
|
95 |
|
$this->app = $data->app ?? null; |
|
|
|
|
99
|
95 |
|
$this->appname = $data->appname ?? null; |
|
|
|
|
100
|
95 |
|
$this->appversion = $data->appversion ?? null; |
|
|
|
|
101
|
95 |
|
$this->framework = $data->framework ?? null; |
|
|
|
|
102
|
95 |
|
$this->frameworkversion = $data->frameworkversion ?? null; |
|
|
|
|
103
|
95 |
|
$this->url = $data->url ?? null; |
|
|
|
|
104
|
|
|
|
105
|
|
|
// network |
106
|
95 |
|
$this->nettype = $data->nettype ?? null; |
|
|
|
|
107
|
95 |
|
$this->proxy = $data->proxy ?? null; |
|
|
|
|
108
|
|
|
|
109
|
|
|
// screen |
110
|
95 |
|
$this->width = $data->width ?? null; |
|
|
|
|
111
|
95 |
|
$this->height = $data->height ?? null; |
|
|
|
|
112
|
95 |
|
$this->dpi = $data->dpi ?? null; |
|
|
|
|
113
|
95 |
|
$this->density = $data->density ?? null; |
|
|
|
|
114
|
95 |
|
$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
|
95 |
|
protected static function getTokens(string $ua, array $single, array $ignore) : array|false { |
150
|
|
|
|
151
|
|
|
// prepare regexp |
152
|
95 |
|
$single = \implode('|', \array_map('preg_quote', $single)); |
153
|
95 |
|
$pattern = '/\{[^}]++\}|[^()\[\];,\/ _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/ _-](?:\/[^;,()\[\] ]++)?|[0-9]/i'; |
154
|
|
|
|
155
|
|
|
// split up ua string |
156
|
95 |
|
if (\preg_match_all($pattern, $ua, $match)) { |
157
|
|
|
|
158
|
|
|
// remove ignore values |
159
|
95 |
|
$tokens = \array_diff($match[0], $ignore); |
160
|
|
|
|
161
|
|
|
// special case for handling like |
162
|
95 |
|
foreach ($tokens AS $key => $item) { |
163
|
95 |
|
if (\str_starts_with($item, 'like ')) { |
164
|
|
|
|
165
|
|
|
// chop off words up to a useful token e.g. Platform/Version |
166
|
6 |
|
if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) { |
167
|
5 |
|
$tokens[$key] = \mb_substr($item, $pos + 1); |
168
|
|
|
|
169
|
|
|
// just remove the token |
170
|
|
|
} else { |
171
|
1 |
|
unset($tokens[$key]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// rekey and return |
177
|
95 |
|
return \array_values($tokens); |
178
|
|
|
} |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Parses a User Agent string |
184
|
|
|
* |
185
|
|
|
* @param string $ua The User Agent string to be parsed |
186
|
|
|
* @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed |
187
|
|
|
*/ |
188
|
95 |
|
public static function parse(string $ua) : agentzero|false { |
189
|
95 |
|
if (($config = config::get()) === null) { |
190
|
|
|
|
191
|
95 |
|
} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) { |
192
|
|
|
|
193
|
|
|
// extract UA info |
194
|
95 |
|
$browser = new \stdClass(); |
195
|
95 |
|
$tokenslower = \array_map('mb_strtolower', $tokens); |
196
|
95 |
|
foreach ($config['match'] AS $key => $item) { |
197
|
95 |
|
$item->match($browser, $key, $tokens, $tokenslower); |
198
|
|
|
} |
199
|
95 |
|
return new agentzero($ua, $browser); |
200
|
|
|
} |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
} |