From fee92083b95eb7755354637c4c7371fe71b0c52a Mon Sep 17 00:00:00 2001
From: Eric Lupinacci <eric@smellup.net>
Date: Sun, 20 Nov 2022 12:45:31 +0100
Subject: [PATCH] =?UTF-8?q?change:=20Le=20filtre=20`taille=5Fen=5Foctets()?=
 =?UTF-8?q?`=20retourne=20des=20unit=C3=A9s=20coh=C3=A9rentes?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Par défaut en système binaire (Kio, Mio, Gio…)
- On peut demander en système décimal (Ko, Mo, Go…)

```php
taille_en_octets(1024 * 10); // 10 kio (BI par défaut)
taille_en_octets(1024 * 10, 'BI'); // 10 kio
taille_en_octets(1024 * 10, 'SI'); // 10.2 ko
```

Refs: #5046
---
 ecrire/inc/filtres.php  | 71 ++++++++++++++++++++++++-----------------
 ecrire/lang/spip_fr.php |  4 +++
 2 files changed, 46 insertions(+), 29 deletions(-)

diff --git a/ecrire/inc/filtres.php b/ecrire/inc/filtres.php
index 39f36412f3..c30e83ac8e 100644
--- a/ecrire/inc/filtres.php
+++ b/ecrire/inc/filtres.php
@@ -1290,43 +1290,56 @@ function majuscules($texte) {
 }
 
 /**
- * Retourne une taille en octets humainement lisible
+ * Renvoie une taille de dossier ou de fichier humainement lisible en ajustant le format et l'unité.
  *
- * Tel que "127.4 ko" ou "3.1 Mo"
+ * La fonction renvoie la valeur et l'unité en fonction du système utilisé (binaire ou décimal).
  *
  * @example
- *     - `[(#TAILLE|taille_en_octets)]`
- *     - `[(#VAL{123456789}|taille_en_octets)]` affiche `117.7 Mo`
+ *     - `[(#TAILLE|taille_en_octets)]` affiche xxx.x Mio
+ *     - `[(#VAL{123456789}|taille_en_octets{BI})]` affiche `117.7 Mio`
+ *     - `[(#VAL{123456789}|taille_en_octets{SI})]` affiche `123.5 Mo`
  *
  * @filtre
- * @link https://www.spip.net/4316
- * @param int $taille
- * @return string
- **/
-function taille_en_octets($taille) {
-	if (!defined('_KILOBYTE')) {
-		/**
-		 * Définit le nombre d'octets dans un Kilobyte
-		 *
-		 * @var int
-		 **/
-		define('_KILOBYTE', 1024);
-	}
+ *
+ * @param int    $octets  Taille d'un dossier ou fichier en octets
+ * @param string $systeme Système d'unité dans lequel calculer et afficher la taille lisble. Vaut `BI` (défaut) ou `SI`.
+ *
+ * @return string Taille affichée de manière humainement lisible
+ */
+function taille_en_octets($octets, $systeme = 'BI') {
+
+	// Texte à afficher pour la taille
+	$affichage = '';
+
+	static $unites = ['octets', 'ko', 'mo', 'go'];
+	static $precisions = [0, 1, 1, 2];
+
+	if ($octets >= 1) {
+		// Déterminer le nombre d'octets représentant le kilo en fonction du système choisi
+		$systeme = strtolower($systeme);
+		if ($systeme === 'bi') {
+			$kilo = 1024;
+			$suffixe_item = "_$systeme";
+		} elseif ($systeme === 'si') {
+			$kilo = 1000;
+			$suffixe_item = '';
+		} else {
+			return $affichage;
+		}
 
-	if ($taille < 1) {
-		return '';
-	}
-	if ($taille < _KILOBYTE) {
-		$taille = _T('taille_octets', ['taille' => $taille]);
-	} elseif ($taille < _KILOBYTE * _KILOBYTE) {
-		$taille = _T('taille_ko', ['taille' => round($taille / _KILOBYTE, 1)]);
-	} elseif ($taille < _KILOBYTE * _KILOBYTE * _KILOBYTE) {
-		$taille = _T('taille_mo', ['taille' => round($taille / _KILOBYTE / _KILOBYTE, 1)]);
-	} else {
-		$taille = _T('taille_go', ['taille' => round($taille / _KILOBYTE / _KILOBYTE / _KILOBYTE, 2)]);
+		// Identification de la puissance en "kilo" correspondant à l'unité la plus appropriée
+		$puissance = floor(log($octets, $kilo));
+
+		// Calcul de la taille et choix de l'unité
+		$affichage = _T(
+			'spip:taille_' . $unites[$puissance] . $suffixe_item,
+			[
+				'taille' => round($octets / pow($kilo, $puissance), $precisions[$puissance])
+			]
+		);
 	}
 
-	return $taille;
+	return $affichage;
 }
 
 
diff --git a/ecrire/lang/spip_fr.php b/ecrire/lang/spip_fr.php
index 869c19d1d5..aa963e03ed 100644
--- a/ecrire/lang/spip_fr.php
+++ b/ecrire/lang/spip_fr.php
@@ -635,6 +635,10 @@ et vous reconnecter au site.
 	'taille_ko' => '@taille@ ko',
 	'taille_mo' => '@taille@ Mo',
 	'taille_octets' => '@taille@ octets',
+	'taille_go_bi' => '@taille@ Gio',
+	'taille_ko_bi' => '@taille@ kio',
+	'taille_mo_bi' => '@taille@ Mio',
+	'taille_octets_bi' => '@taille@ octets',
 	'texte_actualite_site_1' => 'Quand vous serez familiarisé(e) avec l’interface, vous pourrez cliquer sur « ',
 	'texte_actualite_site_2' => 'interface complète',
 	'texte_actualite_site_3' => ' » pour ouvrir plus de possibilités.',
-- 
GitLab