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 serving optimised JS
23 * @copyright 2010 Petr Skoda (skodak)
24 * @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
32 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
33 require_once('Minify.php');
35 $file = min_optional_param('file', '', 'RAW');
36 $rev = min_optional_param('rev', 0, 'INT');
38 // some security first - pick only files with .js extension in dirroot
40 $files = explode(',', $file);
41 foreach ($files as $fsfile) {
42 $jsfile = realpath($CFG->dirroot.$fsfile);
43 if ($jsfile === false) {
47 if (strpos($jsfile, $CFG->dirroot . DIRECTORY_SEPARATOR) !== 0) {
48 // hackers - not in dirroot
51 if (substr($jsfile, -3) !== '.js') {
52 // hackers - not a JS file
59 // bad luck - no valid files
65 function minify($files) {
68 $cachedir = $CFG->dataroot.'/cache/js';
69 // make sure the cache dir exist
70 if (!file_exists($cachedir)) {
71 @mkdir($cachedir, $CFG->directorypermissions, true);
74 if (0 === stripos(PHP_OS, 'win')) {
75 Minify::setDocRoot(); // IIS may need help
77 Minify::setCache($cachedir, true);
80 // Maximum age to cache
81 'maxAge' => (60*60*24*20),
82 // The files to minify
86 Minify::serve('Files', $options);