weekly release 2.2.5+
[moodle.git] / theme / styles.php
blob67385e3de098ab14ff95ba360d15322f8e881ff9
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for serving the one huge CSS of each theme.
21 * @package moodlecore
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 // disable moodle specific debug messages and any errors in output,
28 // comment out when debugging or better look into error log!
29 define('NO_DEBUG_DISPLAY', true);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
36 $type = min_optional_param('type', 'all', 'SAFEDIR');
37 $rev = min_optional_param('rev', 0, 'INT');
39 if (!in_array($type, array('all', 'ie', 'editor', 'plugins', 'parents', 'theme'))) {
40 header('HTTP/1.0 404 not found');
41 die('Theme was not found, sorry.');
44 if (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 {
49 header('HTTP/1.0 404 not found');
50 die('Theme was not found, sorry.');
53 if ($type === 'ie') {
54 send_ie_css($themename, $rev);
57 $candidatesheet = "$CFG->cachedir/theme/$themename/css/$type.css";
59 if (file_exists($candidatesheet)) {
60 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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
63 $lifetime = 60*60*24*30; // 30 days
64 header('HTTP/1.1 304 Not Modified');
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');
68 die;
70 send_cached_css($candidatesheet, $rev);
73 //=================================================================================
74 // ok, now we need to start normal moodle script, we need to load all libs and $DB
75 define('ABORT_AFTER_CONFIG_CANCEL', true);
77 define('NO_MOODLE_COOKIES', true); // Session not used here
78 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
80 require("$CFG->dirroot/lib/setup.php");
81 // setup include path
82 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
83 require_once('Minify.php');
85 $theme = theme_config::load($themename);
87 if ($type === 'editor') {
88 $files = $theme->editor_css_files();
89 store_css($theme, $candidatesheet, $files);
90 } else {
91 $css = $theme->css_files();
92 $allfiles = array();
93 foreach ($css as $key=>$value) {
94 $cssfiles = array();
95 foreach($value as $val) {
96 if (is_array($val)) {
97 foreach ($val as $k=>$v) {
98 $cssfiles[] = $v;
100 } else {
101 $cssfiles[] = $val;
104 $cssfile = "$CFG->cachedir/theme/$themename/css/$key.css";
105 store_css($theme, $cssfile, $cssfiles);
106 $allfiles = array_merge($allfiles, $cssfiles);
108 $cssfile = "$CFG->cachedir/theme/$themename/css/all.css";
109 store_css($theme, $cssfile, $allfiles);
111 send_cached_css($candidatesheet, $rev);
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.
118 function store_css(theme_config $theme, $csspath, $cssfiles) {
119 $css = $theme->post_process(minify($cssfiles));
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();
123 check_dir_exists(dirname($csspath));
124 $fp = fopen($csspath, 'w');
125 fwrite($fp, $css);
126 fclose($fp);
129 function send_ie_css($themename, $rev) {
130 $lifetime = 60*60*24*30; // 30 days
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 :-( **/
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);
138 EOF;
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: ');
145 header('Cache-Control: max-age='.$lifetime);
146 header('Accept-Ranges: none');
147 header('Content-Type: text/css; charset=utf-8');
148 header('Content-Length: '.strlen($css));
150 echo $css;
151 die;
154 function send_cached_css($csspath, $rev) {
155 $lifetime = 60*60*24*30; // 30 days
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: ');
161 header('Cache-Control: max-age='.$lifetime);
162 header('Accept-Ranges: none');
163 header('Content-Type: text/css; charset=utf-8');
164 if (!min_enable_zlib_compression()) {
165 header('Content-Length: '.filesize($csspath));
168 readfile($csspath);
169 die;
172 function minify($files) {
173 if (0 === stripos(PHP_OS, 'win')) {
174 Minify::setDocRoot(); // IIS may need help
176 // disable all caching, we do it in moodle
177 Minify::setCache(null, false);
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
192 $result = Minify::serve('Files', $options);
193 return $result['content'];