Passed
Push — main ( a35a1e...365803 )
by Will
08:34 queued 05:25
created

agentzero::parse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
crap 7.0222
rs 8.8333
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 string $ua The user-agent string
63
	 * @param \stdClass $data A stdClass object containing the UA details
64
	 */
65 99
	private function __construct(string $ua, \stdClass $data) {
66 99
		$this->string = $ua;
0 ignored issues
show
Bug introduced by
The property string is declared read-only in hexydec\agentzero\agentzero.
Loading history...
67
68
		// categories
69 99
		$this->type = $data->type ?? null;
0 ignored issues
show
Bug introduced by
The property type is declared read-only in hexydec\agentzero\agentzero.
Loading history...
70 99
		$this->category = $data->category ?? null;
0 ignored issues
show
Bug introduced by
The property category is declared read-only in hexydec\agentzero\agentzero.
Loading history...
71
72
		// device
73 99
		$this->vendor = $data->vendor ?? null;
0 ignored issues
show
Bug introduced by
The property vendor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
74 99
		$this->device = $data->device ?? null;
0 ignored issues
show
Bug introduced by
The property device is declared read-only in hexydec\agentzero\agentzero.
Loading history...
75 99
		$this->model = $data->model ?? null;
0 ignored issues
show
Bug introduced by
The property model is declared read-only in hexydec\agentzero\agentzero.
Loading history...
76 99
		$this->build = $data->build ?? null;
0 ignored issues
show
Bug introduced by
The property build is declared read-only in hexydec\agentzero\agentzero.
Loading history...
77 99
		$this->ram = $data->ram ?? null;
0 ignored issues
show
Bug introduced by
The property ram is declared read-only in hexydec\agentzero\agentzero.
Loading history...
78
79
		// architecture
80 99
		$this->processor = $data->processor ?? null;
0 ignored issues
show
Bug introduced by
The property processor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
81 99
		$this->architecture = $data->architecture ?? null;
0 ignored issues
show
Bug introduced by
The property architecture is declared read-only in hexydec\agentzero\agentzero.
Loading history...
82 99
		$this->bits = $data->bits ?? null;
0 ignored issues
show
Bug introduced by
The property bits is declared read-only in hexydec\agentzero\agentzero.
Loading history...
83 99
		$this->cpu = $data->cpu ?? null;
0 ignored issues
show
Bug introduced by
The property cpu is declared read-only in hexydec\agentzero\agentzero.
Loading history...
84 99
		$this->cpuclock = $data->cpuclock ?? null;
0 ignored issues
show
Bug introduced by
The property cpuclock is declared read-only in hexydec\agentzero\agentzero.
Loading history...
85
86
		// platform
87 99
		$this->kernel = $data->kernel ?? null;
0 ignored issues
show
Bug introduced by
The property kernel is declared read-only in hexydec\agentzero\agentzero.
Loading history...
88 99
		$this->platform = $data->platform ?? null;
0 ignored issues
show
Bug introduced by
The property platform is declared read-only in hexydec\agentzero\agentzero.
Loading history...
89 99
		$this->platformversion = $data->platformversion ?? null;
0 ignored issues
show
Bug introduced by
The property platformversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
90
91
		// browser
92 99
		$this->engine = $data->engine ?? null;
0 ignored issues
show
Bug introduced by
The property engine is declared read-only in hexydec\agentzero\agentzero.
Loading history...
93 99
		$this->engineversion = $data->engineversion ?? null;
0 ignored issues
show
Bug introduced by
The property engineversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
94 99
		$this->browser = $data->browser ?? null;
0 ignored issues
show
Bug introduced by
The property browser is declared read-only in hexydec\agentzero\agentzero.
Loading history...
95 99
		$this->browserversion = $data->browserversion ?? null;
0 ignored issues
show
Bug introduced by
The property browserversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
96 99
		$this->language = $data->language ?? null;
0 ignored issues
show
Bug introduced by
The property language is declared read-only in hexydec\agentzero\agentzero.
Loading history...
97
98
		// app
99 99
		$this->app = $data->app ?? null;
0 ignored issues
show
Bug introduced by
The property app is declared read-only in hexydec\agentzero\agentzero.
Loading history...
100 99
		$this->appname = $data->appname ?? null;
0 ignored issues
show
Bug introduced by
The property appname is declared read-only in hexydec\agentzero\agentzero.
Loading history...
101 99
		$this->appversion = $data->appversion ?? null;
0 ignored issues
show
Bug introduced by
The property appversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
102 99
		$this->framework = $data->framework ?? null;
0 ignored issues
show
Bug introduced by
The property framework is declared read-only in hexydec\agentzero\agentzero.
Loading history...
103 99
		$this->frameworkversion = $data->frameworkversion ?? null;
0 ignored issues
show
Bug introduced by
The property frameworkversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
104 99
		$this->url = $data->url ?? null;
0 ignored issues
show
Bug introduced by
The property url is declared read-only in hexydec\agentzero\agentzero.
Loading history...
105
106
		// network
107 99
		$this->nettype = $data->nettype ?? null;
0 ignored issues
show
Bug introduced by
The property nettype is declared read-only in hexydec\agentzero\agentzero.
Loading history...
108 99
		$this->proxy = $data->proxy ?? null;
0 ignored issues
show
Bug introduced by
The property proxy is declared read-only in hexydec\agentzero\agentzero.
Loading history...
109
110
		// screen
111 99
		$this->width = $data->width ?? null;
0 ignored issues
show
Bug introduced by
The property width is declared read-only in hexydec\agentzero\agentzero.
Loading history...
112 99
		$this->height = $data->height ?? null;
0 ignored issues
show
Bug introduced by
The property height is declared read-only in hexydec\agentzero\agentzero.
Loading history...
113 99
		$this->dpi = $data->dpi ?? null;
0 ignored issues
show
Bug introduced by
The property dpi is declared read-only in hexydec\agentzero\agentzero.
Loading history...
114 99
		$this->density = $data->density ?? null;
0 ignored issues
show
Bug introduced by
The property density is declared read-only in hexydec\agentzero\agentzero.
Loading history...
115 99
		$this->darkmode = $data->darkmode ?? null;
0 ignored issues
show
Bug introduced by
The property darkmode is declared read-only in hexydec\agentzero\agentzero.
Loading history...
116
	}
117
118
	/**
119
	 * Retrieves calculated properties
120
	 * 
121
	 * @param string $key The name of the property to retrieve
122
	 * @return string|int|null The requested property or null if it doesn't exist
123
	 */
124
	public function __get(string $key) : string|int|null {
125
		switch ($key) {
126
			case 'host':
127
				if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) {
128
					return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host;
129
				}
130
				return null;
131
			case 'browsermajorversion':
132
			case 'enginemajorversion':
133
			case 'platformmajorversion':
134
			case 'appmajorversion':
135
				$item = \str_replace('major', '', $key);
136
				$value = $this->{$item} ?? null;
137
				return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789')));
138
		}
139
		return $this->{$key} ?? null;
140
	}
141
142
	/**
143
	 * Extracts tokens from a UA string
144
	 * 
145
	 * @param string $ua The User Agent string to be tokenised
146
	 * @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly
147
	 * @param array<string> $ignore An array of tokens that can be ignored in the UA string
148
	 * @return false|array<int,string> An array of tokens, or false if no tokens could be extracted
149
	 */
150 99
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
151
152
		// prepare regexp
153 99
		$single = \implode('|', \array_map('\\preg_quote', $single, \array_fill(0, \count($single), '/')));
154 99
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
155
156
		// split up ua string
157 99
		if (\preg_match_all($pattern, $ua, $match)) {
158
159
			// userland token processing
160 99
			$tokens = [];
161 99
			foreach ($match[0] AS $key => $item) {
162 99
				$lower = \mb_strtolower($item);
163
164
				// special case for handling like
165 99
				if (\str_starts_with($lower, 'like ')) {
166
167
					// chop off words up to a useful token e.g. Platform/Version
168 76
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
169 76
						$tokens[$key] = \mb_substr($item, $pos + 1);
170
					}
171
172
				// check token is not ignored
173 99
				} elseif (!\in_array($lower, $ignore, true)) {
174 99
					$tokens[$key] = $item;
175
				}
176
			}
177
178
			// rekey and return
179 99
			return \array_values($tokens);
180
		}
181
		return false;
182
	}
183
184
	/**
185
	 * Parses a User Agent string
186
	 * 
187
	 * @param string $ua The User Agent string to be parsed
188
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
189
	 */
190 99
	public static function parse(string $ua) : agentzero|false {
191 99
		if (($ua = \preg_replace('/\s{2,}/', ' ', $ua)) === null) {
192
193 99
		} elseif (($config = config::get()) === null) {
194
195 99
		} elseif (($tokens = self::getTokens(\trim($ua, ' "\''), $config['single'], $config['ignore'])) !== false) {
196
197
			// extract UA info
198 99
			$browser = new \stdClass();
199 99
			$tokenslower = \array_map('mb_strtolower', $tokens);
200 99
			foreach ($config['match'] AS $key => $item) {
201 99
				$item->match($browser, $key, $tokens, $tokenslower);
202
			}
203
204
			// default information
205 99
			$arr = (array) $browser;
206 99
			if (empty($arr) && !empty($tokens)) {
207 1
				self::parseDefault($browser, $tokens);
208
			}
209
210
			// create agentzero object and return
211 99
			return new agentzero($ua, $browser);
212
		}
213
		return false;
214
	}
215
216
	/**
217
	 * Parse the UA string when no other extractions were able to be made
218
	 * 
219
	 * @param \stdClass $obj A standard class object to populate
220
	 * @param array<string> $tokens An array of tokens
221
	 * @return void
222
	 */
223 1
	protected static function parseDefault(\stdClass $obj, array $tokens) : void {
224 1
		$obj->type = 'robot';
225 1
		$obj->category = 'scraper';
226
227
		// find app names
228 1
		foreach ($tokens AS $item) {
229 1
			if (\str_contains($item, '/')) {
230 1
				$parts = \explode('/', $item);
231 1
				$obj->app = crawlers::normaliseAppname($parts[0]);
232 1
				$obj->appname = $parts[0];
233 1
				if (!empty($parts[1])) {
234 1
					$obj->appversion = \ltrim($parts[1], 'v');
235
				}
236 1
				return;
237
			}
238
		}
239
240
		// parse the string
241 1
		foreach ($tokens AS $token) {
242 1
			$name = [];
243 1
			foreach (\explode(' ', $token) AS $item) {
244 1
				$ver = \ltrim($item, 'v'); // strip 'v' off the front of version number
245 1
				if (\strpbrk($ver, '0123456789.') === $ver) {
246 1
					$app = \implode(' ', $name);
247 1
					$obj->app = crawlers::normaliseAppname($app);
248 1
					$obj->appname = $app;
249 1
					$obj->appversion = $ver;
250 1
					return;
251
				} else {
252 1
					$name[] = $item;
253
				}
254
			}
255
		}
256
257
		// just use the string
258 1
		$obj->app = crawlers::normaliseAppname($tokens[0]);
259 1
		$obj->appname = $tokens[0];
260
	}
261
}