MDL-40563 use revisions for theme css cache files and fix potential race condition
[moodle.git] / theme / styles_debug.php
CommitLineData
78946b9b 1<?php
78946b9b
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 of individual style sheets in designer mode.
19 *
574909ef 20 * @package core
78946b9b
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
73e504bc 25
7829cf79
PS
26define('ABORT_AFTER_CONFIG', true);
27require('../config.php'); // this stops immediately at the beginning of lib/setup.php
0e641c74 28require_once($CFG->dirroot.'/lib/csslib.php');
78946b9b 29
7829cf79 30$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
4399b9d5 31$type = min_optional_param('type', '', 'SAFEDIR');
7829cf79
PS
32$subtype = min_optional_param('subtype', '', 'SAFEDIR');
33$sheet = min_optional_param('sheet', '', 'SAFEDIR');
383b89a1 34$usesvg = (bool)min_optional_param('svg', '1', 'INT');
78946b9b 35
d6b64d15
PS
36if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
37 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
38}
39
73e504bc
PS
40if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
41 // exists
42} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
43 // exists
44} else {
0e641c74 45 css_send_css_not_found();
78946b9b
PS
46}
47
7c986f04
PS
48// no gzip compression when debugging
49
383b89a1
SH
50if ($usesvg) {
51 $candidatesheet = "$CFG->cachedir/theme/$themename/designer.ser";
52} else {
53 // Add to the sheet name, one day we'll be able to just drop this.
54 $candidatesheet = "$CFG->cachedir/theme/$themename/designer_nosvg.ser";
55}
7829cf79
PS
56
57if (!file_exists($candidatesheet)) {
383b89a1 58
0e641c74 59 css_send_css_not_found();
78946b9b
PS
60}
61
7829cf79 62if (!$css = file_get_contents($candidatesheet)) {
0e641c74 63 css_send_css_not_found();
78946b9b
PS
64}
65
7829cf79 66$css = unserialize($css);
78946b9b 67
71be124d
SH
68if ($type === 'editor') {
69 if (isset($css['editor'])) {
0e641c74 70 css_send_uncached_css($css['editor']);
71be124d
SH
71 }
72} else if ($type === 'ie') {
4399b9d5
PS
73 // IE is a sloppy browser with weird limits, sorry
74 if ($subtype === 'plugins') {
0e641c74 75 css_send_uncached_css($css['plugins']);
4399b9d5
PS
76
77 } else if ($subtype === 'parents') {
78 $sendcss = array();
a6338a13
SH
79 if (empty($sheet)) {
80 // If not specific parent has been specified as $sheet then build a
81 // collection of @import statements into this one sheet.
82 // We shouldn't ever actually get here, but none the less we'll deal
83 // with it incase we ever do.
84 // @import statements arn't processed until after concurrent CSS requests
85 // making them slightly evil.
86 foreach (array_keys($css['parents']) as $sheet) {
87 $sendcss[] = "@import url(styles_debug.php?theme=$themename&type=$type&subtype=$subtype&sheet=$sheet);";
88 }
89 } else {
90 // Build up the CSS for that parent so we can serve it as one file.
91 foreach ($css[$subtype][$sheet] as $parent=>$css) {
92 $sendcss[] = $css;
93 }
4399b9d5 94 }
0e641c74 95 css_send_uncached_css($sendcss);
4399b9d5 96 } else if ($subtype === 'theme') {
0e641c74 97 css_send_uncached_css($css['theme']);
4399b9d5 98 }
c09e1a2c
PS
99
100} else if ($type === 'plugin') {
78946b9b 101 if (isset($css['plugins'][$subtype])) {
0e641c74 102 css_send_uncached_css($css['plugins'][$subtype]);
78946b9b
PS
103 }
104
105} else if ($type === 'parent') {
106 if (isset($css['parents'][$subtype][$sheet])) {
0e641c74 107 css_send_uncached_css($css['parents'][$subtype][$sheet]);
78946b9b
PS
108 }
109
110} else if ($type === 'theme') {
111 if (isset($css['theme'][$sheet])) {
0e641c74 112 css_send_uncached_css($css['theme'][$sheet]);
78946b9b
PS
113 }
114}
0e641c74 115css_send_css_not_found();