From 246c1cf0a44f07168c702203bf30d5f48f17bab0 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 16 Mar 2021 11:25:14 -0500 Subject: [PATCH] Adds support for TelegramBot, Twitterbot and iMessageBot --- .mddoc.xml | 5 ++--- README.md | 7 +++++-- src/UserAgent/Browsers.php | 3 +++ src/UserAgentParser.php | 6 +++--- tests/user_agents.dist.json | 20 ++++++++++++++++++++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.mddoc.xml b/.mddoc.xml index c1194e0..22b4fdb 100644 --- a/.mddoc.xml +++ b/.mddoc.xml @@ -7,13 +7,12 @@ -
@@ -106,7 +105,7 @@ $ua->browserVersion();
diff --git a/README.md b/README.md index 817b29c..61fed65 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A simple, streamlined PHP user-agent parser! -Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php +Licensed under the MIT license: https://www.opensource.org/licenses/mit-license.php ## Upgrading to `1.*` @@ -161,6 +161,7 @@ Predefined helper constants from `donatj\UserAgent\Browsers` | `Browsers::GOOGLEBOT_VIDEO` | Googlebot-Video | | `Browsers::HEADLESSCHROME` | HeadlessChrome | | `Browsers::IEMOBILE` | IEMobile | +| `Browsers::IMESSAGEBOT` | iMessageBot | | `Browsers::KINDLE` | Kindle | | `Browsers::LYNX` | Lynx | | `Browsers::MIDORI` | Midori | @@ -176,7 +177,9 @@ Predefined helper constants from `donatj\UserAgent\Browsers` | `Browsers::SAILFISHBROWSER` | SailfishBrowser | | `Browsers::SAMSUNGBROWSER` | SamsungBrowser | | `Browsers::SILK` | Silk | +| `Browsers::TELEGRAMBOT` | TelegramBot | | `Browsers::TIZENBROWSER` | TizenBrowser | +| `Browsers::TWITTERBOT` | Twitterbot | | `Browsers::UC_BROWSER` | UC Browser | | `Browsers::VALVE_STEAM_TENFOOT` | Valve Steam Tenfoot | | `Browsers::VIVALDI` | Vivaldi | @@ -185,4 +188,4 @@ Predefined helper constants from `donatj\UserAgent\Browsers` | `Browsers::YANDEX` | Yandex | | `Browsers::YANDEXBOT` | YandexBot | -More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). +More information is available at [Donat Studios](https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT). \ No newline at end of file diff --git a/src/UserAgent/Browsers.php b/src/UserAgent/Browsers.php index 35d5af0..bf4844f 100644 --- a/src/UserAgent/Browsers.php +++ b/src/UserAgent/Browsers.php @@ -26,6 +26,7 @@ interface Browsers { const GOOGLEBOT_VIDEO = 'Googlebot-Video'; const HEADLESSCHROME = 'HeadlessChrome'; const IEMOBILE = 'IEMobile'; + const IMESSAGEBOT = 'iMessageBot'; const KINDLE = 'Kindle'; const LYNX = 'Lynx'; const MIDORI = 'Midori'; @@ -41,7 +42,9 @@ interface Browsers { const SAILFISHBROWSER = 'SailfishBrowser'; const SAMSUNGBROWSER = 'SamsungBrowser'; const SILK = 'Silk'; + const TELEGRAMBOT = 'TelegramBot'; const TIZENBROWSER = 'TizenBrowser'; + const TWITTERBOT = 'Twitterbot'; const UC_BROWSER = 'UC Browser'; const VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot'; const VIVALDI = 'Vivaldi'; diff --git a/src/UserAgentParser.php b/src/UserAgentParser.php index 4ab2306..4c7ea24 100644 --- a/src/UserAgentParser.php +++ b/src/UserAgentParser.php @@ -94,7 +94,7 @@ function parse_user_agent( $u_agent = null ) { %(?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|SailfishBrowser|XiaoMi/MiuiBrowser| -Baiduspider|Applebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl| +Baiduspider|Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl| Valve\ Steam\ Tenfoot| NintendoBrowser|PLAYSTATION\ (\d|Vita)+) (?:\)?;?) @@ -149,8 +149,8 @@ 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', 'XiaoMi/MiuiBrowser' => 'MiuiBrowser' ), $key, $browser) ) { - $version = $result[BROWSER_VERSION][$key]; + if( $findT(array( 'OPR' => 'Opera', 'Facebot' => 'iMessageBot', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge', 'XiaoMi/MiuiBrowser' => 'MiuiBrowser' ), $key, $browser) ) { + $version = is_numeric(substr($result[BROWSER_VERSION][$key], 0, 1)) ? $result[BROWSER_VERSION][$key] : null; } elseif( $find('Playstation Vita', $key, $platform) ) { $platform = 'PlayStation Vita'; $browser = 'Browser'; diff --git a/tests/user_agents.dist.json b/tests/user_agents.dist.json index 723f8c6..494a557 100644 --- a/tests/user_agents.dist.json +++ b/tests/user_agents.dist.json @@ -224,6 +224,11 @@ "browser": "HeadlessChrome", "version": "69.0.3452.0" }, + "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit\/601.2.4 (KHTML, like Gecko) Version\/9.0.1 Safari\/601.2.4 facebookexternalhit\/1.1 Facebot Twitterbot\/1.0": { + "platform": "Macintosh", + "browser": "iMessageBot", + "version": null + }, "Mozilla\/5.0 (X11; U; Linux i686; de-DE; rv:1.8.1.9) Gecko\/20071103 Midori\/0.0.10": { "platform": "Linux", "browser": "Midori", @@ -1094,6 +1099,11 @@ "browser": "curl", "version": "7.19.7" }, + "facebookexternalhit\/1.1": { + "platform": null, + "browser": "facebookexternalhit", + "version": "1.1" + }, "facebookexternalhit\/1.1 (+http:\/\/www.facebook.com\/externalhit_uatext.php)": { "platform": null, "browser": "facebookexternalhit", @@ -1134,6 +1144,16 @@ "browser": "msnbot-media", "version": "1.1" }, + "TelegramBot (like TwitterBot)": { + "platform": null, + "browser": "TelegramBot", + "version": null + }, + "Twitterbot\/1.0": { + "platform": null, + "browser": "Twitterbot", + "version": "1.0" + }, "WordPress\/3.7.1; http:\/\/wordpress.com": { "platform": null, "browser": "WordPress",