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'])) {
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 header('HTTP/1.1 304 Not Modified');
62 send_cached_css($candidatesheet, $rev);
65 //=================================================================================
66 // ok, now we need to start normal moodle script, we need to load all libs and $DB
67 define('ABORT_AFTER_CONFIG_CANCEL', true);
69 define('NO_MOODLE_COOKIES', true); // Session not used here
70 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
72 require("$CFG->dirroot/lib/setup.php");
74 $theme = theme_config::load($themename);
76 if ($type === 'editor') {
77 $css = $theme->editor_css_content();
78 store_css($candidatesheet, $css);
80 $css = $theme->css_content();
81 foreach ($css as $key=>$value) {
83 foreach($value as $val) {
85 foreach ($val as $k=>$v) {
86 $sheet .= compress_css($v)."\n";
89 $sheet .= compress_css($val)."\n";
93 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/$key.css";
94 store_css($cssfile, $sheet);
96 $css = implode('', $css);
97 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/all.css";
98 store_css($cssfile, $css);
101 send_cached_css($candidatesheet, $rev);
104 //=================================================================================
105 //=== utility functions ==
106 // we are not using filelib because we need to fine tune all header
107 // parameters to get the best performance.
109 function store_css($csspath, $css) {
110 check_dir_exists(dirname($csspath), true, true);
111 $fp = fopen($csspath, 'w');
116 function send_ie_css($themename, $rev) {
117 $lifetime = 60*60*24*3;
120 /** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/
121 @import url(styles.php?theme=$themename&rev=$rev&type=plugins);
122 @import url(styles.php?theme=$themename&rev=$rev&type=parents);
123 @import url(styles.php?theme=$themename&rev=$rev&type=theme);
127 header('Etag: '.md5($rev));
128 header('Content-Disposition: inline; filename="styles.php"');
129 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
130 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
132 header('Accept-Ranges: none');
133 header('Content-Type: text/css');
134 header('Content-Length: '.strlen($css));
140 function send_cached_css($csspath, $rev) {
141 $lifetime = 60*60*24*20;
143 header('Content-Disposition: inline; filename="styles.php"');
144 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
145 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
147 header('Accept-Ranges: none');
148 header('Content-Type: text/css');
149 if (!min_enable_zlib_compression()) {
150 header('Content-Length: '.filesize($csspath));
157 function compress_css($css) {
158 // remove comments - credit for this regex goes to "reinhold weber"
159 $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
161 // replace newlines, tabs, etc.
162 $css = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $css);
164 // replace repeated spaces
165 $css = preg_replace('/ +/', ' ', $css);
167 // fix whitespace around separators
168 $css = preg_replace('/ ([;,:\{\}])/', '$1', $css);
169 $css = preg_replace('/([;,:\{\}]) /', '$1', $css);
171 $css = str_replace('url (', 'url(', $css);