Merge branch 'MDL-38391-m' of git://github.com/andrewnicols/moodle
[moodle.git] / theme / yui_combo.php
CommitLineData
60f2c866 1<?php
aa42314d
PS
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is responsible for serving of yui images
20 *
21 * @package moodlecore
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
ee891dac
PS
27// disable moodle specific debug messages and any errors in output,
28// comment out when debugging or better look into error log!
29define('NO_DEBUG_DISPLAY', true);
30
aa42314d
PS
31// we need just the values from config.php and minlib.php
32define('ABORT_AFTER_CONFIG', true);
33require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34
35// get special url parameters
d5222fae
PS
36
37list($parts, $slasharguments) = combo_params();
38if (!$parts) {
aa42314d
PS
39 combo_not_found();
40}
41
dbe14f39 42$etag = sha1($parts);
945f19f7
PS
43$parts = trim($parts, '&');
44
aa42314d
PS
45// find out what we are serving - only one type per request
46$content = '';
47if (substr($parts, -3) === '.js') {
a6338a13 48 $mimetype = 'application/javascript';
aa42314d
PS
49} else if (substr($parts, -4) === '.css') {
50 $mimetype = 'text/css';
51} else {
52 combo_not_found();
53}
54
ccc0fff9
TL
55// if they are requesting a revision that's not -1, and they have supplied an
56// If-Modified-Since header, we can send back a 304 Not Modified since the
57// content never changes (the rev number is increased any time the content changes)
14b1579a 58if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
dbe14f39 59 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa603b14 60 header('HTTP/1.1 304 Not Modified');
ccc0fff9 61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 62 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9 63 header('Content-Type: '.$mimetype);
dbe14f39 64 header('Etag: '.$etag);
ccc0fff9
TL
65 die;
66}
67
aa42314d 68$parts = explode('&', $parts);
77387297 69$cache = true;
dbe14f39 70$lastmodified = 0;
aa42314d
PS
71
72foreach ($parts as $part) {
945f19f7
PS
73 if (empty($part)) {
74 continue;
75 }
a45e8fd3 76 $filecontent = '';
aa42314d
PS
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
a4738eb3
PS
80 $content .= "\n// Wrong combo resource $part!\n";
81 continue;
aa42314d 82 }
2b722f87
SH
83 //debug($bits);
84 $version = array_shift($bits);
1c76d55a 85 if ($version === 'moodle') {
55834476
DM
86 if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
87 define('ABORT_AFTER_CONFIG_CANCEL', true);
88 define('NO_UPGRADE_CHECK', true);
89 define('NO_MOODLE_COOKIES', true);
90 require($CFG->libdir.'/setup.php');
2f76f101 91 }
77387297
SH
92 $revision = (int)array_shift($bits);
93 if ($revision === -1) {
94 // Revision -1 says please don't cache the JS
95 $cache = false;
96 }
2b722f87 97 $frankenstyle = array_shift($bits);
3b17690c 98 $filename = array_pop($bits);
a45e8fd3 99 $modulename = $bits[0];
2b722f87 100 $dir = get_component_directory($frankenstyle);
3b17690c
SH
101 if ($mimetype == 'text/css') {
102 $bits[] = 'assets';
103 $bits[] = 'skins';
104 $bits[] = 'sam';
105 }
a45e8fd3
ARN
106
107 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
108 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
109
110 // By default, try and use the /yui/build directory.
111 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
112 $contentfile = $dir . '/yui/build/' . $frankenstylemodulename . '/' . $frankenstylefilename;
113
114 // If the shifted versions don't exist, fall back to the non-shifted file.
115 if (!file_exists($contentfile) or !is_file($contentfile)) {
116 // We have to revert to the non-minified and non-debug versions.
117 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
118 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
119 }
1c76d55a
PS
120 } else if ($version === '2in3') {
121 $contentfile = "$CFG->libdir/yuilib/$part";
122
123 } else if ($version == 'gallery') {
124 $contentfile = "$CFG->libdir/yui/$part";
125
2b722f87 126 } else {
1c76d55a 127 if ($version != $CFG->yui3version) {
2b722f87
SH
128 $content .= "\n// Wrong yui version $part!\n";
129 continue;
130 }
1c76d55a 131 $contentfile = "$CFG->libdir/yuilib/$part";
aa42314d 132 }
aa42314d 133 if (!file_exists($contentfile) or !is_file($contentfile)) {
a2bdf340
PS
134 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
135 $content .= "\n// Combo resource $part ($location) not found!\n";
a4738eb3 136 continue;
aa42314d 137 }
a45e8fd3
ARN
138
139 if (empty($filecontent)) {
140 $filecontent = file_get_contents($contentfile);
141 }
dbe14f39
PS
142 $fmodified = filemtime($contentfile);
143 if ($fmodified > $lastmodified) {
144 $lastmodified = $fmodified;
145 }
aa42314d 146
6e7b4601 147 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
d5222fae 148 $sep = ($slasharguments ? '/' : '?file=');
6e7b4601 149
aa42314d 150 if ($mimetype === 'text/css') {
3b17690c 151 if ($version == 'moodle') {
a45e8fd3
ARN
152 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
153 $imagebits = array(
154 $sep . $version,
155 $frankenstyle,
156 $modulename,
157 array_shift($bits),
158 '$1.$2'
159 );
160
161 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
1c76d55a
PS
162 } else if ($version == '2in3') {
163 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
164 // I've added this as a separate regex so it can be easily removed once
165 // YUI standardise there CSS methods
166 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
167
168 // search for all images in yui2 CSS and serve them through the yui_image.php script
169 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
170
3b17690c 171 } else if ($version == 'gallery') {
2a102b90 172 // search for all images in gallery module CSS and serve them through the yui_image.php script
d5222fae 173 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
1c76d55a 174
2a102b90 175 } else {
4d909122
SH
176 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
177 // I've added this as a separate regex so it can be easily removed once
178 // YUI standardise there CSS methods
6e6034e5 179 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d909122 180
2a102b90 181 // search for all images in yui2 CSS and serve them through the yui_image.php script
d5222fae 182 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
2a102b90 183 }
aa42314d
PS
184 }
185
186 $content .= $filecontent;
187}
188
dbe14f39
PS
189if ($lastmodified == 0) {
190 $lastmodified = time();
191}
192
77387297 193if ($cache) {
dbe14f39 194 combo_send_cached($content, $mimetype, $etag, $lastmodified);
77387297
SH
195} else {
196 combo_send_uncached($content, $mimetype);
197}
aa42314d
PS
198
199
77387297
SH
200/**
201 * Send the JavaScript cached
202 * @param string $content
203 * @param string $mimetype
dbe14f39
PS
204 * @param string $etag
205 * @param int $lastmodified
77387297 206 */
dbe14f39
PS
207function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
208 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa42314d
PS
209
210 header('Content-Disposition: inline; filename="combo"');
dbe14f39 211 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
aa42314d
PS
212 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
213 header('Pragma: ');
84e1e044 214 header('Cache-Control: public, max-age='.$lifetime);
aa42314d
PS
215 header('Accept-Ranges: none');
216 header('Content-Type: '.$mimetype);
dbe14f39 217 header('Etag: '.$etag);
7c986f04
PS
218 if (!min_enable_zlib_compression()) {
219 header('Content-Length: '.strlen($content));
220 }
aa42314d 221
aa42314d
PS
222 echo $content;
223 die;
224}
225
77387297
SH
226/**
227 * Send the JavaScript uncached
228 * @param string $content
229 * @param string $mimetype
230 */
231function combo_send_uncached($content, $mimetype) {
232 header('Content-Disposition: inline; filename="combo"');
233 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
234 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
235 header('Pragma: ');
236 header('Accept-Ranges: none');
237 header('Content-Type: '.$mimetype);
238 if (!min_enable_zlib_compression()) {
239 header('Content-Length: '.strlen($content));
240 }
241
242 echo $content;
243 die;
244}
245
37c82592 246function combo_not_found($message = '') {
aa42314d 247 header('HTTP/1.0 404 not found');
37c82592
PS
248 if ($message) {
249 echo $message;
250 } else {
251 echo 'Combo resource not found, sorry.';
252 }
253 die;
aa42314d
PS
254}
255
256function combo_params() {
18ac11b7
PS
257 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
258 // url rewriting
259 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
260 return array($slashargument, true);
261
262 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
37c82592 263 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
d5222fae 264 return array($parts[1], false);
aa42314d 265
6e7b4601 266 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
18ac11b7 267 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
d5222fae 268 return array($_SERVER['QUERY_STRING'], false);
aa42314d 269
6e7b4601
PS
270 } else if ($slashargument = min_get_slash_argument()) {
271 $slashargument = ltrim($slashargument, '/');
d5222fae 272 return array($slashargument, true);
6e7b4601 273
aa42314d 274 } else {
37c82592
PS
275 // unsupported server, sorry!
276 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314d
PS
277 }
278}