MDL-21241 theme CSS and JS gzip compression + some more fixes
[moodle.git] / theme / yui_combo.php
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.
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/>.
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  */
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();
34 }
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/x-javascript';
42 } else if (substr($parts, -4) === '.css') {
43     $mimetype = 'text/css';
44 } else {
45     combo_not_found();
46 }
48 $parts = explode('&', $parts);
50 foreach ($parts as $part) {
51     if (empty($part)) {
52         continue;
53     }
54     $part = min_clean_param($part, 'SAFEPATH');
55     $bits = explode('/', $part);
56     if (count($bits) < 2) {
57         $content .= "\n// Wrong combo resource $part!\n";
58         continue;
59     }
60     $version = $bits[0];
61     if ($version != $CFG->yui3version and $version != $CFG->yui2version) {
62         $content .= "\n// Wrong yui version $part!\n";
63         continue;
64     }
65     $contentfile = "$CFG->libdir/yui/$part";
66     if (!file_exists($contentfile) or !is_file($contentfile)) {
67         $content .= "\n// Combo resource $part not found!\n";
68         continue;
69     }
70     $filecontent = file_get_contents($contentfile);
72     if ($mimetype === 'text/css') {
73         // search for all images in yui2 CSS and serve them through the yui_image.php script
74         $filecontent = preg_replace('/([a-z-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
75     }
77     $content .= $filecontent;
78 }
81 combo_send_cached($content, $mimetype);
85 function combo_send_cached($content, $mimetype) {
86     $lifetime = 60*60*24*300; // 300 days === forever
88     header('Content-Disposition: inline; filename="combo"');
89     header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
90     header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
91     header('Pragma: ');
92     header('Cache-Control: max-age=315360000');
93     header('Accept-Ranges: none');
94     header('Content-Type: '.$mimetype);
95     if (!min_enable_zlib_compression()) {
96         header('Content-Length: '.strlen($content));
97     }
99     echo $content;
100     die;
103 function combo_not_found() {
104     header('HTTP/1.0 404 not found');
105     die('Combo resource not found, sorry.');
108 function combo_params() {
109     if (!empty($_SERVER['REQUEST_URI'])) {
110         $parts = explode('?', $_SERVER['REQUEST_URI']);
111         if (count($parts) != 2) {
112             return '';
113         }
114         return $parts[1];
116     } else if (!empty($_SERVER['QUERY_STRING'])) {
117         return $_SERVER['QUERY_STRING'];
119     } else {
120         return '';
121     }