Merge branch 'MDL-57809-34-progressbar' of https://github.com/roperto/moodle
[moodle.git] / theme / styles.php
CommitLineData
b7009474 1<?php
b7009474 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/**
78946b9b 18 * This file is responsible for serving the one huge CSS of each theme.
b7009474 19 *
574909ef 20 * @package core
78946b9b 21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
b7009474 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
98245a84 25// Disable moodle specific debug messages and any errors in output,
c198390a
PS
26// comment out when debugging or better look into error log!
27define('NO_DEBUG_DISPLAY', true);
28
78946b9b 29define('ABORT_AFTER_CONFIG', true);
98245a84 30require('../config.php');
0e641c74 31require_once($CFG->dirroot.'/lib/csslib.php');
b7009474 32
7070b7f2
PS
33if ($slashargument = min_get_slash_argument()) {
34 $slashargument = ltrim($slashargument, '/');
35 if (substr_count($slashargument, '/') < 2) {
e1075027 36 css_send_css_not_found();
7070b7f2 37 }
383b89a1
SH
38
39 if (strpos($slashargument, '_s/') === 0) {
98245a84 40 // Can't use SVG.
383b89a1
SH
41 $slashargument = substr($slashargument, 3);
42 $usesvg = false;
43 } else {
44 $usesvg = true;
45 }
46
e1075027
SH
47 $chunk = null;
48 if (preg_match('#/(chunk(\d+)(/|$))#', $slashargument, $matches)) {
49 $chunk = (int)$matches[2];
50 $slashargument = str_replace($matches[1], '', $slashargument);
51 }
52
7070b7f2
PS
53 list($themename, $rev, $type) = explode('/', $slashargument, 3);
54 $themename = min_clean_param($themename, 'SAFEDIR');
5dd40ec4 55 $rev = min_clean_param($rev, 'RAW');
7070b7f2
PS
56 $type = min_clean_param($type, 'SAFEDIR');
57
58} else {
59 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
5dd40ec4 60 $rev = min_optional_param('rev', 0, 'RAW');
7070b7f2 61 $type = min_optional_param('type', 'all', 'SAFEDIR');
e1075027 62 $chunk = min_optional_param('chunk', null, 'INT');
383b89a1 63 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
7070b7f2 64}
b7009474 65
5dd40ec4
RW
66// Check if we received a theme sub revision which allows us
67// to handle local caching on a per theme basis.
68$values = explode('_', $rev);
69$rev = min_clean_param(array_shift($values), 'INT');
70$themesubrev = array_shift($values);
71
72if (is_null($themesubrev)) {
73 // Default to the current theme subrevision if one isn't
74 // provided in the URL.
75 $themesubrev = theme_get_sub_revision_for_theme($themename);
76} else {
77 $themesubrev = min_clean_param($themesubrev, 'INT');
78}
79
1eb2b517
SH
80if ($type === 'editor') {
81 // The editor CSS is never chunked.
82 $chunk = null;
1d987cae 83} else if ($type === 'all' || $type === 'all-rtl') {
1eb2b517
SH
84 // We're fine.
85} else {
ef201c96 86 css_send_css_not_found();
b7009474 87}
88
73e504bc 89if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
98245a84 90 // The theme exists in standard location - ok.
73e504bc 91} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
98245a84 92 // Alternative theme location contains this theme - ok.
73e504bc 93} else {
78946b9b
PS
94 header('HTTP/1.0 404 not found');
95 die('Theme was not found, sorry.');
96}
b7009474 97
cb8108dd 98$candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
5dd40ec4
RW
99$etag = "$rev/$themename/$type/$themesubrev";
100$candidatename = ($themesubrev > 0) ? "{$type}_{$themesubrev}" : $type;
383b89a1
SH
101if (!$usesvg) {
102 // Add to the sheet name, one day we'll be able to just drop this.
103 $candidatedir .= '/nosvg';
104 $etag .= '/nosvg';
105}
1eb2b517 106
e1075027
SH
107if ($chunk !== null) {
108 $etag .= '/chunk'.$chunk;
109 $candidatename .= '.'.$chunk;
110}
111$candidatesheet = "$candidatedir/$candidatename.css";
383b89a1 112$etag = sha1($etag);
b7009474 113
78946b9b 114if (file_exists($candidatesheet)) {
aa603b14 115 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
98245a84
PS
116 // We do not actually need to verify the etag value because our files
117 // never change in cache because we increment the rev counter.
869b1255 118 css_send_unmodified(filemtime($candidatesheet), $etag);
78946b9b 119 }
2c61e7cc 120 css_send_cached_css($candidatesheet, $etag);
b7009474 121}
122
98245a84 123// Ok, now we need to start normal moodle script, we need to load all libs and $DB.
78946b9b 124define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 125
98245a84
PS
126define('NO_MOODLE_COOKIES', true); // Session not used here.
127define('NO_UPGRADE_CHECK', true); // Ignore upgrade check.
fdeb7fa1 128
78946b9b 129require("$CFG->dirroot/lib/setup.php");
b7009474 130
78946b9b 131$theme = theme_config::load($themename);
383b89a1 132$theme->force_svg_use($usesvg);
1d987cae 133$theme->set_rtl_mode($type === 'all-rtl' ? true : false);
fdeb7fa1 134
ef201c96 135$themerev = theme_get_revision();
5dd40ec4 136$currentthemesubrev = theme_get_sub_revision_for_theme($themename);
383b89a1 137
ef201c96 138$cache = true;
5dd40ec4
RW
139// If the client is requesting a revision that doesn't match both
140// the global theme revision and the theme specific revision then
141// tell the browser not to cache this style sheet because it's
142// likely being regnerated.
143if ($themerev <= 0 or $themerev != $rev or $themesubrev != $currentthemesubrev) {
ef201c96
PS
144 $rev = $themerev;
145 $cache = false;
146
98245a84 147 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
5dd40ec4
RW
148 $etag = "$rev/$themename/$type/$themesubrev";
149 $candidatename = ($themesubrev > 0) ? "{$type}_{$themesubrev}" : $type;
ef201c96
PS
150 if (!$usesvg) {
151 // Add to the sheet name, one day we'll be able to just drop this.
152 $candidatedir .= '/nosvg';
153 $etag .= '/nosvg';
154 }
155
156 if ($chunk !== null) {
157 $etag .= '/chunk'.$chunk;
158 $candidatename .= '.'.$chunk;
159 }
160 $candidatesheet = "$candidatedir/$candidatename.css";
161 $etag = sha1($etag);
383b89a1 162}
ef201c96 163
cb8108dd 164make_localcache_directory('theme', false);
2c61e7cc 165
78946b9b 166if ($type === 'editor') {
98245a84
PS
167 $csscontent = $theme->get_css_content_editor();
168 css_store_css($theme, "$candidatedir/editor.css", $csscontent, false);
ef201c96 169
78946b9b 170} else {
d0f6660b
AN
171 // Fetch a lock whilst the CSS is fetched as this can be slow and CPU intensive.
172 // Each client should wait for one to finish the compilation before starting the compiler.
173 $lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content');
174 $lock = $lockfactory->get_lock($themename, rand(90, 120));
175
176 if (file_exists($candidatesheet)) {
177 // The file was built while we waited for the lock, we release the lock and serve the file.
178 if ($lock) {
179 $lock->release();
180 }
d433cf37 181
d0f6660b 182 if ($cache) {
d433cf37 183 css_send_cached_css($candidatesheet, $etag);
d0f6660b
AN
184 } else {
185 css_send_uncached_css(file_get_contents($candidatesheet));
d433cf37
FM
186 }
187 }
188
d0f6660b
AN
189 // The lock is still held, and the sheet still does not exist.
190 // Compile the CSS content.
4cc2f33b
SL
191 if (!$csscontent = $theme->get_css_cached_content()) {
192 $csscontent = $theme->get_css_content();
193 $theme->set_css_content_cache($csscontent);
194 }
98245a84 195
e1075027 196 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
1eb2b517
SH
197 if (!empty($slashargument)) {
198 if ($usesvg) {
1d987cae 199 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/$type";
e1075027 200 } else {
1d987cae 201 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/$type";
1eb2b517
SH
202 }
203 } else {
204 if ($usesvg) {
1d987cae 205 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type";
1eb2b517 206 } else {
1d987cae 207 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type&svg=0";
e1075027 208 }
1eb2b517 209 }
d433cf37 210
5dd40ec4 211 css_store_css($theme, "$candidatedir/$candidatename.css", $csscontent, true, $chunkurl);
d433cf37 212
d433cf37 213 if ($lock) {
d0f6660b
AN
214 // Now that the CSS has been generated and/or stored, release the lock.
215 // This will allow waiting clients to use the newly generated and stored CSS.
d433cf37
FM
216 $lock->release();
217 }
045f492c 218}
979d3207 219
98245a84
PS
220if (!$cache) {
221 // Do not pollute browser caches if invalid revision requested,
222 // let's ignore legacy IE breakage here too.
223 css_send_uncached_css($csscontent);
ef201c96 224
034dbd75 225} else if ($chunk !== null and file_exists($candidatesheet)) {
98245a84
PS
226 // Greetings stupid legacy IEs!
227 css_send_cached_css($candidatesheet, $etag);
979d3207 228
ef201c96 229} else {
98245a84
PS
230 // Real browsers - this is the expected result!
231 css_send_cached_css_content($csscontent, $etag);
ef201c96 232}