From ff74627e62e6d1749bb36ec4b096137e3262f3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Fri, 12 Jul 2013 21:48:21 +0200 Subject: [PATCH] MDL-40546 send proper caching headers when $CFG->cachejs = 0 This should help developers writing old style JS that is served via lib/javascript.php. --- lib/javascript.php | 8 +++++--- lib/outputrequirementslib.php | 11 ++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/javascript.php b/lib/javascript.php index 380334f52ad..7a97dfc7ea8 100644 --- a/lib/javascript.php +++ b/lib/javascript.php @@ -42,7 +42,7 @@ if ($slashargument = min_get_slash_argument()) { $file = '/'.min_clean_param($file, 'SAFEPATH'); } else { - $rev = min_optional_param('rev', 0, 'INT'); + $rev = min_optional_param('rev', -1, 'INT'); $file = min_optional_param('jsfile', '', 'RAW'); // 'file' would collide with URL rewriting! } @@ -76,9 +76,11 @@ if (!$jsfiles) { } $etag = sha1($rev.implode(',', $jsfiles)); -$candidate = $CFG->cachedir.'/js/'.$etag; -if ($rev > -1) { +// Use the caching only for meaningful revision numbers which prevents future cache poisoning. +if ($rev > 0 and $rev < (time() + 60*60)) { + $candidate = $CFG->cachedir.'/js/'.$etag; + if (file_exists($candidate)) { if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { // we do not actually need to verify the etag value because our files diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index 76310d94a2f..abbc903c47b 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -613,12 +613,17 @@ class page_requirements_manager { throw new coding_exception('Attempt to require a JavaScript file that does not exist.', $url); } } - if (!empty($CFG->cachejs) and !empty($CFG->jsrev) and $CFG->jsrev > 0 and substr($url, -3) === '.js') { + if (substr($url, -3) === '.js') { + if (empty($CFG->cachejs) or !isset($CFG->jsrev)) { + $jsrev = -1; + } else { + $jsrev = (int)$CFG->jsrev; + } if (empty($CFG->slasharguments)) { - return new moodle_url($CFG->httpswwwroot.'/lib/javascript.php', array('rev'=>$CFG->jsrev, 'jsfile'=>$url)); + return new moodle_url($CFG->httpswwwroot.'/lib/javascript.php', array('rev'=>$jsrev, 'jsfile'=>$url)); } else { $returnurl = new moodle_url($CFG->httpswwwroot.'/lib/javascript.php'); - $returnurl->set_slashargument('/'.$CFG->jsrev.$url); + $returnurl->set_slashargument('/'.$jsrev.$url); return $returnurl; } } else {