3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This file is responsible for serving the one huge CSS of each theme.
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 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32 $rev = min_optional_param('rev', 0, 'INT');
33 $type = min_optional_param('type', 'head', 'RAW');
35 if ($type !== 'head' and $type !== 'footer') {
36 header('HTTP/1.0 404 not found');
37 die('Theme was not found, sorry.');
40 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
42 } else if (!empty($CFG->themedir
) and file_exists("$CFG->themedir/$themename/config.php")) {
45 header('HTTP/1.0 404 not found');
46 die('Theme was not found, sorry.');
49 $candidate = "$CFG->dataroot/cache/theme/$themename/javascript_$type.js";
51 if ($rev > -1 and file_exists($candidate)) {
52 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
53 // we do not actually need to verify the etag value because our files
54 // never change in cache because we increment the rev parameter
55 header('HTTP/1.1 304 Not Modified');
58 send_cached_js($candidate, $rev);
61 //=================================================================================
62 // ok, now we need to start normal moodle script, we need to load all libs and $DB
63 define('ABORT_AFTER_CONFIG_CANCEL', true);
65 define('NO_MOODLE_COOKIES', true); // Session not used here
66 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
68 require("$CFG->dirroot/lib/setup.php");
70 set_include_path($CFG->libdir
. '/minify/lib' . PATH_SEPARATOR
. get_include_path());
71 require_once('Minify.php');
73 $theme = theme_config
::load($themename);
76 check_dir_exists(dirname($candidate));
77 $fp = fopen($candidate, 'w');
78 fwrite($fp, minify($theme->javascript_files($type)));
80 send_cached_js($candidate);
82 send_uncached_js($theme->javascript_content($type));
85 //=================================================================================
86 //=== utility functions ==
87 // we are not using filelib because we need to fine tune all header
88 // parameters to get the best performance.
90 function send_cached_js($jspath) {
91 $lifetime = 60*60*24*20;
93 header('Content-Disposition: inline; filename="javascript.php"');
94 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
95 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
97 header('Accept-Ranges: none');
98 header('Content-Type: application/javascript; charset=utf-8');
99 if (!min_enable_zlib_compression()) {
100 header('Content-Length: '.filesize($jspath));
107 function send_uncached_js($js) {
108 header('Content-Disposition: inline; filename="javascript.php"');
109 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
110 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
2) .' GMT');
112 header('Accept-Ranges: none');
113 header('Content-Type: application/javascript; charset=utf-8');
114 header('Content-Length: '.strlen($js));
120 function minify($files) {
123 if (0 === stripos(PHP_OS
, 'win')) {
124 Minify
::setDocRoot(); // IIS may need help
126 Minify
::setCache('', true);
129 'bubbleCssImports' => false,
130 // Don't gzip content we just want text for storage
131 'encodeOutput' => false,
132 // Maximum age to cache, not used but required
134 // The files to minify
136 // Turn orr URI rewriting
137 'rewriteCssUris' => false,
138 // This returns the CSS rather than echoing it for display
142 $result = Minify
::serve('Files', $options);
143 return $result['content'];