From 1425b39b3d19c1511be1756aba231f85e75780c0 Mon Sep 17 00:00:00 2001
From: Cerdic <cedric@yterium.com>
Date: Fri, 10 Mar 2023 11:58:14 +0100
Subject: [PATCH] refactor: utiliser la fonction interne du core
 `spip_livrer_fichier()`

(cherry picked from commit 1d2d5ac9b7abaa7a7f92467a3cd3966a422c4437)
---
 action/acceder_document.php | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)

diff --git a/action/acceder_document.php b/action/acceder_document.php
index 601c5b6b..f2288fd0 100644
--- a/action/acceder_document.php
+++ b/action/acceder_document.php
@@ -89,42 +89,23 @@ function action_acceder_document_dist() {
 			break;
 
 		default:
-			header('Content-Type: ' . $doc['mime_type']);
-
-			// pour les images ne pas passer en attachment
-			// sinon, lorsqu'on pointe directement sur leur adresse,
-			// le navigateur les downloade au lieu de les afficher
+			include_spip('inc/livrer_fichier');
 
+			$options = ['attachment' => false];
 			if ($doc['inclus'] == 'non') {
+				$options['attachment'] = true;
 				// Si le fichier a un titre avec extension,
 				// ou si c'est un nom bien connu d'Unix, le prendre
 				// sinon l'ignorer car certains navigateurs pataugent
-
-				$f = basename($file);
 				if (
 					isset($doc['titre'])
 					and (preg_match('/^\w+[.]\w+$/', $doc['titre']) or $doc['titre'] == 'Makefile')
 				) {
-					$f = $doc['titre'];
+					$options['attachment'] = $doc['titre'];
 				}
-
-				// ce content-type est necessaire pour eviter des corruptions de zip dans ie6
-				header('Content-Type: application/octet-stream');
-
-				header("Content-Disposition: attachment; filename=\"$f\";");
-				header('Content-Transfer-Encoding: binary');
-
-				// fix for IE catching or PHP bug issue
-				header('Pragma: public');
-				header('Expires: 0'); // set expiration time
-				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
-			}
-
-			if ($cl = filesize($file)) {
-				header('Content-Length: ' . $cl);
 			}
-
-			readfile($file);
+			spip_livrer_fichier($file, $doc['mime_type'], $options);
+			exit;
 			break;
 	}
 }
-- 
GitLab