2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file is responsible for serving of yui Javascript and CSS
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34 // get special url parameters
36 list($parts, $slasharguments) = combo_params();
42 $parts = trim($parts, '&');
44 // find out what we are serving - only one type per request
46 if (substr($parts, -3) === '.js') {
47 $mimetype = 'application/javascript';
48 } else if (substr($parts, -4) === '.css') {
49 $mimetype = 'text/css';
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)
57 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
58 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
59 header('HTTP/1.1 304 Not Modified');
60 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
61 header('Cache-Control: public, max-age='.$lifetime);
62 header('Content-Type: '.$mimetype);
63 header('Etag: "'.$etag.'"');
67 $parts = explode('&', $parts);
71 while (count($parts)) {
72 $part = array_shift($parts);
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
80 $content .= "\n// Wrong combo resource $part!\n";
84 $version = array_shift($bits);
85 if ($version === 'rollup') {
86 $yuipatchedversion = explode('_', array_shift($bits));
87 $revision = $yuipatchedversion[0];
88 $rollupname = array_shift($bits);
90 if (strpos($rollupname, 'yui-moodlesimple') !== false) {
91 if (substr($rollupname, -3) === '.js') {
92 // Determine which version of this rollup should be used.
94 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
95 if (isset($matches[1])) {
96 $filesuffix = $matches[0];
100 } else if (substr($rollupname, -4) === '.css') {
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];
128 'node-event-delegate',
133 'querystring-stringify-simple',
141 // Some extras we use everywhere.
145 'event-custom-complex',
149 'attribute-observable',
168 'widget-position-align',
170 'widget-position-constrain',
184 // Cache is used by moodle-core-tooltip which we include everywhere.
188 // We need to add these new parts to the beginning of the $parts list, not the end.
189 if ($type === 'js') {
191 foreach ($yuimodules as $module) {
192 $newparts[] = $yuiversion . '/' . $module . '/' . $module . $filesuffix;
194 $newparts[] = 'yuiuseall/yuiuseall';
195 $parts = array_merge($newparts, $parts);
198 foreach ($yuimodules as $module) {
199 $candidate = $yuiversion . '/' . $module . '/assets/skins/sam/' . $module . '.css';
200 if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
203 $newparts[] = $candidate;
206 $parts = array_merge($newparts, $parts);
211 // Handle the mcore rollup.
212 if (strpos($rollupname, 'mcore') !== false) {
214 'core/tooltip/tooltip',
215 'core/popuphelp/popuphelp',
216 'core/widget-focusafterclose/widget-focusafterclose',
217 'core/dock/dock-loader',
218 'core/notification/notification-dialogue',
221 // Determine which version of this rollup should be used.
223 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
224 if (isset($matches[1])) {
225 $filesuffix = $matches[0];
228 // We need to add these new parts to the beginning of the $parts list, not the end.
230 foreach ($yuimodules as $module) {
231 $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
233 $parts = array_merge($newparts, $parts);
237 if ($version === 'm') {
240 if ($version === 'moodle') {
241 if (count($bits) <= 3) {
242 // This is an invalid module load attempt.
243 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
246 $revision = (int)array_shift($bits);
247 if ($revision === -1) {
248 // Revision -1 says please don't cache the JS
251 $frankenstyle = array_shift($bits);
252 $filename = array_pop($bits);
253 $modulename = $bits[0];
254 $dir = core_component
::get_component_directory($frankenstyle);
256 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
257 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
258 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
260 // Submodules are stored in a directory with the full submodule name.
261 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
262 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
264 // By default, try and use the /yui/build directory.
265 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
266 if ($mimetype == 'text/css') {
267 // CSS assets are in a slightly different place to the JS.
268 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
270 // Add the path to the bits to handle fallback for non-shifted assets.
275 $contentfile = $contentfile . '/' . $frankenstylefilename;
278 // If the shifted versions don't exist, fall back to the non-shifted file.
279 if (!file_exists($contentfile) or !is_file($contentfile)) {
280 // We have to revert to the non-minified and non-debug versions.
281 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
282 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
284 } else if ($version === '2in3') {
285 $contentfile = "$CFG->libdir/yuilib/$part";
287 } else if ($version == 'gallery') {
288 if (count($bits) <= 2) {
289 // This is an invalid module load attempt.
290 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
293 $revision = (int)array_shift($bits);
294 if ($revision === -1) {
295 // Revision -1 says please don't cache the JS
298 $contentfile = "$CFG->libdir/yuilib/gallery/" . join('/', $bits);
300 } else if ($version == 'yuiuseall') {
301 // Create global Y that is available in global scope,
302 // this is the trick behind original SimpleYUI.
303 $filecontent = "var Y = YUI().use('*');";
306 // Allow support for revisions on YUI between official releases.
307 // We can just discard the subrevision since it is only used to invalidate the browser cache.
308 $yuipatchedversion = explode('_', $version);
309 $yuiversion = $yuipatchedversion[0];
310 if ($yuiversion != $CFG->yui3version
) {
311 $content .= "\n// Wrong yui version $part!\n";
314 $newpart = explode('/', $part);
315 $newpart[0] = $yuiversion;
316 $part = implode('/', $newpart);
317 $contentfile = "$CFG->libdir/yuilib/$part";
319 if (!file_exists($contentfile) or !is_file($contentfile)) {
320 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot
, '/').'/', '', $contentfile);
321 $content .= "\n// Combo resource $part ($location) not found!\n";
325 if (empty($filecontent)) {
326 $filecontent = file_get_contents($contentfile);
328 $fmodified = filemtime($contentfile);
329 if ($fmodified > $lastmodified) {
330 $lastmodified = $fmodified;
333 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot
);
334 $sep = ($slasharguments ?
'/' : '?file=');
336 if ($mimetype === 'text/css') {
337 if ($version == 'moodle') {
338 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
347 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
348 } else if ($version == '2in3') {
349 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
350 // I've added this as a separate regex so it can be easily removed once
351 // YUI standardise there CSS methods
352 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
354 // search for all images in yui2 CSS and serve them through the yui_image.php script
355 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version
.'/$1.$2', $filecontent);
357 } else if ($version == 'gallery') {
358 // Replace any references to the CDN with a relative link.
359 $filecontent = preg_replace('#(' . preg_quote('http://yui.yahooapis.com/') . '(gallery-[^/]*/))#', '../../../../', $filecontent);
361 // Replace all relative image links with the a link to yui_image.php.
362 $filecontent = preg_replace('#(' . preg_quote('../../../../') . ')(gallery-[^/]*/assets/skins/sam/[a-z0-9_-]+)\.(png|gif)#',
363 $relroot . '/theme/yui_image.php' . $sep . '/gallery/' . $revision . '/$2.$3', $filecontent);
366 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
367 // I've added this as a separate regex so it can be easily removed once
368 // YUI standardise there CSS methods
369 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
371 // search for all images in yui2 CSS and serve them through the yui_image.php script
372 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
376 $content .= $filecontent;
379 if ($lastmodified == 0) {
380 $lastmodified = time();
384 combo_send_cached($content, $mimetype, $etag, $lastmodified);
386 combo_send_uncached($content, $mimetype);
391 * Send the JavaScript cached
392 * @param string $content
393 * @param string $mimetype
394 * @param string $etag
395 * @param int $lastmodified
397 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
398 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
400 header('Content-Disposition: inline; filename="combo"');
401 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
402 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
404 header('Cache-Control: public, max-age='.$lifetime);
405 header('Accept-Ranges: none');
406 header('Content-Type: '.$mimetype);
407 header('Etag: "'.$etag.'"');
408 if (!min_enable_zlib_compression()) {
409 header('Content-Length: '.strlen($content));
417 * Send the JavaScript uncached
418 * @param string $content
419 * @param string $mimetype
421 function combo_send_uncached($content, $mimetype) {
422 header('Content-Disposition: inline; filename="combo"');
423 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
424 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
2) .' GMT');
426 header('Accept-Ranges: none');
427 header('Content-Type: '.$mimetype);
428 if (!min_enable_zlib_compression()) {
429 header('Content-Length: '.strlen($content));
436 function combo_not_found($message = '') {
437 header('HTTP/1.0 404 not found');
441 echo 'Combo resource not found, sorry.';
446 function combo_params() {
447 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
449 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
450 return array($slashargument, true);
452 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
453 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
454 return array($parts[1], false);
456 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
457 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
458 return array($_SERVER['QUERY_STRING'], false);
460 } else if ($slashargument = min_get_slash_argument(false)) {
461 $slashargument = ltrim($slashargument, '/');
462 return array($slashargument, true);
465 // unsupported server, sorry!
466 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');