3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file is responsible for serving the one huge CSS of each theme.
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 // we need just the values from config.php and minlib.php
28 define('ABORT_AFTER_CONFIG', true);
29 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
31 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32 $type = min_optional_param('type', 'all', 'SAFEDIR');
33 $rev = min_optional_param('rev', 0, 'INT');
35 if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
36 header('HTTP/1.0 404 not found');
37 die('Theme was not found, sorry.');
40 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
42 } else if (!empty($CFG->themedir
) and file_exists("$CFG->themedir/$themename/config.php")) {
45 header('HTTP/1.0 404 not found');
46 die('Theme was not found, sorry.');
50 send_ie_css($themename, $rev);
53 $candidatesheet = "$CFG->dataroot/cache/theme/$themename/css/$type.css";
55 if (file_exists($candidatesheet)) {
56 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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
59 $lifetime = 60*60*24*30; // 30 days
60 header('HTTP/1.1 304 Not Modified');
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');
66 send_cached_css($candidatesheet, $rev);
69 //=================================================================================
70 // ok, now we need to start normal moodle script, we need to load all libs and $DB
71 define('ABORT_AFTER_CONFIG_CANCEL', true);
73 define('NO_MOODLE_COOKIES', true); // Session not used here
74 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
76 require("$CFG->dirroot/lib/setup.php");
78 set_include_path($CFG->libdir
. '/minify/lib' . PATH_SEPARATOR
. get_include_path());
79 require_once('Minify.php');
81 $theme = theme_config
::load($themename);
83 if ($type === 'editor') {
84 $files = $theme->editor_css_files();
85 store_css($theme, $candidatesheet, $files);
87 $css = $theme->css_files();
89 foreach ($css as $key=>$value) {
91 foreach($value as $val) {
93 foreach ($val as $k=>$v) {
100 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/$key.css";
101 store_css($theme, $cssfile, $cssfiles);
102 $allfiles = array_merge($allfiles, $cssfiles);
104 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/all.css";
105 store_css($theme, $cssfile, $allfiles);
107 send_cached_css($candidatesheet, $rev);
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.
114 function store_css(theme_config
$theme, $csspath, $cssfiles) {
115 $css = $theme->post_process(minify($cssfiles));
116 check_dir_exists(dirname($csspath));
117 $fp = fopen($csspath, 'w');
122 function send_ie_css($themename, $rev) {
123 $lifetime = 60*60*24*30; // 30 days
126 /** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/
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);
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');
138 header('Cache-Control: max-age='.$lifetime);
139 header('Accept-Ranges: none');
140 header('Content-Type: text/css; charset=utf-8');
141 header('Content-Length: '.strlen($css));
147 function send_cached_css($csspath, $rev) {
148 $lifetime = 60*60*24*30; // 30 days
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');
154 header('Cache-Control: max-age='.$lifetime);
155 header('Accept-Ranges: none');
156 header('Content-Type: text/css; charset=utf-8');
157 if (!min_enable_zlib_compression()) {
158 header('Content-Length: '.filesize($csspath));
165 function minify($files) {
166 if (0 === stripos(PHP_OS
, 'win')) {
167 Minify
::setDocRoot(); // IIS may need help
169 // disable all caching, we do it in moodle
170 Minify
::setCache(null, false);
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
180 // Turn orr URI rewriting
181 'rewriteCssUris' => false,
182 // This returns the CSS rather than echoing it for display
185 $result = Minify
::serve('Files', $options);
186 return $result['content'];