Skip to content
Browse files

MDL-25611 browser detection cleanup and improvements

  • Loading branch information...
1 parent 82721a2 commit 0667103514bbbea63d37ee31ea87514a6b5fc4b5 @skodak skodak committed
Showing with 153 additions and 50 deletions.
  1. +2 −1 grade/report/grader/lib.php
  2. +2 −1 lib/ajax/ajaxlib.php
  3. +10 −8 lib/editor/tinymce/lib.php
  4. +90 −36 lib/moodlelib.php
  5. +49 −4 lib/simpletest/testmoodlelib.php
View
3 grade/report/grader/lib.php
@@ -1531,7 +1531,8 @@ public function is_fixed_students() {
check_browser_version('Gecko', '2006010100') ||
check_browser_version('Camino', '1.0') ||
check_browser_version('Opera', '6.0') ||
- check_browser_version('Safari', '2.0'));
+ check_browser_version('Chrome', '6') ||
+ check_browser_version('Safari', '300'));
}
/**
View
3 lib/ajax/ajaxlib.php
@@ -71,8 +71,9 @@ function ajaxenabled(array $browsers = null) {
$ff = check_browser_version('Gecko', 20051106);
$op = check_browser_version('Opera', 9.0);
$sa = check_browser_version('Safari', 412);
+ $ch = check_browser_version('Chrome', 6);
- if (!$ie && !$ff && !$op && !$sa) {
+ if (!$ie && !$ff && !$op && !$sa && !$ch) {
/** @see http://en.wikipedia.org/wiki/User_agent */
// Gecko build 20051107 is what is in Firefox 1.5.
// We still have issues with AJAX in other browsers.
View
18 lib/editor/tinymce/lib.php
@@ -31,17 +31,19 @@ class tinymce_texteditor extends texteditor {
public $version = '3.3.9.2';
public function supported_by_browser() {
- if (check_browser_version('MSIE', 5.5)) {
+ if (check_browser_version('MSIE', 6)) {
return true;
- } else if (check_browser_version('Gecko', 20030516)) {
+ }
+ if (check_browser_version('Gecko', 20030516)) {
return true;
- } else if (check_browser_version('Safari iOS')) {
- return false;
- } else if (check_browser_version('Android WebKit')) {
- return false;
- } else if (check_browser_version('Safari', 3)) {
+ }
+ if (check_browser_version('Safari', 412)) {
return true;
- } else if (check_browser_version('Opera', 9)) {
+ }
+ if (check_browser_version('Chrome', 6)) {
+ return true;
+ }
+ if (check_browser_version('Opera', 9)) {
return true;
}
View
126 lib/moodlelib.php
@@ -7497,10 +7497,10 @@ function check_browser_operating_system($brand) {
*
* @uses $_SERVER
* @param string $brand The browser identifier being tested
- * @param int $version The version of the browser
+ * @param int $version The version of the browser, if not specified any version (except 5.5 for IE for BC reasons)
* @return bool true if the given version is below that of the detected browser
*/
- function check_browser_version($brand='MSIE', $version=5.5) {
+ function check_browser_version($brand, $version = null) {
if (empty($_SERVER['HTTP_USER_AGENT'])) {
return false;
}
@@ -7509,8 +7509,13 @@ function check_browser_version($brand='MSIE', $version=5.5) {
switch ($brand) {
- case 'Camino': /// Mozilla Firefox browsers
-
+ case 'Camino': /// OSX browser using Gecke engine
+ if (strpos($agent, 'Camino') === false) {
+ return false;
+ }
+ if (empty($version)) {
+ return true; // no version specified
+ }
if (preg_match("/Camino\/([0-9\.]+)/i", $agent, $match)) {
if (version_compare($match[1], $version) >= 0) {
return true;
@@ -7520,7 +7525,12 @@ function check_browser_version($brand='MSIE', $version=5.5) {
case 'Firefox': /// Mozilla Firefox browsers
-
+ if (strpos($agent, 'Iceweasel') === false and strpos($agent, 'Firefox') === false) {
+ return false;
+ }
+ if (empty($version)) {
+ return true; // no version specified
+ }
if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $agent, $match)) {
if (version_compare($match[2], $version) >= 0) {
return true;
@@ -7530,8 +7540,7 @@ function check_browser_version($brand='MSIE', $version=5.5) {
case 'Gecko': /// Gecko based browsers
-
- if (substr_count($agent, 'Camino')) {
+ if (empty($version) and substr_count($agent, 'Camino')) {
// MacOS X Camino support
$version = 20041110;
}
@@ -7547,25 +7556,30 @@ function check_browser_version($brand='MSIE', $version=5.5) {
case 'MSIE': /// Internet Explorer
-
- if (strpos($agent, 'Opera')) { // Reject Opera
+ if (strpos($agent, 'Opera') !== false) { // Reject Opera
return false;
}
- $string = explode(';', $agent);
- if (!isset($string[1])) {
- return false;
- }
- $string = explode(' ', trim($string[1]));
- if (!isset($string[0]) and !isset($string[1])) {
- return false;
+ // in case of IE we have to deal with BC of the version parameter
+ if (is_null($version)) {
+ $version = 5.5; // anything older is not considered a browser at all!
}
- if ($string[0] == $brand and (float)$string[1] >= $version ) {
- return true;
+
+ //see: http://www.useragentstring.com/pages/Internet%20Explorer/
+ if (preg_match("/MSIE ([0-9\.]+)/", $agent, $match)) {
+ if (version_compare($match[1], $version) >= 0) {
+ return true;
+ }
}
break;
- case 'Opera': /// Opera
+ case 'Opera': /// Opera
+ if (strpos($agent, 'Opera') === false) {
+ return false;
+ }
+ if (empty($version)) {
+ return true; // no version specified
+ }
if (preg_match("/Opera\/([0-9\.]+)/i", $agent, $match)) {
if (version_compare($match[1], $version) >= 0) {
return true;
@@ -7573,25 +7587,47 @@ function check_browser_version($brand='MSIE', $version=5.5) {
}
break;
- case 'Safari': /// Safari
- // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SimbianOS
+
+ case 'Safari': /// Desktop or laptop Apple Safari browser
+ if (strpos($agent, 'AppleWebKit') === false) {
+ return false;
+ }
+ // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SimbianOS and any other mobile devices
if (strpos($agent, 'OmniWeb')) { // Reject OmniWeb
return false;
- } elseif (strpos($agent, 'Shiira')) { // Reject Shiira
+ }
+ if (strpos($agent, 'Shiira')) { // Reject Shiira
+ return false;
+ }
+ if (strpos($agent, 'SimbianOS')) { // Reject SimbianOS
return false;
- } elseif (strpos($agent, 'SimbianOS')) { // Reject SimbianOS
+ }
+ 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;
+ }
+ if (strpos($agent, 'Chrome')) { // Reject chrome browsers - it needs to be tested explicitly
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 'Chrome':
+ if (strpos($agent, 'Chrome') === false) {
+ return false;
+ }
+ if (empty($version)) {
+ return true; // no version specified
+ }
if (preg_match("/Chrome\/(.*)[ ]+/i", $agent, $match)) {
if (version_compare($match[1], $version) >= 0) {
return true;
@@ -7599,22 +7635,34 @@ function check_browser_version($brand='MSIE', $version=5.5) {
}
break;
+
case 'Safari iOS': /// Safari on iPhone and iPad
- if (strpos($agent, 'iPhone')) {
- return true;
+ if (strpos($agent, 'AppleWebKit') === false or strpos($agent, 'Safari') === false) {
+ return false;
+ }
+ if (!strpos($agent, 'iPhone') and !strpos($agent, 'iPad') and !strpos($agent, 'iPod')) {
+ return false;
}
- if (strpos($agent, 'iPad')) {
- return true;
+ if (empty($version)) {
+ return true; // no version specified
}
- if (strpos($agent, 'iPod')) {
- return true;
+ if (preg_match("/AppleWebKit\/([0-9]+)/i", $agent, $match)) {
+ if (version_compare($match[1], $version) >= 0) {
+ return true;
+ }
}
break;
+
case 'Android WebKit': /// WebKit browser on Android
- if (strpos($agent, 'Linux; U; Android')) {
- return true;
+ if (strpos($agent, 'Linux; U; Android') === false) {
+ return false;
}
+ if (empty($version)) {
+ return true; // no version specified
+ }
+ //TODO: add version check here
+ return true;
break;
}
@@ -7633,7 +7681,9 @@ function get_browser_version_classes() {
if (check_browser_version("MSIE", "0")) {
$classes[] = 'ie';
- if (check_browser_version("MSIE", 8)) {
+ if (check_browser_version("MSIE", 9)) {
+ $classes[] = 'ie9';
+ } else if (check_browser_version("MSIE", 8)) {
$classes[] = 'ie8';
} elseif (check_browser_version("MSIE", 7)) {
$classes[] = 'ie7';
@@ -7641,16 +7691,20 @@ function get_browser_version_classes() {
$classes[] = 'ie6';
}
- } elseif (check_browser_version("Firefox", 0) || check_browser_version("Gecko", 0) || check_browser_version("Camino", 0)) {
+ } elseif (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", 0)) {
+ } elseif (check_browser_version("Safari") || check_browser_version("Chrome")) {
+ $classes[] = 'safari';
+
+ } else if (check_browser_version("Safari iOS")) {
$classes[] = 'safari';
+ $classes[] = 'ios';
- } elseif (check_browser_version("Opera", 0)) {
+ } elseif (check_browser_version("Opera")) {
$classes[] = 'opera';
}
View
53 lib/simpletest/testmoodlelib.php
@@ -47,10 +47,12 @@ class moodlelib_test extends UnitTestCase {
var $user_agents = array(
'MSIE' => array(
+ '5.0' => array('Windows 98' => 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'),
'5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
'6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
'7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'),
'8.0' => array('Windows Vista' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)'),
+ '9.0' => array('Windows 7' => 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))'),
),
'Firefox' => array(
@@ -63,7 +65,14 @@ class moodlelib_test extends UnitTestCase {
),
'Safari' => array(
'312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
- '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
+ '412' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
+ ),
+ 'Safari iOS' => array(
+ '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'),
+ ),
+ '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'),
),
'Opera' => array(
'8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
@@ -188,25 +197,61 @@ function test_check_browser_version()
{
global $CFG;
- $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
+ $this->assertTrue(check_browser_version('Safari'));
$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('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['Chrome']['8']['Mac OS X'];
+ $this->assertTrue(check_browser_version('Chrome'));
+ $this->assertTrue(check_browser_version('Chrome', 8));
+ $this->assertFalse(check_browser_version('Chrome', 10));
+ $this->assertFalse(check_browser_version('Safari', '1'));
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
+ $this->assertTrue(check_browser_version('Opera'));
$this->assertTrue(check_browser_version('Opera', '8.0'));
$this->assertFalse(check_browser_version('Opera', '10.0'));
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
+ $this->assertTrue(check_browser_version('MSIE'));
$this->assertTrue(check_browser_version('MSIE', '5.0'));
$this->assertFalse(check_browser_version('MSIE', '7.0'));
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98'];
+ $this->assertFalse(check_browser_version('MSIE'));
+ $this->assertTrue(check_browser_version('MSIE', 0));
+ $this->assertTrue(check_browser_version('MSIE', '5.0'));
+ $this->assertFalse(check_browser_version('MSIE', '7.0'));
+
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
+ $this->assertTrue(check_browser_version('MSIE'));
+ $this->assertTrue(check_browser_version('MSIE', 0));
+ $this->assertTrue(check_browser_version('MSIE', '5.0'));
+ $this->assertTrue(check_browser_version('MSIE', '9.0'));
+ $this->assertFalse(check_browser_version('MSIE', '10'));
+
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
+ $this->assertTrue(check_browser_version('Firefox'));
$this->assertTrue(check_browser_version('Firefox', '1.5'));
$this->assertFalse(check_browser_version('Firefox', '3.0'));
}
function test_get_browser_version_classes() {
- $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
+ $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
+ $this->assertEqual(array('safari'), 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'];
@@ -451,7 +496,7 @@ function test_usergetdate() {
// The string version of date comes from server locale setting and does
// not respect user language, so it is necessary to reset that.
$oldlocale = setlocale(LC_TIME, '0');
- setlocale(LC_TIME, 'en_AU.UTF-8');
+ setlocale(LC_TIME, 'en_AU.UTF-8');
$ts = 1261540267; //the time this function was created

0 comments on commit 0667103

Please sign in to comment.
Something went wrong with that request. Please try again.