Completed
Push — master ( da65df...32470c )
by cam
02:06
created
ecrire/inc/flock.php 2 patches
Indentation   +414 added lines, -414 removed lines patch added patch discarded remove patch
@@ -16,12 +16,12 @@  discard block
 block discarded – undo
16 16
  **/
17 17
 
18 18
 if (!defined('_ECRIRE_INC_VERSION')) {
19
-	return;
19
+    return;
20 20
 }
21 21
 
22 22
 if (!defined('_TEST_FILE_EXISTS')) {
23
-	/** Permettre d'éviter des tests file_exists sur certains hébergeurs */
24
-	define('_TEST_FILE_EXISTS', preg_match(',(online|free)[.]fr$,', $_ENV['HTTP_HOST'] ?? ''));
23
+    /** Permettre d'éviter des tests file_exists sur certains hébergeurs */
24
+    define('_TEST_FILE_EXISTS', preg_match(',(online|free)[.]fr$,', $_ENV['HTTP_HOST'] ?? ''));
25 25
 }
26 26
 
27 27
 #define('_SPIP_LOCK_MODE',0); // ne pas utiliser de lock (deconseille)
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
 #define('_SPIP_LOCK_MODE',2); // utiliser le nfslock de spip
30 30
 
31 31
 if (_SPIP_LOCK_MODE == 2) {
32
-	include_spip('inc/nfslock');
32
+    include_spip('inc/nfslock');
33 33
 }
34 34
 
35 35
 $GLOBALS['liste_verrous'] = [];
@@ -52,24 +52,24 @@  discard block
 block discarded – undo
52 52
  *     Ressource sur le fichier ouvert, sinon false.
53 53
  **/
54 54
 function spip_fopen_lock($fichier, $mode, $verrou) {
55
-	if (_SPIP_LOCK_MODE == 1) {
56
-		if ($fl = @fopen($fichier, $mode)) {
57
-			// verrou
58
-			@flock($fl, $verrou);
59
-		}
60
-
61
-		return $fl;
62
-	} elseif (_SPIP_LOCK_MODE == 2) {
63
-		if (($verrou = spip_nfslock($fichier)) && ($fl = @fopen($fichier, $mode))) {
64
-			$GLOBALS['liste_verrous'][$fl] = [$fichier, $verrou];
65
-
66
-			return $fl;
67
-		} else {
68
-			return false;
69
-		}
70
-	}
71
-
72
-	return @fopen($fichier, $mode);
55
+    if (_SPIP_LOCK_MODE == 1) {
56
+        if ($fl = @fopen($fichier, $mode)) {
57
+            // verrou
58
+            @flock($fl, $verrou);
59
+        }
60
+
61
+        return $fl;
62
+    } elseif (_SPIP_LOCK_MODE == 2) {
63
+        if (($verrou = spip_nfslock($fichier)) && ($fl = @fopen($fichier, $mode))) {
64
+            $GLOBALS['liste_verrous'][$fl] = [$fichier, $verrou];
65
+
66
+            return $fl;
67
+        } else {
68
+            return false;
69
+        }
70
+    }
71
+
72
+    return @fopen($fichier, $mode);
73 73
 }
74 74
 
75 75
 /**
@@ -84,14 +84,14 @@  discard block
 block discarded – undo
84 84
  *     true si succès, false sinon.
85 85
  **/
86 86
 function spip_fclose_unlock($handle) {
87
-	if (_SPIP_LOCK_MODE == 1) {
88
-		@flock($handle, LOCK_UN);
89
-	} elseif (_SPIP_LOCK_MODE == 2) {
90
-		spip_nfsunlock(reset($GLOBALS['liste_verrous'][$handle]), end($GLOBALS['liste_verrous'][$handle]));
91
-		unset($GLOBALS['liste_verrous'][$handle]);
92
-	}
93
-
94
-	return @fclose($handle);
87
+    if (_SPIP_LOCK_MODE == 1) {
88
+        @flock($handle, LOCK_UN);
89
+    } elseif (_SPIP_LOCK_MODE == 2) {
90
+        spip_nfsunlock(reset($GLOBALS['liste_verrous'][$handle]), end($GLOBALS['liste_verrous'][$handle]));
91
+        unset($GLOBALS['liste_verrous'][$handle]);
92
+    }
93
+
94
+    return @fclose($handle);
95 95
 }
96 96
 
97 97
 
@@ -105,23 +105,23 @@  discard block
 block discarded – undo
105 105
  *     Contenu du fichier
106 106
  **/
107 107
 function spip_file_get_contents($fichier) {
108
-	if (!str_ends_with($fichier, '.gz')) {
109
-		if (function_exists('file_get_contents')) {
110
-			// quand on est sous windows on ne sait pas si file_get_contents marche
111
-			// on essaye : si ca retourne du contenu alors c'est bon
112
-			// sinon on fait un file() pour avoir le coeur net
113
-			$contenu = @file_get_contents($fichier);
114
-			if (!$contenu && _OS_SERVEUR == 'windows') {
115
-				$contenu = @file($fichier);
116
-			}
117
-		} else {
118
-			$contenu = @file($fichier);
119
-		}
120
-	} else {
121
-		$contenu = @gzfile($fichier);
122
-	}
123
-
124
-	return is_array($contenu) ? implode('', $contenu) : (string)$contenu;
108
+    if (!str_ends_with($fichier, '.gz')) {
109
+        if (function_exists('file_get_contents')) {
110
+            // quand on est sous windows on ne sait pas si file_get_contents marche
111
+            // on essaye : si ca retourne du contenu alors c'est bon
112
+            // sinon on fait un file() pour avoir le coeur net
113
+            $contenu = @file_get_contents($fichier);
114
+            if (!$contenu && _OS_SERVEUR == 'windows') {
115
+                $contenu = @file($fichier);
116
+            }
117
+        } else {
118
+            $contenu = @file($fichier);
119
+        }
120
+    } else {
121
+        $contenu = @gzfile($fichier);
122
+    }
123
+
124
+    return is_array($contenu) ? implode('', $contenu) : (string)$contenu;
125 125
 }
126 126
 
127 127
 
@@ -146,48 +146,48 @@  discard block
 block discarded – undo
146 146
  *     true si l'opération a réussie, false sinon.
147 147
  **/
148 148
 function lire_fichier($fichier, &$contenu, $options = []) {
149
-	$contenu = '';
150
-	// inutile car si le fichier n'existe pas, le lock va renvoyer false juste apres
151
-	// economisons donc les acces disque, sauf chez free qui rale pour un rien
152
-	if (_TEST_FILE_EXISTS && !@file_exists($fichier)) {
153
-		return false;
154
-	}
155
-
156
-	#spip_timer('lire_fichier');
157
-
158
-	// pas de @ sur spip_fopen_lock qui est silencieux de toute facon
159
-	if ($fl = spip_fopen_lock($fichier, 'r', LOCK_SH)) {
160
-		// lire le fichier avant tout
161
-		$contenu = spip_file_get_contents($fichier);
162
-
163
-		// le fichier a-t-il ete supprime par le locker ?
164
-		// on ne verifie que si la tentative de lecture a echoue
165
-		// pour discriminer un contenu vide d'un fichier absent
166
-		// et eviter un acces disque
167
-		if (!$contenu && !@file_exists($fichier)) {
168
-			spip_fclose_unlock($fl);
169
-
170
-			return false;
171
-		}
172
-
173
-		// liberer le verrou
174
-		spip_fclose_unlock($fl);
175
-
176
-		// Verifications
177
-		$ok = true;
178
-		if (isset($options['phpcheck']) && $options['phpcheck'] == 'oui') {
179
-			$ok &= (preg_match(",[?]>\n?$,", $contenu));
180
-		}
181
-
182
-		#spip_log("$fread $fichier ".spip_timer('lire_fichier'));
183
-		if (!$ok) {
184
-			spip_log("echec lecture $fichier");
185
-		}
186
-
187
-		return $ok;
188
-	}
189
-
190
-	return false;
149
+    $contenu = '';
150
+    // inutile car si le fichier n'existe pas, le lock va renvoyer false juste apres
151
+    // economisons donc les acces disque, sauf chez free qui rale pour un rien
152
+    if (_TEST_FILE_EXISTS && !@file_exists($fichier)) {
153
+        return false;
154
+    }
155
+
156
+    #spip_timer('lire_fichier');
157
+
158
+    // pas de @ sur spip_fopen_lock qui est silencieux de toute facon
159
+    if ($fl = spip_fopen_lock($fichier, 'r', LOCK_SH)) {
160
+        // lire le fichier avant tout
161
+        $contenu = spip_file_get_contents($fichier);
162
+
163
+        // le fichier a-t-il ete supprime par le locker ?
164
+        // on ne verifie que si la tentative de lecture a echoue
165
+        // pour discriminer un contenu vide d'un fichier absent
166
+        // et eviter un acces disque
167
+        if (!$contenu && !@file_exists($fichier)) {
168
+            spip_fclose_unlock($fl);
169
+
170
+            return false;
171
+        }
172
+
173
+        // liberer le verrou
174
+        spip_fclose_unlock($fl);
175
+
176
+        // Verifications
177
+        $ok = true;
178
+        if (isset($options['phpcheck']) && $options['phpcheck'] == 'oui') {
179
+            $ok &= (preg_match(",[?]>\n?$,", $contenu));
180
+        }
181
+
182
+        #spip_log("$fread $fichier ".spip_timer('lire_fichier'));
183
+        if (!$ok) {
184
+            spip_log("echec lecture $fichier");
185
+        }
186
+
187
+        return $ok;
188
+    }
189
+
190
+    return false;
191 191
 }
192 192
 
193 193
 
@@ -215,92 +215,92 @@  discard block
 block discarded – undo
215 215
  **/
216 216
 function ecrire_fichier($fichier, $contenu, $ignorer_echec = false, $truncate = true) {
217 217
 
218
-	#spip_timer('ecrire_fichier');
219
-
220
-	// verrouiller le fichier destination
221
-	if ($fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) {
222
-		// ecrire les donnees, compressees le cas echeant
223
-		// (on ouvre un nouveau pointeur sur le fichier, ce qui a l'avantage
224
-		// de le recreer si le locker qui nous precede l'avait supprime...)
225
-		if (str_ends_with($fichier, '.gz')) {
226
-			$contenu = gzencode($contenu);
227
-		}
228
-		// si c'est une ecriture avec troncation , on fait plutot une ecriture complete a cote suivie unlink+rename
229
-		// pour etre sur d'avoir une operation atomique
230
-		// y compris en NFS : http://www.ietf.org/rfc/rfc1094.txt
231
-		// sauf sous wintruc ou ca ne marche pas
232
-		$ok = false;
233
-		if ($truncate && _OS_SERVEUR != 'windows') {
234
-			if (!function_exists('creer_uniqid')) {
235
-				include_spip('inc/acces');
236
-			}
237
-			$id = creer_uniqid();
238
-			// on ouvre un pointeur sur un fichier temporaire en ecriture +raz
239
-			if ($fp2 = spip_fopen_lock("$fichier.$id", 'w', LOCK_EX)) {
240
-				$s = @fwrite($fp2, (string) $contenu, $a = strlen((string) $contenu));
241
-				$ok = ($s === $a);
242
-				spip_fclose_unlock($fp2);
243
-				spip_fclose_unlock($fp);
244
-				$fp = null;
245
-				// unlink direct et pas spip_unlink car on avait deja le verrou
246
-				// a priori pas besoin car rename ecrase la cible
247
-				// @unlink($fichier);
248
-				// le rename aussitot, atomique quand on est pas sous windows
249
-				// au pire on arrive en second en cas de concourance, et le rename echoue
250
-				// --> on a la version de l'autre process qui doit etre identique
251
-				@rename("$fichier.$id", $fichier);
252
-				// precaution en cas d'echec du rename
253
-				if (!_TEST_FILE_EXISTS || @file_exists("$fichier.$id")) {
254
-					@unlink("$fichier.$id");
255
-				}
256
-				if ($ok) {
257
-					$ok = file_exists($fichier);
258
-				}
259
-			} else // echec mais penser a fermer ..
260
-			{
261
-				spip_fclose_unlock($fp);
262
-				$fp = null;
263
-			}
264
-		}
265
-		// sinon ou si methode precedente a echoueee
266
-		// on se rabat sur la methode ancienne
267
-		if (!$ok && !is_null($fp)) {
268
-			// ici on est en ajout ou sous windows, cas desespere
269
-			if ($truncate) {
270
-				@ftruncate($fp, 0);
271
-			}
272
-			$s = @fwrite($fp, $contenu, $a = strlen($contenu));
273
-
274
-			$ok = ($s == $a);
275
-			spip_fclose_unlock($fp);
276
-		}
277
-		// on tente une dernière fois file_put_contents
278
-		if (!$ok) {
279
-			$l = file_put_contents($fichier, $contenu, $truncate ? LOCK_EX : LOCK_EX | FILE_APPEND);
280
-			$ok = ($l === strlen($contenu));
281
-		}
282
-
283
-		// liberer le verrou et fermer le fichier
284
-		@chmod($fichier, _SPIP_CHMOD & 0666);
285
-		if ($ok) {
286
-			if (str_contains($fichier, '.php')) {
287
-				spip_clear_opcode_cache(realpath($fichier));
288
-			}
289
-
290
-			return $ok;
291
-		}
292
-	}
293
-
294
-	if (!$ignorer_echec) {
295
-		include_spip('inc/autoriser');
296
-		if (autoriser('chargerftp')) {
297
-			raler_fichier($fichier);
298
-		}
299
-		spip_unlink($fichier);
300
-	}
301
-	spip_log("Ecriture fichier $fichier impossible", _LOG_INFO_IMPORTANTE);
302
-
303
-	return false;
218
+    #spip_timer('ecrire_fichier');
219
+
220
+    // verrouiller le fichier destination
221
+    if ($fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) {
222
+        // ecrire les donnees, compressees le cas echeant
223
+        // (on ouvre un nouveau pointeur sur le fichier, ce qui a l'avantage
224
+        // de le recreer si le locker qui nous precede l'avait supprime...)
225
+        if (str_ends_with($fichier, '.gz')) {
226
+            $contenu = gzencode($contenu);
227
+        }
228
+        // si c'est une ecriture avec troncation , on fait plutot une ecriture complete a cote suivie unlink+rename
229
+        // pour etre sur d'avoir une operation atomique
230
+        // y compris en NFS : http://www.ietf.org/rfc/rfc1094.txt
231
+        // sauf sous wintruc ou ca ne marche pas
232
+        $ok = false;
233
+        if ($truncate && _OS_SERVEUR != 'windows') {
234
+            if (!function_exists('creer_uniqid')) {
235
+                include_spip('inc/acces');
236
+            }
237
+            $id = creer_uniqid();
238
+            // on ouvre un pointeur sur un fichier temporaire en ecriture +raz
239
+            if ($fp2 = spip_fopen_lock("$fichier.$id", 'w', LOCK_EX)) {
240
+                $s = @fwrite($fp2, (string) $contenu, $a = strlen((string) $contenu));
241
+                $ok = ($s === $a);
242
+                spip_fclose_unlock($fp2);
243
+                spip_fclose_unlock($fp);
244
+                $fp = null;
245
+                // unlink direct et pas spip_unlink car on avait deja le verrou
246
+                // a priori pas besoin car rename ecrase la cible
247
+                // @unlink($fichier);
248
+                // le rename aussitot, atomique quand on est pas sous windows
249
+                // au pire on arrive en second en cas de concourance, et le rename echoue
250
+                // --> on a la version de l'autre process qui doit etre identique
251
+                @rename("$fichier.$id", $fichier);
252
+                // precaution en cas d'echec du rename
253
+                if (!_TEST_FILE_EXISTS || @file_exists("$fichier.$id")) {
254
+                    @unlink("$fichier.$id");
255
+                }
256
+                if ($ok) {
257
+                    $ok = file_exists($fichier);
258
+                }
259
+            } else // echec mais penser a fermer ..
260
+            {
261
+                spip_fclose_unlock($fp);
262
+                $fp = null;
263
+            }
264
+        }
265
+        // sinon ou si methode precedente a echoueee
266
+        // on se rabat sur la methode ancienne
267
+        if (!$ok && !is_null($fp)) {
268
+            // ici on est en ajout ou sous windows, cas desespere
269
+            if ($truncate) {
270
+                @ftruncate($fp, 0);
271
+            }
272
+            $s = @fwrite($fp, $contenu, $a = strlen($contenu));
273
+
274
+            $ok = ($s == $a);
275
+            spip_fclose_unlock($fp);
276
+        }
277
+        // on tente une dernière fois file_put_contents
278
+        if (!$ok) {
279
+            $l = file_put_contents($fichier, $contenu, $truncate ? LOCK_EX : LOCK_EX | FILE_APPEND);
280
+            $ok = ($l === strlen($contenu));
281
+        }
282
+
283
+        // liberer le verrou et fermer le fichier
284
+        @chmod($fichier, _SPIP_CHMOD & 0666);
285
+        if ($ok) {
286
+            if (str_contains($fichier, '.php')) {
287
+                spip_clear_opcode_cache(realpath($fichier));
288
+            }
289
+
290
+            return $ok;
291
+        }
292
+    }
293
+
294
+    if (!$ignorer_echec) {
295
+        include_spip('inc/autoriser');
296
+        if (autoriser('chargerftp')) {
297
+            raler_fichier($fichier);
298
+        }
299
+        spip_unlink($fichier);
300
+    }
301
+    spip_log("Ecriture fichier $fichier impossible", _LOG_INFO_IMPORTANTE);
302
+
303
+    return false;
304 304
 }
305 305
 
306 306
 /**
@@ -320,12 +320,12 @@  discard block
 block discarded – undo
320 320
  *     Écriture avec troncation ?
321 321
  */
322 322
 function ecrire_fichier_securise($fichier, $contenu, $ecrire_quand_meme = false, $truncate = true) {
323
-	if (!str_ends_with($fichier, '.php')) {
324
-		spip_log('Erreur de programmation: ' . $fichier . ' doit finir par .php');
325
-	}
326
-	$contenu = '<' . "?php die ('Acces interdit'); ?" . ">\n" . $contenu;
323
+    if (!str_ends_with($fichier, '.php')) {
324
+        spip_log('Erreur de programmation: ' . $fichier . ' doit finir par .php');
325
+    }
326
+    $contenu = '<' . "?php die ('Acces interdit'); ?" . ">\n" . $contenu;
327 327
 
328
-	return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate);
328
+    return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate);
329 329
 }
330 330
 
331 331
 
@@ -336,25 +336,25 @@  discard block
 block discarded – undo
336 336
  * @return bool
337 337
  */
338 338
 function ecrire_fichier_calcule_si_modifie($fichier, $contenu, $force = false, $use_copy = false) {
339
-	$fichier_tmp = $fichier . '.last';
340
-	if (!ecrire_fichier($fichier_tmp, $contenu, true)) {
341
-		return false;
342
-	}
343
-	if (
344
-		$force
345
-		|| !file_exists($fichier)
346
-		|| md5_file($fichier) !== md5_file($fichier_tmp)
347
-	) {
348
-		if ($use_copy) {
349
-			@copy($fichier_tmp, $fichier);
350
-		}
351
-		else {
352
-			@rename($fichier_tmp, $fichier);
353
-		}
354
-		// eviter que PHP ne reserve le vieux timestamp
355
-		clearstatcache(true, $fichier);
356
-	}
357
-	return true;
339
+    $fichier_tmp = $fichier . '.last';
340
+    if (!ecrire_fichier($fichier_tmp, $contenu, true)) {
341
+        return false;
342
+    }
343
+    if (
344
+        $force
345
+        || !file_exists($fichier)
346
+        || md5_file($fichier) !== md5_file($fichier_tmp)
347
+    ) {
348
+        if ($use_copy) {
349
+            @copy($fichier_tmp, $fichier);
350
+        }
351
+        else {
352
+            @rename($fichier_tmp, $fichier);
353
+        }
354
+        // eviter que PHP ne reserve le vieux timestamp
355
+        clearstatcache(true, $fichier);
356
+    }
357
+    return true;
358 358
 }
359 359
 
360 360
 
@@ -375,11 +375,11 @@  discard block
 block discarded – undo
375 375
  *     true si l'opération a réussie, false sinon.
376 376
  */
377 377
 function lire_fichier_securise($fichier, &$contenu, $options = []) {
378
-	if ($res = lire_fichier($fichier, $contenu, $options)) {
379
-		$contenu = substr((string) $contenu, strlen('<?php die (\'Acces interdit\'); ?>' . "\n"));
380
-	}
378
+    if ($res = lire_fichier($fichier, $contenu, $options)) {
379
+        $contenu = substr((string) $contenu, strlen('<?php die (\'Acces interdit\'); ?>' . "\n"));
380
+    }
381 381
 
382
-	return $res;
382
+    return $res;
383 383
 }
384 384
 
385 385
 /**
@@ -391,25 +391,25 @@  discard block
 block discarded – undo
391 391
  * @uses minipres() Pour afficher le message
392 392
  **/
393 393
 function raler_fichier(string $fichier): never {
394
-	if (!defined('_SPIP_ECRIRE_SCRIPT')) {
395
-		spip_initialisation_suite();
396
-	}
397
-	include_spip('inc/minipres');
398
-	$dir = dirname($fichier);
399
-	http_response_code(401);
400
-	echo minipres(_T('texte_inc_meta_2'), "<h4 style='color: red'>"
401
-		. _T('texte_inc_meta_1', ['fichier' => $fichier])
402
-		. " <a href='"
403
-		. generer_url_ecrire('install', "etape=chmod&test_dir=$dir")
404
-		. "'>"
405
-		. _T('texte_inc_meta_2')
406
-		. '</a> '
407
-		. _T(
408
-			'texte_inc_meta_3',
409
-			['repertoire' => joli_repertoire($dir)]
410
-		)
411
-		. "</h4>\n");
412
-	exit;
394
+    if (!defined('_SPIP_ECRIRE_SCRIPT')) {
395
+        spip_initialisation_suite();
396
+    }
397
+    include_spip('inc/minipres');
398
+    $dir = dirname($fichier);
399
+    http_response_code(401);
400
+    echo minipres(_T('texte_inc_meta_2'), "<h4 style='color: red'>"
401
+        . _T('texte_inc_meta_1', ['fichier' => $fichier])
402
+        . " <a href='"
403
+        . generer_url_ecrire('install', "etape=chmod&test_dir=$dir")
404
+        . "'>"
405
+        . _T('texte_inc_meta_2')
406
+        . '</a> '
407
+        . _T(
408
+            'texte_inc_meta_3',
409
+            ['repertoire' => joli_repertoire($dir)]
410
+        )
411
+        . "</h4>\n");
412
+    exit;
413 413
 }
414 414
 
415 415
 
@@ -424,14 +424,14 @@  discard block
 block discarded – undo
424 424
  *     - true si récent, false sinon
425 425
  */
426 426
 function jeune_fichier($fichier, $n) {
427
-	if (!file_exists($fichier)) {
428
-		return false;
429
-	}
430
-	if (!$c = @filemtime($fichier)) {
431
-		return false;
432
-	}
433
-
434
-	return (time() - $n <= $c);
427
+    if (!file_exists($fichier)) {
428
+        return false;
429
+    }
430
+    if (!$c = @filemtime($fichier)) {
431
+        return false;
432
+    }
433
+
434
+    return (time() - $n <= $c);
435 435
 }
436 436
 
437 437
 /**
@@ -446,22 +446,22 @@  discard block
 block discarded – undo
446 446
  *     - false si on n'arrive pas poser le verrou ou si la suppression échoue
447 447
  */
448 448
 function supprimer_fichier($fichier, $lock = true) {
449
-	if (!@file_exists($fichier)) {
450
-		return true;
451
-	}
452
-
453
-	if ($lock) {
454
-		// verrouiller le fichier destination
455
-		if (!$fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) {
456
-			return false;
457
-		}
458
-
459
-		// liberer le verrou
460
-		spip_fclose_unlock($fp);
461
-	}
462
-
463
-	// supprimer
464
-	return @unlink($fichier);
449
+    if (!@file_exists($fichier)) {
450
+        return true;
451
+    }
452
+
453
+    if ($lock) {
454
+        // verrouiller le fichier destination
455
+        if (!$fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) {
456
+            return false;
457
+        }
458
+
459
+        // liberer le verrou
460
+        spip_fclose_unlock($fp);
461
+    }
462
+
463
+    // supprimer
464
+    return @unlink($fichier);
465 465
 }
466 466
 
467 467
 /**
@@ -471,12 +471,12 @@  discard block
 block discarded – undo
471 471
  *     Chemin du fichier
472 472
  */
473 473
 function spip_unlink($f) {
474
-	if (!is_dir($f)) {
475
-		supprimer_fichier($f, false);
476
-	} else {
477
-		@unlink("$f/.ok");
478
-		@rmdir($f);
479
-	}
474
+    if (!is_dir($f)) {
475
+        supprimer_fichier($f, false);
476
+    } else {
477
+        @unlink("$f/.ok");
478
+        @rmdir($f);
479
+    }
480 480
 }
481 481
 
482 482
 /**
@@ -490,26 +490,26 @@  discard block
 block discarded – undo
490 490
  *   The absolute path of the PHP file to invalidate.
491 491
  */
492 492
 function spip_clear_opcode_cache($filepath) {
493
-	clearstatcache(true, $filepath);
494
-
495
-	// Zend OPcache
496
-	if (function_exists('opcache_invalidate')) {
497
-		$invalidate = @opcache_invalidate($filepath, true);
498
-		// si l'invalidation a echoue lever un flag
499
-		if (!$invalidate && !defined('_spip_attend_invalidation_opcode_cache')) {
500
-			define('_spip_attend_invalidation_opcode_cache', true);
501
-		}
502
-	} elseif (!defined('_spip_attend_invalidation_opcode_cache')) {
503
-		// n'agira que si opcache est effectivement actif (il semble qu'on a pas toujours la fonction opcache_invalidate)
504
-		define('_spip_attend_invalidation_opcode_cache', true);
505
-	}
506
-	// APC.
507
-	if (function_exists('apc_delete_file')) {
508
-		// apc_delete_file() throws a PHP warning in case the specified file was
509
-		// not compiled yet.
510
-		// @see http://php.net/apc-delete-file
511
-		@apc_delete_file($filepath);
512
-	}
493
+    clearstatcache(true, $filepath);
494
+
495
+    // Zend OPcache
496
+    if (function_exists('opcache_invalidate')) {
497
+        $invalidate = @opcache_invalidate($filepath, true);
498
+        // si l'invalidation a echoue lever un flag
499
+        if (!$invalidate && !defined('_spip_attend_invalidation_opcode_cache')) {
500
+            define('_spip_attend_invalidation_opcode_cache', true);
501
+        }
502
+    } elseif (!defined('_spip_attend_invalidation_opcode_cache')) {
503
+        // n'agira que si opcache est effectivement actif (il semble qu'on a pas toujours la fonction opcache_invalidate)
504
+        define('_spip_attend_invalidation_opcode_cache', true);
505
+    }
506
+    // APC.
507
+    if (function_exists('apc_delete_file')) {
508
+        // apc_delete_file() throws a PHP warning in case the specified file was
509
+        // not compiled yet.
510
+        // @see http://php.net/apc-delete-file
511
+        @apc_delete_file($filepath);
512
+    }
513 513
 }
514 514
 
515 515
 /**
@@ -532,25 +532,25 @@  discard block
 block discarded – undo
532 532
  *
533 533
  */
534 534
 function spip_attend_invalidation_opcode_cache($timestamp = null) {
535
-	if (
536
-		function_exists('opcache_get_configuration')
537
-		&& @ini_get('opcache.enable')
538
-		&& @ini_get('opcache.validate_timestamps')
539
-		&& (($duree = (int) @ini_get('opcache.revalidate_freq')) || ($duree = 2))
540
-		&& defined('_spip_attend_invalidation_opcode_cache') // des invalidations ont echouees
541
-	) {
542
-		$wait = $duree + 1;
543
-		if ($timestamp) {
544
-			$wait -= (time() - $timestamp);
545
-			if ($wait < 0) {
546
-				$wait = 0;
547
-			}
548
-		}
549
-		spip_log('Probleme de configuration opcache.revalidate_freq ' . $duree . 's : on attend ' . $wait . 's', _LOG_INFO_IMPORTANTE);
550
-		if ($wait) {
551
-			sleep($duree + 1);
552
-		}
553
-	}
535
+    if (
536
+        function_exists('opcache_get_configuration')
537
+        && @ini_get('opcache.enable')
538
+        && @ini_get('opcache.validate_timestamps')
539
+        && (($duree = (int) @ini_get('opcache.revalidate_freq')) || ($duree = 2))
540
+        && defined('_spip_attend_invalidation_opcode_cache') // des invalidations ont echouees
541
+    ) {
542
+        $wait = $duree + 1;
543
+        if ($timestamp) {
544
+            $wait -= (time() - $timestamp);
545
+            if ($wait < 0) {
546
+                $wait = 0;
547
+            }
548
+        }
549
+        spip_log('Probleme de configuration opcache.revalidate_freq ' . $duree . 's : on attend ' . $wait . 's', _LOG_INFO_IMPORTANTE);
550
+        if ($wait) {
551
+            sleep($duree + 1);
552
+        }
553
+    }
554 554
 }
555 555
 
556 556
 
@@ -563,26 +563,26 @@  discard block
 block discarded – undo
563 563
  * @return bool Suppression reussie.
564 564
  */
565 565
 function supprimer_repertoire($dir) {
566
-	if (!file_exists($dir)) {
567
-		return true;
568
-	}
569
-	if (!is_dir($dir) || is_link($dir)) {
570
-		return @unlink($dir);
571
-	}
572
-
573
-	foreach (scandir($dir) as $item) {
574
-		if ($item == '.' || $item == '..') {
575
-			continue;
576
-		}
577
-		if (!supprimer_repertoire($dir . '/' . $item)) {
578
-			@chmod($dir . '/' . $item, 0777);
579
-			if (!supprimer_repertoire($dir . '/' . $item)) {
580
-				return false;
581
-			}
582
-		};
583
-	}
584
-
585
-	return @rmdir($dir);
566
+    if (!file_exists($dir)) {
567
+        return true;
568
+    }
569
+    if (!is_dir($dir) || is_link($dir)) {
570
+        return @unlink($dir);
571
+    }
572
+
573
+    foreach (scandir($dir) as $item) {
574
+        if ($item == '.' || $item == '..') {
575
+            continue;
576
+        }
577
+        if (!supprimer_repertoire($dir . '/' . $item)) {
578
+            @chmod($dir . '/' . $item, 0777);
579
+            if (!supprimer_repertoire($dir . '/' . $item)) {
580
+                return false;
581
+            }
582
+        };
583
+    }
584
+
585
+    return @rmdir($dir);
586 586
 }
587 587
 
588 588
 
@@ -611,58 +611,58 @@  discard block
 block discarded – undo
611 611
  *     Chemin du répertoire créé.
612 612
  **/
613 613
 function sous_repertoire($base, $subdir = '', $nobase = false, $tantpis = false): string {
614
-	static $dirs = [];
615
-
616
-	$base = str_replace('//', '/', $base);
617
-
618
-	# suppr le dernier caractere si c'est un /
619
-	$base = rtrim($base, '/');
620
-
621
-	if (!strlen($subdir)) {
622
-		$n = strrpos($base, '/');
623
-		if ($n === false) {
624
-			return $nobase ? '' : ($base . '/');
625
-		}
626
-		$subdir = substr($base, $n + 1);
627
-		$base = substr($base, 0, $n + 1);
628
-	} else {
629
-		$base .= '/';
630
-		$subdir = str_replace('/', '', $subdir);
631
-	}
632
-
633
-	$baseaff = $nobase ? '' : $base;
634
-	if (isset($dirs[$base . $subdir])) {
635
-		return $baseaff . $dirs[$base . $subdir];
636
-	}
637
-
638
-	$path = $base . $subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf'
639
-
640
-	if (file_exists("$path/.ok")) {
641
-		return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
642
-	}
643
-
644
-	@mkdir($path, _SPIP_CHMOD);
645
-	@chmod($path, _SPIP_CHMOD);
646
-
647
-	if (is_dir($path) && is_writable($path)) {
648
-		@touch("$path/.ok");
649
-		spip_log("creation $base$subdir/");
650
-
651
-		return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
652
-	}
653
-
654
-	// en cas d'echec c'est peut etre tout simplement que le disque est plein :
655
-	// l'inode du fichier dir_test existe, mais impossible d'y mettre du contenu
656
-	spip_log("echec creation $base{$subdir}");
657
-	if ($tantpis) {
658
-		return '';
659
-	}
660
-	// FIXME: throw an Exception…
661
-	if (!_DIR_RESTREINT) {
662
-		$base = preg_replace(',^' . _DIR_RACINE . ',', '', $base);
663
-	}
664
-	$base .= $subdir;
665
-	raler_fichier($base . '/.ok');
614
+    static $dirs = [];
615
+
616
+    $base = str_replace('//', '/', $base);
617
+
618
+    # suppr le dernier caractere si c'est un /
619
+    $base = rtrim($base, '/');
620
+
621
+    if (!strlen($subdir)) {
622
+        $n = strrpos($base, '/');
623
+        if ($n === false) {
624
+            return $nobase ? '' : ($base . '/');
625
+        }
626
+        $subdir = substr($base, $n + 1);
627
+        $base = substr($base, 0, $n + 1);
628
+    } else {
629
+        $base .= '/';
630
+        $subdir = str_replace('/', '', $subdir);
631
+    }
632
+
633
+    $baseaff = $nobase ? '' : $base;
634
+    if (isset($dirs[$base . $subdir])) {
635
+        return $baseaff . $dirs[$base . $subdir];
636
+    }
637
+
638
+    $path = $base . $subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf'
639
+
640
+    if (file_exists("$path/.ok")) {
641
+        return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
642
+    }
643
+
644
+    @mkdir($path, _SPIP_CHMOD);
645
+    @chmod($path, _SPIP_CHMOD);
646
+
647
+    if (is_dir($path) && is_writable($path)) {
648
+        @touch("$path/.ok");
649
+        spip_log("creation $base$subdir/");
650
+
651
+        return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
652
+    }
653
+
654
+    // en cas d'echec c'est peut etre tout simplement que le disque est plein :
655
+    // l'inode du fichier dir_test existe, mais impossible d'y mettre du contenu
656
+    spip_log("echec creation $base{$subdir}");
657
+    if ($tantpis) {
658
+        return '';
659
+    }
660
+    // FIXME: throw an Exception…
661
+    if (!_DIR_RESTREINT) {
662
+        $base = preg_replace(',^' . _DIR_RACINE . ',', '', $base);
663
+    }
664
+    $base .= $subdir;
665
+    raler_fichier($base . '/.ok');
666 666
 }
667 667
 
668 668
 
@@ -695,55 +695,55 @@  discard block
 block discarded – undo
695 695
  *     Chemins des fichiers trouvés.
696 696
  **/
697 697
 function preg_files($dir, $pattern = -1 /* AUTO */, $maxfiles = 10000, $recurs = []) {
698
-	$nbfiles = 0;
699
-	if ($pattern == -1) {
700
-		$pattern = '';
701
-	}
702
-	$fichiers = [];
703
-	// revenir au repertoire racine si on a recu dossier/truc
704
-	// pour regarder dossier/truc/ ne pas oublier le / final
705
-	$dir = preg_replace(',/[^/]*$,', '', $dir);
706
-	if ($dir == '') {
707
-		$dir = '.';
708
-	}
709
-
710
-	if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) {
711
-		while (($f = readdir($d)) !== false && ($nbfiles < $maxfiles)) {
712
-			if (
713
-				$f[0] != '.'
714
-				&& $f != 'remove.txt'
715
-				&& is_readable($f = "$dir/$f")
716
-			) {
717
-				if (is_file($f)) {
718
-					if (!$pattern || preg_match(";$pattern;iS", $f)) {
719
-						$fichiers[] = $f;
720
-						$nbfiles++;
721
-					}
722
-				} else {
723
-					if (is_dir($f) && is_array($recurs)) {
724
-						$rp = @realpath($f);
725
-						if (!is_string($rp) || !strlen($rp)) {
726
-							$rp = $f;
727
-						} # realpath n'est peut etre pas autorise
728
-						if (!isset($recurs[$rp])) {
729
-							$recurs[$rp] = true;
730
-							$beginning = $fichiers;
731
-							$end = preg_files(
732
-								"$f/",
733
-								$pattern,
734
-								$maxfiles - $nbfiles,
735
-								$recurs
736
-							);
737
-							$fichiers = array_merge((array)$beginning, (array)$end);
738
-							$nbfiles = count($fichiers);
739
-						}
740
-					}
741
-				}
742
-			}
743
-		}
744
-		closedir($d);
745
-	}
746
-	sort($fichiers);
747
-
748
-	return $fichiers;
698
+    $nbfiles = 0;
699
+    if ($pattern == -1) {
700
+        $pattern = '';
701
+    }
702
+    $fichiers = [];
703
+    // revenir au repertoire racine si on a recu dossier/truc
704
+    // pour regarder dossier/truc/ ne pas oublier le / final
705
+    $dir = preg_replace(',/[^/]*$,', '', $dir);
706
+    if ($dir == '') {
707
+        $dir = '.';
708
+    }
709
+
710
+    if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) {
711
+        while (($f = readdir($d)) !== false && ($nbfiles < $maxfiles)) {
712
+            if (
713
+                $f[0] != '.'
714
+                && $f != 'remove.txt'
715
+                && is_readable($f = "$dir/$f")
716
+            ) {
717
+                if (is_file($f)) {
718
+                    if (!$pattern || preg_match(";$pattern;iS", $f)) {
719
+                        $fichiers[] = $f;
720
+                        $nbfiles++;
721
+                    }
722
+                } else {
723
+                    if (is_dir($f) && is_array($recurs)) {
724
+                        $rp = @realpath($f);
725
+                        if (!is_string($rp) || !strlen($rp)) {
726
+                            $rp = $f;
727
+                        } # realpath n'est peut etre pas autorise
728
+                        if (!isset($recurs[$rp])) {
729
+                            $recurs[$rp] = true;
730
+                            $beginning = $fichiers;
731
+                            $end = preg_files(
732
+                                "$f/",
733
+                                $pattern,
734
+                                $maxfiles - $nbfiles,
735
+                                $recurs
736
+                            );
737
+                            $fichiers = array_merge((array)$beginning, (array)$end);
738
+                            $nbfiles = count($fichiers);
739
+                        }
740
+                    }
741
+                }
742
+            }
743
+        }
744
+        closedir($d);
745
+    }
746
+    sort($fichiers);
747
+
748
+    return $fichiers;
749 749
 }
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 		$contenu = @gzfile($fichier);
122 122
 	}
123 123
 
124
-	return is_array($contenu) ? implode('', $contenu) : (string)$contenu;
124
+	return is_array($contenu) ? implode('', $contenu) : (string) $contenu;
125 125
 }
126 126
 
127 127
 
@@ -321,9 +321,9 @@  discard block
 block discarded – undo
321 321
  */
322 322
 function ecrire_fichier_securise($fichier, $contenu, $ecrire_quand_meme = false, $truncate = true) {
323 323
 	if (!str_ends_with($fichier, '.php')) {
324
-		spip_log('Erreur de programmation: ' . $fichier . ' doit finir par .php');
324
+		spip_log('Erreur de programmation: '.$fichier.' doit finir par .php');
325 325
 	}
326
-	$contenu = '<' . "?php die ('Acces interdit'); ?" . ">\n" . $contenu;
326
+	$contenu = '<'."?php die ('Acces interdit'); ?".">\n".$contenu;
327 327
 
328 328
 	return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate);
329 329
 }
@@ -336,7 +336,7 @@  discard block
 block discarded – undo
336 336
  * @return bool
337 337
  */
338 338
 function ecrire_fichier_calcule_si_modifie($fichier, $contenu, $force = false, $use_copy = false) {
339
-	$fichier_tmp = $fichier . '.last';
339
+	$fichier_tmp = $fichier.'.last';
340 340
 	if (!ecrire_fichier($fichier_tmp, $contenu, true)) {
341 341
 		return false;
342 342
 	}
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
  */
377 377
 function lire_fichier_securise($fichier, &$contenu, $options = []) {
378 378
 	if ($res = lire_fichier($fichier, $contenu, $options)) {
379
-		$contenu = substr((string) $contenu, strlen('<?php die (\'Acces interdit\'); ?>' . "\n"));
379
+		$contenu = substr((string) $contenu, strlen('<?php die (\'Acces interdit\'); ?>'."\n"));
380 380
 	}
381 381
 
382 382
 	return $res;
@@ -546,7 +546,7 @@  discard block
 block discarded – undo
546 546
 				$wait = 0;
547 547
 			}
548 548
 		}
549
-		spip_log('Probleme de configuration opcache.revalidate_freq ' . $duree . 's : on attend ' . $wait . 's', _LOG_INFO_IMPORTANTE);
549
+		spip_log('Probleme de configuration opcache.revalidate_freq '.$duree.'s : on attend '.$wait.'s', _LOG_INFO_IMPORTANTE);
550 550
 		if ($wait) {
551 551
 			sleep($duree + 1);
552 552
 		}
@@ -574,9 +574,9 @@  discard block
 block discarded – undo
574 574
 		if ($item == '.' || $item == '..') {
575 575
 			continue;
576 576
 		}
577
-		if (!supprimer_repertoire($dir . '/' . $item)) {
578
-			@chmod($dir . '/' . $item, 0777);
579
-			if (!supprimer_repertoire($dir . '/' . $item)) {
577
+		if (!supprimer_repertoire($dir.'/'.$item)) {
578
+			@chmod($dir.'/'.$item, 0777);
579
+			if (!supprimer_repertoire($dir.'/'.$item)) {
580 580
 				return false;
581 581
 			}
582 582
 		};
@@ -621,7 +621,7 @@  discard block
 block discarded – undo
621 621
 	if (!strlen($subdir)) {
622 622
 		$n = strrpos($base, '/');
623 623
 		if ($n === false) {
624
-			return $nobase ? '' : ($base . '/');
624
+			return $nobase ? '' : ($base.'/');
625 625
 		}
626 626
 		$subdir = substr($base, $n + 1);
627 627
 		$base = substr($base, 0, $n + 1);
@@ -631,14 +631,14 @@  discard block
 block discarded – undo
631 631
 	}
632 632
 
633 633
 	$baseaff = $nobase ? '' : $base;
634
-	if (isset($dirs[$base . $subdir])) {
635
-		return $baseaff . $dirs[$base . $subdir];
634
+	if (isset($dirs[$base.$subdir])) {
635
+		return $baseaff.$dirs[$base.$subdir];
636 636
 	}
637 637
 
638
-	$path = $base . $subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf'
638
+	$path = $base.$subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf'
639 639
 
640 640
 	if (file_exists("$path/.ok")) {
641
-		return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
641
+		return $baseaff.($dirs[$base.$subdir] = "$subdir/");
642 642
 	}
643 643
 
644 644
 	@mkdir($path, _SPIP_CHMOD);
@@ -648,7 +648,7 @@  discard block
 block discarded – undo
648 648
 		@touch("$path/.ok");
649 649
 		spip_log("creation $base$subdir/");
650 650
 
651
-		return $baseaff . ($dirs[$base . $subdir] = "$subdir/");
651
+		return $baseaff.($dirs[$base.$subdir] = "$subdir/");
652 652
 	}
653 653
 
654 654
 	// en cas d'echec c'est peut etre tout simplement que le disque est plein :
@@ -659,10 +659,10 @@  discard block
 block discarded – undo
659 659
 	}
660 660
 	// FIXME: throw an Exception…
661 661
 	if (!_DIR_RESTREINT) {
662
-		$base = preg_replace(',^' . _DIR_RACINE . ',', '', $base);
662
+		$base = preg_replace(',^'._DIR_RACINE.',', '', $base);
663 663
 	}
664 664
 	$base .= $subdir;
665
-	raler_fichier($base . '/.ok');
665
+	raler_fichier($base.'/.ok');
666 666
 }
667 667
 
668 668
 
@@ -734,7 +734,7 @@  discard block
 block discarded – undo
734 734
 								$maxfiles - $nbfiles,
735 735
 								$recurs
736 736
 							);
737
-							$fichiers = array_merge((array)$beginning, (array)$end);
737
+							$fichiers = array_merge((array) $beginning, (array) $end);
738 738
 							$nbfiles = count($fichiers);
739 739
 						}
740 740
 					}
Please login to merge, or discard this patch.