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