From 01d6cd4dce5daf9086663f32c68828a102882822 Mon Sep 17 00:00:00 2001
From: Matthieu Marcillaud <marcimat@rezo.net>
Date: Wed, 15 Dec 2021 01:04:12 +0100
Subject: [PATCH] PHP 8.1 #4968 : deprecated-- sur des fonctions de textes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- texte_script et texte_backend qui pouvaient obtenir null en entrée.
- Typages sur inserer_attribut, vider_attribut, attribut_html, texte_backend, texte_script.
---
 ecrire/inc/filtres.php                 | 15 +++++++++------
 ecrire/inc/filtres_images_lib_mini.php |  2 +-
 ecrire/inc/utils.php                   |  2 +-
 ecrire/public/composer.php             |  2 +-
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/ecrire/inc/filtres.php b/ecrire/inc/filtres.php
index c070d3d1fb..57d3b6e4c9 100644
--- a/ecrire/inc/filtres.php
+++ b/ecrire/inc/filtres.php
@@ -909,7 +909,10 @@ function corriger_caracteres($texte) {
  * @return string
  *     Texte encodé pour XML
  */
-function texte_backend($texte) {
+function texte_backend(string $texte) : string {
+	if ($texte === '') {
+		return '';
+	}
 
 	static $apostrophe = ['&#8217;', "'"]; # n'allouer qu'une fois
 
@@ -960,7 +963,7 @@ function texte_backend($texte) {
  * @return string
  *     Texte encodé et quote pour XML
  */
-function texte_backendq($texte) {
+function texte_backendq(string $texte) : string {
 	return addslashes(texte_backend($texte));
 }
 
@@ -1327,7 +1330,7 @@ function taille_en_octets($taille) {
  * @return string
  *     Texte prêt pour être utilisé en attribut HTML
  **/
-function attribut_html($texte, $textebrut = true) {
+function attribut_html(string $texte, $textebrut = true) : string {
 	$u = $GLOBALS['meta']['pcre_u'];
 	if ($textebrut) {
 		$texte = preg_replace([",\n,", ',\s(?=\s),msS' . $u], [' ', ''], textebrut($texte));
@@ -2040,7 +2043,7 @@ function extraire_attribut($balise, $attribut, $complet = false) {
  * @return string
  *     Code html modifié
  **/
-function inserer_attribut($balise, $attribut, $val, $proteger = true, $vider = false) {
+function inserer_attribut(string $balise, string $attribut, string $val, bool $proteger = true, bool $vider = false) : string {
 	// preparer l'attribut
 	// supprimer les &nbsp; etc mais pas les balises html
 	// qui ont un sens dans un attribut value d'un input
@@ -2050,7 +2053,7 @@ function inserer_attribut($balise, $attribut, $val, $proteger = true, $vider = f
 
 	// echapper les ' pour eviter tout bug
 	$val = str_replace("'", '&#039;', $val);
-	if ($vider and strlen($val) == 0) {
+	if ($vider and strlen($val) === 0) {
 		$insert = '';
 	} else {
 		$insert = " $attribut='$val'";
@@ -2088,7 +2091,7 @@ function inserer_attribut($balise, $attribut, $val, $proteger = true, $vider = f
  * @param string $attribut Nom de l'attribut à enlever
  * @return string Code HTML sans l'attribut
  **/
-function vider_attribut($balise, $attribut) {
+function vider_attribut(string $balise, string $attribut) : string {
 	return inserer_attribut($balise, $attribut, '', false, true);
 }
 
diff --git a/ecrire/inc/filtres_images_lib_mini.php b/ecrire/inc/filtres_images_lib_mini.php
index 60d8f78228..a92560c4a0 100644
--- a/ecrire/inc/filtres_images_lib_mini.php
+++ b/ecrire/inc/filtres_images_lib_mini.php
@@ -1264,7 +1264,7 @@ function _image_tag_changer_taille($tag, $width, $height, $style = false) {
 		$tag = inserer_attribut($tag, 'hspace', '');
 	}
 
-	$tag = inserer_attribut($tag, 'style', $style, true, $style ? false : true);
+	$tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true);
 
 	return $tag;
 }
diff --git a/ecrire/inc/utils.php b/ecrire/inc/utils.php
index 76e124c313..77b1298765 100644
--- a/ecrire/inc/utils.php
+++ b/ecrire/inc/utils.php
@@ -1305,7 +1305,7 @@ function http_script($script, $src = '', $noscript = '') {
  * @return string
  *     Texte échappé
  **/
-function texte_script($texte) {
+function texte_script(string $texte) : string {
 	return str_replace('\'', '\\\'', str_replace('\\', '\\\\', $texte));
 }
 
diff --git a/ecrire/public/composer.php b/ecrire/public/composer.php
index 7801cd1f19..2330f6cb1f 100644
--- a/ecrire/public/composer.php
+++ b/ecrire/public/composer.php
@@ -352,7 +352,7 @@ function argumenter_squelette($v) {
 	if (is_object($v)) {
 		return var_export($v, true);
 	} elseif (!is_array($v)) {
-		return "'" . texte_script($v) . "'";
+		return "'" . texte_script((string) $v) . "'";
 	} else {
 		$out = [];
 		foreach ($v as $k => $val) {
-- 
GitLab