Merge branch 'MDL-73142-311' of https://github.com/susyzan/moodle into MOODLE_311_STABLE
[moodle.git] / lib / jssourcemap.php
bloba39a451b64cc364285fb002e01b41a260dcf4fa6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is serving the javascript source map.
20 * @package core
21 * @copyright 2019 Ryan Wyllie
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 // Disable moodle specific debug messages and any errors in output,
26 // comment out when debugging or better look into error log!
27 define('NO_DEBUG_DISPLAY', true);
29 require('../config.php');
30 require_once("$CFG->dirroot/lib/configonlylib.php");
31 require_once("$CFG->dirroot/lib/classes/requirejs.php");
33 $slashargument = min_get_slash_argument();
34 if (!$slashargument) {
35 // The above call to min_get_slash_argument should always work.
36 die('Invalid request');
39 $slashargument = ltrim($slashargument, '/');
40 // Split into revision and module name.
41 [$file] = explode('/', $slashargument, 1);
42 $file = '/' . min_clean_param($file, 'SAFEPATH');
44 // Only load js files from the js modules folder from the components.
45 [$unused, $component, $module] = explode('/', $file, 3);
47 // When running a lazy load, we only deal with one file so we can just return the working sourcemap.
48 $jsfiles = core_requirejs::find_one_amd_module($component, $module, false);
49 $jsfile = reset($jsfiles);
51 $mapfile = $jsfile . '.map';
53 if (file_exists($mapfile)) {
54 $mapdata = file_get_contents($mapfile);
55 $mapdata = json_decode($mapdata, true);
57 $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
58 $srcfilename = str_replace('/amd/build/', '/amd/src/', $shortfilename);
59 $srcfilename = str_replace('.min.js', '.js', $srcfilename);
60 $fullsrcfilename = $CFG->wwwroot . $srcfilename;
61 $mapdata['sources'][0] = $fullsrcfilename;
63 echo json_encode($mapdata);
64 } else {
65 // If there is no source map file, then we will not generate one for you, sorry.
66 header('HTTP/1.0 404 not found');