MDL-25589 removing extra align value
[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
PS
55if (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');
60 die;
61 }
62 send_cached_css($candidatesheet, $rev);
b7009474 63}
64
78946b9b
PS
65//=================================================================================
66// ok, now we need to start normal moodle script, we need to load all libs and $DB
67define('ABORT_AFTER_CONFIG_CANCEL', true);
fdeb7fa1 68
78946b9b
PS
69define('NO_MOODLE_COOKIES', true); // Session not used here
70define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
fdeb7fa1 71
78946b9b 72require("$CFG->dirroot/lib/setup.php");
045f492c
SH
73// setup include path
74set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
75require_once('Minify.php');
b7009474 76
78946b9b 77$theme = theme_config::load($themename);
fdeb7fa1 78
78946b9b 79if ($type === 'editor') {
71be124d
SH
80 $files = $theme->editor_css_files();
81 store_css($theme, $candidatesheet, $files);
78946b9b 82} else {
045f492c
SH
83 $css = $theme->css_files();
84 $allfiles = array();
78946b9b 85 foreach ($css as $key=>$value) {
045f492c 86 $cssfiles = array();
78946b9b
PS
87 foreach($value as $val) {
88 if (is_array($val)) {
e10f304d 89 foreach ($val as $k=>$v) {
045f492c 90 $cssfiles[] = $v;
e10f304d 91 }
78946b9b 92 } else {
045f492c 93 $cssfiles[] = $val;
78946b9b
PS
94 }
95 }
78946b9b 96 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/$key.css";
045f492c
SH
97 store_css($theme, $cssfile, $cssfiles);
98 $allfiles = array_merge($allfiles, $cssfiles);
fdeb7fa1 99 }
78946b9b 100 $cssfile = "$CFG->dataroot/cache/theme/$themename/css/all.css";
045f492c 101 store_css($theme, $cssfile, $allfiles);
fdeb7fa1 102}
78946b9b 103send_cached_css($candidatesheet, $rev);
b7009474 104
78946b9b
PS
105//=================================================================================
106//=== utility functions ==
107// we are not using filelib because we need to fine tune all header
108// parameters to get the best performance.
b7009474 109
045f492c
SH
110function store_css(theme_config $theme, $csspath, $cssfiles) {
111 $css = $theme->post_process(minify($cssfiles));
c426ef3a 112 check_dir_exists(dirname($csspath));
78946b9b
PS
113 $fp = fopen($csspath, 'w');
114 fwrite($fp, $css);
115 fclose($fp);
b7009474 116}
117
78946b9b
PS
118function send_ie_css($themename, $rev) {
119 $lifetime = 60*60*24*3;
120
121 $css = <<<EOF
122/** 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
123@import url(styles.php?theme=$themename&rev=$rev&type=plugins);
124@import url(styles.php?theme=$themename&rev=$rev&type=parents);
125@import url(styles.php?theme=$themename&rev=$rev&type=theme);
126
127EOF;
128
129 header('Etag: '.md5($rev));
130 header('Content-Disposition: inline; filename="styles.php"');
131 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
132 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
133 header('Pragma: ');
134 header('Accept-Ranges: none');
135 header('Content-Type: text/css');
e10f304d 136 header('Content-Length: '.strlen($css));
78946b9b 137
78946b9b
PS
138 echo $css;
139 die;
b7009474 140}
b7009474 141
78946b9b
PS
142function send_cached_css($csspath, $rev) {
143 $lifetime = 60*60*24*20;
b7009474 144
78946b9b
PS
145 header('Content-Disposition: inline; filename="styles.php"');
146 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
147 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
148 header('Pragma: ');
149 header('Accept-Ranges: none');
150 header('Content-Type: text/css');
7c986f04
PS
151 if (!min_enable_zlib_compression()) {
152 header('Content-Length: '.filesize($csspath));
153 }
b7009474 154
78946b9b
PS
155 readfile($csspath);
156 die;
b7009474 157}
e10f304d 158
045f492c
SH
159function minify($files) {
160 if (0 === stripos(PHP_OS, 'win')) {
161 Minify::setDocRoot(); // IIS may need help
162 }
163 Minify::setCache('', true);
164
165 $options = array(
166 'bubbleCssImports' => false,
167 // Don't gzip content we just want text for storage
168 'encodeOutput' => false,
169 // Maximum age to cache, not used but required
170 'maxAge' => (60*60*24*20),
171 // The files to minify
172 'files' => $files,
173 // Turn orr URI rewriting
174 'rewriteCssUris' => false,
175 // This returns the CSS rather than echoing it for display
176 'quiet' => true
177 );
178 $result = Minify::serve('Files', $options);
179 return $result['content'];
180}