MDL-21208 longer image caching in theme designer mode (performance)
[moodle.git] / theme / styles_debug.php
CommitLineData
78946b9b
PS
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/**
19 * This file is responsible for serving of individual style sheets in designer mode.
20 *
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
24 */
25
73e504bc 26
78946b9b
PS
27// no chaching
28define('NO_MOODLE_COOKIES', true); // Session not used here
29define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
30require('../config.php');
31
32$themename = required_param('theme', PARAM_SAFEDIR);
33$type = required_param('type', PARAM_SAFEDIR);
34$subtype = optional_param('subtype', '', PARAM_SAFEDIR);
35$sheet = optional_param('sheet', '', PARAM_SAFEDIR);
36
73e504bc
PS
37if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
38 // exists
39} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
40 // exists
41} else {
78946b9b
PS
42 css_not_found();
43}
44
45if (theme_get_revision() > -1) {
46 //bad luck - this should not happen!
47 css_not_found();
48}
49
50$theme = theme_config::load($themename);
51
52if ($type === 'editor') {
53 $css = $theme->editor_css_content();
54 send_uncached_css($css);
55}
56
57$css = $theme->css_content();
58
9b2735f9
PS
59if ($type === 'yui2') {
60 send_uncached_css(reset($css['yui2']));
78946b9b
PS
61
62} else if ($type === 'plugin') {
63 if (isset($css['plugins'][$subtype])) {
64 send_uncached_css($css['plugins'][$subtype]);
65 }
66
67} else if ($type === 'parent') {
68 if (isset($css['parents'][$subtype][$sheet])) {
69 send_uncached_css($css['parents'][$subtype][$sheet]);
70 }
71
72} else if ($type === 'theme') {
73 if (isset($css['theme'][$sheet])) {
74 send_uncached_css($css['theme'][$sheet]);
75 }
76}
77css_not_found();
78
79//=================================================================================
80//=== utility functions ==
81// we are not using filelib because we need to fine tune all header
82// parameters to get the best performance.
83
84function send_uncached_css($css) {
85 header('Content-Disposition: inline; filename="styles_debug.php"');
86 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
87 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
88 header('Pragma: ');
89 header('Accept-Ranges: none');
90 header('Content-Type: text/css');
91 header('Content-Length: '.strlen($css));
92
93 while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
94 echo($css);
95 die;
96}
97
98function css_not_found() {
99 header('HTTP/1.0 404 not found');
100 die('CSS was not found, sorry.');
101}