From 23056b4b2437a0c75a3db2a7270559097c89c95d Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 10:52:10 -0500 Subject: [PATCH 01/36] Adds key constants # Conflicts: # src/UserAgentParser.php --- src/UserAgentParser.php | 54 ++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 6ba738c..b9e185e 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -1,5 +1,9 @@ $platform, 'browser' => $browser, 'version' => $version ); + $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); if( !$u_agent ) { return $empty; @@ -40,15 +44,15 @@ function parse_user_agent( $u_agent = null ) { $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 ) { - if( $keys = array_intersect($priority, $result['platform']) ) { + $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); + if( count($result[UAP_KEY_PLATFORM]) > 1 ) { + if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { $platform = reset($keys); } else { - $platform = $result['platform'][0]; + $platform = $result[UAP_KEY_PLATFORM][0]; } - } elseif( isset($result['platform'][0]) ) { - $platform = $result['platform'][0]; + } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { + $platform = $result[UAP_KEY_PLATFORM][0]; } } @@ -68,22 +72,22 @@ 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['version'][0]) ) { + if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( 'platform' => $platform ?: null, 'browser' => $result['browser'], 'version' => isset($result['version']) ? $result['version'] ?: null : null ); + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); } return $empty; } if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { - $rv_result = $rv_result['version']; + $rv_result = $rv_result[UAP_KEY_VERSION]; } - $browser = $result['browser'][0]; - $version = $result['version'][0]; + $browser = $result[UAP_KEY_BROWSER][0]; + $version = $result[UAP_KEY_VERSION][0]; - $lowerBrowser = array_map('strtolower', $result['browser']); + $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { $search = (array)$search; @@ -115,27 +119,27 @@ function parse_user_agent( $u_agent = null ) { $key = 0; $val = ''; if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { - $version = $result['version'][$key]; + $version = $result[UAP_KEY_VERSION][$key]; }elseif( $find('Playstation Vita', $key, $platform) ) { $platform = 'PlayStation Vita'; $browser = 'Browser'; } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; $platform = 'Kindle Fire'; - if( !($version = $result['version'][$key]) || !is_numeric($version[0]) ) { - $version = $result['version'][array_search('Version', $result['browser'])]; + if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { + $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; } } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) { $browser = 'NintendoBrowser'; - $version = $result['version'][$key]; + $version = $result[UAP_KEY_VERSION][$key]; } elseif( $find('Kindle', $key, $platform) ) { - $browser = $result['browser'][$key]; - $version = $result['version'][$key]; + $browser = $result[UAP_KEY_BROWSER][$key]; + $version = $result[UAP_KEY_VERSION][$key]; } elseif( $find('Opera', $key, $browser) ) { $find('Version', $key); - $version = $result['version'][$key]; + $version = $result[UAP_KEY_VERSION][$key]; } elseif( $find('Puffin', $key, $browser) ) { - $version = $result['version'][$key]; + $version = $result[UAP_KEY_VERSION][$key]; if( strlen($version) > 3 ) { $part = substr($version, -2); if( ctype_upper($part) ) { @@ -148,7 +152,7 @@ function parse_user_agent( $u_agent = null ) { } } } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { - $version = $result['version'][$key]; + $version = $result[UAP_KEY_VERSION][$key]; } elseif( $rv_result && $find('Trident') ) { $browser = 'MSIE'; $version = $rv_result; @@ -165,13 +169,13 @@ function parse_user_agent( $u_agent = null ) { } $find('Version', $key); - $version = $result['version'][$key]; - } elseif( $pKey = preg_grep('/playstation \d/i', $result['browser']) ) { + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { $pKey = reset($pKey); $platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey); $browser = 'NetFront'; } - return array( 'platform' => $platform ?: null, 'browser' => $browser ?: null, 'version' => $version ?: null ); + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); } From 176d98f79997643c779d298a82d4dd7ea155deb5 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Thu, 21 Jun 2018 11:57:11 -0500 Subject: [PATCH 02/36] Adds constant_generator.php --- bin/constant_generator.php | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 bin/constant_generator.php diff --git a/bin/constant_generator.php b/bin/constant_generator.php new file mode 100644 index 0000000..53279bd --- /dev/null +++ b/bin/constant_generator.php @@ -0,0 +1,72 @@ + $val ) { + $kex = strtoupper($val['browser']); + if( $kex != "" ) { + $kex = preg_replace('/\W+/', '_', $kex); + if( !isset($browsers[$kex][$val['browser']]) ) { + $browsers[$kex][$val['browser']] = 0; + } + + $browsers[$kex][$val['browser']]++; + } + + $kex = strtoupper($val['platform']); + if( $kex != "" ) { + $kex = preg_replace('/\W+/', '_', $kex); + + if( !isset($platforms[$kex][$val['platform']]) ) { + $platforms[$kex][$val['platform']] = 0; + } + + $platforms[$kex][$val['platform']]++; + } +} + +ksort($browsers); +$file = basename(__FILE__); +echo << $val ) { + echo "const UAP_BROWSER_{$const} = " . var_export(key($val), true) . ";\n"; +} + +echo "\n"; + +foreach( $platforms as $platform ) { + if( count($platform) !== 1 ) { + echo "bad browser count\n"; + die(2); + } +} + +foreach( $platforms as $const => $val ) { + echo "const UAP_PLATFORM_{$const} = " . var_export(key($val), true) . ";\n"; +} + +echo "\n"; \ No newline at end of file From 83cdd8da82773033304c828ab5701f444a364b01 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Thu, 21 Jun 2018 11:58:52 -0500 Subject: [PATCH 03/36] Add rough makefile --- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ea49b5e --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: test +test: + ./vendor/bin/phpunit + +.PHONY: generate +generate: + php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json + php bin/constant_generator.php > src/browser_constants.php From e40f8c77b3c79eea3d2844583fd4bb09d94db53a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Thu, 21 Jun 2018 12:00:08 -0500 Subject: [PATCH 04/36] =?UTF-8?q?Don=E2=80=99t=20file=5Fput=5Fcontents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/user_agent_sorter.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/user_agent_sorter.php b/bin/user_agent_sorter.php index d7ebfb4..d6e028a 100644 --- a/bin/user_agent_sorter.php +++ b/bin/user_agent_sorter.php @@ -12,8 +12,8 @@ uasort($uas, function ( $a, $b ) { - if($a['platform'] === null && $b['platform'] !== null) return 1; - if($b['platform'] === null && $a['platform'] !== null) return -1; + if( $a['platform'] === null && $b['platform'] !== null ) return 1; + if( $b['platform'] === null && $a['platform'] !== null ) return -1; $desktop = array( 'Windows', 'Linux', 'Macintosh', 'Chrome OS' ); @@ -32,7 +32,6 @@ if( $result == 0 ) { $result = compare_version($a['version'], $b['version']); } - } } else { $result = strnatcasecmp($a['platform'], $b['platform']); @@ -43,7 +42,6 @@ if( $result == 0 ) { $result = compare_version($a['version'], $b['version']); } - } } @@ -59,9 +57,9 @@ } $jsonPretty = new Camspiers\JsonPretty\JsonPretty; + $json = $jsonPretty->prettify($uas) . "\n"; echo $json; -file_put_contents($jsonfile, $json); function compare_version( $a, $b ) { From 883688df74b82a727a240072dca1aaa1e82def5f Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Thu, 21 Jun 2018 12:03:44 -0500 Subject: [PATCH 05/36] Add autogenerated constants --- composer.json | 2 +- src/browser_constants.php | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/browser_constants.php diff --git a/composer.json b/composer.json index 4b07ee6..94375d2 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,6 @@ } ], "autoload": { - "files": [ "src/UserAgentParser.php" ] + "files": [ "src/UserAgentParser.php", "src/browser_constants.php" ] } } diff --git a/src/browser_constants.php b/src/browser_constants.php new file mode 100644 index 0000000..8f3f00a --- /dev/null +++ b/src/browser_constants.php @@ -0,0 +1,74 @@ + Date: Tue, 21 Jan 2020 11:49:26 -0600 Subject: [PATCH 06/36] Use Namespaces # Conflicts: # src/browser_constants.php --- bin/constant_generator.php | 16 +++-- src/browser_constants.php | 142 +++++++++++++++++++------------------ 2 files changed, 83 insertions(+), 75 deletions(-) diff --git a/bin/constant_generator.php b/bin/constant_generator.php index 53279bd..4fc39b0 100644 --- a/bin/constant_generator.php +++ b/bin/constant_generator.php @@ -52,21 +52,23 @@ } } +echo "namespace donatj\UserAgentParser\Browser {\n\n"; +$maxKey = max(array_map('strlen', array_keys($browsers))); foreach( $browsers as $const => $val ) { - echo "const UAP_BROWSER_{$const} = " . var_export(key($val), true) . ";\n"; + printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); } - -echo "\n"; +echo "}\n\n"; foreach( $platforms as $platform ) { if( count($platform) !== 1 ) { - echo "bad browser count\n"; + echo "bad platform count\n"; die(2); } } +echo "namespace donatj\UserAgentParser\Platform {\n\n"; +$maxKey = max(array_map('strlen', array_keys($platforms))); foreach( $platforms as $const => $val ) { - echo "const UAP_PLATFORM_{$const} = " . var_export(key($val), true) . ";\n"; + printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); } - -echo "\n"; \ No newline at end of file +echo "}\n\n"; \ No newline at end of file diff --git a/src/browser_constants.php b/src/browser_constants.php index 8f3f00a..33199fb 100644 --- a/src/browser_constants.php +++ b/src/browser_constants.php @@ -2,73 +2,79 @@ // DO NOT EDIT THIS FILE - IT IS GENERATED BY constant_generator.php -const UAP_BROWSER_ADSBOT_GOOGLE = 'AdsBot-Google'; -const UAP_BROWSER_ANDROID_BROWSER = 'Android Browser'; -const UAP_BROWSER_BAIDUSPIDER = 'Baiduspider'; -const UAP_BROWSER_BINGBOT = 'bingbot'; -const UAP_BROWSER_BLACKBERRY_BROWSER = 'BlackBerry Browser'; -const UAP_BROWSER_BROWSER = 'Browser'; -const UAP_BROWSER_BUNJALLOO = 'Bunjalloo'; -const UAP_BROWSER_CAMINO = 'Camino'; -const UAP_BROWSER_CHROME = 'Chrome'; -const UAP_BROWSER_CURL = 'curl'; -const UAP_BROWSER_EDGE = 'Edge'; -const UAP_BROWSER_FACEBOOKEXTERNALHIT = 'facebookexternalhit'; -const UAP_BROWSER_FEEDVALIDATOR = 'FeedValidator'; -const UAP_BROWSER_FIREFOX = 'Firefox'; -const UAP_BROWSER_GOOGLEBOT = 'Googlebot'; -const UAP_BROWSER_GOOGLEBOT_IMAGE = 'Googlebot-Image'; -const UAP_BROWSER_GOOGLEBOT_VIDEO = 'Googlebot-Video'; -const UAP_BROWSER_HEADLESSCHROME = 'HeadlessChrome'; -const UAP_BROWSER_IEMOBILE = 'IEMobile'; -const UAP_BROWSER_KINDLE = 'Kindle'; -const UAP_BROWSER_LYNX = 'Lynx'; -const UAP_BROWSER_MIDORI = 'Midori'; -const UAP_BROWSER_MSIE = 'MSIE'; -const UAP_BROWSER_MSNBOT_MEDIA = 'msnbot-media'; -const UAP_BROWSER_NETFRONT = 'NetFront'; -const UAP_BROWSER_NINTENDOBROWSER = 'NintendoBrowser'; -const UAP_BROWSER_OCULUSBROWSER = 'OculusBrowser'; -const UAP_BROWSER_OPERA = 'Opera'; -const UAP_BROWSER_PUFFIN = 'Puffin'; -const UAP_BROWSER_SAFARI = 'Safari'; -const UAP_BROWSER_SAMSUNGBROWSER = 'SamsungBrowser'; -const UAP_BROWSER_SILK = 'Silk'; -const UAP_BROWSER_TIZENBROWSER = 'TizenBrowser'; -const UAP_BROWSER_UC_BROWSER = 'UC Browser'; -const UAP_BROWSER_VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot'; -const UAP_BROWSER_VIVALDI = 'Vivaldi'; -const UAP_BROWSER_WGET = 'Wget'; -const UAP_BROWSER_WORDPRESS = 'WordPress'; -const UAP_BROWSER_YANDEX = 'Yandex'; -const UAP_BROWSER_YANDEXBOT = 'YandexBot'; +namespace donatj\UserAgentParser\Browser { -const UAP_PLATFORM_MACINTOSH = 'Macintosh'; -const UAP_PLATFORM_CHROME_OS = 'Chrome OS'; -const UAP_PLATFORM_LINUX = 'Linux'; -const UAP_PLATFORM_WINDOWS = 'Windows'; -const UAP_PLATFORM_ANDROID = 'Android'; -const UAP_PLATFORM_BLACKBERRY = 'BlackBerry'; -const UAP_PLATFORM_FREEBSD = 'FreeBSD'; -const UAP_PLATFORM_IPAD = 'iPad'; -const UAP_PLATFORM_IPHONE = 'iPhone'; -const UAP_PLATFORM_IPOD = 'iPod'; -const UAP_PLATFORM_KINDLE = 'Kindle'; -const UAP_PLATFORM_KINDLE_FIRE = 'Kindle Fire'; -const UAP_PLATFORM_NETBSD = 'NetBSD'; -const UAP_PLATFORM_NEW_NINTENDO_3DS = 'New Nintendo 3DS'; -const UAP_PLATFORM_NINTENDO_3DS = 'Nintendo 3DS'; -const UAP_PLATFORM_NINTENDO_DS = 'Nintendo DS'; -const UAP_PLATFORM_NINTENDO_SWITCH = 'Nintendo Switch'; -const UAP_PLATFORM_NINTENDO_WII = 'Nintendo Wii'; -const UAP_PLATFORM_NINTENDO_WIIU = 'Nintendo WiiU'; -const UAP_PLATFORM_OPENBSD = 'OpenBSD'; -const UAP_PLATFORM_PLAYBOOK = 'PlayBook'; -const UAP_PLATFORM_PLAYSTATION_3 = 'PlayStation 3'; -const UAP_PLATFORM_PLAYSTATION_4 = 'PlayStation 4'; -const UAP_PLATFORM_PLAYSTATION_VITA = 'PlayStation Vita'; -const UAP_PLATFORM_TIZEN = 'Tizen'; -const UAP_PLATFORM_WINDOWS_PHONE = 'Windows Phone'; -const UAP_PLATFORM_XBOX = 'Xbox'; -const UAP_PLATFORM_XBOX_ONE = 'Xbox One'; + const ADSBOT_GOOGLE = 'AdsBot-Google'; + const ANDROID_BROWSER = 'Android Browser'; + const BAIDUSPIDER = 'Baiduspider'; + const BINGBOT = 'bingbot'; + const BLACKBERRY_BROWSER = 'BlackBerry Browser'; + const BROWSER = 'Browser'; + const BUNJALLOO = 'Bunjalloo'; + const CAMINO = 'Camino'; + const CHROME = 'Chrome'; + const CURL = 'curl'; + const EDGE = 'Edge'; + const FACEBOOKEXTERNALHIT = 'facebookexternalhit'; + const FEEDVALIDATOR = 'FeedValidator'; + const FIREFOX = 'Firefox'; + const GOOGLEBOT = 'Googlebot'; + const GOOGLEBOT_IMAGE = 'Googlebot-Image'; + const GOOGLEBOT_VIDEO = 'Googlebot-Video'; + const HEADLESSCHROME = 'HeadlessChrome'; + const IEMOBILE = 'IEMobile'; + const KINDLE = 'Kindle'; + const LYNX = 'Lynx'; + const MIDORI = 'Midori'; + const MSIE = 'MSIE'; + const MSNBOT_MEDIA = 'msnbot-media'; + const NETFRONT = 'NetFront'; + const NINTENDOBROWSER = 'NintendoBrowser'; + const OCULUSBROWSER = 'OculusBrowser'; + const OPERA = 'Opera'; + const PUFFIN = 'Puffin'; + const SAFARI = 'Safari'; + const SAMSUNGBROWSER = 'SamsungBrowser'; + const SILK = 'Silk'; + const TIZENBROWSER = 'TizenBrowser'; + const UC_BROWSER = 'UC Browser'; + const VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot'; + const VIVALDI = 'Vivaldi'; + const WGET = 'Wget'; + const WORDPRESS = 'WordPress'; + const YANDEX = 'Yandex'; + const YANDEXBOT = 'YandexBot'; +} + +namespace donatj\UserAgentParser\Platform { + + const MACINTOSH = 'Macintosh'; + const CHROME_OS = 'Chrome OS'; + const LINUX = 'Linux'; + const WINDOWS = 'Windows'; + const ANDROID = 'Android'; + const BLACKBERRY = 'BlackBerry'; + const FREEBSD = 'FreeBSD'; + const IPAD = 'iPad'; + const IPHONE = 'iPhone'; + const IPOD = 'iPod'; + const KINDLE = 'Kindle'; + const KINDLE_FIRE = 'Kindle Fire'; + const NETBSD = 'NetBSD'; + const NEW_NINTENDO_3DS = 'New Nintendo 3DS'; + const NINTENDO_3DS = 'Nintendo 3DS'; + const NINTENDO_DS = 'Nintendo DS'; + const NINTENDO_SWITCH = 'Nintendo Switch'; + const NINTENDO_WII = 'Nintendo Wii'; + const NINTENDO_WIIU = 'Nintendo WiiU'; + const OPENBSD = 'OpenBSD'; + const PLAYBOOK = 'PlayBook'; + const PLAYSTATION_3 = 'PlayStation 3'; + const PLAYSTATION_4 = 'PlayStation 4'; + const PLAYSTATION_VITA = 'PlayStation Vita'; + const TIZEN = 'Tizen'; + const WINDOWS_PHONE = 'Windows Phone'; + const XBOX = 'Xbox'; + const XBOX_ONE = 'Xbox One'; +} From f03038aba59f2f322cfe75cfa62606b4cf7fdfb0 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 8 Mar 2019 12:01:24 -0600 Subject: [PATCH 07/36] Adds make init # Conflicts: # Makefile --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index ea49b5e..43a9b37 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,8 @@ test: generate: php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json php bin/constant_generator.php > src/browser_constants.php + +.PHONY: init +init: + php bin/init_user_agent.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json + make generate \ No newline at end of file From 11f98389da5a8a07602a4f19820f5acd97809b8a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Thu, 25 Oct 2018 15:16:17 -0500 Subject: [PATCH 08/36] Adds @license for when it gets copied --- src/UserAgentParser.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index b9e185e..f83da83 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -1,5 +1,8 @@ Date: Thu, 25 Oct 2018 15:16:42 -0500 Subject: [PATCH 09/36] Cleanup --- src/UserAgentParser.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index f83da83..32011cb 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -1,8 +1,5 @@ Camino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit| - 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[0-9A-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|Edge|Edg|CriOS|UCBrowser|Puffin|OculusBrowser|SamsungBrowser| +Baiduspider|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[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { From 142ab47fa7ab52a82f6f395d1758acebd90abb69 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 26 Aug 2019 16:21:55 -0500 Subject: [PATCH 10/36] More minor cleanup --- src/UserAgentParser.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 32011cb..21121af 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -20,7 +20,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 ) { @@ -38,9 +38,12 @@ function parse_user_agent( $u_agent = null ) { } if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { - preg_match_all('/(?PBB\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_match_all(<<<'REGEX' +/(?PBB\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 +REGEX + , $parent_matches[1], $result); $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); From 149a89f9d52fc639e5166a537d3a298b82ca022d Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 26 Aug 2019 16:44:50 -0500 Subject: [PATCH 11/36] Small generator updates --- bin/constant_generator.php | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/constant_generator.php b/bin/constant_generator.php index 4fc39b0..e544600 100644 --- a/bin/constant_generator.php +++ b/bin/constant_generator.php @@ -13,7 +13,7 @@ $browsers = array(); foreach( $uas as $key => $val ) { $kex = strtoupper($val['browser']); - if( $kex != "" ) { + if( $kex !== '' ) { $kex = preg_replace('/\W+/', '_', $kex); if( !isset($browsers[$kex][$val['browser']]) ) { $browsers[$kex][$val['browser']] = 0; @@ -23,7 +23,7 @@ } $kex = strtoupper($val['platform']); - if( $kex != "" ) { + if( $kex !== '' ) { $kex = preg_replace('/\W+/', '_', $kex); if( !isset($platforms[$kex][$val['platform']]) ) { diff --git a/composer.json b/composer.json index 94375d2..6c90a3f 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "php": ">=5.3.0" }, "require-dev": { - "camspiers/json-pretty": "0.1.*", + "camspiers/json-pretty": "~1.0", "phpunit/phpunit": "~4.8", "donatj/drop": "*" }, From 2b411c07874153bd809749ad3bb1ca91e61d5ad0 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:00:22 -0500 Subject: [PATCH 12/36] Adds namespace and namespace alias # Conflicts: # src/UserAgentParser.php --- src/UserAgentParser.php | 268 +++++++++++++++++++++------------------- 1 file changed, 143 insertions(+), 125 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 21121af..9448f70 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -1,71 +1,87 @@ - * - * @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( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { - $u_agent = (string)$_SERVER['HTTP_USER_AGENT']; +namespace { + + /** + * Parses a user agent string into its important parts + * + * This method is defined for backwards comparability with the old global method. + * + * @see \donatj\UserAgentParser\parse_user_agent + */ + function parse_user_agent( $u_agent = null ) { + return \donatj\UserAgentParser\parse_user_agent($u_agent); } +} - if( $u_agent === null ) { - throw new \InvalidArgumentException('parse_user_agent requires a user agent'); - } +namespace donatj\UserAgentParser { + + const UAP_KEY_PLATFORM = 'platform'; + const UAP_KEY_BROWSER = 'browser'; + const UAP_KEY_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. + * + * @author Jesse G. Donat + * + * @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( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { + $u_agent = (string)$_SERVER['HTTP_USER_AGENT']; + } - $platform = null; - $browser = null; - $version = null; + if( $u_agent === null ) { + throw new \InvalidArgumentException('parse_user_agent requires a user agent'); + } - $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); + $platform = null; + $browser = null; + $version = null; - if( !$u_agent ) { - return $empty; - } + $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); - if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { - preg_match_all(<<<'REGEX' + if( !$u_agent ) { + return $empty; + } + + if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { + preg_match_all(<<<'REGEX' /(?PBB\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 REGEX - , $parent_matches[1], $result); + , $parent_matches[1], $result); - $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); + $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); - $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); - if( count($result[UAP_KEY_PLATFORM]) > 1 ) { - if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { - $platform = reset($keys); - } else { + $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); + if( count($result[UAP_KEY_PLATFORM]) > 1 ) { + if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { + $platform = reset($keys); + } else { + $platform = $result[UAP_KEY_PLATFORM][0]; + } + } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { $platform = $result[UAP_KEY_PLATFORM][0]; } - } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { - $platform = $result[UAP_KEY_PLATFORM][0]; } - } - if( $platform == 'linux-gnu' || $platform == 'X11' ) { - $platform = 'Linux'; - } elseif( $platform == 'CrOS' ) { - $platform = 'Chrome OS'; - } + if( $platform == 'linux-gnu' || $platform == 'X11' ) { + $platform = 'Linux'; + } elseif( $platform == 'CrOS' ) { + $platform = 'Chrome OS'; + } - preg_match_all(<<<'REGEX' + preg_match_all(<<<'REGEX' %(?PCamino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit| 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| @@ -74,41 +90,41 @@ function parse_user_agent( $u_agent = null ) { (?:\)?;?) (?:(?:[:/ ])(?P[0-9A-Z.]+)|/(?:[A-Z]*))%ix REGEX - , $u_agent, $result); + , $u_agent, $result); - // If nothing matched, return null (to avoid undefined index errors) - if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { - if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); - } + // If nothing matched, return null (to avoid undefined index errors) + if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { + if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); + } - return $empty; - } + return $empty; + } - if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { - $rv_result = $rv_result[UAP_KEY_VERSION]; - } + if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { + $rv_result = $rv_result[UAP_KEY_VERSION]; + } - $browser = $result[UAP_KEY_BROWSER][0]; - $version = $result[UAP_KEY_VERSION][0]; + $browser = $result[UAP_KEY_BROWSER][0]; + $version = $result[UAP_KEY_VERSION][0]; - $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); + $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { - $search = (array)$search; + $search = (array)$search; - foreach( $search as $val ) { - $xkey = array_search(strtolower($val), $lowerBrowser); - if( $xkey !== false ) { - $value = $val; - $key = $xkey; + foreach( $search as $val ) { + $xkey = array_search(strtolower($val), $lowerBrowser); + if( $xkey !== false ) { + $value = $val; + $key = $xkey; - return true; + return true; + } } - } - return false; - }; + return false; + }; $findT = function ( array $search, &$key = null, &$value = null ) use ( $find ) { $value2 = null; @@ -121,66 +137,68 @@ function parse_user_agent( $u_agent = null ) { return false; }; - $key = 0; - $val = ''; + $key = 0; + $val = ''; if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { $version = $result[UAP_KEY_VERSION][$key]; }elseif( $find('Playstation Vita', $key, $platform) ) { - $platform = 'PlayStation Vita'; - $browser = 'Browser'; - } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { - $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; - $platform = 'Kindle Fire'; - if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { - $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; - } - } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) { - $browser = 'NintendoBrowser'; - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Kindle', $key, $platform) ) { - $browser = $result[UAP_KEY_BROWSER][$key]; - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Opera', $key, $browser) ) { - $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Puffin', $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; - if( strlen($version) > 3 ) { - $part = substr($version, -2); - if( ctype_upper($part) ) { - $version = substr($version, 0, -2); - - $flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); - if( isset($flags[$part]) ) { - $platform = $flags[$part]; + $platform = 'PlayStation Vita'; + $browser = 'Browser'; + } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { + $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; + $platform = 'Kindle Fire'; + if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { + $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; + } + } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) { + $browser = 'NintendoBrowser'; + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Kindle', $key, $platform) ) { + $browser = $result[UAP_KEY_BROWSER][$key]; + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Opera', $key, $browser) ) { + $find('Version', $key); + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Puffin', $key, $browser) ) { + $version = $result[UAP_KEY_VERSION][$key]; + if( strlen($version) > 3 ) { + $part = substr($version, -2); + if( ctype_upper($part) ) { + $version = substr($version, 0, -2); + + $flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); + if( isset($flags[$part]) ) { + $platform = $flags[$part]; + } } } - } } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; + $version = $result[UAP_KEY_VERSION][$key]; } elseif( $rv_result && $find('Trident') ) { - $browser = 'MSIE'; - $version = $rv_result; - } elseif( $browser == 'AppleWebKit' ) { - if( $platform == 'Android' ) { - $browser = 'Android Browser'; - } elseif( strpos($platform, 'BB') === 0 ) { - $browser = 'BlackBerry Browser'; - $platform = 'BlackBerry'; - } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { - $browser = 'BlackBerry Browser'; - } else { - $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); - } + $browser = 'MSIE'; + $version = $rv_result; + } elseif( $browser == 'AppleWebKit' ) { + if( $platform == 'Android' ) { + $browser = 'Android Browser'; + } elseif( strpos($platform, 'BB') === 0 ) { + $browser = 'BlackBerry Browser'; + $platform = 'BlackBerry'; + } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { + $browser = 'BlackBerry Browser'; + } else { + $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); + } - $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { - $pKey = reset($pKey); + $find('Version', $key); + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { + $pKey = reset($pKey); - $platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey); - $browser = 'NetFront'; - } + $platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey); + $browser = 'NetFront'; + } - return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); + } } + From 77bb404caf254f8fa2b0016c3bef82d4e1ea746a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:02:00 -0500 Subject: [PATCH 13/36] Refactor into a class # Conflicts: # src/UserAgentParser.php --- src/UserAgentParser.php | 281 ++++++++++++++++++++-------------------- 1 file changed, 143 insertions(+), 138 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 9448f70..780dc2f 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -10,7 +10,9 @@ * @see \donatj\UserAgentParser\parse_user_agent */ function parse_user_agent( $u_agent = null ) { - return \donatj\UserAgentParser\parse_user_agent($u_agent); + $parser = new donatj\UserAgentParser\UserAgentParser(); + + return $parser($u_agent); } } @@ -20,68 +22,70 @@ function parse_user_agent( $u_agent = null ) { const UAP_KEY_BROWSER = 'browser'; const UAP_KEY_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. - * - * @author Jesse G. Donat - * - * @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( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { - $u_agent = (string)$_SERVER['HTTP_USER_AGENT']; - } + class UserAgentParser { + + /** + * 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. + * + * @author Jesse G. Donat + * + * @link https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT + * @link https://github.com/donatj/PhpUserAgent + * + * @license MIT + */ + public function __invoke( $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'); - } + if( $u_agent === null ) { + throw new \InvalidArgumentException('parse_user_agent requires a user agent'); + } - $platform = null; - $browser = null; - $version = null; + $platform = null; + $browser = null; + $version = null; - $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); + $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); - if( !$u_agent ) { - return $empty; - } + if( !$u_agent ) { + return $empty; + } - if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { - preg_match_all(<<<'REGEX' + if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { + preg_match_all(<<<'REGEX' /(?PBB\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 REGEX - , $parent_matches[1], $result); + , $parent_matches[1], $result); - $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); + $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); - $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); - if( count($result[UAP_KEY_PLATFORM]) > 1 ) { - if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { - $platform = reset($keys); - } else { + $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); + if( count($result[UAP_KEY_PLATFORM]) > 1 ) { + if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { + $platform = reset($keys); + } else { + $platform = $result[UAP_KEY_PLATFORM][0]; + } + } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { $platform = $result[UAP_KEY_PLATFORM][0]; } - } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { - $platform = $result[UAP_KEY_PLATFORM][0]; } - } - if( $platform == 'linux-gnu' || $platform == 'X11' ) { - $platform = 'Linux'; - } elseif( $platform == 'CrOS' ) { - $platform = 'Chrome OS'; - } + if( $platform == 'linux-gnu' || $platform == 'X11' ) { + $platform = 'Linux'; + } elseif( $platform == 'CrOS' ) { + $platform = 'Chrome OS'; + } - preg_match_all(<<<'REGEX' + preg_match_all(<<<'REGEX' %(?PCamino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit| 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| @@ -90,115 +94,116 @@ function parse_user_agent( $u_agent = null ) { (?:\)?;?) (?:(?:[:/ ])(?P[0-9A-Z.]+)|/(?:[A-Z]*))%ix REGEX - , $u_agent, $result); + , $u_agent, $result); - // If nothing matched, return null (to avoid undefined index errors) - if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { - if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); - } + // If nothing matched, return null (to avoid undefined index errors) + if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { + if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); + } - return $empty; - } + return $empty; + } - if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { - $rv_result = $rv_result[UAP_KEY_VERSION]; - } + if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { + $rv_result = $rv_result[UAP_KEY_VERSION]; + } - $browser = $result[UAP_KEY_BROWSER][0]; - $version = $result[UAP_KEY_VERSION][0]; + $browser = $result[UAP_KEY_BROWSER][0]; + $version = $result[UAP_KEY_VERSION][0]; - $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); + $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); - $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { - $search = (array)$search; + $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; + foreach( $search as $val ) { + $xkey = array_search(strtolower($val), $lowerBrowser); + if( $xkey !== false ) { + $value = $val; + $key = $xkey; - return true; + return true; + } } - } - return false; - }; + return false; + }; - $findT = function ( array $search, &$key = null, &$value = null ) use ( $find ) { - $value2 = null; - if( $find(array_keys($search), $key, $value2) ) { - $value = $search[$value2]; + $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 true; + } - return false; - }; - - $key = 0; - $val = ''; - if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; - }elseif( $find('Playstation Vita', $key, $platform) ) { - $platform = 'PlayStation Vita'; - $browser = 'Browser'; - } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { - $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; - $platform = 'Kindle Fire'; - if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { - $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; - } - } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) { - $browser = 'NintendoBrowser'; - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Kindle', $key, $platform) ) { - $browser = $result[UAP_KEY_BROWSER][$key]; - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Opera', $key, $browser) ) { - $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $find('Puffin', $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; - if( strlen($version) > 3 ) { - $part = substr($version, -2); - if( ctype_upper($part) ) { - $version = substr($version, 0, -2); - - $flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); - if( isset($flags[$part]) ) { - $platform = $flags[$part]; + return false; + }; + + $key = 0; + $val = ''; + if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Playstation Vita', $key, $platform) ) { + $platform = 'PlayStation Vita'; + $browser = 'Browser'; + } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { + $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; + $platform = 'Kindle Fire'; + if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { + $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; + } + } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) { + $browser = 'NintendoBrowser'; + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Kindle', $key, $platform) ) { + $browser = $result[UAP_KEY_BROWSER][$key]; + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Opera', $key, $browser) ) { + $find('Version', $key); + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $find('Puffin', $key, $browser) ) { + $version = $result[UAP_KEY_VERSION][$key]; + if( strlen($version) > 3 ) { + $part = substr($version, -2); + if( ctype_upper($part) ) { + $version = substr($version, 0, -2); + + $flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); + if( isset($flags[$part]) ) { + $platform = $flags[$part]; + } } } - } - } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $rv_result && $find('Trident') ) { - $browser = 'MSIE'; - $version = $rv_result; - } elseif( $browser == 'AppleWebKit' ) { - if( $platform == 'Android' ) { - $browser = 'Android Browser'; - } elseif( strpos($platform, 'BB') === 0 ) { - $browser = 'BlackBerry Browser'; - $platform = 'BlackBerry'; - } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { - $browser = 'BlackBerry Browser'; - } else { - $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); - } + } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $rv_result && $find('Trident') ) { + $browser = 'MSIE'; + $version = $rv_result; + } elseif( $browser == 'AppleWebKit' ) { + if( $platform == 'Android' ) { + $browser = 'Android Browser'; + } elseif( strpos($platform, 'BB') === 0 ) { + $browser = 'BlackBerry Browser'; + $platform = 'BlackBerry'; + } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { + $browser = 'BlackBerry Browser'; + } else { + $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); + } - $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { - $pKey = reset($pKey); + $find('Version', $key); + $version = $result[UAP_KEY_VERSION][$key]; + } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { + $pKey = reset($pKey); - $platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey); - $browser = 'NetFront'; - } + $platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey); + $browser = 'NetFront'; + } - return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); + return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); + } } } From 51c9492f541ddb6e27bf2cb846394b984c101658 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:05:30 -0500 Subject: [PATCH 14/36] Cleaner constant naming # Conflicts: # src/UserAgentParser.php --- src/UserAgentParser.php | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 780dc2f..70ff7ce 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -18,9 +18,9 @@ function parse_user_agent( $u_agent = null ) { namespace donatj\UserAgentParser { - const UAP_KEY_PLATFORM = 'platform'; - const UAP_KEY_BROWSER = 'browser'; - const UAP_KEY_VERSION = 'version'; + const PLATFORM = 'platform'; + const BROWSER = 'browser'; + const BROWSER_VERSION = 'version'; class UserAgentParser { @@ -51,7 +51,7 @@ public function __invoke( $u_agent = null ) { $browser = null; $version = null; - $empty = array( UAP_KEY_PLATFORM => $platform, UAP_KEY_BROWSER => $browser, UAP_KEY_VERSION => $version ); + $empty = array( PLATFORM => $platform, BROWSER => $browser, BROWSER_VERSION => $version ); if( !$u_agent ) { return $empty; @@ -67,15 +67,15 @@ public function __invoke( $u_agent = null ) { $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' ); - $result[UAP_KEY_PLATFORM] = array_unique($result[UAP_KEY_PLATFORM]); - if( count($result[UAP_KEY_PLATFORM]) > 1 ) { - if( $keys = array_intersect($priority, $result[UAP_KEY_PLATFORM]) ) { + $result[PLATFORM] = array_unique($result[PLATFORM]); + if( count($result[PLATFORM]) > 1 ) { + if( $keys = array_intersect($priority, $result[PLATFORM]) ) { $platform = reset($keys); } else { - $platform = $result[UAP_KEY_PLATFORM][0]; + $platform = $result[PLATFORM][0]; } - } elseif( isset($result[UAP_KEY_PLATFORM][0]) ) { - $platform = $result[UAP_KEY_PLATFORM][0]; + } elseif( isset($result[PLATFORM][0]) ) { + $platform = $result[PLATFORM][0]; } } @@ -97,22 +97,22 @@ public function __invoke( $u_agent = null ) { , $u_agent, $result); // If nothing matched, return null (to avoid undefined index errors) - if( !isset($result[UAP_KEY_BROWSER][0]) || !isset($result[UAP_KEY_VERSION][0]) ) { + if( !isset($result[BROWSER][0]) || !isset($result[BROWSER_VERSION][0]) ) { if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $result[UAP_KEY_BROWSER], UAP_KEY_VERSION => isset($result[UAP_KEY_VERSION]) ? $result[UAP_KEY_VERSION] ?: null : null ); + return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => isset($result[BROWSER_VERSION]) ? $result[BROWSER_VERSION] ?: null : null ); } return $empty; } if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { - $rv_result = $rv_result[UAP_KEY_VERSION]; + $rv_result = $rv_result[BROWSER_VERSION]; } - $browser = $result[UAP_KEY_BROWSER][0]; - $version = $result[UAP_KEY_VERSION][0]; + $browser = $result[BROWSER][0]; + $version = $result[BROWSER_VERSION][0]; - $lowerBrowser = array_map('strtolower', $result[UAP_KEY_BROWSER]); + $lowerBrowser = array_map('strtolower', $result[BROWSER]); $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { $search = (array)$search; @@ -144,27 +144,27 @@ public function __invoke( $u_agent = null ) { $key = 0; $val = ''; if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; + $version = $result[BROWSER_VERSION][$key]; } elseif( $find('Playstation Vita', $key, $platform) ) { $platform = 'PlayStation Vita'; $browser = 'Browser'; } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { $browser = $val == 'Silk' ? 'Silk' : 'Kindle'; $platform = 'Kindle Fire'; - if( !($version = $result[UAP_KEY_VERSION][$key]) || !is_numeric($version[0]) ) { - $version = $result[UAP_KEY_VERSION][array_search('Version', $result[UAP_KEY_BROWSER])]; + 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[UAP_KEY_VERSION][$key]; + $version = $result[BROWSER_VERSION][$key]; } elseif( $find('Kindle', $key, $platform) ) { - $browser = $result[UAP_KEY_BROWSER][$key]; - $version = $result[UAP_KEY_VERSION][$key]; + $browser = $result[BROWSER][$key]; + $version = $result[BROWSER_VERSION][$key]; } elseif( $find('Opera', $key, $browser) ) { $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; + $version = $result[BROWSER_VERSION][$key]; } elseif( $find('Puffin', $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; + $version = $result[BROWSER_VERSION][$key]; if( strlen($version) > 3 ) { $part = substr($version, -2); if( ctype_upper($part) ) { @@ -177,7 +177,7 @@ public function __invoke( $u_agent = null ) { } } } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { - $version = $result[UAP_KEY_VERSION][$key]; + $version = $result[BROWSER_VERSION][$key]; } elseif( $rv_result && $find('Trident') ) { $browser = 'MSIE'; $version = $rv_result; @@ -194,15 +194,15 @@ public function __invoke( $u_agent = null ) { } $find('Version', $key); - $version = $result[UAP_KEY_VERSION][$key]; - } elseif( $pKey = preg_grep('/playstation \d/i', $result[UAP_KEY_BROWSER]) ) { + $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 array( UAP_KEY_PLATFORM => $platform ?: null, UAP_KEY_BROWSER => $browser ?: null, UAP_KEY_VERSION => $version ?: null ); + return array( PLATFORM => $platform ?: null, BROWSER => $browser ?: null, BROWSER_VERSION => $version ?: null ); } } } From 8d57f996a2a95bee0ac77b0965cf4cbc5a8767ec Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 26 Aug 2019 17:28:42 -0500 Subject: [PATCH 15/36] Changes namespace --- bin/constant_generator.php | 4 ++-- src/UserAgentParser.php | 8 ++++---- src/browser_constants.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bin/constant_generator.php b/bin/constant_generator.php index e544600..5d31ca4 100644 --- a/bin/constant_generator.php +++ b/bin/constant_generator.php @@ -52,7 +52,7 @@ } } -echo "namespace donatj\UserAgentParser\Browser {\n\n"; +echo "namespace donatj\UserAgent\Browser {\n\n"; $maxKey = max(array_map('strlen', array_keys($browsers))); foreach( $browsers as $const => $val ) { printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); @@ -66,7 +66,7 @@ } } -echo "namespace donatj\UserAgentParser\Platform {\n\n"; +echo "namespace donatj\UserAgent\Platform {\n\n"; $maxKey = max(array_map('strlen', array_keys($platforms))); foreach( $platforms as $const => $val ) { printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 70ff7ce..6d3c3a0 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -7,16 +7,16 @@ * * This method is defined for backwards comparability with the old global method. * - * @see \donatj\UserAgentParser\parse_user_agent + * @see \donatj\UserAgent\parse_user_agent */ function parse_user_agent( $u_agent = null ) { - $parser = new donatj\UserAgentParser\UserAgentParser(); + $parser = new donatj\UserAgent\UserAgentParser; return $parser($u_agent); } } -namespace donatj\UserAgentParser { +namespace donatj\UserAgent { const PLATFORM = 'platform'; const BROWSER = 'browser'; @@ -28,7 +28,7 @@ class UserAgentParser { * 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 + * @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 diff --git a/src/browser_constants.php b/src/browser_constants.php index 33199fb..419b70d 100644 --- a/src/browser_constants.php +++ b/src/browser_constants.php @@ -2,7 +2,7 @@ // DO NOT EDIT THIS FILE - IT IS GENERATED BY constant_generator.php -namespace donatj\UserAgentParser\Browser { +namespace donatj\UserAgent\Browser { const ADSBOT_GOOGLE = 'AdsBot-Google'; const ANDROID_BROWSER = 'Android Browser'; @@ -46,7 +46,7 @@ const YANDEXBOT = 'YandexBot'; } -namespace donatj\UserAgentParser\Platform { +namespace donatj\UserAgent\Platform { const MACINTOSH = 'Macintosh'; const CHROME_OS = 'Chrome OS'; From d6c8275c8a32673082bada03f246ba7126d41b53 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 26 Aug 2019 17:30:07 -0500 Subject: [PATCH 16/36] Adds PSR-4 autoloader for kicks --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6c90a3f..a39edb2 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,9 @@ } ], "autoload": { - "files": [ "src/UserAgentParser.php", "src/browser_constants.php" ] + "files": [ "src/UserAgentParser.php", "src/browser_constants.php" ], + "psr-4": { + "donatj\\UserAgent\\": "src/" + } } } From 32b539aa2c38763b1ce11d7f73c9d851f5dd9cd9 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 26 Aug 2019 17:45:56 -0500 Subject: [PATCH 17/36] Cleans up the docs situation --- src/UserAgentParser.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 6d3c3a0..bc2fd1b 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -1,5 +1,14 @@ + * + * @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 { /** @@ -7,6 +16,10 @@ * * 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. + * * @see \donatj\UserAgent\parse_user_agent */ function parse_user_agent( $u_agent = null ) { @@ -30,13 +43,6 @@ class UserAgentParser { * @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. - * - * @author Jesse G. Donat - * - * @link https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT - * @link https://github.com/donatj/PhpUserAgent - * - * @license MIT */ public function __invoke( $u_agent = null ) { if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { From c109060398de0e701a259fd4980b5cbe04494d27 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:06:14 -0500 Subject: [PATCH 18/36] Removes object. # Conflicts: # src/UserAgentParser.php --- src/UserAgentParser.php | 237 ++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 121 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index bc2fd1b..2871c83 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -23,9 +23,7 @@ * @see \donatj\UserAgent\parse_user_agent */ function parse_user_agent( $u_agent = null ) { - $parser = new donatj\UserAgent\UserAgentParser; - - return $parser($u_agent); + return \donatj\UserAgent\parse_user_agent($u_agent); } } @@ -35,63 +33,61 @@ function parse_user_agent( $u_agent = null ) { const BROWSER = 'browser'; const BROWSER_VERSION = 'version'; - class UserAgentParser { - - /** - * 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. - */ - public function __invoke( $u_agent = null ) { - if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) { - $u_agent = (string)$_SERVER['HTTP_USER_AGENT']; - } + /** + * 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'); - } + if( $u_agent === null ) { + throw new \InvalidArgumentException('parse_user_agent requires a user agent'); + } - $platform = null; - $browser = null; - $version = null; + $platform = null; + $browser = null; + $version = null; - $empty = array( PLATFORM => $platform, BROWSER => $browser, BROWSER_VERSION => $version ); + $empty = array( PLATFORM => $platform, BROWSER => $browser, BROWSER_VERSION => $version ); - if( !$u_agent ) { - return $empty; - } + if( !$u_agent ) { + return $empty; + } - if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { - preg_match_all(<<<'REGEX' + if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) { + preg_match_all(<<<'REGEX' /(?PBB\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 REGEX - , $parent_matches[1], $result); + , $parent_matches[1], $result); - $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', '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 ) { - if( $keys = array_intersect($priority, $result[PLATFORM]) ) { - $platform = reset($keys); - } else { - $platform = $result[PLATFORM][0]; - } - } elseif( isset($result[PLATFORM][0]) ) { + $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'; - } + if( $platform == 'linux-gnu' || $platform == 'X11' ) { + $platform = 'Linux'; + } elseif( $platform == 'CrOS' ) { + $platform = 'Chrome OS'; + } - preg_match_all(<<<'REGEX' + preg_match_all(<<<'REGEX' %(?PCamino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit| 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| @@ -100,41 +96,41 @@ public function __invoke( $u_agent = null ) { (?:\)?;?) (?:(?:[:/ ])(?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[BROWSER_VERSION][0]) ) { - if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => isset($result[BROWSER_VERSION]) ? $result[BROWSER_VERSION] ?: null : null ); - } + , $u_agent, $result); - return $empty; + // If nothing matched, return null (to avoid undefined index errors) + if( !isset($result[BROWSER][0]) || !isset($result[BROWSER_VERSION][0]) ) { + if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { + return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => isset($result[BROWSER_VERSION]) ? $result[BROWSER_VERSION] ?: null : null ); } - if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { - $rv_result = $rv_result[BROWSER_VERSION]; - } + return $empty; + } + + if( preg_match('/rv:(?P[0-9A-Z.]+)/i', $u_agent, $rv_result) ) { + $rv_result = $rv_result[BROWSER_VERSION]; + } - $browser = $result[BROWSER][0]; - $version = $result[BROWSER_VERSION][0]; + $browser = $result[BROWSER][0]; + $version = $result[BROWSER_VERSION][0]; - $lowerBrowser = array_map('strtolower', $result[BROWSER]); + $lowerBrowser = array_map('strtolower', $result[BROWSER]); $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { - $search = (array)$search; + $search = (array)$search; - foreach( $search as $val ) { - $xkey = array_search(strtolower($val), $lowerBrowser); - if( $xkey !== false ) { - $value = $val; - $key = $xkey; + foreach( $search as $val ) { + $xkey = array_search(strtolower($val), $lowerBrowser); + if( $xkey !== false ) { + $value = $val; + $key = $xkey; - return true; - } + return true; } + } - return false; - }; + return false; + }; $findT = function ( array $search, &$key = null, &$value = null ) use ( $find ) { $value2 = null; @@ -147,69 +143,68 @@ public function __invoke( $u_agent = null ) { return false; }; - $key = 0; - $val = ''; + $key = 0; + $val = ''; if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { $version = $result[BROWSER_VERSION][$key]; } elseif( $find('Playstation Vita', $key, $platform) ) { - $platform = 'PlayStation Vita'; - $browser = 'Browser'; - } elseif( $find(array( '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 = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); - if( isset($flags[$part]) ) { - $platform = $flags[$part]; - } + $platform = 'PlayStation Vita'; + $browser = 'Browser'; + } elseif( $find(array( '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 = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' ); + if( isset($flags[$part]) ) { + $platform = $flags[$part]; } } + } } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { - $version = $result[BROWSER_VERSION][$key]; + $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($platform, 'BB') === 0 ) { - $browser = 'BlackBerry Browser'; - $platform = 'BlackBerry'; - } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { - $browser = 'BlackBerry Browser'; - } else { - $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); - } - - $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'; + $browser = 'MSIE'; + $version = $rv_result; + } elseif( $browser == 'AppleWebKit' ) { + if( $platform == 'Android' ) { + $browser = 'Android Browser'; + } elseif( strpos($platform, 'BB') === 0 ) { + $browser = 'BlackBerry Browser'; + $platform = 'BlackBerry'; + } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) { + $browser = 'BlackBerry Browser'; + } else { + $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser); } - return array( PLATFORM => $platform ?: null, BROWSER => $browser ?: null, BROWSER_VERSION => $version ?: null ); + $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 array( PLATFORM => $platform ?: null, BROWSER => $browser ?: null, BROWSER_VERSION => $version ?: null ); } } From 178c7cedcf02ae77ce01a27c7b155ab13029605c Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 27 Aug 2019 12:54:43 -0500 Subject: [PATCH 19/36] Adds helper objects and tests --- composer.json | 2 +- src/UserAgent/UserAgent.php | 53 +++++++++++++++++++ src/UserAgent/UserAgentParser.php | 39 ++++++++++++++ ...st.php => UserAgentParserFunctionTest.php} | 2 +- tests/UserAgentParserObjectTest.php | 45 ++++++++++++++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/UserAgent/UserAgent.php create mode 100644 src/UserAgent/UserAgentParser.php rename tests/{UserAgentParserTest.php => UserAgentParserFunctionTest.php} (94%) create mode 100644 tests/UserAgentParserObjectTest.php diff --git a/composer.json b/composer.json index a39edb2..3544610 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "autoload": { "files": [ "src/UserAgentParser.php", "src/browser_constants.php" ], "psr-4": { - "donatj\\UserAgent\\": "src/" + "donatj\\UserAgent\\": "src/UserAgent" } } } diff --git a/src/UserAgent/UserAgent.php b/src/UserAgent/UserAgent.php new file mode 100644 index 0000000..9ea7b78 --- /dev/null +++ b/src/UserAgent/UserAgent.php @@ -0,0 +1,53 @@ +platform = $platform; + $this->browser = $browser; + $this->browserVersion = $browserVersion; + } + + /** + * @return string|null + */ + public function platform() { + return $this->platform; + } + + /** + * @return string|null + */ + public function browser() { + return $this->browser; + } + + /** + * @return string|null + */ + public function browserVersion() { + return $this->browserVersion; + } +} diff --git a/src/UserAgent/UserAgentParser.php b/src/UserAgent/UserAgentParser.php new file mode 100644 index 0000000..94ad120 --- /dev/null +++ b/src/UserAgent/UserAgentParser.php @@ -0,0 +1,39 @@ +parse($u_agent); + } + +} diff --git a/tests/UserAgentParserTest.php b/tests/UserAgentParserFunctionTest.php similarity index 94% rename from tests/UserAgentParserTest.php rename to tests/UserAgentParserFunctionTest.php index 73930f4..7bb5b34 100644 --- a/tests/UserAgentParserTest.php +++ b/tests/UserAgentParserFunctionTest.php @@ -1,6 +1,6 @@ $parts ) { + $out[] = array( $string ); + } + + return $out; + } + + /** + * @dataProvider userAgentDataProvider + */ + public function test_parse( $string ) { + $parser = new UserAgentParser; + $result = $parser->parse($string); + + $expected = parse_user_agent($string); + + $this->assertSame($expected[\donatj\UserAgent\PLATFORM], $result->platform()); + $this->assertSame($expected[\donatj\UserAgent\BROWSER], $result->browser()); + $this->assertSame($expected[\donatj\UserAgent\BROWSER_VERSION], $result->browserVersion()); + } + + /** + * @dataProvider userAgentDataProvider + */ + public function test_invoke( $string ) { + $parser = new UserAgentParser; + $result = $parser($string); + + $expected = parse_user_agent($string); + + $this->assertSame($expected[\donatj\UserAgent\PLATFORM], $result->platform()); + $this->assertSame($expected[\donatj\UserAgent\BROWSER], $result->browser()); + $this->assertSame($expected[\donatj\UserAgent\BROWSER_VERSION], $result->browserVersion()); + } + +} From 703edcad4cf204e2fc8bd5bd7ccf6cbc48b6b993 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 27 Aug 2019 12:58:06 -0500 Subject: [PATCH 20/36] Formatting throughout --- bin/user_agent_sorter.php | 29 +++++++++++++++++++---------- src/UserAgent/UserAgentParser.php | 8 ++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/bin/user_agent_sorter.php b/bin/user_agent_sorter.php index d6e028a..2593636 100644 --- a/bin/user_agent_sorter.php +++ b/bin/user_agent_sorter.php @@ -12,16 +12,25 @@ uasort($uas, function ( $a, $b ) { - if( $a['platform'] === null && $b['platform'] !== null ) return 1; - if( $b['platform'] === null && $a['platform'] !== null ) return -1; + if( $a['platform'] === null && $b['platform'] !== null ) { + return 1; + } + if( $b['platform'] === null && $a['platform'] !== null ) { + return -1; + } $desktop = array( 'Windows', 'Linux', 'Macintosh', 'Chrome OS' ); $ad = in_array($a['platform'], $desktop); $bd = in_array($b['platform'], $desktop); - if( !$ad && $bd ) return 1; - if( $ad && !$bd ) return -1; + if( !$ad && $bd ) { + return 1; + } + + if( $ad && !$bd ) { + return -1; + } if( $ad ) { $result = strnatcasecmp($a['browser'], $b['browser']); @@ -74,12 +83,12 @@ function compare_version( $a, $b ) { $aa = strtolower(isset($cmp_a[$i]) ? $cmp_a[$i] : '0'); $bb = strtolower(isset($cmp_b[$i]) ? $cmp_b[$i] : '0'); - if( is_numeric($aa) && is_numeric($bb) ) { - if( $aa != $bb ) { - $value = ($aa > $bb ? 1 : -1); - break; - } - } else if( $cmp = strcmp($aa, $bb) ) { + if( is_numeric($aa) && is_numeric($bb) && $aa !== $bb ) { + $value = ($aa > $bb ? 1 : -1); + break; + } + + if( $cmp = strcmp($aa, $bb) ) { $value = $cmp / abs($cmp); break; } diff --git a/src/UserAgent/UserAgentParser.php b/src/UserAgent/UserAgentParser.php index 94ad120..4ce3e55 100644 --- a/src/UserAgent/UserAgentParser.php +++ b/src/UserAgent/UserAgentParser.php @@ -7,11 +7,11 @@ class UserAgentParser { /** * Parses a user agent string into its important parts * - * @see \donatj\UserAgent\parse_user_agent() - * * @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL * @return UserAgent an object with 'browser', 'browserVersion' and 'platform' methods * @throws \InvalidArgumentException on not having a proper user agent to parse. + * @see \donatj\UserAgent\parse_user_agent() + * */ public function parse( $u_agent = null ) { $parsed = parse_user_agent($u_agent); @@ -26,11 +26,11 @@ public function parse( $u_agent = null ) { /** * Parses a user agent string into its important parts * - * @see \donatj\UserAgent\parse_user_agent() - * * @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL * @return UserAgent an object with 'browser', 'browserVersion' and 'platform' methods * @throws \InvalidArgumentException on not having a proper user agent to parse. + * @see \donatj\UserAgent\parse_user_agent() + * */ public function __invoke( $u_agent = null ) { return $this->parse($u_agent); From 4af62e53d9549f910d8a9a97df40560c971e729b Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 27 Aug 2019 13:00:45 -0500 Subject: [PATCH 21/36] UA Sorter cleanup --- bin/user_agent_sorter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/user_agent_sorter.php b/bin/user_agent_sorter.php index 2593636..07c6fb8 100644 --- a/bin/user_agent_sorter.php +++ b/bin/user_agent_sorter.php @@ -9,6 +9,7 @@ foreach( $uas as $key => &$val ) { $val['key'] = $key; } +unset($val); uasort($uas, function ( $a, $b ) { @@ -21,8 +22,8 @@ $desktop = array( 'Windows', 'Linux', 'Macintosh', 'Chrome OS' ); - $ad = in_array($a['platform'], $desktop); - $bd = in_array($b['platform'], $desktop); + $ad = in_array($a['platform'], $desktop, true); + $bd = in_array($b['platform'], $desktop, true); if( !$ad && $bd ) { return 1; @@ -64,6 +65,7 @@ foreach( $uas as &$val ) { unset($val['key']); } +unset($val); $jsonPretty = new Camspiers\JsonPretty\JsonPretty; From 0b72fe3927afa019c5c02d8af69c1ad78bf6334a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 27 Aug 2019 13:24:15 -0500 Subject: [PATCH 22/36] Updates constant generator to make classes rather than namespaced constants --- Makefile | 2 +- bin/constant_generator.php | 23 ++++++----- composer.json | 12 +++++- .../Browsers.php} | 35 ++--------------- src/UserAgent/Platforms.php | 39 +++++++++++++++++++ 5 files changed, 66 insertions(+), 45 deletions(-) rename src/{browser_constants.php => UserAgent/Browsers.php} (60%) create mode 100644 src/UserAgent/Platforms.php diff --git a/Makefile b/Makefile index 43a9b37..827e734 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ test: .PHONY: generate generate: php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json - php bin/constant_generator.php > src/browser_constants.php + php bin/constant_generator.php .PHONY: init init: diff --git a/bin/constant_generator.php b/bin/constant_generator.php index 5d31ca4..34c2540 100644 --- a/bin/constant_generator.php +++ b/bin/constant_generator.php @@ -35,8 +35,8 @@ } ksort($browsers); -$file = basename(__FILE__); -echo << $val ) { - printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); + $browserBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); } -echo "}\n\n"; +$browserBody .= "\n}\n\n"; foreach( $platforms as $platform ) { if( count($platform) !== 1 ) { @@ -66,9 +66,12 @@ } } -echo "namespace donatj\UserAgent\Platform {\n\n"; -$maxKey = max(array_map('strlen', array_keys($platforms))); +$platformBody = "{$header}namespace donatj\UserAgent;\n\ninterface Platforms {\n\n"; +$maxKey = max(array_map('strlen', array_keys($platforms))); foreach( $platforms as $const => $val ) { - printf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); + $platformBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true)); } -echo "}\n\n"; \ No newline at end of file +$platformBody .= "\n}\n\n"; + +file_put_contents(__DIR__ . '/../src/UserAgent/Browsers.php', $browserBody); +file_put_contents(__DIR__ . '/../src/UserAgent/Platforms.php', $platformBody); \ No newline at end of file diff --git a/composer.json b/composer.json index 3544610..3a4246a 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,13 @@ "name": "donatj/phpuseragentparser", "type": "library", "description": "Lightning fast, minimalist PHP UserAgent string parser.", - "keywords": ["user agent", "useragent", "parser", "browser", "browser detection"], + "keywords": [ + "user agent", + "useragent", + "parser", + "browser", + "browser detection" + ], "homepage": "http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT", "license": "MIT", "require": { @@ -22,7 +28,9 @@ } ], "autoload": { - "files": [ "src/UserAgentParser.php", "src/browser_constants.php" ], + "files": [ + "src/UserAgentParser.php" + ], "psr-4": { "donatj\\UserAgent\\": "src/UserAgent" } diff --git a/src/browser_constants.php b/src/UserAgent/Browsers.php similarity index 60% rename from src/browser_constants.php rename to src/UserAgent/Browsers.php index 419b70d..99e3ca0 100644 --- a/src/browser_constants.php +++ b/src/UserAgent/Browsers.php @@ -2,7 +2,9 @@ // DO NOT EDIT THIS FILE - IT IS GENERATED BY constant_generator.php -namespace donatj\UserAgent\Browser { +namespace donatj\UserAgent; + +interface Browsers { const ADSBOT_GOOGLE = 'AdsBot-Google'; const ANDROID_BROWSER = 'Android Browser'; @@ -44,37 +46,6 @@ const WORDPRESS = 'WordPress'; const YANDEX = 'Yandex'; const YANDEXBOT = 'YandexBot'; -} - -namespace donatj\UserAgent\Platform { - const MACINTOSH = 'Macintosh'; - const CHROME_OS = 'Chrome OS'; - const LINUX = 'Linux'; - const WINDOWS = 'Windows'; - const ANDROID = 'Android'; - const BLACKBERRY = 'BlackBerry'; - const FREEBSD = 'FreeBSD'; - const IPAD = 'iPad'; - const IPHONE = 'iPhone'; - const IPOD = 'iPod'; - const KINDLE = 'Kindle'; - const KINDLE_FIRE = 'Kindle Fire'; - const NETBSD = 'NetBSD'; - const NEW_NINTENDO_3DS = 'New Nintendo 3DS'; - const NINTENDO_3DS = 'Nintendo 3DS'; - const NINTENDO_DS = 'Nintendo DS'; - const NINTENDO_SWITCH = 'Nintendo Switch'; - const NINTENDO_WII = 'Nintendo Wii'; - const NINTENDO_WIIU = 'Nintendo WiiU'; - const OPENBSD = 'OpenBSD'; - const PLAYBOOK = 'PlayBook'; - const PLAYSTATION_3 = 'PlayStation 3'; - const PLAYSTATION_4 = 'PlayStation 4'; - const PLAYSTATION_VITA = 'PlayStation Vita'; - const TIZEN = 'Tizen'; - const WINDOWS_PHONE = 'Windows Phone'; - const XBOX = 'Xbox'; - const XBOX_ONE = 'Xbox One'; } diff --git a/src/UserAgent/Platforms.php b/src/UserAgent/Platforms.php new file mode 100644 index 0000000..9ef745b --- /dev/null +++ b/src/UserAgent/Platforms.php @@ -0,0 +1,39 @@ + Date: Tue, 27 Aug 2019 13:30:27 -0500 Subject: [PATCH 23/36] travis: make test --- .travis.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e85a3d..79c1f59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,16 +3,16 @@ sudo: false dist: precise php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - nightly - - hhvm + - "5.3" + - "5.4" + - "5.5" + - "5.6" + - "7.0" + - "7.1" + - "7.2" + - "7.3" + - "nightly" + - "hhvm" matrix: allow_failures: @@ -20,4 +20,4 @@ matrix: - php: hhvm install: composer install -script: vendor/bin/phpunit --coverage-clover=coverage.clover +script: make test From 4c7b860235b0a6664f50d9016aff89554b6042fb Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:06:59 -0500 Subject: [PATCH 24/36] Programmatically generate README # Conflicts: # README.md --- .mddoc.xml | 83 ++++++++++++++++++++++++++++++ README.md | 147 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 167 insertions(+), 63 deletions(-) create mode 100644 .mddoc.xml diff --git a/.mddoc.xml b/.mddoc.xml new file mode 100644 index 0000000..0c43d5f --- /dev/null +++ b/.mddoc.xml @@ -0,0 +1,83 @@ + + +
+ + + + + +
+ +
+
+ +
+
+ + +
+ +
+
+ +
+
+
+ +
+
+ PHP User Agent is available through Packagist via Composer. + +
+
+ '[Detected Platform]', + 'browser' => '[Detected Browser]', + 'version' => '[Detected Browser Version]', +); +*/ +``` +]]> +
+
+ +
+
+ +
+ +
+
+
diff --git a/README.md b/README.md index 30470f8..1fdd22a 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,12 @@ [![Join the chat at https://gitter.im/PhpUserAgentParser/Lobby](https://badges.gitter.im/PhpUserAgentParser/Lobby.svg)](https://gitter.im/PhpUserAgentParser/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![Latest Stable Version](https://poser.pugx.org/donatj/phpuseragentparser/v/stable.svg)](https://packagist.org/packages/donatj/phpuseragentparser) [![Total Downloads](https://poser.pugx.org/donatj/phpuseragentparser/downloads.svg)](https://packagist.org/packages/donatj/phpuseragentparser) [![Latest Unstable Version](https://poser.pugx.org/donatj/phpuseragentparser/v/unstable.svg)](https://packagist.org/packages/donatj/phpuseragentparser) [![License](https://poser.pugx.org/donatj/phpuseragentparser/license.svg)](https://packagist.org/packages/donatj/phpuseragentparser) -![CI](https://github.com/donatj/PhpUserAgent/workflows/CI/badge.svg) -[![Build Status](https://travis-ci.org/donatj/PhpUserAgent.svg?branch=master)](https://travis-ci.org/donatj/PhpUserAgent) + +[![Latest Stable Version](https://poser.pugx.org/donatj/phpuseragentparser/version)](https://packagist.org/packages/donatj/phpuseragentparser) +[![Total Downloads](https://poser.pugx.org/donatj/phpuseragentparser/downloads)](https://packagist.org/packages/donatj/phpuseragentparser) +[![License](https://poser.pugx.org/donatj/phpuseragentparser/license)](https://packagist.org/packages/donatj/phpuseragentparser) +[![Build Status](https://travis-ci.org/donatj/Flags.svg?branch=master)](https://travis-ci.org/donatj/Flags) + ## What It Is @@ -12,10 +15,9 @@ A simple, streamlined PHP user-agent parser! Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - ## Why Use This -You have your choice in user-agent parsers. This one detects **all modern browsers** in a very light, quick, understandable fashion. +You have your choice in user-agent parsers. This one detects **all modern browsers** in a very light, quick, understandable fashion. It is less than 200 lines of code, and consists of just three regular expressions! It can also correctly identify exotic versions of IE others fail on. @@ -25,11 +27,12 @@ It offers 100% unit test coverage, is installable via Composer, and is very easy This is not meant as a browser "knowledge engine" but rather a simple parser. Anything not adequately provided directly by the user agent string itself will simply not be provided by this. + ### OS Versions User-agent strings **are not** a reliable source of OS Version! -- Many agents simply don't send the information. +- Many agents simply don't send the information. - Others provide varying levels of accuracy. - Parsing Windows versions alone almost nearly doubles the size of the code. @@ -39,22 +42,20 @@ All that said, there is the start of a [branch to do it](https://github.com/dona ### Undetectable Browsers -- **Brave** - Brave is simply not differentiable from Chrome. This was a design descision on their part. +- **Brave** - Brave is simply not differentiable from Chrome. This was a design decision on their part. ## Requirements - - PHP 5.3.0+ +- **php**: >=5.3.0 ## Installing PHP User Agent is available through Packagist via Composer. -```json -{ - "require": { - "donatj/phpuseragentparser": "*" - } -} +Install the latest version with: + +```bash +composer require 'donatj/phpuseragentparser' ``` ## Sample Usage @@ -72,57 +73,77 @@ array( ## Currently Detected Platforms -- Desktop - - Windows - - Linux - - Macintosh - - Chrome OS -- Mobile - - Android - - iPhone - - iPad / iPod Touch - - Windows Phone OS - - Kindle - - Kindle Fire - - BlackBerry - - Playbook - - Tizen -- Console - - Oculus - - Nintendo 3DS - - New Nintendo 3DS - - Nintendo Wii - - Nintendo WiiU - - PlayStation 3 - - PlayStation 4 - - PlayStation Vita - - Xbox 360 - - Xbox One +- Macintosh +- Chrome OS +- Linux +- Windows +- Android +- BlackBerry +- FreeBSD +- iPad +- iPhone +- iPod +- Kindle +- Kindle Fire +- NetBSD +- New Nintendo 3DS +- Nintendo 3DS +- Nintendo DS +- Nintendo Switch +- Nintendo Wii +- Nintendo WiiU +- OpenBSD +- PlayBook +- PlayStation 3 +- PlayStation 4 +- PlayStation Vita +- Tizen +- Windows Phone +- Xbox +- Xbox One ## Currently Detected Browsers -- Android Browser -- BlackBerry Browser -- Camino -- Kindle / Silk -- Firefox / IceWeasel / IceCat -- Safari -- Internet Explorer / Edge -- IEMobile -- Chrome / HeadlessChrome -- Yandex Browser -- Opera -- Midori -- Vivaldi -- TizenBrowser -- OculusBrowser -- SamsungBrowser -- UC Browser -- Lynx -- Wget -- Curl -- Puffin - - +- AdsBot-Google +- Android Browser +- Baiduspider +- bingbot +- BlackBerry Browser +- Browser +- Bunjalloo +- Camino +- Chrome +- curl +- Edge +- facebookexternalhit +- FeedValidator +- Firefox +- Googlebot +- Googlebot-Image +- Googlebot-Video +- HeadlessChrome +- IEMobile +- Kindle +- Lynx +- Midori +- MSIE +- msnbot-media +- NetFront +- NintendoBrowser +- OculusBrowser +- Opera +- Opera Next +- Puffin +- Safari +- SamsungBrowser +- Silk +- TizenBrowser +- UC Browser +- Valve Steam Tenfoot +- Vivaldi +- Wget +- WordPress +- Yandex +- YandexBot More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). From f3a92aca097815bd412ecbf37d4215c8a1858b53 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 25 Nov 2019 17:10:11 -0600 Subject: [PATCH 25/36] Updates README for new usage --- .mddoc.xml | 19 ++++++++++++++++++- README.md | 21 +++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.mddoc.xml b/.mddoc.xml index 0c43d5f..94d1ccf 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -55,8 +55,10 @@ All that said, there is the start of a [branch to do it](https://github.com/dona PHP User Agent is available through Packagist via Composer. -
+
+ parse(); +// or +$ua = $parser(); + +$ua->platform(); +$ua->browser(); +$ua->browserVersion(); +``` ]]>
diff --git a/README.md b/README.md index 1fdd22a..fd9c3ce 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,9 @@ Install the latest version with: composer require 'donatj/phpuseragentparser' ``` -## Sample Usage +## Classic Usage + +The classic procedural use is as simple as: ```php $ua_info = parse_user_agent(); @@ -71,6 +73,21 @@ array( */ ``` + +The new object oriented wrapper form: + +```php +$parser = new UserAgentParser(); + +$ua = $parser->parse(); +// or +$ua = $parser(); + +$ua->platform(); +$ua->browser(); +$ua->browserVersion(); +``` + ## Currently Detected Platforms - Macintosh @@ -146,4 +163,4 @@ array( - Yandex - YandexBot -More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). +More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). \ No newline at end of file From 991ac3a1d2c5a5dedde64e58d6f953e7fd57d937 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 25 Nov 2019 17:12:06 -0600 Subject: [PATCH 26/36] Clarifies header --- .mddoc.xml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.mddoc.xml b/.mddoc.xml index 94d1ccf..660e407 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -55,7 +55,7 @@ All that said, there is the start of a [branch to do it](https://github.com/dona PHP User Agent is available through Packagist via Composer.
-
+
Date: Fri, 24 Apr 2020 11:12:24 -0500 Subject: [PATCH 27/36] Adds docs helpers --- .helpers/constants.php | 8 ++++++++ README.md | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .helpers/constants.php diff --git a/.helpers/constants.php b/.helpers/constants.php new file mode 100644 index 0000000..6039589 --- /dev/null +++ b/.helpers/constants.php @@ -0,0 +1,8 @@ +getConstants() as $constant ) { + echo "- {$constant}\n"; +} diff --git a/README.md b/README.md index 0e4d239..84858ba 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,6 @@ $ua->browserVersion(); - NintendoBrowser - OculusBrowser - Opera -- Opera Next - Puffin - Safari - SamsungBrowser From 05ec829470e6f2991f9755ccd4dea0db3d0bda87 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:13:43 -0500 Subject: [PATCH 28/36] Adds deprecation notice --- src/UserAgentParser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 2871c83..4d9ceb6 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -20,6 +20,7 @@ * @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 ) { From 9f908adafa6695bcb5d96ff7d70351a57da95b58 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:15:11 -0500 Subject: [PATCH 29/36] Post rebase formatting --- src/UserAgentParser.php | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 4d9ceb6..163444b 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -117,7 +117,7 @@ function parse_user_agent( $u_agent = null ) { $lowerBrowser = array_map('strtolower', $result[BROWSER]); - $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { + $find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) { $search = (array)$search; foreach( $search as $val ) { @@ -133,22 +133,22 @@ function parse_user_agent( $u_agent = null ) { return false; }; - $findT = function ( array $search, &$key = null, &$value = null ) use ( $find ) { - $value2 = null; - if( $find(array_keys($search), $key, $value2) ) { - $value = $search[$value2]; + $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 true; + } - return false; - }; + return false; + }; $key = 0; $val = ''; - if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { - $version = $result[BROWSER_VERSION][$key]; - } elseif( $find('Playstation Vita', $key, $platform) ) { + if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) { + $version = $result[BROWSER_VERSION][$key]; + } elseif( $find('Playstation Vita', $key, $platform) ) { $platform = 'PlayStation Vita'; $browser = 'Browser'; } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) { @@ -179,9 +179,9 @@ function parse_user_agent( $u_agent = null ) { } } } - } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { + } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) { $version = $result[BROWSER_VERSION][$key]; - } elseif( $rv_result && $find('Trident') ) { + } elseif( $rv_result && $find('Trident') ) { $browser = 'MSIE'; $version = $rv_result; } elseif( $browser == 'AppleWebKit' ) { @@ -208,4 +208,3 @@ function parse_user_agent( $u_agent = null ) { return array( PLATFORM => $platform ?: null, BROWSER => $browser ?: null, BROWSER_VERSION => $version ?: null ); } } - From e08caabae6305449d2c19f262d5f7bcc485a7646 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 11:39:57 -0500 Subject: [PATCH 30/36] Updates README --- .mddoc.xml | 3 ++- README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.mddoc.xml b/.mddoc.xml index 660e407..f7da1ac 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -7,7 +7,8 @@ - + +
Date: Fri, 24 Apr 2020 11:57:23 -0500 Subject: [PATCH 31/36] Adds a UserAgentInterface, update docs --- src/UserAgent/UserAgent.php | 6 +++++- src/UserAgent/UserAgentInterface.php | 25 +++++++++++++++++++++++++ src/UserAgent/UserAgentParser.php | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/UserAgent/UserAgentInterface.php diff --git a/src/UserAgent/UserAgent.php b/src/UserAgent/UserAgent.php index 9ea7b78..cf13f72 100644 --- a/src/UserAgent/UserAgent.php +++ b/src/UserAgent/UserAgent.php @@ -2,7 +2,7 @@ namespace donatj\UserAgent; -class UserAgent { +class UserAgent implements UserAgentInterface { /** * @var string|null @@ -32,6 +32,7 @@ public function __construct( $platform, $browser, $browserVersion ) { /** * @return string|null + * @see \donatj\UserAgent\Platforms for a list of tested platforms */ public function platform() { return $this->platform; @@ -39,12 +40,15 @@ public function platform() { /** * @return string|null + * @see \donatj\UserAgent\Browsers for a list of tested browsers. */ public function browser() { return $this->browser; } /** + * The version string. Formatting depends on the browser. + * * @return string|null */ public function browserVersion() { diff --git a/src/UserAgent/UserAgentInterface.php b/src/UserAgent/UserAgentInterface.php new file mode 100644 index 0000000..645d62b --- /dev/null +++ b/src/UserAgent/UserAgentInterface.php @@ -0,0 +1,25 @@ + Date: Fri, 24 Apr 2020 12:23:14 -0500 Subject: [PATCH 32/36] Simplifies ternary --- src/UserAgentParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 163444b..ee5bc6f 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -102,7 +102,7 @@ function parse_user_agent( $u_agent = null ) { // If nothing matched, return null (to avoid undefined index errors) if( !isset($result[BROWSER][0]) || !isset($result[BROWSER_VERSION][0]) ) { if( preg_match('%^(?!Mozilla)(?P[A-Z0-9\-]+)(/(?P[0-9A-Z.]+))?%ix', $u_agent, $result) ) { - return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => isset($result[BROWSER_VERSION]) ? $result[BROWSER_VERSION] ?: null : null ); + return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => empty($result[BROWSER_VERSION]) ? null : $result[BROWSER_VERSION] ); } return $empty; From 296f8de301efd20bae421392a9eb8bab920c470a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 12:37:57 -0500 Subject: [PATCH 33/36] Adds upgrading notes to README --- .mddoc.xml | 7 +++++++ README.md | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/.mddoc.xml b/.mddoc.xml index f7da1ac..928f4c5 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -14,6 +14,13 @@ A simple, streamlined PHP user-agent parser! Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php +]]> +
+
+
diff --git a/README.md b/README.md index 13133bb..5ed0205 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,12 @@ A simple, streamlined PHP user-agent parser! Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php +## Upgrading to `1.*` + +The new `1.*` release **does not break compatibility** with `0.*` and nothing need to change to upgrade. However, the global `parse_user_agent` is now deprecated; it has been replaced with the namespaced `\donatj\UserAgent\parse_user_agent` and functions exactly the same. You can easily replace any existing call to `parse_user_agent` with `\donatj\UserAgent\parse_user_agent` + +In addition, 1.x adds a convenience object wrapper you may use should you prefer. More information on this is in the Usage section below. + ## Why Use This You have your choice in user-agent parsers. This one detects **all modern browsers** in a very light, quick, understandable fashion. From d1459fc054949b73b6950af1582d41252e521c8a Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 13:01:46 -0500 Subject: [PATCH 34/36] Update the format of README output --- .helpers/constants.php | 11 +++- README.md | 144 ++++++++++++++++++++++------------------- 2 files changed, 85 insertions(+), 70 deletions(-) diff --git a/.helpers/constants.php b/.helpers/constants.php index 6039589..268f1a1 100644 --- a/.helpers/constants.php +++ b/.helpers/constants.php @@ -3,6 +3,13 @@ require __DIR__ . '/../vendor/autoload.php'; $class = new ReflectionClass($argv[1]); -foreach( $class->getConstants() as $constant ) { - echo "- {$constant}\n"; + +echo "Predefined helper constants from {$class->getName()}\n\n"; + +echo "| Constant | {$argv[2]} | \n|----------|----------| \n"; + +foreach( $class->getConstants() as $constant => $value ) { + echo "| `{$class->getShortName()}::{$constant}` | {$value} | \n"; } + +echo "\n"; \ No newline at end of file diff --git a/README.md b/README.md index 5ed0205..0c81751 100644 --- a/README.md +++ b/README.md @@ -97,76 +97,84 @@ $ua->browserVersion(); ## Currently Detected Platforms -- Macintosh -- Chrome OS -- Linux -- Windows -- Android -- BlackBerry -- FreeBSD -- iPad -- iPhone -- iPod -- Kindle -- Kindle Fire -- NetBSD -- New Nintendo 3DS -- Nintendo 3DS -- Nintendo DS -- Nintendo Switch -- Nintendo Wii -- Nintendo WiiU -- OpenBSD -- PlayBook -- PlayStation 3 -- PlayStation 4 -- PlayStation Vita -- Tizen -- Windows Phone -- Xbox -- Xbox One +Predefined helper constants from donatj\UserAgent\Platforms + +| Constant | Platform | +|----------|----------| +| `Platforms::MACINTOSH` | Macintosh | +| `Platforms::CHROME_OS` | Chrome OS | +| `Platforms::LINUX` | Linux | +| `Platforms::WINDOWS` | Windows | +| `Platforms::ANDROID` | Android | +| `Platforms::BLACKBERRY` | BlackBerry | +| `Platforms::FREEBSD` | FreeBSD | +| `Platforms::IPAD` | iPad | +| `Platforms::IPHONE` | iPhone | +| `Platforms::IPOD` | iPod | +| `Platforms::KINDLE` | Kindle | +| `Platforms::KINDLE_FIRE` | Kindle Fire | +| `Platforms::NETBSD` | NetBSD | +| `Platforms::NEW_NINTENDO_3DS` | New Nintendo 3DS | +| `Platforms::NINTENDO_3DS` | Nintendo 3DS | +| `Platforms::NINTENDO_DS` | Nintendo DS | +| `Platforms::NINTENDO_SWITCH` | Nintendo Switch | +| `Platforms::NINTENDO_WII` | Nintendo Wii | +| `Platforms::NINTENDO_WIIU` | Nintendo WiiU | +| `Platforms::OPENBSD` | OpenBSD | +| `Platforms::PLAYBOOK` | PlayBook | +| `Platforms::PLAYSTATION_3` | PlayStation 3 | +| `Platforms::PLAYSTATION_4` | PlayStation 4 | +| `Platforms::PLAYSTATION_VITA` | PlayStation Vita | +| `Platforms::TIZEN` | Tizen | +| `Platforms::WINDOWS_PHONE` | Windows Phone | +| `Platforms::XBOX` | Xbox | +| `Platforms::XBOX_ONE` | Xbox One | ## Currently Detected Browsers -- AdsBot-Google -- Android Browser -- Baiduspider -- bingbot -- BlackBerry Browser -- Browser -- Bunjalloo -- Camino -- Chrome -- curl -- Edge -- facebookexternalhit -- FeedValidator -- Firefox -- Googlebot -- Googlebot-Image -- Googlebot-Video -- HeadlessChrome -- IEMobile -- Kindle -- Lynx -- Midori -- MSIE -- msnbot-media -- NetFront -- NintendoBrowser -- OculusBrowser -- Opera -- Puffin -- Safari -- SamsungBrowser -- Silk -- TizenBrowser -- UC Browser -- Valve Steam Tenfoot -- Vivaldi -- Wget -- WordPress -- Yandex -- YandexBot +Predefined helper constants from donatj\UserAgent\Browsers + +| Constant | Browser | +|----------|----------| +| `Browsers::ADSBOT_GOOGLE` | AdsBot-Google | +| `Browsers::ANDROID_BROWSER` | Android Browser | +| `Browsers::BAIDUSPIDER` | Baiduspider | +| `Browsers::BINGBOT` | bingbot | +| `Browsers::BLACKBERRY_BROWSER` | BlackBerry Browser | +| `Browsers::BROWSER` | Browser | +| `Browsers::BUNJALLOO` | Bunjalloo | +| `Browsers::CAMINO` | Camino | +| `Browsers::CHROME` | Chrome | +| `Browsers::CURL` | curl | +| `Browsers::EDGE` | Edge | +| `Browsers::FACEBOOKEXTERNALHIT` | facebookexternalhit | +| `Browsers::FEEDVALIDATOR` | FeedValidator | +| `Browsers::FIREFOX` | Firefox | +| `Browsers::GOOGLEBOT` | Googlebot | +| `Browsers::GOOGLEBOT_IMAGE` | Googlebot-Image | +| `Browsers::GOOGLEBOT_VIDEO` | Googlebot-Video | +| `Browsers::HEADLESSCHROME` | HeadlessChrome | +| `Browsers::IEMOBILE` | IEMobile | +| `Browsers::KINDLE` | Kindle | +| `Browsers::LYNX` | Lynx | +| `Browsers::MIDORI` | Midori | +| `Browsers::MSIE` | MSIE | +| `Browsers::MSNBOT_MEDIA` | msnbot-media | +| `Browsers::NETFRONT` | NetFront | +| `Browsers::NINTENDOBROWSER` | NintendoBrowser | +| `Browsers::OCULUSBROWSER` | OculusBrowser | +| `Browsers::OPERA` | Opera | +| `Browsers::PUFFIN` | Puffin | +| `Browsers::SAFARI` | Safari | +| `Browsers::SAMSUNGBROWSER` | SamsungBrowser | +| `Browsers::SILK` | Silk | +| `Browsers::TIZENBROWSER` | TizenBrowser | +| `Browsers::UC_BROWSER` | UC Browser | +| `Browsers::VALVE_STEAM_TENFOOT` | Valve Steam Tenfoot | +| `Browsers::VIVALDI` | Vivaldi | +| `Browsers::WGET` | Wget | +| `Browsers::WORDPRESS` | WordPress | +| `Browsers::YANDEX` | Yandex | +| `Browsers::YANDEXBOT` | YandexBot | More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). \ No newline at end of file From 8b7a450a05438d6edfacd5d2075f102ce733cc3f Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 13:03:13 -0500 Subject: [PATCH 35/36] Slight README improvment --- .helpers/constants.php | 2 +- .mddoc.xml | 4 ++-- README.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.helpers/constants.php b/.helpers/constants.php index 268f1a1..dc750a9 100644 --- a/.helpers/constants.php +++ b/.helpers/constants.php @@ -4,7 +4,7 @@ $class = new ReflectionClass($argv[1]); -echo "Predefined helper constants from {$class->getName()}\n\n"; +echo "Predefined helper constants from `{$class->getName()}`\n\n"; echo "| Constant | {$argv[2]} | \n|----------|----------| \n"; diff --git a/.mddoc.xml b/.mddoc.xml index 928f4c5..d7d610e 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -95,10 +95,10 @@ $ua->browserVersion(); ]]>
- +
- +
browserVersion(); ## Currently Detected Platforms -Predefined helper constants from donatj\UserAgent\Platforms +Predefined helper constants from `donatj\UserAgent\Platforms` | Constant | Platform | |----------|----------| @@ -132,7 +132,7 @@ Predefined helper constants from donatj\UserAgent\Platforms ## Currently Detected Browsers -Predefined helper constants from donatj\UserAgent\Browsers +Predefined helper constants from `donatj\UserAgent\Browsers` | Constant | Browser | |----------|----------| From 5769c4ffe164a42119b1fce48cc63413fcdd29ea Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 24 Apr 2020 13:04:19 -0500 Subject: [PATCH 36/36] Make CI run `make test` --- .github/workflows/ci.yml | 4 ++-- Makefile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd1e7d6..315a4dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,5 +28,5 @@ jobs: - name: Install dependencies with composer run: composer install - - name: Test with phpunit - run: vendor/bin/phpunit --coverage-text + - name: Run tests + run: make test diff --git a/Makefile b/Makefile index 827e734..9281366 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: test test: - ./vendor/bin/phpunit + ./vendor/bin/phpunit --coverage-text .PHONY: generate generate: