MDL-42453 theme_base: Fixed Error in base/style/admin.css that breaks CSS validator.
[moodle.git] / theme / yui_combo.php
blobd1fe8cb4617d1975b4942301e04c338072b2443a
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 $revision = array_shift($bits);
87 $rollupname = array_shift($bits);
89 // Determine whether we should minify this rollup.
90 preg_match('/(-min)?\.js/', $rollupname, $matches);
91 $filesuffix = '.js';
92 if (isset($matches[1])) {
93 $filesuffix = '-min.js';
96 if (strpos($rollupname, 'yui-moodlesimple') !== false) {
97 $yuimodules = array(
98 // Include everything from original SimpleYUI,
99 // this list can be built using http://yuilibrary.com/yui/configurator/ by selecting all modules
100 // listed in https://github.com/yui/yui3/blob/v3.12.0/build/simpleyui/simpleyui.js#L21327
101 'yui',
102 'yui-base',
103 'get',
104 'features',
105 'loader-base',
106 'loader-rollup',
107 'loader-yui3',
108 'oop',
109 'event-custom-base',
110 'dom-core',
111 'dom-base',
112 'color-base',
113 'dom-style',
114 'selector-native',
115 'selector',
116 'node-core',
117 'node-base',
118 'event-base',
119 'event-base-ie',
120 'pluginhost-base',
121 'pluginhost-config',
122 'event-delegate',
123 'node-event-delegate',
124 'node-pluginhost',
125 'dom-screen',
126 'node-screen',
127 'node-style',
128 'querystring-stringify-simple',
129 'io-base',
130 'json-parse',
131 'transition',
132 'selector-css2',
133 'selector-css3',
134 'dom-style-ie',
136 // Some extras we use everywhere.
137 'escape',
139 'attribute-core',
140 'event-custom-complex',
141 'base-core',
142 'attribute-base',
143 'attribute-extras',
144 'attribute-observable',
145 'base-observable',
146 'base-base',
147 'base-pluginhost',
148 'base-build',
149 'event-synthetic',
151 'attribute-complex',
152 'event-mouseenter',
153 'event-key',
154 'event-outside',
155 'event-autohide',
156 'event-focus',
157 'classnamemanager',
158 'widget-base',
159 'widget-htmlparser',
160 'widget-skin',
161 'widget-uievents',
162 'widget-stdmod',
163 'widget-position',
164 'widget-position-align',
165 'widget-stack',
166 'widget-position-constrain',
167 'overlay',
169 'widget-autohide',
170 'button-core',
171 'button-plugin',
172 'widget-buttons',
173 'widget-modality',
174 'panel',
175 'yui-throttle',
176 'dd-ddm-base',
177 'dd-drag',
178 'dd-plugin',
181 // We need to add these new parts to the beginning of the $parts list, not the end.
182 $newparts = array();
183 foreach ($yuimodules as $module) {
184 $newparts[] = $revision . '/' . $module . '/' . $module . $filesuffix;
186 $newparts[] = 'yuiuseall/yuiuseall';
187 $parts = array_merge($newparts, $parts);
190 // Handle the mcore rollup.
191 if (strpos($rollupname, 'mcore') !== false) {
192 $yuimodules = array(
193 'core/tooltip/tooltip',
194 'core/dock/dock-loader',
195 'core/notification/notification-dialogue',
198 $filesuffix = '.js';
199 if (isset($matches[1])) {
200 $filesuffix = '-min.js';
203 // We need to add these new parts to the beginning of the $parts list, not the end.
204 $newparts = array();
205 foreach ($yuimodules as $module) {
206 $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
208 $parts = array_merge($newparts, $parts);
210 continue;
212 if ($version === 'm') {
213 $version = 'moodle';
215 if ($version === 'moodle') {
216 if (count($bits) <= 3) {
217 // This is an invalid module load attempt.
218 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
219 continue;
221 $revision = (int)array_shift($bits);
222 if ($revision === -1) {
223 // Revision -1 says please don't cache the JS
224 $cache = false;
226 $frankenstyle = array_shift($bits);
227 $filename = array_pop($bits);
228 $modulename = $bits[0];
229 $dir = core_component::get_component_directory($frankenstyle);
231 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
232 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
233 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
235 // Submodules are stored in a directory with the full submodule name.
236 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
237 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js'), '', $frankenstylefilename);
239 // By default, try and use the /yui/build directory.
240 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
241 if ($mimetype == 'text/css') {
242 // CSS assets are in a slightly different place to the JS.
243 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
245 // Add the path to the bits to handle fallback for non-shifted assets.
246 $bits[] = 'assets';
247 $bits[] = 'skins';
248 $bits[] = 'sam';
249 } else {
250 $contentfile = $contentfile . '/' . $frankenstylefilename;
253 // If the shifted versions don't exist, fall back to the non-shifted file.
254 if (!file_exists($contentfile) or !is_file($contentfile)) {
255 // We have to revert to the non-minified and non-debug versions.
256 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
257 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
259 } else if ($version === '2in3') {
260 $contentfile = "$CFG->libdir/yuilib/$part";
262 } else if ($version == 'gallery') {
263 $contentfile = "$CFG->libdir/yui/$part";
265 } else if ($version == 'yuiuseall') {
266 // Create global Y that is available in global scope,
267 // this is the trick behind original SimpleYUI.
268 $filecontent = "var Y = YUI().use('*');";
270 } else {
271 if ($version != $CFG->yui3version) {
272 $content .= "\n// Wrong yui version $part!\n";
273 continue;
275 $contentfile = "$CFG->libdir/yuilib/$part";
277 if (!file_exists($contentfile) or !is_file($contentfile)) {
278 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
279 $content .= "\n// Combo resource $part ($location) not found!\n";
280 continue;
283 if (empty($filecontent)) {
284 $filecontent = file_get_contents($contentfile);
286 $fmodified = filemtime($contentfile);
287 if ($fmodified > $lastmodified) {
288 $lastmodified = $fmodified;
291 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
292 $sep = ($slasharguments ? '/' : '?file=');
294 if ($mimetype === 'text/css') {
295 if ($version == 'moodle') {
296 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
297 $imagebits = array(
298 $sep . $version,
299 $frankenstyle,
300 $modulename,
301 array_shift($bits),
302 '$1.$2'
305 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
306 } else if ($version == '2in3') {
307 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
308 // I've added this as a separate regex so it can be easily removed once
309 // YUI standardise there CSS methods
310 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
312 // search for all images in yui2 CSS and serve them through the yui_image.php script
313 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
315 } else if ($version == 'gallery') {
316 // search for all images in gallery module CSS and serve them through the yui_image.php script
317 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
319 } else {
320 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
321 // I've added this as a separate regex so it can be easily removed once
322 // YUI standardise there CSS methods
323 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
325 // search for all images in yui2 CSS and serve them through the yui_image.php script
326 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
330 $content .= $filecontent;
333 if ($lastmodified == 0) {
334 $lastmodified = time();
337 if ($cache) {
338 combo_send_cached($content, $mimetype, $etag, $lastmodified);
339 } else {
340 combo_send_uncached($content, $mimetype);
345 * Send the JavaScript cached
346 * @param string $content
347 * @param string $mimetype
348 * @param string $etag
349 * @param int $lastmodified
351 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
352 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
354 header('Content-Disposition: inline; filename="combo"');
355 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
356 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
357 header('Pragma: ');
358 header('Cache-Control: public, max-age='.$lifetime);
359 header('Accept-Ranges: none');
360 header('Content-Type: '.$mimetype);
361 header('Etag: "'.$etag.'"');
362 if (!min_enable_zlib_compression()) {
363 header('Content-Length: '.strlen($content));
366 echo $content;
367 die;
371 * Send the JavaScript uncached
372 * @param string $content
373 * @param string $mimetype
375 function combo_send_uncached($content, $mimetype) {
376 header('Content-Disposition: inline; filename="combo"');
377 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
378 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
379 header('Pragma: ');
380 header('Accept-Ranges: none');
381 header('Content-Type: '.$mimetype);
382 if (!min_enable_zlib_compression()) {
383 header('Content-Length: '.strlen($content));
386 echo $content;
387 die;
390 function combo_not_found($message = '') {
391 header('HTTP/1.0 404 not found');
392 if ($message) {
393 echo $message;
394 } else {
395 echo 'Combo resource not found, sorry.';
397 die;
400 function combo_params() {
401 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
402 // url rewriting
403 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
404 return array($slashargument, true);
406 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
407 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
408 return array($parts[1], false);
410 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
411 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
412 return array($_SERVER['QUERY_STRING'], false);
414 } else if ($slashargument = min_get_slash_argument()) {
415 $slashargument = ltrim($slashargument, '/');
416 return array($slashargument, true);
418 } else {
419 // unsupported server, sorry!
420 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');