From 40458b7bf8c1c8746dc0327e7befbe960eb7adc6 Mon Sep 17 00:00:00 2001
From: Cerdic <cedric@yterium.com>
Date: Tue, 9 Feb 2021 12:01:42 +0100
Subject: [PATCH] sortir la verification des tailles acceptable dans une
 fonction surchargeable inc_verifier_taille_document_acceptable_dist qui
 recoit en plus un flag pour distinguer un logo d'une image normale

---
 action/ajouter_documents.php                |  76 ++------------
 inc/verifier_taille_document_acceptable.php | 107 ++++++++++++++++++++
 2 files changed, 113 insertions(+), 70 deletions(-)
 create mode 100644 inc/verifier_taille_document_acceptable.php

diff --git a/action/ajouter_documents.php b/action/ajouter_documents.php
index ff43e320..91f6f74b 100644
--- a/action/ajouter_documents.php
+++ b/action/ajouter_documents.php
@@ -521,78 +521,14 @@ function fixer_fichier_upload($file, $mode = '') {
  */
 function verifier_taille_document_acceptable(&$infos) {
 
-	// si ce n'est pas une image
-	if (!$infos['type_image']) {
-		if (defined('_DOC_MAX_SIZE') and _DOC_MAX_SIZE > 0 and $infos['taille'] > _DOC_MAX_SIZE * 1024) {
-			return _T(
-				'medias:info_doc_max_poids',
-				array(
-					'maxi' => taille_en_octets(_DOC_MAX_SIZE * 1024),
-					'actuel' => taille_en_octets($infos['taille'])
-				)
-			);
-		}
-	} // si c'est une image
-	else {
-		if ((defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH and $infos['largeur'] > _IMG_MAX_WIDTH)
-			or (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT and $infos['hauteur'] > _IMG_MAX_HEIGHT)
-		) {
-			$max_width = (defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH) ? _IMG_MAX_WIDTH : '*';
-			$max_height = (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT) ? _IMG_MAX_HEIGHT : '*';
-
-			// pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures
-			// on met directement a la taille maxi a la volee
-			if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') {
-				include_spip('inc/filtres');
-				$img = filtrer('image_reduire', $infos['fichier'], $max_width, $max_height);
-				$img = extraire_attribut($img, 'src');
-				$img = supprimer_timestamp($img);
-				if (@file_exists($img) and $img !== $infos['fichier']) {
-					spip_unlink($infos['fichier']);
-					@rename($img, $infos['fichier']);
-					list($h, $w) = taille_image($infos['fichier'], true);
-					$infos['largeur'] = $w;
-					$infos['hauteur'] = $h;
-					$infos['taille'] = @filesize($infos['fichier']);
-				}
-			}
+	$is_logo = in_array($infos['mode'], ['logoon', 'logooff']);
 
-			if ((defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH and $infos['largeur'] > _IMG_MAX_WIDTH)
-				or (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT and $infos['hauteur'] > _IMG_MAX_HEIGHT)
-			) {
-				return _T(
-					'medias:info_image_max_taille',
-					array(
-						'maxi' =>
-							_T(
-								'info_largeur_vignette',
-								array(
-									'largeur_vignette' => $max_width,
-									'hauteur_vignette' => $max_height
-								)
-							),
-						'actuel' =>
-							_T(
-								'info_largeur_vignette',
-								array(
-									'largeur_vignette' => $infos['largeur'],
-									'hauteur_vignette' => $infos['hauteur']
-								)
-							)
-					)
-				);
-			}
-		}
+	$verifier_taille_document_acceptable = charger_fonction('verifier_taille_document_acceptable', 'inc');
+	$res = $verifier_taille_document_acceptable($infos, $is_logo);
 
-		if (defined('_IMG_MAX_SIZE') and _IMG_MAX_SIZE > 0 and $infos['taille'] > _IMG_MAX_SIZE * 1024) {
-			return _T(
-				'medias:info_image_max_poids',
-				array(
-					'maxi' => taille_en_octets(_IMG_MAX_SIZE * 1024),
-					'actuel' => taille_en_octets($infos['taille'])
-				)
-			);
-		}
+	// si erreur, on arrete la
+	if ($res !== true) {
+		return $res;
 	}
 
 	// verifier en fonction du mode si une fonction est proposee
diff --git a/inc/verifier_taille_document_acceptable.php b/inc/verifier_taille_document_acceptable.php
new file mode 100644
index 00000000..8ac0c895
--- /dev/null
+++ b/inc/verifier_taille_document_acceptable.php
@@ -0,0 +1,107 @@
+<?php
+/***************************************************************************\
+ *  SPIP, Système de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright © avec tendresse depuis 2001                                 *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
+ *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+/**
+ * Gestion des vignettes de types de fichier
+ *
+ * @package SPIP\Medias\Vignette
+ **/
+
+if (!defined('_ECRIRE_INC_VERSION')) {
+	return;
+}
+
+
+/**
+ * Verifier si le fichier respecte les contraintes de tailles
+ *
+ * @param  array $infos
+ * @param  bool $is_logo
+ * @return bool|mixed|string
+ */
+function inc_verifier_taille_document_acceptable_dist(&$infos, $is_logo = false) {
+
+	// si ce n'est pas une image
+	if (!$infos['type_image']) {
+		if (defined('_DOC_MAX_SIZE') and _DOC_MAX_SIZE > 0 and $infos['taille'] > _DOC_MAX_SIZE * 1024) {
+			return _T(
+				'medias:info_doc_max_poids',
+				array(
+					'maxi' => taille_en_octets(_DOC_MAX_SIZE * 1024),
+					'actuel' => taille_en_octets($infos['taille'])
+				)
+			);
+		}
+	} // si c'est une image
+	else {
+		if ((defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH and $infos['largeur'] > _IMG_MAX_WIDTH)
+			or (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT and $infos['hauteur'] > _IMG_MAX_HEIGHT)
+		) {
+			$max_width = (defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH) ? _IMG_MAX_WIDTH : '*';
+			$max_height = (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT) ? _IMG_MAX_HEIGHT : '*';
+
+			// pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures
+			// on met directement a la taille maxi a la volee
+			if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') {
+				include_spip('inc/filtres');
+				$img = filtrer('image_reduire', $infos['fichier'], $max_width, $max_height);
+				$img = extraire_attribut($img, 'src');
+				$img = supprimer_timestamp($img);
+				if (@file_exists($img) and $img !== $infos['fichier']) {
+					spip_unlink($infos['fichier']);
+					@rename($img, $infos['fichier']);
+					list($h, $w) = taille_image($infos['fichier'], true);
+					$infos['largeur'] = $w;
+					$infos['hauteur'] = $h;
+					$infos['taille'] = @filesize($infos['fichier']);
+				}
+			}
+
+			if ((defined('_IMG_MAX_WIDTH') and _IMG_MAX_WIDTH and $infos['largeur'] > _IMG_MAX_WIDTH)
+				or (defined('_IMG_MAX_HEIGHT') and _IMG_MAX_HEIGHT and $infos['hauteur'] > _IMG_MAX_HEIGHT)
+			) {
+				return _T(
+					'medias:info_image_max_taille',
+					array(
+						'maxi' =>
+							_T(
+								'info_largeur_vignette',
+								array(
+									'largeur_vignette' => $max_width,
+									'hauteur_vignette' => $max_height
+								)
+							),
+						'actuel' =>
+							_T(
+								'info_largeur_vignette',
+								array(
+									'largeur_vignette' => $infos['largeur'],
+									'hauteur_vignette' => $infos['hauteur']
+								)
+							)
+					)
+				);
+			}
+		}
+
+		if (defined('_IMG_MAX_SIZE') and _IMG_MAX_SIZE > 0 and $infos['taille'] > _IMG_MAX_SIZE * 1024) {
+			return _T(
+				'medias:info_image_max_poids',
+				array(
+					'maxi' => taille_en_octets(_IMG_MAX_SIZE * 1024),
+					'actuel' => taille_en_octets($infos['taille'])
+				)
+			);
+		}
+	}
+
+	return true;
+}
-- 
GitLab