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