MDL-40563 use revisions for theme js cache files
[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
73e504bc 25
c198390a
PS
26// disable moodle specific debug messages and any errors in output,
27// comment out when debugging or better look into error log!
28define('NO_DEBUG_DISPLAY', true);
29
78946b9b
PS
30// we need just the values from config.php and minlib.php
31define('ABORT_AFTER_CONFIG', true);
32require('../config.php'); // this stops immediately at the beginning of lib/setup.php
0e641c74 33require_once($CFG->dirroot.'/lib/csslib.php');
b7009474 34
7070b7f2
PS
35if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 2) {
e1075027 38 css_send_css_not_found();
7070b7f2 39 }
383b89a1
SH
40
41 if (strpos($slashargument, '_s/') === 0) {
42 // Can't use SVG
43 $slashargument = substr($slashargument, 3);
44 $usesvg = false;
45 } else {
46 $usesvg = true;
47 }
48
e1075027
SH
49 $chunk = null;
50 if (preg_match('#/(chunk(\d+)(/|$))#', $slashargument, $matches)) {
51 $chunk = (int)$matches[2];
52 $slashargument = str_replace($matches[1], '', $slashargument);
53 }
54
7070b7f2
PS
55 list($themename, $rev, $type) = explode('/', $slashargument, 3);
56 $themename = min_clean_param($themename, 'SAFEDIR');
57 $rev = min_clean_param($rev, 'INT');
58 $type = min_clean_param($type, 'SAFEDIR');
59
60} else {
61 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
62 $rev = min_optional_param('rev', 0, 'INT');
63 $type = min_optional_param('type', 'all', 'SAFEDIR');
e1075027 64 $chunk = min_optional_param('chunk', null, 'INT');
383b89a1 65 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
7070b7f2 66}
b7009474 67
1eb2b517
SH
68if ($type === 'editor') {
69 // The editor CSS is never chunked.
70 $chunk = null;
71} else if ($type === 'all') {
72 // We're fine.
73} else {
78946b9b
PS
74 header('HTTP/1.0 404 not found');
75 die('Theme was not found, sorry.');
b7009474 76}
77
73e504bc
PS
78if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
79 // exists
80} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
81 // exists
82} else {
78946b9b
PS
83 header('HTTP/1.0 404 not found');
84 die('Theme was not found, sorry.');
85}
b7009474 86
383b89a1
SH
87$candidatedir = "$CFG->cachedir/theme/$themename/css";
88$etag = "$themename/$rev/$type";
e1075027 89$candidatename = $type;
383b89a1
SH
90if (!$usesvg) {
91 // Add to the sheet name, one day we'll be able to just drop this.
92 $candidatedir .= '/nosvg';
93 $etag .= '/nosvg';
94}
1eb2b517 95
e1075027
SH
96if ($chunk !== null) {
97 $etag .= '/chunk'.$chunk;
98 $candidatename .= '.'.$chunk;
99}
100$candidatesheet = "$candidatedir/$candidatename.css";
383b89a1 101$etag = sha1($etag);
b7009474 102
78946b9b 103if (file_exists($candidatesheet)) {
aa603b14 104 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
78946b9b
PS
105 // we do not actually need to verify the etag value because our files
106 // never change in cache because we increment the rev parameter
869b1255 107 css_send_unmodified(filemtime($candidatesheet), $etag);
78946b9b 108 }
2c61e7cc 109 css_send_cached_css($candidatesheet, $etag);
b7009474 110}
111
78946b9b
PS
112//=================================================================================
113// ok, now we need to start normal moodle script, we need to load all libs and $DB
114define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 115
78946b9b
PS
116define('NO_MOODLE_COOKIES', true); // Session not used here
117define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
fdeb7fa1 118
78946b9b 119require("$CFG->dirroot/lib/setup.php");
b7009474 120
78946b9b 121$theme = theme_config::load($themename);
383b89a1 122$theme->force_svg_use($usesvg);
fdeb7fa1 123
2c61e7cc 124$rev = theme_get_revision();
383b89a1
SH
125
126$etag = "$themename/$rev/$type";
127if (!$usesvg) {
128 // Add to the etag, one day we'll be able to just delete svg nonsense this.
129 $etag .= '/nosvg';
130}
131$etag = sha1($etag);
2c61e7cc 132
78946b9b 133if ($type === 'editor') {
7e08c95f 134 $cssfiles = $theme->editor_css_files();
0e641c74 135 css_store_css($theme, $candidatesheet, $cssfiles);
78946b9b 136} else {
e1075027 137 // IE requests plugins/parents/theme instead of all at once.
383b89a1
SH
138 $basedir = "$CFG->cachedir/theme/$themename/css";
139 if (!$usesvg) {
140 $basedir .= '/nosvg';
141 }
045f492c
SH
142 $css = $theme->css_files();
143 $allfiles = array();
e1075027 144 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
1eb2b517
SH
145 if (!empty($slashargument)) {
146 if ($usesvg) {
147 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/all";
e1075027 148 } else {
1eb2b517
SH
149 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/all";
150 }
151 } else {
152 if ($usesvg) {
153 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=all";
154 } else {
155 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=all&svg=0";
e1075027 156 }
1eb2b517
SH
157 }
158 $cssfiles = array();
159 foreach ($css as $key=>$value) {
78946b9b
PS
160 foreach($value as $val) {
161 if (is_array($val)) {
e10f304d 162 foreach ($val as $k=>$v) {
045f492c 163 $cssfiles[] = $v;
e10f304d 164 }
78946b9b 165 } else {
045f492c 166 $cssfiles[] = $val;
78946b9b
PS
167 }
168 }
fdeb7fa1 169 }
1eb2b517 170 css_store_css($theme, "$basedir/all.css", $cssfiles, true, $chunkurl);
045f492c 171}
979d3207
PS
172
173// verify nothing failed in cache file creation
174clearstatcache();
175if (!file_exists($candidatesheet)) {
176 css_send_css_not_found();
177}
178
2c61e7cc 179css_send_cached_css($candidatesheet, $etag);