MDL-40392 Navigation -> my courses listing tests
[moodle.git] / lib / javascript.php
blob380334f52add48c252a96da33d479647ae4965d4
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is serving optimised JS
20 * @package core_lib
21 * @copyright 2010 Petr Skoda (skodak)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 // disable moodle specific debug messages and any errors in output,
26 // comment out when debugging or better look into error log!
27 define('NO_DEBUG_DISPLAY', true);
29 // we need just the values from config.php and minlib.php
30 define('ABORT_AFTER_CONFIG', true);
31 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
32 require_once("$CFG->dirroot/lib/jslib.php");
34 if ($slashargument = min_get_slash_argument()) {
35 $slashargument = ltrim($slashargument, '/');
36 if (substr_count($slashargument, '/') < 1) {
37 image_not_found();
39 // image must be last because it may contain "/"
40 list($rev, $file) = explode('/', $slashargument, 2);
41 $rev = min_clean_param($rev, 'INT');
42 $file = '/'.min_clean_param($file, 'SAFEPATH');
44 } else {
45 $rev = min_optional_param('rev', 0, 'INT');
46 $file = min_optional_param('jsfile', '', 'RAW'); // 'file' would collide with URL rewriting!
49 // some security first - pick only files with .js extension in dirroot
50 $jsfiles = array();
51 $files = explode(',', $file);
52 foreach ($files as $fsfile) {
53 $jsfile = realpath($CFG->dirroot.$fsfile);
54 if ($jsfile === false) {
55 // does not exist
56 continue;
58 if ($CFG->dirroot === '/') {
59 // Some shared hosting sites serve files directly from '/',
60 // this is NOT supported, but at least allow JS when showing
61 // errors and warnings.
62 } else if (strpos($jsfile, $CFG->dirroot . DIRECTORY_SEPARATOR) !== 0) {
63 // hackers - not in dirroot
64 continue;
66 if (substr($jsfile, -3) !== '.js') {
67 // hackers - not a JS file
68 continue;
70 $jsfiles[] = $jsfile;
73 if (!$jsfiles) {
74 // bad luck - no valid files
75 die();
78 $etag = sha1($rev.implode(',', $jsfiles));
79 $candidate = $CFG->cachedir.'/js/'.$etag;
81 if ($rev > -1) {
82 if (file_exists($candidate)) {
83 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
84 // we do not actually need to verify the etag value because our files
85 // never change in cache because we increment the rev parameter
86 js_send_unmodified(filemtime($candidate), $etag);
88 js_send_cached($candidate, $etag);
90 } else {
91 js_write_cache_file_content($candidate, js_minify($jsfiles));
92 // verify nothing failed in cache file creation
93 clearstatcache();
94 if (file_exists($candidate)) {
95 js_send_cached($candidate, $etag);
100 $content = '';
101 foreach ($jsfiles as $jsfile) {
102 $content .= file_get_contents($jsfile)."\n";
104 js_send_uncached($content, $etag);