Merge branch 'MDL-38885-master-int' of git://github.com/FMCorz/moodle
[moodle.git] / theme / styles.php
blobeeab1ef79043a068a470690f7947b5d55eab0720
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
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
33 require_once($CFG->dirroot.'/lib/csslib.php');
35 if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 2) {
38 css_send_css_not_found();
41 if (strpos($slashargument, '_s/') === 0) {
42 // Can't use SVG
43 $slashargument = substr($slashargument, 3);
44 $usesvg = false;
45 } else {
46 $usesvg = true;
49 $chunk = null;
50 if (preg_match('#/(chunk(\d+)(/|$))#', $slashargument, $matches)) {
51 $chunk = (int)$matches[2];
52 $slashargument = str_replace($matches[1], '', $slashargument);
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');
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');
64 $chunk = min_optional_param('chunk', null, 'INT');
65 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
68 if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
69 header('HTTP/1.0 404 not found');
70 die('Theme was not found, sorry.');
73 if (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 {
78 header('HTTP/1.0 404 not found');
79 die('Theme was not found, sorry.');
82 if ($type === 'ie') {
83 css_send_ie_css($themename, $rev, $etag, !empty($slashargument));
86 $candidatedir = "$CFG->cachedir/theme/$themename/css";
87 $etag = "$themename/$rev/$type";
88 $candidatename = $type;
89 if (!$usesvg) {
90 // Add to the sheet name, one day we'll be able to just drop this.
91 $candidatedir .= '/nosvg';
92 $etag .= '/nosvg';
94 if ($chunk !== null) {
95 $etag .= '/chunk'.$chunk;
96 $candidatename .= '.'.$chunk;
98 $candidatesheet = "$candidatedir/$candidatename.css";
99 $etag = sha1($etag);
101 if (file_exists($candidatesheet)) {
102 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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
105 css_send_unmodified(filemtime($candidatesheet), $etag);
107 css_send_cached_css($candidatesheet, $etag);
110 //=================================================================================
111 // ok, now we need to start normal moodle script, we need to load all libs and $DB
112 define('ABORT_AFTER_CONFIG_CANCEL', true);
114 define('NO_MOODLE_COOKIES', true); // Session not used here
115 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
117 require("$CFG->dirroot/lib/setup.php");
119 $theme = theme_config::load($themename);
120 $theme->force_svg_use($usesvg);
122 $rev = theme_get_revision();
124 $etag = "$themename/$rev/$type";
125 if (!$usesvg) {
126 // Add to the etag, one day we'll be able to just delete svg nonsense this.
127 $etag .= '/nosvg';
129 $etag = sha1($etag);
131 if ($type === 'editor') {
132 $cssfiles = $theme->editor_css_files();
133 css_store_css($theme, $candidatesheet, $cssfiles);
134 } else {
135 // IE requests plugins/parents/theme instead of all at once.
136 $basedir = "$CFG->cachedir/theme/$themename/css";
137 if (!$usesvg) {
138 $basedir .= '/nosvg';
140 $css = $theme->css_files();
141 $allfiles = array();
142 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
143 foreach ($css as $key=>$value) {
144 if (!empty($slashargument)) {
145 if ($usesvg) {
146 $chunkurl = "{$relroot}/theme/styles.php/{$themename}/{$rev}/{$key}";
147 } else {
148 $chunkurl = "{$relroot}/theme/styles.php/_s/{$themename}/{$rev}/{$key}";
150 } else {
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";
157 $cssfiles = array();
158 foreach($value as $val) {
159 if (is_array($val)) {
160 foreach ($val as $k=>$v) {
161 $cssfiles[] = $v;
163 } else {
164 $cssfiles[] = $val;
167 $cssfile = "$basedir/$key.css";
168 css_store_css($theme, $cssfile, $cssfiles, true, $chunkurl);
169 $allfiles = array_merge($allfiles, $cssfiles);
171 $cssfile = "$basedir/all.css";
172 css_store_css($theme, $cssfile, $allfiles);
175 // verify nothing failed in cache file creation
176 clearstatcache();
177 if (!file_exists($candidatesheet)) {
178 css_send_css_not_found();
181 css_send_cached_css($candidatesheet, $etag);