Merge branch 'MDL-62181-33-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_33_...
[moodle.git] / theme / styles.php
blobcc2714f1604a0f219f516bd6cbcbedb60bc618e2
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, 'INT');
56 $type = min_clean_param($type, 'SAFEDIR');
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');
62 $chunk = min_optional_param('chunk', null, 'INT');
63 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
66 if ($type === 'editor') {
67 // The editor CSS is never chunked.
68 $chunk = null;
69 } else if ($type === 'all' || $type === 'all-rtl') {
70 // We're fine.
71 } else {
72 css_send_css_not_found();
75 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
76 // The theme exists in standard location - ok.
77 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
78 // Alternative theme location contains this theme - ok.
79 } else {
80 header('HTTP/1.0 404 not found');
81 die('Theme was not found, sorry.');
84 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
85 $etag = "$rev/$themename/$type";
86 $candidatename = $type;
87 if (!$usesvg) {
88 // Add to the sheet name, one day we'll be able to just drop this.
89 $candidatedir .= '/nosvg';
90 $etag .= '/nosvg';
93 if ($chunk !== null) {
94 $etag .= '/chunk'.$chunk;
95 $candidatename .= '.'.$chunk;
97 $candidatesheet = "$candidatedir/$candidatename.css";
98 $etag = sha1($etag);
100 if (file_exists($candidatesheet)) {
101 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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.
104 css_send_unmodified(filemtime($candidatesheet), $etag);
106 css_send_cached_css($candidatesheet, $etag);
109 // Ok, now we need to start normal moodle script, we need to load all libs and $DB.
110 define('ABORT_AFTER_CONFIG_CANCEL', true);
112 define('NO_MOODLE_COOKIES', true); // Session not used here.
113 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check.
115 require("$CFG->dirroot/lib/setup.php");
117 $theme = theme_config::load($themename);
118 $theme->force_svg_use($usesvg);
119 $theme->set_rtl_mode($type === 'all-rtl' ? true : false);
121 $themerev = theme_get_revision();
123 $cache = true;
124 if ($themerev <= 0 or $themerev != $rev) {
125 $rev = $themerev;
126 $cache = false;
128 $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
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';
137 if ($chunk !== null) {
138 $etag .= '/chunk'.$chunk;
139 $candidatename .= '.'.$chunk;
141 $candidatesheet = "$candidatedir/$candidatename.css";
142 $etag = sha1($etag);
145 make_localcache_directory('theme', false);
147 if ($type === 'editor') {
148 $csscontent = $theme->get_css_content_editor();
149 css_store_css($theme, "$candidatedir/editor.css", $csscontent, false);
151 } else {
152 // Fetch a lock whilst the CSS is fetched as this can be slow and CPU intensive.
153 // Each client should wait for one to finish the compilation before starting the compiler.
154 $lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content');
155 $lock = $lockfactory->get_lock($themename, rand(90, 120));
157 if (file_exists($candidatesheet)) {
158 // The file was built while we waited for the lock, we release the lock and serve the file.
159 if ($lock) {
160 $lock->release();
163 if ($cache) {
164 css_send_cached_css($candidatesheet, $etag);
165 } else {
166 css_send_uncached_css(file_get_contents($candidatesheet));
170 // The lock is still held, and the sheet still does not exist.
171 // Compile the CSS content.
172 if (!$csscontent = $theme->get_css_cached_content()) {
173 $csscontent = $theme->get_css_content();
174 $theme->set_css_content_cache($csscontent);
177 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
178 if (!empty($slashargument)) {
179 if ($usesvg) {
180 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/$type";
181 } else {
182 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/$type";
184 } else {
185 if ($usesvg) {
186 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type";
187 } else {
188 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type&svg=0";
192 css_store_css($theme, "$candidatedir/$type.css", $csscontent, true, $chunkurl);
194 if ($lock) {
195 // Now that the CSS has been generated and/or stored, release the lock.
196 // This will allow waiting clients to use the newly generated and stored CSS.
197 $lock->release();
201 if (!$cache) {
202 // Do not pollute browser caches if invalid revision requested,
203 // let's ignore legacy IE breakage here too.
204 css_send_uncached_css($csscontent);
206 } else if ($chunk !== null and file_exists($candidatesheet)) {
207 // Greetings stupid legacy IEs!
208 css_send_cached_css($candidatesheet, $etag);
210 } else {
211 // Real browsers - this is the expected result!
212 css_send_cached_css_content($csscontent, $etag);