weekly release 2.6dev
[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
c724f835 68if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
78946b9b
PS
69 header('HTTP/1.0 404 not found');
70 die('Theme was not found, sorry.');
b7009474 71}
72
73e504bc
PS
73if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
74 // exists
75} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
76 // exists
77} else {
78946b9b
PS
78 header('HTTP/1.0 404 not found');
79 die('Theme was not found, sorry.');
80}
b7009474 81
78946b9b 82if ($type === 'ie') {
2c61e7cc 83 css_send_ie_css($themename, $rev, $etag, !empty($slashargument));
78946b9b 84}
b7009474 85
383b89a1
SH
86$candidatedir = "$CFG->cachedir/theme/$themename/css";
87$etag = "$themename/$rev/$type";
e1075027 88$candidatename = $type;
383b89a1
SH
89if (!$usesvg) {
90 // Add to the sheet name, one day we'll be able to just drop this.
91 $candidatedir .= '/nosvg';
92 $etag .= '/nosvg';
93}
e1075027
SH
94if ($chunk !== null) {
95 $etag .= '/chunk'.$chunk;
96 $candidatename .= '.'.$chunk;
97}
98$candidatesheet = "$candidatedir/$candidatename.css";
383b89a1 99$etag = sha1($etag);
b7009474 100
78946b9b 101if (file_exists($candidatesheet)) {
aa603b14 102 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
78946b9b
PS
103 // we do not actually need to verify the etag value because our files
104 // never change in cache because we increment the rev parameter
869b1255 105 css_send_unmodified(filemtime($candidatesheet), $etag);
78946b9b 106 }
2c61e7cc 107 css_send_cached_css($candidatesheet, $etag);
b7009474 108}
109
78946b9b
PS
110//=================================================================================
111// ok, now we need to start normal moodle script, we need to load all libs and $DB
112define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 113
78946b9b
PS
114define('NO_MOODLE_COOKIES', true); // Session not used here
115define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
fdeb7fa1 116
78946b9b 117require("$CFG->dirroot/lib/setup.php");
b7009474 118
78946b9b 119$theme = theme_config::load($themename);
383b89a1 120$theme->force_svg_use($usesvg);
fdeb7fa1 121
2c61e7cc 122$rev = theme_get_revision();
383b89a1
SH
123
124$etag = "$themename/$rev/$type";
125if (!$usesvg) {
126 // Add to the etag, one day we'll be able to just delete svg nonsense this.
127 $etag .= '/nosvg';
128}
129$etag = sha1($etag);
2c61e7cc 130
78946b9b 131if ($type === 'editor') {
7e08c95f 132 $cssfiles = $theme->editor_css_files();
0e641c74 133 css_store_css($theme, $candidatesheet, $cssfiles);
78946b9b 134} else {
e1075027 135 // IE requests plugins/parents/theme instead of all at once.
383b89a1
SH
136 $basedir = "$CFG->cachedir/theme/$themename/css";
137 if (!$usesvg) {
138 $basedir .= '/nosvg';
139 }
045f492c
SH
140 $css = $theme->css_files();
141 $allfiles = array();
e1075027 142 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
78946b9b 143 foreach ($css as $key=>$value) {
e1075027 144 if (!empty($slashargument)) {
13b0140a
SH
145 if ($usesvg) {
146 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/{$key}";
147 } else {
148 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/{$key}";
149 }
e1075027 150 } else {
13b0140a
SH
151 if ($usesvg) {
152 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type={$key}";
153 } else {
154 $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type={$key}&svg=0";
155 }
e1075027 156 }
045f492c 157 $cssfiles = array();
78946b9b
PS
158 foreach($value as $val) {
159 if (is_array($val)) {
e10f304d 160 foreach ($val as $k=>$v) {
045f492c 161 $cssfiles[] = $v;
e10f304d 162 }
78946b9b 163 } else {
045f492c 164 $cssfiles[] = $val;
78946b9b
PS
165 }
166 }
383b89a1 167 $cssfile = "$basedir/$key.css";
e1075027 168 css_store_css($theme, $cssfile, $cssfiles, true, $chunkurl);
045f492c 169 $allfiles = array_merge($allfiles, $cssfiles);
fdeb7fa1 170 }
383b89a1 171 $cssfile = "$basedir/all.css";
0e641c74 172 css_store_css($theme, $cssfile, $allfiles);
045f492c 173}
979d3207
PS
174
175// verify nothing failed in cache file creation
176clearstatcache();
177if (!file_exists($candidatesheet)) {
178 css_send_css_not_found();
179}
180
2c61e7cc 181css_send_cached_css($candidatesheet, $etag);