2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file is serving optimised JS for RequireJS.
21 * @copyright 2015 Damyon Wiese
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 // We need just the values from config.php and minlib.php.
30 define('ABORT_AFTER_CONFIG', true);
31 require('../config.php'); // This stops immediately at the beginning of lib/setup.php.
32 require_once("$CFG->dirroot/lib/jslib.php");
33 require_once("$CFG->dirroot/lib/classes/requirejs.php");
35 $slashargument = min_get_slash_argument();
36 if (!$slashargument) {
37 // The above call to min_get_slash_argument should always work.
38 die('Invalid request');
41 $slashargument = ltrim($slashargument, '/');
42 if (substr_count($slashargument, '/') < 1) {
43 header('HTTP/1.0 404 not found');
44 die('Slash argument must contain both a revision and a file path');
46 // Split into revision and module name.
47 list($rev, $file) = explode('/', $slashargument, 2);
48 $rev = min_clean_param($rev, 'INT');
49 $file = '/' . min_clean_param($file, 'SAFEPATH');
51 // Only load js files from the js modules folder from the components.
53 list($unused, $component, $module) = explode('/', $file, 3);
55 // No subdirs allowed - only flat module structure please.
56 if (strpos('/', $module) !== false) {
57 die('Invalid module');
60 // Some (huge) modules are better loaded lazily (when they are used). If we are requesting
61 // one of these modules, only return the one module, not the combo.
62 $lazysuffix = "-lazy.js";
63 $lazyload = (strpos($module, $lazysuffix) !== false);
66 // We are lazy loading a single file - so include the component/filename pair in the etag.
67 $etag = sha1($rev . '/' . $component . '/' . $module);
69 // We loading all (non-lazy) files - so only the rev makes this request unique.
74 // Use the caching only for meaningful revision numbers which prevents future cache poisoning.
75 if ($rev > 0 and $rev < (time() +
60 * 60)) {
76 $candidate = $CFG->localcachedir
. '/requirejs/' . $etag;
78 if (file_exists($candidate)) {
79 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
80 // We do not actually need to verify the etag value because our files
81 // never change in cache because we increment the rev parameter.
82 js_send_unmodified(filemtime($candidate), $etag);
84 js_send_cached($candidate, $etag, 'requirejs.php');
90 $jsfiles = core_requirejs
::find_one_amd_module($component, $module);
92 // Here we respond to the request by returning ALL amd modules. This saves
93 // round trips in production.
95 $jsfiles = core_requirejs
::find_all_amd_modules();
99 foreach ($jsfiles as $modulename => $jsfile) {
100 $js = file_get_contents($jsfile);
102 error_log('Failed to load JavaScript file ' . $jsfile);
103 $js = "/* Failed to load JavaScript file {$jsfile}. */\n";
104 $content = $js . $content;
108 // Inject the module name into the define.
109 $replace = 'define(\'' . $modulename . '\', ';
111 // Replace only the first occurrence.
112 $js = implode($replace, explode($search, $js, 2));
116 js_write_cache_file_content($candidate, $content);
117 // Verify nothing failed in cache file creation.
119 if (file_exists($candidate)) {
120 js_send_cached($candidate, $etag, 'requirejs.php');
127 $jsfiles = core_requirejs
::find_one_amd_module($component, $module, true);
129 $jsfiles = core_requirejs
::find_all_amd_modules(true);
133 foreach ($jsfiles as $modulename => $jsfile) {
134 $shortfilename = str_replace($CFG->dirroot
, '', $jsfile);
135 $js = "// ---- $shortfilename ----\n";
136 $js .= file_get_contents($jsfile) . "\n";
137 // Inject the module name into the define.
138 $replace = 'define(\'' . $modulename . '\', ';
141 if (strpos($js, $search) === false) {
142 // We can't call debugging because we only have minimal config loaded.
143 header('HTTP/1.0 500 error');
144 die('JS file: ' . $shortfilename . ' cannot be loaded, or does not contain a javascript module in AMD format. "define()" not found.');
147 // Replace only the first occurrence.
148 $js = implode($replace, explode($search, $js, 2));
151 js_send_uncached($content, 'requirejs.php');