From 27ed3a064475b6043ab82fdda121129c0c0e0cec Mon Sep 17 00:00:00 2001 From: meirzamoodle Date: Fri, 9 May 2025 10:46:07 +0700 Subject: [PATCH] MDL-83350 files: Avoid cacheing user and file repo files Set the lifetime to zero for files referenced in the local/internal repository (private files and file system), because they should not be cached. Add `no-store` to the cache-control to ensure resources are never cached, always fetching the latest version. --- lib/filelib.php | 4 ++-- repository/filesystem/lib.php | 2 ++ repository/user/lib.php | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index 98c487b36b3..d8161d0461b 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2609,8 +2609,8 @@ function send_file($path, $filename, $lifetime = null , $filter=0, $pathisstring header('Cache-Control: private, max-age=10, no-transform'); header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); header('Pragma: '); - } else { //normal http - prevent caching at all cost - header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0, no-transform'); + } else { // Normal http - prevent caching at all cost. + header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0, no-transform', 'no-store'); header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); header('Pragma: no-cache'); } diff --git a/repository/filesystem/lib.php b/repository/filesystem/lib.php index c0c62ccac1f..ac0cf1ae8bd 100644 --- a/repository/filesystem/lib.php +++ b/repository/filesystem/lib.php @@ -622,6 +622,8 @@ class repository_filesystem extends repository { $filename = $options['filename']; } $dontdie = ($options && isset($options['dontdie'])); + // If the file is linked to the original then we should not cache it. + $lifetime = 0; send_file($file, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie); } else { send_file_not_found(); diff --git a/repository/user/lib.php b/repository/user/lib.php index 6ae186123af..6b25e60cc20 100644 --- a/repository/user/lib.php +++ b/repository/user/lib.php @@ -167,4 +167,12 @@ class repository_user extends repository { public function contains_private_data() { return false; } + + #[\Override] + public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, ?array $options = null) { + // If the file is linked to the original then we should not cache it. + $lifetime = 0; + + parent::send_file($storedfile, $lifetime, $filter, $forcedownload, $options); + } }