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