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 of individual style sheets in designer mode.
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 define('ABORT_AFTER_CONFIG', true);
28 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
30 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
31 $type = min_optional_param('type', '', 'SAFEDIR');
32 $subtype = min_optional_param('subtype', '', 'SAFEDIR');
33 $sheet = min_optional_param('sheet', '', 'SAFEDIR');
35 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
36 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
39 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
41 } else if (!empty($CFG->themedir
) and file_exists("$CFG->themedir/$themename/config.php")) {
47 // no gzip compression when debugging
49 $candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";
51 if (!file_exists($candidatesheet)) {
55 if (!$css = file_get_contents($candidatesheet)) {
59 $css = unserialize($css);
62 // IE is a sloppy browser with weird limits, sorry
63 if ($subtype === 'plugins') {
64 $sendcss = implode("\n\n", $css['plugins']);
65 $sendcss = str_replace("\n", "\r\n", $sendcss);
66 send_uncached_css($sendcss);
68 } else if ($subtype === 'parents') {
71 // If not specific parent has been specified as $sheet then build a
72 // collection of @import statements into this one sheet.
73 // We shouldn't ever actually get here, but none the less we'll deal
74 // with it incase we ever do.
75 // @import statements arn't processed until after concurrent CSS requests
76 // making them slightly evil.
77 foreach (array_keys($css['parents']) as $sheet) {
78 $sendcss[] = "@import url(styles_debug.php?theme=$themename&type=$type&subtype=$subtype&sheet=$sheet);";
81 // Build up the CSS for that parent so we can serve it as one file.
82 foreach ($css[$subtype][$sheet] as $parent=>$css) {
86 $sendcss = implode("\n\n", $sendcss);
87 $sendcss = str_replace("\n", "\r\n", $sendcss);
88 send_uncached_css($sendcss);
90 } else if ($subtype === 'theme') {
91 $sendcss = implode("\n\n", $css['theme']);
92 $sendcss = str_replace("\n", "\r\n", $sendcss);
93 send_uncached_css($sendcss);
96 } else if ($type === 'plugin') {
97 if (isset($css['plugins'][$subtype])) {
98 send_uncached_css($css['plugins'][$subtype]);
101 } else if ($type === 'parent') {
102 if (isset($css['parents'][$subtype][$sheet])) {
103 send_uncached_css($css['parents'][$subtype][$sheet], 30); // parent sheets are not supposed to change much, right?
106 } else if ($type === 'theme') {
107 if (isset($css['theme'][$sheet])) {
108 send_uncached_css($css['theme'][$sheet]);
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 send_uncached_css($css) {
119 header('Content-Disposition: inline; filename="styles_debug.php"');
120 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
121 header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME
) .' GMT');
123 header('Accept-Ranges: none');
124 header('Content-Type: text/css');
125 //header('Content-Length: '.strlen($css));
131 function css_not_found() {
132 header('HTTP/1.0 404 not found');
133 die('CSS was not found, sorry.');