weekly release 2.3dev (blame stronk7 for 0202 mistake)
[moodle.git] / theme / styles.php
CommitLineData
b7009474 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
78946b9b 19 * This file is responsible for serving the one huge CSS of each theme.
b7009474 20 *
21 * @package moodlecore
78946b9b 22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
b7009474 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
73e504bc 26
c198390a
PS
27// disable moodle specific debug messages and any errors in output,
28// comment out when debugging or better look into error log!
29define('NO_DEBUG_DISPLAY', true);
30
78946b9b
PS
31// we need just the values from config.php and minlib.php
32define('ABORT_AFTER_CONFIG', true);
33require('../config.php'); // this stops immediately at the beginning of lib/setup.php
b7009474 34
78946b9b
PS
35$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
36$type = min_optional_param('type', 'all', 'SAFEDIR');
37$rev = min_optional_param('rev', 0, 'INT');
b7009474 38
c724f835 39if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
78946b9b
PS
40 header('HTTP/1.0 404 not found');
41 die('Theme was not found, sorry.');
b7009474 42}
43
73e504bc
PS
44if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
45 // exists
46} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
47 // exists
48} else {
78946b9b
PS
49 header('HTTP/1.0 404 not found');
50 die('Theme was not found, sorry.');
51}
b7009474 52
78946b9b
PS
53if ($type === 'ie') {
54 send_ie_css($themename, $rev);
55}
b7009474 56
365bec4c 57$candidatesheet = "$CFG->cachedir/theme/$themename/css/$type.css";
b7009474 58
78946b9b 59if (file_exists($candidatesheet)) {
aa603b14 60 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
78946b9b
PS
61 // we do not actually need to verify the etag value because our files
62 // never change in cache because we increment the rev parameter
ccc0fff9 63 $lifetime = 60*60*24*30; // 30 days
aa603b14 64 header('HTTP/1.1 304 Not Modified');
ccc0fff9
TL
65 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
66 header('Cache-Control: max-age='.$lifetime);
67 header('Content-Type: text/css; charset=utf-8');
78946b9b
PS
68 die;
69 }
70 send_cached_css($candidatesheet, $rev);
b7009474 71}
72
78946b9b
PS
73//=================================================================================
74// ok, now we need to start normal moodle script, we need to load all libs and $DB
75define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 76
78946b9b
PS
77define('NO_MOODLE_COOKIES', true); // Session not used here
78define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
fdeb7fa1 79
78946b9b 80require("$CFG->dirroot/lib/setup.php");
045f492c
SH
81// setup include path
82set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
83require_once('Minify.php');
b7009474 84
78946b9b 85$theme = theme_config::load($themename);
fdeb7fa1 86
78946b9b 87if ($type === 'editor') {
71be124d
SH
88 $files = $theme->editor_css_files();
89 store_css($theme, $candidatesheet, $files);
78946b9b 90} else {
045f492c
SH
91 $css = $theme->css_files();
92 $allfiles = array();
78946b9b 93 foreach ($css as $key=>$value) {
045f492c 94 $cssfiles = array();
78946b9b
PS
95 foreach($value as $val) {
96 if (is_array($val)) {
e10f304d 97 foreach ($val as $k=>$v) {
045f492c 98 $cssfiles[] = $v;
e10f304d 99 }
78946b9b 100 } else {
045f492c 101 $cssfiles[] = $val;
78946b9b
PS
102 }
103 }
365bec4c 104 $cssfile = "$CFG->cachedir/theme/$themename/css/$key.css";
045f492c
SH
105 store_css($theme, $cssfile, $cssfiles);
106 $allfiles = array_merge($allfiles, $cssfiles);
fdeb7fa1 107 }
365bec4c 108 $cssfile = "$CFG->cachedir/theme/$themename/css/all.css";
045f492c 109 store_css($theme, $cssfile, $allfiles);
fdeb7fa1 110}
78946b9b 111send_cached_css($candidatesheet, $rev);
b7009474 112
78946b9b
PS
113//=================================================================================
114//=== utility functions ==
115// we are not using filelib because we need to fine tune all header
116// parameters to get the best performance.
b7009474 117
045f492c
SH
118function store_css(theme_config $theme, $csspath, $cssfiles) {
119 $css = $theme->post_process(minify($cssfiles));
e164a0df
PS
120 // note: cache reset might have purged our cache dir structure,
121 // make sure we do not use stale file stat cache in the next check_dir_exists()
122 clearstatcache();
c426ef3a 123 check_dir_exists(dirname($csspath));
78946b9b
PS
124 $fp = fopen($csspath, 'w');
125 fwrite($fp, $css);
126 fclose($fp);
b7009474 127}
128
78946b9b 129function send_ie_css($themename, $rev) {
ccc0fff9 130 $lifetime = 60*60*24*30; // 30 days
78946b9b
PS
131
132 $css = <<<EOF
133/** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/
78946b9b
PS
134@import url(styles.php?theme=$themename&rev=$rev&type=plugins);
135@import url(styles.php?theme=$themename&rev=$rev&type=parents);
136@import url(styles.php?theme=$themename&rev=$rev&type=theme);
137
138EOF;
139
140 header('Etag: '.md5($rev));
141 header('Content-Disposition: inline; filename="styles.php"');
142 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
143 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
144 header('Pragma: ');
ccc0fff9 145 header('Cache-Control: max-age='.$lifetime);
78946b9b 146 header('Accept-Ranges: none');
8a7703ce 147 header('Content-Type: text/css; charset=utf-8');
e10f304d 148 header('Content-Length: '.strlen($css));
78946b9b 149
78946b9b
PS
150 echo $css;
151 die;
b7009474 152}
b7009474 153
78946b9b 154function send_cached_css($csspath, $rev) {
ccc0fff9 155 $lifetime = 60*60*24*30; // 30 days
b7009474 156
78946b9b
PS
157 header('Content-Disposition: inline; filename="styles.php"');
158 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
159 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
160 header('Pragma: ');
ccc0fff9 161 header('Cache-Control: max-age='.$lifetime);
78946b9b 162 header('Accept-Ranges: none');
8a7703ce 163 header('Content-Type: text/css; charset=utf-8');
7c986f04
PS
164 if (!min_enable_zlib_compression()) {
165 header('Content-Length: '.filesize($csspath));
166 }
b7009474 167
78946b9b
PS
168 readfile($csspath);
169 die;
b7009474 170}
e10f304d 171
045f492c
SH
172function minify($files) {
173 if (0 === stripos(PHP_OS, 'win')) {
174 Minify::setDocRoot(); // IIS may need help
175 }
e1c2a211
PS
176 // disable all caching, we do it in moodle
177 Minify::setCache(null, false);
045f492c
SH
178
179 $options = array(
180 'bubbleCssImports' => false,
181 // Don't gzip content we just want text for storage
182 'encodeOutput' => false,
183 // Maximum age to cache, not used but required
184 'maxAge' => (60*60*24*20),
185 // The files to minify
186 'files' => $files,
187 // Turn orr URI rewriting
188 'rewriteCssUris' => false,
189 // This returns the CSS rather than echoing it for display
190 'quiet' => true
191 );
192 $result = Minify::serve('Files', $options);
193 return $result['content'];
194}