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