weekly release 3.4dev
[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');
55 $rev = min_clean_param($rev, 'INT');
56 $type = min_clean_param($type, 'SAFEDIR');
57
58} else {
59 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
60 $rev = min_optional_param('rev', 0, 'INT');
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
1eb2b517
SH
66if ($type === 'editor') {
67 // The editor CSS is never chunked.
68 $chunk = null;
1d987cae 69} else if ($type === 'all' || $type === 'all-rtl') {
1eb2b517
SH
70 // We're fine.
71} else {
ef201c96 72 css_send_css_not_found();
b7009474 73}
74
73e504bc 75if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
98245a84 76 // The theme exists in standard location - ok.
73e504bc 77} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
98245a84 78 // Alternative theme location contains this theme - ok.
73e504bc 79} else {
78946b9b
PS
80 header('HTTP/1.0 404 not found');
81 die('Theme was not found, sorry.');
82}
b7009474 83
cb8108dd 84$candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
ef201c96 85$etag = "$rev/$themename/$type";
e1075027 86$candidatename = $type;
383b89a1
SH
87if (!$usesvg) {
88 // Add to the sheet name, one day we'll be able to just drop this.
89 $candidatedir .= '/nosvg';
90 $etag .= '/nosvg';
91}
1eb2b517 92
e1075027
SH
93if ($chunk !== null) {
94 $etag .= '/chunk'.$chunk;
95 $candidatename .= '.'.$chunk;
96}
97$candidatesheet = "$candidatedir/$candidatename.css";
383b89a1 98$etag = sha1($etag);
b7009474 99
78946b9b 100if (file_exists($candidatesheet)) {
aa603b14 101 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
98245a84
PS
102 // We do not actually need to verify the etag value because our files
103 // never change in cache because we increment the rev counter.
869b1255 104 css_send_unmodified(filemtime($candidatesheet), $etag);
78946b9b 105 }
2c61e7cc 106 css_send_cached_css($candidatesheet, $etag);
b7009474 107}
108
98245a84 109// Ok, now we need to start normal moodle script, we need to load all libs and $DB.
78946b9b 110define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 111
98245a84
PS
112define('NO_MOODLE_COOKIES', true); // Session not used here.
113define('NO_UPGRADE_CHECK', true); // Ignore upgrade check.
fdeb7fa1 114
78946b9b 115require("$CFG->dirroot/lib/setup.php");
b7009474 116
78946b9b 117$theme = theme_config::load($themename);
383b89a1 118$theme->force_svg_use($usesvg);
1d987cae 119$theme->set_rtl_mode($type === 'all-rtl' ? true : false);
fdeb7fa1 120
ef201c96 121$themerev = theme_get_revision();
383b89a1 122
ef201c96
PS
123$cache = true;
124if ($themerev <= 0 or $themerev != $rev) {
125 $rev = $themerev;
126 $cache = false;
127
98245a84 128 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
ef201c96
PS
129 $etag = "$rev/$themename/$type";
130 $candidatename = $type;
131 if (!$usesvg) {
132 // Add to the sheet name, one day we'll be able to just drop this.
133 $candidatedir .= '/nosvg';
134 $etag .= '/nosvg';
135 }
136
137 if ($chunk !== null) {
138 $etag .= '/chunk'.$chunk;
139 $candidatename .= '.'.$chunk;
140 }
141 $candidatesheet = "$candidatedir/$candidatename.css";
142 $etag = sha1($etag);
383b89a1 143}
ef201c96 144
cb8108dd 145make_localcache_directory('theme', false);
2c61e7cc 146
78946b9b 147if ($type === 'editor') {
98245a84
PS
148 $csscontent = $theme->get_css_content_editor();
149 css_store_css($theme, "$candidatedir/editor.css", $csscontent, false);
ef201c96 150
78946b9b 151} else {
d433cf37
FM
152
153 $lock = null;
65b8336e 154 // Lock system to prevent concurrent requests to compile LESS/SCSS, which is really slow and CPU intensive.
d433cf37
FM
155 // Each client should wait for one to finish the compilation before starting a new compiling process.
156 // We only do this when the file will be cached...
65b8336e 157 if (in_array($type, ['less', 'scss']) && $cache) {
d433cf37
FM
158 $lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content');
159 // We wait for the lock to be acquired, the timeout does not need to be strict here.
160 $lock = $lockfactory->get_lock($themename, rand(15, 30));
161 if (file_exists($candidatesheet)) {
162 // The file was built while we waited for the lock, we release the lock and serve the file.
163 if ($lock) {
164 $lock->release();
165 }
166 css_send_cached_css($candidatesheet, $etag);
167 }
168 }
169
ef201c96 170 // Older IEs require smaller chunks.
98245a84
PS
171 $csscontent = $theme->get_css_content();
172
e1075027 173 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
1eb2b517
SH
174 if (!empty($slashargument)) {
175 if ($usesvg) {
1d987cae 176 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/$type";
e1075027 177 } else {
1d987cae 178 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/$type";
1eb2b517
SH
179 }
180 } else {
181 if ($usesvg) {
1d987cae 182 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type";
1eb2b517 183 } else {
1d987cae 184 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type&svg=0";
e1075027 185 }
1eb2b517 186 }
d433cf37 187
1d987cae 188 css_store_css($theme, "$candidatedir/$type.css", $csscontent, true, $chunkurl);
d433cf37
FM
189
190 // Release the lock.
191 if ($lock) {
192 $lock->release();
193 }
045f492c 194}
979d3207 195
98245a84
PS
196if (!$cache) {
197 // Do not pollute browser caches if invalid revision requested,
198 // let's ignore legacy IE breakage here too.
199 css_send_uncached_css($csscontent);
ef201c96 200
034dbd75 201} else if ($chunk !== null and file_exists($candidatesheet)) {
98245a84
PS
202 // Greetings stupid legacy IEs!
203 css_send_cached_css($candidatesheet, $etag);
979d3207 204
ef201c96 205} else {
98245a84
PS
206 // Real browsers - this is the expected result!
207 css_send_cached_css_content($csscontent, $etag);
ef201c96 208}