MDL-43648 form: fix hours and minutes in date_time_selector
[moodle.git] / theme / styles.php
blob4977095decde2ad2636b4212755a35de47c26401
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
25 // Disable moodle specific debug messages and any errors in output,
26 // comment out when debugging or better look into error log!
27 define('NO_DEBUG_DISPLAY', true);
29 define('ABORT_AFTER_CONFIG', true);
30 require('../config.php');
31 require_once($CFG->dirroot.'/lib/csslib.php');
33 if ($slashargument = min_get_slash_argument()) {
34 $slashargument = ltrim($slashargument, '/');
35 if (substr_count($slashargument, '/') < 2) {
36 css_send_css_not_found();
39 if (strpos($slashargument, '_s/') === 0) {
40 // Can't use SVG.
41 $slashargument = substr($slashargument, 3);
42 $usesvg = false;
43 } else {
44 $usesvg = true;
47 $chunk = null;
48 if (preg_match('#/(chunk(\d+)(/|$))#', $slashargument, $matches)) {
49 $chunk = (int)$matches[2];
50 $slashargument = str_replace($matches[1], '', $slashargument);
53 list($themename, $rev, $type) = explode('/', $slashargument, 3);
54 $themename = min_clean_param($themename, 'SAFEDIR');
55 $rev = min_clean_param($rev, 'RAW');
56 $type = min_clean_param($type, 'SAFEDIR');
58 } else {
59 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
60 $rev = min_optional_param('rev', 0, 'RAW');
61 $type = min_optional_param('type', 'all', 'SAFEDIR');
62 $chunk = min_optional_param('chunk', null, 'INT');
63 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
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);
72 if (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');
80 // Check that type fits into the expected values.
81 if ($type === 'editor') {
82 // The editor CSS is never chunked.
83 $chunk = null;
84 } else if ($type === 'all' || $type === 'all-rtl') {
85 // We're fine.
86 } else {
87 css_send_css_not_found();
90 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
91 // The theme exists in standard location - ok.
92 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
93 // Alternative theme location contains this theme - ok.
94 } else {
95 header('HTTP/1.0 404 not found');
96 die('Theme was not found, sorry.');
99 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
100 $candidatesheet = "{$candidatedir}/" . theme_styles_get_filename($type, $themesubrev, $usesvg);
101 $chunkedcandidatesheet = "{$candidatedir}/" . theme_styles_get_filename($type, $themesubrev, $usesvg, $chunk);
102 $etag = theme_styles_get_etag($themename, $rev, $type, $themesubrev, $usesvg, $chunk);
104 if (file_exists($chunkedcandidatesheet)) {
105 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
106 // We do not actually need to verify the etag value because our files
107 // never change in cache because we increment the rev counter.
108 css_send_unmodified(filemtime($chunkedcandidatesheet), $etag);
110 css_send_cached_css($chunkedcandidatesheet, $etag);
113 // Ok, now we need to start normal moodle script, we need to load all libs and $DB.
114 define('ABORT_AFTER_CONFIG_CANCEL', true);
116 define('NO_MOODLE_COOKIES', true); // Session not used here.
117 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check.
119 require("$CFG->dirroot/lib/setup.php");
121 $theme = theme_config::load($themename);
122 $theme->force_svg_use($usesvg);
123 $theme->set_rtl_mode($type === 'all-rtl' ? true : false);
125 $themerev = theme_get_revision();
126 $currentthemesubrev = theme_get_sub_revision_for_theme($themename);
128 $cache = true;
129 // If the client is requesting a revision that doesn't match both
130 // the global theme revision and the theme specific revision then
131 // tell the browser not to cache this style sheet because it's
132 // likely being regenerated.
133 if ($themerev <= 0 or $themerev != $rev or $themesubrev != $currentthemesubrev) {
134 $rev = $themerev;
135 $cache = false;
137 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
138 $candidatesheet = "{$candidatedir}/" . theme_styles_get_filename($type, $themesubrev, $usesvg);
139 $chunkedcandidatesheet = "{$candidatedir}/" . theme_styles_get_filename($type, $themesubrev, $usesvg, $chunk);
140 $etag = theme_styles_get_etag($themename, $rev, $type, $themesubrev, $usesvg, $chunk);
143 make_localcache_directory('theme', false);
145 if ($type === 'editor') {
146 $csscontent = $theme->get_css_content_editor();
147 css_store_css($theme, $candidatesheet, $csscontent, false);
149 if ($cache) {
150 css_send_cached_css($candidatesheet, $etag);
151 } else {
152 css_send_uncached_css(file_get_contents($candidatesheet));
157 if (($fallbacksheet = theme_styles_fallback_content($theme)) && !$theme->has_css_cached_content()) {
158 // The theme is not yet available and a fallback is available.
159 // Return the fallback immediately, specifying the Content-Length, then generate in the background.
160 $css = file_get_contents($fallbacksheet);
161 css_send_temporary_css($css);
163 // The fallback content has now been sent.
164 // There will be an attempt to generate the content, but it should not be served.
165 // The Content-Length above means that the client will disregard it anyway.
166 $sendaftergeneration = false;
168 // There may be another client currently holding a lock and generating the stylesheet.
169 // Use a very low lock timeout as the connection will be ended immediately afterwards.
170 $locktimeout = 1;
171 } else {
172 // There is no fallback content to be issued here, therefore the generated content must be output.
173 $sendaftergeneration = true;
175 // Use a realistic lock timeout as the intention is to avoid lock contention.
176 $locktimeout = rand(90, 120);
179 // Attempt to fetch the lock.
180 $lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content');
181 $lock = $lockfactory->get_lock($themename, $locktimeout);
183 if ($sendaftergeneration || $lock) {
184 // Either the lock was successful, or the lock was unsuccessful but the content *must* be sent.
185 if (!file_exists($chunkedcandidatesheet)) {
186 // The content does not exist locally.
187 // Generate and save it.
188 $candidatesheet = theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidatedir);
191 if ($lock) {
192 $lock->release();
195 if ($sendaftergeneration) {
196 if (!$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(file_get_contents($candidatesheet));
201 } else if ($chunk !== null and file_exists($chunkedcandidatesheet)) {
202 // Greetings stupid legacy IEs!
203 css_send_cached_css($chunkedcandidatesheet, $etag);
205 } else {
206 // Real browsers - this is the expected result!
207 css_send_cached_css($candidatesheet, $etag);
213 * Generate the theme CSS and store it.
215 * @param theme_config $theme The theme to be generated
216 * @param int $rev The theme revision
217 * @param int $themesubrev The theme sub-revision
218 * @param string $candidatedir The directory that it should be stored in
219 * @return string The path that the primary (non-chunked) CSS was written to
221 function theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidatedir) {
222 global $CFG;
224 // Generate the content first.
225 if (!$csscontent = $theme->get_css_cached_content()) {
226 $csscontent = $theme->get_css_content();
227 $theme->set_css_content_cache($csscontent);
230 if ($theme->get_rtl_mode()) {
231 $type = "all-rtl";
232 } else {
233 $type = "all";
236 // Determine the candidatesheet path.
237 // Note: Do not pass any value for chunking as this is calcualted during css storage.
238 $candidatesheet = "{$candidatedir}/" . theme_styles_get_filename($type, $themesubrev, $theme->use_svg_icons());
240 // Determine the chunking URL.
241 // Note, this will be removed when support for IE9 is removed.
242 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
243 if (!empty(min_get_slash_argument())) {
244 if ($theme->use_svg_icons()) {
245 $chunkurl = "{$relroot}/theme/styles.php/{$theme->name}/{$rev}/$type";
246 } else {
247 $chunkurl = "{$relroot}/theme/styles.php/_s/{$theme->name}/{$rev}/$type";
249 } else {
250 if ($theme->use_svg_icons()) {
251 $chunkurl = "{$relroot}/theme/styles.php?theme={$theme->name}&rev={$rev}&type=$type";
252 } else {
253 $chunkurl = "{$relroot}/theme/styles.php?theme={$theme->name}&rev={$rev}&type=$type&svg=0";
257 // Store the CSS.
258 css_store_css($theme, $candidatesheet, $csscontent, true, $chunkurl);
260 // Store the fallback CSS in the temp directory.
261 // This file is used as a fallback when waiting for a theme to compile and is not versioned in any way.
262 $fallbacksheet = make_temp_directory("theme/{$theme->name}")
263 . "/"
264 . theme_styles_get_filename($type, 0, $theme->use_svg_icons());
265 css_store_css($theme, $fallbacksheet, $csscontent, true, $chunkurl);
267 return $candidatesheet;
271 * Fetch the preferred fallback content location if available.
273 * @param theme_config $theme The theme to be generated
274 * @return string The path to the fallback sheet on disk
276 function theme_styles_fallback_content($theme) {
277 global $CFG;
279 if (!$theme->usefallback) {
280 // This theme does not support fallbacks.
281 return false;
284 $type = $theme->get_rtl_mode() ? 'all-rtl' : 'all';
285 $filename = theme_styles_get_filename($type);
287 $fallbacksheet = "{$CFG->tempdir}/theme/{$theme->name}/{$filename}";
288 if (file_exists($fallbacksheet)) {
289 return $fallbacksheet;
292 return false;
296 * Get the filename for the specified configuration.
298 * @param string $type The requested sheet type
299 * @param int $themesubrev The theme sub-revision
300 * @param bool $usesvg Whether SVGs are allowed
301 * @param int $chunk The chunk number if specified
302 * @return string The filename for this sheet
304 function theme_styles_get_filename($type, $themesubrev = 0, $usesvg = true, $chunk = null) {
305 $filename = $type;
306 $filename .= ($themesubrev > 0) ? "_{$themesubrev}" : '';
307 $filename .= $usesvg ? '' : '-nosvg';
308 $filename .= $chunk ? ".{$chunk}" : '';
310 return "{$filename}.css";
314 * Determine the correct etag for the specified configuration.
316 * @param string $themename The name of the theme
317 * @param int $rev The revision number
318 * @param string $type The requested sheet type
319 * @param int $themesubrev The theme sub-revision
320 * @param bool $usesvg Whether SVGs are allowed
321 * @param int $chunk The chunk number if specified
322 * @return string The etag to use for this request
324 function theme_styles_get_etag($themename, $rev, $type, $themesubrev, $usesvg, $chunk) {
325 $etag = [$rev, $themename, $type, $themesubrev];
327 if (!$usesvg) {
328 $etag[] = 'nosvg';
331 if ($chunk) {
332 $etag[] = "chunk{$chunk}";
335 return sha1(implode('/', $etag));