forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
styles_debug.php
135 lines (112 loc) · 4.62 KB
/
styles_debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is responsible for serving of individual style sheets in designer mode.
*
* @package moodlecore
* @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('ABORT_AFTER_CONFIG', true);
require('../config.php'); // this stops immediately at the beginning of lib/setup.php
$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
$type = min_optional_param('type', '', 'SAFEDIR');
$subtype = min_optional_param('subtype', '', 'SAFEDIR');
$sheet = min_optional_param('sheet', '', 'SAFEDIR');
if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
}
if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
// exists
} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
// exists
} else {
css_not_found();
}
// no gzip compression when debugging
$candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";
if (!file_exists($candidatesheet)) {
css_not_found();
}
if (!$css = file_get_contents($candidatesheet)) {
css_not_found();
}
$css = unserialize($css);
if ($type === 'ie') {
// IE is a sloppy browser with weird limits, sorry
if ($subtype === 'plugins') {
$sendcss = implode("\n\n", $css['plugins']);
$sendcss = str_replace("\n", "\r\n", $sendcss);
send_uncached_css($sendcss);
} else if ($subtype === 'parents') {
$sendcss = array();
if (empty($sheet)) {
// If not specific parent has been specified as $sheet then build a
// collection of @import statements into this one sheet.
// We shouldn't ever actually get here, but none the less we'll deal
// with it incase we ever do.
// @import statements arn't processed until after concurrent CSS requests
// making them slightly evil.
foreach (array_keys($css['parents']) as $sheet) {
$sendcss[] = "@import url(styles_debug.php?theme=$themename&type=$type&subtype=$subtype&sheet=$sheet);";
}
} else {
// Build up the CSS for that parent so we can serve it as one file.
foreach ($css[$subtype][$sheet] as $parent=>$css) {
$sendcss[] = $css;
}
}
$sendcss = implode("\n\n", $sendcss);
$sendcss = str_replace("\n", "\r\n", $sendcss);
send_uncached_css($sendcss);
} else if ($subtype === 'theme') {
$sendcss = implode("\n\n", $css['theme']);
$sendcss = str_replace("\n", "\r\n", $sendcss);
send_uncached_css($sendcss);
}
} else if ($type === 'plugin') {
if (isset($css['plugins'][$subtype])) {
send_uncached_css($css['plugins'][$subtype]);
}
} else if ($type === 'parent') {
if (isset($css['parents'][$subtype][$sheet])) {
send_uncached_css($css['parents'][$subtype][$sheet], 30); // parent sheets are not supposed to change much, right?
}
} else if ($type === 'theme') {
if (isset($css['theme'][$sheet])) {
send_uncached_css($css['theme'][$sheet]);
}
}
css_not_found();
//=================================================================================
//=== utility functions ==
// we are not using filelib because we need to fine tune all header
// parameters to get the best performance.
function send_uncached_css($css) {
header('Content-Disposition: inline; filename="styles_debug.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME) .' GMT');
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: text/css');
//header('Content-Length: '.strlen($css));
echo($css);
die;
}
function css_not_found() {
header('HTTP/1.0 404 not found');
die('CSS was not found, sorry.');
}