-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserDetection.php
More file actions
2379 lines (2135 loc) · 96.8 KB
/
BrowserDetection.php
File metadata and controls
2379 lines (2135 loc) · 96.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Browser detection class file.
* This file contains everything required to use the BrowserDetection class. Tested with PHP 5.3.29 - 7.4.0.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
* later version (if any).
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details at: https://www.gnu.org/licenses/lgpl-3.0.html
*
* @package Browser_Detection
* @version 2.9.6
* @last-modified May 1, 2022
* @author Alexandre Valiquette
* @copyright Copyright (c) 2022, Wolfcast
* @link https://wolfcast.com/
*/
namespace Wolfcast;
/**
* The BrowserDetection class facilitates the identification of the user's environment such as Web browser, version,
* platform and device type.
*
* Typical usage:
*
* $browser = new Wolfcast\BrowserDetection();
* if ($browser->getName() == Wolfcast\BrowserDetection::BROWSER_FIREFOX &&
* $browser->compareVersions($browser->getVersion(), '5.0') >= 0) {
* echo 'You are using FireFox version 5 or greater.';
* }
*
* The class is a rewrite of Chris Schuld's Browser class version 1.9 which is mostly unmaintained since August 20th,
* 2010. Chris' class was based on the original work from Gary White.
*
* Updates:
*
* 2022-05-01: Version 2.9.6
* + Added support for Chrome OS.
* + Added support for macOS Monterey and macOS Big Sur.
* + Now correctly detects AArch64 as 64-bit.
* + Added support for PHP 8.
* + Tested with latest Web Browsers and platforms.
*
* 2020-02-02: Version 2.9.5
* + WARNING! Breaking change: complete rework of robots detection. Now robot name and version is detected in addition
* of browser name and version. Use getRobotName() and getRobotVersion() when isRobot() is true.
* + WARNING! Breaking change: due to robots detection rework the following methods signatures has changed (isRobot
* parameter removed): addCustomBrowserDetection(), checkSimpleBrowserUA(), checkBrowserUAWithVersion().
* + Added possibility to support new robots with addCustomRobotDetection().
* + Added support for the new Microsoft Edge based on Chromium.
* + Added version names for Android 10 and later (Google no longer use candy names for new versions).
* + Added macOS Catalina detection.
* + Added Windows Server 2019 detection (Windows Server 2016 can be no longer detected due to the fact that they both
* use the same version number and that the build is not included in the user agent).
*
* 2019-03-27: Version 2.9.3
* + Fixed Edge detection on Android.
* + Added Android Q detection.
* + Now filtering superglobals.
*
* 2019-02-28: Version 2.9.2
* + Fixed Opera detection.
*
* 2018-08-23: Version 2.9.1
* + Fixed Chrome detection under iOS.
* + Added Android Pie detection.
* + Added macOS Mojave detection.
*
* 2018-07-15: Version 2.9.0
* + WARNING! Breaking change: new Wolfcast namespace. Use new Wolfcast\BrowserDetection().
* + iPad, iPhone and iPod are all under iOS now.
* + Added Android Oreo detection.
* + Added macOS High Sierra detection.
* + Added UC Browser detection.
* + Improved regular expressions (even less false positives).
* + Removed AOL detection.
* + Removed the following Web browsers detection: Amaya, Galeon, NetPositive, OmniWeb, Vivaldi detection (use
* addCustomBrowserDetection()).
* + Removed the following legacy platforms detection: BeOS, OS/2, SunOS (use addCustomPlatformDetection()).
*
* 2016-11-28: Version 2.5.1
* + Better detection of 64-bit platforms.
*
* 2016-08-19: Version 2.5.0
* + Platform version and platform version name are now supported for Mac.
* + Fixed platform version name for Android.
*
* 2016-08-02: Version 2.4.0
* + Platform version and platform version name are now supported for Android.
* + Added support for the Samsung Internet browser.
* + Added support for the Vivaldi browser.
* + Better support for legacy Windows versions.
*
* 2016-02-11: Version 2.3.0
* + WARNING! Breaking change: public method getBrowser() is renamed to getName().
* + WARNING! Breaking change: changed the compareVersions() return values to be more in line with other libraries.
* + You can now get the exact platform version (name or version numbers) on which the browser is run on with
* getPlatformVersion(). Only working with Windows operating systems at the moment.
* + You can now determine if the browser is executed from a 64-bit platform with is64bitPlatform().
* + Better detection of mobile platform for Googlebot.
*
* 2016-01-04: Version 2.2.0
* + Added support for Microsoft Edge.
*
* 2014-12-30: Version 2.1.2
* + Better detection of Opera.
*
* 2014-07-11: Version 2.1.1
* + Better detection of mobile devices and platforms.
*
* 2014-06-04: Version 2.1.0
* + Added support for IE 11+.
*
* 2013-05-27: Version 2.0.0 which is (almost) a complete rewrite based on Chris Schuld's Browser class version 1.9 plus
* changes below.
* + Added support for Opera Mobile
* + Added support for the Windows Phone (formerly Windows Mobile) platform
* + Added support for BlackBerry Tablet OS and BlackBerry 10
* + Added support for the Symbian platform
* + Added support for Bingbot
* + Added support for the Yahoo! Multimedia crawler
* + Removed iPhone/iPad/iPod browsers since there are not browsers but platforms - test them with getPlatform()
* + Removed support for Shiretoko (Firefox 3.5 alpha/beta) and MSN Browser
* + Merged Nokia and Nokia S60
* + Updated some deprecated browser names
* + Many public methods are now protected
* + Documentation updated
*
* 2010-07-04:
* + Added detection of IE compatibility view - test with getIECompatibilityView()
* + Added support for all (deprecated) Netscape versions
* + Added support for Safari < 3.0
* + Better Firefox version parsing
* + Better Opera version parsing
* + Better Mozilla detection
*
* @package Browser_Detection
* @version 2.9.6
* @last-modified May 1, 2022
* @author Alexandre Valiquette, Chris Schuld, Gary White
* @copyright Copyright (c) 2022, Wolfcast
* @license https://www.gnu.org/licenses/lgpl-3.0.html
* @link https://wolfcast.com/
* @link https://wolfcast.com/open-source/browser-detection/tutorial.php
* @link https://chrisschuld.com/
* @link https://www.apptools.com/phptools/browser/
*/
class BrowserDetection
{
/**#@+
* Constant for the name of the Web browser.
*/
const BROWSER_ANDROID = 'Android';
const BROWSER_BLACKBERRY = 'BlackBerry';
const BROWSER_CHROME = 'Chrome';
const BROWSER_EDGE = 'Edge';
const BROWSER_FIREBIRD = 'Firebird';
const BROWSER_FIREFOX = 'Firefox';
const BROWSER_ICAB = 'iCab';
const BROWSER_ICECAT = 'GNU IceCat';
const BROWSER_ICEWEASEL = 'GNU IceWeasel';
const BROWSER_IE = 'Internet Explorer';
const BROWSER_IE_MOBILE = 'Internet Explorer Mobile';
const BROWSER_KONQUEROR = 'Konqueror';
const BROWSER_LYNX = 'Lynx';
const BROWSER_MOZILLA = 'Mozilla';
const BROWSER_MSNTV = 'MSN TV';
const BROWSER_NETSCAPE = 'Netscape';
const BROWSER_NOKIA = 'Nokia Browser';
const BROWSER_OPERA = 'Opera';
const BROWSER_OPERA_MINI = 'Opera Mini';
const BROWSER_OPERA_MOBILE = 'Opera Mobile';
const BROWSER_PHOENIX = 'Phoenix';
const BROWSER_SAFARI = 'Safari';
const BROWSER_SAMSUNG = 'Samsung Internet';
const BROWSER_TABLET_OS = 'BlackBerry Tablet OS';
const BROWSER_UC = 'UC Browser';
const BROWSER_UNKNOWN = 'unknown';
/**#@-*/
/**#@+
* Constant for the name of the platform on which the Web browser runs.
*/
const PLATFORM_ANDROID = 'Android';
const PLATFORM_BLACKBERRY = 'BlackBerry';
const PLATFORM_CHROME_OS = 'Chrome OS';
const PLATFORM_FREEBSD = 'FreeBSD';
const PLATFORM_IOS = 'iOS';
const PLATFORM_LINUX = 'Linux';
const PLATFORM_MACINTOSH = 'Macintosh';
const PLATFORM_NETBSD = 'NetBSD';
const PLATFORM_NOKIA = 'Nokia';
const PLATFORM_OPENBSD = 'OpenBSD';
const PLATFORM_OPENSOLARIS = 'OpenSolaris';
const PLATFORM_SYMBIAN = 'Symbian';
const PLATFORM_UNKNOWN = 'unknown';
const PLATFORM_VERSION_UNKNOWN = 'unknown';
const PLATFORM_WINDOWS = 'Windows';
const PLATFORM_WINDOWS_CE = 'Windows CE';
const PLATFORM_WINDOWS_PHONE = 'Windows Phone';
/**#@-*/
/**#@+
* Constant for the name of the robot.
*/
const ROBOT_BINGBOT = 'Bingbot';
const ROBOT_GOOGLEBOT = 'Googlebot';
const ROBOT_MSNBOT = 'MSNBot';
const ROBOT_SLURP = 'Yahoo! Slurp';
const ROBOT_UNKNOWN = '';
const ROBOT_VERSION_UNKNOWN = '';
const ROBOT_W3CVALIDATOR = 'W3C Validator';
const ROBOT_YAHOO_MM = 'Yahoo! Multimedia';
/**#@-*/
/**
* Version unknown constant.
*/
const VERSION_UNKNOWN = 'unknown';
/**
* @var string
* @access private
*/
private $_agent = '';
/**
* @var string
* @access private
*/
private $_browserName = '';
/**
* @var string
* @access private
*/
private $_compatibilityViewName = '';
/**
* @var string
* @access private
*/
private $_compatibilityViewVer = '';
/**
* @var array
* @access private
*/
private $_customBrowserDetection = array();
/**
* @var array
* @access private
*/
private $_customPlatformDetection = array();
/**
* @var array
* @access private
*/
private $_customRobotDetection = array();
/**
* @var boolean
* @access private
*/
private $_is64bit = false;
/**
* @var boolean
* @access private
*/
private $_isMobile = false;
/**
* @var boolean
* @access private
*/
private $_isRobot = false;
/**
* @var string
* @access private
*/
private $_platform = '';
/**
* @var string
* @access private
*/
private $_platformVersion = '';
/**
* @var string
* @access private
*/
private $_robotName = '';
/**
* @var string
* @access private
*/
private $_robotVersion = '';
/**
* @var string
* @access private
*/
private $_version = '';
//--- MAGIC METHODS ------------------------------------------------------------------------------------------------
/**
* BrowserDetection class constructor.
* @param string $useragent (optional) The user agent to work with. Leave empty for the current user agent
* (contained in $_SERVER['HTTP_USER_AGENT']).
*/
public function __construct($useragent = '')
{
$this->setUserAgent($useragent);
}
/**
* Determine how the class will react when it is treated like a string.
* @return string Returns an HTML formatted string with a summary of the browser informations.
*/
public function __toString()
{
$result = '';
$values = array();
$values[] = array('label' => 'User agent', 'value' => $this->getUserAgent());
$values[] = array('label' => 'Browser name', 'value' => $this->getName());
$values[] = array('label' => 'Browser version', 'value' => $this->getVersion());
$values[] = array('label' => 'Platform family', 'value' => $this->getPlatform());
$values[] = array('label' => 'Platform version', 'value' => $this->getPlatformVersion(true));
$values[] = array('label' => 'Platform version name', 'value' => $this->getPlatformVersion());
$values[] = array('label' => 'Platform is 64-bit', 'value' => $this->is64bitPlatform() ? 'true' : 'false');
$values[] = array('label' => 'Is mobile', 'value' => $this->isMobile() ? 'true' : 'false');
$values[] = array('label' => 'Is robot', 'value' => $this->isRobot() ? 'true' : 'false');
$values[] = array('label' => 'Robot name', 'value' => $this->isRobot() ? ($this->getRobotName() != self::ROBOT_UNKNOWN ? $this->getRobotName() : 'Unknown') : 'Not applicable');
$values[] = array('label' => 'Robot version', 'value' => $this->isRobot() ? ($this->getRobotVersion() != self::ROBOT_VERSION_UNKNOWN ? $this->getRobotVersion() : 'Unknown') : 'Not applicable');
$values[] = array('label' => 'IE is in compatibility view', 'value' => $this->isInIECompatibilityView() ? 'true' : 'false');
$values[] = array('label' => 'Emulated IE version', 'value' => $this->isInIECompatibilityView() ? $this->getIECompatibilityView() : 'Not applicable');
$values[] = array('label' => 'Is Chrome Frame', 'value' => $this->isChromeFrame() ? 'true' : 'false');
foreach ($values as $currVal) {
$result .= '<strong>' . htmlspecialchars($currVal['label'], ENT_NOQUOTES) . ':</strong> ' . $currVal['value'] . '<br />' . PHP_EOL;
}
return $result;
}
//--- PUBLIC MEMBERS -----------------------------------------------------------------------------------------------
/**
* Dynamically add support for a new Web browser.
* @param string $browserName The Web browser name (used for display).
* @param mixed $uaNameToLookFor (optional) The string (or array of strings) representing the browser name to find
* in the user agent. If omitted, $browserName will be used.
* @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
* @param string $separator (optional) The separator string used to split the browser name and the version number in
* the user agent.
* @param boolean $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the browser name can be found anywhere in the user agent string.
* @see removeCustomBrowserDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
*/
public function addCustomBrowserDetection($browserName, $uaNameToLookFor = '', $isMobile = false, $separator = '/', $uaNameFindWords = true)
{
if ($browserName == '') {
return false;
}
if (array_key_exists($browserName, $this->_customBrowserDetection)) {
unset($this->_customBrowserDetection[$browserName]);
}
if ($uaNameToLookFor == '') {
$uaNameToLookFor = $browserName;
}
$this->_customBrowserDetection[$browserName] = array('uaNameToLookFor' => $uaNameToLookFor, 'isMobile' => $isMobile == true,
'separator' => $separator, 'uaNameFindWords' => $uaNameFindWords == true);
return true;
}
/**
* Dynamically add support for a new platform.
* @param string $platformName The platform name (used for display).
* @param mixed $platformNameToLookFor (optional) The string (or array of strings) representing the platform name to
* find in the user agent. If omitted, $platformName will be used.
* @param boolean $isMobile (optional) Determines if the platform is from a mobile device.
* @param boolean $uaNameFindWords (optional) Determines if the platform name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* @see removeCustomPlatformDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
*/
public function addCustomPlatformDetection($platformName, $platformNameToLookFor = '', $isMobile = false, $uaNameFindWords = true)
{
if ($platformName == '') {
return false;
}
if (array_key_exists($platformName, $this->_customPlatformDetection)) {
unset($this->_customPlatformDetection[$platformName]);
}
if ($platformNameToLookFor == '') {
$platformNameToLookFor = $platformName;
}
$this->_customPlatformDetection[$platformName] = array('platformNameToLookFor' => $platformNameToLookFor,
'isMobile' => $isMobile == true,
'uaNameFindWords' => $uaNameFindWords == true);
return true;
}
/**
* Dynamically add support for a new robot.
* @param string $robotName The robot name (used for display).
* @param mixed $uaNameToLookFor (optional) The string (or array of strings) representing the robot name to find
* in the user agent. If omitted, $robotName will be used.
* @param boolean $isMobile (optional) Determines if the robot should be considered as mobile or not.
* @param string $separator (optional) The separator string used to split the robot name and the version number in
* the user agent.
* @param boolean $uaNameFindWords (optional) Determines if the robot name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the robot name can be found anywhere in the user agent string.
* @see removeCustomRobotDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
*/
public function addCustomRobotDetection($robotName, $uaNameToLookFor = '', $isMobile = false, $separator = '/', $uaNameFindWords = true)
{
if ($robotName == '') {
return false;
}
if (array_key_exists($robotName, $this->_customRobotDetection)) {
unset($this->_customRobotDetection[$robotName]);
}
if ($uaNameToLookFor == '') {
$uaNameToLookFor = $robotName;
}
$this->_customRobotDetection[$robotName] = array('uaNameToLookFor' => $uaNameToLookFor, 'isMobile' => $isMobile == true,
'separator' => $separator, 'uaNameFindWords' => $uaNameFindWords == true);
return true;
}
/**
* Compare two version number strings.
* @param string $sourceVer The source version number.
* @param string $compareVer The version number to compare with the source version number.
* @return int Returns -1 if $sourceVer < $compareVer, 0 if $sourceVer == $compareVer or 1 if $sourceVer >
* $compareVer.
*/
public function compareVersions($sourceVer, $compareVer)
{
$sourceVer = explode('.', $sourceVer);
foreach ($sourceVer as $k => $v) {
$sourceVer[$k] = $this->parseInt($v);
}
$compareVer = explode('.', $compareVer);
foreach ($compareVer as $k => $v) {
$compareVer[$k] = $this->parseInt($v);
}
if (count($sourceVer) != count($compareVer)) {
if (count($sourceVer) > count($compareVer)) {
for ($i = count($compareVer); $i < count($sourceVer); $i++) {
$compareVer[$i] = 0;
}
} else {
for ($i = count($sourceVer); $i < count($compareVer); $i++) {
$sourceVer[$i] = 0;
}
}
}
foreach ($sourceVer as $i => $srcVerPart) {
if ($srcVerPart > $compareVer[$i]) {
return 1;
} else {
if ($srcVerPart < $compareVer[$i]) {
return -1;
}
}
}
return 0;
}
/**
* Get the name and version of the browser emulated in the compatibility view mode (if any). Since Internet
* Explorer 8, IE can be put in compatibility mode to make websites that were created for older browsers, especially
* IE 6 and 7, look better in IE 8+ which renders web pages closer to the standards and thus differently from those
* older versions of IE.
* @param boolean $asArray (optional) Determines if the return value must be an array (true) or a string (false).
* @return mixed If a string was requested, the function returns the name and version of the browser emulated in
* the compatibility view mode or an empty string if the browser is not in compatibility view mode. If an array was
* requested, an array with the keys 'browser' and 'version' is returned.
*/
public function getIECompatibilityView($asArray = false)
{
if ($asArray) {
return array('browser' => $this->_compatibilityViewName, 'version' => $this->_compatibilityViewVer);
} else {
return trim($this->_compatibilityViewName . ' ' . $this->_compatibilityViewVer);
}
}
/**
* Return the BrowserDetection class version.
* @return string Returns the version as a sting with the #.#.# format.
*/
public function getLibVersion()
{
return '2.9.6';
}
/**
* Get the name of the browser. All of the return values are class constants. You can compare them like this:
* $myBrowserInstance->getName() == BrowserDetection::BROWSER_FIREFOX.
* @return string Returns the name of the browser or BrowserDetection::BROWSER_UNKNOWN if unknown.
*/
public function getName()
{
return $this->_browserName;
}
/**
* Get the name of the platform family on which the browser is run on (such as Windows, Apple, etc.). All of
* the return values are class constants. You can compare them like this:
* $myBrowserInstance->getPlatform() == BrowserDetection::PLATFORM_ANDROID.
* @return string Returns the name of the platform or BrowserDetection::PLATFORM_UNKNOWN if unknown.
*/
public function getPlatform()
{
return $this->_platform;
}
/**
* Get the platform version on which the browser is run on. It can be returned as a string number like 'NT 6.3' or
* as a name like 'Windows 8.1'. When returning version string numbers for Windows NT OS families the number is
* prefixed by 'NT ' to differentiate from older Windows 3.x & 9x release. At the moment only the Windows and
* Android operating systems are supported.
* @param boolean $returnVersionNumbers (optional) Determines if the return value must be versions numbers as a
* string (true) or the version name (false).
* @param boolean $returnServerFlavor (optional) Since some Windows NT versions have the same values, this flag
* determines if the Server flavor is returned or not. For instance Windows 8.1 and Windows Server 2012 R2 both use
* version 6.3. This parameter is only useful when testing for Windows.
* @return string Returns the version name/version numbers of the platform or the constant PLATFORM_VERSION_UNKNOWN
* if unknown.
*/
public function getPlatformVersion($returnVersionNumbers = false, $returnServerFlavor = false)
{
if ($this->_platformVersion == self::PLATFORM_VERSION_UNKNOWN || $this->_platformVersion == '') {
return self::PLATFORM_VERSION_UNKNOWN;
}
if ($returnVersionNumbers) {
return $this->_platformVersion;
} else {
switch ($this->getPlatform()) {
case self::PLATFORM_WINDOWS:
if (substr($this->_platformVersion, 0, 3) == 'NT ') {
return $this->windowsNTVerToStr(substr($this->_platformVersion, 3), $returnServerFlavor);
} else {
return $this->windowsVerToStr($this->_platformVersion);
}
break;
case self::PLATFORM_MACINTOSH:
return $this->macVerToStr($this->_platformVersion);
case self::PLATFORM_ANDROID:
return $this->androidVerToStr($this->_platformVersion);
case self::PLATFORM_IOS:
return $this->iOSVerToStr($this->_platformVersion);
default: return self::PLATFORM_VERSION_UNKNOWN;
}
}
}
/**
* Get the name of the robot. All of the return values are class constants. You can compare them like this:
* $myBrowserInstance->getRobotName() == BrowserDetection::ROBOT_GOOGLEBOT.
* @return string Returns the name of the robot or BrowserDetection::ROBOT_UNKNOWN if unknown.
*/
public function getRobotName()
{
return $this->_robotName;
}
/**
* Get the version of the robot.
* @return string Returns the version of the robot or BrowserDetection::ROBOT_VERSION_UNKNOWN if unknown.
*/
public function getRobotVersion()
{
return $this->_robotVersion;
}
/**
* Get the user agent value used by the class to determine the browser details.
* @return string The user agent string.
*/
public function getUserAgent()
{
return $this->_agent;
}
/**
* Get the version of the browser.
* @return string Returns the version of the browser or BrowserDetection::VERSION_UNKNOWN if unknown.
*/
public function getVersion()
{
return $this->_version;
}
/**
* Determine if the browser is executed from a 64-bit platform. Keep in mind that not all platforms/browsers report
* this and the result may not always be accurate.
* @return boolean Returns true if the browser is executed from a 64-bit platform.
*/
public function is64bitPlatform()
{
return $this->_is64bit;
}
/**
* Determine if the browser runs Google Chrome Frame (it's a plug-in designed for Internet Explorer 6+ based on the
* open-source Chromium project - it's like a Chrome browser within IE).
* @return boolean Returns true if the browser is using Google Chrome Frame, false otherwise.
*/
public function isChromeFrame()
{
return $this->containString($this->_agent, 'chromeframe');
}
/**
* Determine if the browser is in compatibility view or not. Since Internet Explorer 8, IE can be put in
* compatibility mode to make websites that were created for older browsers, especially IE 6 and 7, look better in
* IE 8+ which renders web pages closer to the standards and thus differently from those older versions of IE.
* @return boolean Returns true if the browser is in compatibility view, false otherwise.
*/
public function isInIECompatibilityView()
{
return ($this->_compatibilityViewName != '') || ($this->_compatibilityViewVer != '');
}
/**
* Determine if the browser is from a mobile device or not.
* @return boolean Returns true if the browser is from a mobile device, false otherwise.
*/
public function isMobile()
{
return $this->_isMobile;
}
/**
* Determine if the browser is a robot (Googlebot, Bingbot, Yahoo! Slurp...) or not.
* @return boolean Returns true if the browser is a robot, false otherwise.
*/
public function isRobot()
{
return $this->_isRobot;
}
/**
* Remove support for a previously added Web browser.
* @param string $browserName The Web browser name as used when added.
* @see addCustomBrowserDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
*/
public function removeCustomBrowserDetection($browserName)
{
if (array_key_exists($browserName, $this->_customBrowserDetection)) {
unset($this->_customBrowserDetection[$browserName]);
return true;
}
return false;
}
/**
* Remove support for a previously added platform.
* @param string $platformName The platform name as used when added.
* @see addCustomPlatformDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
*/
public function removeCustomPlatformDetection($platformName)
{
if (array_key_exists($platformName, $this->_customPlatformDetection)) {
unset($this->_customPlatformDetection[$platformName]);
return true;
}
return false;
}
/**
* Remove support for a previously added robot.
* @param string $robotName The robot name as used when added.
* @see addCustomRobotDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
*/
public function removeCustomRobotDetection($robotName)
{
if (array_key_exists($robotName, $this->_customRobotDetection)) {
unset($this->_customRobotDetection[$robotName]);
return true;
}
return false;
}
/**
* Set the user agent to use with the class.
* @param string $agentString (optional) The value of the user agent. If an empty string is sent (default),
* $_SERVER['HTTP_USER_AGENT'] will be used.
*/
public function setUserAgent($agentString = '')
{
if (!is_string($agentString) || trim($agentString) == '') {
//https://bugs.php.net/bug.php?id=49184
if (filter_has_var(INPUT_SERVER, 'HTTP_USER_AGENT')) {
$agentString = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
} else if (array_key_exists('HTTP_USER_AGENT', $_SERVER) && is_string($_SERVER['HTTP_USER_AGENT'])) {
$agentString = filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
} else {
$agentString = '';
}
if ($agentString === false || $agentString === NULL) {
//filter_input or filter_var failed
$agentString = '';
}
}
$this->reset();
$this->_agent = $agentString;
$this->detect();
}
//--- PROTECTED MEMBERS --------------------------------------------------------------------------------------------
/**
* Convert the Android version numbers to the operating system name. For instance '1.6' returns 'Donut'.
* @access protected
* @param string $androidVer The Android version numbers as a string.
* @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
* numbers.
*/
protected function androidVerToStr($androidVer)
{
//https://en.wikipedia.org/wiki/Android_version_history
if ($this->compareVersions($androidVer, '10') >= 0) {
$majorVer = strstr($androidVer, '.', true);
if ($majorVer == '') {
$majorVer = $androidVer;
}
return self::BROWSER_ANDROID . ' ' . $majorVer;
} else if ($this->compareVersions($androidVer, '9') >= 0 && $this->compareVersions($androidVer, '10') < 0) {
return 'Pie';
} else if ($this->compareVersions($androidVer, '8') >= 0 && $this->compareVersions($androidVer, '9') < 0) {
return 'Oreo';
} else if ($this->compareVersions($androidVer, '7') >= 0 && $this->compareVersions($androidVer, '8') < 0) {
return 'Nougat';
} else if ($this->compareVersions($androidVer, '6') >= 0 && $this->compareVersions($androidVer, '7') < 0) {
return 'Marshmallow';
} else if ($this->compareVersions($androidVer, '5') >= 0 && $this->compareVersions($androidVer, '5.2') < 0) {
return 'Lollipop';
} else if ($this->compareVersions($androidVer, '4.4') >= 0 && $this->compareVersions($androidVer, '4.5') < 0) {
return 'KitKat';
} else if ($this->compareVersions($androidVer, '4.1') >= 0 && $this->compareVersions($androidVer, '4.4') < 0) {
return 'Jelly Bean';
} else if ($this->compareVersions($androidVer, '4') >= 0 && $this->compareVersions($androidVer, '4.1') < 0) {
return 'Ice Cream Sandwich';
} else if ($this->compareVersions($androidVer, '3') >= 0 && $this->compareVersions($androidVer, '3.3') < 0) {
return 'Honeycomb';
} else if ($this->compareVersions($androidVer, '2.3') >= 0 && $this->compareVersions($androidVer, '2.4') < 0) {
return 'Gingerbread';
} else if ($this->compareVersions($androidVer, '2.2') >= 0 && $this->compareVersions($androidVer, '2.3') < 0) {
return 'Froyo';
} else if ($this->compareVersions($androidVer, '2') >= 0 && $this->compareVersions($androidVer, '2.2') < 0) {
return 'Eclair';
} else if ($this->compareVersions($androidVer, '1.6') == 0) {
return 'Donut';
} else if ($this->compareVersions($androidVer, '1.5') == 0) {
return 'Cupcake';
} else {
return self::PLATFORM_VERSION_UNKNOWN; //Unknown/unnamed Android version
}
}
/**
* Determine if the browser is the Android browser (based on the WebKit layout engine and coupled with Chrome's
* JavaScript engine) or not.
* @access protected
* @return boolean Returns true if the browser is the Android browser, false otherwise.
*/
protected function checkBrowserAndroid()
{
//Android don't use the standard "Android/1.0", it uses "Android 1.0;" instead
return $this->checkSimpleBrowserUA('Android', $this->_agent, self::BROWSER_ANDROID, true);
}
/**
* Determine if the browser is the BlackBerry browser or not.
* @access protected
* @link https://web.archive.org/web/20170328000854/http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/How-to-detect-the-BlackBerry-Browser/ta-p/559862
* @return boolean Returns true if the browser is the BlackBerry browser, false otherwise.
*/
protected function checkBrowserBlackBerry()
{
$found = false;
//Tablet OS check
if ($this->checkSimpleBrowserUA('RIM Tablet OS', $this->_agent, self::BROWSER_TABLET_OS, true)) {
return true;
}
//Version 6, 7 & 10 check (versions 8 & 9 does not exists)
if ($this->checkBrowserUAWithVersion(array('BlackBerry', 'BB10'), $this->_agent, self::BROWSER_BLACKBERRY, true)) {
if ($this->getVersion() == self::VERSION_UNKNOWN) {
$found = true;
} else {
return true;
}
}
//Version 4.2 to 5.0 check
if ($this->checkSimpleBrowserUA('BlackBerry', $this->_agent, self::BROWSER_BLACKBERRY, true, '/', false)) {
if ($this->getVersion() == self::VERSION_UNKNOWN) {
$found = true;
} else {
return true;
}
}
return $found;
}
/**
* Determine if the browser is Chrome or not.
* @access protected
* @link https://www.google.com/chrome/
* @return boolean Returns true if the browser is Chrome, false otherwise.
*/
protected function checkBrowserChrome()
{
return $this->checkSimpleBrowserUA(array('Chrome', 'CriOS'), $this->_agent, self::BROWSER_CHROME);
}
/**
* Determine if the browser is among the custom browser rules or not. Rules are checked in the order they were
* added.
* @access protected
* @return boolean Returns true if we found the browser we were looking for in the custom rules, false otherwise.
*/
protected function checkBrowserCustom()
{
foreach ($this->_customBrowserDetection as $browserName => $customBrowser) {
$uaNameToLookFor = $customBrowser['uaNameToLookFor'];
$isMobile = $customBrowser['isMobile'];
$separator = $customBrowser['separator'];
$uaNameFindWords = $customBrowser['uaNameFindWords'];
if ($this->checkSimpleBrowserUA($uaNameToLookFor, $this->_agent, $browserName, $isMobile, $separator, $uaNameFindWords)) {
return true;
}
}
return false;
}
/**
* Determine if the browser is Edge or not.
* @access protected
* @return boolean Returns true if the browser is Edge, false otherwise.
*/
protected function checkBrowserEdge()
{
return $this->checkSimpleBrowserUA(array('Edg', 'Edge', 'EdgA'), $this->_agent, self::BROWSER_EDGE);
}
/**
* Determine if the browser is Firebird or not. Firebird was the name of Firefox from version 0.6 to 0.7.1.
* @access protected
* @return boolean Returns true if the browser is Firebird, false otherwise.
*/
protected function checkBrowserFirebird()
{
return $this->checkSimpleBrowserUA('Firebird', $this->_agent, self::BROWSER_FIREBIRD);
}
/**
* Determine if the browser is Firefox or not.
* @access protected
* @link https://www.mozilla.org/en-US/firefox/new/
* @return boolean Returns true if the browser is Firefox, false otherwise.
*/
protected function checkBrowserFirefox()
{
//Safari heavily matches with Firefox, ensure that Safari is filtered out...
if (preg_match('/.*Firefox[ (\/]*([a-z0-9.-]*)/i', $this->_agent, $matches) &&
!$this->containString($this->_agent, 'Safari')) {
$this->setBrowser(self::BROWSER_FIREFOX);
$this->setVersion($matches[1]);
$this->setMobile(false);
$this->setRobot(false);
return true;
}
return false;
}
/**
* Determine if the browser is iCab or not.
* @access protected
* @link http://www.icab.de/
* @return boolean Returns true if the browser is iCab, false otherwise.
*/
protected function checkBrowserIcab()
{
//Some (early) iCab versions don't use the standard "iCab/1.0", they uses "iCab 1.0;" instead
return $this->checkSimpleBrowserUA('iCab', $this->_agent, self::BROWSER_ICAB);
}
/**
* Determine if the browser is GNU IceCat (formerly known as GNU IceWeasel) or not.
* @access protected
* @link https://www.gnu.org/software/gnuzilla/
* @return boolean Returns true if the browser is GNU IceCat, false otherwise.
*/
protected function checkBrowserIceCat()
{
return $this->checkSimpleBrowserUA('IceCat', $this->_agent, self::BROWSER_ICECAT);
}
/**
* Determine if the browser is GNU IceWeasel (now know as GNU IceCat) or not.
* @access protected
* @see checkBrowserIceCat()
* @return boolean Returns true if the browser is GNU IceWeasel, false otherwise.
*/
protected function checkBrowserIceWeasel()
{
return $this->checkSimpleBrowserUA('Iceweasel', $this->_agent, self::BROWSER_ICEWEASEL);
}
/**
* Determine if the browser is Internet Explorer or not.
* @access protected
* @link https://www.microsoft.com/ie/
* @link https://en.wikipedia.org/wiki/Internet_Explorer_Mobile
* @return boolean Returns true if the browser is Internet Explorer, false otherwise.
*/
protected function checkBrowserInternetExplorer()
{
//Test for Internet Explorer Mobile (formerly Pocket Internet Explorer)
if ($this->checkSimpleBrowserUA(array('IEMobile', 'MSPIE'), $this->_agent, self::BROWSER_IE_MOBILE, true)) {
return true;
}
//Several browsers uses IE compatibility UAs filter these browsers out (but after testing for IE Mobile)
if ($this->containString($this->_agent, 'Opera') || $this->containString($this->_agent, array('BlackBerry', 'Nokia'), true, false)) {
return false;
}
//Test for Internet Explorer 1
if ($this->checkSimpleBrowserUA('Microsoft Internet Explorer', $this->_agent, self::BROWSER_IE)) {
if ($this->getVersion() == self::VERSION_UNKNOWN) {
if (preg_match('/308|425|426|474|0b1/i', $this->_agent)) {
$this->setVersion('1.5');
} else {
$this->setVersion('1.0');
}
}
return true;
}
//Test for Internet Explorer 2+
if ($this->containString($this->_agent, array('MSIE', 'Trident'))) {
$version = '';