Merge branch 'MDL-41891_master' of https://github.com/nadavkav/moodle
[moodle.git] / theme / javascript.php
CommitLineData
38aafea2 1<?php
38aafea2
PS
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.
13//
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/>.
16
17/**
18 * This file is responsible for serving the one huge CSS of each theme.
19 *
8ab29743 20 * @package core
38aafea2
PS
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
e68c5f36 25
c198390a
PS
26// disable moodle specific debug messages and any errors in output,
27// comment out when debugging or better look into error log!
28define('NO_DEBUG_DISPLAY', true);
29
e68c5f36
PS
30// we need just the values from config.php and minlib.php
31define('ABORT_AFTER_CONFIG', true);
32require('../config.php'); // this stops immediately at the beginning of lib/setup.php
8ab29743 33require_once("$CFG->dirroot/lib/jslib.php");
e68c5f36 34
ecbad2ad
PS
35if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 2) {
38 image_not_found();
39 }
40 // image must be last because it may contain "/"
41 list($themename, $rev, $type) = explode('/', $slashargument, 3);
42 $themename = min_clean_param($themename, 'SAFEDIR');
43 $rev = min_clean_param($rev, 'INT');
44 $type = min_clean_param($type, 'SAFEDIR');
45
46} else {
47 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
0238ca0d 48 $rev = min_optional_param('rev', -1, 'INT');
ecbad2ad
PS
49 $type = min_optional_param('type', 'head', 'RAW');
50}
04c01408 51
baeb20bb 52if ($type !== 'head' and $type !== 'footer') {
04c01408
PS
53 header('HTTP/1.0 404 not found');
54 die('Theme was not found, sorry.');
55}
56
73e504bc
PS
57if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
58 // exists
59} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
60 // exists
61} else {
e68c5f36
PS
62 header('HTTP/1.0 404 not found');
63 die('Theme was not found, sorry.');
64}
65
cb8108dd 66$candidate = "$CFG->localcachedir/theme/$rev/$themename/javascript_$type.js";
0238ca0d 67$etag = sha1("$rev/$themename/$type");
e68c5f36 68
0238ca0d 69if ($rev > 0 and file_exists($candidate)) {
aa603b14 70 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
e68c5f36
PS
71 // we do not actually need to verify the etag value because our files
72 // never change in cache because we increment the rev parameter
8ab29743 73 js_send_unmodified(filemtime($candidate), $etag);
e68c5f36 74 }
8ab29743 75 js_send_cached($candidate, $etag);
e68c5f36
PS
76}
77
78//=================================================================================
79// ok, now we need to start normal moodle script, we need to load all libs and $DB
80define('ABORT_AFTER_CONFIG_CANCEL', true);
81
82define('NO_MOODLE_COOKIES', true); // Session not used here
83define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
84
85require("$CFG->dirroot/lib/setup.php");
86
87$theme = theme_config::load($themename);
0238ca0d 88$themerev = theme_get_revision();
e68c5f36 89
0238ca0d
PS
90if ($themerev <= 0 or $rev != $themerev) {
91 // Do not send caching headers if they do not request current revision,
92 // we do not want to pollute browser caches with outdated JS.
93 js_send_uncached($theme->javascript_content($type));
94}
8475b970 95
cb8108dd 96make_localcache_directory('theme', false);
0238ca0d 97
6b32d6bc 98js_write_cache_file_content($candidate, core_minify::js_files($theme->javascript_files($type)));
0238ca0d
PS
99// Verify nothing failed in cache file creation.
100clearstatcache();
101if (file_exists($candidate)) {
102 js_send_cached($candidate, $etag);
045f492c 103}
979d3207
PS
104
105js_send_uncached($theme->javascript_content($type));