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();
41 $parts = trim($parts, '&');
43 // Remove any duplicate parts, since each file only needs to be loaded once (which also helps reduce total file size).
44 $parts = implode('&', array_unique(explode('&', $parts)));
46 // Limit length of parts to match the YUI loader limit of 1024, to prevent loading an arbitrary number of files.
47 if (strlen($parts) > 1024) {
48 $parts = substr($parts, 0, 1024);
50 // If the shortened $parts has been cut off mid-way through a filename, trim back to the end of the previous filename.
51 if (substr($parts, -3) !== '.js' && substr($parts, -4) !== '.css') {
52 $parts = substr($parts, 0, strrpos($parts, '&'));
56 // find out what we are serving - only one type per request
58 if (substr($parts, -3) === '.js') {
59 $mimetype = 'application/javascript';
60 } else if (substr($parts, -4) === '.css') {
61 $mimetype = 'text/css';
68 // if they are requesting a revision that's not -1, and they have supplied an
69 // If-Modified-Since header, we can send back a 304 Not Modified since the
70 // content never changes (the rev number is increased any time the content changes)
71 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
72 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
73 header('HTTP/1.1 304 Not Modified');
74 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
75 header('Cache-Control: public, max-age='.$lifetime);
76 header('Content-Type: '.$mimetype);
77 header('Etag: "'.$etag.'"');
81 $parts = explode('&', $parts);
85 while (count($parts)) {
86 $part = array_shift($parts);
91 $part = min_clean_param($part, 'SAFEPATH');
92 $bits = explode('/', $part);
93 if (count($bits) < 2) {
94 $content .= "\n// Wrong combo resource $part!\n";
98 $version = array_shift($bits);
99 if ($version === 'rollup') {
100 $yuipatchedversion = explode('_', array_shift($bits));
101 $revision = $yuipatchedversion[0];
102 $rollupname = array_shift($bits);
104 if (strpos($rollupname, 'yui-moodlesimple') !== false) {
105 if (substr($rollupname, -3) === '.js') {
106 // Determine which version of this rollup should be used.
108 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
109 if (isset($matches[1])) {
110 $filesuffix = $matches[0];
114 } else if (substr($rollupname, -4) === '.css') {
120 // Allow support for revisions on YUI between official releases.
121 // We can just discard the subrevision since it is only used to invalidate the browser cache.
122 $yuipatchedversion = explode('_', $revision);
123 $yuiversion = $yuipatchedversion[0];
142 'node-event-delegate',
147 'querystring-stringify-simple',
155 // Some extras we use everywhere.
159 'event-custom-complex',
163 'attribute-observable',
182 'widget-position-align',
184 'widget-position-constrain',
199 // We need to add these new parts to the beginning of the $parts list, not the end.
200 if ($type === 'js') {
202 foreach ($yuimodules as $module) {
203 $newparts[] = $yuiversion . '/' . $module . '/' . $module . $filesuffix;
205 $newparts[] = 'yuiuseall/yuiuseall';
206 $parts = array_merge($newparts, $parts);
209 foreach ($yuimodules as $module) {
210 $candidate = $yuiversion . '/' . $module . '/assets/skins/sam/' . $module . '.css';
211 if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
214 $newparts[] = $candidate;
217 $parts = array_merge($newparts, $parts);
224 if ($version === 'm') {
227 if ($version === 'moodle') {
228 if (count($bits) <= 3) {
229 // This is an invalid module load attempt.
230 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
233 $revision = (int)array_shift($bits);
234 if (!min_is_revision_valid_and_current($revision)) {
235 // A non-current revision means please don't cache the JS
239 $frankenstyle = array_shift($bits);
240 $filename = array_pop($bits);
241 $modulename = $bits[0];
242 $dir = core_component
::get_component_directory($frankenstyle);
244 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
245 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
246 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
248 // Submodules are stored in a directory with the full submodule name.
249 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
250 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
252 // By default, try and use the /yui/build directory.
253 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
254 if ($mimetype == 'text/css') {
255 // CSS assets are in a slightly different place to the JS.
256 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
258 // Add the path to the bits to handle fallback for non-shifted assets.
263 $contentfile = $contentfile . '/' . $frankenstylefilename;
266 // If the shifted versions don't exist, fall back to the non-shifted file.
267 if (!file_exists($contentfile) or !is_file($contentfile)) {
268 // We have to revert to the non-minified and non-debug versions.
269 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
270 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
272 } else if ($version === '2in3') {
273 $contentfile = "$CFG->libdir/yuilib/$part";
275 } else if ($version == 'gallery') {
276 if (count($bits) <= 2) {
277 // This is an invalid module load attempt.
278 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
281 $revision = (int)array_shift($bits);
282 if (!min_is_revision_valid_and_current($revision)) {
283 // A non-current revision means please don't cache the JS
287 $contentfile = "$CFG->libdir/yuilib/gallery/" . join('/', $bits);
289 } else if ($version == 'yuiuseall') {
290 // Create global Y that is available in global scope,
291 // this is the trick behind original SimpleYUI.
292 $filecontent = "var Y = YUI().use('*');";
295 // Allow support for revisions on YUI between official releases.
296 // We can just discard the subrevision since it is only used to invalidate the browser cache.
297 $yuipatchedversion = explode('_', $version);
298 $yuiversion = $yuipatchedversion[0];
299 if ($yuiversion != $CFG->yui3version
) {
300 $content .= "\n// Wrong yui version $part!\n";
303 $newpart = explode('/', $part);
304 $newpart[0] = $yuiversion;
305 $part = implode('/', $newpart);
306 $contentfile = "$CFG->libdir/yuilib/$part";
308 if (!file_exists($contentfile) or !is_file($contentfile)) {
309 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot
, '/').'/', '', $contentfile);
310 $content .= "\n// Combo resource $part ($location) not found!\n";
314 if (empty($filecontent)) {
315 $filecontent = file_get_contents($contentfile);
317 $fmodified = filemtime($contentfile);
318 if ($fmodified > $lastmodified) {
319 $lastmodified = $fmodified;
322 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot
);
323 $sep = ($slasharguments ?
'/' : '?file=');
325 if ($mimetype === 'text/css') {
326 if ($version == 'moodle') {
327 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
336 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
337 } else if ($version == '2in3') {
338 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
339 // I've added this as a separate regex so it can be easily removed once
340 // YUI standardise there CSS methods
341 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
343 // search for all images in yui2 CSS and serve them through the yui_image.php script
344 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version
.'/$1.$2', $filecontent);
346 } else if ($version == 'gallery') {
347 // Replace any references to the CDN with a relative link.
348 $filecontent = preg_replace('#(' . preg_quote('http://yui.yahooapis.com/') . '(gallery-[^/]*/))#', '../../../../', $filecontent);
350 // Replace all relative image links with the a link to yui_image.php.
351 $filecontent = preg_replace('#(' . preg_quote('../../../../') . ')(gallery-[^/]*/assets/skins/sam/[a-z0-9_-]+)\.(png|gif)#',
352 $relroot . '/theme/yui_image.php' . $sep . '/gallery/' . $revision . '/$2.$3', $filecontent);
355 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
356 // I've added this as a separate regex so it can be easily removed once
357 // YUI standardise there CSS methods
358 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
360 // search for all images in yui2 CSS and serve them through the yui_image.php script
361 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
365 $content .= $filecontent;
368 if ($lastmodified == 0) {
369 $lastmodified = time();
373 combo_send_cached($content, $mimetype, $etag, $lastmodified);
375 combo_send_uncached($content, $mimetype);
380 * Send the JavaScript cached
381 * @param string $content
382 * @param string $mimetype
383 * @param string $etag
384 * @param int $lastmodified
386 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
387 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
389 header('Content-Disposition: inline; filename="combo"');
390 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
391 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
393 header('Cache-Control: public, max-age='.$lifetime.', immutable');
394 header('Accept-Ranges: none');
395 header('Content-Type: '.$mimetype);
396 header('Etag: "'.$etag.'"');
397 if (!min_enable_zlib_compression()) {
398 header('Content-Length: '.strlen($content));
406 * Send the JavaScript uncached
407 * @param string $content
408 * @param string $mimetype
410 function combo_send_uncached($content, $mimetype) {
411 header('Content-Disposition: inline; filename="combo"');
412 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
413 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
2) .' GMT');
415 header('Accept-Ranges: none');
416 header('Content-Type: '.$mimetype);
417 if (!min_enable_zlib_compression()) {
418 header('Content-Length: '.strlen($content));
425 function combo_not_found($message = '') {
426 header('HTTP/1.0 404 not found');
430 echo 'Combo resource not found, sorry.';
435 function combo_params() {
436 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
438 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
439 return array($slashargument, true);
441 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
442 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
443 return array($parts[1], false);
445 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
446 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
447 return array($_SERVER['QUERY_STRING'], false);
449 } else if ($slashargument = min_get_slash_argument(false)) {
450 $slashargument = ltrim($slashargument, '/');
451 return array($slashargument, true);
454 // unsupported server, sorry!
455 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');