Merge branch 'wip-MDL-25454-master' of git://github.com/marinaglancy/moodle
[moodle.git] / theme / yui_combo.php
blob8d46a1d7148cee48d43f44a9ead02bc966d224d8
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 // we need just the values from config.php and minlib.php
28 define('ABORT_AFTER_CONFIG', true);
29 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
31 // get special url parameters
32 if (!$parts = combo_params()) {
33 combo_not_found();
36 $parts = trim($parts, '&');
38 // find out what we are serving - only one type per request
39 $content = '';
40 if (substr($parts, -3) === '.js') {
41 $mimetype = 'application/javascript';
42 } else if (substr($parts, -4) === '.css') {
43 $mimetype = 'text/css';
44 } else {
45 combo_not_found();
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)
51 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
52 $lifetime = 60*60*24*30; // 30 days
53 header('HTTP/1.1 304 Not Modified');
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;
60 $parts = explode('&', $parts);
61 $cache = true;
63 foreach ($parts as $part) {
64 if (empty($part)) {
65 continue;
67 $part = min_clean_param($part, 'SAFEPATH');
68 $bits = explode('/', $part);
69 if (count($bits) < 2) {
70 $content .= "\n// Wrong combo resource $part!\n";
71 continue;
73 //debug($bits);
74 $version = array_shift($bits);
75 if ($version == 'moodle') {
76 //TODO: this is a ugly hack because we should not load any libs here!
77 define('MOODLE_INTERNAL', true);
78 require_once($CFG->libdir.'/moodlelib.php');
79 $revision = (int)array_shift($bits);
80 if ($revision === -1) {
81 // Revision -1 says please don't cache the JS
82 $cache = false;
84 $frankenstyle = array_shift($bits);
85 $filename = array_pop($bits);
86 $dir = get_component_directory($frankenstyle);
87 if ($mimetype == 'text/css') {
88 $bits[] = 'assets';
89 $bits[] = 'skins';
90 $bits[] = 'sam';
92 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
93 } else {
94 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
95 $content .= "\n// Wrong yui version $part!\n";
96 continue;
98 $contentfile = "$CFG->libdir/yui/$part";
100 if (!file_exists($contentfile) or !is_file($contentfile)) {
101 $content .= "\n// Combo resource $part not found!\n";
102 continue;
104 $filecontent = file_get_contents($contentfile);
106 if ($mimetype === 'text/css') {
107 if ($version == 'moodle') {
108 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
109 } else if ($version == 'gallery') {
110 // search for all images in gallery module CSS and serve them through the yui_image.php script
111 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
112 } else {
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
116 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
118 // search for all images in yui2 CSS and serve them through the yui_image.php script
119 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
123 $content .= $filecontent;
126 if ($cache) {
127 combo_send_cached($content, $mimetype);
128 } else {
129 combo_send_uncached($content, $mimetype);
134 * Send the JavaScript cached
135 * @param string $content
136 * @param string $mimetype
138 function combo_send_cached($content, $mimetype) {
139 $lifetime = 60*60*24*30; // 30 days
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: ');
145 header('Cache-Control: max-age='.$lifetime);
146 header('Accept-Ranges: none');
147 header('Content-Type: '.$mimetype);
148 if (!min_enable_zlib_compression()) {
149 header('Content-Length: '.strlen($content));
152 echo $content;
153 die;
157 * Send the JavaScript uncached
158 * @param string $content
159 * @param string $mimetype
161 function 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));
172 echo $content;
173 die;
176 function combo_not_found($message = '') {
177 header('HTTP/1.0 404 not found');
178 if ($message) {
179 echo $message;
180 } else {
181 echo 'Combo resource not found, sorry.';
183 die;
186 function combo_params() {
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);
190 return $parts[1];
192 } else if (isset($_SERVER['QUERY_STRING'])) {
193 return $_SERVER['QUERY_STRING'];
195 } else {
196 // unsupported server, sorry!
197 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');