From e98f358ba71726e2ef77aadee179a2678c162da8 Mon Sep 17 00:00:00 2001 From: Shelton Koskie Date: Wed, 24 Jul 2013 13:55:20 -0500 Subject: [PATCH 1/6] Added Facebook Bot Filter. Untested. --- lib/phpUserAgentStringParser.php | 37 +++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/phpUserAgentStringParser.php b/lib/phpUserAgentStringParser.php index e33548e..9b07b32 100644 --- a/lib/phpUserAgentStringParser.php +++ b/lib/phpUserAgentStringParser.php @@ -39,7 +39,7 @@ public function parse($userAgentString = null) /** * Detect quickly informations from the user agent string - * + * * @param string $userAgentString user agent string * @return array user agent informations array */ @@ -83,7 +83,7 @@ protected function doParse($userAgentString) // Find operating system $pattern = '#'.join('|', $this->getKnownOperatingSystems()).'#'; - + if (preg_match($pattern, $userAgent['string'], $match)) { if (isset($match[0])) @@ -94,7 +94,7 @@ protected function doParse($userAgentString) // Find engine $pattern = '#'.join('|', $this->getKnownEngines()).'#'; - + if (preg_match($pattern, $userAgent['string'], $match)) { if (isset($match[0])) @@ -143,12 +143,13 @@ public function getFilters() 'filterOperaVersion', 'filterYahoo', 'filterMsie', + 'filterFacebookBot' ); } /** * Add a filter to be called when parsing a user agent - * + * * @param string $filter name of the filter method */ public function addFilter($filter) @@ -308,14 +309,24 @@ protected function filterMsie(array &$userAgent) } } - /** - * Android has a safari like signature - */ - protected function filterAndroid(array &$userAgent) { - if ('safari' === $userAgent['browser_name'] && strpos($userAgent['string'], 'android ')) { - $userAgent['browser_name'] = 'android'; - $userAgent['operating_system'] = 'android'; - $userAgent['browser_version'] = preg_replace('|.+android ([0-9]+(?:\.[0-9]+)+).+|', '$1', $userAgent['string']); - } + /** + * Android has a safari like signature + */ + protected function filterAndroid(array &$userAgent) + { + if ('safari' === $userAgent['browser_name'] && strpos($userAgent['string'], 'android ')) + { + $userAgent['browser_name'] = 'android'; + $userAgent['operating_system'] = 'android'; + $userAgent['browser_version'] = preg_replace('|.+android ([0-9]+(?:\.[0-9]+)+).+|', '$1', $userAgent['string']); + } + } + + protected function filterFacebookBot(array &$userAgent) + { + if('facebookexternalhit' === $userAgent['browser_name']) + { + $userAgent['browser_name'] = 'facebookbot'; } + } } From a4ac08e8a6d569e9d61ea21fa76e3c13ce79464e Mon Sep 17 00:00:00 2001 From: Shelton Koskie Date: Wed, 24 Jul 2013 14:20:32 -0500 Subject: [PATCH 2/6] Added isBot method. Code is from user diem-project, commit number 1381e1674c57e4f574efc2c2bfa29db76eb56134 of 2011-09-06, which was forked and modified, but never merged back into the main repo. --- lib/phpUserAgent.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/phpUserAgent.php b/lib/phpUserAgent.php index 2e29b0d..0545d1c 100644 --- a/lib/phpUserAgent.php +++ b/lib/phpUserAgent.php @@ -135,6 +135,26 @@ public function isUnknown() return empty($this->browserName); } + /** + * Returns true if user agent is a known bot/crawler + * + * @return boolean + */ + public function isBot() + { + return in_array($this->getBrowserName(), $this->getKnownBots()); + } + + /** + * Returns an array of strings identifying know bots + * + * @return array + */ + public function getKnownBots() + { + return array('googlebot'); + } + /** * @return string combined browser name and version */ From 44aabca2915190868438f876c2008a2749374310 Mon Sep 17 00:00:00 2001 From: Shelton Koskie Date: Wed, 24 Jul 2013 14:27:58 -0500 Subject: [PATCH 3/6] Expanded list of known bots. --- lib/phpUserAgent.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/phpUserAgent.php b/lib/phpUserAgent.php index 0545d1c..61c1d7d 100644 --- a/lib/phpUserAgent.php +++ b/lib/phpUserAgent.php @@ -152,7 +152,12 @@ public function isBot() */ public function getKnownBots() { - return array('googlebot'); + return array( + 'googlebot', + 'msnbot', + 'yahoobot', + 'facebookbot' + ); } /** From 4adc72777d820ac270e1800dc65e0eff49cc1dee Mon Sep 17 00:00:00 2001 From: NacerMontreal Date: Sat, 3 Aug 2013 11:25:18 -0400 Subject: [PATCH 4/6] Update phpUserAgentStringParser.php To Give exacte Operating system. I have made this change. I wish that it will help. Thanks --- lib/phpUserAgentStringParser.php | 56 ++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/phpUserAgentStringParser.php b/lib/phpUserAgentStringParser.php index e33548e..c5913ed 100644 --- a/lib/phpUserAgentStringParser.php +++ b/lib/phpUserAgentStringParser.php @@ -204,13 +204,32 @@ protected function getKnownBrowserAliases() protected function getKnownOperatingSystems() { return array( - 'windows', - 'macintosh', - 'linux', - 'freebsd', - 'unix', - 'iphone' + + 'Windows 8', + 'Windows 7', + 'Windows Vista', + 'Windows Server 2003/XP x64', + 'Windows XP', + 'Windows XP', + 'Windows 2000', + 'Windows ME', + 'Windows 98', + 'Windows 95', + 'Windows 3.11', + 'Mac OS X', + 'Macintosh; Intel Mac OS X', + 'Mac OS 9', + 'Linux', + 'Ubuntu', + 'iPhone', + 'iPod', + 'iPad', + 'Android', + 'BlackBerry', + 'Mobile' + ); + } /** @@ -220,7 +239,30 @@ protected function getKnownOperatingSystems() */ protected function getKnownOperatingSystemAliases() { - return array(); + return array( + 'windows nt 6.2' => 'Windows 8', + 'windows nt 6.1' => 'Windows 7', + 'windows nt 6.0' => 'Windows Vista', + 'windows nt 5.2' => 'Windows Server 2003/XP x64', + 'windows nt 5.1' => 'Windows XP', + 'windows xp' => 'Windows XP', + 'windows nt 5.0' => 'Windows 2000', + 'windows me' => 'Windows ME', + 'win98' => 'Windows 98', + 'win95' => 'Windows 95', + 'win16' => 'Windows 3.11', + 'macintosh|mac os x' => 'Mac OS X', + 'macintosh; intel mac os x' =>'Macintosh; Intel Mac OS X', + 'mac_powerpc' => 'Mac OS 9', + 'linux' => 'Linux', + 'ubuntu' => 'Ubuntu', + 'iphone' => 'iPhone', + 'ipod' => 'iPod', + 'ipad' => 'iPad', + 'android' => 'Android', + 'blackberry' => 'BlackBerry', + 'webos' => 'Mobile' + ); } /** From dc8033028105e4f593c1a2a3744c7eb748eb21a6 Mon Sep 17 00:00:00 2001 From: Erwan Date: Fri, 11 Oct 2013 11:05:00 +0200 Subject: [PATCH 5/6] Add iPad (detection and test UA) + indentation correction --- composer.json | 5 ---- lib/phpUserAgentStringParser.php | 11 +++++---- test/StringParserTest.php | 39 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 864001e..4ed3638 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,5 @@ "lib/phpUserAgent.php", "lib/phpUserAgentStringParser.php" ] - }, - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } } } diff --git a/lib/phpUserAgentStringParser.php b/lib/phpUserAgentStringParser.php index e33548e..c1aa4b5 100644 --- a/lib/phpUserAgentStringParser.php +++ b/lib/phpUserAgentStringParser.php @@ -39,7 +39,7 @@ public function parse($userAgentString = null) /** * Detect quickly informations from the user agent string - * + * * @param string $userAgentString user agent string * @return array user agent informations array */ @@ -83,7 +83,7 @@ protected function doParse($userAgentString) // Find operating system $pattern = '#'.join('|', $this->getKnownOperatingSystems()).'#'; - + if (preg_match($pattern, $userAgent['string'], $match)) { if (isset($match[0])) @@ -94,7 +94,7 @@ protected function doParse($userAgentString) // Find engine $pattern = '#'.join('|', $this->getKnownEngines()).'#'; - + if (preg_match($pattern, $userAgent['string'], $match)) { if (isset($match[0])) @@ -148,7 +148,7 @@ public function getFilters() /** * Add a filter to be called when parsing a user agent - * + * * @param string $filter name of the filter method */ public function addFilter($filter) @@ -209,7 +209,8 @@ protected function getKnownOperatingSystems() 'linux', 'freebsd', 'unix', - 'iphone' + 'iphone', + 'ipad' ); } diff --git a/test/StringParserTest.php b/test/StringParserTest.php index 5906682..b1e2e7f 100644 --- a/test/StringParserTest.php +++ b/test/StringParserTest.php @@ -80,29 +80,32 @@ 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; de-de) AppleWebKit/528.18 (KHTML, like Gecko) Mobile/7D11' => array('applewebkit', '528.18', 'iphone', 'webkit'), - // Motorola Xoom - 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13' => - array('android', '3.0', 'android', 'webkit'), + // Motorola Xoom + 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13' + => array('android', '3.0', 'android', 'webkit'), - // Samsung Galaxy Tab - 'Mozilla/5.0 (Linux U Android 2.2 es-es GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => - array('android', '2.2', 'android', 'webkit'), + // Samsung Galaxy Tab + 'Mozilla/5.0 (Linux U Android 2.2 es-es GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' + => array('android', '2.2', 'android', 'webkit'), - // Google Nexus - 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => - array('android', '2.2', 'android', 'webkit'), + // Google Nexus + 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' + => array('android', '2.2', 'android', 'webkit'), - // HTC Desire + // HTC Desire + 'Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' + => array('android', '2.1', 'android', 'webkit'), - 'Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' => - array('android', '2.1', 'android', 'webkit'), + 'Mozilla/5.0 (Linux; U; Android 2.3.6; ru-ru; GT-B5512 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' + => array('android', '2.3.6', 'android', 'webkit'), - 'Mozilla/5.0 (Linux; U; Android 2.3.6; ru-ru; GT-B5512 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => - array('android', '2.3.6', 'android', 'webkit'), + // Nexus 7 + 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' + => array('android', '4.1.1', 'android', 'webkit'), - // Nexus 7 - 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' => - array('android', '4.1.1', 'android', 'webkit'), + // Ipad + 'Mozilla/5.0 (iPad; CPU OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25' + => array('safari', '6.0', 'ipad', 'webkit'), ); $t = new lime_test(count($tests)); @@ -120,6 +123,6 @@ ); $result = $parser->parse($userAgentString); - + $t->is_deeply($result, $expected, $userAgentString.' -> '.implode(', ', $result)); } From 372af881bf05fb8100dd3b52f0f59b02d0a1e9b5 Mon Sep 17 00:00:00 2001 From: Erwan Date: Thu, 21 Nov 2013 11:06:44 +0100 Subject: [PATCH 6/6] Add IE11 --- lib/phpUserAgentStringParser.php | 6 ++++++ test/StringParserTest.php | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/phpUserAgentStringParser.php b/lib/phpUserAgentStringParser.php index f66b8d0..e76d3ad 100644 --- a/lib/phpUserAgentStringParser.php +++ b/lib/phpUserAgentStringParser.php @@ -342,6 +342,7 @@ protected function filterYahoo(array &$userAgent) /** * MSIE does not always declare its engine + * IE11 hasn't msie in his user agent string */ protected function filterMsie(array &$userAgent) { @@ -349,6 +350,11 @@ protected function filterMsie(array &$userAgent) { $userAgent['engine'] = 'trident'; } + if (empty($userAgent['browser_name']) && ($userAgent['engine'] == 'trident') && strpos($userAgent['string'], 'rv:')) + { + $userAgent['browser_name'] = 'msie'; + $userAgent['browser_version'] = preg_replace('|.+rv:([0-9]+(?:\.[0-9]+)+).+|', '$1', $userAgent['string']); + } } /** diff --git a/test/StringParserTest.php b/test/StringParserTest.php index c21b3aa..b18a59b 100644 --- a/test/StringParserTest.php +++ b/test/StringParserTest.php @@ -110,6 +110,10 @@ // Facebook 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' => array('facebookbot', '1.1', null, null), + + // IE 11 + 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko' + => array('msie', '11.0', 'Windows 7', 'trident'), ); $t = new lime_test(count($tests));