01191: HandleDownload should respect EnableIMSCaching

Summary: HandleDownload should respect EnableIMSCaching
Created: 2010-05-10 04:43
Status: Closed - added for 2.2.17
Category: Bug
Assigned:
Priority: 5
Version: 2.2.x

Description: Setting the $EnableIMSCaching variable should also enable the caching of attached files. Currently, if $EnableDirectDownload is disabled, all requests for attachments will be replied to with the full file, even if a 304 Not modified reply would be valid.

To fix this, the following patch is required in upload.php:

--- scripts/upload.php	2010-05-10 10:18:32.000000000 +0300
+++ scripts/upload.php	2010-05-10 12:50:07.000000000 +0300
@@ -176,5 +176,5 @@
 function HandleDownload($pagename, $auth = 'read') {
-  global $UploadFileFmt, $UploadExts, $DownloadDisposition;
+  global $UploadFileFmt, $UploadExts, $DownloadDisposition, $EnableIMSCaching;
   SDV($DownloadDisposition, "inline");
   UploadAuth($pagename, $auth);
   $upname = MakeUploadName($pagename, @$_REQUEST['upname']);
@@ -184,6 +184,14 @@
     Abort("?requested file not found");
     exit();
   }
+  if (IsEnabled($EnableIMSCaching, 0)) {
+    header('Cache-Control: ');
+    header('Expires: ');
+    $filelastmod = gmdate('D, d M Y H:i:s \G\M\T', filemtime($filepath));
+    if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] == $filelastmod)
+      { header("HTTP/1.0 304 Not Modified"); exit(); }
+    header("Last-Modified: $filelastmod");
+  }
   preg_match('/\\.([^.]+)$/',$filepath,$match); 
   if ($UploadExts[@$match[1]]) 
     header("Content-Type: {$UploadExts[@$match[1]]}");

Do we want to use header('Cache-Control: no-cache'); to prevent proxies and caches to store the protected file? --Petko May 27, 2010, at 03:46 PM

Could do. header('Cache-Control: private'); is a little bit more lenient and sounds like a more appropriate response, but no-cache would be fine as well. —Eemeli Aro

Private added. Thanks! --Petko

See also