1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\BrowserDetector; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Browser Detection. |
7
|
|
|
*/ |
8
|
|
|
class Browser |
9
|
|
|
{ |
10
|
|
|
const UNKNOWN = 'unknown'; |
11
|
|
|
const VIVALDI = 'Vivaldi'; |
12
|
|
|
const OPERA = 'Opera'; |
13
|
|
|
const OPERA_MINI = 'Opera Mini'; |
14
|
|
|
const WEBTV = 'WebTV'; |
15
|
|
|
const IE = 'Internet Explorer'; |
16
|
|
|
const POCKET_IE = 'Pocket Internet Explorer'; |
17
|
|
|
const KONQUEROR = 'Konqueror'; |
18
|
|
|
const ICAB = 'iCab'; |
19
|
|
|
const OMNIWEB = 'OmniWeb'; |
20
|
|
|
const FIREBIRD = 'Firebird'; |
21
|
|
|
const FIREFOX = 'Firefox'; |
22
|
|
|
const SEAMONKEY = 'SeaMonkey'; |
23
|
|
|
const ICEWEASEL = 'Iceweasel'; |
24
|
|
|
const SHIRETOKO = 'Shiretoko'; |
25
|
|
|
const MOZILLA = 'Mozilla'; |
26
|
|
|
const AMAYA = 'Amaya'; |
27
|
|
|
const LYNX = 'Lynx'; |
28
|
|
|
const SAFARI = 'Safari'; |
29
|
|
|
const CHROME = 'Chrome'; |
30
|
|
|
const NAVIGATOR = 'Navigator'; |
31
|
|
|
const GOOGLEBOT = 'GoogleBot'; |
32
|
|
|
const SLURP = 'Yahoo! Slurp'; |
33
|
|
|
const W3CVALIDATOR = 'W3C Validator'; |
34
|
|
|
const BLACKBERRY = 'BlackBerry'; |
35
|
|
|
const ICECAT = 'IceCat'; |
36
|
|
|
const NOKIA_S60 = 'Nokia S60 OSS Browser'; |
37
|
|
|
const NOKIA = 'Nokia Browser'; |
38
|
|
|
const MSN = 'MSN Browser'; |
39
|
|
|
const MSNBOT = 'MSN Bot'; |
40
|
|
|
const NETSCAPE_NAVIGATOR = 'Netscape Navigator'; |
41
|
|
|
const GALEON = 'Galeon'; |
42
|
|
|
const NETPOSITIVE = 'NetPositive'; |
43
|
|
|
const PHOENIX = 'Phoenix'; |
44
|
|
|
const GSA = 'Google Search Appliance'; |
45
|
|
|
const YANDEX = 'Yandex'; |
46
|
|
|
const EDGE = 'Edge'; |
47
|
|
|
|
48
|
|
|
const VERSION_UNKNOWN = 'unknown'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var UserAgent |
52
|
|
|
*/ |
53
|
|
|
private $userAgent; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $name; |
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $version; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $isRobot = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
private $isChromeFrame = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
private $isCompatibilityMode = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param null|string|UserAgent $userAgent |
81
|
|
|
* |
82
|
|
|
* @throws \Sinergi\BrowserDetector\InvalidArgumentException |
83
|
|
|
*/ |
84
|
7 |
View Code Duplication |
public function __construct($userAgent = null) |
|
|
|
|
85
|
|
|
{ |
86
|
7 |
|
if ($userAgent instanceof UserAgent) { |
87
|
|
|
$this->setUserAgent($userAgent); |
88
|
7 |
|
} elseif (null === $userAgent || is_string($userAgent)) { |
89
|
7 |
|
$this->setUserAgent(new UserAgent($userAgent)); |
90
|
7 |
|
} else { |
91
|
|
|
throw new InvalidArgumentException(); |
92
|
|
|
} |
93
|
7 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the name of the OS. |
97
|
|
|
* |
98
|
|
|
* @param string $name |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
7 |
|
public function setName($name) |
103
|
|
|
{ |
104
|
7 |
|
$this->name = (string)$name; |
105
|
|
|
|
106
|
7 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the name of the Browser. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
7 |
|
public function getName() |
115
|
|
|
{ |
116
|
7 |
|
if (!isset($this->name)) { |
117
|
7 |
|
BrowserDetector::detect($this, $this->getUserAgent()); |
118
|
7 |
|
} |
119
|
|
|
|
120
|
7 |
|
return $this->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check to see if the specific browser is valid. |
125
|
|
|
* |
126
|
|
|
* @param string $name |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function isBrowser($name) |
131
|
|
|
{ |
132
|
|
|
return (0 == strcasecmp($this->getName(), trim($name))); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the version of the browser. |
137
|
|
|
* |
138
|
|
|
* @param string $version |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
7 |
|
public function setVersion($version) |
143
|
|
|
{ |
144
|
7 |
|
$this->version = (string)$version; |
145
|
|
|
|
146
|
7 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* The version of the browser. |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
7 |
|
public function getVersion() |
155
|
|
|
{ |
156
|
7 |
|
if (!isset($this->name)) { |
157
|
|
|
BrowserDetector::detect($this, $this->getUserAgent()); |
158
|
|
|
} |
159
|
|
|
|
160
|
7 |
|
return (string) $this->version; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set the Browser to be a robot. |
165
|
|
|
* |
166
|
|
|
* @param bool $isRobot |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function setIsRobot($isRobot) |
171
|
|
|
{ |
172
|
|
|
$this->isRobot = (bool)$isRobot; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Is the browser from a robot (ex Slurp,GoogleBot)? |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function getIsRobot() |
183
|
|
|
{ |
184
|
|
|
if (!isset($this->name)) { |
185
|
|
|
BrowserDetector::detect($this, $this->getUserAgent()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->isRobot; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
public function isRobot() |
195
|
|
|
{ |
196
|
|
|
return $this->getIsRobot(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param bool $isChromeFrame |
201
|
|
|
* |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function setIsChromeFrame($isChromeFrame) |
205
|
|
|
{ |
206
|
|
|
$this->isChromeFrame = (bool)$isChromeFrame; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Used to determine if the browser is actually "chromeframe". |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function getIsChromeFrame() |
217
|
|
|
{ |
218
|
|
|
if (!isset($this->name)) { |
219
|
|
|
BrowserDetector::detect($this, $this->getUserAgent()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->isChromeFrame; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function isChromeFrame() |
229
|
|
|
{ |
230
|
|
|
return $this->getIsChromeFrame(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param UserAgent $userAgent |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
7 |
|
public function setUserAgent(UserAgent $userAgent) |
239
|
|
|
{ |
240
|
7 |
|
$this->userAgent = $userAgent; |
241
|
|
|
|
242
|
7 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return UserAgent |
247
|
|
|
*/ |
248
|
7 |
|
public function getUserAgent() |
249
|
|
|
{ |
250
|
7 |
|
return $this->userAgent; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param bool |
255
|
|
|
* |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
1 |
|
public function setIsCompatibilityMode($isCompatibilityMode) |
259
|
|
|
{ |
260
|
1 |
|
$this->isCompatibilityMode = $isCompatibilityMode; |
261
|
|
|
|
262
|
1 |
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
|
|
public function isCompatibilityMode() |
269
|
|
|
{ |
270
|
|
|
return $this->isCompatibilityMode; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Render pages outside of IE's compatibility mode. |
275
|
|
|
*/ |
276
|
|
|
public function endCompatibilityMode() |
277
|
|
|
{ |
278
|
|
|
header('X-UA-Compatible: IE=edge'); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.