From 74251660e2faffe8078e45bcab406809e6849b4f Mon Sep 17 00:00:00 2001
From: Gilles Vincent <gilles.vincent@gmail.com>
Date: Thu, 4 Feb 2016 23:32:58 +0000
Subject: [PATCH] =?UTF-8?q?Avant=20de=20d'utiliser=20un=20readdir(),=20il?=
 =?UTF-8?q?=20faut=20toujours=20verifier=20que=20le=20r=C3=A9sultat=20de?=
 =?UTF-8?q?=20la=20fonction=20opendir()=20est=20une=20ressource=20valide.?=
 =?UTF-8?q?=20Nettoyage=20du=20code=20pour=20enlever=20les=20'@'=20inutile?=
 =?UTF-8?q?s=20devant=20opendir()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ecrire/action/calculer_taille_cache.php |  2 +-
 ecrire/exec/admin_plugin.php            |  2 +-
 ecrire/inc/documents.php                | 19 ++++++-------
 ecrire/inc/flock.php                    |  2 +-
 ecrire/inc/install.php                  |  2 +-
 ecrire/inc/invalideur.php               |  6 ++---
 ecrire/inc/plugin.php                   |  2 +-
 ecrire/maj/svn10000.php                 | 36 +++++++++++++------------
 ecrire/public/composer.php              |  2 +-
 prive/formulaires/declarer_bases.php    |  2 +-
 10 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/ecrire/action/calculer_taille_cache.php b/ecrire/action/calculer_taille_cache.php
index f74b01a4d1..6a1b01a9be 100644
--- a/ecrire/action/calculer_taille_cache.php
+++ b/ecrire/action/calculer_taille_cache.php
@@ -75,7 +75,7 @@ function action_calculer_taille_cache_dist($arg = null) {
  * @return int Taille en octets
  */
 function calculer_taille_dossier($dir) {
-	$handle = @opendir($dir);
+	$handle = opendir($dir);
 	if (!$handle) {
 		return 0;
 	}
diff --git a/ecrire/exec/admin_plugin.php b/ecrire/exec/admin_plugin.php
index 03f9138489..df4712dc1b 100644
--- a/ecrire/exec/admin_plugin.php
+++ b/ecrire/exec/admin_plugin.php
@@ -297,7 +297,7 @@ function liste_librairies() {
 	$libs = array();
 	foreach (array_reverse(creer_chemin()) as $d) {
 		if (is_dir($dir = $d . 'lib/')
-			and $t = @opendir($dir)
+			and $t = opendir($dir)
 		) {
 			while (($f = readdir($t)) !== false) {
 				if ($f[0] != '.'
diff --git a/ecrire/inc/documents.php b/ecrire/inc/documents.php
index a78f86928c..6531ad1eed 100644
--- a/ecrire/inc/documents.php
+++ b/ecrire/inc/documents.php
@@ -100,15 +100,16 @@ function creer_repertoire_documents($ext) {
  * @param string $nom
  */
 function effacer_repertoire_temporaire($nom) {
-	$d = opendir($nom);
-	while (($f = readdir($d)) !== false) {
-		if (is_file("$nom/$f")) {
-			spip_unlink("$nom/$f");
-		} else {
-			if ($f <> '.' and $f <> '..'
-				and is_dir("$nom/$f")
-			) {
-				effacer_repertoire_temporaire("$nom/$f");
+	if ($d = opendir($nom)) {
+		while (($f = readdir($d)) !== false) {
+			if (is_file("$nom/$f")) {
+				spip_unlink("$nom/$f");
+			} else {
+				if ($f <> '.' and $f <> '..'
+					and is_dir("$nom/$f")
+				) {
+					effacer_repertoire_temporaire("$nom/$f");
+				}
 			}
 		}
 	}
diff --git a/ecrire/inc/flock.php b/ecrire/inc/flock.php
index 3b6439465b..2553cce925 100644
--- a/ecrire/inc/flock.php
+++ b/ecrire/inc/flock.php
@@ -673,7 +673,7 @@ function preg_files($dir, $pattern = -1 /* AUTO */, $maxfiles = 10000, $recurs =
 		$dir = '.';
 	}
 
-	if (@is_dir($dir) and is_readable($dir) and $d = @opendir($dir)) {
+	if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) {
 		while (($f = readdir($d)) !== false && ($nbfiles < $maxfiles)) {
 			if ($f[0] != '.' # ignorer . .. .svn etc
 				and $f != 'CVS'
diff --git a/ecrire/inc/install.php b/ecrire/inc/install.php
index df533f8955..ab9d6c242b 100644
--- a/ecrire/inc/install.php
+++ b/ecrire/inc/install.php
@@ -354,7 +354,7 @@ function fieldset_champs($champs = array()) {
 function install_select_serveur() {
 	$options = array();
 	$dir = _DIR_RESTREINT . 'req/';
-	$d = @opendir($dir);
+	$d = opendir($dir);
 	if (!$d) {
 		return array();
 	}
diff --git a/ecrire/inc/invalideur.php b/ecrire/inc/invalideur.php
index 064929b511..f5436c5c13 100644
--- a/ecrire/inc/invalideur.php
+++ b/ecrire/inc/invalideur.php
@@ -50,7 +50,7 @@ if (!defined('_AGE_CACHE_ATIME')) {
 function nombre_de_fichiers_repertoire($dir, $nb_estim_taille = 20) {
 	$taille = 0; // mesurer la taille de N fichiers au hasard dans le repertoire
 	$nb = $nb_estim_taille;
-	if (!$h = @opendir($dir)) {
+	if (!$h = opendir($dir)) {
 		return false;
 	}
 	$total = 0;
@@ -84,7 +84,7 @@ function taille_du_cache() {
 	$time = $GLOBALS['meta']['cache_mark'];
 	for ($i=0; $i < 256; $i++) {
 		$dir = _DIR_CACHE.sprintf('%02s', dechex($i));
-		if (@is_dir($dir) AND is_readable($dir) AND $d = @opendir($dir)) {
+		if (@is_dir($dir) AND is_readable($dir) AND $d = opendir($dir)) {
 			while (($f = readdir($d)) !== false) {
 				if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) AND $a = stat("$dir/$f")) {
 					$n++;
@@ -178,7 +178,7 @@ function suivre_invalideur($cond, $modif = true) {
  *     Nombre de fichiers supprimés
  **/
 function purger_repertoire($dir, $options = array()) {
-	$handle = @opendir($dir);
+	$handle = opendir($dir);
 	if (!$handle) {
 		return;
 	}
diff --git a/ecrire/inc/plugin.php b/ecrire/inc/plugin.php
index 81de076efb..d79ee19be9 100644
--- a/ecrire/inc/plugin.php
+++ b/ecrire/inc/plugin.php
@@ -76,7 +76,7 @@ function fast_find_plugin_dirs($dir, $max_prof = 100) {
 	}
 
 	$subdirs = array();
-	if (@is_dir($dir) and is_readable($dir) and $d = @opendir($dir)) {
+	if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) {
 		while (($f = readdir($d)) !== false) {
 			if ($f[0] != '.' # ignorer . .. .svn etc
 				and $f != 'CVS'
diff --git a/ecrire/maj/svn10000.php b/ecrire/maj/svn10000.php
index 7000a133f7..a11a901ef3 100644
--- a/ecrire/maj/svn10000.php
+++ b/ecrire/maj/svn10000.php
@@ -654,11 +654,12 @@ $GLOBALS['maj'][19268] = array(
  **/
 function supprimer_toutes_sessions() {
 	spip_log("supprimer sessions auteur");
-	$dir = opendir(_DIR_SESSIONS);
-	while (($f = readdir($dir)) !== false) {
-		spip_unlink(_DIR_SESSIONS . $f);
-		if (time() >= _TIME_OUT) {
-			return;
+	if ($dir = opendir(_DIR_SESSIONS)) {
+		while (($f = readdir($dir)) !== false) {
+			spip_unlink(_DIR_SESSIONS . $f);
+			if (time() >= _TIME_OUT) {
+				return;
+			}
 		}
 	}
 }
@@ -674,18 +675,19 @@ $GLOBALS['maj'][21676] = array(
 function ranger_cache_gd2() {
 	spip_log("ranger_cache_gd2");
 	$base = _DIR_VAR . "cache-gd2/";
-	$dir = opendir($base);
-	while (($f = readdir($dir)) !== false) {
-		if (!is_dir($base . $f) and strncmp($f, ".", 1) !== 0
-			and preg_match(",[0-9a-f]{32}\.\w+,", $f)
-		) {
-			$sub = substr($f, 0, 2);
-			$sub = sous_repertoire($base, $sub);
-			@rename($base . $f, $sub . substr($f, 2));
-			@unlink($base . $f); // au cas ou le rename a foire (collision)
-		}
-		if (time() >= _TIME_OUT) {
-			return;
+	if ($dir = opendir($base)) {
+		while (($f = readdir($dir)) !== false) {
+			if (!is_dir($base . $f) and strncmp($f, ".", 1) !== 0
+				and preg_match(",[0-9a-f]{32}\.\w+,", $f)
+			) {
+				$sub = substr($f, 0, 2);
+				$sub = sous_repertoire($base, $sub);
+				@rename($base . $f, $sub . substr($f, 2));
+				@unlink($base . $f); // au cas ou le rename a foire (collision)
+			}
+			if (time() >= _TIME_OUT) {
+				return;
+			}
 		}
 	}
 }
diff --git a/ecrire/public/composer.php b/ecrire/public/composer.php
index b679a3604e..46e00dc96f 100644
--- a/ecrire/public/composer.php
+++ b/ecrire/public/composer.php
@@ -550,7 +550,7 @@ function lister_objets_avec_logos($type) {
 		. join('|', $GLOBALS['formats_logos'])
 		. ")$/";
 
-	if ($d = @opendir(_DIR_LOGOS)) {
+	if ($d = opendir(_DIR_LOGOS)) {
 		while (($f = readdir($d)) !== false) {
 			if (preg_match($type, $f, $r)) {
 				$logos[] = $r[1];
diff --git a/prive/formulaires/declarer_bases.php b/prive/formulaires/declarer_bases.php
index 8a7d4eb6d8..91a3fcd01f 100644
--- a/prive/formulaires/declarer_bases.php
+++ b/prive/formulaires/declarer_bases.php
@@ -56,7 +56,7 @@ function formulaires_declarer_bases_charger_dist() {
 function liste_serveurs() {
 	$options = array();
 	$dir = _DIR_RESTREINT . 'req/';
-	$d = @opendir($dir);
+	$d = opendir($dir);
 	if (!$d) {
 		return array();
 	}
-- 
GitLab