MDL-40478 JavaScript: Support loading of YUI submodules in the yui-loader
[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/**
18 * This file is responsible for serving of yui images
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
PS
70
71foreach ($parts as $part) {
945f19f7
PS
72 if (empty($part)) {
73 continue;
74 }
a45e8fd3 75 $filecontent = '';
aa42314d
PS
76 $part = min_clean_param($part, 'SAFEPATH');
77 $bits = explode('/', $part);
78 if (count($bits) < 2) {
a4738eb3
PS
79 $content .= "\n// Wrong combo resource $part!\n";
80 continue;
aa42314d 81 }
2b722f87
SH
82 //debug($bits);
83 $version = array_shift($bits);
1c76d55a 84 if ($version === 'moodle') {
4f65d03b
ARN
85 if (count($bits) <= 3) {
86 // This is an invalid module load attempt.
87 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
88 continue;
89 }
55834476
DM
90 if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
91 define('ABORT_AFTER_CONFIG_CANCEL', true);
92 define('NO_UPGRADE_CHECK', true);
93 define('NO_MOODLE_COOKIES', true);
94 require($CFG->libdir.'/setup.php');
2f76f101 95 }
77387297
SH
96 $revision = (int)array_shift($bits);
97 if ($revision === -1) {
98 // Revision -1 says please don't cache the JS
99 $cache = false;
100 }
2b722f87 101 $frankenstyle = array_shift($bits);
3b17690c 102 $filename = array_pop($bits);
a45e8fd3 103 $modulename = $bits[0];
2b722f87 104 $dir = get_component_directory($frankenstyle);
a45e8fd3
ARN
105
106 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
107 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
f0e79573 108 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
a45e8fd3 109
f320e8d0
AN
110 // Submodules are stored in a directory with the full submodule name.
111 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
112 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js'), '', $frankenstylefilename);
113
a45e8fd3 114 // By default, try and use the /yui/build directory.
f320e8d0 115 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
f0e79573
ARN
116 if ($mimetype == 'text/css') {
117 // CSS assets are in a slightly different place to the JS.
f320e8d0 118 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
f0e79573
ARN
119
120 // Add the path to the bits to handle fallback for non-shifted assets.
121 $bits[] = 'assets';
122 $bits[] = 'skins';
123 $bits[] = 'sam';
124 } else {
f320e8d0 125 $contentfile = $contentfile . '/' . $frankenstylefilename;
f0e79573 126 }
a45e8fd3
ARN
127
128 // If the shifted versions don't exist, fall back to the non-shifted file.
129 if (!file_exists($contentfile) or !is_file($contentfile)) {
130 // We have to revert to the non-minified and non-debug versions.
131 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
132 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
133 }
1c76d55a
PS
134 } else if ($version === '2in3') {
135 $contentfile = "$CFG->libdir/yuilib/$part";
136
137 } else if ($version == 'gallery') {
138 $contentfile = "$CFG->libdir/yui/$part";
139
2b722f87 140 } else {
1c76d55a 141 if ($version != $CFG->yui3version) {
2b722f87
SH
142 $content .= "\n// Wrong yui version $part!\n";
143 continue;
144 }
1c76d55a 145 $contentfile = "$CFG->libdir/yuilib/$part";
aa42314d 146 }
aa42314d 147 if (!file_exists($contentfile) or !is_file($contentfile)) {
a2bdf340
PS
148 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
149 $content .= "\n// Combo resource $part ($location) not found!\n";
a4738eb3 150 continue;
aa42314d 151 }
a45e8fd3
ARN
152
153 if (empty($filecontent)) {
154 $filecontent = file_get_contents($contentfile);
155 }
dbe14f39
PS
156 $fmodified = filemtime($contentfile);
157 if ($fmodified > $lastmodified) {
158 $lastmodified = $fmodified;
159 }
aa42314d 160
6e7b4601 161 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
d5222fae 162 $sep = ($slasharguments ? '/' : '?file=');
6e7b4601 163
aa42314d 164 if ($mimetype === 'text/css') {
3b17690c 165 if ($version == 'moodle') {
a45e8fd3
ARN
166 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
167 $imagebits = array(
168 $sep . $version,
169 $frankenstyle,
170 $modulename,
171 array_shift($bits),
172 '$1.$2'
173 );
174
175 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
1c76d55a
PS
176 } else if ($version == '2in3') {
177 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
178 // I've added this as a separate regex so it can be easily removed once
179 // YUI standardise there CSS methods
180 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
181
182 // search for all images in yui2 CSS and serve them through the yui_image.php script
183 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
184
3b17690c 185 } else if ($version == 'gallery') {
2a102b90 186 // search for all images in gallery module CSS and serve them through the yui_image.php script
d5222fae 187 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
1c76d55a 188
2a102b90 189 } else {
4d909122
SH
190 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
191 // I've added this as a separate regex so it can be easily removed once
192 // YUI standardise there CSS methods
6e6034e5 193 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d909122 194
2a102b90 195 // search for all images in yui2 CSS and serve them through the yui_image.php script
d5222fae 196 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
2a102b90 197 }
aa42314d
PS
198 }
199
200 $content .= $filecontent;
201}
202
dbe14f39
PS
203if ($lastmodified == 0) {
204 $lastmodified = time();
205}
206
77387297 207if ($cache) {
dbe14f39 208 combo_send_cached($content, $mimetype, $etag, $lastmodified);
77387297
SH
209} else {
210 combo_send_uncached($content, $mimetype);
211}
aa42314d
PS
212
213
77387297
SH
214/**
215 * Send the JavaScript cached
216 * @param string $content
217 * @param string $mimetype
dbe14f39
PS
218 * @param string $etag
219 * @param int $lastmodified
77387297 220 */
dbe14f39
PS
221function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
222 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa42314d
PS
223
224 header('Content-Disposition: inline; filename="combo"');
dbe14f39 225 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
aa42314d
PS
226 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
227 header('Pragma: ');
84e1e044 228 header('Cache-Control: public, max-age='.$lifetime);
aa42314d
PS
229 header('Accept-Ranges: none');
230 header('Content-Type: '.$mimetype);
06eca486 231 header('Etag: "'.$etag.'"');
7c986f04
PS
232 if (!min_enable_zlib_compression()) {
233 header('Content-Length: '.strlen($content));
234 }
aa42314d 235
aa42314d
PS
236 echo $content;
237 die;
238}
239
77387297
SH
240/**
241 * Send the JavaScript uncached
242 * @param string $content
243 * @param string $mimetype
244 */
245function combo_send_uncached($content, $mimetype) {
246 header('Content-Disposition: inline; filename="combo"');
247 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
248 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
249 header('Pragma: ');
250 header('Accept-Ranges: none');
251 header('Content-Type: '.$mimetype);
252 if (!min_enable_zlib_compression()) {
253 header('Content-Length: '.strlen($content));
254 }
255
256 echo $content;
257 die;
258}
259
37c82592 260function combo_not_found($message = '') {
aa42314d 261 header('HTTP/1.0 404 not found');
37c82592
PS
262 if ($message) {
263 echo $message;
264 } else {
265 echo 'Combo resource not found, sorry.';
266 }
267 die;
aa42314d
PS
268}
269
270function combo_params() {
18ac11b7
PS
271 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
272 // url rewriting
273 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
274 return array($slashargument, true);
275
276 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
37c82592 277 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
d5222fae 278 return array($parts[1], false);
aa42314d 279
6e7b4601 280 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
18ac11b7 281 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
d5222fae 282 return array($_SERVER['QUERY_STRING'], false);
aa42314d 283
6e7b4601
PS
284 } else if ($slashargument = min_get_slash_argument()) {
285 $slashargument = ltrim($slashargument, '/');
d5222fae 286 return array($slashargument, true);
6e7b4601 287
aa42314d 288 } else {
37c82592
PS
289 // unsupported server, sorry!
290 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314d
PS
291 }
292}