From 1fa5b1d20340763ce351d41a4f467fa844819b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Mon, 8 Aug 2022 13:55:41 +0200 Subject: [PATCH 1/8] Fix passing null to string parameter deprecation Fixing "strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated" --- src/UserAgentParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 14d2175..4aadc63 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -190,7 +190,7 @@ function parse_user_agent( $u_agent = null ) { } elseif( $browser == 'AppleWebKit' ) { if( $platform == 'Android' ) { $browser = 'Android Browser'; - } elseif( strpos($platform, 'BB') === 0 ) { + } elseif( $platform !== null && strpos($platform, 'BB') === 0 ) { $browser = 'BlackBerry Browser'; $platform = 'BlackBerry'; } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { From 6d0bf0cf4a6483ece4db093948e027bfe61552dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Mon, 8 Aug 2022 16:43:58 +0200 Subject: [PATCH 2/8] Add test --- tests/user_agents.dist.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/user_agents.dist.json b/tests/user_agents.dist.json index b813e52..96de32a 100644 --- a/tests/user_agents.dist.json +++ b/tests/user_agents.dist.json @@ -1368,5 +1368,10 @@ "platform": null, "browser": "YandexBot", "version": "3.0" + }, + "Lenovo-A880/S100 Linux/3.4.5 Android/4.2 Release/08.07.2013 Browser/AppleWebKit 534.30 Profile/ Configuration;": { + "platform": null, + "browser": "AppleWebKit", + "version": "534.30" } } From 31a4e18839ad1e2582383179fe0bad0e36f3bdce Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:02:49 -0500 Subject: [PATCH 3/8] Improve older Android Browser support --- src/UserAgentParser.php | 8 ++++++++ tests/user_agents.dist.json | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 4aadc63..692bb18 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -88,6 +88,14 @@ function parse_user_agent( $u_agent = null ) { $platform = 'Chrome OS'; } elseif( $platform == 'Adr' ) { $platform = 'Android'; + } elseif( $platform === null ) { + if(preg_match_all(<<<'REGEX' +%(?PAndroid) +(?:[:/ ](?P[0-9A-Z.]+)|/[A-Z]*)%ix +REGEX +, $u_agent, $result)) { + $platform = $result[PLATFORM][0]; + } } preg_match_all(<<<'REGEX' diff --git a/tests/user_agents.dist.json b/tests/user_agents.dist.json index 96de32a..83e26e7 100644 --- a/tests/user_agents.dist.json +++ b/tests/user_agents.dist.json @@ -1370,8 +1370,8 @@ "version": "3.0" }, "Lenovo-A880/S100 Linux/3.4.5 Android/4.2 Release/08.07.2013 Browser/AppleWebKit 534.30 Profile/ Configuration;": { - "platform": null, - "browser": "AppleWebKit", + "platform": "Android", + "browser": "Android Browser", "version": "534.30" } } From 09a918ab1af29686a4b02e5529cdf7b45cb777f0 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:09:19 -0500 Subject: [PATCH 4/8] Mild cleanup --- src/UserAgentParser.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 692bb18..55e05c5 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -62,7 +62,8 @@ function parse_user_agent( $u_agent = null ) { if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { 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)?) +/(?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 @@ -104,7 +105,7 @@ function parse_user_agent( $u_agent = null ) { OculusBrowser|SamsungBrowser|SailfishBrowser|XiaoMi/MiuiBrowser| Baiduspider|Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl| Valve\ Steam\ Tenfoot| -NintendoBrowser|PLAYSTATION\ (\d|Vita)+) +NintendoBrowser|PLAYSTATION\ (?:\d|Vita)+) \)?;? (?:[:/ ](?P[0-9A-Z.]+)|/[A-Z]*)%ix REGEX From 05a2a94eb36c27e6937fd61b13ec93c377c080ae Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:17:07 -0500 Subject: [PATCH 5/8] Fix capturing groups --- src/UserAgentParser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 55e05c5..f2ff3af 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -62,8 +62,8 @@ function parse_user_agent( $u_agent = null ) { if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { 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)?) +/(?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 From 0be38381363b4208f547351d20eaadc933cab133 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:17:46 -0500 Subject: [PATCH 6/8] Slightly cleaner isset --- src/UserAgentParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index f2ff3af..649ea4e 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -112,7 +112,7 @@ function parse_user_agent( $u_agent = null ) { , $u_agent, $result); // If nothing matched, return null (to avoid undefined index errors) - if( !isset($result[BROWSER][0]) || !isset($result[BROWSER_VERSION][0]) ) { + if( !isset($result[BROWSER][0], $result[BROWSER_VERSION][0]) ) { if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { return [ PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => empty($result[BROWSER_VERSION]) ? null : $result[BROWSER_VERSION] ]; } From af736e4a53903b991ff7fece0c869e901cfe9366 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:40:18 -0500 Subject: [PATCH 7/8] =?UTF-8?q?Adds=20=E2=80=9Cmake=20clean=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index ad4604f..61f4653 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,11 @@ test: ./vendor/bin/phpunit --coverage-text +.PHONY: clean +clean: + -rm tests/user_agents.json + touch tests/user_agents.json + .PHONY: generate generate: php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.dist.json From 801e198496023adee41ea12cf2d91c437340ec86 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 8 Aug 2022 11:40:42 -0500 Subject: [PATCH 8/8] Simplify empty Android platform chjeck --- src/UserAgentParser.php | 8 ++------ tests/user_agents.dist.json | 10 +++++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 649ea4e..78a5d0e 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -90,11 +90,7 @@ function parse_user_agent( $u_agent = null ) { } elseif( $platform == 'Adr' ) { $platform = 'Android'; } elseif( $platform === null ) { - if(preg_match_all(<<<'REGEX' -%(?PAndroid) -(?:[:/ ](?P[0-9A-Z.]+)|/[A-Z]*)%ix -REGEX -, $u_agent, $result)) { + if(preg_match_all('%(?PAndroid)[:/ ]%ix', $u_agent, $result)) { $platform = $result[PLATFORM][0]; } } @@ -199,7 +195,7 @@ function parse_user_agent( $u_agent = null ) { } elseif( $browser == 'AppleWebKit' ) { if( $platform == 'Android' ) { $browser = 'Android Browser'; - } elseif( $platform !== null && 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 83e26e7..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", @@ -1368,10 +1373,5 @@ "platform": null, "browser": "YandexBot", "version": "3.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" } }