Merge branch 'MDL-38957_master' of git://github.com/dmonllao/moodle
[moodle.git] / theme / yui_combo.php
blob957d37f356c0fca963e877dc07f5052f95414fce
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for serving of yui images
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
27 // disable moodle specific debug messages and any errors in output,
28 // comment out when debugging or better look into error log!
29 define('NO_DEBUG_DISPLAY', true);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 // get special url parameters
37 list($parts, $slasharguments) = combo_params();
38 if (!$parts) {
39 combo_not_found();
42 $etag = sha1($parts);
43 $parts = trim($parts, '&');
45 // find out what we are serving - only one type per request
46 $content = '';
47 if (substr($parts, -3) === '.js') {
48 $mimetype = 'application/javascript';
49 } else if (substr($parts, -4) === '.css') {
50 $mimetype = 'text/css';
51 } else {
52 combo_not_found();
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)
58 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
59 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
60 header('HTTP/1.1 304 Not Modified');
61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
62 header('Cache-Control: public, max-age='.$lifetime);
63 header('Content-Type: '.$mimetype);
64 header('Etag: '.$etag);
65 die;
68 $parts = explode('&', $parts);
69 $cache = true;
70 $lastmodified = 0;
72 foreach ($parts as $part) {
73 if (empty($part)) {
74 continue;
76 $filecontent = '';
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
80 $content .= "\n// Wrong combo resource $part!\n";
81 continue;
83 //debug($bits);
84 $version = array_shift($bits);
85 if ($version === 'moodle') {
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;
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');
97 $revision = (int)array_shift($bits);
98 if ($revision === -1) {
99 // Revision -1 says please don't cache the JS
100 $cache = false;
102 $frankenstyle = array_shift($bits);
103 $filename = array_pop($bits);
104 $modulename = $bits[0];
105 $dir = get_component_directory($frankenstyle);
107 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
108 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
109 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
111 // By default, try and use the /yui/build directory.
112 if ($mimetype == 'text/css') {
113 // CSS assets are in a slightly different place to the JS.
114 $contentfile = $dir . '/yui/build/' . $frankenstylemodulename . '/assets/skins/sam/' . $frankenstylefilename;
116 // Add the path to the bits to handle fallback for non-shifted assets.
117 $bits[] = 'assets';
118 $bits[] = 'skins';
119 $bits[] = 'sam';
120 } else {
121 $contentfile = $dir . '/yui/build/' . $frankenstylemodulename . '/' . $frankenstylefilename;
124 // If the shifted versions don't exist, fall back to the non-shifted file.
125 if (!file_exists($contentfile) or !is_file($contentfile)) {
126 // We have to revert to the non-minified and non-debug versions.
127 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
128 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
130 } else if ($version === '2in3') {
131 $contentfile = "$CFG->libdir/yuilib/$part";
133 } else if ($version == 'gallery') {
134 $contentfile = "$CFG->libdir/yui/$part";
136 } else {
137 if ($version != $CFG->yui3version) {
138 $content .= "\n// Wrong yui version $part!\n";
139 continue;
141 $contentfile = "$CFG->libdir/yuilib/$part";
143 if (!file_exists($contentfile) or !is_file($contentfile)) {
144 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
145 $content .= "\n// Combo resource $part ($location) not found!\n";
146 continue;
149 if (empty($filecontent)) {
150 $filecontent = file_get_contents($contentfile);
152 $fmodified = filemtime($contentfile);
153 if ($fmodified > $lastmodified) {
154 $lastmodified = $fmodified;
157 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
158 $sep = ($slasharguments ? '/' : '?file=');
160 if ($mimetype === 'text/css') {
161 if ($version == 'moodle') {
162 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
163 $imagebits = array(
164 $sep . $version,
165 $frankenstyle,
166 $modulename,
167 array_shift($bits),
168 '$1.$2'
171 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
172 } else if ($version == '2in3') {
173 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
174 // I've added this as a separate regex so it can be easily removed once
175 // YUI standardise there CSS methods
176 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
178 // search for all images in yui2 CSS and serve them through the yui_image.php script
179 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
181 } else if ($version == 'gallery') {
182 // search for all images in gallery module CSS and serve them through the yui_image.php script
183 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
185 } else {
186 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
187 // I've added this as a separate regex so it can be easily removed once
188 // YUI standardise there CSS methods
189 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
191 // search for all images in yui2 CSS and serve them through the yui_image.php script
192 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
196 $content .= $filecontent;
199 if ($lastmodified == 0) {
200 $lastmodified = time();
203 if ($cache) {
204 combo_send_cached($content, $mimetype, $etag, $lastmodified);
205 } else {
206 combo_send_uncached($content, $mimetype);
211 * Send the JavaScript cached
212 * @param string $content
213 * @param string $mimetype
214 * @param string $etag
215 * @param int $lastmodified
217 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
218 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
220 header('Content-Disposition: inline; filename="combo"');
221 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
222 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
223 header('Pragma: ');
224 header('Cache-Control: public, max-age='.$lifetime);
225 header('Accept-Ranges: none');
226 header('Content-Type: '.$mimetype);
227 header('Etag: '.$etag);
228 if (!min_enable_zlib_compression()) {
229 header('Content-Length: '.strlen($content));
232 echo $content;
233 die;
237 * Send the JavaScript uncached
238 * @param string $content
239 * @param string $mimetype
241 function combo_send_uncached($content, $mimetype) {
242 header('Content-Disposition: inline; filename="combo"');
243 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
244 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
245 header('Pragma: ');
246 header('Accept-Ranges: none');
247 header('Content-Type: '.$mimetype);
248 if (!min_enable_zlib_compression()) {
249 header('Content-Length: '.strlen($content));
252 echo $content;
253 die;
256 function combo_not_found($message = '') {
257 header('HTTP/1.0 404 not found');
258 if ($message) {
259 echo $message;
260 } else {
261 echo 'Combo resource not found, sorry.';
263 die;
266 function combo_params() {
267 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
268 // url rewriting
269 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
270 return array($slashargument, true);
272 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
273 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
274 return array($parts[1], false);
276 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
277 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
278 return array($_SERVER['QUERY_STRING'], false);
280 } else if ($slashargument = min_get_slash_argument()) {
281 $slashargument = ltrim($slashargument, '/');
282 return array($slashargument, true);
284 } else {
285 // unsupported server, sorry!
286 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');