forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yui_combo.php
233 lines (203 loc) · 8.3 KB
/
yui_combo.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?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 yui images
*
* @package moodlecore
* @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// disable moodle specific debug messages and any errors in output,
// comment out when debugging or better look into error log!
define('NO_DEBUG_DISPLAY', true);
// we need just the values from config.php and minlib.php
define('ABORT_AFTER_CONFIG', true);
require('../config.php'); // this stops immediately at the beginning of lib/setup.php
// get special url parameters
list($parts, $slasharguments) = combo_params();
if (!$parts) {
combo_not_found();
}
$etag = sha1($parts);
$parts = trim($parts, '&');
// find out what we are serving - only one type per request
$content = '';
if (substr($parts, -3) === '.js') {
$mimetype = 'application/javascript';
} else if (substr($parts, -4) === '.css') {
$mimetype = 'text/css';
} else {
combo_not_found();
}
// if they are requesting a revision that's not -1, and they have supplied an
// If-Modified-Since header, we can send back a 304 Not Modified since the
// content never changes (the rev number is increased any time the content changes)
if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
$lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
header('HTTP/1.1 304 Not Modified');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Cache-Control: public, max-age='.$lifetime);
header('Content-Type: '.$mimetype);
header('Etag: '.$etag);
die;
}
$parts = explode('&', $parts);
$cache = true;
$lastmodified = 0;
foreach ($parts as $part) {
if (empty($part)) {
continue;
}
$part = min_clean_param($part, 'SAFEPATH');
$bits = explode('/', $part);
if (count($bits) < 2) {
$content .= "\n// Wrong combo resource $part!\n";
continue;
}
//debug($bits);
$version = array_shift($bits);
if ($version == 'moodle') {
//TODO: this is a ugly hack because we should not load any libs here!
if (!defined('MOODLE_INTERNAL')) {
define('MOODLE_INTERNAL', true);
require_once($CFG->libdir.'/moodlelib.php');
}
$revision = (int)array_shift($bits);
if ($revision === -1) {
// Revision -1 says please don't cache the JS
$cache = false;
}
$frankenstyle = array_shift($bits);
$filename = array_pop($bits);
$dir = get_component_directory($frankenstyle);
if ($mimetype == 'text/css') {
$bits[] = 'assets';
$bits[] = 'skins';
$bits[] = 'sam';
}
$contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
} else {
if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
$content .= "\n// Wrong yui version $part!\n";
continue;
}
$contentfile = "$CFG->libdir/yui/$part";
}
if (!file_exists($contentfile) or !is_file($contentfile)) {
$content .= "\n// Combo resource $part ($contentfile) not found!\n";
continue;
}
$filecontent = file_get_contents($contentfile);
$fmodified = filemtime($contentfile);
if ($fmodified > $lastmodified) {
$lastmodified = $fmodified;
}
$relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
$sep = ($slasharguments ? '/' : '?file=');
if ($mimetype === 'text/css') {
if ($version == 'moodle') {
$filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
} else if ($version == 'gallery') {
// search for all images in gallery module CSS and serve them through the yui_image.php script
$filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
} else {
// First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
// I've added this as a separate regex so it can be easily removed once
// YUI standardise there CSS methods
$filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
// search for all images in yui2 CSS and serve them through the yui_image.php script
$filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
}
}
$content .= $filecontent;
}
if ($lastmodified == 0) {
$lastmodified = time();
}
if ($cache) {
combo_send_cached($content, $mimetype, $etag, $lastmodified);
} else {
combo_send_uncached($content, $mimetype);
}
/**
* Send the JavaScript cached
* @param string $content
* @param string $mimetype
* @param string $etag
* @param int $lastmodified
*/
function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
$lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
header('Content-Disposition: inline; filename="combo"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
header('Cache-Control: public, max-age='.$lifetime);
header('Accept-Ranges: none');
header('Content-Type: '.$mimetype);
header('Etag: '.$etag);
if (!min_enable_zlib_compression()) {
header('Content-Length: '.strlen($content));
}
echo $content;
die;
}
/**
* Send the JavaScript uncached
* @param string $content
* @param string $mimetype
*/
function combo_send_uncached($content, $mimetype) {
header('Content-Disposition: inline; filename="combo"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: '.$mimetype);
if (!min_enable_zlib_compression()) {
header('Content-Length: '.strlen($content));
}
echo $content;
die;
}
function combo_not_found($message = '') {
header('HTTP/1.0 404 not found');
if ($message) {
echo $message;
} else {
echo 'Combo resource not found, sorry.';
}
die;
}
function combo_params() {
if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
// url rewriting
$slashargument = substr($_SERVER['QUERY_STRING'], 6);
return array($slashargument, true);
} else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
return array($parts[1], false);
} else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
// note: buggy or misconfigured IIS does return the query string in REQUEST_URI
return array($_SERVER['QUERY_STRING'], false);
} else if ($slashargument = min_get_slash_argument()) {
$slashargument = ltrim($slashargument, '/');
return array($slashargument, true);
} else {
// unsupported server, sorry!
combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
}
}