MDL-24355 tag: minor clean up of tags changes
[moodle.git] / lib / javascript.php
blob9e19def57194a91eca44c363dc41d296f0b05449
1 <?php
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.
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/>.
18 /**
19 * This file is serving optimised JS
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
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');
33 // setup include path
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
41 $jsfiles = array();
42 $files = explode(',', $file);
43 foreach ($files as $fsfile) {
44 $jsfile = realpath($CFG->dirroot.$fsfile);
45 if ($jsfile === false) {
46 // does not exist
47 continue;
49 if (strpos($jsfile, $CFG->dirroot . DIRECTORY_SEPARATOR) !== 0) {
50 // hackers - not in dirroot
51 continue;
53 if (substr($jsfile, -3) !== '.js') {
54 // hackers - not a JS file
55 continue;
57 $jsfiles[] = $jsfile;
60 if (!$jsfiles) {
61 // bad luck - no valid files
62 die();
65 minify($jsfiles);
67 function minify($files) {
68 global $CFG;
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);
81 $options = array(
82 // Maximum age to cache
83 'maxAge' => (60*60*24*20),
84 // The files to minify
85 'files' => $files
88 Minify::serve('Files', $options);
89 die();