MDL-32683 use slashargument urls for theme style sheets
[moodle.git] / theme / javascript.php
CommitLineData
38aafea2
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is responsible for serving the one huge CSS of each theme.
20 *
21 * @package moodlecore
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
e68c5f36 26
c198390a
PS
27// disable moodle specific debug messages and any errors in output,
28// comment out when debugging or better look into error log!
29define('NO_DEBUG_DISPLAY', true);
30
e68c5f36
PS
31// we need just the values from config.php and minlib.php
32define('ABORT_AFTER_CONFIG', true);
33require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34
35$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
36$rev = min_optional_param('rev', 0, 'INT');
baeb20bb 37$type = min_optional_param('type', 'head', 'RAW');
04c01408 38
baeb20bb 39if ($type !== 'head' and $type !== 'footer') {
04c01408
PS
40 header('HTTP/1.0 404 not found');
41 die('Theme was not found, sorry.');
42}
43
73e504bc
PS
44if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
45 // exists
46} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
47 // exists
48} else {
e68c5f36
PS
49 header('HTTP/1.0 404 not found');
50 die('Theme was not found, sorry.');
51}
52
365bec4c 53$candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js";
e68c5f36
PS
54
55if ($rev > -1 and file_exists($candidate)) {
aa603b14 56 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
e68c5f36
PS
57 // we do not actually need to verify the etag value because our files
58 // never change in cache because we increment the rev parameter
ccc0fff9 59 $lifetime = 60*60*24*30; // 30 days
aa603b14 60 header('HTTP/1.1 304 Not Modified');
ccc0fff9 61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 62 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9 63 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
64 die;
65 }
66 send_cached_js($candidate, $rev);
67}
68
69//=================================================================================
70// ok, now we need to start normal moodle script, we need to load all libs and $DB
71define('ABORT_AFTER_CONFIG_CANCEL', true);
72
73define('NO_MOODLE_COOKIES', true); // Session not used here
74define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
75
76require("$CFG->dirroot/lib/setup.php");
045f492c
SH
77// setup include path
78set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
79require_once('Minify.php');
e68c5f36
PS
80
81$theme = theme_config::load($themename);
82
e68c5f36 83if ($rev > -1) {
e164a0df
PS
84 // note: cache reset might have purged our cache dir structure,
85 // make sure we do not use stale file stat cache in the next check_dir_exists()
86 clearstatcache();
c426ef3a 87 check_dir_exists(dirname($candidate));
e68c5f36 88 $fp = fopen($candidate, 'w');
045f492c 89 fwrite($fp, minify($theme->javascript_files($type)));
e68c5f36
PS
90 fclose($fp);
91 send_cached_js($candidate);
e68c5f36 92} else {
045f492c 93 send_uncached_js($theme->javascript_content($type));
e68c5f36
PS
94}
95
96//=================================================================================
97//=== utility functions ==
98// we are not using filelib because we need to fine tune all header
99// parameters to get the best performance.
100
101function send_cached_js($jspath) {
7e9f1b63
PS
102 global $CFG;
103 require("$CFG->dirroot/lib/xsendfilelib.php");
104
ccc0fff9 105 $lifetime = 60*60*24*30; // 30 days
e68c5f36 106
baeb20bb 107 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
108 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
109 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
110 header('Pragma: ');
8c672cf9 111 header('Cache-Control: public, max-age='.$lifetime);
e68c5f36 112 header('Accept-Ranges: none');
8a7703ce 113 header('Content-Type: application/javascript; charset=utf-8');
7e9f1b63
PS
114
115 if (xsendfile($jspath)) {
116 die;
117 }
118
7c986f04
PS
119 if (!min_enable_zlib_compression()) {
120 header('Content-Length: '.filesize($jspath));
121 }
e68c5f36 122
e68c5f36
PS
123 readfile($jspath);
124 die;
125}
126
127function send_uncached_js($js) {
baeb20bb 128 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
129 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
130 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
131 header('Pragma: ');
132 header('Accept-Ranges: none');
8a7703ce 133 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
134 header('Content-Length: '.strlen($js));
135
e68c5f36
PS
136 echo $js;
137 die;
138}
045f492c
SH
139
140function minify($files) {
d1f582d9
PS
141 if (empty($files)) {
142 return '';
143 }
144
045f492c
SH
145 if (0 === stripos(PHP_OS, 'win')) {
146 Minify::setDocRoot(); // IIS may need help
147 }
e1c2a211
PS
148 // disable all caching, we do it in moodle
149 Minify::setCache(null, false);
045f492c
SH
150
151 $options = array(
152 'bubbleCssImports' => false,
153 // Don't gzip content we just want text for storage
154 'encodeOutput' => false,
155 // Maximum age to cache, not used but required
156 'maxAge' => 1800,
157 // The files to minify
158 'files' => $files,
159 // Turn orr URI rewriting
160 'rewriteCssUris' => false,
161 // This returns the CSS rather than echoing it for display
162 'quiet' => true
163 );
164
d1f582d9
PS
165 $error = 'unknown';
166 try {
167 $result = Minify::serve('Files', $options);
168 if ($result['success']) {
169 return $result['content'];
170 }
171 } catch (Exception $e) {
172 $error = $e->getMessage();
173 $error = str_replace("\r", ' ', $error);
174 $error = str_replace("\n", ' ', $error);
175 }
176
177 // minification failed - try to inform the theme developer and include the non-minified version
178 $js = <<<EOD
179try {console.log('Error: Minimisation of theme javascript failed!');} catch (e) {}
180
181// Error: $error
182// Problem detected during javascript minimisation, please review the following code
183// =================================================================================
184
185
186EOD;
187 foreach ($files as $jsfile) {
188 $js .= file_get_contents($jsfile)."\n";
189 }
190 return $js;
045f492c 191}