[TYPO3-core] RFC #16892: Bug: t3lib_utility_Client::getBrowserInfo() fails to detect Chrome

Andreas Lappe nd at off-pist.de
Sun Jan 2 22:30:52 CET 2011


This is an SVN patch request.

Type: Bugfix

Bugtracker references:
http://bugs.typo3.org/view.php?id=16892

Branches:
trunk

Problem:
t3lib_utility_Client::getBrowserInfo() detects Chrome as safari because
the UserAgent has Safari as last entry. 

Solution:
The attached patch fixes it and adds a UnitTest to make sure it works
and does not interfere with Safari, Konqueror etc.

Notes:
The test goes together with the tests posted in 
http://bugs.typo3.org/view.php?id=16878

So long
Andy
-- 
“Remember that ‘He who dies with the most toys wins’ is a bumper
sticker, not wisdom.” --Barry Schwartz
-------------- next part --------------
diff --git a/t3lib/utility/class.t3lib_utility_client.php b/t3lib/utility/class.t3lib_utility_client.php
index 864e082..f272d9f 100644
--- a/t3lib/utility/class.t3lib_utility_client.php
+++ b/t3lib/utility/class.t3lib_utility_client.php
@@ -86,8 +86,13 @@ final class t3lib_utility_Client {
 				// in the UA).  That's usually the most correct.
 				// For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
 			$lastIndex = count($matches['browser']) - 1;
-			$browserInfo['browser'] = $matches['browser'][$lastIndex];
-			$browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
+			// Chrome seems to place Safari as last string, so we need to make an exception here:
+			if (t3lib_div::inArray($matches['browser'], 'chrome')) {
+				$browserInfo['browser'] = 'chrome';
+			} else {
+				$browserInfo['browser'] = $matches['browser'][$lastIndex];
+				$browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
+			}
 				//But return all parsed browsers / version in an extra array
 			for ($i = 0; $i <= $lastIndex; $i++) {
 				if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
diff --git a/tests/t3lib/utility/t3lib_utility_clientTest.php b/tests/t3lib/utility/t3lib_utility_clientTest.php
index 57410de..e2068df 100644
--- a/tests/t3lib/utility/t3lib_utility_clientTest.php
+++ b/tests/t3lib/utility/t3lib_utility_clientTest.php
@@ -513,5 +513,18 @@ class t3lib_utility_clientTest extends tx_phpunit_testcase {
 			$infoArray['browser']
 		);
 	}
+
+	/**
+	 * @test
+	 */
+	public function getBrowserInfoReturnsCorrectBrowserValueForUserAgentStringOfChrome() {
+		$userAgentString = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_0; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.231 Safari/534.10';
+		$infoArray = t3lib_utility_Client::getBrowserInfo($userAgentString);
+
+		$this->assertSame(
+			'chrome',
+			$infoArray['browser']
+		);
+	}
 }
 ?>


More information about the TYPO3-team-core mailing list