From ae7eb2e886c4ecbab1cf8fc75c0b4d4a27bb629d Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud <marcimat@rezo.net> Date: Tue, 14 Mar 2023 20:04:08 +0100 Subject: [PATCH] chore: Rector up to PHP 8.0 sur ecrire/inc (partiel) --- ecrire/inc/filtres_ecrire.php | 44 ++- ecrire/inc/filtres_images_lib_mini.php | 298 +++++++++------------ ecrire/inc/filtres_images_mini.php | 34 +-- ecrire/inc/filtres_mime.php | 12 +- ecrire/inc/filtres_selecteur_generique.php | 38 ++- ecrire/inc/genie.php | 7 +- ecrire/inc/icone_renommer.php | 12 +- ecrire/inc/importer_csv.php | 33 +-- ecrire/inc/livrer_fichier.php | 10 +- ecrire/inc/log.php | 26 +- 10 files changed, 213 insertions(+), 301 deletions(-) diff --git a/ecrire/inc/filtres_ecrire.php b/ecrire/inc/filtres_ecrire.php index 5e111aba53..d69a2cf58f 100644 --- a/ecrire/inc/filtres_ecrire.php +++ b/ecrire/inc/filtres_ecrire.php @@ -54,7 +54,7 @@ function parametres_css_prive() { parse_str($couleurs($c), $c); $args = array_merge($args, $c); - if (_request('var_mode') == 'recalcul' or (defined('_VAR_MODE') and _VAR_MODE == 'recalcul')) { + if (_request('var_mode') == 'recalcul' || defined('_VAR_MODE') && _VAR_MODE == 'recalcul') { $args['var_mode'] = 'recalcul'; } @@ -92,7 +92,7 @@ function chercher_rubrique( ) { include_spip('inc/autoriser'); - if (intval($id_objet) && !autoriser('modifier', $objet, $id_objet)) { + if ((int) $id_objet && !autoriser('modifier', $objet, $id_objet)) { return ''; } if (!sql_countsel('spip_rubriques')) { @@ -116,8 +116,8 @@ function chercher_rubrique( // confirmation du deplacement if ( sql_table_exists('spip_breves') - and ($contient_breves = sql_countsel('spip_breves', 'id_rubrique=' . intval($id_objet))) - and ($contient_breves > 0) + && ($contient_breves = sql_countsel('spip_breves', 'id_rubrique=' . (int) $id_objet)) + && $contient_breves > 0 ) { // FIXME: utiliser singulier_ou_pluriel, migrer dans plugin Brèves $scb = ($contient_breves > 1 ? 's' : ''); @@ -138,7 +138,7 @@ function chercher_rubrique( } $form .= $confirm; if ($actionable) { - if (strpos($form, '<select') !== false) { + if (str_contains($form, '<select')) { $form .= "<div style='text-align: " . $GLOBALS['spip_lang_right'] . ";'>" . '<input class="fondo submit btn" type="submit" value="' . _T('bouton_choisir') . '"/>' . '</div>'; @@ -186,7 +186,7 @@ function avoir_visiteurs($past = false, $accepter = true) { if ($GLOBALS['meta']['forums_publics'] == 'abo') { return true; } - if ($accepter and $GLOBALS['meta']['accepter_visiteurs'] <> 'non') { + if ($accepter && $GLOBALS['meta']['accepter_visiteurs'] != 'non') { return true; } if (sql_countsel('spip_articles', "accepter_forum='abo'")) { @@ -352,7 +352,7 @@ function auteurs_lister_statuts($quoi = 'tous', $en_base = true): array { 'statut' ); - return array_unique(array_merge($statut, $s_complement)); + return array_unique([...$statut, ...$s_complement]); default: case 'tous': @@ -361,7 +361,7 @@ function auteurs_lister_statuts($quoi = 'tous', $en_base = true): array { sql_allfetsel('DISTINCT statut', 'spip_auteurs', sql_in('statut', $statut, 'NOT')), 'statut' ); - $statut = array_merge($statut, $s_complement); + $statut = [...$statut, ...$s_complement]; if ($en_base) { $check = array_column(sql_allfetsel('DISTINCT statut', 'spip_auteurs', sql_in('statut', $statut)), 'statut'); $retire = array_diff($statut, $check); @@ -385,10 +385,10 @@ function auteurs_lister_statuts($quoi = 'tous', $en_base = true): array { */ function trouver_rubrique_creer_objet($id_rubrique, $objet) { - if (!$id_rubrique and defined('_CHOIX_RUBRIQUE_PAR_DEFAUT') and _CHOIX_RUBRIQUE_PAR_DEFAUT) { - $in = !(is_countable($GLOBALS['connect_id_rubrique']) ? count($GLOBALS['connect_id_rubrique']) : 0) - ? '' - : (' AND ' . sql_in('id_rubrique', $GLOBALS['connect_id_rubrique'])); + if (!$id_rubrique && defined('_CHOIX_RUBRIQUE_PAR_DEFAUT') && _CHOIX_RUBRIQUE_PAR_DEFAUT) { + $in = (is_countable($GLOBALS['connect_id_rubrique']) ? count($GLOBALS['connect_id_rubrique']) : 0) + ? ' AND ' . sql_in('id_rubrique', $GLOBALS['connect_id_rubrique']) + : ''; // on tente d'abord l'ecriture a la racine dans le cas des rubriques uniquement if ($objet == 'rubrique') { @@ -465,7 +465,7 @@ function alertes_auteur($id_auteur): string { if ( isset($GLOBALS['meta']['message_crash_tables']) - and autoriser('detruire', null, null, $id_auteur) + && autoriser('detruire', null, null, $id_auteur) ) { include_spip('genie/maintenance'); if ($msg = message_crash_tables()) { @@ -475,20 +475,16 @@ function alertes_auteur($id_auteur): string { if ( isset($GLOBALS['meta']['message_crash_plugins']) - and $GLOBALS['meta']['message_crash_plugins'] - and autoriser('configurer', '_plugins', null, $id_auteur) - and is_array($msg = unserialize($GLOBALS['meta']['message_crash_plugins'])) + && $GLOBALS['meta']['message_crash_plugins'] + && autoriser('configurer', '_plugins', null, $id_auteur) + && is_array($msg = unserialize($GLOBALS['meta']['message_crash_plugins'])) ) { $msg = implode(', ', array_map('joli_repertoire', array_keys($msg))); $alertes[] = _T('plugins_erreur', ['plugins' => $msg]); } $a = $GLOBALS['meta']['message_alertes_auteurs'] ?? ''; - if ( - $a - and is_array($a = unserialize($a)) - and count($a) - ) { + if ($a && is_array($a = unserialize($a)) && count($a)) { $update = false; if (isset($a[$GLOBALS['visiteur_session']['statut']])) { $alertes = array_merge($alertes, $a[$GLOBALS['visiteur_session']['statut']]); @@ -507,7 +503,7 @@ function alertes_auteur($id_auteur): string { if ( isset($GLOBALS['meta']['plugin_erreur_activation']) - and autoriser('configurer', '_plugins', null, $id_auteur) + && autoriser('configurer', '_plugins', null, $id_auteur) ) { include_spip('inc/plugin'); $alertes[] = plugin_donne_erreurs(); @@ -526,7 +522,7 @@ function alertes_auteur($id_auteur): string { if ($alertes = array_filter($alertes)) { return "<div class='wrap-messages-alertes'><div class='messages-alertes'>" . - join(' | ', $alertes) + implode(' | ', $alertes) . '</div></div>'; } @@ -542,7 +538,7 @@ function alertes_auteur($id_auteur): string { function filtre_afficher_enfant_rub_dist($id_rubrique) { include_spip('inc/presenter_enfants'); - return afficher_enfant_rub(intval($id_rubrique)); + return afficher_enfant_rub((int) $id_rubrique); } /** diff --git a/ecrire/inc/filtres_images_lib_mini.php b/ecrire/inc/filtres_images_lib_mini.php index dc21e1aa93..4c48898628 100644 --- a/ecrire/inc/filtres_images_lib_mini.php +++ b/ecrire/inc/filtres_images_lib_mini.php @@ -137,23 +137,19 @@ function _couleur_rgb_to_hsl($R, $G, $B) { $S = 0; } else { // Chromatic data... - if ($L < 0.5) { - $S = $del_Max / ($var_Max + $var_Min); - } else { - $S = $del_Max / (2 - $var_Max - $var_Min); - } + $S = $L < 0.5 ? $del_Max / ($var_Max + $var_Min) : $del_Max / (2 - $var_Max - $var_Min); $del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max; $del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max; $del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max; - if ($var_R == $var_Max) { + if ($var_R === $var_Max) { $H = $del_B - $del_G; } else { - if ($var_G == $var_Max) { + if ($var_G === $var_Max) { $H = (1 / 3) + $del_R - $del_B; } else { - if ($var_B == $var_Max) { + if ($var_B === $var_Max) { $H = (2 / 3) + $del_G - $del_R; } } @@ -215,11 +211,7 @@ function _couleur_hsl_to_rgb($H, $S, $L) { $G = $L * 255; $B = $L * 255; } else { - if ($L < 0.5) { - $var_2 = $L * (1 + $S); - } else { - $var_2 = ($L + $S) - ($S * $L); - } + $var_2 = $L < 0.5 ? $L * (1 + $S) : ($L + $S) - ($S * $L); $var_1 = 2 * $L - $var_2; @@ -255,7 +247,7 @@ function statut_effacer_images_temporaires($stat) { if ($stat === 'get') { return $statut; } - $statut = $stat ? true : false; + $statut = (bool) $stat; } @@ -308,7 +300,8 @@ function statut_effacer_images_temporaires($stat) { * - array : tableau décrivant de l'image */ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_creation = null, $find_in_path = false, $support_svg = false) { - $ret = []; + $valeurs = []; + $ret = []; $f = null; static $images_recalcul = []; if (strlen($img) == 0) { @@ -317,18 +310,17 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr $source = trim(extraire_attribut($img, 'src') ?? ''); if (strlen($source) < 1) { - if (strpos($img, '<img ') !== 0) { + if (!str_starts_with($img, '<img ')) { $source = $img; $img = "<img src='$source' />"; - } - else { + } else { // pas d'attribut src sur cette balise <img../> return false; } } elseif ( preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs) - and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1]) - and in_array($extension, _image_extensions_acceptees_en_entree()) + && ($extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])) + && in_array($extension, _image_extensions_acceptees_en_entree()) ) { # gerer img src="data:....base64" $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension); @@ -353,20 +345,20 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr return ''; } if ( - $extension = _image_trouver_extension($fichier) - and $sanitizer = charger_fonction($extension, 'sanitizer', true) + ($extension = _image_trouver_extension($fichier)) + && ($sanitizer = charger_fonction($extension, 'sanitizer', true)) ) { $sanitizer($fichier); } } else { // enlever le timestamp eventuel - if (strpos($source, '?') !== false) { - $source = preg_replace(',[?][0-9]+$,', '', $source); + if (str_contains($source, '?')) { + $source = preg_replace(',[?]\d+$,', '', $source); } if ( - strpos($source, '?') !== false - and strncmp($source, _DIR_IMG, strlen(_DIR_IMG)) == 0 - and file_exists($f = preg_replace(',[?].*$,', '', $source)) + str_contains($source, '?') + && str_starts_with($source, _DIR_IMG) + && file_exists($f = preg_replace(',[?].*$,', '', $source)) ) { $source = $f; } @@ -380,8 +372,7 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr if ( $forcer_format !== false - // ignorer forcer_format si on a une image svg, que le filtre appelant ne supporte pas SVG, et que le forcage est un autre format image - and ($terminaison_dest !== 'svg' or $support_svg or !in_array($forcer_format, _image_extensions_acceptees_en_sortie())) + && ($terminaison_dest !== 'svg' || $support_svg || !in_array($forcer_format, _image_extensions_acceptees_en_sortie())) ) { $terminaison_dest = $forcer_format; } @@ -393,8 +384,8 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr $nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1)); $fichier_dest = $nom_fichier; if ( - ($find_in_path and $f = find_in_path($fichier) and $fichier = $f) - or @file_exists($f = $fichier) + $find_in_path && ($f = find_in_path($fichier)) && ($fichier = $f) + || @file_exists($f = $fichier) ) { // on ne passe jamais la balise img qui est peut-être en x2 et à laquelle on ne peut pas faire confiance // on lit directement les infos du fichier @@ -402,10 +393,10 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr $date_src = @filemtime($f); } elseif ( @file_exists($f = "$fichier.src") - and lire_fichier($f, $valeurs) - and $valeurs = unserialize($valeurs) - and isset($valeurs['hauteur_dest']) - and isset($valeurs['largeur_dest']) + && lire_fichier($f, $valeurs) + && ($valeurs = unserialize($valeurs)) + && isset($valeurs['hauteur_dest']) + && isset($valeurs['largeur_dest']) ) { $ret['hauteur'] = $valeurs['hauteur_dest']; $ret['largeur'] = $valeurs['largeur_dest']; @@ -416,10 +407,11 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr } // pas de taille mesurable ? - if (!$ret['hauteur'] - or !($ret['hauteur'] = intval(round($ret['hauteur']))) - or !$ret['largeur'] - or !($ret['largeur'] = intval(round($ret['largeur']))) + if ( + !$ret['hauteur'] + || !($ret['hauteur'] = (int) round($ret['hauteur'])) + || !$ret['largeur'] + || !($ret['largeur'] = (int) round($ret['largeur'])) ) { return false; } @@ -438,7 +430,7 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr // cas particulier de reduire : // un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi $cache = 'cache-gd2'; - if (substr($effet, 0, 7) == 'reduire') { + if (str_starts_with($effet, 'reduire')) { [, $maxWidth, $maxHeight] = explode('-', $effet); [$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight); $ret['largeur_dest'] = $destWidth; @@ -469,7 +461,7 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr $creer = true; // si recalcul des images demande, recalculer chaque image une fois - if (defined('_VAR_IMAGES') and _VAR_IMAGES and !isset($images_recalcul[$fichier_dest])) { + if (defined('_VAR_IMAGES') && _VAR_IMAGES && !isset($images_recalcul[$fichier_dest])) { $images_recalcul[$fichier_dest] = true; } else { if (@file_exists($f = $fichier_dest)) { @@ -479,25 +471,23 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr } else { if ( @file_exists($f = "$fichier_dest.src") - and lire_fichier($f, $valeurs) - and $valeurs = unserialize($valeurs) - and $valeurs['date'] >= $date_src + && lire_fichier($f, $valeurs) + && ($valeurs = unserialize($valeurs)) + && $valeurs['date'] >= $date_src ) { $creer = false; } } } - if ($creer) { - if (!@file_exists($fichier)) { - if (!@file_exists("$fichier.src")) { - spip_log("Image absente : $fichier", 'images' . _LOG_ERREUR); + if ($creer && !@file_exists($fichier)) { + if (!@file_exists("$fichier.src")) { + spip_log("Image absente : $fichier", 'images' . _LOG_ERREUR); - return false; - } - # on reconstruit l'image source absente a partir de la chaine des .src - reconstruire_image_intermediaire($fichier); - } - } + return false; + } + # on reconstruit l'image source absente a partir de la chaine des .src + reconstruire_image_intermediaire($fichier); + } if ($creer) { spip_log( @@ -543,7 +533,7 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr // traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue if ($term_fonction === 'svg') { - if ($creer and !$support_svg) { + if ($creer && !$support_svg) { process_image_svg_identite($ret); $ret['creer'] = false; } @@ -590,10 +580,10 @@ function _image_extensions_acceptees_en_sortie() { if (empty($extensions)) { $extensions = _image_extensions_acceptees_en_entree(); $extensions = array_diff($extensions, ['jpeg']); - if (in_array('gif', $extensions) and !function_exists('imagegif')) { + if (in_array('gif', $extensions) && !function_exists('imagegif')) { $extensions = array_diff($extensions, ['gif']); } - if (in_array('webp', $extensions) and !function_exists('imagewebp')) { + if (in_array('webp', $extensions) && !function_exists('imagewebp')) { $extensions = array_diff($extensions, ['webp']); } } @@ -622,8 +612,7 @@ function _image_extensions_conservent_transparence() { function _image_trouver_extension($path) { $preg_extensions = implode('|', _image_extensions_acceptees_en_entree()); if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) { - $terminaison = strtolower($regs[1]); - return $terminaison; + return strtolower($regs[1]); } return ''; } @@ -650,15 +639,10 @@ function _image_trouver_extension_pertinente($path) { return $terminaison; } - if (isset($info['mime'])) { - $mime = $info['mime']; - } - else { - $mime = image_type_to_mime_type($info[2]); - } + $mime = $info['mime'] ?? image_type_to_mime_type($info[2]); $_terminaison = _image_trouver_extension_depuis_mime($mime); - if ($_terminaison and $_terminaison !== $terminaison) { + if ($_terminaison && $_terminaison !== $terminaison) { spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE); $terminaison = $_terminaison; } @@ -670,36 +654,14 @@ function _image_trouver_extension_pertinente($path) { * @return string */ function _image_trouver_extension_depuis_mime($mime) { - switch (strtolower($mime)) { - case 'image/png': - case 'image/x-png': - $terminaison = 'png'; - break; - - case 'image/jpg': - case 'image/jpeg': - case 'image/pjpeg': - $terminaison = 'jpeg'; - break; - - case 'image/gif': - $terminaison = 'gif'; - break; - - case 'image/webp': - case 'image/x-webp': - $terminaison = 'webp'; - break; - - case 'image/svg+xml': - $terminaison = 'svg'; - break; - - default: - $terminaison = ''; - } - - return $terminaison; + return match (strtolower($mime)) { + 'image/png', 'image/x-png' => 'png', + 'image/jpg', 'image/jpeg', 'image/pjpeg' => 'jpeg', + 'image/gif' => 'gif', + 'image/webp', 'image/x-webp' => 'webp', + 'image/svg+xml' => 'svg', + default => '', + }; } @@ -987,13 +949,13 @@ function _image_imagewebp($img, $fichier, $qualite = _IMG_GD_QUALITE) { function _image_imagesvg($img, $fichier) { $tmp = $fichier . '.tmp'; - if (strpos($img, '<') === false) { + if (!str_contains($img, '<')) { $img = supprimer_timestamp($img); if (!file_exists($img)) { return false; } @copy($img, $tmp); - if (filesize($tmp) == filesize($img)) { + if (filesize($tmp) === filesize($img)) { spip_unlink($fichier); // le fichier peut deja exister @rename($tmp, $fichier); return true; @@ -1048,22 +1010,20 @@ function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction $ret = false; #un flag pour reperer les images gravees $lock = ( - !statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout - or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src')) + !statut_effacer_images_temporaires('get') + || @file_exists($valeurs['fichier_dest']) + && !@file_exists($valeurs['fichier_dest'] . '.src') ); - if ( - function_exists($fonction) + if (function_exists($fonction) && ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image && isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant - && !$lock - ) { - if (@file_exists($valeurs['fichier_dest'])) { - // dans tous les cas mettre a jour la taille de l'image finale - [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']); - $valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition - ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true); - } - } + && !$lock && @file_exists($valeurs['fichier_dest'])) { + // dans tous les cas mettre a jour la taille de l'image finale + [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']); + $valeurs['date'] = @filemtime($valeurs['fichier_dest']); + // pour la retrouver apres disparition + ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true); + } return $ret; } @@ -1078,14 +1038,15 @@ function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction * Chemin vers le fichier manquant **/ function reconstruire_image_intermediaire($fichier_manquant) { - $reconstruire = []; + $source = null; + $reconstruire = []; $fichier = $fichier_manquant; while ( - strpos($fichier, '://') === false - and !@file_exists($fichier) - and lire_fichier($src = "$fichier.src", $source) - and $valeurs = unserialize($source) - and ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...) + !str_contains($fichier, '://') + && !@file_exists($fichier) + && lire_fichier($src = "$fichier.src", $source) + && ($valeurs = unserialize($source)) + && ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...) ) { spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant $reconstruire[] = $valeurs['reconstruction']; @@ -1118,22 +1079,23 @@ function reconstruire_image_intermediaire($fichier_manquant) { * Chemin du fichier d'image calculé **/ function ramasse_miettes($fichier) { - if ( - strpos($fichier, '://') !== false - or !lire_fichier($src = "$fichier.src", $source) - or !$valeurs = unserialize($source) + $source = null; + if ( + str_contains($fichier, '://') + || !lire_fichier($src = "$fichier.src", $source) + || !$valeurs = unserialize($source) ) { return; } spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire while ( - ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...) - and (substr($fichier, 0, strlen(_DIR_VAR)) == _DIR_VAR) # et est dans local - and (lire_fichier( + ($fichier = $valeurs['fichier']) + && str_starts_with($fichier, _DIR_VAR) + && lire_fichier( $src = "$fichier.src", $source - )) # le fichier a une source connue (c'est donc une image calculee intermediaire) - and ($valeurs = unserialize($source)) # et valide + ) + && ($valeurs = unserialize($source)) # et valide ) { # on efface le fichier spip_unlink($fichier); @@ -1172,21 +1134,19 @@ function image_graver($img) { if (($p = strpos($fichier, '?')) !== false) { $fichier = substr($fichier, 0, $p); } - if (strlen($fichier) < 1) { - if (strpos($img, '<img ') !== 0) { - $fichier = $img; - } + if (strlen($fichier) < 1 && !str_starts_with($img, '<img ')) { + $fichier = $img; } if (strlen($fichier)) { # si jamais le fichier final n'a pas ete calcule car suppose temporaire # et qu'il ne s'agit pas d'une URL - if (strpos($fichier, '://') === false and !@file_exists($fichier)) { + if (!str_contains($fichier, '://') && !@file_exists($fichier)) { reconstruire_image_intermediaire($fichier); } ramasse_miettes($fichier); // ajouter le timestamp si besoin - if (strpos($fichier_ori, '?') === false) { + if (!str_contains($fichier_ori, '?')) { // on utilise str_replace pour attraper le onmouseover des logo si besoin $img = str_replace($fichier_ori, timestamp($fichier_ori), $img); } @@ -1227,7 +1187,7 @@ function _image_tag_changer_taille($tag, $width, $height, $style = false) { if ($style) { $style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style); } - if ($style and $style[0] === ';') { + if ($style && $style[0] === ';') { $style = substr($style, 1); } @@ -1244,9 +1204,7 @@ function _image_tag_changer_taille($tag, $width, $height, $style = false) { $tag = inserer_attribut($tag, 'hspace', ''); } - $tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true); - - return $tag; + return inserer_attribut($tag, 'style', (string) $style, true, !(bool) $style); } @@ -1304,7 +1262,7 @@ function _image_ecrire_tag($valeurs, $surcharge = []) { $tag = str_replace($src, $surcharge['src'], $tag); // si il y a des & dans src, alors ils peuvent provenir d'un & // pas garanti comme methode, mais mieux que rien - if (strpos($src, '&') !== false) { + if (str_contains($src, '&')) { $tag = str_replace(str_replace('&', '&', $src), $surcharge['src'], $tag); } $src = $surcharge['src']; @@ -1320,13 +1278,13 @@ function _image_ecrire_tag($valeurs, $surcharge = []) { $tag = inserer_attribut($tag, 'class', $class); } - if (count($surcharge)) { + if ($surcharge !== []) { foreach ($surcharge as $attribut => $valeur) { $tag = inserer_attribut($tag, $attribut, $valeur); } } - $tag = pipeline( + return pipeline( 'image_ecrire_tag_finir', [ 'args' => [ @@ -1336,8 +1294,6 @@ function _image_ecrire_tag($valeurs, $surcharge = []) { 'data' => $tag ] ); - - return $tag; } /** @@ -1371,12 +1327,12 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO $format_sortie = $valeurs['format_dest']; - if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) { + if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { $process = $GLOBALS['meta']['image_process']; } // si le doc n'est pas une image dans un format accetpable, refuser - if (!$force and !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) { + if (!$force && !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) { return; } $destination = "$destdir/$destfile"; @@ -1386,7 +1342,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) { [$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight); } - } elseif ($process == 'convert' or $process == 'imagick') { + } elseif ($process == 'convert' || $process == 'imagick') { $destWidth = $maxWidth; $destHeight = $maxHeight; } else { @@ -1398,7 +1354,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO $vignette = ''; // Si l'image est de la taille demandee (ou plus petite), simplement la retourner - if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) { + if ($srcWidth && $srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { $vignette = $destination . '.' . $format; @copy($image, $vignette); } @@ -1526,7 +1482,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO } // gd ou gd2 - elseif ($process == 'gd1' or $process == 'gd2') { + elseif ($process == 'gd1' || $process == 'gd2') { if (!function_exists('gd_info')) { spip_log('Librairie GD absente !', _LOG_ERREUR); @@ -1557,7 +1513,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO // Initialisation de l'image destination $destImage = null; - if ($process == 'gd2' and $destFormat != 'gif') { + if ($process == 'gd2' && $destFormat != 'gif') { $destImage = ImageCreateTrueColor($destWidth, $destHeight); } if (!$destImage) { @@ -1566,7 +1522,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO // Recopie de l'image d'origine avec adaptation de la taille $ok = false; - if (($process == 'gd2') and function_exists('ImageCopyResampled')) { + if ($process == 'gd2' && function_exists('ImageCopyResampled')) { if ($format == 'gif') { // Si un GIF est transparent, // fabriquer un PNG transparent @@ -1600,7 +1556,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO ImageDestroy($destImage); } - if (!$vignette or !$size = @spip_getimagesize($vignette)) { + if (!$vignette || !$size = @spip_getimagesize($vignette)) { $size = [$destWidth, $destHeight]; } @@ -1644,7 +1600,7 @@ function _image_ratio(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeig $ratioWidth = $srcWidth / $maxWidth; $ratioHeight = $srcHeight / $maxHeight; - if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) { + if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { $destWidth = $srcWidth; $destHeight = $srcHeight; } elseif ($ratioWidth < $ratioHeight) { @@ -1656,8 +1612,8 @@ function _image_ratio(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeig } return [ - intval(round($destWidth)), - intval(round($destHeight)), + (int) round($destWidth), + (int) round($destHeight), max($ratioWidth, $ratioHeight) ]; } @@ -1682,7 +1638,7 @@ function ratio_passe_partout(int $srcWidth, int $srcHeight, int $maxWidth, int $ $ratioWidth = $srcWidth / $maxWidth; $ratioHeight = $srcHeight / $maxHeight; - if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) { + if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { $destWidth = $srcWidth; $destHeight = $srcHeight; } elseif ($ratioWidth > $ratioHeight) { @@ -1694,8 +1650,8 @@ function ratio_passe_partout(int $srcWidth, int $srcHeight, int $maxWidth, int $ } return [ - intval(round($destWidth)), - intval(round($destHeight)), + (int) round($destWidth), + (int) round($destHeight), min($ratioWidth, $ratioHeight) ]; } @@ -1749,29 +1705,21 @@ function process_image_svg_identite($image) { **/ function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $process = 'AUTO') { $image = false; - if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) { + if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { $process = $GLOBALS['meta']['image_process']; } # determiner le format de sortie $format_sortie = false; // le choix par defaut sera bon if ($process == 'netpbm') { $format_sortie = 'jpg'; - } elseif ($process == 'gd1' or $process == 'gd2') { + } elseif ($process == 'gd1' || $process == 'gd2') { $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); // on verifie que l'extension choisie est bonne (en principe oui) $gd_formats = formats_image_acceptables(true); if ( - is_array($image) - and (!in_array($image['format_dest'], $gd_formats) - or (!in_array($image['format_dest'], _image_extensions_acceptees_en_sortie())) - ) + is_array($image) && (!in_array($image['format_dest'], $gd_formats) || !in_array($image['format_dest'], _image_extensions_acceptees_en_sortie())) ) { - if ($image['format_source'] == 'jpg') { - $formats_sortie = ['jpg', 'png', 'gif']; - } else // les gif sont passes en png preferentiellement pour etre homogene aux autres filtres images - { - $formats_sortie = ['png', 'jpg', 'gif']; - } + $formats_sortie = $image['format_source'] == 'jpg' ? ['jpg', 'png', 'gif'] : ['png', 'jpg', 'gif']; // Choisir le format destination // - on sauve de preference en JPEG (meilleure compression) // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire @@ -1779,7 +1727,7 @@ function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $pro # pas *ecrire* $format_sortie = ''; foreach ($formats_sortie as $fmt) { - if (in_array($fmt, $gd_formats) and in_array($fmt, _image_extensions_acceptees_en_sortie())) { + if (in_array($fmt, $gd_formats) && in_array($fmt, _image_extensions_acceptees_en_sortie())) { $format_sortie = $fmt; break; } @@ -1792,11 +1740,11 @@ function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $pro $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); } - if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) { + if (!is_array($image) || !$image['largeur'] || !$image['hauteur']) { spip_log("image_reduire_src:pas de version locale de $img ou extension non prise en charge"); // on peut resizer en mode html si on dispose des elements [$srcw, $srch] = taille_image($img); - if ($srcw and $srch) { + if ($srcw && $srch) { [$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y); return _image_tag_changer_taille($img, $w, $h); @@ -1889,7 +1837,7 @@ class phpthumb_functions { public static function LittleEndian2String($number, $minbytes = 1) { $intstring = ''; while ($number > 0) { - $intstring = $intstring . chr($number & 255); + $intstring .= chr($number & 255); $number >>= 8; } @@ -1922,7 +1870,7 @@ class phpthumb_functions { if ($bpp[$key] == 32) { $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); - } elseif ($bpp[$key] == 24) { + } elseif ($bpp[$key] === 24) { $icXOR[$key] .= chr($b) . chr($g) . chr($r); } @@ -1945,7 +1893,7 @@ class phpthumb_functions { } } - foreach ($gd_image_array as $key => $gd_image) { + foreach (array_keys($gd_image_array) as $key) { $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8); // BITMAPINFOHEADER - 40 bytes @@ -1971,7 +1919,7 @@ class phpthumb_functions { $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? $dwImageOffset = 6 + (count($gd_image_array) * 16); - foreach ($gd_image_array as $key => $gd_image) { + foreach (array_keys($gd_image_array) as $key) { // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image @@ -1997,7 +1945,7 @@ class phpthumb_functions { $dwImageOffset += strlen($icAND[$key]); } - foreach ($gd_image_array as $key => $gd_image) { + foreach (array_keys($gd_image_array) as $key) { $icondata .= $BitmapInfoHeader[$key]; $icondata .= $icXOR[$key]; $icondata .= $icAND[$key]; diff --git a/ecrire/inc/filtres_images_mini.php b/ecrire/inc/filtres_images_mini.php index c0ecf2a305..afef155567 100644 --- a/ecrire/inc/filtres_images_mini.php +++ b/ecrire/inc/filtres_images_mini.php @@ -257,9 +257,7 @@ function couleur_foncer($couleur, $coeff = 0.5) { $green = $couleurs['green'] - round(($couleurs['green']) * $coeff); $blue = $couleurs['blue'] - round(($couleurs['blue']) * $coeff); - $couleur = _couleur_dec_to_hex($red, $green, $blue); - - return $couleur; + return _couleur_dec_to_hex($red, $green, $blue); } /** @@ -281,9 +279,7 @@ function couleur_eclaircir($couleur, $coeff = 0.5) { $green = $couleurs['green'] + round((255 - $couleurs['green']) * $coeff); $blue = $couleurs['blue'] + round((255 - $couleurs['blue']) * $coeff); - $couleur = _couleur_dec_to_hex($red, $green, $blue); - - return $couleur; + return _couleur_dec_to_hex($red, $green, $blue); } /** @@ -311,17 +307,17 @@ function image_select($img, $width_min = 0, $height_min = 0, $width_max = 10000, } [$h, $l] = taille_image($img); $select = true; - if ($l < $width_min or $l > $width_max or $h < $height_min or $h > $height_max) { + if ($l < $width_min || $l > $width_max || $h < $height_min || $h > $height_max) { $select = false; } $class = extraire_attribut($img, 'class'); $p = strpos($class, 'filtre_inactif'); - if (($select == false) and ($p === false)) { + if ($select == false && $p === false) { $class .= ' filtre_inactif'; $img = inserer_attribut($img, 'class', $class); } - if (($select == true) and ($p !== false)) { + if ($select == true && $p !== false) { // no_image_filtrer : historique, a virer $class = preg_replace(',\s*(filtre_inactif|no_image_filtrer),', '', $class); $img = inserer_attribut($img, 'class', $class); @@ -400,13 +396,13 @@ function image_passe_partout( $taille_y = $taille_x; } - if ($taille_x === 0 and $taille_y > 0) { + if ($taille_x === 0 && $taille_y > 0) { $taille_x = 1; } # {0,300} -> c'est 300 qui compte - elseif ($taille_x > 0 and $taille_y === 0) { + elseif ($taille_x > 0 && $taille_y === 0) { $taille_y = 1; } # {300,0} -> c'est 300 qui compte - elseif ($taille_x == 0 and $taille_y === 0) { + elseif ($taille_x == 0 && $taille_y === 0) { return ''; } @@ -473,21 +469,21 @@ function image_reduire( // Determiner la taille x,y maxi // prendre le reglage de previsu par defaut if ($taille === -1) { - $taille = (isset($GLOBALS['meta']['taille_preview']) and intval($GLOBALS['meta']['taille_preview'])) - ? intval($GLOBALS['meta']['taille_preview']) + $taille = (isset($GLOBALS['meta']['taille_preview']) && (int) $GLOBALS['meta']['taille_preview']) + ? (int) $GLOBALS['meta']['taille_preview'] : 150; } if ($taille_y === -1) { $taille_y = $taille; } - if ($taille === 0 and $taille_y > 0) { + if ($taille === 0 && $taille_y > 0) { $taille = 10000; } # {0,300} -> c'est 300 qui compte - elseif ($taille > 0 and $taille_y === 0) { + elseif ($taille > 0 && $taille_y === 0) { $taille_y = 10000; } # {300,0} -> c'est 300 qui compte - elseif ($taille == 0 and $taille_y === 0) { + elseif ($taille == 0 && $taille_y === 0) { return ''; } @@ -547,9 +543,7 @@ function image_reduire_par($img, $val = 1, $force = false) { $l = 0; } - $img = image_reduire($img, $l, $h, $force); - - return $img; + return image_reduire($img, $l, $h, $force); } /** diff --git a/ecrire/inc/filtres_mime.php b/ecrire/inc/filtres_mime.php index 6be6bac8e9..709e355fcf 100644 --- a/ecrire/inc/filtres_mime.php +++ b/ecrire/inc/filtres_mime.php @@ -137,12 +137,12 @@ function filtre_text_csv_dist($t) { include_spip('inc/csv'); [$entete, $lignes, $caption] = analyse_csv($t); foreach ($lignes as &$l) { - $l = join('|', $l); + $l = implode('|', $l); } - $corps = join("\n", $lignes) . "\n"; + $corps = implode("\n", $lignes) . "\n"; $corps = $caption . "\n|{{" . - join('}}|{{', $entete) . + implode('}}|{{', $entete) . '}}|' . "\n|" . str_replace("\n", "|\n|", $corps); @@ -171,7 +171,7 @@ function filtre_text_html_dist($t) { $style = ''; // recuperer les styles internes if (preg_match_all(',<style>(.*?)</style>,is', $h, $r, PREG_PATTERN_ORDER)) { - $style = join("\n", $r[1]); + $style = implode("\n", $r[1]); } // ... et externes @@ -186,9 +186,9 @@ function filtre_text_html_dist($t) { } } // Pourquoi SafeHtml transforme-t-il en texte les scripts dans Body ? - $t = safehtml(preg_replace(',<script' . '.*?</script>,is', '', $t)); + $t = safehtml(preg_replace(',<script.*?</script>,is', '', $t)); - return (!$style ? '' : "\n<style>" . $style . '</style>') . $t; + return ($style ? "\n<style>" . $style . '</style>' : '') . $t; } /** diff --git a/ecrire/inc/filtres_selecteur_generique.php b/ecrire/inc/filtres_selecteur_generique.php index e27a2282f3..b7f15f68c1 100644 --- a/ecrire/inc/filtres_selecteur_generique.php +++ b/ecrire/inc/filtres_selecteur_generique.php @@ -67,12 +67,10 @@ function selecteur_lister_objets($includelist = [], $excludelist = []) { } } - $objets = [ + return [ 'selectionner' => array_unique($objets_selectionner), 'afficher' => array_unique($objets_afficher), ]; - - return $objets; } /** @@ -102,21 +100,21 @@ function picker_selected($selected, $type = '') { $select = []; $type = preg_replace(',\W,', '', $type); - if ($selected and !is_array($selected)) { + if ($selected && !is_array($selected)) { $selected = explode(',', $selected); } if (is_array($selected)) { foreach ($selected as $value) { // Si c'est le bon format déjà - if (preg_match('/^([\w]+)[|]([0-9]+)$/', $value, $captures)) { + if (preg_match('/^([\w]+)[|](\d+)$/', $value, $captures)) { $objet = $captures[1]; - $id_objet = intval($captures[2]); + $id_objet = (int) $captures[2]; // Si on cherche un type et que c'est le bon, on renvoit un tableau que d'identifiants - if (is_string($type) and $type == $objet and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { + if (is_string($type) && $type == $objet && ($id_objet || in_array($objet, ['racine', 'rubrique']))) { $select[] = $id_objet; - } elseif (!$type and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { + } elseif (!$type && ($id_objet || in_array($objet, ['racine', 'rubrique']))) { $select[] = ['objet' => $objet, 'id_objet' => $id_objet]; } } @@ -133,7 +131,7 @@ function picker_selected($selected, $type = '') { * * @param string|int $ref * Référence de l'objet à chercher sous forme raccourcie rub123 art123 ou meme 123 si pas d'ambiguité ou si un article - * @param mixed $rubriques_ou_objets + * @param array|bool $rubriques_ou_objets * Soit un booléen (pouvant être une chaîne vide aussi) indiquant que les rubriques sont sélectionnables * soit un tableau complet des objets sélectionnables. * @param bool $articles @@ -145,7 +143,7 @@ function picker_identifie_id_rapide($ref, $rubriques_ou_objets = false, $article // On construit un tableau des objets sélectionnables suivant les paramètres $objets = []; - if ($rubriques_ou_objets and is_array($rubriques_ou_objets)) { + if ($rubriques_ou_objets && is_array($rubriques_ou_objets)) { $objets = $rubriques_ou_objets; } else { if ($rubriques_ou_objets) { @@ -157,10 +155,10 @@ function picker_identifie_id_rapide($ref, $rubriques_ou_objets = false, $article } // si id numerique et un seul objet possible, pas d'ambiguite - if (is_numeric($ref) and count($objets) === 1) { + if (is_numeric($ref) && count($objets) === 1) { $type = reset($objets); $type = objet_type($type); - $id = intval($ref); + $id = (int) $ref; $ref = $type . $ref; } else { @@ -215,26 +213,26 @@ function test_enfants_rubrique($id_rubrique, $types = []) { $types = (is_array($types) ? array_filter($types) : []); // recuperer tous les freres et soeurs de la rubrique visee - $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); - $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent=' . intval($id_parent)); + $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . (int) $id_rubrique); + $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent=' . (int) $id_parent); $fratrie = array_column($fratrie, 'id_rubrique'); $has = sql_allfetsel('DISTINCT id_parent', 'spip_rubriques', sql_in('id_parent', $fratrie)); $has = array_column($has, 'id_parent'); $fratrie = array_diff($fratrie, $has); - while (count($fratrie) and is_array($types) and count($types)) { + while (count($fratrie) && is_array($types) && count($types)) { $type = array_shift($types); $h = sql_allfetsel('DISTINCT id_rubrique', table_objet_sql($type), sql_in('id_rubrique', $fratrie)); $h = array_column($h, 'id_rubrique'); - $has = array_merge($has, $h); + $has = [...$has, ...$h]; $fratrie = array_diff($fratrie, $h); } - if (count($has)) { - $has_child = $has_child + array_combine($has, array_pad([], count($has), true)); + if ($has !== []) { + $has_child += array_combine($has, array_pad([], count($has), true)); } - if (count($fratrie)) { - $has_child = $has_child + array_combine($fratrie, array_pad([], count($fratrie), false)); + if ($fratrie !== []) { + $has_child += array_combine($fratrie, array_pad([], count($fratrie), false)); } } diff --git a/ecrire/inc/genie.php b/ecrire/inc/genie.php index a2566724da..00ef1204d5 100644 --- a/ecrire/inc/genie.php +++ b/ecrire/inc/genie.php @@ -121,9 +121,10 @@ function taches_generales($taches_generales = []) { // nouveautes if ( - isset($GLOBALS['meta']['adresse_neuf']) and $GLOBALS['meta']['adresse_neuf'] - and $GLOBALS['meta']['jours_neuf'] - and ($GLOBALS['meta']['quoi_de_neuf'] == 'oui') + isset($GLOBALS['meta']['adresse_neuf']) + && $GLOBALS['meta']['adresse_neuf'] + && $GLOBALS['meta']['jours_neuf'] + && $GLOBALS['meta']['quoi_de_neuf'] == 'oui' ) { $taches_generales['mail'] = 3600 * 24 * $GLOBALS['meta']['jours_neuf']; } diff --git a/ecrire/inc/icone_renommer.php b/ecrire/inc/icone_renommer.php index d8cc435fa7..ba400d61f4 100644 --- a/ecrire/inc/icone_renommer.php +++ b/ecrire/inc/icone_renommer.php @@ -19,14 +19,14 @@ include_spip('base/objets'); function inc_icone_renommer_dist($fond, $fonction) { $size = 24; if ( - preg_match('/(?:-([0-9]{1,3}))?([.](gif|png|svg))?$/i', $fond, $match) - and ((isset($match[0]) and $match[0]) or (isset($match[1]) and $match[1])) + preg_match('/(?:-(\d{1,3}))?([.](gif|png|svg))?$/i', $fond, $match) + && (isset($match[0]) && $match[0] || isset($match[1]) && $match[1]) ) { - if (isset($match[1]) and $match[1]) { + if (isset($match[1]) && $match[1]) { $size = $match[1]; } $type = substr($fond, 0, -strlen($match[0])); - if (!isset($match[2]) or !$match[2]) { + if (!isset($match[2]) || !$match[2]) { $fond .= '.png'; } } else { @@ -52,8 +52,8 @@ function inc_icone_renommer_dist($fond, $fonction) { if ( $rtl - and $fr = $dir . '/' . str_replace("$type-", "$type-rtl-", basename($icone)) - and file_exists($fr) + && ($fr = $dir . '/' . str_replace("$type-", "$type-rtl-", basename($icone))) + && file_exists($fr) ) { $fond = $fr; } diff --git a/ecrire/inc/importer_csv.php b/ecrire/inc/importer_csv.php index a64b6b82bb..ece4b716ab 100644 --- a/ecrire/inc/importer_csv.php +++ b/ecrire/inc/importer_csv.php @@ -33,7 +33,7 @@ include_spip('inc/charsets'); * Importe un texte de CSV dans un charset et le passe dans le charset du site (utf8 probablement) * Les CSV peuvent sous ms@@@ avoir le charset 'iso-8859-1', mais pasfois aussi 'windows-1252' :/ * - * @param mixed $texte + * @param string $texte * @param bool|string $definir_charset_source * false : ne fait rien * string : utilisera pour les prochains imports le charset indiqué @@ -44,18 +44,10 @@ function importer_csv_importcharset($texte, $definir_charset_source = false) { // le plus frequent, en particulier avec les trucs de ms@@@ static $charset_source = 'iso-8859-1'; if ($definir_charset_source) { - if ($definir_charset_source === true) { - $charset_source = 'iso-8859-1'; - } else { - $charset_source = $definir_charset_source; - } + $charset_source = $definir_charset_source === true ? 'iso-8859-1' : $definir_charset_source; } // mais open-office sait faire mieux, donc mefiance ! - if (is_utf8($texte)) { - $charset = 'utf-8'; - } else { - $charset = $charset_source; - } + $charset = is_utf8($texte) ? 'utf-8' : $charset_source; return importer_charset($texte, $charset); } @@ -108,11 +100,8 @@ function inc_importer_csv_dist($file, $options = []) { $options = array_merge($default_options, $options); - $return = false; - if ( - @file_exists($file) - and $handle = fopen($file, 'r') - ) { + $return = []; + if (@file_exists($file) && ($handle = fopen($file, 'r'))) { if ($options['charset_source']) { importer_csv_importcharset('', $options['charset_source']); } @@ -123,25 +112,21 @@ function inc_importer_csv_dist($file, $options = []) { $header = array_map('importer_csv_nettoie_key', $header); $header_type = []; foreach ($header as $heading) { - if (!isset($header_type[$heading])) { - $header_type[$heading] = 'scalar'; - } else { - $header_type[$heading] = 'array'; - } + $header_type[$heading] = isset($header_type[$heading]) ? 'array' : 'scalar'; } } } while (($data = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos'])) !== false) { $data = array_map('importer_csv_importcharset', $data); - if ($options['head'] and isset($header)) { + if ($options['head'] && isset($header)) { $row = []; foreach ($header as $key => $heading) { if ($header_type[$heading] == 'array') { if (!isset($row[$heading])) { $row[$heading] = []; } - if (isset($data[$key]) and strlen($data[$key])) { + if (isset($data[$key]) && strlen($data[$key])) { $row[$heading][] = $data[$key]; } } else { @@ -158,5 +143,5 @@ function inc_importer_csv_dist($file, $options = []) { } } - return $return; + return $return ?: false; } diff --git a/ecrire/inc/livrer_fichier.php b/ecrire/inc/livrer_fichier.php index ebf38c98e0..57b6859959 100644 --- a/ecrire/inc/livrer_fichier.php +++ b/ecrire/inc/livrer_fichier.php @@ -41,11 +41,11 @@ function spip_livrer_fichier($fichier, $content_type = 'application/octet-stream 'range' => null, ]; $options = array_merge($defaut, $options); - if (is_numeric($options['expires']) and $options['expires'] > 0) { + if (is_numeric($options['expires']) && $options['expires'] > 0) { $options['expires'] = gmdate('D, d M Y H:i:s', time() + $options['expires']) . ' GMT'; } - if (is_null($options) and isset($_SERVER['HTTP_RANGE'])) { + if (is_null($options) && isset($_SERVER['HTTP_RANGE'])) { $options['range'] = $_SERVER['HTTP_RANGE']; } @@ -73,9 +73,7 @@ function spip_livrer_fichier_entetes($fichier, $content_type = 'application/octe header('Accept-Ranges: bytes'); header('Content-Type: ' . $content_type); - if ($fs = stat($fichier) - and !empty($fs['size']) - and !empty($fs['mtime'])) { + if (($fs = stat($fichier)) && !empty($fs['size']) && !empty($fs['mtime'])) { header("Last-Modified: " . gmdate("D, d M Y H:i:s", $fs['mtime']) . " GMT"); header(sprintf('Etag: "%x-%x"', $fs['size'], str_pad($fs['mtime'], 16, "0"))); } @@ -146,7 +144,7 @@ function spip_livrer_fichier_partie($fichier, $range = null) { // Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451" - if ($range and preg_match('%bytes=(\d+)-(\d+)?%i', $range, $match)) { + if ($range && preg_match('%bytes=(\d+)-(\d+)?%i', $range, $match)) { ### Offset signifies where we should begin to read the file $byteOffset = (int)$match[1]; diff --git a/ecrire/inc/log.php b/ecrire/inc/log.php index 9bdeedd4bd..00b347a82f 100644 --- a/ecrire/inc/log.php +++ b/ecrire/inc/log.php @@ -18,7 +18,7 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) static $compteur = []; static $debugverb = ''; // pour ne pas le recalculer au reappel - if (is_null($logname) or !is_string($logname)) { + if (is_null($logname) || !is_string($logname)) { $logname = defined('_FILE_LOG') ? _FILE_LOG : 'spip'; } if (!isset($compteur[$logname])) { @@ -26,12 +26,8 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) } if ( $logname != 'maj' - and defined('_MAX_LOG') - and ( - $compteur[$logname]++ > _MAX_LOG - or !$GLOBALS['nombre_de_logs'] - or !$GLOBALS['taille_des_logs'] - ) + && defined('_MAX_LOG') + && ($compteur[$logname]++ > _MAX_LOG || !$GLOBALS['nombre_de_logs'] || !$GLOBALS['taille_des_logs']) ) { return; } @@ -65,11 +61,11 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) $message = var_export($message, true); } - if (!$debugverb and defined('_LOG_FILELINE') and _LOG_FILELINE) { + if (!$debugverb && defined('_LOG_FILELINE') && _LOG_FILELINE) { $debug = debug_backtrace(); $l = $debug[1]['line']; $fi = $debug[1]['file']; - if (strncmp($fi, _ROOT_RACINE, strlen(_ROOT_RACINE)) == 0) { + if (str_starts_with($fi, _ROOT_RACINE)) { $fi = substr($fi, strlen(_ROOT_RACINE)); } $fu = $debug[2]['function'] ?? ''; @@ -85,7 +81,7 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) if ( @is_readable($logfile) - and (!$s = @filesize($logfile) or $s > $GLOBALS['taille_des_logs'] * 1024) + && ((!$s = @filesize($logfile)) || $s > $GLOBALS['taille_des_logs'] * 1024) ) { $rotate = $GLOBALS['nombre_de_logs']; $m .= "[-- rotate --]\n"; @@ -93,13 +89,12 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) $f = @fopen($logfile, 'ab'); if ($f) { - fputs($f, (defined('_LOG_BRUT') and _LOG_BRUT) ? $m : str_replace('<', '<', $m)); + fwrite($f, (defined('_LOG_BRUT') && _LOG_BRUT) ? $m : str_replace('<', '<', $m)); fclose($f); } if ( - $rotate-- > 0 - and function_exists('spip_unlink') + $rotate-- > 0 && function_exists('spip_unlink') ) { spip_unlink($logfile . '.' . $rotate); while ($rotate--) { @@ -108,10 +103,7 @@ function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) } // Dupliquer les erreurs specifiques dans le log general - if ( - defined('_FILE_LOG') - and ($logname !== _FILE_LOG) - ) { + if (defined('_FILE_LOG') && $logname !== _FILE_LOG) { inc_log_dist($logname == 'maj' ? 'cf maj.log' : $message); } $debugverb = ''; -- GitLab