MDL-35701 respect case of external column names in enrol_database
[moodle.git] / theme / yui_combo.php
blobbd19b5039cc262dc99c930b0b3b80747c8c5385f
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for serving of yui images
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
27 // disable moodle specific debug messages and any errors in output,
28 // comment out when debugging or better look into error log!
29 define('NO_DEBUG_DISPLAY', true);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 // get special url parameters
37 list($parts, $slasharguments) = combo_params();
38 if (!$parts) {
39 combo_not_found();
42 $etag = sha1($parts);
43 $parts = trim($parts, '&');
45 // find out what we are serving - only one type per request
46 $content = '';
47 if (substr($parts, -3) === '.js') {
48 $mimetype = 'application/javascript';
49 } else if (substr($parts, -4) === '.css') {
50 $mimetype = 'text/css';
51 } else {
52 combo_not_found();
55 // if they are requesting a revision that's not -1, and they have supplied an
56 // If-Modified-Since header, we can send back a 304 Not Modified since the
57 // content never changes (the rev number is increased any time the content changes)
58 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
59 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
60 header('HTTP/1.1 304 Not Modified');
61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
62 header('Cache-Control: public, max-age='.$lifetime);
63 header('Content-Type: '.$mimetype);
64 header('Etag: '.$etag);
65 die;
68 $parts = explode('&', $parts);
69 $cache = true;
70 $lastmodified = 0;
72 foreach ($parts as $part) {
73 if (empty($part)) {
74 continue;
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 //TODO: this is a ugly hack because we should not load any libs here!
86 if (!defined('MOODLE_INTERNAL')) {
87 define('MOODLE_INTERNAL', true);
88 require_once($CFG->libdir.'/moodlelib.php');
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 $dir = get_component_directory($frankenstyle);
98 if ($mimetype == 'text/css') {
99 $bits[] = 'assets';
100 $bits[] = 'skins';
101 $bits[] = 'sam';
103 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
104 } else if ($version === '2in3') {
105 $contentfile = "$CFG->libdir/yuilib/$part";
107 } else if ($version == 'gallery') {
108 $contentfile = "$CFG->libdir/yui/$part";
110 } else {
111 if ($version != $CFG->yui3version) {
112 $content .= "\n// Wrong yui version $part!\n";
113 continue;
115 $contentfile = "$CFG->libdir/yuilib/$part";
117 if (!file_exists($contentfile) or !is_file($contentfile)) {
118 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
119 $content .= "\n// Combo resource $part ($location) not found!\n";
120 continue;
122 $filecontent = file_get_contents($contentfile);
123 $fmodified = filemtime($contentfile);
124 if ($fmodified > $lastmodified) {
125 $lastmodified = $fmodified;
128 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
129 $sep = ($slasharguments ? '/' : '?file=');
131 if ($mimetype === 'text/css') {
132 if ($version == 'moodle') {
133 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
135 } else if ($version == '2in3') {
136 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
137 // I've added this as a separate regex so it can be easily removed once
138 // YUI standardise there CSS methods
139 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
141 // search for all images in yui2 CSS and serve them through the yui_image.php script
142 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
144 } else if ($version == 'gallery') {
145 // search for all images in gallery module CSS and serve them through the yui_image.php script
146 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
148 } else {
149 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
150 // I've added this as a separate regex so it can be easily removed once
151 // YUI standardise there CSS methods
152 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
154 // search for all images in yui2 CSS and serve them through the yui_image.php script
155 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
159 $content .= $filecontent;
162 if ($lastmodified == 0) {
163 $lastmodified = time();
166 if ($cache) {
167 combo_send_cached($content, $mimetype, $etag, $lastmodified);
168 } else {
169 combo_send_uncached($content, $mimetype);
174 * Send the JavaScript cached
175 * @param string $content
176 * @param string $mimetype
177 * @param string $etag
178 * @param int $lastmodified
180 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
181 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
183 header('Content-Disposition: inline; filename="combo"');
184 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
185 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
186 header('Pragma: ');
187 header('Cache-Control: public, max-age='.$lifetime);
188 header('Accept-Ranges: none');
189 header('Content-Type: '.$mimetype);
190 header('Etag: '.$etag);
191 if (!min_enable_zlib_compression()) {
192 header('Content-Length: '.strlen($content));
195 echo $content;
196 die;
200 * Send the JavaScript uncached
201 * @param string $content
202 * @param string $mimetype
204 function combo_send_uncached($content, $mimetype) {
205 header('Content-Disposition: inline; filename="combo"');
206 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
207 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
208 header('Pragma: ');
209 header('Accept-Ranges: none');
210 header('Content-Type: '.$mimetype);
211 if (!min_enable_zlib_compression()) {
212 header('Content-Length: '.strlen($content));
215 echo $content;
216 die;
219 function combo_not_found($message = '') {
220 header('HTTP/1.0 404 not found');
221 if ($message) {
222 echo $message;
223 } else {
224 echo 'Combo resource not found, sorry.';
226 die;
229 function combo_params() {
230 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
231 // url rewriting
232 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
233 return array($slashargument, true);
235 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
236 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
237 return array($parts[1], false);
239 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
240 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
241 return array($_SERVER['QUERY_STRING'], false);
243 } else if ($slashargument = min_get_slash_argument()) {
244 $slashargument = ltrim($slashargument, '/');
245 return array($slashargument, true);
247 } else {
248 // unsupported server, sorry!
249 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');