diff --git a/public/admin/tool/mobile/referer.php b/public/admin/tool/mobile/referer.php new file mode 100644 index 00000000000..c5e5965c503 --- /dev/null +++ b/public/admin/tool/mobile/referer.php @@ -0,0 +1,56 @@ +. + +/** + * Script to allow set the Moodle LMS site referer header when embedding remote content on the app. + * + * @package tool_mobile + * @copyright 2025 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('NO_MOODLE_COOKIES', true); +require(__DIR__ . '/../../../config.php'); + +// This script is only for the Moodle app, when referer protected remote content is being embedded. +// This is a security measure as well because the user agent cannot be tampered via XSS attacks. +if (!\core_useragent::is_moodle_app()) { + throw new moodle_exception('apprequired', 'tool_mobile'); +} + +$url = required_param('url', PARAM_URL); +$delay = optional_param('delay', 500, PARAM_INT); +$debug = optional_param('debug', false, PARAM_BOOL); + +// Check if the URL to redirect is valid and not a local URL. +if (empty($url) || !empty(clean_param($url, PARAM_LOCALURL))) { + throw new moodle_exception('invalidurl'); +} + +// Delay has to be positive number and max of 5 seconds, +// enough to see the debugging info at least when required. +$delay = max(0, min($delay, 5000)); + +$data = [ + 'lang' => current_language(), + 'url' => $url, + 'delay' => $delay, + 'debug' => $debug, + 'useragent' => $_SERVER['HTTP_USER_AGENT'] ?? '', + 'referer' => $_SERVER['HTTP_REFERER'] ?? '', +]; + +echo $OUTPUT->render_from_template('tool_mobile/referer', $data); diff --git a/public/admin/tool/mobile/templates/referer.mustache b/public/admin/tool/mobile/templates/referer.mustache new file mode 100644 index 00000000000..f049307f16d --- /dev/null +++ b/public/admin/tool/mobile/templates/referer.mustache @@ -0,0 +1,67 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template tool_mobile/referer + + This template will render the code to + redirect to the URL setting the site referer. + + Variables required for this template: + * lang: The language of the user. + * url: The URL to redirect to. + * delay: The delay in milliseconds before the redirection happens. + * debug: Boolean to indicate if debug info should be shown. + * useragent: The user agent of the client, displayed when debug enabled. + * referer: The referer header of the client, displayed when debug enabled. + + Example context (json): + { + "lang": "en", + "url": "https://example.com", + "delay": 500, + "debug": true, + "useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", + "referer": "https://moodle.example.com" + } + +}} + + + + {{#str}} redirect, core {{/str}} + + + + + {{#debug}} +
+

Debug Information

+

Your User Agent is: {{useragent}}

+

Your referer is: {{referer}}

+
+ {{/debug}} + +

{{#str}} redirect, core {{/str}}

+ + + + +