MDL-32612 respect NO_DEBUG_DISPLAY in scripts that need only config.php
[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 76 //TODO: this is a ugly hack because we should not load any libs here!
2f76f101
ARN
77 if (!defined('MOODLE_INTERNAL')) {
78 define('MOODLE_INTERNAL', true);
79 require_once($CFG->libdir.'/moodlelib.php');
80 }
77387297
SH
81 $revision = (int)array_shift($bits);
82 if ($revision === -1) {
83 // Revision -1 says please don't cache the JS
84 $cache = false;
85 }
2b722f87 86 $frankenstyle = array_shift($bits);
3b17690c 87 $filename = array_pop($bits);
2b722f87 88 $dir = get_component_directory($frankenstyle);
3b17690c
SH
89 if ($mimetype == 'text/css') {
90 $bits[] = 'assets';
91 $bits[] = 'skins';
92 $bits[] = 'sam';
93 }
94 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
2b722f87
SH
95 } else {
96 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
97 $content .= "\n// Wrong yui version $part!\n";
98 continue;
99 }
100 $contentfile = "$CFG->libdir/yui/$part";
aa42314d 101 }
aa42314d 102 if (!file_exists($contentfile) or !is_file($contentfile)) {
56838156 103 $content .= "\n// Combo resource $part ($contentfile) not found!\n";
a4738eb3 104 continue;
aa42314d
PS
105 }
106 $filecontent = file_get_contents($contentfile);
107
108 if ($mimetype === 'text/css') {
3b17690c 109 if ($version == 'moodle') {
6e6034e5 110 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
3b17690c 111 } else if ($version == 'gallery') {
2a102b90 112 // search for all images in gallery module CSS and serve them through the yui_image.php script
6e6034e5 113 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
2a102b90 114 } else {
4d909122
SH
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
6e6034e5 118 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d909122 119
2a102b90 120 // search for all images in yui2 CSS and serve them through the yui_image.php script
6e6034e5 121 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
2a102b90 122 }
aa42314d
PS
123 }
124
125 $content .= $filecontent;
126}
127
77387297
SH
128if ($cache) {
129 combo_send_cached($content, $mimetype);
130} else {
131 combo_send_uncached($content, $mimetype);
132}
aa42314d
PS
133
134
77387297
SH
135/**
136 * Send the JavaScript cached
137 * @param string $content
138 * @param string $mimetype
139 */
aa42314d 140function combo_send_cached($content, $mimetype) {
ccc0fff9 141 $lifetime = 60*60*24*30; // 30 days
aa42314d
PS
142
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');
146 header('Pragma: ');
ccc0fff9 147 header('Cache-Control: max-age='.$lifetime);
aa42314d
PS
148 header('Accept-Ranges: none');
149 header('Content-Type: '.$mimetype);
7c986f04
PS
150 if (!min_enable_zlib_compression()) {
151 header('Content-Length: '.strlen($content));
152 }
aa42314d 153
aa42314d
PS
154 echo $content;
155 die;
156}
157
77387297
SH
158/**
159 * Send the JavaScript uncached
160 * @param string $content
161 * @param string $mimetype
162 */
163function 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');
167 header('Pragma: ');
168 header('Accept-Ranges: none');
169 header('Content-Type: '.$mimetype);
170 if (!min_enable_zlib_compression()) {
171 header('Content-Length: '.strlen($content));
172 }
173
174 echo $content;
175 die;
176}
177
37c82592 178function combo_not_found($message = '') {
aa42314d 179 header('HTTP/1.0 404 not found');
37c82592
PS
180 if ($message) {
181 echo $message;
182 } else {
183 echo 'Combo resource not found, sorry.';
184 }
185 die;
aa42314d
PS
186}
187
188function combo_params() {
37c82592
PS
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);
aa42314d
PS
192 return $parts[1];
193
37c82592 194 } else if (isset($_SERVER['QUERY_STRING'])) {
aa42314d
PS
195 return $_SERVER['QUERY_STRING'];
196
197 } else {
37c82592
PS
198 // unsupported server, sorry!
199 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314d
PS
200 }
201}