|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace hexydec\agentzero; |
|
4
|
|
|
|
|
5
|
|
|
class other { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Generates a configuration array for matching other |
|
9
|
|
|
* |
|
10
|
|
|
* @return array<string,props> An array with keys representing the string to match, and values a props object defining how to generate the match and which properties to set |
|
11
|
|
|
*/ |
|
12
|
2 |
|
public static function get() : array { |
|
13
|
1 |
|
return [ |
|
14
|
1 |
|
'{' => new props('any', function (string $value) : ?array { |
|
15
|
2 |
|
if (($start = \mb_strpos($value, '{')) === false) { |
|
16
|
|
|
|
|
17
|
2 |
|
} elseif (($end = \mb_strpos($value, '}', $start)) !== false) { |
|
18
|
1 |
|
$json = \mb_substr($value, $start, $end - $start + 1); |
|
19
|
|
|
|
|
20
|
|
|
// decode JSON |
|
21
|
1 |
|
if (($data = \json_decode($json, true)) !== null) { |
|
22
|
1 |
|
$cat = [ |
|
23
|
1 |
|
'type' => 'human' |
|
24
|
1 |
|
]; |
|
25
|
1 |
|
$data = \array_change_key_case((array) $data); |
|
26
|
|
|
|
|
27
|
|
|
// special case for chrome - browser |
|
28
|
1 |
|
if (($data['app'] ?? null) === 'com.google.chrome.ios') { |
|
29
|
|
|
$cat['browser'] = 'Chrome'; |
|
30
|
|
|
$cat['browserversion'] = $data['appversion'] ?? null; |
|
31
|
|
|
unset($data['app'], $data['appversion']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// map values |
|
35
|
1 |
|
$map = [ |
|
36
|
1 |
|
'os' => 'platform', |
|
37
|
1 |
|
'osversion' => 'platformversion', |
|
38
|
1 |
|
'isdarktheme' => 'darkmode', |
|
39
|
1 |
|
'locale' => 'language' |
|
40
|
1 |
|
]; |
|
41
|
1 |
|
$fields = ['os', 'osversion', 'platform', 'platformversion', 'app', 'appversion', 'isdarktheme', 'locale', 'device']; |
|
42
|
1 |
|
foreach ($fields AS $item) { |
|
43
|
1 |
|
if (isset($data[$item])) { |
|
44
|
1 |
|
if ($item === 'app') { |
|
45
|
1 |
|
$cat['app'] = apps::getApp($data[$item]); |
|
46
|
1 |
|
$cat['appname'] = $data[$item]; |
|
47
|
1 |
|
} elseif ($item === 'device') { |
|
48
|
1 |
|
$cat = \array_merge($cat, devices::getDevice($data[$item])); |
|
49
|
1 |
|
} elseif ($item === 'os') { |
|
50
|
1 |
|
$parts = \explode(' ', $data[$item]); |
|
51
|
1 |
|
if (\is_numeric($parts[1] ?? null)) { |
|
52
|
1 |
|
$cat['platform'] = $parts[0]; |
|
53
|
1 |
|
$cat['platformversion'] = $parts[1]; |
|
54
|
|
|
} else { |
|
55
|
1 |
|
$cat['platform'] = $data[$item]; |
|
56
|
|
|
} |
|
57
|
|
|
} else { |
|
58
|
1 |
|
$cat[$map[$item] ?? $item] = $data[$item]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
1 |
|
return $cat; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
1 |
|
return null; |
|
66
|
1 |
|
}) |
|
67
|
1 |
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
} |