Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
+35
-23
@@ -3,20 +3,24 @@
|
||||
/**
|
||||
* Parses a user agent string into its important parts
|
||||
*
|
||||
* @author Jesse G. Donat <donatj@gmail.com>
|
||||
* @link https://github.com/donatj/PhpUserAgent
|
||||
* @link http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
|
||||
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
|
||||
* @throws \InvalidArgumentException on not having a proper user agent to parse.
|
||||
* @return string[] an array with browser, version and platform keys
|
||||
* @throws \InvalidArgumentException on not having a proper user agent to parse.
|
||||
*
|
||||
* @author Jesse G. Donat <donatj@gmail.com>
|
||||
*
|
||||
* @link https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
|
||||
* @link https://github.com/donatj/PhpUserAgent
|
||||
*
|
||||
* @license MIT
|
||||
*/
|
||||
function parse_user_agent( $u_agent = null ) {
|
||||
if( is_null($u_agent) ) {
|
||||
if( isset($_SERVER['HTTP_USER_AGENT']) ) {
|
||||
$u_agent = $_SERVER['HTTP_USER_AGENT'];
|
||||
} else {
|
||||
throw new \InvalidArgumentException('parse_user_agent requires a user agent');
|
||||
}
|
||||
if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) {
|
||||
$u_agent = $_SERVER['HTTP_USER_AGENT'];
|
||||
}
|
||||
|
||||
if( $u_agent === null ) {
|
||||
throw new \InvalidArgumentException('parse_user_agent requires a user agent');
|
||||
}
|
||||
|
||||
$platform = null;
|
||||
@@ -25,14 +29,16 @@ function parse_user_agent( $u_agent = null ) {
|
||||
|
||||
$empty = array( 'platform' => $platform, 'browser' => $browser, 'version' => $version );
|
||||
|
||||
if( !$u_agent ) return $empty;
|
||||
if( !$u_agent ) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
if( preg_match('/\((.*?)\)/im', $u_agent, $parent_matches) ) {
|
||||
preg_match_all('/(?P<platform>BB\d+;|Android|CrOS|Tizen|iPhone|iPad|iPod|Linux|Macintosh|Windows(\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|X11|(New\ )?Nintendo\ (WiiU?|3?DS)|Xbox(\ One)?)
|
||||
if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) {
|
||||
preg_match_all('/(?P<platform>BB\d+;|Android|CrOS|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', $parent_matches[1], $result, PREG_PATTERN_ORDER);
|
||||
(?:;|$)/imx', $parent_matches[1], $result);
|
||||
|
||||
$priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'CrOS', 'X11' );
|
||||
$priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' );
|
||||
|
||||
$result['platform'] = array_unique($result['platform']);
|
||||
if( count($result['platform']) > 1 ) {
|
||||
@@ -53,13 +59,13 @@ function parse_user_agent( $u_agent = null ) {
|
||||
}
|
||||
|
||||
preg_match_all('%(?P<browser>Camino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit|
|
||||
TizenBrowser|Chrome|Vivaldi|IEMobile|Opera|OPR|Silk|Midori|Edge|CriOS|UCBrowser|Puffin|SamsungBrowser|
|
||||
TizenBrowser|(?:Headless)?Chrome|YaBrowser|Vivaldi|IEMobile|Opera|OPR|Silk|Midori|Edge|Edg|CriOS|UCBrowser|Puffin|OculusBrowser|SamsungBrowser|
|
||||
Baiduspider|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl|
|
||||
Valve\ Steam\ Tenfoot|
|
||||
NintendoBrowser|PLAYSTATION\ (\d|Vita)+)
|
||||
(?:\)?;?)
|
||||
(?:(?:[:/ ])(?P<version>[0-9A-Z.]+)|/(?:[A-Z]*))%ix',
|
||||
$u_agent, $result, PREG_PATTERN_ORDER);
|
||||
$u_agent, $result);
|
||||
|
||||
// If nothing matched, return null (to avoid undefined index errors)
|
||||
if( !isset($result['browser'][0]) || !isset($result['version'][0]) ) {
|
||||
@@ -70,7 +76,7 @@ function parse_user_agent( $u_agent = null ) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/si', $u_agent, $rv_result) ) {
|
||||
if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/i', $u_agent, $rv_result) ) {
|
||||
$rv_result = $rv_result['version'];
|
||||
}
|
||||
|
||||
@@ -115,7 +121,7 @@ function parse_user_agent( $u_agent = null ) {
|
||||
$browser = $result['browser'][$key];
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $find('OPR', $key) ) {
|
||||
$browser = 'Opera Next';
|
||||
$browser = 'Opera';
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $find('Opera', $key, $browser) ) {
|
||||
$find('Version', $key);
|
||||
@@ -133,7 +139,13 @@ function parse_user_agent( $u_agent = null ) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome' ), $key, $browser) ) {
|
||||
} elseif( $find('YaBrowser', $key, $browser) ) {
|
||||
$browser = 'Yandex';
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $find(array( 'Edge', 'Edg' ), $key, $browser) ) {
|
||||
$browser = 'Edge';
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $find(array( 'IEMobile', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) {
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $rv_result && $find('Trident', $key) ) {
|
||||
$browser = 'MSIE';
|
||||
@@ -145,7 +157,7 @@ function parse_user_agent( $u_agent = null ) {
|
||||
$browser = 'Chrome';
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $browser == 'AppleWebKit' ) {
|
||||
if( $platform == 'Android' && !($key = 0) ) {
|
||||
if( $platform == 'Android' ) {
|
||||
$browser = 'Android Browser';
|
||||
} elseif( strpos($platform, 'BB') === 0 ) {
|
||||
$browser = 'BlackBerry Browser';
|
||||
@@ -158,10 +170,10 @@ function parse_user_agent( $u_agent = null ) {
|
||||
|
||||
$find('Version', $key);
|
||||
$version = $result['version'][$key];
|
||||
} elseif( $pKey = preg_grep('/playstation \d/i', array_map('strtolower', $result['browser'])) ) {
|
||||
} elseif( $pKey = preg_grep('/playstation \d/i', $result['browser']) ) {
|
||||
$pKey = reset($pKey);
|
||||
|
||||
$platform = 'PlayStation ' . preg_replace('/[^\d]/i', '', $pKey);
|
||||
$platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey);
|
||||
$browser = 'NetFront';
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user