Completed
Push — master ( 973190...c61990 )
by cam
04:49
created
ecrire/public/cacher.php 2 patches
Indentation   +291 added lines, -291 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
 /**
@@ -40,13 +40,13 @@  discard block
 block discarded – undo
40 40
  * @return bool
41 41
  */
42 42
 function ecrire_cache($nom_cache, $valeur) {
43
-	$d = substr($nom_cache, 0, 2);
44
-	$u = substr($nom_cache, 2, 2);
45
-	$rep = _DIR_CACHE;
46
-	$rep = sous_repertoire($rep, '', false, true);
47
-	$rep = sous_repertoire($rep, $d, false, true);
43
+    $d = substr($nom_cache, 0, 2);
44
+    $u = substr($nom_cache, 2, 2);
45
+    $rep = _DIR_CACHE;
46
+    $rep = sous_repertoire($rep, '', false, true);
47
+    $rep = sous_repertoire($rep, $d, false, true);
48 48
 
49
-	return ecrire_fichier($rep . $u . ".cache", serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
49
+    return ecrire_fichier($rep . $u . ".cache", serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
50 50
 }
51 51
 
52 52
 /**
@@ -56,35 +56,35 @@  discard block
 block discarded – undo
56 56
  * @return mixed
57 57
  */
58 58
 function lire_cache($nom_cache) {
59
-	$d = substr($nom_cache, 0, 2);
60
-	$u = substr($nom_cache, 2, 2);
61
-	if (file_exists($f = _DIR_CACHE . "$d/$u.cache")
62
-		and lire_fichier($f, $tmp)
63
-		and $tmp = unserialize($tmp)
64
-		and $tmp['nom_cache'] == $nom_cache
65
-		and isset($tmp['valeur'])
66
-	) {
67
-		return $tmp['valeur'];
68
-	}
69
-
70
-	return false;
59
+    $d = substr($nom_cache, 0, 2);
60
+    $u = substr($nom_cache, 2, 2);
61
+    if (file_exists($f = _DIR_CACHE . "$d/$u.cache")
62
+        and lire_fichier($f, $tmp)
63
+        and $tmp = unserialize($tmp)
64
+        and $tmp['nom_cache'] == $nom_cache
65
+        and isset($tmp['valeur'])
66
+    ) {
67
+        return $tmp['valeur'];
68
+    }
69
+
70
+    return false;
71 71
 }
72 72
 
73 73
 // Parano : on signe le cache, afin d'interdire un hack d'injection
74 74
 // dans notre memcache
75 75
 function cache_signature(&$page) {
76
-	if (!isset($GLOBALS['meta']['cache_signature'])) {
77
-		include_spip('inc/acces');
78
-		include_spip('auth/sha256.inc');
79
-		ecrire_meta('cache_signature',
80
-			spip_sha256(
81
-				$_SERVER["DOCUMENT_ROOT"] 
82
-				. (isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER["SERVER_SIGNATURE"] : "")
83
-				. creer_uniqid()
84
-			), 'non');
85
-	}
86
-
87
-	return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
76
+    if (!isset($GLOBALS['meta']['cache_signature'])) {
77
+        include_spip('inc/acces');
78
+        include_spip('auth/sha256.inc');
79
+        ecrire_meta('cache_signature',
80
+            spip_sha256(
81
+                $_SERVER["DOCUMENT_ROOT"] 
82
+                . (isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER["SERVER_SIGNATURE"] : "")
83
+                . creer_uniqid()
84
+            ), 'non');
85
+    }
86
+
87
+    return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
88 88
 }
89 89
 
90 90
 /**
@@ -98,14 +98,14 @@  discard block
 block discarded – undo
98 98
  * @return array
99 99
  */
100 100
 function gzip_page($page) {
101
-	if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) {
102
-		$page['gz'] = true;
103
-		$page['texte'] = gzcompress($page['texte']);
104
-	} else {
105
-		$page['gz'] = false;
106
-	}
107
-
108
-	return $page;
101
+    if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) {
102
+        $page['gz'] = true;
103
+        $page['texte'] = gzcompress($page['texte']);
104
+    } else {
105
+        $page['gz'] = false;
106
+    }
107
+
108
+    return $page;
109 109
 }
110 110
 
111 111
 /**
@@ -120,10 +120,10 @@  discard block
 block discarded – undo
120 120
  * @return void
121 121
  */
122 122
 function gunzip_page(&$page) {
123
-	if ($page['gz']) {
124
-		$page['texte'] = gzuncompress($page['texte']);
125
-		$page['gz'] = false; // ne pas gzuncompress deux fois une meme page
126
-	}
123
+    if ($page['gz']) {
124
+        $page['texte'] = gzuncompress($page['texte']);
125
+        $page['gz'] = false; // ne pas gzuncompress deux fois une meme page
126
+    }
127 127
 }
128 128
 
129 129
 /**
@@ -139,72 +139,72 @@  discard block
 block discarded – undo
139 139
  */
140 140
 /// https://code.spip.net/@cache_valide
141 141
 function cache_valide(&$page, $date) {
142
-	$now = $_SERVER['REQUEST_TIME'];
143
-
144
-	// Apparition d'un nouvel article post-date ?
145
-	if (isset($GLOBALS['meta']['post_dates'])
146
-		and $GLOBALS['meta']['post_dates'] == 'non'
147
-		and isset($GLOBALS['meta']['date_prochain_postdate'])
148
-		and $now > $GLOBALS['meta']['date_prochain_postdate']
149
-	) {
150
-		spip_log('Un article post-date invalide le cache');
151
-		include_spip('inc/rubriques');
152
-		calculer_prochain_postdate(true);
153
-	}
154
-
155
-	if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) {
156
-		return -1;
157
-	}
158
-	if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) {
159
-		return -1;
160
-	}
161
-	if (defined('_NO_CACHE')) {
162
-		return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE;
163
-	}
164
-
165
-	// pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache)
166
-	if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) {
167
-		return _IS_BOT ? -1 : 1;
168
-	}
169
-
170
-	// controle de la signature
171
-	if ($page['sig'] !== cache_signature($page)) {
172
-		return _IS_BOT ? -1 : 1;
173
-	}
174
-
175
-	// #CACHE{n,statique} => on n'invalide pas avec derniere_modif
176
-	// cf. ecrire/public/balises.php, balise_CACHE_dist()
177
-	if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') {
178
-
179
-		// Cache invalide par la meta 'derniere_modif'
180
-		// sauf pour les bots, qui utilisent toujours le cache
181
-		if (!_IS_BOT
182
-			and $GLOBALS['derniere_modif_invalide']
183
-			and isset($GLOBALS['meta']['derniere_modif'])
184
-			and $date < $GLOBALS['meta']['derniere_modif']
185
-		) {
186
-			return 1;
187
-		}
188
-
189
-	}
190
-
191
-	// Sinon comparer l'age du fichier a sa duree de cache
192
-	$duree = intval($page['entetes']['X-Spip-Cache']);
193
-	$cache_mark = (isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0);
194
-	if ($duree == 0)  #CACHE{0}
195
-	{
196
-		return -1;
197
-	} // sauf pour les bots, qui utilisent toujours le cache
198
-	else {
199
-		if ((!_IS_BOT and $date + $duree < $now)
200
-			# le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots
201
-			or $date < $cache_mark
202
-		) {
203
-			return _IS_BOT ? -1 : 1;
204
-		} else {
205
-			return 0;
206
-		}
207
-	}
142
+    $now = $_SERVER['REQUEST_TIME'];
143
+
144
+    // Apparition d'un nouvel article post-date ?
145
+    if (isset($GLOBALS['meta']['post_dates'])
146
+        and $GLOBALS['meta']['post_dates'] == 'non'
147
+        and isset($GLOBALS['meta']['date_prochain_postdate'])
148
+        and $now > $GLOBALS['meta']['date_prochain_postdate']
149
+    ) {
150
+        spip_log('Un article post-date invalide le cache');
151
+        include_spip('inc/rubriques');
152
+        calculer_prochain_postdate(true);
153
+    }
154
+
155
+    if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) {
156
+        return -1;
157
+    }
158
+    if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) {
159
+        return -1;
160
+    }
161
+    if (defined('_NO_CACHE')) {
162
+        return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE;
163
+    }
164
+
165
+    // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache)
166
+    if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) {
167
+        return _IS_BOT ? -1 : 1;
168
+    }
169
+
170
+    // controle de la signature
171
+    if ($page['sig'] !== cache_signature($page)) {
172
+        return _IS_BOT ? -1 : 1;
173
+    }
174
+
175
+    // #CACHE{n,statique} => on n'invalide pas avec derniere_modif
176
+    // cf. ecrire/public/balises.php, balise_CACHE_dist()
177
+    if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') {
178
+
179
+        // Cache invalide par la meta 'derniere_modif'
180
+        // sauf pour les bots, qui utilisent toujours le cache
181
+        if (!_IS_BOT
182
+            and $GLOBALS['derniere_modif_invalide']
183
+            and isset($GLOBALS['meta']['derniere_modif'])
184
+            and $date < $GLOBALS['meta']['derniere_modif']
185
+        ) {
186
+            return 1;
187
+        }
188
+
189
+    }
190
+
191
+    // Sinon comparer l'age du fichier a sa duree de cache
192
+    $duree = intval($page['entetes']['X-Spip-Cache']);
193
+    $cache_mark = (isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0);
194
+    if ($duree == 0)  #CACHE{0}
195
+    {
196
+        return -1;
197
+    } // sauf pour les bots, qui utilisent toujours le cache
198
+    else {
199
+        if ((!_IS_BOT and $date + $duree < $now)
200
+            # le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots
201
+            or $date < $cache_mark
202
+        ) {
203
+            return _IS_BOT ? -1 : 1;
204
+        } else {
205
+            return 0;
206
+        }
207
+    }
208 208
 }
209 209
 
210 210
 /**
@@ -219,55 +219,55 @@  discard block
 block discarded – undo
219 219
  */
220 220
 function creer_cache(&$page, &$chemin_cache) {
221 221
 
222
-	// Ne rien faire si on est en preview, debug, ou si une erreur
223
-	// grave s'est presentee (compilation du squelette, MySQL, etc)
224
-	// le cas var_nocache ne devrait jamais arriver ici (securite)
225
-	// le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable
226
-	if ((defined('_VAR_NOCACHE') and _VAR_NOCACHE)
227
-		or defined('spip_interdire_cache')
228
-	) {
229
-		return;
230
-	}
231
-
232
-	// Si la page c1234 a un invalideur de session 'zz', sauver dans
233
-	// 'tmp/cache/MD5(chemin_cache)_zz'
234
-	if (isset($page['invalideurs'])
235
-		and isset($page['invalideurs']['session'])
236
-	) {
237
-		// on verifie que le contenu du chemin cache indique seulement
238
-		// "cache sessionne" ; sa date indique la date de validite
239
-		// des caches sessionnes
240
-		if (!$tmp = lire_cache($chemin_cache)) {
241
-			spip_log('Creation cache sessionne ' . $chemin_cache);
242
-			$tmp = array(
243
-				'invalideurs' => array('session' => ''),
244
-				'lastmodified' => $_SERVER['REQUEST_TIME']
245
-			);
246
-			ecrire_cache($chemin_cache, $tmp);
247
-		}
248
-		$chemin_cache = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
249
-			array("session" => $page['invalideurs']['session']));
250
-	}
251
-
252
-	// ajouter la date de production dans le cache lui meme
253
-	// (qui contient deja sa duree de validite)
254
-	$page['lastmodified'] = $_SERVER['REQUEST_TIME'];
255
-
256
-	// compresser le contenu si besoin
257
-	$pagez = gzip_page($page);
258
-
259
-	// signer le contenu
260
-	$pagez['sig'] = cache_signature($pagez);
261
-
262
-	// l'enregistrer, compresse ou non...
263
-	$ok = ecrire_cache($chemin_cache, $pagez);
264
-
265
-	spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
266
-		. $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
267
-
268
-	// Inserer ses invalideurs
269
-	include_spip('inc/invalideur');
270
-	maj_invalideurs($chemin_cache, $page);
222
+    // Ne rien faire si on est en preview, debug, ou si une erreur
223
+    // grave s'est presentee (compilation du squelette, MySQL, etc)
224
+    // le cas var_nocache ne devrait jamais arriver ici (securite)
225
+    // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable
226
+    if ((defined('_VAR_NOCACHE') and _VAR_NOCACHE)
227
+        or defined('spip_interdire_cache')
228
+    ) {
229
+        return;
230
+    }
231
+
232
+    // Si la page c1234 a un invalideur de session 'zz', sauver dans
233
+    // 'tmp/cache/MD5(chemin_cache)_zz'
234
+    if (isset($page['invalideurs'])
235
+        and isset($page['invalideurs']['session'])
236
+    ) {
237
+        // on verifie que le contenu du chemin cache indique seulement
238
+        // "cache sessionne" ; sa date indique la date de validite
239
+        // des caches sessionnes
240
+        if (!$tmp = lire_cache($chemin_cache)) {
241
+            spip_log('Creation cache sessionne ' . $chemin_cache);
242
+            $tmp = array(
243
+                'invalideurs' => array('session' => ''),
244
+                'lastmodified' => $_SERVER['REQUEST_TIME']
245
+            );
246
+            ecrire_cache($chemin_cache, $tmp);
247
+        }
248
+        $chemin_cache = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
249
+            array("session" => $page['invalideurs']['session']));
250
+    }
251
+
252
+    // ajouter la date de production dans le cache lui meme
253
+    // (qui contient deja sa duree de validite)
254
+    $page['lastmodified'] = $_SERVER['REQUEST_TIME'];
255
+
256
+    // compresser le contenu si besoin
257
+    $pagez = gzip_page($page);
258
+
259
+    // signer le contenu
260
+    $pagez['sig'] = cache_signature($pagez);
261
+
262
+    // l'enregistrer, compresse ou non...
263
+    $ok = ecrire_cache($chemin_cache, $pagez);
264
+
265
+    spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
266
+        . $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
267
+
268
+    // Inserer ses invalideurs
269
+    include_spip('inc/invalideur');
270
+    maj_invalideurs($chemin_cache, $page);
271 271
 
272 272
 }
273 273
 
@@ -283,15 +283,15 @@  discard block
 block discarded – undo
283 283
  * @return void
284 284
  */
285 285
 function nettoyer_petit_cache($prefix, $duree = 300) {
286
-	// determiner le repertoire a purger : 'tmp/CACHE/rech/'
287
-	$dircache = sous_repertoire(_DIR_CACHE, $prefix);
288
-	if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
289
-		foreach (preg_files($dircache, '[.]txt$') as $f) {
290
-			if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
291
-				spip_unlink($f);
292
-			}
293
-		}
294
-	}
286
+    // determiner le repertoire a purger : 'tmp/CACHE/rech/'
287
+    $dircache = sous_repertoire(_DIR_CACHE, $prefix);
288
+    if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
289
+        foreach (preg_files($dircache, '[.]txt$') as $f) {
290
+            if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
291
+                spip_unlink($f);
292
+            }
293
+        }
294
+    }
295 295
 }
296 296
 
297 297
 
@@ -321,126 +321,126 @@  discard block
 block discarded – undo
321 321
  */
322 322
 function public_cacher_dist($contexte, &$use_cache, &$chemin_cache, &$page, &$lastmodified) {
323 323
 
324
-	# fonction de cache minimale : dire "non on ne met rien en cache"
325
-	# $use_cache = -1; return;
326
-
327
-	// Second appel, destine a l'enregistrement du cache sur le disque
328
-	if (isset($chemin_cache)) {
329
-		return creer_cache($page, $chemin_cache);
330
-	}
331
-
332
-	// Toute la suite correspond au premier appel
333
-	$contexte_implicite = $page['contexte_implicite'];
334
-
335
-	// Cas ignorant le cache car completement dynamique
336
-	if ($_SERVER['REQUEST_METHOD'] == 'POST'
337
-		or _request('connect')
338
-	) {
339
-		$use_cache = -1;
340
-		$lastmodified = 0;
341
-		$chemin_cache = "";
342
-		$page = array();
343
-
344
-		return;
345
-	}
346
-
347
-	// Controler l'existence d'un cache nous correspondant
348
-	$chemin_cache = generer_nom_fichier_cache($contexte, $page);
349
-	$lastmodified = 0;
350
-
351
-	// charger le cache s'il existe (et si il a bien le bon hash = anticollision)
352
-	if (!$page = lire_cache($chemin_cache)) {
353
-		$page = array();
354
-	}
355
-
356
-	// s'il est sessionne, charger celui correspondant a notre session
357
-	if (isset($page['invalideurs'])
358
-		and isset($page['invalideurs']['session'])
359
-	) {
360
-		$chemin_cache_session = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
361
-			array("session" => spip_session()));
362
-		if ($page_session = lire_cache($chemin_cache_session)
363
-			and $page_session['lastmodified'] >= $page['lastmodified']
364
-		) {
365
-			$page = $page_session;
366
-		} else {
367
-			$page = array();
368
-		}
369
-	}
370
-
371
-
372
-	// Faut-il effacer des pages invalidees (en particulier ce cache-ci) ?
373
-	if (isset($GLOBALS['meta']['invalider'])) {
374
-		// ne le faire que si la base est disponible
375
-		if (spip_connect()) {
376
-			include_spip('inc/invalideur');
377
-			retire_caches($chemin_cache); # API invalideur inutile
378
-			supprimer_fichier(_DIR_CACHE . $chemin_cache);
379
-			if (isset($chemin_cache_session) and $chemin_cache_session) {
380
-				supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
381
-			}
382
-		}
383
-	}
384
-
385
-	// Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande,
386
-	// on supprime le cache
387
-	if (defined('_VAR_MODE') && _VAR_MODE &&
388
-		(isset($_COOKIE['spip_session'])
389
-			|| isset($_COOKIE['spip_admin'])
390
-			|| @file_exists(_ACCESS_FILE_NAME))
391
-	) {
392
-		$page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
393
-		include_spip('inc/invalideur');
394
-		retire_caches($chemin_cache); # API invalideur inutile
395
-		supprimer_fichier(_DIR_CACHE . $chemin_cache);
396
-		if (isset($chemin_cache_session) and $chemin_cache_session) {
397
-			supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
398
-		}
399
-	}
400
-
401
-	// $delais par defaut
402
-	// pour toutes les pages sans #CACHE{} hors modeles/ et espace privé
403
-	// qui sont a cache nul par defaut
404
-	if (!isset($GLOBALS['delais'])) {
405
-		if (!defined('_DUREE_CACHE_DEFAUT')) {
406
-			define('_DUREE_CACHE_DEFAUT', 24 * 3600);
407
-		}
408
-		$GLOBALS['delais'] = _DUREE_CACHE_DEFAUT;
409
-	}
410
-
411
-	// determiner la validite de la page
412
-	if ($page) {
413
-		$use_cache = cache_valide($page, isset($page['lastmodified']) ? $page['lastmodified'] : 0);
414
-		// le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence
415
-		// par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul
416
-		$page['contexte_implicite'] = $contexte_implicite;
417
-		if (!$use_cache) {
418
-			// $page est un cache utilisable
419
-			gunzip_page($page);
420
-
421
-			return;
422
-		}
423
-	} else {
424
-		$page = array('contexte_implicite' => $contexte_implicite);
425
-		$use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul
426
-	}
427
-
428
-	// Si pas valide mais pas de connexion a la base, le garder quand meme
429
-	if (!spip_connect()) {
430
-		if (isset($page['texte'])) {
431
-			gunzip_page($page);
432
-			$use_cache = 0;
433
-		} else {
434
-			spip_log("Erreur base de donnees, impossible utiliser $chemin_cache");
435
-			include_spip('inc/minipres');
436
-
437
-			return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), array('status' => 503));
438
-		}
439
-	}
440
-
441
-	if ($use_cache < 0) {
442
-		$chemin_cache = '';
443
-	}
444
-
445
-	return;
324
+    # fonction de cache minimale : dire "non on ne met rien en cache"
325
+    # $use_cache = -1; return;
326
+
327
+    // Second appel, destine a l'enregistrement du cache sur le disque
328
+    if (isset($chemin_cache)) {
329
+        return creer_cache($page, $chemin_cache);
330
+    }
331
+
332
+    // Toute la suite correspond au premier appel
333
+    $contexte_implicite = $page['contexte_implicite'];
334
+
335
+    // Cas ignorant le cache car completement dynamique
336
+    if ($_SERVER['REQUEST_METHOD'] == 'POST'
337
+        or _request('connect')
338
+    ) {
339
+        $use_cache = -1;
340
+        $lastmodified = 0;
341
+        $chemin_cache = "";
342
+        $page = array();
343
+
344
+        return;
345
+    }
346
+
347
+    // Controler l'existence d'un cache nous correspondant
348
+    $chemin_cache = generer_nom_fichier_cache($contexte, $page);
349
+    $lastmodified = 0;
350
+
351
+    // charger le cache s'il existe (et si il a bien le bon hash = anticollision)
352
+    if (!$page = lire_cache($chemin_cache)) {
353
+        $page = array();
354
+    }
355
+
356
+    // s'il est sessionne, charger celui correspondant a notre session
357
+    if (isset($page['invalideurs'])
358
+        and isset($page['invalideurs']['session'])
359
+    ) {
360
+        $chemin_cache_session = generer_nom_fichier_cache(array("chemin_cache" => $chemin_cache),
361
+            array("session" => spip_session()));
362
+        if ($page_session = lire_cache($chemin_cache_session)
363
+            and $page_session['lastmodified'] >= $page['lastmodified']
364
+        ) {
365
+            $page = $page_session;
366
+        } else {
367
+            $page = array();
368
+        }
369
+    }
370
+
371
+
372
+    // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ?
373
+    if (isset($GLOBALS['meta']['invalider'])) {
374
+        // ne le faire que si la base est disponible
375
+        if (spip_connect()) {
376
+            include_spip('inc/invalideur');
377
+            retire_caches($chemin_cache); # API invalideur inutile
378
+            supprimer_fichier(_DIR_CACHE . $chemin_cache);
379
+            if (isset($chemin_cache_session) and $chemin_cache_session) {
380
+                supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
381
+            }
382
+        }
383
+    }
384
+
385
+    // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande,
386
+    // on supprime le cache
387
+    if (defined('_VAR_MODE') && _VAR_MODE &&
388
+        (isset($_COOKIE['spip_session'])
389
+            || isset($_COOKIE['spip_admin'])
390
+            || @file_exists(_ACCESS_FILE_NAME))
391
+    ) {
392
+        $page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
393
+        include_spip('inc/invalideur');
394
+        retire_caches($chemin_cache); # API invalideur inutile
395
+        supprimer_fichier(_DIR_CACHE . $chemin_cache);
396
+        if (isset($chemin_cache_session) and $chemin_cache_session) {
397
+            supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
398
+        }
399
+    }
400
+
401
+    // $delais par defaut
402
+    // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé
403
+    // qui sont a cache nul par defaut
404
+    if (!isset($GLOBALS['delais'])) {
405
+        if (!defined('_DUREE_CACHE_DEFAUT')) {
406
+            define('_DUREE_CACHE_DEFAUT', 24 * 3600);
407
+        }
408
+        $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT;
409
+    }
410
+
411
+    // determiner la validite de la page
412
+    if ($page) {
413
+        $use_cache = cache_valide($page, isset($page['lastmodified']) ? $page['lastmodified'] : 0);
414
+        // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence
415
+        // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul
416
+        $page['contexte_implicite'] = $contexte_implicite;
417
+        if (!$use_cache) {
418
+            // $page est un cache utilisable
419
+            gunzip_page($page);
420
+
421
+            return;
422
+        }
423
+    } else {
424
+        $page = array('contexte_implicite' => $contexte_implicite);
425
+        $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul
426
+    }
427
+
428
+    // Si pas valide mais pas de connexion a la base, le garder quand meme
429
+    if (!spip_connect()) {
430
+        if (isset($page['texte'])) {
431
+            gunzip_page($page);
432
+            $use_cache = 0;
433
+        } else {
434
+            spip_log("Erreur base de donnees, impossible utiliser $chemin_cache");
435
+            include_spip('inc/minipres');
436
+
437
+            return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), array('status' => 503));
438
+        }
439
+    }
440
+
441
+    if ($use_cache < 0) {
442
+        $chemin_cache = '';
443
+    }
444
+
445
+    return;
446 446
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 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
 /**
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 	$rep = sous_repertoire($rep, '', false, true);
47 47
 	$rep = sous_repertoire($rep, $d, false, true);
48 48
 
49
-	return ecrire_fichier($rep . $u . ".cache", serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
49
+	return ecrire_fichier($rep.$u.".cache", serialize(array("nom_cache" => $nom_cache, "valeur" => $valeur)));
50 50
 }
51 51
 
52 52
 /**
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
 function lire_cache($nom_cache) {
59 59
 	$d = substr($nom_cache, 0, 2);
60 60
 	$u = substr($nom_cache, 2, 2);
61
-	if (file_exists($f = _DIR_CACHE . "$d/$u.cache")
61
+	if (file_exists($f = _DIR_CACHE."$d/$u.cache")
62 62
 		and lire_fichier($f, $tmp)
63 63
 		and $tmp = unserialize($tmp)
64 64
 		and $tmp['nom_cache'] == $nom_cache
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 			), 'non');
85 85
 	}
86 86
 
87
-	return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']);
87
+	return crc32($GLOBALS['meta']['cache_signature'].$page['texte']);
88 88
 }
89 89
 
90 90
 /**
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
 		// "cache sessionne" ; sa date indique la date de validite
239 239
 		// des caches sessionnes
240 240
 		if (!$tmp = lire_cache($chemin_cache)) {
241
-			spip_log('Creation cache sessionne ' . $chemin_cache);
241
+			spip_log('Creation cache sessionne '.$chemin_cache);
242 242
 			$tmp = array(
243 243
 				'invalideurs' => array('session' => ''),
244 244
 				'lastmodified' => $_SERVER['REQUEST_TIME']
@@ -262,8 +262,8 @@  discard block
 block discarded – undo
262 262
 	// l'enregistrer, compresse ou non...
263 263
 	$ok = ecrire_cache($chemin_cache, $pagez);
264 264
 
265
-	spip_log((_IS_BOT ? "Bot:" : "") . "Creation du cache $chemin_cache pour "
266
-		. $page['entetes']['X-Spip-Cache'] . " secondes" . ($ok ? '' : ' (erreur!)'), _LOG_INFO);
265
+	spip_log((_IS_BOT ? "Bot:" : "")."Creation du cache $chemin_cache pour "
266
+		. $page['entetes']['X-Spip-Cache']." secondes".($ok ? '' : ' (erreur!)'), _LOG_INFO);
267 267
 
268 268
 	// Inserer ses invalideurs
269 269
 	include_spip('inc/invalideur');
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
 function nettoyer_petit_cache($prefix, $duree = 300) {
286 286
 	// determiner le repertoire a purger : 'tmp/CACHE/rech/'
287 287
 	$dircache = sous_repertoire(_DIR_CACHE, $prefix);
288
-	if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) {
288
+	if (spip_touch($dircache.'purger_'.$prefix, $duree, true)) {
289 289
 		foreach (preg_files($dircache, '[.]txt$') as $f) {
290 290
 			if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) {
291 291
 				spip_unlink($f);
@@ -375,9 +375,9 @@  discard block
 block discarded – undo
375 375
 		if (spip_connect()) {
376 376
 			include_spip('inc/invalideur');
377 377
 			retire_caches($chemin_cache); # API invalideur inutile
378
-			supprimer_fichier(_DIR_CACHE . $chemin_cache);
378
+			supprimer_fichier(_DIR_CACHE.$chemin_cache);
379 379
 			if (isset($chemin_cache_session) and $chemin_cache_session) {
380
-				supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
380
+				supprimer_fichier(_DIR_CACHE.$chemin_cache_session);
381 381
 			}
382 382
 		}
383 383
 	}
@@ -392,9 +392,9 @@  discard block
 block discarded – undo
392 392
 		$page = array('contexte_implicite' => $contexte_implicite); // ignorer le cache deja lu
393 393
 		include_spip('inc/invalideur');
394 394
 		retire_caches($chemin_cache); # API invalideur inutile
395
-		supprimer_fichier(_DIR_CACHE . $chemin_cache);
395
+		supprimer_fichier(_DIR_CACHE.$chemin_cache);
396 396
 		if (isset($chemin_cache_session) and $chemin_cache_session) {
397
-			supprimer_fichier(_DIR_CACHE . $chemin_cache_session);
397
+			supprimer_fichier(_DIR_CACHE.$chemin_cache_session);
398 398
 		}
399 399
 	}
400 400
 
Please login to merge, or discard this patch.