|
1
|
|
|
<?php |
|
2
|
|
|
namespace vipnytt; |
|
3
|
|
|
|
|
4
|
|
|
use vipnytt\UserAgentParser\Exceptions\FormatException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class UserAgentParser |
|
8
|
|
|
* |
|
9
|
|
|
* @package vipnytt |
|
10
|
|
|
*/ |
|
11
|
|
|
class UserAgentParser |
|
12
|
|
|
{ |
|
13
|
|
|
private $userAgent; |
|
14
|
|
|
private $groups = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $userAgent |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($userAgent) |
|
22
|
|
|
{ |
|
23
|
|
|
mb_detect_encoding($userAgent); |
|
24
|
|
|
$this->userAgent = trim($userAgent); |
|
25
|
|
|
$this->checkFormat(); |
|
26
|
|
|
$this->explode(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Validate the UserAgent format |
|
31
|
|
|
* |
|
32
|
|
|
* @throws FormatException |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function checkFormat() |
|
35
|
|
|
{ |
|
36
|
|
|
if (preg_match('/\s/', $this->userAgent)) { |
|
37
|
|
|
throw new FormatException("Format not supported. Please use `name/version` or just `name`, eg. `MyUserAgent/1.0` and `MyUserAgent`."); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Parses all possible User-Agent groups to an array |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
private function explode() |
|
47
|
|
|
{ |
|
48
|
|
|
$groups = [$this->userAgent]; |
|
49
|
|
|
|
|
50
|
|
|
$groups[] = $this->stripVersion(); |
|
51
|
|
|
while (mb_stripos(end($groups), '-') !== false) { |
|
52
|
|
|
$current = end($groups); |
|
53
|
|
|
$groups[] = mb_substr($current, 0, mb_strripos($current, '-')); |
|
54
|
|
|
} |
|
55
|
|
|
foreach ($groups as $group) { |
|
56
|
|
|
if (!in_array($group, $this->groups)) { |
|
57
|
|
|
$this->groups[] = $group; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Strip version number |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function stripVersion() |
|
68
|
|
|
{ |
|
69
|
|
|
if (mb_stripos($this->userAgent, '/') !== false) { |
|
70
|
|
|
return mb_split('/', $this->userAgent, 2)[0]; |
|
71
|
|
|
} |
|
72
|
|
|
return $this->userAgent; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Find matching User-Agent |
|
77
|
|
|
* Selects the best matching from an array, or false if none matches |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $array |
|
80
|
|
|
* @return string|false |
|
81
|
|
|
*/ |
|
82
|
|
|
public function match($array) |
|
83
|
|
|
{ |
|
84
|
|
|
$array = array_map('mb_strtolower', $array); |
|
85
|
|
|
foreach ($this->groups as $userAgent) { |
|
86
|
|
|
if (in_array(mb_strtolower($userAgent), $array)) { |
|
87
|
|
|
return $userAgent; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Export all User-Agents as an array |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function export() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->groups; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|