MDL-28701 Fixed broken $CFG->tempdir default and added $CFG->cachedir
[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
78946b9b
PS
27// we need just the values from config.php and minlib.php
28define('ABORT_AFTER_CONFIG', true);
29require('../config.php'); // this stops immediately at the beginning of lib/setup.php
b7009474 30
78946b9b
PS
31$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32$type = min_optional_param('type', 'all', 'SAFEDIR');
33$rev = min_optional_param('rev', 0, 'INT');
b7009474 34
c724f835 35if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
78946b9b
PS
36 header('HTTP/1.0 404 not found');
37 die('Theme was not found, sorry.');
b7009474 38}
39
73e504bc
PS
40if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
41 // exists
42} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
43 // exists
44} else {
78946b9b
PS
45 header('HTTP/1.0 404 not found');
46 die('Theme was not found, sorry.');
47}
b7009474 48
78946b9b
PS
49if ($type === 'ie') {
50 send_ie_css($themename, $rev);
51}
b7009474 52
78946b9b 53$candidatesheet = "$CFG->dataroot/cache/theme/$themename/css/$type.css";
b7009474 54
78946b9b 55if (file_exists($candidatesheet)) {
aa603b14 56 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
78946b9b
PS
57 // we do not actually need to verify the etag value because our files
58 // never change in cache because we increment the rev parameter
ccc0fff9 59 $lifetime = 60*60*24*30; // 30 days
aa603b14 60 header('HTTP/1.1 304 Not Modified');
ccc0fff9
TL
61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
62 header('Cache-Control: max-age='.$lifetime);
63 header('Content-Type: text/css; charset=utf-8');
78946b9b
PS
64 die;
65 }
66 send_cached_css($candidatesheet, $rev);
b7009474 67}
68
78946b9b
PS
69//=================================================================================
70// ok, now we need to start normal moodle script, we need to load all libs and $DB
71define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 72
78946b9b
PS
73define('NO_MOODLE_COOKIES', true); // Session not used here
74define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
fdeb7fa1 75
78946b9b 76require("$CFG->dirroot/lib/setup.php");
045f492c
SH
77// setup include path
78set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
79require_once('Minify.php');
b7009474 80
78946b9b 81$theme = theme_config::load($themename);
fdeb7fa1 82
78946b9b 83if ($type === 'editor') {
71be124d
SH
84 $files = $theme->editor_css_files();
85 store_css($theme, $candidatesheet, $files);
78946b9b 86} else {
045f492c
SH
87 $css = $theme->css_files();
88 $allfiles = array();
78946b9b 89 foreach ($css as $key=>$value) {
045f492c 90 $cssfiles = array();
78946b9b
PS
91 foreach($value as $val) {
92 if (is_array($val)) {
e10f304d 93 foreach ($val as $k=>$v) {
045f492c 94 $cssfiles[] = $v;
e10f304d 95 }
78946b9b 96 } else {
045f492c 97 $cssfiles[] = $val;
78946b9b
PS
98 }
99 }
78946b9b 100 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/$key.css";
045f492c
SH
101 store_css($theme, $cssfile, $cssfiles);
102 $allfiles = array_merge($allfiles, $cssfiles);
fdeb7fa1 103 }
78946b9b 104 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/all.css";
045f492c 105 store_css($theme, $cssfile, $allfiles);
fdeb7fa1 106}
78946b9b 107send_cached_css($candidatesheet, $rev);
b7009474 108
78946b9b
PS
109//=================================================================================
110//=== utility functions ==
111// we are not using filelib because we need to fine tune all header
112// parameters to get the best performance.
b7009474 113
045f492c
SH
114function store_css(theme_config $theme, $csspath, $cssfiles) {
115 $css = $theme->post_process(minify($cssfiles));
c426ef3a 116 check_dir_exists(dirname($csspath));
78946b9b
PS
117 $fp = fopen($csspath, 'w');
118 fwrite($fp, $css);
119 fclose($fp);
b7009474 120}
121
78946b9b 122function send_ie_css($themename, $rev) {
ccc0fff9 123 $lifetime = 60*60*24*30; // 30 days
78946b9b
PS
124
125 $css = <<<EOF
126/** 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
127@import url(styles.php?theme=$themename&rev=$rev&type=plugins);
128@import url(styles.php?theme=$themename&rev=$rev&type=parents);
129@import url(styles.php?theme=$themename&rev=$rev&type=theme);
130
131EOF;
132
133 header('Etag: '.md5($rev));
134 header('Content-Disposition: inline; filename="styles.php"');
135 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
136 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
137 header('Pragma: ');
ccc0fff9 138 header('Cache-Control: max-age='.$lifetime);
78946b9b 139 header('Accept-Ranges: none');
8a7703ce 140 header('Content-Type: text/css; charset=utf-8');
e10f304d 141 header('Content-Length: '.strlen($css));
78946b9b 142
78946b9b
PS
143 echo $css;
144 die;
b7009474 145}
b7009474 146
78946b9b 147function send_cached_css($csspath, $rev) {
ccc0fff9 148 $lifetime = 60*60*24*30; // 30 days
b7009474 149
78946b9b
PS
150 header('Content-Disposition: inline; filename="styles.php"');
151 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
152 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
153 header('Pragma: ');
ccc0fff9 154 header('Cache-Control: max-age='.$lifetime);
78946b9b 155 header('Accept-Ranges: none');
8a7703ce 156 header('Content-Type: text/css; charset=utf-8');
7c986f04
PS
157 if (!min_enable_zlib_compression()) {
158 header('Content-Length: '.filesize($csspath));
159 }
b7009474 160
78946b9b
PS
161 readfile($csspath);
162 die;
b7009474 163}
e10f304d 164
045f492c
SH
165function minify($files) {
166 if (0 === stripos(PHP_OS, 'win')) {
167 Minify::setDocRoot(); // IIS may need help
168 }
e1c2a211
PS
169 // disable all caching, we do it in moodle
170 Minify::setCache(null, false);
045f492c
SH
171
172 $options = array(
173 'bubbleCssImports' => false,
174 // Don't gzip content we just want text for storage
175 'encodeOutput' => false,
176 // Maximum age to cache, not used but required
177 'maxAge' => (60*60*24*20),
178 // The files to minify
179 'files' => $files,
180 // Turn orr URI rewriting
181 'rewriteCssUris' => false,
182 // This returns the CSS rather than echoing it for display
183 'quiet' => true
184 );
185 $result = Minify::serve('Files', $options);
186 return $result['content'];
187}