weekly release 3.7dev
[moodle.git] / theme / yui_combo.php
CommitLineData
60f2c866 1<?php
aa42314d
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
2ea00a58 18 * This file is responsible for serving of yui Javascript and CSS
aa42314d 19 *
574909ef 20 * @package core
aa42314d
PS
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
ee891dac
PS
26// disable moodle specific debug messages and any errors in output,
27// comment out when debugging or better look into error log!
28define('NO_DEBUG_DISPLAY', true);
29
aa42314d
PS
30// we need just the values from config.php and minlib.php
31define('ABORT_AFTER_CONFIG', true);
32require('../config.php'); // this stops immediately at the beginning of lib/setup.php
33
34// get special url parameters
d5222fae
PS
35
36list($parts, $slasharguments) = combo_params();
37if (!$parts) {
aa42314d
PS
38 combo_not_found();
39}
40
dbe14f39 41$etag = sha1($parts);
945f19f7
PS
42$parts = trim($parts, '&');
43
aa42314d
PS
44// find out what we are serving - only one type per request
45$content = '';
46if (substr($parts, -3) === '.js') {
a6338a13 47 $mimetype = 'application/javascript';
aa42314d
PS
48} else if (substr($parts, -4) === '.css') {
49 $mimetype = 'text/css';
50} else {
51 combo_not_found();
52}
53
ccc0fff9
TL
54// if they are requesting a revision that's not -1, and they have supplied an
55// If-Modified-Since header, we can send back a 304 Not Modified since the
56// content never changes (the rev number is increased any time the content changes)
14b1579a 57if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
dbe14f39 58 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa603b14 59 header('HTTP/1.1 304 Not Modified');
ccc0fff9 60 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 61 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9 62 header('Content-Type: '.$mimetype);
06eca486 63 header('Etag: "'.$etag.'"');
ccc0fff9
TL
64 die;
65}
66
aa42314d 67$parts = explode('&', $parts);
77387297 68$cache = true;
dbe14f39 69$lastmodified = 0;
aa42314d 70
eee4cbb7
AN
71while (count($parts)) {
72 $part = array_shift($parts);
945f19f7
PS
73 if (empty($part)) {
74 continue;
75 }
a45e8fd3 76 $filecontent = '';
aa42314d
PS
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
a4738eb3
PS
80 $content .= "\n// Wrong combo resource $part!\n";
81 continue;
aa42314d 82 }
2ea00a58 83
2b722f87 84 $version = array_shift($bits);
eee4cbb7 85 if ($version === 'rollup') {
0f722cfb
AN
86 $yuipatchedversion = explode('_', array_shift($bits));
87 $revision = $yuipatchedversion[0];
eee4cbb7
AN
88 $rollupname = array_shift($bits);
89
d17f25a5 90 if (strpos($rollupname, 'yui-moodlesimple') !== false) {
7d26038d 91 if (substr($rollupname, -3) === '.js') {
104d698f 92 // Determine which version of this rollup should be used.
7d26038d 93 $filesuffix = '.js';
104d698f 94 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
7d26038d 95 if (isset($matches[1])) {
104d698f 96 $filesuffix = $matches[0];
7d26038d 97 }
104d698f 98
7d26038d
PS
99 $type = 'js';
100 } else if (substr($rollupname, -4) === '.css') {
101 $type = 'css';
102 } else {
103 continue;
104 }
105
aea29737
AN
106 // Allow support for revisions on YUI between official releases.
107 // We can just discard the subrevision since it is only used to invalidate the browser cache.
108 $yuipatchedversion = explode('_', $revision);
109 $yuiversion = $yuipatchedversion[0];
110
eee4cbb7 111 $yuimodules = array(
eee4cbb7 112 'yui',
eee4cbb7
AN
113 'oop',
114 'event-custom-base',
115 'dom-core',
116 'dom-base',
117 'color-base',
118 'dom-style',
119 'selector-native',
120 'selector',
121 'node-core',
122 'node-base',
123 'event-base',
124 'event-base-ie',
125 'pluginhost-base',
126 'pluginhost-config',
127 'event-delegate',
128 'node-event-delegate',
129 'node-pluginhost',
130 'dom-screen',
131 'node-screen',
132 'node-style',
133 'querystring-stringify-simple',
134 'io-base',
135 'json-parse',
136 'transition',
137 'selector-css2',
138 'selector-css3',
139 'dom-style-ie',
140
141 // Some extras we use everywhere.
142 'escape',
c8b9f6d9
AN
143
144 'attribute-core',
145 'event-custom-complex',
146 'base-core',
147 'attribute-base',
148 'attribute-extras',
149 'attribute-observable',
150 'base-observable',
151 'base-base',
152 'base-pluginhost',
153 'base-build',
154 'event-synthetic',
155
156 'attribute-complex',
157 'event-mouseenter',
158 'event-key',
159 'event-outside',
c8b9f6d9
AN
160 'event-focus',
161 'classnamemanager',
162 'widget-base',
163 'widget-htmlparser',
164 'widget-skin',
165 'widget-uievents',
166 'widget-stdmod',
167 'widget-position',
168 'widget-position-align',
169 'widget-stack',
170 'widget-position-constrain',
171 'overlay',
172
c8b9f6d9
AN
173 'widget-autohide',
174 'button-core',
175 'button-plugin',
176 'widget-buttons',
177 'widget-modality',
178 'panel',
179 'yui-throttle',
180 'dd-ddm-base',
181 'dd-drag',
182 'dd-plugin',
9a45ac14
AN
183
184 // Cache is used by moodle-core-tooltip which we include everywhere.
052d64a4 185 'cache-base',
eee4cbb7
AN
186 );
187
188 // We need to add these new parts to the beginning of the $parts list, not the end.
7d26038d
PS
189 if ($type === 'js') {
190 $newparts = array();
191 foreach ($yuimodules as $module) {
aea29737 192 $newparts[] = $yuiversion . '/' . $module . '/' . $module . $filesuffix;
7d26038d
PS
193 }
194 $newparts[] = 'yuiuseall/yuiuseall';
195 $parts = array_merge($newparts, $parts);
196 } else {
197 $newparts = array();
198 foreach ($yuimodules as $module) {
aea29737 199 $candidate = $yuiversion . '/' . $module . '/assets/skins/sam/' . $module . '.css';
7d26038d
PS
200 if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
201 continue;
202 }
203 $newparts[] = $candidate;
204 }
205 if ($newparts) {
206 $parts = array_merge($newparts, $parts);
207 }
eee4cbb7 208 }
eee4cbb7
AN
209 }
210
eee4cbb7
AN
211 continue;
212 }
03e36e70
AN
213 if ($version === 'm') {
214 $version = 'moodle';
215 }
1c76d55a 216 if ($version === 'moodle') {
4f65d03b
ARN
217 if (count($bits) <= 3) {
218 // This is an invalid module load attempt.
219 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
220 continue;
221 }
77387297
SH
222 $revision = (int)array_shift($bits);
223 if ($revision === -1) {
224 // Revision -1 says please don't cache the JS
225 $cache = false;
226 }
2b722f87 227 $frankenstyle = array_shift($bits);
3b17690c 228 $filename = array_pop($bits);
a45e8fd3 229 $modulename = $bits[0];
cba94cec 230 $dir = core_component::get_component_directory($frankenstyle);
a45e8fd3
ARN
231
232 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
233 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
f0e79573 234 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
a45e8fd3 235
f320e8d0
AN
236 // Submodules are stored in a directory with the full submodule name.
237 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
c3faa40f 238 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
f320e8d0 239
a45e8fd3 240 // By default, try and use the /yui/build directory.
f320e8d0 241 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
f0e79573
ARN
242 if ($mimetype == 'text/css') {
243 // CSS assets are in a slightly different place to the JS.
f320e8d0 244 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
f0e79573
ARN
245
246 // Add the path to the bits to handle fallback for non-shifted assets.
247 $bits[] = 'assets';
248 $bits[] = 'skins';
249 $bits[] = 'sam';
250 } else {
f320e8d0 251 $contentfile = $contentfile . '/' . $frankenstylefilename;
f0e79573 252 }
a45e8fd3
ARN
253
254 // If the shifted versions don't exist, fall back to the non-shifted file.
255 if (!file_exists($contentfile) or !is_file($contentfile)) {
256 // We have to revert to the non-minified and non-debug versions.
257 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
258 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
259 }
1c76d55a
PS
260 } else if ($version === '2in3') {
261 $contentfile = "$CFG->libdir/yuilib/$part";
262
263 } else if ($version == 'gallery') {
5b49f8b3
AN
264 if (count($bits) <= 2) {
265 // This is an invalid module load attempt.
266 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
267 continue;
268 }
269 $revision = (int)array_shift($bits);
270 if ($revision === -1) {
271 // Revision -1 says please don't cache the JS
272 $cache = false;
273 }
274 $contentfile = "$CFG->libdir/yuilib/gallery/" . join('/', $bits);
1c76d55a 275
c8b9f6d9 276 } else if ($version == 'yuiuseall') {
2ea00a58
PS
277 // Create global Y that is available in global scope,
278 // this is the trick behind original SimpleYUI.
c8b9f6d9
AN
279 $filecontent = "var Y = YUI().use('*');";
280
2b722f87 281 } else {
aea29737
AN
282 // Allow support for revisions on YUI between official releases.
283 // We can just discard the subrevision since it is only used to invalidate the browser cache.
284 $yuipatchedversion = explode('_', $version);
285 $yuiversion = $yuipatchedversion[0];
286 if ($yuiversion != $CFG->yui3version) {
2b722f87
SH
287 $content .= "\n// Wrong yui version $part!\n";
288 continue;
289 }
aea29737
AN
290 $newpart = explode('/', $part);
291 $newpart[0] = $yuiversion;
292 $part = implode('/', $newpart);
1c76d55a 293 $contentfile = "$CFG->libdir/yuilib/$part";
aa42314d 294 }
aa42314d 295 if (!file_exists($contentfile) or !is_file($contentfile)) {
a2bdf340
PS
296 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
297 $content .= "\n// Combo resource $part ($location) not found!\n";
a4738eb3 298 continue;
aa42314d 299 }
a45e8fd3
ARN
300
301 if (empty($filecontent)) {
302 $filecontent = file_get_contents($contentfile);
303 }
dbe14f39
PS
304 $fmodified = filemtime($contentfile);
305 if ($fmodified > $lastmodified) {
306 $lastmodified = $fmodified;
307 }
aa42314d 308
6e7b4601 309 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
d5222fae 310 $sep = ($slasharguments ? '/' : '?file=');
6e7b4601 311
aa42314d 312 if ($mimetype === 'text/css') {
3b17690c 313 if ($version == 'moodle') {
a45e8fd3
ARN
314 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
315 $imagebits = array(
316 $sep . $version,
317 $frankenstyle,
318 $modulename,
319 array_shift($bits),
320 '$1.$2'
321 );
322
323 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
1c76d55a
PS
324 } else if ($version == '2in3') {
325 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
326 // I've added this as a separate regex so it can be easily removed once
327 // YUI standardise there CSS methods
328 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
329
330 // search for all images in yui2 CSS and serve them through the yui_image.php script
331 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
332
3b17690c 333 } else if ($version == 'gallery') {
5b49f8b3
AN
334 // Replace any references to the CDN with a relative link.
335 $filecontent = preg_replace('#(' . preg_quote('http://yui.yahooapis.com/') . '(gallery-[^/]*/))#', '../../../../', $filecontent);
336
337 // Replace all relative image links with the a link to yui_image.php.
338 $filecontent = preg_replace('#(' . preg_quote('../../../../') . ')(gallery-[^/]*/assets/skins/sam/[a-z0-9_-]+)\.(png|gif)#',
339 $relroot . '/theme/yui_image.php' . $sep . '/gallery/' . $revision . '/$2.$3', $filecontent);
1c76d55a 340
2a102b90 341 } else {
4d909122
SH
342 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
343 // I've added this as a separate regex so it can be easily removed once
344 // YUI standardise there CSS methods
6e6034e5 345 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d909122 346
2a102b90 347 // search for all images in yui2 CSS and serve them through the yui_image.php script
d5222fae 348 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
2a102b90 349 }
aa42314d
PS
350 }
351
352 $content .= $filecontent;
353}
354
dbe14f39
PS
355if ($lastmodified == 0) {
356 $lastmodified = time();
357}
358
77387297 359if ($cache) {
dbe14f39 360 combo_send_cached($content, $mimetype, $etag, $lastmodified);
77387297
SH
361} else {
362 combo_send_uncached($content, $mimetype);
363}
aa42314d
PS
364
365
77387297
SH
366/**
367 * Send the JavaScript cached
368 * @param string $content
369 * @param string $mimetype
dbe14f39
PS
370 * @param string $etag
371 * @param int $lastmodified
77387297 372 */
dbe14f39
PS
373function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
374 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa42314d
PS
375
376 header('Content-Disposition: inline; filename="combo"');
dbe14f39 377 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
aa42314d
PS
378 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
379 header('Pragma: ');
ed0a1cd7 380 header('Cache-Control: public, max-age='.$lifetime.', immutable');
aa42314d
PS
381 header('Accept-Ranges: none');
382 header('Content-Type: '.$mimetype);
06eca486 383 header('Etag: "'.$etag.'"');
7c986f04
PS
384 if (!min_enable_zlib_compression()) {
385 header('Content-Length: '.strlen($content));
386 }
aa42314d 387
aa42314d
PS
388 echo $content;
389 die;
390}
391
77387297
SH
392/**
393 * Send the JavaScript uncached
394 * @param string $content
395 * @param string $mimetype
396 */
397function combo_send_uncached($content, $mimetype) {
398 header('Content-Disposition: inline; filename="combo"');
399 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
400 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
401 header('Pragma: ');
402 header('Accept-Ranges: none');
403 header('Content-Type: '.$mimetype);
404 if (!min_enable_zlib_compression()) {
405 header('Content-Length: '.strlen($content));
406 }
407
408 echo $content;
409 die;
410}
411
37c82592 412function combo_not_found($message = '') {
aa42314d 413 header('HTTP/1.0 404 not found');
37c82592
PS
414 if ($message) {
415 echo $message;
416 } else {
417 echo 'Combo resource not found, sorry.';
418 }
419 die;
aa42314d
PS
420}
421
422function combo_params() {
18ac11b7
PS
423 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
424 // url rewriting
425 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
426 return array($slashargument, true);
427
428 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
37c82592 429 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
d5222fae 430 return array($parts[1], false);
aa42314d 431
6e7b4601 432 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
18ac11b7 433 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
d5222fae 434 return array($_SERVER['QUERY_STRING'], false);
aa42314d 435
0bb431e3 436 } else if ($slashargument = min_get_slash_argument(false)) {
6e7b4601 437 $slashargument = ltrim($slashargument, '/');
d5222fae 438 return array($slashargument, true);
6e7b4601 439
aa42314d 440 } else {
37c82592
PS
441 // unsupported server, sorry!
442 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314d
PS
443 }
444}