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