Merge branch 'MDL-64281-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 20 Mar 2019 23:27:27 +0000 (00:27 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 20 Mar 2019 23:27:27 +0000 (00:27 +0100)
lang/en/admin.php
lib/classes/useragent.php
lib/tests/useragent_test.php
lib/weblib.php

index 779a889..5e21e57 100644 (file)
@@ -61,7 +61,7 @@ $string['allowindexingexceptlogin'] = 'Everywhere except login and signup pages'
 $string['allowindexingnowhere'] = 'Nowhere';
 $string['allowusermailcharset'] = 'Allow user to select character set';
 $string['allowframembedding'] = 'Allow frame embedding';
-$string['allowframembedding_help'] = 'If enabled, this site may be embedded in a frame in a remote system, as recommended when using the \'Publish as LTI tool\' enrolment plugin. Otherwise, it is recommended to leave frame embedding disabled for security reasons.';
+$string['allowframembedding_help'] = 'If enabled, this site may be embedded in a frame in a remote system, as recommended when using the \'Publish as LTI tool\' enrolment plugin. Otherwise, it is recommended to leave frame embedding disabled for security reasons.<br />Please, note also that for the mobile app this setting is ignored and frame embedding is always allowed.';
 $string['allowguestmymoodle'] = 'Allow guest access to Dashboard';
 $string['allowobjectembed'] = 'Allow EMBED and OBJECT tags';
 $string['allowthemechangeonurl'] = 'Allow theme changes in the URL';
index 2e54708..763543b 100644 (file)
@@ -1068,6 +1068,23 @@ class core_useragent {
         return false;
     }
 
+    /**
+     * Returns true if the client appears to be the Moodle app (or an app based on the Moodle app code).
+     *
+     * @return bool true if the client is the Moodle app
+     * @since Moodle 3.7
+     */
+    public static function is_moodle_app() {
+        $useragent = self::get_user_agent_string();
+
+        // Make it case insensitive, things can change in the app or desktop app depending on the platform frameworks.
+        if (stripos($useragent, 'MoodleMobile') !== false) {
+            return true;
+        }
+
+        return false;
+    }
+
     /**
      * Checks if current browser supports files with give extension as <video> or <audio> source
      *
index 8c0ad12..3342e35 100644 (file)
@@ -1386,6 +1386,141 @@ class core_useragent_testcase extends advanced_testcase {
                     ),
                ),
             ),
+
+            // Macos Desktop app.
+            array(
+                'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) moodlemobile/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_webkit'                    => true,
+                    'is_chrome'                    => true,
+                    'check_chrome_version'         => array(
+                        '7'                        => true,
+                        '8'                        => true,
+                        '10'                       => true,
+                        '39'                       => true,
+                    ),
+                    'versionclasses'               => array(
+                        'chrome',
+                    ),
+               ),
+            ),
+
+            // Linux Desktop app.
+            array(
+                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) moodledesktop/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_webkit'                    => true,
+                    'is_chrome'                    => true,
+                    'check_chrome_version'         => array(
+                        '7'                        => true,
+                        '8'                        => true,
+                        '10'                       => true,
+                        '39'                       => true,
+                    ),
+                    'versionclasses'               => array(
+                        'chrome',
+                    ),
+               ),
+            ),
+
+            // Windows Desktop app.
+            array(
+                'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) moodledesktop/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_webkit'                    => true,
+                    'is_chrome'                    => true,
+                    'check_chrome_version'         => array(
+                        '7'                        => true,
+                        '8'                        => true,
+                        '10'                       => true,
+                        '39'                       => true,
+                    ),
+                    'versionclasses'               => array(
+                        'chrome',
+                    ),
+               ),
+            ),
+
+            // Android app.
+            array(
+                'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_webkit'                    => true,
+                    'is_webkit_android'            => true,
+                    'is_chrome'                    => true,
+                    'check_chrome_version'         => array(
+                        '7'                        => true,
+                        '8'                        => true,
+                        '10'                       => true,
+                        '39'                       => true,
+                    ),
+                    'devicetype'                   => 'mobile',
+                    'check_webkit_android_version' => array(
+                        '525'                       => true,
+                        '527'                       => true,
+                    ),
+                    'versionclasses'               => array(
+                        'android',
+                        'chrome'
+                    ),
+               ),
+            ),
+
+            // Android app, user agent lower case.
+            array(
+                'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 moodlemobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_webkit'                    => true,
+                    'is_webkit_android'            => true,
+                    'is_chrome'                    => true,
+                    'check_chrome_version'         => array(
+                        '7'                        => true,
+                        '8'                        => true,
+                        '10'                       => true,
+                        '39'                       => true,
+                    ),
+                    'devicetype'                   => 'mobile',
+                    'check_webkit_android_version' => array(
+                        '525'                       => true,
+                        '527'                       => true,
+                    ),
+                    'versionclasses'               => array(
+                        'android',
+                        'chrome'
+                    ),
+               ),
+            ),
+
+            // iOS (iPhone) app.
+            array(
+                'Mozilla/5.0 (iPhone; CPU OS 13_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_ios'                       => true,
+                    'is_webkit'                    => true,
+                    'devicetype'                   => 'mobile',
+                    'versionclasses'               => array(
+                    ),
+               ),
+            ),
+
+            // iOS (iPad) app.
+            array(
+                'Mozilla/5.0 (iPad; CPU OS 12_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D39 MoodleMobile',
+                array(
+                    'is_moodle_app'                => true,
+                    'is_ios'                       => true,
+                    'is_webkit'                    => true,
+                    'devicetype'                   => 'tablet',
+                    'versionclasses'               => array(
+                    ),
+               ),
+            ),
         );
     }
 
@@ -1710,7 +1845,10 @@ class core_useragent_testcase extends advanced_testcase {
         if (isset($tests['is_ios']) && $tests['is_ios']) {
             $this->assertTrue(core_useragent::is_ios(),
                 "Browser was not identified as an iOS device browser");
-            $this->assertTrue(core_useragent::check_safari_ios_version());
+            // The iOS app is not Safari based.
+            if (!isset($tests['is_moodle_app']) || !$tests['is_moodle_app']) {
+                $this->assertTrue(core_useragent::check_safari_ios_version());
+            }
         } else {
             $this->assertFalse(core_useragent::is_ios(),
                 "Browser was incorrectly identified as an iOS device browser");
@@ -1880,4 +2018,15 @@ class core_useragent_testcase extends advanced_testcase {
         $expectation = isset($tests['is_web_crawler']) ? $tests['is_web_crawler'] : false;
         $this->assertSame($expectation, core_useragent::is_web_crawler());
     }
+
+    /**
+     * @dataProvider user_agents_providers
+     */
+    public function test_useragent_moodle_app($useragent, $tests) {
+        // Setup the core_useragent instance.
+        core_useragent::instance(true, $useragent);
+
+        $expectation = isset($tests['is_moodle_app']) ? $tests['is_moodle_app'] : false;
+        $this->assertSame($expectation, core_useragent::is_moodle_app());
+    }
 }
index dac9deb..3199a1d 100644 (file)
@@ -2278,7 +2278,8 @@ function send_headers($contenttype, $cacheable = true) {
     }
     @header('Accept-Ranges: none');
 
-    if (empty($CFG->allowframembedding)) {
+    // The Moodle app must be allowed to embed content always.
+    if (empty($CFG->allowframembedding) && !core_useragent::is_moodle_app()) {
         @header('X-Frame-Options: sameorigin');
     }
 }