Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UASystem.php
Go to the documentation of this file.
1<?php
2
3
4namespace Ubiquity\utils\base;
5
15class UASystem {
16
17 const BROWSERS=['MSIE'=>'Internet Explorer','Firefox'=>'Mozilla Firefox','Edg'=>'Microsoft Edge','OPR'=>'Opera','Chrome'=>'Google Chrome','Safari'=>'Apple Safari','Netscape'=>'Netscape'];
18
19 const SYSTEMS=['linux'=>'linux','mac'=>'macintosh|mac os x','windows'=>'windows|win32'];
20
21 private static $browserInfos;
22
23 private static function getBrowserInfos() {
24 $userAgent = $_SERVER['HTTP_USER_AGENT'];
25 $browserName = 'Unknown';
26 $platform = self::getPlatformFromUserAgent($userAgent);
27 $version = '';
28
29 foreach (self::BROWSERS as $k=>$name){
30 if(\preg_match("/$k/i",$userAgent)) {
31 $browserName = $name;
32 $ub = $k;
33 break;
34 }
35 }
36
37 $known = ['Version', $ub, 'other'];
38 $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
39 \preg_match_all($pattern, $userAgent, $matches);
40
41 $i = \count($matches['browser']);
42 if ($i != 1) {
43 if (\strripos($userAgent,'Version') < \strripos($userAgent,$ub)){
44 $version= $matches['version'][0]??null;
45 }
46 else {
47 $version= $matches['version'][1]??null;
48 }
49 }
50 else {
51 $version= $matches['version'][0]??null;
52 }
53
54 $version??='?';
55
56 return [
57 'userAgent' => $userAgent,
58 'name' => $browserName,
59 'version' => $version,
60 'platform' => $platform,
61 'pattern' => $pattern
62 ];
63 }
64
65 private static function getPlatformFromUserAgent(string $userAgent):string{
66 $platform='Unknown';
67 foreach (self::SYSTEMS as $name=>$reg){
68 if (\preg_match("/$reg/i", $userAgent)) {
69 $platform = $name;
70 }
71 }
72 return $platform;
73 }
74 private static function _getBrowser():array{
75 return self::$browserInfos??=self::getBrowserInfos();
76 }
77 public static function getBrowserComplete():string{
78 $b=self::_getBrowser();
79 return $b['name'].' '.$b['version'];
80 }
81
82 public static function getBrowserName():string{
83 return self::_getBrowser()['name'];
84 }
85
86 public static function getBrowserVersion():string{
87 return self::_getBrowser()['version'];
88 }
89
90 public static function getPlatform():string{
91 return self::_getBrowser()['platform'];
92 }
93}
User agent detection.
Definition UASystem.php:15
static getPlatformFromUserAgent(string $userAgent)
Definition UASystem.php:65