MDL-32683 use slashargument urls for yui combo loader
[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
36if (!$parts = combo_params()) {
37 combo_not_found();
38}
39
945f19f7
PS
40$parts = trim($parts, '&');
41
aa42314d
PS
42// find out what we are serving - only one type per request
43$content = '';
44if (substr($parts, -3) === '.js') {
a6338a13 45 $mimetype = 'application/javascript';
aa42314d
PS
46} else if (substr($parts, -4) === '.css') {
47 $mimetype = 'text/css';
48} else {
49 combo_not_found();
50}
51
ccc0fff9
TL
52// if they are requesting a revision that's not -1, and they have supplied an
53// If-Modified-Since header, we can send back a 304 Not Modified since the
54// content never changes (the rev number is increased any time the content changes)
14b1579a 55if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
ccc0fff9 56 $lifetime = 60*60*24*30; // 30 days
aa603b14 57 header('HTTP/1.1 304 Not Modified');
ccc0fff9 58 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 59 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9
TL
60 header('Content-Type: '.$mimetype);
61 die;
62}
63
aa42314d 64$parts = explode('&', $parts);
77387297 65$cache = true;
aa42314d
PS
66
67foreach ($parts as $part) {
945f19f7
PS
68 if (empty($part)) {
69 continue;
70 }
aa42314d
PS
71 $part = min_clean_param($part, 'SAFEPATH');
72 $bits = explode('/', $part);
73 if (count($bits) < 2) {
a4738eb3
PS
74 $content .= "\n// Wrong combo resource $part!\n";
75 continue;
aa42314d 76 }
2b722f87
SH
77 //debug($bits);
78 $version = array_shift($bits);
79 if ($version == 'moodle') {
78bfb562 80 //TODO: this is a ugly hack because we should not load any libs here!
2f76f101
ARN
81 if (!defined('MOODLE_INTERNAL')) {
82 define('MOODLE_INTERNAL', true);
83 require_once($CFG->libdir.'/moodlelib.php');
84 }
77387297
SH
85 $revision = (int)array_shift($bits);
86 if ($revision === -1) {
87 // Revision -1 says please don't cache the JS
88 $cache = false;
89 }
2b722f87 90 $frankenstyle = array_shift($bits);
3b17690c 91 $filename = array_pop($bits);
2b722f87 92 $dir = get_component_directory($frankenstyle);
3b17690c
SH
93 if ($mimetype == 'text/css') {
94 $bits[] = 'assets';
95 $bits[] = 'skins';
96 $bits[] = 'sam';
97 }
98 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
2b722f87
SH
99 } else {
100 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
101 $content .= "\n// Wrong yui version $part!\n";
102 continue;
103 }
104 $contentfile = "$CFG->libdir/yui/$part";
aa42314d 105 }
aa42314d 106 if (!file_exists($contentfile) or !is_file($contentfile)) {
56838156 107 $content .= "\n// Combo resource $part ($contentfile) not found!\n";
a4738eb3 108 continue;
aa42314d
PS
109 }
110 $filecontent = file_get_contents($contentfile);
111
6e7b4601
PS
112 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
113
aa42314d 114 if ($mimetype === 'text/css') {
3b17690c 115 if ($version == 'moodle') {
6e7b4601 116 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
3b17690c 117 } else if ($version == 'gallery') {
2a102b90 118 // search for all images in gallery module CSS and serve them through the yui_image.php script
6e7b4601 119 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
2a102b90 120 } else {
4d909122
SH
121 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
122 // I've added this as a separate regex so it can be easily removed once
123 // YUI standardise there CSS methods
6e6034e5 124 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d909122 125
2a102b90 126 // search for all images in yui2 CSS and serve them through the yui_image.php script
6e7b4601 127 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php?file='.$version.'/$1.$2', $filecontent);
2a102b90 128 }
aa42314d
PS
129 }
130
131 $content .= $filecontent;
132}
133
77387297
SH
134if ($cache) {
135 combo_send_cached($content, $mimetype);
136} else {
137 combo_send_uncached($content, $mimetype);
138}
aa42314d
PS
139
140
77387297
SH
141/**
142 * Send the JavaScript cached
143 * @param string $content
144 * @param string $mimetype
145 */
aa42314d 146function combo_send_cached($content, $mimetype) {
ccc0fff9 147 $lifetime = 60*60*24*30; // 30 days
aa42314d
PS
148
149 header('Content-Disposition: inline; filename="combo"');
150 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
151 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
152 header('Pragma: ');
ccc0fff9 153 header('Cache-Control: max-age='.$lifetime);
aa42314d
PS
154 header('Accept-Ranges: none');
155 header('Content-Type: '.$mimetype);
7c986f04
PS
156 if (!min_enable_zlib_compression()) {
157 header('Content-Length: '.strlen($content));
158 }
aa42314d 159
aa42314d
PS
160 echo $content;
161 die;
162}
163
77387297
SH
164/**
165 * Send the JavaScript uncached
166 * @param string $content
167 * @param string $mimetype
168 */
169function combo_send_uncached($content, $mimetype) {
170 header('Content-Disposition: inline; filename="combo"');
171 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
172 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
173 header('Pragma: ');
174 header('Accept-Ranges: none');
175 header('Content-Type: '.$mimetype);
176 if (!min_enable_zlib_compression()) {
177 header('Content-Length: '.strlen($content));
178 }
179
180 echo $content;
181 die;
182}
183
37c82592 184function combo_not_found($message = '') {
aa42314d 185 header('HTTP/1.0 404 not found');
37c82592
PS
186 if ($message) {
187 echo $message;
188 } else {
189 echo 'Combo resource not found, sorry.';
190 }
191 die;
aa42314d
PS
192}
193
194function combo_params() {
37c82592
PS
195 // note: buggy or misconfigured IIS does return the query string in REQUEST_URL
196 if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
197 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
aa42314d
PS
198 return $parts[1];
199
6e7b4601 200 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
aa42314d
PS
201 return $_SERVER['QUERY_STRING'];
202
6e7b4601
PS
203 } else if ($slashargument = min_get_slash_argument()) {
204 $slashargument = ltrim($slashargument, '/');
205 return $slashargument;
206
aa42314d 207 } else {
37c82592
PS
208 // unsupported server, sorry!
209 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314d
PS
210 }
211}