Merge branch 'MDL-81397' of https://github.com/paulholden/moodle
[moodle.git] / theme / javascript.php
blob203ca94c08be18e59eee5396ae33d4ff951d506b
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 responsible for serving the one huge CSS of each theme.
20 * @package core
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
33 require_once("$CFG->dirroot/lib/jslib.php");
35 if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 2) {
38 header('HTTP/1.0 404 not found');
39 die('Slash argument must contain both a revision and a file path');
41 // image must be last because it may contain "/"
42 list($themename, $rev, $type) = explode('/', $slashargument, 3);
43 $themename = min_clean_param($themename, 'SAFEDIR');
44 $rev = min_clean_param($rev, 'INT');
45 $type = min_clean_param($type, 'SAFEDIR');
47 } else {
48 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
49 $rev = min_optional_param('rev', -1, 'INT');
50 $type = min_optional_param('type', 'head', 'RAW');
53 if (!min_is_revision_valid_and_current($rev)) {
54 // If the rev is invalid, normalise it to -1 to disable all caching.
55 $rev = -1;
58 if ($type !== 'head' and $type !== 'footer') {
59 header('HTTP/1.0 404 not found');
60 die('Theme was not found, sorry.');
63 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
64 // exists
65 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
66 // exists
67 } else {
68 header('HTTP/1.0 404 not found');
69 die('Theme was not found, sorry.');
72 $candidate = "$CFG->localcachedir/theme/$rev/$themename/javascript_$type.js";
73 $etag = sha1("$rev/$themename/$type");
75 if ($rev > 0 and file_exists($candidate)) {
76 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
77 // we do not actually need to verify the etag value because our files
78 // never change in cache because we increment the rev parameter
79 js_send_unmodified(filemtime($candidate), $etag);
81 js_send_cached($candidate, $etag);
84 //=================================================================================
85 // ok, now we need to start normal moodle script, we need to load all libs and $DB
86 define('ABORT_AFTER_CONFIG_CANCEL', true);
88 define('NO_MOODLE_COOKIES', true); // Session not used here
89 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
91 require("$CFG->dirroot/lib/setup.php");
93 $theme = theme_config::load($themename);
94 $themerev = theme_get_revision();
96 if ($themerev <= 0 or $rev != $themerev) {
97 // Do not send caching headers if they do not request current revision,
98 // we do not want to pollute browser caches with outdated JS.
99 js_send_uncached($theme->javascript_content($type));
102 make_localcache_directory('theme', false);
104 js_write_cache_file_content($candidate, core_minify::js_files($theme->javascript_files($type)));
105 // Verify nothing failed in cache file creation.
106 clearstatcache();
107 if (file_exists($candidate)) {
108 js_send_cached($candidate, $etag);
111 js_send_uncached($theme->javascript_content($type));