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