Completed
Push — master ( 1f4ad4...6aa332 )
by cam
06:01 queued 20s
created
ecrire/public/cacher.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -41,7 +41,7 @@
 block discarded – undo
41 41
  * Attention a modifier simultanement le sanity check de
42 42
  * la fonction retire_cache() de inc/invalideur
43 43
  *
44
- * @param $nom_cache
44
+ * @param string $nom_cache
45 45
  * @return string
46 46
  */
47 47
 function cache_chemin_fichier($nom_cache, $ecrire = false) {
Please login to merge, or discard this patch.
Indentation   +304 added lines, -304 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
 \***************************************************************************/
12 12
 
13 13
 if (!defined('_ECRIRE_INC_VERSION')) {
14
-	return;
14
+    return;
15 15
 }
16 16
 
17 17
 /**
@@ -27,9 +27,9 @@  discard block
 block discarded – undo
27 27
  * @return string
28 28
  */
29 29
 function generer_nom_fichier_cache($contexte, $page) {
30
-	$u = md5(var_export(array($contexte, $page), true));
30
+    $u = md5(var_export(array($contexte, $page), true));
31 31
 
32
-	return $u . ".cache";
32
+    return $u . ".cache";
33 33
 }
34 34
 
35 35
 /**
@@ -45,26 +45,26 @@  discard block
 block discarded – undo
45 45
  * @return string
46 46
  */
47 47
 function cache_chemin_fichier($nom_cache, $ecrire = false) {
48
-	static $l1, $l2;
49
-	if (is_null($l1)) {
50
-		$length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8,max(_CACHE_PROFONDEUR_STOCKAGE,2)) : 4);
51
-		$l1 = intval(floor($length / 2));
52
-		$l2 = $length - $l1;
53
-	}
54
-	$d = substr($nom_cache, 0, $l1);
55
-	$u = substr($nom_cache, $l1, $l2);
56
-
57
-	if ($ecrire) {
58
-		$rep = sous_repertoire(_DIR_CACHE, '', false, true);
59
-		$rep = sous_repertoire($rep, 'calcul/', false, true);
60
-		$rep = sous_repertoire($rep, $d, false, true);
61
-	}
62
-	else {
63
-		// en lecture on essaye pa de creer les repertoires, on va au plus vite
64
-		$rep = _DIR_CACHE . "$d/";
65
-	}
66
-
67
-	return $rep . $u . ".cache";
48
+    static $l1, $l2;
49
+    if (is_null($l1)) {
50
+        $length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8,max(_CACHE_PROFONDEUR_STOCKAGE,2)) : 4);
51
+        $l1 = intval(floor($length / 2));
52
+        $l2 = $length - $l1;
53
+    }
54
+    $d = substr($nom_cache, 0, $l1);
55
+    $u = substr($nom_cache, $l1, $l2);
56
+
57
+    if ($ecrire) {
58
+        $rep = sous_repertoire(_DIR_CACHE, '', false, true);
59
+        $rep = sous_repertoire($rep, 'calcul/', false, true);
60
+        $rep = sous_repertoire($rep, $d, false, true);
61
+    }
62
+    else {
63
+        // en lecture on essaye pa de creer les repertoires, on va au plus vite
64
+        $rep = _DIR_CACHE . "$d/";
65
+    }
66
+
67
+    return $rep . $u . ".cache";
68 68
 }
69 69
 
70 70
 /**
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
  * @return bool
76 76
  */
77 77
 function ecrire_cache($nom_cache, $valeur) {
78
-	return ecrire_fichier(cache_chemin_fichier($nom_cache, true), serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
78
+    return ecrire_fichier(cache_chemin_fichier($nom_cache, true), serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
79 79
 }
80 80
 
81 81
 /**
@@ -85,33 +85,33 @@  discard block
 block discarded – undo
85 85
  * @return mixed
86 86
  */
87 87
 function lire_cache($nom_cache) {
88
-	if (file_exists($f = cache_chemin_fichier($nom_cache))
89
-		and lire_fichier($f, $tmp)
90
-		and $tmp = unserialize($tmp)
91
-		and $tmp['nom_cache'] == $nom_cache
92
-		and isset($tmp['valeur'])
93
-	) {
94
-		return $tmp['valeur'];
95
-	}
96
-
97
-	return false;
88
+    if (file_exists($f = cache_chemin_fichier($nom_cache))
89
+        and lire_fichier($f, $tmp)
90
+        and $tmp = unserialize($tmp)
91
+        and $tmp['nom_cache'] == $nom_cache
92
+        and isset($tmp['valeur'])
93
+    ) {
94
+        return $tmp['valeur'];
95
+    }
96
+
97
+    return false;
98 98
 }
99 99
 
100 100
 // Parano : on signe le cache, afin d'interdire un hack d'injection
101 101
 // dans notre memcache
102 102
 function cache_signature(&$page) {
103
-	if (!isset($GLOBALS['meta']['cache_signature'])) {
104
-		include_spip('inc/acces');
105
-		include_spip('auth/sha256.inc');
106
-		ecrire_meta('cache_signature',
107
-			spip_sha256(
108
-				$_SERVER["DOCUMENT_ROOT"] 
109
-				. (isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER["SERVER_SIGNATURE"] : "")
110
-				. creer_uniqid()
111
-			), 'non');
112
-	}
113
-
114
-	return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
103
+    if (!isset($GLOBALS['meta']['cache_signature'])) {
104
+        include_spip('inc/acces');
105
+        include_spip('auth/sha256.inc');
106
+        ecrire_meta('cache_signature',
107
+            spip_sha256(
108
+                $_SERVER["DOCUMENT_ROOT"] 
109
+                . (isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER["SERVER_SIGNATURE"] : "")
110
+                . creer_uniqid()
111
+            ), 'non');
112
+    }
113
+
114
+    return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
115 115
 }
116 116
 
117 117
 /**
@@ -125,14 +125,14 @@  discard block
 block discarded – undo
125 125
  * @return array
126 126
  */
127 127
 function gzip_page($page) {
128
-	if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) {
129
-		$page['gz'] = true;
130
-		$page['texte'] = gzcompress($page['texte']);
131
-	} else {
132
-		$page['gz'] = false;
133
-	}
134
-
135
-	return $page;
128
+    if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) {
129
+        $page['gz'] = true;
130
+        $page['texte'] = gzcompress($page['texte']);
131
+    } else {
132
+        $page['gz'] = false;
133
+    }
134
+
135
+    return $page;
136 136
 }
137 137
 
138 138
 /**
@@ -147,10 +147,10 @@  discard block
 block discarded – undo
147 147
  * @return void
148 148
  */
149 149
 function gunzip_page(&$page) {
150
-	if ($page['gz']) {
151
-		$page['texte'] = gzuncompress($page['texte']);
152
-		$page['gz'] = false; // ne pas gzuncompress deux fois une meme page
153
-	}
150
+    if ($page['gz']) {
151
+        $page['texte'] = gzuncompress($page['texte']);
152
+        $page['gz'] = false; // ne pas gzuncompress deux fois une meme page
153
+    }
154 154
 }
155 155
 
156 156
 /**
@@ -166,72 +166,72 @@  discard block
 block discarded – undo
166 166
  */
167 167
 /// https://code.spip.net/@cache_valide
168 168
 function cache_valide(&$page, $date) {
169
-	$now = $_SERVER['REQUEST_TIME'];
170
-
171
-	// Apparition d'un nouvel article post-date ?
172
-	if (isset($GLOBALS['meta']['post_dates'])
173
-		and $GLOBALS['meta']['post_dates'] == 'non'
174
-		and isset($GLOBALS['meta']['date_prochain_postdate'])
175
-		and $now > $GLOBALS['meta']['date_prochain_postdate']
176
-	) {
177
-		spip_log('Un article post-date invalide le cache');
178
-		include_spip('inc/rubriques');
179
-		calculer_prochain_postdate(true);
180
-	}
181
-
182
-	if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) {
183
-		return -1;
184
-	}
185
-	if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) {
186
-		return -1;
187
-	}
188
-	if (defined('_NO_CACHE')) {
189
-		return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE;
190
-	}
191
-
192
-	// pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache)
193
-	if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) {
194
-		return _IS_BOT ? -1 : 1;
195
-	}
196
-
197
-	// controle de la signature
198
-	if ($page['sig'] !== cache_signature($page)) {
199
-		return _IS_BOT ? -1 : 1;
200
-	}
201
-
202
-	// #CACHE{n,statique} => on n'invalide pas avec derniere_modif
203
-	// cf. ecrire/public/balises.php, balise_CACHE_dist()
204
-	if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') {
205
-
206
-		// Cache invalide par la meta 'derniere_modif'
207
-		// sauf pour les bots, qui utilisent toujours le cache
208
-		if (!_IS_BOT
209
-			and $GLOBALS['derniere_modif_invalide']
210
-			and isset($GLOBALS['meta']['derniere_modif'])
211
-			and $date < $GLOBALS['meta']['derniere_modif']
212
-		) {
213
-			return 1;
214
-		}
215
-
216
-	}
217
-
218
-	// Sinon comparer l'age du fichier a sa duree de cache
219
-	$duree = intval($page['entetes']['X-Spip-Cache']);
220
-	$cache_mark = (isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0);
221
-	if ($duree == 0)  #CACHE{0}
222
-	{
223
-		return -1;
224
-	} // sauf pour les bots, qui utilisent toujours le cache
225
-	else {
226
-		if ((!_IS_BOT and $date + $duree < $now)
227
-			# le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots
228
-			or $date < $cache_mark
229
-		) {
230
-			return _IS_BOT ? -1 : 1;
231
-		} else {
232
-			return 0;
233
-		}
234
-	}
169
+    $now = $_SERVER['REQUEST_TIME'];
170
+
171
+    // Apparition d'un nouvel article post-date ?
172
+    if (isset($GLOBALS['meta']['post_dates'])
173
+        and $GLOBALS['meta']['post_dates'] == 'non'
174
+        and isset($GLOBALS['meta']['date_prochain_postdate'])
175
+        and $now > $GLOBALS['meta']['date_prochain_postdate']
176
+    ) {
177
+        spip_log('Un article post-date invalide le cache');
178
+        include_spip('inc/rubriques');
179
+        calculer_prochain_postdate(true);
180
+    }
181
+
182
+    if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) {
183
+        return -1;
184
+    }
185
+    if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) {
186
+        return -1;
187
+    }
188
+    if (defined('_NO_CACHE')) {
189
+        return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE;
190
+    }
191
+
192
+    // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache)
193
+    if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) {
194
+        return _IS_BOT ? -1 : 1;
195
+    }
196
+
197
+    // controle de la signature
198
+    if ($page['sig'] !== cache_signature($page)) {
199
+        return _IS_BOT ? -1 : 1;
200
+    }
201
+
202
+    // #CACHE{n,statique} => on n'invalide pas avec derniere_modif
203
+    // cf. ecrire/public/balises.php, balise_CACHE_dist()
204
+    if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') {
205
+
206
+        // Cache invalide par la meta 'derniere_modif'
207
+        // sauf pour les bots, qui utilisent toujours le cache
208
+        if (!_IS_BOT
209
+            and $GLOBALS['derniere_modif_invalide']
210
+            and isset($GLOBALS['meta']['derniere_modif'])
211
+            and $date < $GLOBALS['meta']['derniere_modif']
212
+        ) {
213
+            return 1;
214
+        }
215
+
216
+    }
217
+
218
+    // Sinon comparer l'age du fichier a sa duree de cache
219
+    $duree = intval($page['entetes']['X-Spip-Cache']);
220
+    $cache_mark = (isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0);
221
+    if ($duree == 0)  #CACHE{0}
222
+    {
223
+        return -1;
224
+    } // sauf pour les bots, qui utilisent toujours le cache
225
+    else {
226
+        if ((!_IS_BOT and $date + $duree < $now)
227
+            # le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots
228
+            or $date < $cache_mark
229
+        ) {
230
+            return _IS_BOT ? -1 : 1;
231
+        } else {
232
+            return 0;
233
+        }
234
+    }
235 235
 }
236 236
 
237 237
 /**
@@ -246,55 +246,55 @@  discard block
 block discarded – undo
246 246
  */
247 247
 function creer_cache(&$page, &$chemin_cache) {
248 248
 
249
-	// Ne rien faire si on est en preview, debug, ou si une erreur
250
-	// grave s'est presentee (compilation du squelette, MySQL, etc)
251
-	// le cas var_nocache ne devrait jamais arriver ici (securite)
252
-	// le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable
253
-	if ((defined('_VAR_NOCACHE') and _VAR_NOCACHE)
254
-		or defined('spip_interdire_cache')
255
-	) {
256
-		return;
257
-	}
258
-
259
-	// Si la page c1234 a un invalideur de session 'zz', sauver dans
260
-	// 'tmp/cache/MD5(chemin_cache)_zz'
261
-	if (isset($page['invalideurs'])
262
-		and isset($page['invalideurs']['session'])
263
-	) {
264
-		// on verifie que le contenu du chemin cache indique seulement
265
-		// "cache sessionne" ; sa date indique la date de validite
266
-		// des caches sessionnes
267
-		if (!$tmp = lire_cache($chemin_cache)) {
268
-			spip_log('Creation cache sessionne ' . $chemin_cache);
269
-			$tmp = array(
270
-				'invalideurs' => array('session' => ''),
271
-				'lastmodified' => $_SERVER['REQUEST_TIME']
272
-			);
273
-			ecrire_cache($chemin_cache, $tmp);
274
-		}
275
-		$chemin_cache = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
276
-			array("session" => $page['invalideurs']['session']));
277
-	}
278
-
279
-	// ajouter la date de production dans le cache lui meme
280
-	// (qui contient deja sa duree de validite)
281
-	$page['lastmodified'] = $_SERVER['REQUEST_TIME'];
282
-
283
-	// compresser le contenu si besoin
284
-	$pagez = gzip_page($page);
285
-
286
-	// signer le contenu
287
-	$pagez['sig'] = cache_signature($pagez);
288
-
289
-	// l'enregistrer, compresse ou non...
290
-	$ok = ecrire_cache($chemin_cache, $pagez);
291
-
292
-	spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
293
-		. $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
294
-
295
-	// Inserer ses invalideurs
296
-	include_spip('inc/invalideur');
297
-	maj_invalideurs($chemin_cache, $page);
249
+    // Ne rien faire si on est en preview, debug, ou si une erreur
250
+    // grave s'est presentee (compilation du squelette, MySQL, etc)
251
+    // le cas var_nocache ne devrait jamais arriver ici (securite)
252
+    // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable
253
+    if ((defined('_VAR_NOCACHE') and _VAR_NOCACHE)
254
+        or defined('spip_interdire_cache')
255
+    ) {
256
+        return;
257
+    }
258
+
259
+    // Si la page c1234 a un invalideur de session 'zz', sauver dans
260
+    // 'tmp/cache/MD5(chemin_cache)_zz'
261
+    if (isset($page['invalideurs'])
262
+        and isset($page['invalideurs']['session'])
263
+    ) {
264
+        // on verifie que le contenu du chemin cache indique seulement
265
+        // "cache sessionne" ; sa date indique la date de validite
266
+        // des caches sessionnes
267
+        if (!$tmp = lire_cache($chemin_cache)) {
268
+            spip_log('Creation cache sessionne ' . $chemin_cache);
269
+            $tmp = array(
270
+                'invalideurs' => array('session' => ''),
271
+                'lastmodified' => $_SERVER['REQUEST_TIME']
272
+            );
273
+            ecrire_cache($chemin_cache, $tmp);
274
+        }
275
+        $chemin_cache = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
276
+            array("session" => $page['invalideurs']['session']));
277
+    }
278
+
279
+    // ajouter la date de production dans le cache lui meme
280
+    // (qui contient deja sa duree de validite)
281
+    $page['lastmodified'] = $_SERVER['REQUEST_TIME'];
282
+
283
+    // compresser le contenu si besoin
284
+    $pagez = gzip_page($page);
285
+
286
+    // signer le contenu
287
+    $pagez['sig'] = cache_signature($pagez);
288
+
289
+    // l'enregistrer, compresse ou non...
290
+    $ok = ecrire_cache($chemin_cache, $pagez);
291
+
292
+    spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
293
+        . $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
294
+
295
+    // Inserer ses invalideurs
296
+    include_spip('inc/invalideur');
297
+    maj_invalideurs($chemin_cache, $page);
298 298
 
299 299
 }
300 300
 
@@ -310,15 +310,15 @@  discard block
 block discarded – undo
310 310
  * @return void
311 311
  */
312 312
 function nettoyer_petit_cache($prefix, $duree = 300) {
313
-	// determiner le repertoire a purger : 'tmp/CACHE/rech/'
314
-	$dircache = sous_repertoire(_DIR_CACHE, $prefix);
315
-	if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
316
-		foreach (preg_files($dircache, '[.]txt$') as $f) {
317
-			if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
318
-				spip_unlink($f);
319
-			}
320
-		}
321
-	}
313
+    // determiner le repertoire a purger : 'tmp/CACHE/rech/'
314
+    $dircache = sous_repertoire(_DIR_CACHE, $prefix);
315
+    if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
316
+        foreach (preg_files($dircache, '[.]txt$') as $f) {
317
+            if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
318
+                spip_unlink($f);
319
+            }
320
+        }
321
+    }
322 322
 }
323 323
 
324 324
 
@@ -348,126 +348,126 @@  discard block
 block discarded – undo
348 348
  */
349 349
 function public_cacher_dist($contexte, &$use_cache, &$chemin_cache, &$page, &$lastmodified) {
350 350
 
351
-	# fonction de cache minimale : dire "non on ne met rien en cache"
352
-	# $use_cache = -1; return;
353
-
354
-	// Second appel, destine a l'enregistrement du cache sur le disque
355
-	if (isset($chemin_cache)) {
356
-		return creer_cache($page, $chemin_cache);
357
-	}
358
-
359
-	// Toute la suite correspond au premier appel
360
-	$contexte_implicite = $page['contexte_implicite'];
361
-
362
-	// Cas ignorant le cache car completement dynamique
363
-	if ($_SERVER['REQUEST_METHOD'] == 'POST'
364
-		or _request('connect')
365
-	) {
366
-		$use_cache = -1;
367
-		$lastmodified = 0;
368
-		$chemin_cache = "";
369
-		$page = array();
370
-
371
-		return;
372
-	}
373
-
374
-	// Controler l'existence d'un cache nous correspondant
375
-	$chemin_cache = generer_nom_fichier_cache($contexte, $page);
376
-	$lastmodified = 0;
377
-
378
-	// charger le cache s'il existe (et si il a bien le bon hash = anticollision)
379
-	if (!$page = lire_cache($chemin_cache)) {
380
-		$page = array();
381
-	}
382
-
383
-	// s'il est sessionne, charger celui correspondant a notre session
384
-	if (isset($page['invalideurs'])
385
-		and isset($page['invalideurs']['session'])
386
-	) {
387
-		$chemin_cache_session = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
388
-			array("session" => spip_session()));
389
-		if ($page_session = lire_cache($chemin_cache_session)
390
-			and $page_session['lastmodified'] >= $page['lastmodified']
391
-		) {
392
-			$page = $page_session;
393
-		} else {
394
-			$page = array();
395
-		}
396
-	}
397
-
398
-
399
-	// Faut-il effacer des pages invalidees (en particulier ce cache-ci) ?
400
-	if (isset($GLOBALS['meta']['invalider'])) {
401
-		// ne le faire que si la base est disponible
402
-		if (spip_connect()) {
403
-			include_spip('inc/invalideur');
404
-			retire_caches($chemin_cache); # API invalideur inutile
405
-			supprimer_fichier(_DIR_CACHE . $chemin_cache);
406
-			if (isset($chemin_cache_session) and $chemin_cache_session) {
407
-				supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
408
-			}
409
-		}
410
-	}
411
-
412
-	// Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande,
413
-	// on supprime le cache
414
-	if (defined('_VAR_MODE') && _VAR_MODE &&
415
-		(isset($_COOKIE['spip_session'])
416
-			|| isset($_COOKIE['spip_admin'])
417
-			|| @file_exists(_ACCESS_FILE_NAME))
418
-	) {
419
-		$page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
420
-		include_spip('inc/invalideur');
421
-		retire_caches($chemin_cache); # API invalideur inutile
422
-		supprimer_fichier(_DIR_CACHE . $chemin_cache);
423
-		if (isset($chemin_cache_session) and $chemin_cache_session) {
424
-			supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
425
-		}
426
-	}
427
-
428
-	// $delais par defaut
429
-	// pour toutes les pages sans #CACHE{} hors modeles/ et espace privé
430
-	// qui sont a cache nul par defaut
431
-	if (!isset($GLOBALS['delais'])) {
432
-		if (!defined('_DUREE_CACHE_DEFAUT')) {
433
-			define('_DUREE_CACHE_DEFAUT', 24 * 3600);
434
-		}
435
-		$GLOBALS['delais'] = _DUREE_CACHE_DEFAUT;
436
-	}
437
-
438
-	// determiner la validite de la page
439
-	if ($page) {
440
-		$use_cache = cache_valide($page, isset($page['lastmodified']) ? $page['lastmodified'] : 0);
441
-		// le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence
442
-		// par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul
443
-		$page['contexte_implicite'] = $contexte_implicite;
444
-		if (!$use_cache) {
445
-			// $page est un cache utilisable
446
-			gunzip_page($page);
447
-
448
-			return;
449
-		}
450
-	} else {
451
-		$page = array('contexte_implicite' => $contexte_implicite);
452
-		$use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul
453
-	}
454
-
455
-	// Si pas valide mais pas de connexion a la base, le garder quand meme
456
-	if (!spip_connect()) {
457
-		if (isset($page['texte'])) {
458
-			gunzip_page($page);
459
-			$use_cache = 0;
460
-		} else {
461
-			spip_log("Erreur base de donnees, impossible utiliser $chemin_cache");
462
-			include_spip('inc/minipres');
463
-
464
-			return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), array('status' => 503));
465
-		}
466
-	}
467
-
468
-	if ($use_cache < 0) {
469
-		$chemin_cache = '';
470
-	}
471
-
472
-	return;
351
+    # fonction de cache minimale : dire "non on ne met rien en cache"
352
+    # $use_cache = -1; return;
353
+
354
+    // Second appel, destine a l'enregistrement du cache sur le disque
355
+    if (isset($chemin_cache)) {
356
+        return creer_cache($page, $chemin_cache);
357
+    }
358
+
359
+    // Toute la suite correspond au premier appel
360
+    $contexte_implicite = $page['contexte_implicite'];
361
+
362
+    // Cas ignorant le cache car completement dynamique
363
+    if ($_SERVER['REQUEST_METHOD'] == 'POST'
364
+        or _request('connect')
365
+    ) {
366
+        $use_cache = -1;
367
+        $lastmodified = 0;
368
+        $chemin_cache = "";
369
+        $page = array();
370
+
371
+        return;
372
+    }
373
+
374
+    // Controler l'existence d'un cache nous correspondant
375
+    $chemin_cache = generer_nom_fichier_cache($contexte, $page);
376
+    $lastmodified = 0;
377
+
378
+    // charger le cache s'il existe (et si il a bien le bon hash = anticollision)
379
+    if (!$page = lire_cache($chemin_cache)) {
380
+        $page = array();
381
+    }
382
+
383
+    // s'il est sessionne, charger celui correspondant a notre session
384
+    if (isset($page['invalideurs'])
385
+        and isset($page['invalideurs']['session'])
386
+    ) {
387
+        $chemin_cache_session = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
388
+            array("session" => spip_session()));
389
+        if ($page_session = lire_cache($chemin_cache_session)
390
+            and $page_session['lastmodified'] >= $page['lastmodified']
391
+        ) {
392
+            $page = $page_session;
393
+        } else {
394
+            $page = array();
395
+        }
396
+    }
397
+
398
+
399
+    // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ?
400
+    if (isset($GLOBALS['meta']['invalider'])) {
401
+        // ne le faire que si la base est disponible
402
+        if (spip_connect()) {
403
+            include_spip('inc/invalideur');
404
+            retire_caches($chemin_cache); # API invalideur inutile
405
+            supprimer_fichier(_DIR_CACHE . $chemin_cache);
406
+            if (isset($chemin_cache_session) and $chemin_cache_session) {
407
+                supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
408
+            }
409
+        }
410
+    }
411
+
412
+    // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande,
413
+    // on supprime le cache
414
+    if (defined('_VAR_MODE') && _VAR_MODE &&
415
+        (isset($_COOKIE['spip_session'])
416
+            || isset($_COOKIE['spip_admin'])
417
+            || @file_exists(_ACCESS_FILE_NAME))
418
+    ) {
419
+        $page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
420
+        include_spip('inc/invalideur');
421
+        retire_caches($chemin_cache); # API invalideur inutile
422
+        supprimer_fichier(_DIR_CACHE . $chemin_cache);
423
+        if (isset($chemin_cache_session) and $chemin_cache_session) {
424
+            supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
425
+        }
426
+    }
427
+
428
+    // $delais par defaut
429
+    // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé
430
+    // qui sont a cache nul par defaut
431
+    if (!isset($GLOBALS['delais'])) {
432
+        if (!defined('_DUREE_CACHE_DEFAUT')) {
433
+            define('_DUREE_CACHE_DEFAUT', 24 * 3600);
434
+        }
435
+        $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT;
436
+    }
437
+
438
+    // determiner la validite de la page
439
+    if ($page) {
440
+        $use_cache = cache_valide($page, isset($page['lastmodified']) ? $page['lastmodified'] : 0);
441
+        // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence
442
+        // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul
443
+        $page['contexte_implicite'] = $contexte_implicite;
444
+        if (!$use_cache) {
445
+            // $page est un cache utilisable
446
+            gunzip_page($page);
447
+
448
+            return;
449
+        }
450
+    } else {
451
+        $page = array('contexte_implicite' => $contexte_implicite);
452
+        $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul
453
+    }
454
+
455
+    // Si pas valide mais pas de connexion a la base, le garder quand meme
456
+    if (!spip_connect()) {
457
+        if (isset($page['texte'])) {
458
+            gunzip_page($page);
459
+            $use_cache = 0;
460
+        } else {
461
+            spip_log("Erreur base de donnees, impossible utiliser $chemin_cache");
462
+            include_spip('inc/minipres');
463
+
464
+            return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), array('status' => 503));
465
+        }
466
+    }
467
+
468
+    if ($use_cache < 0) {
469
+        $chemin_cache = '';
470
+    }
471
+
472
+    return;
473 473
 }
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
 function generer_nom_fichier_cache($contexte, $page) {
30 30
 	$u = md5(var_export(array($contexte, $page), true));
31 31
 
32
-	return $u . ".cache";
32
+	return $u.".cache";
33 33
 }
34 34
 
35 35
 /**
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
 function cache_chemin_fichier($nom_cache, $ecrire = false) {
48 48
 	static $l1, $l2;
49 49
 	if (is_null($l1)) {
50
-		$length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8,max(_CACHE_PROFONDEUR_STOCKAGE,2)) : 4);
50
+		$length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8, max(_CACHE_PROFONDEUR_STOCKAGE, 2)) : 4);
51 51
 		$l1 = intval(floor($length / 2));
52 52
 		$l2 = $length - $l1;
53 53
 	}
@@ -61,10 +61,10 @@  discard block
 block discarded – undo
61 61
 	}
62 62
 	else {
63 63
 		// en lecture on essaye pa de creer les repertoires, on va au plus vite
64
-		$rep = _DIR_CACHE . "$d/";
64
+		$rep = _DIR_CACHE."$d/";
65 65
 	}
66 66
 
67
-	return $rep . $u . ".cache";
67
+	return $rep.$u.".cache";
68 68
 }
69 69
 
70 70
 /**
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
 			), 'non');
112 112
 	}
113 113
 
114
-	return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
114
+	return crc32($GLOBALS['meta']['cache_signature'].$page['texte']);
115 115
 }
116 116
 
117 117
 /**
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
 		// "cache sessionne" ; sa date indique la date de validite
266 266
 		// des caches sessionnes
267 267
 		if (!$tmp = lire_cache($chemin_cache)) {
268
-			spip_log('Creation cache sessionne ' . $chemin_cache);
268
+			spip_log('Creation cache sessionne '.$chemin_cache);
269 269
 			$tmp = array(
270 270
 				'invalideurs' => array('session' => ''),
271 271
 				'lastmodified' => $_SERVER['REQUEST_TIME']
@@ -289,8 +289,8 @@  discard block
 block discarded – undo
289 289
 	// l'enregistrer, compresse ou non...
290 290
 	$ok = ecrire_cache($chemin_cache, $pagez);
291 291
 
292
-	spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
293
-		. $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
292
+	spip_log((_IS_BOT ? "Bot:" : "")."Creation du cache $chemin_cache pour "
293
+		. $page['entetes']['X-Spip-Cache']." secondes".($ok ? '' : ' (erreur!)'), _LOG_INFO);
294 294
 
295 295
 	// Inserer ses invalideurs
296 296
 	include_spip('inc/invalideur');
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 function nettoyer_petit_cache($prefix, $duree = 300) {
313 313
 	// determiner le repertoire a purger : 'tmp/CACHE/rech/'
314 314
 	$dircache = sous_repertoire(_DIR_CACHE, $prefix);
315
-	if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
315
+	if (spip_touch($dircache.'purger_'.$prefix, $duree, true)) {
316 316
 		foreach (preg_files($dircache, '[.]txt$') as $f) {
317 317
 			if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
318 318
 				spip_unlink($f);
@@ -402,9 +402,9 @@  discard block
 block discarded – undo
402 402
 		if (spip_connect()) {
403 403
 			include_spip('inc/invalideur');
404 404
 			retire_caches($chemin_cache); # API invalideur inutile
405
-			supprimer_fichier(_DIR_CACHE . $chemin_cache);
405
+			supprimer_fichier(_DIR_CACHE.$chemin_cache);
406 406
 			if (isset($chemin_cache_session) and $chemin_cache_session) {
407
-				supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
407
+				supprimer_fichier(_DIR_CACHE.$chemin_cache_session);
408 408
 			}
409 409
 		}
410 410
 	}
@@ -419,9 +419,9 @@  discard block
 block discarded – undo
419 419
 		$page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
420 420
 		include_spip('inc/invalideur');
421 421
 		retire_caches($chemin_cache); # API invalideur inutile
422
-		supprimer_fichier(_DIR_CACHE . $chemin_cache);
422
+		supprimer_fichier(_DIR_CACHE.$chemin_cache);
423 423
 		if (isset($chemin_cache_session) and $chemin_cache_session) {
424
-			supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
424
+			supprimer_fichier(_DIR_CACHE.$chemin_cache_session);
425 425
 		}
426 426
 	}
427 427
 
Please login to merge, or discard this patch.
Braces   +4 added lines, -3 removed lines patch added patch discarded remove patch
@@ -58,8 +58,7 @@  discard block
 block discarded – undo
58 58
 		$rep = sous_repertoire(_DIR_CACHE, '', false, true);
59 59
 		$rep = sous_repertoire($rep, 'calcul/', false, true);
60 60
 		$rep = sous_repertoire($rep, $d, false, true);
61
-	}
62
-	else {
61
+	} else {
63 62
 		// en lecture on essaye pa de creer les repertoires, on va au plus vite
64 63
 		$rep = _DIR_CACHE . "$d/";
65 64
 	}
@@ -218,9 +217,11 @@  discard block
 block discarded – undo
218 217
 	// Sinon comparer l'age du fichier a sa duree de cache
219 218
 	$duree = intval($page['entetes']['X-Spip-Cache']);
220 219
 	$cache_mark = (isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0);
221
-	if ($duree == 0)  #CACHE{0}
220
+	if ($duree == 0) {
221
+	    #CACHE{0}
222 222
 	{
223 223
 		return -1;
224
+	}
224 225
 	} // sauf pour les bots, qui utilisent toujours le cache
225 226
 	else {
226 227
 		if ((!_IS_BOT and $date + $duree < $now)
Please login to merge, or discard this patch.