|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace jeremykenedy\LaravelLogger\App\Http\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait UserAgentDetails |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get the user's agents details. |
|
9
|
|
|
* |
|
10
|
|
|
* @param $ua |
|
11
|
|
|
* |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function details($ua) |
|
15
|
|
|
{ |
|
16
|
|
|
$ua = is_null($ua) ? $_SERVER['HTTP_USER_AGENT'] : $ua; |
|
17
|
|
|
// Enumerate all common platforms, this is usually placed in braces (order is important! First come first serve..) |
|
|
|
|
|
|
18
|
|
|
$platforms = 'Windows|iPad|iPhone|Macintosh|Android|BlackBerry|Unix|Linux'; |
|
19
|
|
|
|
|
20
|
|
|
// All browsers except MSIE/Trident and.. |
|
21
|
|
|
// NOT for browsers that use this syntax: Version/0.xx Browsername |
|
22
|
|
|
$browsers = 'Firefox|Chrome|Opera'; |
|
23
|
|
|
|
|
24
|
|
|
// Specifically for browsers that use this syntax: Version/0.xx Browername |
|
25
|
|
|
$browsers_v = 'Safari|Mobile'; // Mobile is mentioned in Android and BlackBerry UA's |
|
26
|
|
|
|
|
27
|
|
|
// Fill in your most common engines.. |
|
28
|
|
|
$engines = 'Gecko|Trident|Webkit|Presto'; |
|
29
|
|
|
|
|
30
|
|
|
// Regex the crap out of the user agent, making multiple selections and.. |
|
31
|
|
|
$regex_pat = "/((Mozilla)\/[\d\.]+|(Opera)\/[\d\.]+)\s\(.*?((MSIE)\s([\d\.]+).*?(Windows)|({$platforms})).*?\s.*?({$engines})[\/\s]+[\d\.]+(\;\srv\:([\d\.]+)|.*?).*?(Version[\/\s]([\d\.]+)(.*?({$browsers_v})|$)|(({$browsers})[\/\s]+([\d\.]+))|$).*/i"; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
// .. placing them in this order, delimited by | |
|
34
|
|
|
$replace_pat = '$7$8|$2$3|$9|${17}${15}$5$3|${18}${13}$6${11}'; |
|
35
|
|
|
|
|
36
|
|
|
// Run the preg_replace .. and explode on | |
|
37
|
|
|
$ua_array = explode('|', preg_replace($regex_pat, $replace_pat, $ua, PREG_PATTERN_ORDER)); |
|
38
|
|
|
|
|
39
|
|
|
if (count($ua_array) > 1) { |
|
40
|
|
|
$return['platform'] = $ua_array[0]; // Windows / iPad / MacOS / BlackBerry |
|
|
|
|
|
|
41
|
|
|
$return['type'] = $ua_array[1]; // Mozilla / Opera etc. |
|
42
|
|
|
$return['renderer'] = $ua_array[2]; // WebKit / Presto / Trident / Gecko etc. |
|
43
|
|
|
$return['browser'] = $ua_array[3]; // Chrome / Safari / MSIE / Firefox |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
Not necessary but this will filter out Chromes ridiculously long version |
|
47
|
|
|
numbers 31.0.1234.122 becomes 31.0, while a "normal" 3 digit version number |
|
48
|
|
|
like 10.2.1 would stay 10.2.1, 11.0 stays 11.0. Non-match stays what it is. |
|
49
|
|
|
*/ |
|
50
|
|
|
if (preg_match("/^[\d]+\.[\d]+(?:\.[\d]{0,2}$)?/", $ua_array[4], $matches)) { |
|
51
|
|
|
$return['version'] = $matches[0]; |
|
52
|
|
|
} else { |
|
53
|
|
|
$return['version'] = $ua_array[4]; |
|
54
|
|
|
} |
|
55
|
|
|
} else { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Replace some browsernames e.g. MSIE -> Internet Explorer |
|
60
|
|
|
switch (strtolower($return['browser'])) { |
|
61
|
|
|
case 'msie': |
|
62
|
|
|
case 'trident': |
|
63
|
|
|
$return['browser'] = 'Internet Explorer'; |
|
64
|
|
|
break; |
|
65
|
|
|
case '': // IE 11 is a steamy turd (thanks Microsoft...) |
|
66
|
|
|
if (strtolower($return['renderer']) == 'trident') { |
|
67
|
|
|
$return['browser'] = 'Internet Explorer'; |
|
68
|
|
|
} |
|
69
|
|
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
switch (strtolower($return['platform'])) { |
|
73
|
|
|
case 'android': // These browsers claim to be Safari but are BB Mobile |
|
74
|
|
|
case 'blackberry': // and Android Mobile |
|
75
|
|
|
if ($return['browser'] == 'Safari' || $return['browser'] == 'Mobile' || $return['browser'] == '') { |
|
|
|
|
|
|
76
|
|
|
$return['browser'] = "{$return['platform']} mobile"; |
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Return the locales language from PHP's Local |
|
86
|
|
|
* http://php.net/manual/en/class.locale.php |
|
87
|
|
|
* http://php.net/manual/en/locale.acceptfromhttp.php. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $locale :: LIKE "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" > return 'fr-FR'; |
|
90
|
|
|
* Fallback if No Locale CLASS @sudwebdesign |
|
91
|
|
|
* |
|
92
|
|
|
* @return string (Example: "en_US") |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function localeLang($locale) |
|
95
|
|
|
{ |
|
96
|
|
|
if (class_exists('Locale')) { |
|
97
|
|
|
return \Locale::acceptFromHttp($locale); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$a = explode(',', $locale); |
|
101
|
|
|
$a = explode(';', $a[1]); |
|
102
|
|
|
|
|
103
|
|
|
return $a[0]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.