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