NOBUG: Turned off zlib compression when serving javascript files.
[moodle.git] / lib / javascript.php
CommitLineData
0139ec3f
PS
1<?php
2
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/>.
17
18/**
19 * This file is serving optimised JS
20 *
78bfb562
PS
21 * @package core
22 * @subpackage lib
23 * @copyright 2010 Petr Skoda (skodak)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0139ec3f
PS
25 */
26
27// we need just the values from config.php and minlib.php
28define('ABORT_AFTER_CONFIG', true);
29require('../config.php'); // this stops immediately at the beginning of lib/setup.php
30
4c066ce7
SH
31ini_set('zlib.output_compression', 'Off');
32
045f492c
SH
33// setup include path
34set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
35require_once('Minify.php');
0139ec3f 36
045f492c
SH
37$file = min_optional_param('file', '', 'RAW');
38$rev = min_optional_param('rev', 0, 'INT');
0139ec3f 39
7a2df05c
PS
40// some security first - pick only files with .js extension in dirroot
41$jsfiles = array();
42$files = explode(',', $file);
43foreach ($files as $fsfile) {
44 $jsfile = realpath($CFG->dirroot.$fsfile);
45 if ($jsfile === false) {
46 // does not exist
47 continue;
5a75ac0a 48 }
7a2df05c
PS
49 if (strpos($jsfile, $CFG->dirroot . DIRECTORY_SEPARATOR) !== 0) {
50 // hackers - not in dirroot
51 continue;
52 }
5ac09e8b 53 if (substr($jsfile, -3) !== '.js') {
7a2df05c
PS
54 // hackers - not a JS file
55 continue;
56 }
57 $jsfiles[] = $jsfile;
58}
59
60if (!$jsfiles) {
61 // bad luck - no valid files
62 die();
0139ec3f
PS
63}
64
045f492c 65minify($jsfiles);
baeb20bb 66
045f492c
SH
67function minify($files) {
68 global $CFG;
0139ec3f 69
09c48cea
PS
70 $cachedir = $CFG->dataroot.'/cache/js';
71 // make sure the cache dir exist
72 if (!file_exists($cachedir)) {
73 @mkdir($cachedir, $CFG->directorypermissions, true);
74 }
75
045f492c
SH
76 if (0 === stripos(PHP_OS, 'win')) {
77 Minify::setDocRoot(); // IIS may need help
78 }
09c48cea 79 Minify::setCache($cachedir, true);
0139ec3f 80
045f492c
SH
81 $options = array(
82 // Maximum age to cache
83 'maxAge' => (60*60*24*20),
84 // The files to minify
85 'files' => $files
86 );
0139ec3f 87
045f492c
SH
88 Minify::serve('Files', $options);
89 die();
5a75ac0a 90}