MDL-41806 Add assessors to moodle_url class
[moodle.git] / theme / yui_combo.php
blob47e5e90d97b82353c4086a1e6b4a39735c8752f0
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 $revision = (int)array_shift($bits);
91 if ($revision === -1) {
92 // Revision -1 says please don't cache the JS
93 $cache = false;
95 $frankenstyle = array_shift($bits);
96 $filename = array_pop($bits);
97 $modulename = $bits[0];
98 $dir = core_component::get_component_directory($frankenstyle);
100 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
101 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
102 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
104 // Submodules are stored in a directory with the full submodule name.
105 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
106 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js'), '', $frankenstylefilename);
108 // By default, try and use the /yui/build directory.
109 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
110 if ($mimetype == 'text/css') {
111 // CSS assets are in a slightly different place to the JS.
112 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
114 // Add the path to the bits to handle fallback for non-shifted assets.
115 $bits[] = 'assets';
116 $bits[] = 'skins';
117 $bits[] = 'sam';
118 } else {
119 $contentfile = $contentfile . '/' . $frankenstylefilename;
122 // If the shifted versions don't exist, fall back to the non-shifted file.
123 if (!file_exists($contentfile) or !is_file($contentfile)) {
124 // We have to revert to the non-minified and non-debug versions.
125 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
126 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
128 } else if ($version === '2in3') {
129 $contentfile = "$CFG->libdir/yuilib/$part";
131 } else if ($version == 'gallery') {
132 $contentfile = "$CFG->libdir/yui/$part";
134 } else {
135 if ($version != $CFG->yui3version) {
136 $content .= "\n// Wrong yui version $part!\n";
137 continue;
139 $contentfile = "$CFG->libdir/yuilib/$part";
141 if (!file_exists($contentfile) or !is_file($contentfile)) {
142 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
143 $content .= "\n// Combo resource $part ($location) not found!\n";
144 continue;
147 if (empty($filecontent)) {
148 $filecontent = file_get_contents($contentfile);
150 $fmodified = filemtime($contentfile);
151 if ($fmodified > $lastmodified) {
152 $lastmodified = $fmodified;
155 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
156 $sep = ($slasharguments ? '/' : '?file=');
158 if ($mimetype === 'text/css') {
159 if ($version == 'moodle') {
160 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
161 $imagebits = array(
162 $sep . $version,
163 $frankenstyle,
164 $modulename,
165 array_shift($bits),
166 '$1.$2'
169 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
170 } else if ($version == '2in3') {
171 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
172 // I've added this as a separate regex so it can be easily removed once
173 // YUI standardise there CSS methods
174 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
176 // search for all images in yui2 CSS and serve them through the yui_image.php script
177 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
179 } else if ($version == 'gallery') {
180 // search for all images in gallery module CSS and serve them through the yui_image.php script
181 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
183 } else {
184 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
185 // I've added this as a separate regex so it can be easily removed once
186 // YUI standardise there CSS methods
187 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
189 // search for all images in yui2 CSS and serve them through the yui_image.php script
190 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
194 $content .= $filecontent;
197 if ($lastmodified == 0) {
198 $lastmodified = time();
201 if ($cache) {
202 combo_send_cached($content, $mimetype, $etag, $lastmodified);
203 } else {
204 combo_send_uncached($content, $mimetype);
209 * Send the JavaScript cached
210 * @param string $content
211 * @param string $mimetype
212 * @param string $etag
213 * @param int $lastmodified
215 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
216 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
218 header('Content-Disposition: inline; filename="combo"');
219 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
220 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
221 header('Pragma: ');
222 header('Cache-Control: public, max-age='.$lifetime);
223 header('Accept-Ranges: none');
224 header('Content-Type: '.$mimetype);
225 header('Etag: "'.$etag.'"');
226 if (!min_enable_zlib_compression()) {
227 header('Content-Length: '.strlen($content));
230 echo $content;
231 die;
235 * Send the JavaScript uncached
236 * @param string $content
237 * @param string $mimetype
239 function combo_send_uncached($content, $mimetype) {
240 header('Content-Disposition: inline; filename="combo"');
241 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
242 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
243 header('Pragma: ');
244 header('Accept-Ranges: none');
245 header('Content-Type: '.$mimetype);
246 if (!min_enable_zlib_compression()) {
247 header('Content-Length: '.strlen($content));
250 echo $content;
251 die;
254 function combo_not_found($message = '') {
255 header('HTTP/1.0 404 not found');
256 if ($message) {
257 echo $message;
258 } else {
259 echo 'Combo resource not found, sorry.';
261 die;
264 function combo_params() {
265 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
266 // url rewriting
267 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
268 return array($slashargument, true);
270 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
271 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
272 return array($parts[1], false);
274 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
275 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
276 return array($_SERVER['QUERY_STRING'], false);
278 } else if ($slashargument = min_get_slash_argument()) {
279 $slashargument = ltrim($slashargument, '/');
280 return array($slashargument, true);
282 } else {
283 // unsupported server, sorry!
284 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');