From 5ab8d46a79f50291905c4f45cfeb69ee31ec1394 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 12:03:30 -0500 Subject: [PATCH] Backport updates from v1.7.0 --- src/UserAgentParser.php | 44 +++++++++++++++++++++++-------------- tests/user_agents.dist.json | 5 +++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index f3b0fa3..77d679a 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -18,7 +18,7 @@ */ function parse_user_agent( $u_agent = null ) { if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { - $u_agent = $_SERVER['HTTP_USER_AGENT']; + $u_agent = (string)$_SERVER['HTTP_USER_AGENT']; } if( $u_agent === null ) { @@ -29,16 +29,20 @@ function parse_user_agent( $u_agent = null ) { $browser = null; $version = null; - $empty = array( 'platform' => $platform, 'browser' => $browser, 'version' => $version ); + $return = array( 'platform' => $platform, 'browser' => $browser, 'version' => $version ); if( !$u_agent ) { - return $empty; + return $return; } if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { - preg_match_all('/(?PBB\d+;|Android|Adr|Symbian|Sailfish|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_match_all(<<<'REGEX' +/(?PBB\d+;|Android|Adr|Symbian|Sailfish|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 +REGEX +, $parent_matches[1], $result); $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11', 'Sailfish' ); @@ -60,17 +64,23 @@ function parse_user_agent( $u_agent = null ) { $platform = 'Chrome OS'; } elseif( $platform == 'Adr' ) { $platform = 'Android'; + } elseif( $platform === null ) { + if(preg_match_all('%(?PAndroid)[:/ ]%ix', $u_agent, $result)) { + $platform = $result['platform'][0]; + } } - preg_match_all('%(?PCamino|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| - Baiduspider|Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl| - Valve\ Steam\ Tenfoot| - NintendoBrowser|PLAYSTATION\ (\d|Vita)+) - (?:\)?;?) - (?:(?:[:/ ])(?P[\dA-Z.]+)|/(?:[A-Z]*))%ix', - $u_agent, $result); + preg_match_all(<<<'REGEX' +%(?PCamino|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| +Baiduspider|Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl| +Valve\ Steam\ Tenfoot| +NintendoBrowser|PLAYSTATION\ (?:\d|Vita)+) +\)?;? +(?:[:/ ](?P[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]) || !isset($result['version'][0]) ) { @@ -78,7 +88,7 @@ function parse_user_agent( $u_agent = null ) { return array( 'platform' => $platform ?: null, 'browser' => $result['browser'], 'version' => isset($result['version']) ? $result['version'] ?: null : null ); } - return $empty; + return $return; } if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { @@ -160,7 +170,7 @@ function parse_user_agent( $u_agent = null ) { } elseif( $browser == 'AppleWebKit' ) { if( $platform == 'Android' ) { $browser = 'Android Browser'; - } elseif( strpos($platform, 'BB') === 0 ) { + } elseif( strpos((string)$platform, 'BB') === 0 ) { $browser = 'BlackBerry Browser'; $platform = 'BlackBerry'; } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { diff --git a/tests/user_agents.dist.json b/tests/user_agents.dist.json index b813e52..1bcd6a3 100644 --- a/tests/user_agents.dist.json +++ b/tests/user_agents.dist.json @@ -619,6 +619,11 @@ "browser": "Android Browser", "version": "4.0" }, + "Lenovo-A880\/S100 Linux\/3.4.5 Android\/4.2 Release\/08.07.2013 Browser\/AppleWebKit 534.30 Profile\/ Configuration;": { + "platform": "Android", + "browser": "Android Browser", + "version": "534.30" + }, "Mozilla\/5.0 (Linux; Android 4.2.2; de-at; SAMSUNG GT-I9195\/I9195XXUAMF6 Build\/JDQ39) AppleWebKit\/535.19 (KHTML, like Gecko) Version\/1.0 Chrome\/18.0.1025.308 Mobile Safari\/535.19": { "platform": "Android", "browser": "Chrome",