weekly release 2.3dev (blame stronk7 for 0202 mistake)
[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
7829cf79
PS
27define('ABORT_AFTER_CONFIG', true);
28require('../config.php'); // this stops immediately at the beginning of lib/setup.php
78946b9b 29
7829cf79 30$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
4399b9d5 31$type = min_optional_param('type', '', 'SAFEDIR');
7829cf79
PS
32$subtype = min_optional_param('subtype', '', 'SAFEDIR');
33$sheet = min_optional_param('sheet', '', 'SAFEDIR');
78946b9b 34
d6b64d15
PS
35if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
36 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
37}
38
73e504bc
PS
39if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
40 // exists
41} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
42 // exists
43} else {
78946b9b
PS
44 css_not_found();
45}
46
7c986f04
PS
47// no gzip compression when debugging
48
365bec4c 49$candidatesheet = "$CFG->cachedir/theme/$themename/designer.ser";
7829cf79
PS
50
51if (!file_exists($candidatesheet)) {
78946b9b
PS
52 css_not_found();
53}
54
7829cf79
PS
55if (!$css = file_get_contents($candidatesheet)) {
56 css_not_found();
78946b9b
PS
57}
58
7829cf79 59$css = unserialize($css);
78946b9b 60
71be124d
SH
61if ($type === 'editor') {
62 if (isset($css['editor'])) {
63 send_uncached_css(implode("\n\n", $css['editor']));
64 }
65} else if ($type === 'ie') {
4399b9d5
PS
66 // IE is a sloppy browser with weird limits, sorry
67 if ($subtype === 'plugins') {
68 $sendcss = implode("\n\n", $css['plugins']);
69 $sendcss = str_replace("\n", "\r\n", $sendcss);
70 send_uncached_css($sendcss);
71
72 } else if ($subtype === 'parents') {
73 $sendcss = array();
a6338a13
SH
74 if (empty($sheet)) {
75 // If not specific parent has been specified as $sheet then build a
76 // collection of @import statements into this one sheet.
77 // We shouldn't ever actually get here, but none the less we'll deal
78 // with it incase we ever do.
79 // @import statements arn't processed until after concurrent CSS requests
80 // making them slightly evil.
81 foreach (array_keys($css['parents']) as $sheet) {
82 $sendcss[] = "@import url(styles_debug.php?theme=$themename&type=$type&subtype=$subtype&sheet=$sheet);";
83 }
84 } else {
85 // Build up the CSS for that parent so we can serve it as one file.
86 foreach ($css[$subtype][$sheet] as $parent=>$css) {
87 $sendcss[] = $css;
88 }
4399b9d5
PS
89 }
90 $sendcss = implode("\n\n", $sendcss);
91 $sendcss = str_replace("\n", "\r\n", $sendcss);
92 send_uncached_css($sendcss);
93
94 } else if ($subtype === 'theme') {
95 $sendcss = implode("\n\n", $css['theme']);
96 $sendcss = str_replace("\n", "\r\n", $sendcss);
97 send_uncached_css($sendcss);
98 }
c09e1a2c
PS
99
100} else if ($type === 'plugin') {
78946b9b
PS
101 if (isset($css['plugins'][$subtype])) {
102 send_uncached_css($css['plugins'][$subtype]);
103 }
104
105} else if ($type === 'parent') {
106 if (isset($css['parents'][$subtype][$sheet])) {
71be124d 107 send_uncached_css($css['parents'][$subtype][$sheet]);
78946b9b
PS
108 }
109
110} else if ($type === 'theme') {
111 if (isset($css['theme'][$sheet])) {
112 send_uncached_css($css['theme'][$sheet]);
113 }
114}
115css_not_found();
116
117//=================================================================================
118//=== utility functions ==
119// we are not using filelib because we need to fine tune all header
120// parameters to get the best performance.
121
c09e1a2c 122function send_uncached_css($css) {
78946b9b
PS
123 header('Content-Disposition: inline; filename="styles_debug.php"');
124 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
c09e1a2c 125 header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME) .' GMT');
78946b9b
PS
126 header('Pragma: ');
127 header('Accept-Ranges: none');
8a7703ce 128 header('Content-Type: text/css; charset=utf-8');
7c986f04 129 //header('Content-Length: '.strlen($css));
78946b9b 130
78946b9b
PS
131 echo($css);
132 die;
133}
134
135function css_not_found() {
136 header('HTTP/1.0 404 not found');
137 die('CSS was not found, sorry.');
c09e1a2c
PS
138}
139