Merge branch 'install_master' of git://github.com/amosbot/moodle
[moodle.git] / theme / yui_combo.php
blob8683cd6953d8dce14fe45e9c7d4d8f7c51e410f7
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
36 if (!$parts = combo_params()) {
37 combo_not_found();
40 $parts = trim($parts, '&');
42 // find out what we are serving - only one type per request
43 $content = '';
44 if (substr($parts, -3) === '.js') {
45 $mimetype = 'application/javascript';
46 } else if (substr($parts, -4) === '.css') {
47 $mimetype = 'text/css';
48 } else {
49 combo_not_found();
52 // if they are requesting a revision that's not -1, and they have supplied an
53 // If-Modified-Since header, we can send back a 304 Not Modified since the
54 // content never changes (the rev number is increased any time the content changes)
55 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
56 $lifetime = 60*60*24*30; // 30 days
57 header('HTTP/1.1 304 Not Modified');
58 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
59 header('Cache-Control: max-age='.$lifetime);
60 header('Content-Type: '.$mimetype);
61 die;
64 $parts = explode('&', $parts);
65 $cache = true;
67 foreach ($parts as $part) {
68 if (empty($part)) {
69 continue;
71 $part = min_clean_param($part, 'SAFEPATH');
72 $bits = explode('/', $part);
73 if (count($bits) < 2) {
74 $content .= "\n// Wrong combo resource $part!\n";
75 continue;
77 //debug($bits);
78 $version = array_shift($bits);
79 if ($version == 'moodle') {
80 //TODO: this is a ugly hack because we should not load any libs here!
81 if (!defined('MOODLE_INTERNAL')) {
82 define('MOODLE_INTERNAL', true);
83 require_once($CFG->libdir.'/moodlelib.php');
85 $revision = (int)array_shift($bits);
86 if ($revision === -1) {
87 // Revision -1 says please don't cache the JS
88 $cache = false;
90 $frankenstyle = array_shift($bits);
91 $filename = array_pop($bits);
92 $dir = get_component_directory($frankenstyle);
93 if ($mimetype == 'text/css') {
94 $bits[] = 'assets';
95 $bits[] = 'skins';
96 $bits[] = 'sam';
98 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
99 } else {
100 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
101 $content .= "\n// Wrong yui version $part!\n";
102 continue;
104 $contentfile = "$CFG->libdir/yui/$part";
106 if (!file_exists($contentfile) or !is_file($contentfile)) {
107 $content .= "\n// Combo resource $part ($contentfile) not found!\n";
108 continue;
110 $filecontent = file_get_contents($contentfile);
112 if ($mimetype === 'text/css') {
113 if ($version == 'moodle') {
114 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
115 } else if ($version == 'gallery') {
116 // search for all images in gallery module CSS and serve them through the yui_image.php script
117 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
118 } else {
119 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
120 // I've added this as a separate regex so it can be easily removed once
121 // YUI standardise there CSS methods
122 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
124 // search for all images in yui2 CSS and serve them through the yui_image.php script
125 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
129 $content .= $filecontent;
132 if ($cache) {
133 combo_send_cached($content, $mimetype);
134 } else {
135 combo_send_uncached($content, $mimetype);
140 * Send the JavaScript cached
141 * @param string $content
142 * @param string $mimetype
144 function combo_send_cached($content, $mimetype) {
145 $lifetime = 60*60*24*30; // 30 days
147 header('Content-Disposition: inline; filename="combo"');
148 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
149 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
150 header('Pragma: ');
151 header('Cache-Control: max-age='.$lifetime);
152 header('Accept-Ranges: none');
153 header('Content-Type: '.$mimetype);
154 if (!min_enable_zlib_compression()) {
155 header('Content-Length: '.strlen($content));
158 echo $content;
159 die;
163 * Send the JavaScript uncached
164 * @param string $content
165 * @param string $mimetype
167 function combo_send_uncached($content, $mimetype) {
168 header('Content-Disposition: inline; filename="combo"');
169 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
170 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
171 header('Pragma: ');
172 header('Accept-Ranges: none');
173 header('Content-Type: '.$mimetype);
174 if (!min_enable_zlib_compression()) {
175 header('Content-Length: '.strlen($content));
178 echo $content;
179 die;
182 function combo_not_found($message = '') {
183 header('HTTP/1.0 404 not found');
184 if ($message) {
185 echo $message;
186 } else {
187 echo 'Combo resource not found, sorry.';
189 die;
192 function combo_params() {
193 // note: buggy or misconfigured IIS does return the query string in REQUEST_URL
194 if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
195 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
196 return $parts[1];
198 } else if (isset($_SERVER['QUERY_STRING'])) {
199 return $_SERVER['QUERY_STRING'];
201 } else {
202 // unsupported server, sorry!
203 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');