Updated the HEAD build version to 20100519
[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
0139ec3f
PS
33$jspath = $CFG->dirroot.$file;
34
baeb20bb
PS
35if (empty($file) or strpos($file, '/') !== 0 or !preg_match('/\.js$/', $file) or !file_exists($jspath)) {
36 header('HTTP/1.0 404 not found');
37 die('JS file was not found, sorry.');
0139ec3f
PS
38}
39
baeb20bb
PS
40send_cached_js($jspath);
41
0139ec3f
PS
42
43//=================================================================================
44//=== utility functions ==
45// we are not using filelib because we need to fine tune all header
46// parameters to get the best performance.
47
48function send_cached_js($jspath) {
49 $lifetime = 60*60*24*20;
50
51 header('Content-Disposition: inline; filename="javascript.php"');
52 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
53 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
54 header('Pragma: ');
55 header('Accept-Ranges: none');
56 header('Content-Type: application/x-javascript');
57 if (!min_enable_zlib_compression()) {
58 header('Content-Length: '.filesize($jspath));
59 }
60
61 readfile($jspath);
62 die;
63}