Permalink
Browse files

MDL-25611 more browser detection cleanup and improvements

general WebKit support, adding version support for android, fixing android browser name (engine name first), adding more tests
  • Loading branch information...
1 parent 0667103 commit 2168425a9ea81d496c9904e66eedf14abbec07c0 @skodak skodak committed Dec 8, 2010
Showing with 57 additions and 11 deletions.
  1. +34 −11 lib/moodlelib.php
  2. +23 −0 lib/simpletest/testmoodlelib.php
View
45 lib/moodlelib.php
@@ -7588,7 +7588,22 @@ function check_browser_version($brand, $version = null) {
break;
- case 'Safari': /// Desktop or laptop Apple Safari browser
+ case 'WebKit': /// WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles)
+ if (strpos($agent, 'AppleWebKit') === false) {
+ return false;
+ }
+ if (empty($version)) {
+ return true; // no version specified
+ }
+ if (preg_match("/AppleWebKit\/([0-9]+)/i", $agent, $match)) {
+ if (version_compare($match[1], $version) >= 0) {
+ return true;
+ }
+ }
+ break;
+
+
+ case 'Safari': /// Desktop version of Apple Safari browser - no mobile or touch devices
if (strpos($agent, 'AppleWebKit') === false) {
return false;
}
@@ -7602,6 +7617,9 @@ function check_browser_version($brand, $version = null) {
if (strpos($agent, 'SimbianOS')) { // Reject SimbianOS
return false;
}
+ if (strpos($agent, 'Android')) { // Reject Androids too
+ return false;
+ }
if (strpos($agent, 'iPhone') or strpos($agent, 'iPad') or strpos($agent, 'iPod')) {
// No Apple mobile devices here - editor does not work, course ajax is not touch compatible, etc.
return false;
@@ -7636,7 +7654,7 @@ function check_browser_version($brand, $version = null) {
break;
- case 'Safari iOS': /// Safari on iPhone and iPad
+ case 'Safari iOS': /// Safari on iPhone, iPad and iPod touch
if (strpos($agent, 'AppleWebKit') === false or strpos($agent, 'Safari') === false) {
return false;
}
@@ -7654,15 +7672,18 @@ function check_browser_version($brand, $version = null) {
break;
- case 'Android WebKit': /// WebKit browser on Android
+ case 'WebKit Android': /// WebKit browser on Android
if (strpos($agent, 'Linux; U; Android') === false) {
return false;
}
if (empty($version)) {
return true; // no version specified
}
- //TODO: add version check here
- return true;
+ if (preg_match("/AppleWebKit\/([0-9]+)/i", $agent, $match)) {
+ if (version_compare($match[1], $version) >= 0) {
+ return true;
+ }
+ }
break;
}
@@ -7691,20 +7712,22 @@ function get_browser_version_classes() {
$classes[] = 'ie6';
}
- } elseif (check_browser_version("Firefox") || check_browser_version("Gecko") || check_browser_version("Camino")) {
+ } else if (check_browser_version("Firefox") || check_browser_version("Gecko") || check_browser_version("Camino")) {
$classes[] = 'gecko';
if (preg_match('/rv\:([1-2])\.([0-9])/', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$classes[] = "gecko{$matches[1]}{$matches[2]}";
}
- } elseif (check_browser_version("Safari") || check_browser_version("Chrome")) {
+ } else if (check_browser_version("WebKit")) {
$classes[] = 'safari';
+ if (check_browser_version("Safari iOS")) {
+ $classes[] = 'ios';
- } else if (check_browser_version("Safari iOS")) {
- $classes[] = 'safari';
- $classes[] = 'ios';
+ } else if (check_browser_version("WebKit Android")) {
+ $classes[] = 'android';
+ }
- } elseif (check_browser_version("Opera")) {
+ } else if (check_browser_version("Opera")) {
$classes[] = 'opera';
}
View
23 lib/simpletest/testmoodlelib.php
@@ -71,6 +71,10 @@ class moodlelib_test extends UnitTestCase {
'528' => array('iPhone' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; cs-cz) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16'),
'533' => array('iPad' => 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5'),
),
+ 'WebKit Android' => array(
+ '525' => array('G1 Phone' => 'Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone'),
+ '530' => array('Nexus' => 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus'),
+ ),
'Chrome' => array(
'8' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10'),
),
@@ -199,21 +203,31 @@ function test_check_browser_version()
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
$this->assertTrue(check_browser_version('Safari'));
+ $this->assertTrue(check_browser_version('WebKit'));
$this->assertTrue(check_browser_version('Safari', '312'));
$this->assertFalse(check_browser_version('Safari', '500'));
$this->assertFalse(check_browser_version('Chrome'));
$this->assertFalse(check_browser_version('Safari iOS'));
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
$this->assertTrue(check_browser_version('Safari iOS'));
+ $this->assertTrue(check_browser_version('WebKit'));
$this->assertTrue(check_browser_version('Safari iOS', '527'));
$this->assertFalse(check_browser_version('Safari iOS', 590));
$this->assertFalse(check_browser_version('Safari', '312'));
$this->assertFalse(check_browser_version('Safari', '500'));
$this->assertFalse(check_browser_version('Chrome'));
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
+ $this->assertTrue(check_browser_version('WebKit'));
+ $this->assertTrue(check_browser_version('WebKit Android', '527'));
+ $this->assertFalse(check_browser_version('WebKit Android', 590));
+ $this->assertFalse(check_browser_version('Safari'));
+ $this->assertFalse(check_browser_version('Chrome'));
+
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
$this->assertTrue(check_browser_version('Chrome'));
+ $this->assertTrue(check_browser_version('WebKit'));
$this->assertTrue(check_browser_version('Chrome', 8));
$this->assertFalse(check_browser_version('Chrome', 10));
$this->assertFalse(check_browser_version('Safari', '1'));
@@ -254,6 +268,15 @@ function test_get_browser_version_classes() {
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
$this->assertEqual(array('safari'), get_browser_version_classes());
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
+ $this->assertEqual(array('safari', 'ios'), get_browser_version_classes());
+
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
+ $this->assertEqual(array('safari', 'android'), get_browser_version_classes());
+
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
+ $this->assertEqual(array('safari'), get_browser_version_classes());
+
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
$this->assertEqual(array('opera'), get_browser_version_classes());

0 comments on commit 2168425

Please sign in to comment.