Commit | Line | Data |
---|---|---|
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 | ||
78946b9b PS |
26 | // we need just the values from config.php and minlib.php |
27 | define('ABORT_AFTER_CONFIG', true); | |
28 | require('../config.php'); // this stops immediately at the beginning of lib/setup.php | |
b7009474 | 29 | |
78946b9b PS |
30 | $themename = min_optional_param('theme', 'standard', 'SAFEDIR'); |
31 | $type = min_optional_param('type', 'all', 'SAFEDIR'); | |
32 | $rev = min_optional_param('rev', 0, 'INT'); | |
b7009474 | 33 | |
78946b9b PS |
34 | if (!in_array($type, array('all', 'ie', 'editor', 'yui', 'plugins', 'parents', 'theme'))) { |
35 | header('HTTP/1.0 404 not found'); | |
36 | die('Theme was not found, sorry.'); | |
b7009474 | 37 | } |
38 | ||
78946b9b PS |
39 | if (!file_exists("$CFG->dirroot/theme/$themename/config.php") and !file_exists("$CFG->dataroot/theme/$themename/config.php")) { |
40 | header('HTTP/1.0 404 not found'); | |
41 | die('Theme was not found, sorry.'); | |
42 | } | |
b7009474 | 43 | |
78946b9b PS |
44 | if ($type === 'ie') { |
45 | send_ie_css($themename, $rev); | |
46 | } | |
b7009474 | 47 | |
78946b9b | 48 | $candidatesheet = "$CFG->dataroot/cache/theme/$themename/css/$type.css"; |
b7009474 | 49 | |
78946b9b PS |
50 | if (file_exists($candidatesheet)) { |
51 | if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) { | |
52 | // we do not actually need to verify the etag value because our files | |
53 | // never change in cache because we increment the rev parameter | |
54 | header('HTTP/1.1 304 Not Modified'); | |
55 | die; | |
56 | } | |
57 | send_cached_css($candidatesheet, $rev); | |
b7009474 | 58 | } |
59 | ||
78946b9b PS |
60 | //================================================================================= |
61 | // ok, now we need to start normal moodle script, we need to load all libs and $DB | |
62 | define('ABORT_AFTER_CONFIG_CANCEL', true); | |
fdeb7fa1 | 63 | |
78946b9b PS |
64 | define('NO_MOODLE_COOKIES', true); // Session not used here |
65 | define('NO_UPGRADE_CHECK', true); // Ignore upgrade check | |
fdeb7fa1 | 66 | |
78946b9b | 67 | require("$CFG->dirroot/lib/setup.php"); |
b7009474 | 68 | |
78946b9b | 69 | $theme = theme_config::load($themename); |
fdeb7fa1 | 70 | |
78946b9b PS |
71 | if ($type === 'editor') { |
72 | $css = $theme->editor_css_content(); | |
73 | store_css($candidatesheet, $css); | |
74 | } else { | |
75 | $css = $theme->css_content(); | |
76 | foreach ($css as $key=>$value) { | |
77 | $sheet = ''; | |
78 | foreach($value as $val) { | |
79 | if (is_array($val)) { | |
80 | $sheet .= "\n\n".implode("\n\n", $val); | |
81 | } else { | |
82 | $sheet .= "\n\n".$val; | |
83 | } | |
84 | } | |
85 | $css[$key] = $sheet; | |
86 | $cssfile = "$CFG->dataroot/cache/theme/$themename/css/$key.css"; | |
87 | store_css($cssfile, $sheet); | |
fdeb7fa1 | 88 | } |
78946b9b PS |
89 | $css = implode('', $css); |
90 | $cssfile = "$CFG->dataroot/cache/theme/$themename/css/all.css"; | |
91 | store_css($cssfile, $css); | |
fdeb7fa1 | 92 | } |
93 | ||
78946b9b | 94 | send_cached_css($candidatesheet, $rev); |
b7009474 | 95 | |
b7009474 | 96 | |
78946b9b PS |
97 | //================================================================================= |
98 | //=== utility functions == | |
99 | // we are not using filelib because we need to fine tune all header | |
100 | // parameters to get the best performance. | |
b7009474 | 101 | |
78946b9b PS |
102 | function store_css($csspath, $css) { |
103 | check_dir_exists(dirname($csspath), true, true); | |
104 | $fp = fopen($csspath, 'w'); | |
105 | fwrite($fp, $css); | |
106 | fclose($fp); | |
b7009474 | 107 | } |
108 | ||
78946b9b PS |
109 | function send_ie_css($themename, $rev) { |
110 | $lifetime = 60*60*24*3; | |
111 | ||
112 | $css = <<<EOF | |
113 | /** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/ | |
114 | @import url(styles.php?theme=$themename&rev=$rev&type=yui); | |
115 | @import url(styles.php?theme=$themename&rev=$rev&type=plugins); | |
116 | @import url(styles.php?theme=$themename&rev=$rev&type=parents); | |
117 | @import url(styles.php?theme=$themename&rev=$rev&type=theme); | |
118 | ||
119 | EOF; | |
120 | ||
121 | header('Etag: '.md5($rev)); | |
122 | header('Content-Disposition: inline; filename="styles.php"'); | |
123 | header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); | |
124 | header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); | |
125 | header('Pragma: '); | |
126 | header('Accept-Ranges: none'); | |
127 | header('Content-Type: text/css'); | |
128 | header('Content-Length: '.strlen($css)); | |
129 | ||
130 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite | |
131 | echo $css; | |
132 | die; | |
b7009474 | 133 | } |
b7009474 | 134 | |
78946b9b PS |
135 | function send_cached_css($csspath, $rev) { |
136 | $lifetime = 60*60*24*20; | |
b7009474 | 137 | |
78946b9b PS |
138 | header('Content-Disposition: inline; filename="styles.php"'); |
139 | header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT'); | |
140 | header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); | |
141 | header('Pragma: '); | |
142 | header('Accept-Ranges: none'); | |
143 | header('Content-Type: text/css'); | |
144 | header('Content-Length: '.filesize($csspath)); | |
b7009474 | 145 | |
78946b9b PS |
146 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
147 | readfile($csspath); | |
148 | die; | |
b7009474 | 149 | } |