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