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
31 ini_set('zlib.output_compression', 'Off');
34 set_include_path($CFG->libdir
. '/minify/lib' . PATH_SEPARATOR
. get_include_path());
35 require_once('Minify.php');
37 $file = min_optional_param('file', '', 'RAW');
38 $rev = min_optional_param('rev', 0, 'INT');
40 // some security first - pick only files with .js extension in dirroot
42 $files = explode(',', $file);
43 foreach ($files as $fsfile) {
44 $jsfile = realpath($CFG->dirroot
.$fsfile);
45 if ($jsfile === false) {
49 if (strpos($jsfile, $CFG->dirroot
. DIRECTORY_SEPARATOR
) !== 0) {
50 // hackers - not in dirroot
53 if (substr($jsfile, -3) !== '.js') {
54 // hackers - not a JS file
61 // bad luck - no valid files
67 function minify($files) {
70 $cachedir = $CFG->dataroot
.'/cache/js';
71 // make sure the cache dir exist
72 if (!file_exists($cachedir)) {
73 @mkdir
($cachedir, $CFG->directorypermissions
, true);
76 if (0 === stripos(PHP_OS
, 'win')) {
77 Minify
::setDocRoot(); // IIS may need help
79 Minify
::setCache($cachedir, true);
82 // Maximum age to cache
83 'maxAge' => (60*60*24*20),
84 // The files to minify
88 Minify
::serve('Files', $options);