Merge branch 'MDL-44440_master' of git://github.com/dmonllao/moodle
[moodle.git] / theme / yui_combo.php
blobab1eeaba323bf64b7d2205f37b7545ca916d84d8
1 <?php
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.
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/>.
17 /**
18 * This file is responsible for serving of yui Javascript and CSS
20 * @package core
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();
37 if (!$parts) {
38 combo_not_found();
41 $etag = sha1($parts);
42 $parts = trim($parts, '&');
44 // find out what we are serving - only one type per request
45 $content = '';
46 if (substr($parts, -3) === '.js') {
47 $mimetype = 'application/javascript';
48 } else if (substr($parts, -4) === '.css') {
49 $mimetype = 'text/css';
50 } else {
51 combo_not_found();
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.'"');
64 die;
67 $parts = explode('&', $parts);
68 $cache = true;
69 $lastmodified = 0;
71 while (count($parts)) {
72 $part = array_shift($parts);
73 if (empty($part)) {
74 continue;
76 $filecontent = '';
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
80 $content .= "\n// Wrong combo resource $part!\n";
81 continue;
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.
93 $filesuffix = '.js';
94 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
95 if (isset($matches[1])) {
96 $filesuffix = $matches[0];
99 $type = 'js';
100 } else if (substr($rollupname, -4) === '.css') {
101 $type = 'css';
102 } else {
103 continue;
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];
111 $yuimodules = array(
112 // Include everything from original SimpleYUI,
113 // this list can be built using http://yuilibrary.com/yui/configurator/ by selecting all modules
114 // listed in https://github.com/yui/yui3/blob/v3.12.0/build/simpleyui/simpleyui.js#L21327
115 'yui',
116 'yui-base',
117 'get',
118 'features',
119 'loader-base',
120 'loader-rollup',
121 'loader-yui3',
122 'oop',
123 'event-custom-base',
124 'dom-core',
125 'dom-base',
126 'color-base',
127 'dom-style',
128 'selector-native',
129 'selector',
130 'node-core',
131 'node-base',
132 'event-base',
133 'event-base-ie',
134 'pluginhost-base',
135 'pluginhost-config',
136 'event-delegate',
137 'node-event-delegate',
138 'node-pluginhost',
139 'dom-screen',
140 'node-screen',
141 'node-style',
142 'querystring-stringify-simple',
143 'io-base',
144 'json-parse',
145 'transition',
146 'selector-css2',
147 'selector-css3',
148 'dom-style-ie',
150 // Some extras we use everywhere.
151 'escape',
153 'attribute-core',
154 'event-custom-complex',
155 'base-core',
156 'attribute-base',
157 'attribute-extras',
158 'attribute-observable',
159 'base-observable',
160 'base-base',
161 'base-pluginhost',
162 'base-build',
163 'event-synthetic',
165 'attribute-complex',
166 'event-mouseenter',
167 'event-key',
168 'event-outside',
169 'event-autohide',
170 'event-focus',
171 'classnamemanager',
172 'widget-base',
173 'widget-htmlparser',
174 'widget-skin',
175 'widget-uievents',
176 'widget-stdmod',
177 'widget-position',
178 'widget-position-align',
179 'widget-stack',
180 'widget-position-constrain',
181 'overlay',
183 'widget-autohide',
184 'button-core',
185 'button-plugin',
186 'widget-buttons',
187 'widget-modality',
188 'panel',
189 'yui-throttle',
190 'dd-ddm-base',
191 'dd-drag',
192 'dd-plugin',
194 // Cache is used by moodle-core-tooltip which we include everywhere.
195 'cache-base',
198 // We need to add these new parts to the beginning of the $parts list, not the end.
199 if ($type === 'js') {
200 $newparts = array();
201 foreach ($yuimodules as $module) {
202 $newparts[] = $yuiversion . '/' . $module . '/' . $module . $filesuffix;
204 $newparts[] = 'yuiuseall/yuiuseall';
205 $parts = array_merge($newparts, $parts);
206 } else {
207 $newparts = array();
208 foreach ($yuimodules as $module) {
209 $candidate = $yuiversion . '/' . $module . '/assets/skins/sam/' . $module . '.css';
210 if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
211 continue;
213 $newparts[] = $candidate;
215 if ($newparts) {
216 $parts = array_merge($newparts, $parts);
221 // Handle the mcore rollup.
222 if (strpos($rollupname, 'mcore') !== false) {
223 $yuimodules = array(
224 'core/tooltip/tooltip',
225 'core/popuphelp/popuphelp',
226 'core/widget-focusafterclose/widget-focusafterclose',
227 'core/dock/dock-loader',
228 'core/notification/notification-dialogue',
231 // Determine which version of this rollup should be used.
232 $filesuffix = '.js';
233 preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
234 if (isset($matches[1])) {
235 $filesuffix = $matches[0];
238 // We need to add these new parts to the beginning of the $parts list, not the end.
239 $newparts = array();
240 foreach ($yuimodules as $module) {
241 $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
243 $parts = array_merge($newparts, $parts);
245 continue;
247 if ($version === 'm') {
248 $version = 'moodle';
250 if ($version === 'moodle') {
251 if (count($bits) <= 3) {
252 // This is an invalid module load attempt.
253 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
254 continue;
256 $revision = (int)array_shift($bits);
257 if ($revision === -1) {
258 // Revision -1 says please don't cache the JS
259 $cache = false;
261 $frankenstyle = array_shift($bits);
262 $filename = array_pop($bits);
263 $modulename = $bits[0];
264 $dir = core_component::get_component_directory($frankenstyle);
266 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
267 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
268 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
270 // Submodules are stored in a directory with the full submodule name.
271 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
272 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
274 // By default, try and use the /yui/build directory.
275 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
276 if ($mimetype == 'text/css') {
277 // CSS assets are in a slightly different place to the JS.
278 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
280 // Add the path to the bits to handle fallback for non-shifted assets.
281 $bits[] = 'assets';
282 $bits[] = 'skins';
283 $bits[] = 'sam';
284 } else {
285 $contentfile = $contentfile . '/' . $frankenstylefilename;
288 // If the shifted versions don't exist, fall back to the non-shifted file.
289 if (!file_exists($contentfile) or !is_file($contentfile)) {
290 // We have to revert to the non-minified and non-debug versions.
291 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
292 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
294 } else if ($version === '2in3') {
295 $contentfile = "$CFG->libdir/yuilib/$part";
297 } else if ($version == 'gallery') {
298 if (count($bits) <= 2) {
299 // This is an invalid module load attempt.
300 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
301 continue;
303 $revision = (int)array_shift($bits);
304 if ($revision === -1) {
305 // Revision -1 says please don't cache the JS
306 $cache = false;
308 $contentfile = "$CFG->libdir/yuilib/gallery/" . join('/', $bits);
310 } else if ($version == 'yuiuseall') {
311 // Create global Y that is available in global scope,
312 // this is the trick behind original SimpleYUI.
313 $filecontent = "var Y = YUI().use('*');";
315 } else {
316 // Allow support for revisions on YUI between official releases.
317 // We can just discard the subrevision since it is only used to invalidate the browser cache.
318 $yuipatchedversion = explode('_', $version);
319 $yuiversion = $yuipatchedversion[0];
320 if ($yuiversion != $CFG->yui3version) {
321 $content .= "\n// Wrong yui version $part!\n";
322 continue;
324 $newpart = explode('/', $part);
325 $newpart[0] = $yuiversion;
326 $part = implode('/', $newpart);
327 $contentfile = "$CFG->libdir/yuilib/$part";
329 if (!file_exists($contentfile) or !is_file($contentfile)) {
330 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
331 $content .= "\n// Combo resource $part ($location) not found!\n";
332 continue;
335 if (empty($filecontent)) {
336 $filecontent = file_get_contents($contentfile);
338 $fmodified = filemtime($contentfile);
339 if ($fmodified > $lastmodified) {
340 $lastmodified = $fmodified;
343 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
344 $sep = ($slasharguments ? '/' : '?file=');
346 if ($mimetype === 'text/css') {
347 if ($version == 'moodle') {
348 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
349 $imagebits = array(
350 $sep . $version,
351 $frankenstyle,
352 $modulename,
353 array_shift($bits),
354 '$1.$2'
357 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
358 } else if ($version == '2in3') {
359 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
360 // I've added this as a separate regex so it can be easily removed once
361 // YUI standardise there CSS methods
362 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
364 // search for all images in yui2 CSS and serve them through the yui_image.php script
365 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
367 } else if ($version == 'gallery') {
368 // Replace any references to the CDN with a relative link.
369 $filecontent = preg_replace('#(' . preg_quote('http://yui.yahooapis.com/') . '(gallery-[^/]*/))#', '../../../../', $filecontent);
371 // Replace all relative image links with the a link to yui_image.php.
372 $filecontent = preg_replace('#(' . preg_quote('../../../../') . ')(gallery-[^/]*/assets/skins/sam/[a-z0-9_-]+)\.(png|gif)#',
373 $relroot . '/theme/yui_image.php' . $sep . '/gallery/' . $revision . '/$2.$3', $filecontent);
375 } else {
376 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
377 // I've added this as a separate regex so it can be easily removed once
378 // YUI standardise there CSS methods
379 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
381 // search for all images in yui2 CSS and serve them through the yui_image.php script
382 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
386 $content .= $filecontent;
389 if ($lastmodified == 0) {
390 $lastmodified = time();
393 if ($cache) {
394 combo_send_cached($content, $mimetype, $etag, $lastmodified);
395 } else {
396 combo_send_uncached($content, $mimetype);
401 * Send the JavaScript cached
402 * @param string $content
403 * @param string $mimetype
404 * @param string $etag
405 * @param int $lastmodified
407 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
408 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
410 header('Content-Disposition: inline; filename="combo"');
411 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
412 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
413 header('Pragma: ');
414 header('Cache-Control: public, max-age='.$lifetime);
415 header('Accept-Ranges: none');
416 header('Content-Type: '.$mimetype);
417 header('Etag: "'.$etag.'"');
418 if (!min_enable_zlib_compression()) {
419 header('Content-Length: '.strlen($content));
422 echo $content;
423 die;
427 * Send the JavaScript uncached
428 * @param string $content
429 * @param string $mimetype
431 function combo_send_uncached($content, $mimetype) {
432 header('Content-Disposition: inline; filename="combo"');
433 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
434 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
435 header('Pragma: ');
436 header('Accept-Ranges: none');
437 header('Content-Type: '.$mimetype);
438 if (!min_enable_zlib_compression()) {
439 header('Content-Length: '.strlen($content));
442 echo $content;
443 die;
446 function combo_not_found($message = '') {
447 header('HTTP/1.0 404 not found');
448 if ($message) {
449 echo $message;
450 } else {
451 echo 'Combo resource not found, sorry.';
453 die;
456 function combo_params() {
457 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
458 // url rewriting
459 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
460 return array($slashargument, true);
462 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
463 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
464 return array($parts[1], false);
466 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
467 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
468 return array($_SERVER['QUERY_STRING'], false);
470 } else if ($slashargument = min_get_slash_argument()) {
471 $slashargument = ltrim($slashargument, '/');
472 return array($slashargument, true);
474 } else {
475 // unsupported server, sorry!
476 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');