MDL-32262 theme_afterburner: fixed missing comma in afterburner_styles.css
[moodle.git] / lib / javascript.php
blobbaae4c025bc2e4a2cda98fc32fe426ccb1483e67
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 ($CFG->dirroot === '/') {
50 // Some shared hosting sites serve files directly from '/',
51 // this is NOT supported, but at least allow JS when showing
52 // errors and warnings.
53 } else if (strpos($jsfile, $CFG->dirroot . DIRECTORY_SEPARATOR) !== 0) {
54 // hackers - not in dirroot
55 continue;
57 if (substr($jsfile, -3) !== '.js') {
58 // hackers - not a JS file
59 continue;
61 $jsfiles[] = $jsfile;
64 if (!$jsfiles) {
65 // bad luck - no valid files
66 die();
69 minify($jsfiles);
71 function minify($files) {
72 global $CFG;
74 $cachedir = $CFG->cachedir.'/js';
75 // make sure the cache dir exist
76 if (!file_exists($cachedir)) {
77 @mkdir($cachedir, $CFG->directorypermissions, true);
80 if (0 === stripos(PHP_OS, 'win')) {
81 Minify::setDocRoot(); // IIS may need help
83 Minify::setCache($cachedir, true);
85 $options = array(
86 // Maximum age to cache
87 'maxAge' => (60*60*24*20),
88 // The files to minify
89 'files' => $files
92 try {
93 Minify::serve('Files', $options);
94 die();
95 } catch (Exception $e) {
96 $error = $e->getMessage();
97 $error = str_replace("\r", ' ', $error);
98 $error = str_replace("\n", ' ', $error);
101 // minification failed - try to inform the developer and include the non-minified version
102 $js = <<<EOD
103 try {console.log('Error: Minimisation of javascript failed!');} catch (e) {}
105 // Error: $error
106 // Problem detected during javascript minimisation, please review the following code
107 // =================================================================================
110 EOD;
111 echo $js;
112 foreach ($files as $jsfile) {
113 echo file_get_contents($jsfile)."\n";