~maj v3.0.19-->v3.0.21
[ptitvelo/web/www.git] / www / ecrire / inc / flock.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15 // ajouter define('_CREER_DIR_PLAT', true); dans mes_options pour restaurer
16 // le fonctionnement des faux repertoires en .plat
17 define('_CREER_DIR_PLAT', false);
18 if (!defined('_TEST_FILE_EXISTS')) define('_TEST_FILE_EXISTS', preg_match(',(online|free)[.]fr$,', isset($_ENV["HTTP_HOST"]) ? $_ENV["HTTP_HOST"] : ""));
19
20 #define('_SPIP_LOCK_MODE',0); // ne pas utiliser de lock (deconseille)
21 #define('_SPIP_LOCK_MODE',1); // utiliser le flock php
22 #define('_SPIP_LOCK_MODE',2); // utiliser le nfslock de spip
23
24 if (_SPIP_LOCK_MODE==2)
25 include_spip('inc/nfslock');
26
27 $GLOBALS['liste_verrous'] = array();
28 // http://doc.spip.org/@spip_fopen_lock
29 function spip_fopen_lock($fichier,$mode,$verrou){
30 if (_SPIP_LOCK_MODE==1){
31 if ($fl = @fopen($fichier,$mode))
32 // verrou
33 @flock($fl, $verrou);
34 return $fl;
35 }
36 elseif(_SPIP_LOCK_MODE==2) {
37 if (($verrou = spip_nfslock($fichier)) && ($fl = @fopen($fichier,$mode))){
38 $GLOBALS['liste_verrous'][$fl] = array($fichier,$verrou);
39 return $fl;
40 }
41 else return false;
42 }
43 return @fopen($fichier,$mode);
44 }
45 // http://doc.spip.org/@spip_fclose_unlock
46 function spip_fclose_unlock($handle){
47 if (_SPIP_LOCK_MODE==1){
48 @flock($handle, LOCK_UN);
49 }
50 elseif(_SPIP_LOCK_MODE==2) {
51 spip_nfsunlock(reset($GLOBALS['liste_verrous'][$handle]),end($GLOBALS['liste_verrous'][$handle]));
52 unset($GLOBALS['liste_verrous'][$handle]);
53 }
54 return @fclose($handle);
55 }
56
57
58 // http://doc.spip.org/@spip_file_get_contents
59 function spip_file_get_contents ($fichier) {
60 if (substr($fichier, -3) != '.gz') {
61 if (function_exists('file_get_contents')
62 AND (
63 // quand on est sous window on ne sait pas si file_get_contents marche
64 // on essaye : si ca retourne du contenu alors c'est bon
65 // sinon on fait un file() pour avoir le coeur net
66 ($contenu = @file_get_contents ($fichier))
67 OR _OS_SERVEUR != 'windows')
68 )
69 return $contenu;
70 else
71 $contenu = @file($fichier);
72 } else
73 $contenu = @gzfile($fichier);
74 return is_array($contenu)?join('', $contenu):(string)$contenu;
75 }
76
77 // options = array(
78 // 'phpcheck' => 'oui' # verifier qu'on a bien du php
79 // dezippe automatiquement les fichiers .gz
80 // http://doc.spip.org/@lire_fichier
81 function lire_fichier ($fichier, &$contenu, $options=false) {
82 $contenu = '';
83 // inutile car si le fichier n'existe pas, le lock va renvoyer false juste apres
84 // economisons donc les acces disque, sauf chez free qui rale pour un rien
85 if (_TEST_FILE_EXISTS AND !@file_exists($fichier))
86 return false;
87
88 #spip_timer('lire_fichier');
89
90 // pas de @ sur spip_fopen_lock qui est silencieux de toute facon
91 if ($fl = spip_fopen_lock($fichier, 'r', LOCK_SH)) {
92 // lire le fichier avant tout
93 $contenu = spip_file_get_contents($fichier);
94
95 // le fichier a-t-il ete supprime par le locker ?
96 // on ne verifie que si la tentative de lecture a echoue
97 // pour discriminer un contenu vide d'un fichier absent
98 // et eviter un acces disque
99 if (!$contenu AND !@file_exists($fichier)) {
100 spip_fclose_unlock($fl);
101 return false;
102 }
103
104 // liberer le verrou
105 spip_fclose_unlock($fl);
106
107 // Verifications
108 $ok = true;
109 if ($options['phpcheck'] == 'oui')
110 $ok &= (preg_match(",[?]>\n?$,", $contenu));
111
112 #spip_log("$fread $fichier ".spip_timer('lire_fichier'));
113 if (!$ok)
114 spip_log("echec lecture $fichier");
115
116 return $ok;
117 }
118 return false;
119 }
120
121 //
122 // Ecrire un fichier de maniere un peu sure
123 //
124 // zippe les fichiers .gz
125 // http://doc.spip.org/@ecrire_fichier
126 function ecrire_fichier ($fichier, $contenu, $ignorer_echec = false, $truncate=true) {
127
128 #spip_timer('ecrire_fichier');
129
130 // verrouiller le fichier destination
131 if ($fp = spip_fopen_lock($fichier, 'a',LOCK_EX)) {
132 // ecrire les donnees, compressees le cas echeant
133 // (on ouvre un nouveau pointeur sur le fichier, ce qui a l'avantage
134 // de le recreer si le locker qui nous precede l'avait supprime...)
135 if (substr($fichier, -3) == '.gz')
136 $contenu = gzencode($contenu);
137 // si c'est une ecriture avec troncation , on fait plutot une ecriture complete a cote suivie unlink+rename
138 // pour etre sur d'avoir une operation atomique
139 // y compris en NFS : http://www.ietf.org/rfc/rfc1094.txt
140 // sauf sous wintruc ou ca ne marche pas
141 $ok = false;
142 if ($truncate AND _OS_SERVEUR != 'windows'){
143 if (!function_exists('creer_uniqid'))
144 include_spip('inc/acces');
145 $id = creer_uniqid();
146 // on ouvre un pointeur sur un fichier temporaire en ecriture +raz
147 if ($fp2 = spip_fopen_lock("$fichier.$id", 'w',LOCK_EX)) {
148 $s = @fputs($fp2, $contenu, $a = strlen($contenu));
149 $ok = ($s == $a);
150 spip_fclose_unlock($fp2);
151 spip_fclose_unlock($fp);
152 // unlink direct et pas spip_unlink car on avait deja le verrou
153 // a priori pas besoin car rename ecrase la cible
154 // @unlink($fichier);
155 // le rename aussitot, atomique quand on est pas sous windows
156 // au pire on arrive en second en cas de concourance, et le rename echoue
157 // --> on a la version de l'autre process qui doit etre identique
158 @rename("$fichier.$id",$fichier);
159 // precaution en cas d'echec du rename
160 if (!_TEST_FILE_EXISTS OR @file_exists("$fichier.$id"))
161 @unlink("$fichier.$id");
162 if ($ok)
163 $ok = file_exists($fichier);
164 }
165 else
166 // echec mais penser a fermer ..
167 spip_fclose_unlock($fp);
168 }
169 // sinon ou si methode precedente a echoueee
170 // on se rabat sur la methode ancienne
171 if (!$ok){
172 // ici on est en ajout ou sous windows, cas desespere
173 if ($truncate)
174 @ftruncate($fp,0);
175 $s = @fputs($fp, $contenu, $a = strlen($contenu));
176
177 $ok = ($s == $a);
178 spip_fclose_unlock($fp);
179 }
180
181 // liberer le verrou et fermer le fichier
182 @chmod($fichier, _SPIP_CHMOD & 0666);
183 if ($ok) {
184 spip_clear_opcode_cache(realpath($fichier));
185 return $ok;
186 }
187 }
188
189 if (!$ignorer_echec){
190 include_spip('inc/autoriser');
191 if (autoriser('chargerftp'))
192 raler_fichier($fichier);
193 spip_unlink($fichier);
194 }
195 spip_log("Ecriture fichier $fichier impossible",_LOG_INFO_IMPORTANTE);
196 return false;
197 }
198
199 /**
200 * Ecrire un contenu dans un fichier encapsule en php pour en empecher l'acces en l'absence
201 * de htaccess
202 * @param string $fichier
203 * @param <type> $contenu
204 * @param <type> $ecrire_quand_meme
205 * @param <type> $truncate
206 */
207 function ecrire_fichier_securise ($fichier, $contenu, $ecrire_quand_meme = false, $truncate=true) {
208 if (substr($fichier,-4) !== '.php')
209 spip_log('Erreur de programmation: '.$fichier.' doit finir par .php');
210 $contenu = "<"."?php die ('Acces interdit'); ?".">\n" . $contenu;
211 return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate);
212 }
213
214 /**
215 * Lire un fichier encapsule en php
216 * @param <type> $fichier
217 * @param <type> $contenu
218 * @param <type> $options
219 */
220 function lire_fichier_securise ($fichier, &$contenu, $options=false) {
221 if ($res = lire_fichier($fichier,$contenu,$options)){
222 $contenu = substr($contenu,strlen("<"."?php die ('Acces interdit'); ?".">\n"));
223 }
224 return $res;
225 }
226
227 // http://doc.spip.org/@raler_fichier
228 function raler_fichier($fichier)
229 {
230 include_spip('inc/minipres');
231 $dir = dirname($fichier);
232 http_status(401);
233 echo minipres(_T('texte_inc_meta_2'), "<h4 style='color: red'>"
234 . _T('texte_inc_meta_1', array('fichier' => $fichier))
235 . " <a href='"
236 . generer_url_ecrire('install', "etape=chmod&test_dir=$dir")
237 . "'>"
238 . _T('texte_inc_meta_2')
239 . "</a> "
240 . _T('texte_inc_meta_3',
241 array('repertoire' => joli_repertoire($dir)))
242 . "</h4>\n");
243 exit;
244 }
245
246 //
247 // Retourne Vrai si son premier argument a ete cree il y a moins de N secondes
248 //
249
250 // http://doc.spip.org/@jeune_fichier
251 function jeune_fichier($fichier, $n)
252 {
253 if (!file_exists($fichier)) return false;
254 if (!$c = @filemtime($fichier)) return false;
255 return (time()-$n <= $c);
256 }
257
258 //
259 // Supprimer le fichier de maniere sympa (flock)
260 //
261 // http://doc.spip.org/@supprimer_fichier
262 function supprimer_fichier($fichier, $lock=true) {
263 if (!@file_exists($fichier))
264 return true;
265
266 if ($lock) {
267 // verrouiller le fichier destination
268 if (!$fp = spip_fopen_lock($fichier, 'a', LOCK_EX))
269 return false;
270
271 // liberer le verrou
272 spip_fclose_unlock($fp);
273 }
274
275 // supprimer
276 return @unlink($fichier);
277 }
278
279 // Supprimer brutalement, si le fichier existe
280 // http://doc.spip.org/@spip_unlink
281 function spip_unlink($f) {
282 if (!is_dir($f))
283 supprimer_fichier($f,false);
284 else {
285 @unlink("$f/.ok");
286 @rmdir($f);
287 }
288 }
289
290 /**
291 * clearstatcache adapte a la version PHP
292 * @param bool $clear_realpath_cache
293 * @param null $filename
294 */
295 function spip_clearstatcache($clear_realpath_cache = false, $filename=null){
296 if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300) {
297 // Below PHP 5.3, clearstatcache does not accept any function parameters.
298 return clearstatcache();
299 }
300 else {
301 return clearstatcache($clear_realpath_cache, $filename);
302 }
303
304 }
305
306
307 /**
308 * Invalidates a PHP file from any active opcode caches.
309 *
310 * If the opcode cache does not support the invalidation of individual files,
311 * the entire cache will be flushed.
312 * kudo : http://cgit.drupalcode.org/drupal/commit/?id=be97f50
313 *
314 * @param string $filepath
315 * The absolute path of the PHP file to invalidate.
316 */
317 function spip_clear_opcode_cache($filepath) {
318 spip_clearstatcache(TRUE, $filepath);
319
320 // Zend OPcache
321 if (function_exists('opcache_invalidate')) {
322 opcache_invalidate($filepath, TRUE);
323 }
324 // APC.
325 if (function_exists('apc_delete_file')) {
326 // apc_delete_file() throws a PHP warning in case the specified file was
327 // not compiled yet.
328 // @see http://php.net/apc-delete-file
329 @apc_delete_file($filepath);
330 }
331 }
332
333 /**
334 * si opcache est actif et en mode validate_timestamps
335 * le timestamp ne sera checke qu'apres revalidate_freq s
336 * il faut donc attendre ce temps la pour etre sur qu'on va bien beneficier de la recompilation
337 * NB c'est une config foireuse deconseillee de opcode cache mais malheureusement utilisee par Octave
338 * cf http://stackoverflow.com/questions/25649416/when-exactly-does-php-5-5-opcache-check-file-timestamp-based-on-revalidate-freq
339 * et http://wiki.mikejung.biz/PHP_OPcache
340 *
341 * Ne fait rien en dehors de ce cas
342 *
343 */
344 function spip_attend_invalidation_opcode_cache(){
345 if (function_exists('opcache_get_configuration')
346 AND @ini_get('opcache.enable')
347 AND @ini_get('opcache.validate_timestamps')
348 AND $duree = @ini_get('opcache.revalidate_freq') ) {
349 sleep($duree+1);
350 }
351 }
352
353 /**
354 * Suppression complete d'un repertoire.
355 *
356 * http://www.php.net/manual/en/function.rmdir.php#92050
357 *
358 * @param string $dir Chemin du repertoire
359 * @return bool Suppression reussie.
360 */
361 function supprimer_repertoire($dir) {
362 if (!file_exists($dir)) return true;
363 if (!is_dir($dir) || is_link($dir)) return @unlink($dir);
364
365 foreach (scandir($dir) as $item) {
366 if ($item == '.' || $item == '..') continue;
367 if (!supprimer_repertoire($dir . "/" . $item)) {
368 @chmod($dir . "/" . $item, 0777);
369 if (!supprimer_repertoire($dir . "/" . $item)) return false;
370 };
371 }
372
373 return @rmdir($dir);
374 }
375
376
377 //
378 // Retourne $base/${subdir}/ si le sous-repertoire peut etre cree,
379 // $base/${subdir}_ sinon ; $nobase signale qu'on ne veut pas de $base/
380 // On peut aussi ne donner qu'un seul argument,
381 // subdir valant alors ce qui suit le dernier / dans $base
382 //
383 // http://doc.spip.org/@sous_repertoire
384 function sous_repertoire($base, $subdir='', $nobase = false, $tantpis=false) {
385 static $dirs = array();
386
387 $base = str_replace("//", "/", $base);
388
389 # suppr le dernier caractere si c'est un / ou un _
390 $base = rtrim($base, '/_');
391
392 if (!strlen($subdir)) {
393 $n = strrpos($base, "/");
394 if ($n === false) return $nobase ? '' : ($base .'/');
395 $subdir = substr($base, $n+1);
396 $base = substr($base, 0, $n+1);
397 } else {
398 $base .= '/';
399 $subdir = str_replace("/", "", $subdir);
400 }
401
402 $baseaff = $nobase ? '' : $base;
403 if (isset($dirs[$base.$subdir]))
404 return $baseaff.$dirs[$base.$subdir];
405
406
407 if (_CREER_DIR_PLAT AND @file_exists("$base${subdir}.plat"))
408 return $baseaff.($dirs[$base.$subdir] = "${subdir}_");
409
410 $path = $base.$subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf'
411
412 if (file_exists("$path/.ok"))
413 return $baseaff.($dirs[$base.$subdir] = "$subdir/");
414
415 @mkdir($path, _SPIP_CHMOD);
416 @chmod($path, _SPIP_CHMOD);
417
418 $ok = false;
419 if ($test = @fopen("$path/dir_test.php", "w")) {
420 @fputs($test, '<'.'?php $ok = true; ?'.'>');
421 @fclose($test);
422 @include("$path/dir_test.php");
423 spip_unlink("$path/dir_test.php");
424 }
425 if ($ok) {
426 @touch ("$path/.ok");
427 spip_log("creation $base$subdir/");
428 return $baseaff.($dirs[$base.$subdir] = "$subdir/");
429 }
430
431 // en cas d'echec c'est peut etre tout simplement que le disque est plein :
432 // l'inode du fichier dir_test existe, mais impossible d'y mettre du contenu
433 // => sauf besoin express (define dans mes_options), ne pas creer le .plat
434 if (_CREER_DIR_PLAT
435 AND $f = @fopen("$base${subdir}.plat", "w"))
436 fclose($f);
437 else {
438 spip_log("echec creation $base${subdir}");
439 if ($tantpis) return '';
440 if (!_DIR_RESTREINT)
441 $base = preg_replace(',^' . _DIR_RACINE .',', '',$base);
442 $base .= $subdir;
443 raler_fichier($base . ($test?'/.ok':'/dir_test.php'));
444 }
445 spip_log("faux sous-repertoire $base${subdir}");
446 return $baseaff.($dirs[$base.$subdir] = "${subdir}_");
447 }
448
449 //
450 // Cette fonction parcourt recursivement le repertoire $dir, et renvoie les
451 // fichiers dont le chemin verifie le pattern (preg) donne en argument.
452 // En cas d'echec retourne un array() vide
453 //
454 // Usage: array preg_files('ecrire/data/', '[.]lock$');
455 //
456 // Attention, afin de conserver la compatibilite avec les repertoires '.plat'
457 // si $dir = 'rep/sous_rep_' au lieu de 'rep/sous_rep/' on scanne 'rep/' et on
458 // applique un pattern '^rep/sous_rep_'
459 // si $recurs vaut false, la fonction ne descend pas dans les sus repertoires
460 //
461 // http://doc.spip.org/@preg_files
462 function preg_files($dir, $pattern=-1 /* AUTO */, $maxfiles = 10000, $recurs=array()) {
463 $nbfiles = 0;
464 if ($pattern == -1)
465 $pattern = "^$dir";
466 $fichiers = array();
467 // revenir au repertoire racine si on a recu dossier/truc
468 // pour regarder dossier/truc/ ne pas oublier le / final
469 $dir = preg_replace(',/[^/]*$,', '', $dir);
470 if ($dir == '') $dir = '.';
471
472 if (@is_dir($dir) AND is_readable($dir) AND $d = @opendir($dir)) {
473 while (($f = readdir($d)) !== false && ($nbfiles<$maxfiles)) {
474 if ($f[0] != '.' # ignorer . .. .svn etc
475 AND $f != 'CVS'
476 AND $f != 'remove.txt'
477 AND is_readable($f = "$dir/$f")) {
478 if (is_file($f)) {
479 if (preg_match(";$pattern;iS", $f))
480 {
481 $fichiers[] = $f;
482 $nbfiles++;
483 }
484 }
485 else if (is_dir($f) AND is_array($recurs)){
486 $rp = @realpath($f);
487 if (!is_string($rp) OR !strlen($rp)) $rp=$f; # realpath n'est peut etre pas autorise
488 if (!isset($recurs[$rp])) {
489 $recurs[$rp] = true;
490 $beginning = $fichiers;
491 $end = preg_files("$f/", $pattern,
492 $maxfiles-$nbfiles, $recurs);
493 $fichiers = array_merge((array)$beginning, (array)$end);
494 $nbfiles = count($fichiers);
495 }
496 }
497 }
498 }
499 closedir($d);
500 }
501 sort($fichiers);
502 return $fichiers;
503 }
504
505 ?>