-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathUserAgentParser.php
More file actions
244 lines (211 loc) · 8.38 KB
/
Copy pathUserAgentParser.php
File metadata and controls
244 lines (211 loc) · 8.38 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
<?php
/**
* @author Jesse G. Donat <donatj@gmail.com>
*
* @link https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
* @link https://github.com/donatj/PhpUserAgent
*
* @license MIT https://github.com/donatj/PhpUserAgent/blob/master/LICENSE.md
*/
namespace {
/**
* Parses a user agent string into its important parts
*
* This method is defined for backwards comparability with the old global method.
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return string[] an array with 'browser', 'version' and 'platform' keys
* @throws \InvalidArgumentException on not having a proper user agent to parse.
*
* @deprecated This exists for backwards compatibility with 0.x and will likely be removed in 2.x
* @see \donatj\UserAgent\parse_user_agent
*/
function parse_user_agent( $u_agent = null ) {
return \donatj\UserAgent\parse_user_agent($u_agent);
}
}
namespace donatj\UserAgent {
const PLATFORM = 'platform';
const BROWSER = 'browser';
const BROWSER_VERSION = 'version';
/**
* Parses a user agent string into its important parts
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return string[] an array with 'browser', 'version' and 'platform' keys
* @throws \InvalidArgumentException on not having a proper user agent to parse.
*/
function parse_user_agent( $u_agent = null ) {
if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) {
$u_agent = (string)$_SERVER['HTTP_USER_AGENT'];
}
if( $u_agent === null ) {
throw new \InvalidArgumentException('parse_user_agent requires a user agent');
}
$platform = null;
$browser = null;
$version = null;
$return = [ PLATFORM => &$platform, BROWSER => &$browser, BROWSER_VERSION => &$version ];
if( !$u_agent ) {
return $return;
}
if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) {
preg_match_all(<<<'REGEX'
/(?P<platform>BB\d+;|Android|Adr|Symbian|Sailfish|CrOS|Fuchsia|Tizen|iPhone|iPad|iPod|Linux|(?:Open|Net|Free)BSD|Macintosh|
Windows(?:\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|X11|(?:New\ )?Nintendo\ (?:WiiU?|3?DS|Switch)|Xbox(?:\ One)?)
(?:\ [^;]*)?
(?:;|$)/imx
REGEX
, $parent_matches[1], $result);
$priority = [ 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'Fuchsia', 'X11', 'Sailfish' ];
$result[PLATFORM] = array_unique($result[PLATFORM]);
if( count($result[PLATFORM]) > 1 ) {
if( $keys = array_intersect($priority, $result[PLATFORM]) ) {
$platform = reset($keys);
} else {
$platform = $result[PLATFORM][0];
}
} elseif( isset($result[PLATFORM][0]) ) {
$platform = $result[PLATFORM][0];
}
}
if( $platform == 'linux-gnu' || $platform == 'X11' ) {
$platform = 'Linux';
} elseif( $platform == 'CrOS' ) {
$platform = 'Chrome OS';
} elseif( $platform == 'Adr' ) {
$platform = 'Android';
} elseif( $platform === null ) {
if( preg_match_all('%(?P<platform>Android)[:/ ]%ix', $u_agent, $result) ) {
$platform = $result[PLATFORM][0];
}
}
preg_match_all(<<<'REGEX'
%(?P<prev>.)?(?P<browser>Camino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit|
TizenBrowser|(?:Headless)?Chrome|YaBrowser|Vivaldi|IEMobile|Opera|OPR|Silk|Midori|(?-i:Edge)|EdgA?|CriOS|UCBrowser|Puffin|
OculusBrowser|SamsungBrowser|SailfishBrowser|XiaoMi/MiuiBrowser|YaApp_Android|Whale|
Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|
Valve\ Steam\ Tenfoot|Mastodon|
NintendoBrowser|PLAYSTATION\ (?:\d|Vita)+)
\)?;?
(?:[:/ ](?P<version>[0-9A-Z.]+)|/[A-Z]*)
%ix
REGEX
, $u_agent, $result);
// If nothing matched, return null (to avoid undefined index errors)
if( !isset($result[BROWSER][0], $result[BROWSER_VERSION][0])
&& preg_match('%^(?!Mozilla)(?P<browser>[A-Z0-9\-]+)([/ :](?P<version>[0-9A-Z.]+))?%ix', $u_agent, $g_result) ) {
return [ PLATFORM => $platform, BROWSER => $g_result[BROWSER], BROWSER_VERSION => empty($g_result[BROWSER_VERSION]) ? null : $g_result[BROWSER_VERSION] ];
}
if(
(
empty($result[BROWSER][0])
|| ($result['prev'][0] !== '')
)
&& preg_match(<<<'REGEX'
%[(;]\s*(?P<browser>[^(/;]+)
(?:[:/ ]v?(?P<version>[0-9A-Z.]+)[^;)\s]*)?
;?(?:\s*robot;)?\s*\+https?:%x
REGEX
, $u_agent, $bot_result)
) {
return [
PLATFORM => $platform,
BROWSER => trim($bot_result['browser']),
BROWSER_VERSION => empty($bot_result['version']) ? null : $bot_result['version'],
];
}
if( !isset($result[BROWSER][0], $result[BROWSER_VERSION][0]) ) {
return $return;
}
if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/i', $u_agent, $rv_result) ) {
$rv_result = $rv_result[BROWSER_VERSION];
}
$browser = $result[BROWSER][0];
$version = $result[BROWSER_VERSION][0];
$lowerBrowser = array_map('strtolower', $result[BROWSER]);
$find = function( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) {
$search = (array)$search;
foreach( $search as $val ) {
$xkey = array_search(strtolower($val), $lowerBrowser);
if( $xkey !== false ) {
$value = $val;
$key = $xkey;
return true;
}
}
return false;
};
$findT = function( array $search, &$key = null, &$value = null ) use ( $find ) {
$value2 = null;
if( $find(array_keys($search), $key, $value2) ) {
$value = $search[$value2];
return true;
}
return false;
};
$key = 0;
$val = '';
if( $findT([ 'OPR' => 'Opera', 'Facebot' => 'iMessageBot', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'YaApp_Android' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge', 'EdgA' => 'Edge', 'XiaoMi/MiuiBrowser' => 'MiuiBrowser' ], $key, $browser) ) {
$version = is_numeric(substr($result[BROWSER_VERSION][$key], 0, 1)) ? $result[BROWSER_VERSION][$key] : null;
} elseif( $find('Playstation Vita', $key, $platform) ) {
$platform = 'PlayStation Vita';
$browser = 'Browser';
} elseif( $find([ 'Kindle Fire', 'Silk' ], $key, $val) ) {
$browser = $val == 'Silk' ? 'Silk' : 'Kindle';
$platform = 'Kindle Fire';
if( !($version = $result[BROWSER_VERSION][$key]) || !is_numeric($version[0]) ) {
$version = $result[BROWSER_VERSION][array_search('Version', $result[BROWSER])];
}
} elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) {
$browser = 'NintendoBrowser';
$version = $result[BROWSER_VERSION][$key];
} elseif( $find([ 'Kindle' ], $key, $platform) ) {
$browser = $result[BROWSER][$key];
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Opera', $key, $browser) ) {
$find('Version', $key);
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Puffin', $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
if( strlen($version) > 3 ) {
$part = substr($version, -2);
if( ctype_upper($part) ) {
$version = substr($version, 0, -2);
$flags = [ 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ];
if( isset($flags[$part]) ) {
$platform = $flags[$part];
}
}
}
} elseif( $find([ 'Googlebot', 'Applebot', 'IEMobile', 'Edge', 'Midori', 'Whale', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome', 'SailfishBrowser' ], $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
} elseif( $rv_result && $find('Trident') ) {
$browser = 'MSIE';
$version = $rv_result;
} elseif( $browser == 'AppleWebKit' ) {
if( $platform == 'Android' ) {
$browser = 'Android Browser';
} elseif( strpos((string)$platform, 'BB') === 0 ) {
$browser = 'BlackBerry Browser';
$platform = 'BlackBerry';
} elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) {
$browser = 'BlackBerry Browser';
} elseif( $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
} elseif( count($result[BROWSER]) ) {
$key = count($result[BROWSER]) - 1;
$browser = $result[BROWSER][$key];
$version = $result[BROWSER_VERSION][$key];
}
if( $find('Version', $key) ) {
$version = $result[BROWSER_VERSION][$key];
}
} elseif( $pKey = preg_grep('/playstation \d/i', $result[BROWSER]) ) {
$pKey = reset($pKey);
$platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey);
$browser = 'NetFront';
}
return $return;
}
}