|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) 2008 Dejan Spasic <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @package Util |
|
9
|
|
|
* @author Dejan Spasic <[email protected]> |
|
10
|
|
|
* @version GIT: $Id:$ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Fam\Util; |
|
14
|
|
|
|
|
15
|
|
|
use Fam\Util\UserAgentParser\OperatingSystem; |
|
16
|
|
|
use Fam\Util\UserAgentParser\UndefinedOperatingSystem; |
|
17
|
|
|
use Fam\Util\UserAgentParser\UndefinedWebClient; |
|
18
|
|
|
use Fam\Util\UserAgentParser\UserAgent; |
|
19
|
|
|
use Fam\Util\UserAgentParser\WebClient; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A lightweight and fast browser detector |
|
23
|
|
|
* |
|
24
|
|
|
* Sniffs the operating system, web client name and web client version from |
|
25
|
|
|
* environment HTTP_USER_AGENT. |
|
26
|
|
|
* |
|
27
|
|
|
* <code> |
|
28
|
|
|
* $userAgent = UserAgentParser::createInstance()->parseUserAgent($_SERVER['HTTP_USER_AGENT']); |
|
29
|
|
|
* |
|
30
|
|
|
* if ($userAgent->isWebClient('firefox')) { |
|
31
|
|
|
* if (UserAgentParser::isWebClientVersionBetween(9.5, 9.6)) { |
|
32
|
|
|
* echo 'firefox between 95 and 96'; |
|
33
|
|
|
* } |
|
34
|
|
|
* else if (UserAgentParser::isWebClientVersionBetween(9.2, 9.4)) { |
|
35
|
|
|
* echo 'firefox between 92 and 94'; |
|
36
|
|
|
* } |
|
37
|
|
|
* } |
|
38
|
|
|
* |
|
39
|
|
|
* if ($userAgent->isOs('macintosh')) { |
|
40
|
|
|
* echo 'Mac'; |
|
41
|
|
|
* } |
|
42
|
|
|
* </code> |
|
43
|
|
|
* |
|
44
|
|
|
* @package Util |
|
45
|
|
|
* @author Dejan Spasic <[email protected]> |
|
46
|
|
|
* @version @@PACKAGE_VERSION@@ |
|
47
|
|
|
*/ |
|
48
|
|
|
class UserAgentParser |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $userAgent = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private $operatingSystems = array(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
private $webClients = array(); |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var OperatingSystem |
|
67
|
|
|
*/ |
|
68
|
|
|
private $undefinedOperatingSystem; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var OperatingSystem |
|
72
|
|
|
*/ |
|
73
|
|
|
private $undefinedWebClient; |
|
74
|
|
|
|
|
75
|
14 |
|
public function __construct() |
|
76
|
|
|
{ |
|
77
|
14 |
|
$this->undefinedOperatingSystem = new UndefinedOperatingSystem(); |
|
78
|
14 |
|
$this->undefinedWebClient = new UndefinedWebClient(); |
|
|
|
|
|
|
79
|
14 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public static function createInstance(): self |
|
82
|
|
|
{ |
|
83
|
2 |
|
$self = new static(); |
|
84
|
2 |
|
$self->initializeCommonOperatingSystems(); |
|
85
|
2 |
|
$self->initializeCommonWebClients(); |
|
86
|
|
|
|
|
87
|
2 |
|
return $self; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
private function initializeCommonOperatingSystems(): void |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->addOperatingSystem(new UserAgentParser\Windows()); |
|
93
|
2 |
|
$this->addOperatingSystem(new UserAgentParser\Macintosh()); |
|
94
|
2 |
|
$this->addOperatingSystem(new UserAgentParser\Unix()); |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
private function initializeCommonWebClients(): void |
|
98
|
|
|
{ |
|
99
|
2 |
|
$this->addWebClient(new UserAgentParser\Firefox()); |
|
100
|
2 |
|
$this->addWebClient(new UserAgentParser\Opera()); |
|
101
|
2 |
|
$this->addWebClient(new UserAgentParser\Safari()); |
|
102
|
2 |
|
$this->addWebClient(new UserAgentParser\InternetExplorer()); |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
public function parseUserAgent(string $userAgent): UserAgent |
|
106
|
|
|
{ |
|
107
|
4 |
|
$this->userAgent = $userAgent; |
|
108
|
|
|
|
|
109
|
4 |
|
return new UserAgent($this->parseOs(), $this->parseWebClient()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return OperatingSystem |
|
114
|
|
|
*/ |
|
115
|
4 |
|
private function parseOs(): OperatingSystem |
|
116
|
|
|
{ |
|
117
|
4 |
|
foreach ($this->operatingSystems as $currentOs) { |
|
118
|
2 |
|
if ($currentOs->match($this->userAgent)) { |
|
119
|
2 |
|
return $currentOs; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
return $this->undefinedOperatingSystem; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
private function parseWebClient(): WebClient |
|
127
|
|
|
{ |
|
128
|
4 |
|
foreach ($this->webClients as $currentWebClient) { |
|
129
|
1 |
|
if ($currentWebClient->match($this->userAgent)) { |
|
130
|
1 |
|
return $currentWebClient; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
3 |
|
return $this->undefinedWebClient; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
7 |
|
public function addOperatingSystem(OperatingSystem $operatingSystem) |
|
138
|
|
|
{ |
|
139
|
7 |
|
$this->operatingSystems[get_class($operatingSystem)] = $operatingSystem; |
|
140
|
7 |
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
public function removeOperatingSystem(OperatingSystem $operatingSystem) |
|
143
|
|
|
{ |
|
144
|
1 |
|
$this->removeOperatingSystemByClassName(get_class($operatingSystem)); |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
public function removeOperatingSystemByClassName(string $operatingSystem) |
|
148
|
|
|
{ |
|
149
|
2 |
|
unset($this->operatingSystems[$operatingSystem]); |
|
150
|
2 |
|
} |
|
151
|
|
|
|
|
152
|
4 |
|
public function getOperatingSystems(): array |
|
153
|
|
|
{ |
|
154
|
4 |
|
return $this->operatingSystems; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
public function getUndefinedOperatingSystem(): OperatingSystem |
|
158
|
|
|
{ |
|
159
|
1 |
|
return $this->undefinedOperatingSystem; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
2 |
|
public function setUndefinedOperatingSystem(OperatingSystem $operatingSystem) |
|
163
|
|
|
{ |
|
164
|
2 |
|
$this->undefinedOperatingSystem = $operatingSystem; |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
6 |
|
public function addWebClient(WebClient $webClient): void |
|
168
|
|
|
{ |
|
169
|
6 |
|
$this->webClients[get_class($webClient)] = $webClient; |
|
170
|
6 |
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
public function removeWebClient(WebClient $webClient): void |
|
173
|
|
|
{ |
|
174
|
1 |
|
$this->removeWebClientByClassName(get_class($webClient)); |
|
175
|
1 |
|
} |
|
176
|
|
|
|
|
177
|
2 |
|
public function removeWebClientByClassName(string $webClient) |
|
178
|
|
|
{ |
|
179
|
2 |
|
unset($this->webClients[$webClient]); |
|
180
|
2 |
|
} |
|
181
|
|
|
|
|
182
|
4 |
|
public function getWebClients(): array |
|
183
|
|
|
{ |
|
184
|
4 |
|
return $this->webClients; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
public function getUndefinedWebClient(): WebClient |
|
188
|
|
|
{ |
|
189
|
1 |
|
return $this->undefinedWebClient; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
2 |
|
public function setUndefinedWebClient(WebClient $webClient): void |
|
193
|
|
|
{ |
|
194
|
2 |
|
$this->undefinedWebClient = $webClient; |
|
|
|
|
|
|
195
|
2 |
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..