3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This file is responsible for serving of yui images
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 // we need just the values from config.php and minlib.php
28 define('ABORT_AFTER_CONFIG', true);
29 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
31 // get special url parameters
32 if (!$parts = combo_params()) {
36 $parts = trim($parts, '&');
38 // find out what we are serving - only one type per request
40 if (substr($parts, -3) === '.js') {
41 $mimetype = 'application/javascript';
42 } else if (substr($parts, -4) === '.css') {
43 $mimetype = 'text/css';
48 // if they are requesting a revision that's not -1, and they have supplied an
49 // If-Modified-Since header, we can send back a 304 Not Modified since the
50 // content never changes (the rev number is increased any time the content changes)
51 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
52 $lifetime = 60*60*24*30; // 30 days
53 header('HTTP/1.1 304 Not Modified');
54 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
55 header('Cache-Control: max-age='.$lifetime);
56 header('Content-Type: '.$mimetype);
60 $parts = explode('&', $parts);
63 foreach ($parts as $part) {
67 $part = min_clean_param($part, 'SAFEPATH');
68 $bits = explode('/', $part);
69 if (count($bits) < 2) {
70 $content .= "\n// Wrong combo resource $part!\n";
74 $version = array_shift($bits);
75 if ($version == 'moodle') {
76 //TODO: this is a ugly hack because we should not load any libs here!
77 if (!defined('MOODLE_INTERNAL')) {
78 define('MOODLE_INTERNAL', true);
79 require_once($CFG->libdir
.'/moodlelib.php');
81 $revision = (int)array_shift($bits);
82 if ($revision === -1) {
83 // Revision -1 says please don't cache the JS
86 $frankenstyle = array_shift($bits);
87 $filename = array_pop($bits);
88 $dir = get_component_directory($frankenstyle);
89 if ($mimetype == 'text/css') {
94 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
96 if ($version != $CFG->yui3version
and $version != $CFG->yui2version
and $version != 'gallery') {
97 $content .= "\n// Wrong yui version $part!\n";
100 $contentfile = "$CFG->libdir/yui/$part";
102 if (!file_exists($contentfile) or !is_file($contentfile)) {
103 $content .= "\n// Combo resource $part not found!\n";
106 $filecontent = file_get_contents($contentfile);
108 if ($mimetype === 'text/css') {
109 if ($version == 'moodle') {
110 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
111 } else if ($version == 'gallery') {
112 // search for all images in gallery module CSS and serve them through the yui_image.php script
113 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
115 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
116 // I've added this as a separate regex so it can be easily removed once
117 // YUI standardise there CSS methods
118 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
120 // search for all images in yui2 CSS and serve them through the yui_image.php script
121 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
125 $content .= $filecontent;
129 combo_send_cached($content, $mimetype);
131 combo_send_uncached($content, $mimetype);
136 * Send the JavaScript cached
137 * @param string $content
138 * @param string $mimetype
140 function combo_send_cached($content, $mimetype) {
141 $lifetime = 60*60*24*30; // 30 days
143 header('Content-Disposition: inline; filename="combo"');
144 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
145 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
147 header('Cache-Control: max-age='.$lifetime);
148 header('Accept-Ranges: none');
149 header('Content-Type: '.$mimetype);
150 if (!min_enable_zlib_compression()) {
151 header('Content-Length: '.strlen($content));
159 * Send the JavaScript uncached
160 * @param string $content
161 * @param string $mimetype
163 function combo_send_uncached($content, $mimetype) {
164 header('Content-Disposition: inline; filename="combo"');
165 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
166 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
2) .' GMT');
168 header('Accept-Ranges: none');
169 header('Content-Type: '.$mimetype);
170 if (!min_enable_zlib_compression()) {
171 header('Content-Length: '.strlen($content));
178 function combo_not_found($message = '') {
179 header('HTTP/1.0 404 not found');
183 echo 'Combo resource not found, sorry.';
188 function combo_params() {
189 // note: buggy or misconfigured IIS does return the query string in REQUEST_URL
190 if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
191 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
194 } else if (isset($_SERVER['QUERY_STRING'])) {
195 return $_SERVER['QUERY_STRING'];
198 // unsupported server, sorry!
199 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');