Merge branch 'install_20_STABLE' of git://github.com/amosbot/moodle into MOODLE_20_STABLE
[moodle.git] / theme / yui_combo.php
blob85c63b09448a9d5c4a106c82ee657dcbeb2b11ac
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 $parts = explode('&', $parts);
49 $cache = true;
51 foreach ($parts as $part) {
52 if (empty($part)) {
53 continue;
55 $part = min_clean_param($part, 'SAFEPATH');
56 $bits = explode('/', $part);
57 if (count($bits) < 2) {
58 $content .= "\n// Wrong combo resource $part!\n";
59 continue;
61 //debug($bits);
62 $version = array_shift($bits);
63 if ($version == 'moodle') {
64 //TODO: this is a ugly hack because we should not load any libs here!
65 define('MOODLE_INTERNAL', true);
66 require_once($CFG->libdir.'/moodlelib.php');
67 $revision = (int)array_shift($bits);
68 if ($revision === -1) {
69 // Revision -1 says please don't cache the JS
70 $cache = false;
72 $frankenstyle = array_shift($bits);
73 $filename = array_pop($bits);
74 $dir = get_component_directory($frankenstyle);
75 if ($mimetype == 'text/css') {
76 $bits[] = 'assets';
77 $bits[] = 'skins';
78 $bits[] = 'sam';
80 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
81 } else {
82 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
83 $content .= "\n// Wrong yui version $part!\n";
84 continue;
86 $contentfile = "$CFG->libdir/yui/$part";
88 if (!file_exists($contentfile) or !is_file($contentfile)) {
89 $content .= "\n// Combo resource $part not found!\n";
90 continue;
92 $filecontent = file_get_contents($contentfile);
94 if ($mimetype === 'text/css') {
95 if ($version == 'moodle') {
96 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
97 } else if ($version == 'gallery') {
98 // search for all images in gallery module CSS and serve them through the yui_image.php script
99 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
100 } else {
101 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
102 // I've added this as a separate regex so it can be easily removed once
103 // YUI standardise there CSS methods
104 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
106 // search for all images in yui2 CSS and serve them through the yui_image.php script
107 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
111 $content .= $filecontent;
114 if ($cache) {
115 combo_send_cached($content, $mimetype);
116 } else {
117 combo_send_uncached($content, $mimetype);
122 * Send the JavaScript cached
123 * @param string $content
124 * @param string $mimetype
126 function combo_send_cached($content, $mimetype) {
127 $lifetime = 60*60*24*300; // 300 days === forever
129 header('Content-Disposition: inline; filename="combo"');
130 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
131 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
132 header('Pragma: ');
133 header('Cache-Control: max-age=315360000');
134 header('Accept-Ranges: none');
135 header('Content-Type: '.$mimetype);
136 if (!min_enable_zlib_compression()) {
137 header('Content-Length: '.strlen($content));
140 echo $content;
141 die;
145 * Send the JavaScript uncached
146 * @param string $content
147 * @param string $mimetype
149 function combo_send_uncached($content, $mimetype) {
150 header('Content-Disposition: inline; filename="combo"');
151 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
152 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
153 header('Pragma: ');
154 header('Accept-Ranges: none');
155 header('Content-Type: '.$mimetype);
156 if (!min_enable_zlib_compression()) {
157 header('Content-Length: '.strlen($content));
160 echo $content;
161 die;
164 function combo_not_found($message = '') {
165 header('HTTP/1.0 404 not found');
166 if ($message) {
167 echo $message;
168 } else {
169 echo 'Combo resource not found, sorry.';
171 die;
174 function combo_params() {
175 // note: buggy or misconfigured IIS does return the query string in REQUEST_URL
176 if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
177 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
178 return $parts[1];
180 } else if (isset($_SERVER['QUERY_STRING'])) {
181 return $_SERVER['QUERY_STRING'];
183 } else {
184 // unsupported server, sorry!
185 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');