MDL-21400 finally, gzip for JS files and proper caching - without admin UI for now...
[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 *
21 * @package moodlecore
22 * @copyright 2010 Petr Skoda (skodak)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// we need just the values from config.php and minlib.php
27define('ABORT_AFTER_CONFIG', true);
28require('../config.php'); // this stops immediately at the beginning of lib/setup.php
29
30$file = min_optional_param('file', '', 'SAFEPATH');
31$rev = min_optional_param('rev', 0, 'INT');
32
33if (empty($file) or strpos($file, '/') !== 0 or !preg_match('/\.js$/', $file)) {
34 die;
35}
36
37$jspath = $CFG->dirroot.$file;
38
39if (file_exists($jspath)) {
40 send_cached_js($jspath);
41}
42
43
44//=================================================================================
45//=== utility functions ==
46// we are not using filelib because we need to fine tune all header
47// parameters to get the best performance.
48
49function send_cached_js($jspath) {
50 $lifetime = 60*60*24*20;
51
52 header('Content-Disposition: inline; filename="javascript.php"');
53 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
54 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
55 header('Pragma: ');
56 header('Accept-Ranges: none');
57 header('Content-Type: application/x-javascript');
58 if (!min_enable_zlib_compression()) {
59 header('Content-Length: '.filesize($jspath));
60 }
61
62 readfile($jspath);
63 die;
64}