@@ -8,19 +8,19 @@ |
||
| 8 | 8 | * @param string $cache_key |
| 9 | 9 | * @param array $page |
| 10 | 10 | */ |
| 11 | - function inc_maj_invalideurs($cache_key, $page): void { |
|
| 12 | - if (!isset($page['contexte']['assert_session'])) { |
|
| 13 | - return; |
|
| 14 | - } |
|
| 11 | + function inc_maj_invalideurs($cache_key, $page): void { |
|
| 12 | + if (!isset($page['contexte']['assert_session'])) { |
|
| 13 | + return; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - $expected_session = (bool) $page['contexte']['assert_session']; |
|
| 17 | - $has_session = $page['invalideurs']['session'] ?? false; |
|
| 16 | + $expected_session = (bool) $page['contexte']['assert_session']; |
|
| 17 | + $has_session = $page['invalideurs']['session'] ?? false; |
|
| 18 | 18 | |
| 19 | - if ($expected_session) { |
|
| 20 | - if (!$has_session) { |
|
| 21 | - CacheSessionTest::addError('PAS de session', $page); |
|
| 22 | - } |
|
| 23 | - } elseif ($has_session) { |
|
| 24 | - CacheSessionTest::addError(sprintf('SESSION %s', $has_session), $page); |
|
| 25 | - } |
|
| 19 | + if ($expected_session) { |
|
| 20 | + if (!$has_session) { |
|
| 21 | + CacheSessionTest::addError('PAS de session', $page); |
|
| 22 | + } |
|
| 23 | + } elseif ($has_session) { |
|
| 24 | + CacheSessionTest::addError(sprintf('SESSION %s', $has_session), $page); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('base/serial'); |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * en droit de l'éliminer |
| 27 | 27 | */ |
| 28 | 28 | if (!defined('_AGE_CACHE_ATIME')) { |
| 29 | - define('_AGE_CACHE_ATIME', 3600); |
|
| 29 | + define('_AGE_CACHE_ATIME', 3600); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | |
@@ -39,31 +39,31 @@ discard block |
||
| 39 | 39 | * @return int Taille approximative en octets |
| 40 | 40 | **/ |
| 41 | 41 | function taille_du_cache() { |
| 42 | - # check dirs until we reach > 500 files |
|
| 43 | - $t = 0; |
|
| 44 | - $n = 0; |
|
| 45 | - $time = $GLOBALS['meta']['cache_mark'] ?? 0; |
|
| 46 | - for ($i = 0; $i < 256; $i++) { |
|
| 47 | - $dir = _DIR_CACHE . sprintf('%02s', dechex($i)); |
|
| 48 | - if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) { |
|
| 49 | - while (($f = readdir($d)) !== false) { |
|
| 50 | - if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) && ($a = stat("$dir/$f"))) { |
|
| 51 | - $n++; |
|
| 52 | - if ($a['mtime'] >= $time) { |
|
| 53 | - if ($a['blocks'] > 0) { |
|
| 54 | - $t += 512 * $a['blocks']; |
|
| 55 | - } else { |
|
| 56 | - $t += $a['size']; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - if ($n > 500) { |
|
| 63 | - return (int) (256 * $t / (1 + $i)); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - return $t; |
|
| 42 | + # check dirs until we reach > 500 files |
|
| 43 | + $t = 0; |
|
| 44 | + $n = 0; |
|
| 45 | + $time = $GLOBALS['meta']['cache_mark'] ?? 0; |
|
| 46 | + for ($i = 0; $i < 256; $i++) { |
|
| 47 | + $dir = _DIR_CACHE . sprintf('%02s', dechex($i)); |
|
| 48 | + if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) { |
|
| 49 | + while (($f = readdir($d)) !== false) { |
|
| 50 | + if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) && ($a = stat("$dir/$f"))) { |
|
| 51 | + $n++; |
|
| 52 | + if ($a['mtime'] >= $time) { |
|
| 53 | + if ($a['blocks'] > 0) { |
|
| 54 | + $t += 512 * $a['blocks']; |
|
| 55 | + } else { |
|
| 56 | + $t += $a['size']; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + if ($n > 500) { |
|
| 63 | + return (int) (256 * $t / (1 + $i)); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + return $t; |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | |
@@ -90,30 +90,30 @@ discard block |
||
| 90 | 90 | * Inutilisé |
| 91 | 91 | **/ |
| 92 | 92 | function inc_suivre_invalideur_dist($cond, $modif = true) { |
| 93 | - if (!$modif) { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // determiner l'objet modifie : forum, article, etc |
|
| 98 | - if (preg_match(',["\']([a-z_]+)[/"\'],', $cond, $r)) { |
|
| 99 | - $objet = objet_type($r[1]); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // stocker la date_modif_$objet (ne sert a rien pour le moment) |
|
| 103 | - if (isset($objet)) { |
|
| 104 | - ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // si $derniere_modif_invalide est un array('article', 'rubrique') |
|
| 108 | - // n'affecter la meta que si un de ces objets est modifie |
|
| 109 | - if (is_array($GLOBALS['derniere_modif_invalide'])) { |
|
| 110 | - if (in_array($objet, $GLOBALS['derniere_modif_invalide'])) { |
|
| 111 | - ecrire_meta('derniere_modif', time()); |
|
| 112 | - } |
|
| 113 | - } // sinon, cas standard, toujours affecter la meta |
|
| 114 | - else { |
|
| 115 | - ecrire_meta('derniere_modif', time()); |
|
| 116 | - } |
|
| 93 | + if (!$modif) { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // determiner l'objet modifie : forum, article, etc |
|
| 98 | + if (preg_match(',["\']([a-z_]+)[/"\'],', $cond, $r)) { |
|
| 99 | + $objet = objet_type($r[1]); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // stocker la date_modif_$objet (ne sert a rien pour le moment) |
|
| 103 | + if (isset($objet)) { |
|
| 104 | + ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // si $derniere_modif_invalide est un array('article', 'rubrique') |
|
| 108 | + // n'affecter la meta que si un de ces objets est modifie |
|
| 109 | + if (is_array($GLOBALS['derniere_modif_invalide'])) { |
|
| 110 | + if (in_array($objet, $GLOBALS['derniere_modif_invalide'])) { |
|
| 111 | + ecrire_meta('derniere_modif', time()); |
|
| 112 | + } |
|
| 113 | + } // sinon, cas standard, toujours affecter la meta |
|
| 114 | + else { |
|
| 115 | + ecrire_meta('derniere_modif', time()); |
|
| 116 | + } |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | |
@@ -138,56 +138,56 @@ discard block |
||
| 138 | 138 | * Nombre de fichiers supprimés |
| 139 | 139 | **/ |
| 140 | 140 | function purger_repertoire($dir, $options = []) { |
| 141 | - if (!is_dir($dir) || !is_readable($dir)) { |
|
| 142 | - return 0; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // sur certains sites on veut absolument garder certains caches référencés dans un CDN |
|
| 146 | - // on peut donc inhiber la purge de ces répertoires pour eviter tout probleme |
|
| 147 | - if (file_exists(rtrim($dir, '/') . '/inhib_purger_repertoire.txt')) { |
|
| 148 | - return 0; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $handle = opendir($dir); |
|
| 152 | - if (!$handle) { |
|
| 153 | - return 0; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $total = 0; |
|
| 157 | - |
|
| 158 | - while (($fichier = @readdir($handle)) !== false) { |
|
| 159 | - // Eviter ".", "..", ".htaccess", ".svn" etc & CACHEDIR.TAG |
|
| 160 | - if ($fichier[0] == '.' || $fichier == 'CACHEDIR.TAG') { |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - $chemin = "$dir/$fichier"; |
|
| 164 | - if (is_file($chemin)) { |
|
| 165 | - if ( |
|
| 166 | - (!isset($options['atime']) || @fileatime($chemin) < $options['atime']) && (!isset($options['mtime']) || @filemtime($chemin) < $options['mtime']) |
|
| 167 | - ) { |
|
| 168 | - supprimer_fichier($chemin); |
|
| 169 | - $total++; |
|
| 170 | - } |
|
| 171 | - } else { |
|
| 172 | - if (is_dir($chemin)) { |
|
| 173 | - $opts = $options; |
|
| 174 | - if (isset($options['limit'])) { |
|
| 175 | - $opts['limit'] = $options['limit'] - $total; |
|
| 176 | - } |
|
| 177 | - $total += purger_repertoire($chemin, $opts); |
|
| 178 | - if (isset($options['subdir']) && $options['subdir']) { |
|
| 179 | - spip_unlink($chemin); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if (isset($options['limit']) && $total >= $options['limit']) { |
|
| 185 | - break; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - closedir($handle); |
|
| 189 | - |
|
| 190 | - return $total; |
|
| 141 | + if (!is_dir($dir) || !is_readable($dir)) { |
|
| 142 | + return 0; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // sur certains sites on veut absolument garder certains caches référencés dans un CDN |
|
| 146 | + // on peut donc inhiber la purge de ces répertoires pour eviter tout probleme |
|
| 147 | + if (file_exists(rtrim($dir, '/') . '/inhib_purger_repertoire.txt')) { |
|
| 148 | + return 0; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $handle = opendir($dir); |
|
| 152 | + if (!$handle) { |
|
| 153 | + return 0; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $total = 0; |
|
| 157 | + |
|
| 158 | + while (($fichier = @readdir($handle)) !== false) { |
|
| 159 | + // Eviter ".", "..", ".htaccess", ".svn" etc & CACHEDIR.TAG |
|
| 160 | + if ($fichier[0] == '.' || $fichier == 'CACHEDIR.TAG') { |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + $chemin = "$dir/$fichier"; |
|
| 164 | + if (is_file($chemin)) { |
|
| 165 | + if ( |
|
| 166 | + (!isset($options['atime']) || @fileatime($chemin) < $options['atime']) && (!isset($options['mtime']) || @filemtime($chemin) < $options['mtime']) |
|
| 167 | + ) { |
|
| 168 | + supprimer_fichier($chemin); |
|
| 169 | + $total++; |
|
| 170 | + } |
|
| 171 | + } else { |
|
| 172 | + if (is_dir($chemin)) { |
|
| 173 | + $opts = $options; |
|
| 174 | + if (isset($options['limit'])) { |
|
| 175 | + $opts['limit'] = $options['limit'] - $total; |
|
| 176 | + } |
|
| 177 | + $total += purger_repertoire($chemin, $opts); |
|
| 178 | + if (isset($options['subdir']) && $options['subdir']) { |
|
| 179 | + spip_unlink($chemin); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if (isset($options['limit']) && $total >= $options['limit']) { |
|
| 185 | + break; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + closedir($handle); |
|
| 189 | + |
|
| 190 | + return $total; |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | |
@@ -199,10 +199,10 @@ discard block |
||
| 199 | 199 | // A priori dans cette version la fonction ne sera pas appelee, car |
| 200 | 200 | // la meta est toujours false ; mais evitons un bug si elle est appellee |
| 201 | 201 | function inc_retire_caches_dist($chemin = '') { |
| 202 | - if (isset($GLOBALS['meta']['invalider_caches'])) { |
|
| 203 | - # concurrence |
|
| 204 | - effacer_meta('invalider_caches'); |
|
| 205 | - } |
|
| 202 | + if (isset($GLOBALS['meta']['invalider_caches'])) { |
|
| 203 | + # concurrence |
|
| 204 | + effacer_meta('invalider_caches'); |
|
| 205 | + } |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | ####################################################################### |
@@ -213,9 +213,9 @@ discard block |
||
| 213 | 213 | ## |
| 214 | 214 | |
| 215 | 215 | function retire_caches($chemin = '') { |
| 216 | - if ($retire_caches = charger_fonction('retire_caches', 'inc', true)) { |
|
| 217 | - return $retire_caches($chemin); |
|
| 218 | - } |
|
| 216 | + if ($retire_caches = charger_fonction('retire_caches', 'inc', true)) { |
|
| 217 | + return $retire_caches($chemin); |
|
| 218 | + } |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | |
@@ -223,10 +223,10 @@ discard block |
||
| 223 | 223 | // (note: si absente, n'est pas appellee) |
| 224 | 224 | |
| 225 | 225 | function calcul_invalideurs($corps, $primary, &$boucles, $id_boucle) { |
| 226 | - if ($calcul_invalideurs = charger_fonction('calcul_invalideurs', 'inc', true)) { |
|
| 227 | - return $calcul_invalideurs($corps, $primary, $boucles, $id_boucle); |
|
| 228 | - } |
|
| 229 | - return $corps; |
|
| 226 | + if ($calcul_invalideurs = charger_fonction('calcul_invalideurs', 'inc', true)) { |
|
| 227 | + return $calcul_invalideurs($corps, $primary, $boucles, $id_boucle); |
|
| 228 | + } |
|
| 229 | + return $corps; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | |
@@ -235,41 +235,41 @@ discard block |
||
| 235 | 235 | // invoquee quand on vide tout le cache en bloc (action/purger) |
| 236 | 236 | // |
| 237 | 237 | function supprime_invalideurs() { |
| 238 | - if ($supprime_invalideurs = charger_fonction('supprime_invalideurs', 'inc', true)) { |
|
| 239 | - return $supprime_invalideurs(); |
|
| 240 | - } |
|
| 238 | + if ($supprime_invalideurs = charger_fonction('supprime_invalideurs', 'inc', true)) { |
|
| 239 | + return $supprime_invalideurs(); |
|
| 240 | + } |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | |
| 244 | 244 | // Calcul des pages : noter dans la base les liens d'invalidation |
| 245 | 245 | function maj_invalideurs($fichier, &$page) { |
| 246 | - if ($maj_invalideurs = charger_fonction('maj_invalideurs', 'inc', true)) { |
|
| 247 | - return $maj_invalideurs($fichier, $page); |
|
| 248 | - } |
|
| 246 | + if ($maj_invalideurs = charger_fonction('maj_invalideurs', 'inc', true)) { |
|
| 247 | + return $maj_invalideurs($fichier, $page); |
|
| 248 | + } |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | |
| 252 | 252 | // les invalideurs sont de la forme "objet/id_objet" |
| 253 | 253 | function insere_invalideur($inval, $fichier) { |
| 254 | - if ($insere_invalideur = charger_fonction('insere_invalideur', 'inc', true)) { |
|
| 255 | - return $insere_invalideur($inval, $fichier); |
|
| 256 | - } |
|
| 254 | + if ($insere_invalideur = charger_fonction('insere_invalideur', 'inc', true)) { |
|
| 255 | + return $insere_invalideur($inval, $fichier); |
|
| 256 | + } |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | // |
| 260 | 260 | // Marquer les fichiers caches invalides comme etant a supprimer |
| 261 | 261 | // |
| 262 | 262 | function applique_invalideur($depart) { |
| 263 | - if ($applique_invalideur = charger_fonction('applique_invalideur', 'inc', true)) { |
|
| 264 | - return $applique_invalideur($depart); |
|
| 265 | - } |
|
| 263 | + if ($applique_invalideur = charger_fonction('applique_invalideur', 'inc', true)) { |
|
| 264 | + return $applique_invalideur($depart); |
|
| 265 | + } |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // |
| 269 | 269 | // Invalider les caches liés à telle condition |
| 270 | 270 | // |
| 271 | 271 | function suivre_invalideur($cond, $modif = true) { |
| 272 | - if ($suivre_invalideur = charger_fonction('suivre_invalideur', 'inc', true)) { |
|
| 273 | - return $suivre_invalideur($cond, $modif); |
|
| 274 | - } |
|
| 272 | + if ($suivre_invalideur = charger_fonction('suivre_invalideur', 'inc', true)) { |
|
| 273 | + return $suivre_invalideur($cond, $modif); |
|
| 274 | + } |
|
| 275 | 275 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | $n = 0; |
| 45 | 45 | $time = $GLOBALS['meta']['cache_mark'] ?? 0; |
| 46 | 46 | for ($i = 0; $i < 256; $i++) { |
| 47 | - $dir = _DIR_CACHE . sprintf('%02s', dechex($i)); |
|
| 47 | + $dir = _DIR_CACHE.sprintf('%02s', dechex($i)); |
|
| 48 | 48 | if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) { |
| 49 | 49 | while (($f = readdir($d)) !== false) { |
| 50 | 50 | if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) && ($a = stat("$dir/$f"))) { |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | // stocker la date_modif_$objet (ne sert a rien pour le moment) |
| 103 | 103 | if (isset($objet)) { |
| 104 | - ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 104 | + ecrire_meta('derniere_modif_'.$objet, time()); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | // si $derniere_modif_invalide est un array('article', 'rubrique') |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | // sur certains sites on veut absolument garder certains caches référencés dans un CDN |
| 146 | 146 | // on peut donc inhiber la purge de ces répertoires pour eviter tout probleme |
| 147 | - if (file_exists(rtrim($dir, '/') . '/inhib_purger_repertoire.txt')) { |
|
| 147 | + if (file_exists(rtrim($dir, '/').'/inhib_purger_repertoire.txt')) { |
|
| 148 | 148 | return 0; |
| 149 | 149 | } |
| 150 | 150 | |
@@ -19,170 +19,170 @@ discard block |
||
| 19 | 19 | **/ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | function assembler($fond, string $connect = '') { |
| 26 | 26 | |
| 27 | - $cache_key = null; |
|
| 28 | - $lastmodified = null; |
|
| 29 | - $res = null; |
|
| 30 | - // flag_preserver est modifie ici, et utilise en globale |
|
| 31 | - // use_cache sert a informer le bouton d'admin pr savoir s'il met un * |
|
| 32 | - // contexte est utilise en globale dans le formulaire d'admin |
|
| 33 | - |
|
| 34 | - $GLOBALS['contexte'] = calculer_contexte(); |
|
| 35 | - $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 36 | - $page['contexte_implicite']['cache'] = $fond . preg_replace( |
|
| 37 | - ',\.[a-zA-Z0-9]*$,', |
|
| 38 | - '', |
|
| 39 | - preg_replace('/[?].*$/', '', $GLOBALS['REQUEST_URI']) |
|
| 40 | - ); |
|
| 41 | - // Cette fonction est utilisee deux fois |
|
| 42 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 43 | - // Les quatre derniers parametres sont modifies par la fonction: |
|
| 44 | - // emplacement, validite, et, s'il est valide, contenu & age |
|
| 45 | - if ($cacher) { |
|
| 46 | - $res = $cacher($GLOBALS['contexte'], $GLOBALS['use_cache'], $cache_key, $page, $lastmodified); |
|
| 47 | - } else { |
|
| 48 | - $GLOBALS['use_cache'] = -1; |
|
| 49 | - } |
|
| 50 | - // Si un resultat est retourne, c'est un message d'impossibilite |
|
| 51 | - if ($res) { |
|
| 52 | - return ['texte' => $res]; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - if (!$cache_key || !$lastmodified) { |
|
| 56 | - $lastmodified = time(); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $headers_only = ($_SERVER['REQUEST_METHOD'] == 'HEAD'); |
|
| 60 | - $calculer_page = true; |
|
| 61 | - |
|
| 62 | - // Pour les pages non-dynamiques (indiquees par #CACHE{duree,cache-client}) |
|
| 63 | - // une perennite valide a meme reponse qu'une requete HEAD (par defaut les |
|
| 64 | - // pages sont dynamiques) |
|
| 65 | - if ( |
|
| 66 | - isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 67 | - && (!defined('_VAR_MODE') || !_VAR_MODE) |
|
| 68 | - && $cache_key && isset($page['entetes']) |
|
| 69 | - && isset($page['entetes']['Cache-Control']) |
|
| 70 | - && strstr($page['entetes']['Cache-Control'], 'max-age=') |
|
| 71 | - && !strstr($_SERVER['SERVER_SOFTWARE'], 'IIS/') |
|
| 72 | - ) { |
|
| 73 | - $since = preg_replace( |
|
| 74 | - '/;.*/', |
|
| 75 | - '', |
|
| 76 | - $_SERVER['HTTP_IF_MODIFIED_SINCE'] |
|
| 77 | - ); |
|
| 78 | - $since = str_replace('GMT', '', $since); |
|
| 79 | - if (trim($since) == gmdate('D, d M Y H:i:s', $lastmodified)) { |
|
| 80 | - $page['status'] = 304; |
|
| 81 | - $headers_only = true; |
|
| 82 | - $calculer_page = false; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Si requete HEAD ou Last-modified compatible, ignorer le texte |
|
| 87 | - // et pas de content-type (pour contrer le bouton admin de inc-public) |
|
| 88 | - if (!$calculer_page) { |
|
| 89 | - $page['texte'] = ''; |
|
| 90 | - } else { |
|
| 91 | - // si la page est prise dans le cache |
|
| 92 | - if (!$GLOBALS['use_cache']) { |
|
| 93 | - // Informer les boutons d'admin du contexte |
|
| 94 | - // (fourni par urls_decoder_url ci-dessous lors de la mise en cache) |
|
| 95 | - $GLOBALS['contexte'] = $page['contexte']; |
|
| 96 | - |
|
| 97 | - // vider les globales url propres qui ne doivent plus etre utilisees en cas |
|
| 98 | - // d'inversion url => objet |
|
| 99 | - // plus necessaire si on utilise bien la fonction urls_decoder_url |
|
| 100 | - #unset($_SERVER['REDIRECT_url_propre']); |
|
| 101 | - #unset($_ENV['url_propre']); |
|
| 102 | - } else { |
|
| 103 | - // Compat ascendante : |
|
| 104 | - // 1. $contexte est global |
|
| 105 | - // (a evacuer car urls_decoder_url gere ce probleme ?) |
|
| 106 | - // et calculer la page |
|
| 107 | - if (!test_espace_prive()) { |
|
| 108 | - include_spip('inc/urls'); |
|
| 109 | - [$fond, $GLOBALS['contexte'], $url_redirect] = urls_decoder_url( |
|
| 110 | - nettoyer_uri(), |
|
| 111 | - $fond, |
|
| 112 | - $GLOBALS['contexte'], |
|
| 113 | - true |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - // squelette par defaut |
|
| 117 | - if (!strlen($fond ?? '')) { |
|
| 118 | - $fond = 'sommaire'; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // produire la page : peut mettre a jour $lastmodified |
|
| 122 | - $produire_page = charger_fonction('produire_page', 'public'); |
|
| 123 | - $page = $produire_page( |
|
| 124 | - $fond, |
|
| 125 | - $GLOBALS['contexte'], |
|
| 126 | - $GLOBALS['use_cache'], |
|
| 127 | - $cache_key, |
|
| 128 | - null, |
|
| 129 | - $page, |
|
| 130 | - $lastmodified, |
|
| 131 | - $connect |
|
| 132 | - ); |
|
| 133 | - if ($page === '') { |
|
| 134 | - $erreur = _T( |
|
| 135 | - 'info_erreur_squelette2', |
|
| 136 | - ['fichier' => spip_htmlspecialchars($fond) . '.' . _EXTENSION_SQUELETTES] |
|
| 137 | - ); |
|
| 138 | - erreur_squelette($erreur); |
|
| 139 | - // eviter des erreurs strictes ensuite sur $page['cle'] en PHP >= 5.4 |
|
| 140 | - $page = ['texte' => '', 'erreur' => $erreur]; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - if ($page && $cache_key) { |
|
| 145 | - $page['cache'] = $cache_key; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - auto_content_type($page); |
|
| 149 | - |
|
| 150 | - $GLOBALS['flag_preserver'] |= headers_sent(); |
|
| 151 | - |
|
| 152 | - // Definir les entetes si ce n'est fait |
|
| 153 | - if (!$GLOBALS['flag_preserver']) { |
|
| 154 | - // Si la page est vide, produire l'erreur 404 ou message d'erreur pour les inclusions |
|
| 155 | - if ( |
|
| 156 | - trim($page['texte']) === '' |
|
| 157 | - && _VAR_MODE !== 'debug' |
|
| 158 | - && !isset($page['entetes']['Location']) // cette page realise une redirection, donc pas d'erreur |
|
| 159 | - ) { |
|
| 160 | - $GLOBALS['contexte']['fond_erreur'] = $fond; |
|
| 161 | - $page = message_page_indisponible($page, $GLOBALS['contexte']); |
|
| 162 | - } |
|
| 163 | - // pas de cache client en mode 'observation' |
|
| 164 | - if (defined('_VAR_MODE') && _VAR_MODE) { |
|
| 165 | - $page['entetes']['Cache-Control'] = 'no-cache,must-revalidate'; |
|
| 166 | - $page['entetes']['Pragma'] = 'no-cache'; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Entete Last-Modified: |
|
| 172 | - // eviter d'etre incoherent en envoyant un lastmodified identique |
|
| 173 | - // a celui qu'on a refuse d'honorer plus haut (cf. #655) |
|
| 174 | - if ( |
|
| 175 | - $lastmodified && !isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && !isset($page['entetes']['Last-Modified']) |
|
| 176 | - ) { |
|
| 177 | - $page['entetes']['Last-Modified'] = gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT'; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // fermer la connexion apres les headers si requete HEAD |
|
| 181 | - if ($headers_only) { |
|
| 182 | - $page['entetes']['Connection'] = 'close'; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - return $page; |
|
| 27 | + $cache_key = null; |
|
| 28 | + $lastmodified = null; |
|
| 29 | + $res = null; |
|
| 30 | + // flag_preserver est modifie ici, et utilise en globale |
|
| 31 | + // use_cache sert a informer le bouton d'admin pr savoir s'il met un * |
|
| 32 | + // contexte est utilise en globale dans le formulaire d'admin |
|
| 33 | + |
|
| 34 | + $GLOBALS['contexte'] = calculer_contexte(); |
|
| 35 | + $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 36 | + $page['contexte_implicite']['cache'] = $fond . preg_replace( |
|
| 37 | + ',\.[a-zA-Z0-9]*$,', |
|
| 38 | + '', |
|
| 39 | + preg_replace('/[?].*$/', '', $GLOBALS['REQUEST_URI']) |
|
| 40 | + ); |
|
| 41 | + // Cette fonction est utilisee deux fois |
|
| 42 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 43 | + // Les quatre derniers parametres sont modifies par la fonction: |
|
| 44 | + // emplacement, validite, et, s'il est valide, contenu & age |
|
| 45 | + if ($cacher) { |
|
| 46 | + $res = $cacher($GLOBALS['contexte'], $GLOBALS['use_cache'], $cache_key, $page, $lastmodified); |
|
| 47 | + } else { |
|
| 48 | + $GLOBALS['use_cache'] = -1; |
|
| 49 | + } |
|
| 50 | + // Si un resultat est retourne, c'est un message d'impossibilite |
|
| 51 | + if ($res) { |
|
| 52 | + return ['texte' => $res]; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + if (!$cache_key || !$lastmodified) { |
|
| 56 | + $lastmodified = time(); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $headers_only = ($_SERVER['REQUEST_METHOD'] == 'HEAD'); |
|
| 60 | + $calculer_page = true; |
|
| 61 | + |
|
| 62 | + // Pour les pages non-dynamiques (indiquees par #CACHE{duree,cache-client}) |
|
| 63 | + // une perennite valide a meme reponse qu'une requete HEAD (par defaut les |
|
| 64 | + // pages sont dynamiques) |
|
| 65 | + if ( |
|
| 66 | + isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 67 | + && (!defined('_VAR_MODE') || !_VAR_MODE) |
|
| 68 | + && $cache_key && isset($page['entetes']) |
|
| 69 | + && isset($page['entetes']['Cache-Control']) |
|
| 70 | + && strstr($page['entetes']['Cache-Control'], 'max-age=') |
|
| 71 | + && !strstr($_SERVER['SERVER_SOFTWARE'], 'IIS/') |
|
| 72 | + ) { |
|
| 73 | + $since = preg_replace( |
|
| 74 | + '/;.*/', |
|
| 75 | + '', |
|
| 76 | + $_SERVER['HTTP_IF_MODIFIED_SINCE'] |
|
| 77 | + ); |
|
| 78 | + $since = str_replace('GMT', '', $since); |
|
| 79 | + if (trim($since) == gmdate('D, d M Y H:i:s', $lastmodified)) { |
|
| 80 | + $page['status'] = 304; |
|
| 81 | + $headers_only = true; |
|
| 82 | + $calculer_page = false; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Si requete HEAD ou Last-modified compatible, ignorer le texte |
|
| 87 | + // et pas de content-type (pour contrer le bouton admin de inc-public) |
|
| 88 | + if (!$calculer_page) { |
|
| 89 | + $page['texte'] = ''; |
|
| 90 | + } else { |
|
| 91 | + // si la page est prise dans le cache |
|
| 92 | + if (!$GLOBALS['use_cache']) { |
|
| 93 | + // Informer les boutons d'admin du contexte |
|
| 94 | + // (fourni par urls_decoder_url ci-dessous lors de la mise en cache) |
|
| 95 | + $GLOBALS['contexte'] = $page['contexte']; |
|
| 96 | + |
|
| 97 | + // vider les globales url propres qui ne doivent plus etre utilisees en cas |
|
| 98 | + // d'inversion url => objet |
|
| 99 | + // plus necessaire si on utilise bien la fonction urls_decoder_url |
|
| 100 | + #unset($_SERVER['REDIRECT_url_propre']); |
|
| 101 | + #unset($_ENV['url_propre']); |
|
| 102 | + } else { |
|
| 103 | + // Compat ascendante : |
|
| 104 | + // 1. $contexte est global |
|
| 105 | + // (a evacuer car urls_decoder_url gere ce probleme ?) |
|
| 106 | + // et calculer la page |
|
| 107 | + if (!test_espace_prive()) { |
|
| 108 | + include_spip('inc/urls'); |
|
| 109 | + [$fond, $GLOBALS['contexte'], $url_redirect] = urls_decoder_url( |
|
| 110 | + nettoyer_uri(), |
|
| 111 | + $fond, |
|
| 112 | + $GLOBALS['contexte'], |
|
| 113 | + true |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + // squelette par defaut |
|
| 117 | + if (!strlen($fond ?? '')) { |
|
| 118 | + $fond = 'sommaire'; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // produire la page : peut mettre a jour $lastmodified |
|
| 122 | + $produire_page = charger_fonction('produire_page', 'public'); |
|
| 123 | + $page = $produire_page( |
|
| 124 | + $fond, |
|
| 125 | + $GLOBALS['contexte'], |
|
| 126 | + $GLOBALS['use_cache'], |
|
| 127 | + $cache_key, |
|
| 128 | + null, |
|
| 129 | + $page, |
|
| 130 | + $lastmodified, |
|
| 131 | + $connect |
|
| 132 | + ); |
|
| 133 | + if ($page === '') { |
|
| 134 | + $erreur = _T( |
|
| 135 | + 'info_erreur_squelette2', |
|
| 136 | + ['fichier' => spip_htmlspecialchars($fond) . '.' . _EXTENSION_SQUELETTES] |
|
| 137 | + ); |
|
| 138 | + erreur_squelette($erreur); |
|
| 139 | + // eviter des erreurs strictes ensuite sur $page['cle'] en PHP >= 5.4 |
|
| 140 | + $page = ['texte' => '', 'erreur' => $erreur]; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + if ($page && $cache_key) { |
|
| 145 | + $page['cache'] = $cache_key; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + auto_content_type($page); |
|
| 149 | + |
|
| 150 | + $GLOBALS['flag_preserver'] |= headers_sent(); |
|
| 151 | + |
|
| 152 | + // Definir les entetes si ce n'est fait |
|
| 153 | + if (!$GLOBALS['flag_preserver']) { |
|
| 154 | + // Si la page est vide, produire l'erreur 404 ou message d'erreur pour les inclusions |
|
| 155 | + if ( |
|
| 156 | + trim($page['texte']) === '' |
|
| 157 | + && _VAR_MODE !== 'debug' |
|
| 158 | + && !isset($page['entetes']['Location']) // cette page realise une redirection, donc pas d'erreur |
|
| 159 | + ) { |
|
| 160 | + $GLOBALS['contexte']['fond_erreur'] = $fond; |
|
| 161 | + $page = message_page_indisponible($page, $GLOBALS['contexte']); |
|
| 162 | + } |
|
| 163 | + // pas de cache client en mode 'observation' |
|
| 164 | + if (defined('_VAR_MODE') && _VAR_MODE) { |
|
| 165 | + $page['entetes']['Cache-Control'] = 'no-cache,must-revalidate'; |
|
| 166 | + $page['entetes']['Pragma'] = 'no-cache'; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Entete Last-Modified: |
|
| 172 | + // eviter d'etre incoherent en envoyant un lastmodified identique |
|
| 173 | + // a celui qu'on a refuse d'honorer plus haut (cf. #655) |
|
| 174 | + if ( |
|
| 175 | + $lastmodified && !isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && !isset($page['entetes']['Last-Modified']) |
|
| 176 | + ) { |
|
| 177 | + $page['entetes']['Last-Modified'] = gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT'; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // fermer la connexion apres les headers si requete HEAD |
|
| 181 | + if ($headers_only) { |
|
| 182 | + $page['entetes']['Connection'] = 'close'; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + return $page; |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -201,27 +201,27 @@ discard block |
||
| 201 | 201 | * @return array Un tableau du contexte de la page |
| 202 | 202 | */ |
| 203 | 203 | function calculer_contexte() { |
| 204 | - static $preg_ignore_variables; |
|
| 205 | - if (empty($preg_ignore_variables)) { |
|
| 206 | - if (!defined('_CONTEXTE_IGNORE_LISTE_VARIABLES')) { |
|
| 207 | - nettoyer_uri_var(''); |
|
| 208 | - } |
|
| 209 | - $preg_ignore_variables = '/(' . implode('|',_CONTEXTE_IGNORE_LISTE_VARIABLES) . ')/'; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - $contexte = []; |
|
| 213 | - foreach ($_GET as $var => $val) { |
|
| 214 | - if (!preg_match($preg_ignore_variables, $var)) { |
|
| 215 | - $contexte[$var] = $val; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - foreach ($_POST as $var => $val) { |
|
| 219 | - if (!preg_match($preg_ignore_variables, $var)) { |
|
| 220 | - $contexte[$var] = $val; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return $contexte; |
|
| 204 | + static $preg_ignore_variables; |
|
| 205 | + if (empty($preg_ignore_variables)) { |
|
| 206 | + if (!defined('_CONTEXTE_IGNORE_LISTE_VARIABLES')) { |
|
| 207 | + nettoyer_uri_var(''); |
|
| 208 | + } |
|
| 209 | + $preg_ignore_variables = '/(' . implode('|',_CONTEXTE_IGNORE_LISTE_VARIABLES) . ')/'; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + $contexte = []; |
|
| 213 | + foreach ($_GET as $var => $val) { |
|
| 214 | + if (!preg_match($preg_ignore_variables, $var)) { |
|
| 215 | + $contexte[$var] = $val; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + foreach ($_POST as $var => $val) { |
|
| 219 | + if (!preg_match($preg_ignore_variables, $var)) { |
|
| 220 | + $contexte[$var] = $val; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return $contexte; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -232,25 +232,25 @@ discard block |
||
| 232 | 232 | * @return array |
| 233 | 233 | */ |
| 234 | 234 | function calculer_contexte_implicite() { |
| 235 | - static $notes = null; |
|
| 236 | - if (is_null($notes)) { |
|
| 237 | - $notes = charger_fonction('notes', 'inc', true); |
|
| 238 | - } |
|
| 239 | - $contexte_implicite = [ |
|
| 240 | - 'squelettes' => $GLOBALS['dossier_squelettes'], // devrait etre 'chemin' => $GLOBALS['path_sig'], ? |
|
| 241 | - 'host' => ($_SERVER['HTTP_HOST'] ?? null), |
|
| 242 | - 'https' => ($_SERVER['HTTPS'] ?? ''), |
|
| 243 | - 'espace' => test_espace_prive(), |
|
| 244 | - 'marqueur' => ($GLOBALS['marqueur'] ?? ''), |
|
| 245 | - 'marqueur_skel' => ($GLOBALS['marqueur_skel'] ?? ''), |
|
| 246 | - 'notes' => $notes ? $notes('', 'contexter_cache') : '', |
|
| 247 | - 'spip_version_code' => $GLOBALS['spip_version_code'], |
|
| 248 | - ]; |
|
| 249 | - if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 250 | - $contexte_implicite['host'] .= '|' . $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $contexte_implicite; |
|
| 235 | + static $notes = null; |
|
| 236 | + if (is_null($notes)) { |
|
| 237 | + $notes = charger_fonction('notes', 'inc', true); |
|
| 238 | + } |
|
| 239 | + $contexte_implicite = [ |
|
| 240 | + 'squelettes' => $GLOBALS['dossier_squelettes'], // devrait etre 'chemin' => $GLOBALS['path_sig'], ? |
|
| 241 | + 'host' => ($_SERVER['HTTP_HOST'] ?? null), |
|
| 242 | + 'https' => ($_SERVER['HTTPS'] ?? ''), |
|
| 243 | + 'espace' => test_espace_prive(), |
|
| 244 | + 'marqueur' => ($GLOBALS['marqueur'] ?? ''), |
|
| 245 | + 'marqueur_skel' => ($GLOBALS['marqueur_skel'] ?? ''), |
|
| 246 | + 'notes' => $notes ? $notes('', 'contexter_cache') : '', |
|
| 247 | + 'spip_version_code' => $GLOBALS['spip_version_code'], |
|
| 248 | + ]; |
|
| 249 | + if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 250 | + $contexte_implicite['host'] .= '|' . $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $contexte_implicite; |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | // |
@@ -259,56 +259,56 @@ discard block |
||
| 259 | 259 | |
| 260 | 260 | function auto_content_type($page) { |
| 261 | 261 | |
| 262 | - if (!isset($GLOBALS['flag_preserver'])) { |
|
| 263 | - $GLOBALS['flag_preserver'] = ($page && preg_match( |
|
| 264 | - '/header\s*\(\s*.content\-type:/isx', |
|
| 265 | - $page['texte'] |
|
| 266 | - ) || (isset($page['entetes']['Content-Type']))); |
|
| 267 | - } |
|
| 262 | + if (!isset($GLOBALS['flag_preserver'])) { |
|
| 263 | + $GLOBALS['flag_preserver'] = ($page && preg_match( |
|
| 264 | + '/header\s*\(\s*.content\-type:/isx', |
|
| 265 | + $page['texte'] |
|
| 266 | + ) || (isset($page['entetes']['Content-Type']))); |
|
| 267 | + } |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | function inclure_page($fond, $contexte, string $connect = '') { |
| 271 | - $use_cache = null; |
|
| 272 | - $cache_key = null; |
|
| 273 | - $lastinclude = null; |
|
| 274 | - $res = null; |
|
| 275 | - static $cacher, $produire_page; |
|
| 276 | - |
|
| 277 | - // enlever le fond de contexte inclus car sinon il prend la main |
|
| 278 | - // dans les sous inclusions -> boucle infinie d'inclusion identique |
|
| 279 | - // (cette precaution n'est probablement plus utile) |
|
| 280 | - unset($contexte['fond']); |
|
| 281 | - $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 282 | - $page['contexte_implicite']['cache'] = $fond; |
|
| 283 | - if (is_null($cacher)) { |
|
| 284 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 285 | - } |
|
| 286 | - // Les quatre derniers parametres sont modifies par la fonction: |
|
| 287 | - // emplacement, validite, et, s'il est valide, contenu & age |
|
| 288 | - if ($cacher) { |
|
| 289 | - $res = $cacher($contexte, $use_cache, $cache_key, $page, $lastinclude); |
|
| 290 | - } else { |
|
| 291 | - $use_cache = -1; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - // $res = message d'erreur : on sort de la |
|
| 295 | - if ($res) { |
|
| 296 | - return ['texte' => $res]; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - // Si use_cache ne vaut pas 0, la page doit etre calculee |
|
| 300 | - // produire la page : peut mettre a jour $lastinclude |
|
| 301 | - // le contexte_cache envoye a cacher() a ete conserve et est passe a produire |
|
| 302 | - if ($use_cache) { |
|
| 303 | - if (is_null($produire_page)) { |
|
| 304 | - $produire_page = charger_fonction('produire_page', 'public'); |
|
| 305 | - } |
|
| 306 | - $page = $produire_page($fond, $contexte, $use_cache, $cache_key, $contexte, $page, $lastinclude, $connect); |
|
| 307 | - } |
|
| 308 | - // dans tous les cas, mettre a jour $GLOBALS['lastmodified'] |
|
| 309 | - $GLOBALS['lastmodified'] = max(($GLOBALS['lastmodified'] ?? 0), $lastinclude); |
|
| 310 | - |
|
| 311 | - return $page; |
|
| 271 | + $use_cache = null; |
|
| 272 | + $cache_key = null; |
|
| 273 | + $lastinclude = null; |
|
| 274 | + $res = null; |
|
| 275 | + static $cacher, $produire_page; |
|
| 276 | + |
|
| 277 | + // enlever le fond de contexte inclus car sinon il prend la main |
|
| 278 | + // dans les sous inclusions -> boucle infinie d'inclusion identique |
|
| 279 | + // (cette precaution n'est probablement plus utile) |
|
| 280 | + unset($contexte['fond']); |
|
| 281 | + $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 282 | + $page['contexte_implicite']['cache'] = $fond; |
|
| 283 | + if (is_null($cacher)) { |
|
| 284 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 285 | + } |
|
| 286 | + // Les quatre derniers parametres sont modifies par la fonction: |
|
| 287 | + // emplacement, validite, et, s'il est valide, contenu & age |
|
| 288 | + if ($cacher) { |
|
| 289 | + $res = $cacher($contexte, $use_cache, $cache_key, $page, $lastinclude); |
|
| 290 | + } else { |
|
| 291 | + $use_cache = -1; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + // $res = message d'erreur : on sort de la |
|
| 295 | + if ($res) { |
|
| 296 | + return ['texte' => $res]; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + // Si use_cache ne vaut pas 0, la page doit etre calculee |
|
| 300 | + // produire la page : peut mettre a jour $lastinclude |
|
| 301 | + // le contexte_cache envoye a cacher() a ete conserve et est passe a produire |
|
| 302 | + if ($use_cache) { |
|
| 303 | + if (is_null($produire_page)) { |
|
| 304 | + $produire_page = charger_fonction('produire_page', 'public'); |
|
| 305 | + } |
|
| 306 | + $page = $produire_page($fond, $contexte, $use_cache, $cache_key, $contexte, $page, $lastinclude, $connect); |
|
| 307 | + } |
|
| 308 | + // dans tous les cas, mettre a jour $GLOBALS['lastmodified'] |
|
| 309 | + $GLOBALS['lastmodified'] = max(($GLOBALS['lastmodified'] ?? 0), $lastinclude); |
|
| 310 | + |
|
| 311 | + return $page; |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | /** |
@@ -326,41 +326,41 @@ discard block |
||
| 326 | 326 | * @return array |
| 327 | 327 | */ |
| 328 | 328 | function public_produire_page_dist( |
| 329 | - $fond, |
|
| 330 | - $contexte, |
|
| 331 | - $use_cache, |
|
| 332 | - $cache_key, |
|
| 333 | - $contexte_cache, |
|
| 334 | - &$page, |
|
| 335 | - &$lastinclude, |
|
| 336 | - $connect = '' |
|
| 329 | + $fond, |
|
| 330 | + $contexte, |
|
| 331 | + $use_cache, |
|
| 332 | + $cache_key, |
|
| 333 | + $contexte_cache, |
|
| 334 | + &$page, |
|
| 335 | + &$lastinclude, |
|
| 336 | + $connect = '' |
|
| 337 | 337 | ) { |
| 338 | - static $parametrer, $cacher; |
|
| 339 | - if (!$parametrer) { |
|
| 340 | - $parametrer = charger_fonction('parametrer', 'public'); |
|
| 341 | - } |
|
| 342 | - $page = $parametrer($fond, $contexte, $cache_key, $connect); |
|
| 343 | - // et on l'enregistre sur le disque |
|
| 344 | - if ( |
|
| 345 | - $cache_key |
|
| 346 | - && $use_cache > -1 |
|
| 347 | - && is_array($page) |
|
| 348 | - && count($page) |
|
| 349 | - && isset($page['entetes']['X-Spip-Cache']) |
|
| 350 | - && $page['entetes']['X-Spip-Cache'] > 0 |
|
| 351 | - ) { |
|
| 352 | - if (is_null($cacher)) { |
|
| 353 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 354 | - } |
|
| 355 | - $lastinclude = time(); |
|
| 356 | - if ($cacher) { |
|
| 357 | - $cacher($contexte_cache, $use_cache, $cache_key, $page, $lastinclude); |
|
| 358 | - } else { |
|
| 359 | - $use_cache = -1; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return $page; |
|
| 338 | + static $parametrer, $cacher; |
|
| 339 | + if (!$parametrer) { |
|
| 340 | + $parametrer = charger_fonction('parametrer', 'public'); |
|
| 341 | + } |
|
| 342 | + $page = $parametrer($fond, $contexte, $cache_key, $connect); |
|
| 343 | + // et on l'enregistre sur le disque |
|
| 344 | + if ( |
|
| 345 | + $cache_key |
|
| 346 | + && $use_cache > -1 |
|
| 347 | + && is_array($page) |
|
| 348 | + && count($page) |
|
| 349 | + && isset($page['entetes']['X-Spip-Cache']) |
|
| 350 | + && $page['entetes']['X-Spip-Cache'] > 0 |
|
| 351 | + ) { |
|
| 352 | + if (is_null($cacher)) { |
|
| 353 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 354 | + } |
|
| 355 | + $lastinclude = time(); |
|
| 356 | + if ($cacher) { |
|
| 357 | + $cacher($contexte_cache, $use_cache, $cache_key, $page, $lastinclude); |
|
| 358 | + } else { |
|
| 359 | + $use_cache = -1; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return $page; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | // Fonction inseree par le compilateur dans le code compile. |
@@ -374,14 +374,14 @@ discard block |
||
| 374 | 374 | // 4: langue |
| 375 | 375 | |
| 376 | 376 | function inserer_balise_dynamique($contexte_exec, $contexte_compil) { |
| 377 | - arguments_balise_dyn_depuis_modele(null, 'reset'); |
|
| 378 | - |
|
| 379 | - if (!is_array($contexte_exec)) { |
|
| 380 | - echo $contexte_exec; |
|
| 381 | - } // message d'erreur etc |
|
| 382 | - else { |
|
| 383 | - inclure_balise_dynamique($contexte_exec, true, $contexte_compil); |
|
| 384 | - } |
|
| 377 | + arguments_balise_dyn_depuis_modele(null, 'reset'); |
|
| 378 | + |
|
| 379 | + if (!is_array($contexte_exec)) { |
|
| 380 | + echo $contexte_exec; |
|
| 381 | + } // message d'erreur etc |
|
| 382 | + else { |
|
| 383 | + inclure_balise_dynamique($contexte_exec, true, $contexte_compil); |
|
| 384 | + } |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | /** |
@@ -394,101 +394,101 @@ discard block |
||
| 394 | 394 | * @return string|void |
| 395 | 395 | */ |
| 396 | 396 | function inclure_balise_dynamique($texte, $echo = true, $contexte_compil = []) { |
| 397 | - if (is_array($texte)) { |
|
| 398 | - [$fond, $delainc, $contexte_inclus] = $texte; |
|
| 399 | - |
|
| 400 | - // delais a l'ancienne, c'est pratiquement mort |
|
| 401 | - $d = $GLOBALS['delais'] ?? null; |
|
| 402 | - $GLOBALS['delais'] = $delainc; |
|
| 403 | - |
|
| 404 | - $page = recuperer_fond( |
|
| 405 | - $fond, |
|
| 406 | - $contexte_inclus, |
|
| 407 | - ['trim' => false, 'raw' => true, 'compil' => $contexte_compil] |
|
| 408 | - ); |
|
| 409 | - |
|
| 410 | - $texte = $page['texte']; |
|
| 411 | - |
|
| 412 | - $GLOBALS['delais'] = $d; |
|
| 413 | - // Faire remonter les entetes |
|
| 414 | - if ( |
|
| 415 | - isset($page['entetes']) |
|
| 416 | - && is_array($page['entetes']) |
|
| 417 | - ) { |
|
| 418 | - // mais pas toutes |
|
| 419 | - unset($page['entetes']['X-Spip-Cache']); |
|
| 420 | - unset($page['entetes']['Content-Type']); |
|
| 421 | - if (isset($GLOBALS['page']) && is_array($GLOBALS['page'])) { |
|
| 422 | - if (!is_array($GLOBALS['page']['entetes'])) { |
|
| 423 | - $GLOBALS['page']['entetes'] = []; |
|
| 424 | - } |
|
| 425 | - $GLOBALS['page']['entetes'] = |
|
| 426 | - array_merge($GLOBALS['page']['entetes'], $page['entetes']); |
|
| 427 | - } |
|
| 428 | - } |
|
| 429 | - // _pipelines au pluriel array('nom_pipeline' => $args...) avec une syntaxe permettant plusieurs pipelines |
|
| 430 | - if ( |
|
| 431 | - isset($page['contexte']['_pipelines']) |
|
| 432 | - && is_array($page['contexte']['_pipelines']) |
|
| 433 | - && count($page['contexte']['_pipelines']) |
|
| 434 | - ) { |
|
| 435 | - foreach ($page['contexte']['_pipelines'] as $pipe => $args) { |
|
| 436 | - $args['contexte'] = $page['contexte']; |
|
| 437 | - unset($args['contexte']['_pipelines']); // par precaution, meme si le risque de boucle infinie est a priori nul |
|
| 438 | - $texte = pipeline( |
|
| 439 | - $pipe, |
|
| 440 | - [ |
|
| 441 | - 'data' => $texte, |
|
| 442 | - 'args' => $args |
|
| 443 | - ] |
|
| 444 | - ); |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - if (defined('_VAR_MODE') && _VAR_MODE == 'debug') { |
|
| 450 | - // compatibilite : avant on donnait le numero de ligne ou rien. |
|
| 451 | - $ligne = intval($contexte_compil[3] ?? $contexte_compil); |
|
| 452 | - $GLOBALS['debug_objets']['resultat'][$ligne] = $texte; |
|
| 453 | - } |
|
| 454 | - if ($echo) { |
|
| 455 | - echo $texte; |
|
| 456 | - } else { |
|
| 457 | - return $texte; |
|
| 458 | - } |
|
| 397 | + if (is_array($texte)) { |
|
| 398 | + [$fond, $delainc, $contexte_inclus] = $texte; |
|
| 399 | + |
|
| 400 | + // delais a l'ancienne, c'est pratiquement mort |
|
| 401 | + $d = $GLOBALS['delais'] ?? null; |
|
| 402 | + $GLOBALS['delais'] = $delainc; |
|
| 403 | + |
|
| 404 | + $page = recuperer_fond( |
|
| 405 | + $fond, |
|
| 406 | + $contexte_inclus, |
|
| 407 | + ['trim' => false, 'raw' => true, 'compil' => $contexte_compil] |
|
| 408 | + ); |
|
| 409 | + |
|
| 410 | + $texte = $page['texte']; |
|
| 411 | + |
|
| 412 | + $GLOBALS['delais'] = $d; |
|
| 413 | + // Faire remonter les entetes |
|
| 414 | + if ( |
|
| 415 | + isset($page['entetes']) |
|
| 416 | + && is_array($page['entetes']) |
|
| 417 | + ) { |
|
| 418 | + // mais pas toutes |
|
| 419 | + unset($page['entetes']['X-Spip-Cache']); |
|
| 420 | + unset($page['entetes']['Content-Type']); |
|
| 421 | + if (isset($GLOBALS['page']) && is_array($GLOBALS['page'])) { |
|
| 422 | + if (!is_array($GLOBALS['page']['entetes'])) { |
|
| 423 | + $GLOBALS['page']['entetes'] = []; |
|
| 424 | + } |
|
| 425 | + $GLOBALS['page']['entetes'] = |
|
| 426 | + array_merge($GLOBALS['page']['entetes'], $page['entetes']); |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | + // _pipelines au pluriel array('nom_pipeline' => $args...) avec une syntaxe permettant plusieurs pipelines |
|
| 430 | + if ( |
|
| 431 | + isset($page['contexte']['_pipelines']) |
|
| 432 | + && is_array($page['contexte']['_pipelines']) |
|
| 433 | + && count($page['contexte']['_pipelines']) |
|
| 434 | + ) { |
|
| 435 | + foreach ($page['contexte']['_pipelines'] as $pipe => $args) { |
|
| 436 | + $args['contexte'] = $page['contexte']; |
|
| 437 | + unset($args['contexte']['_pipelines']); // par precaution, meme si le risque de boucle infinie est a priori nul |
|
| 438 | + $texte = pipeline( |
|
| 439 | + $pipe, |
|
| 440 | + [ |
|
| 441 | + 'data' => $texte, |
|
| 442 | + 'args' => $args |
|
| 443 | + ] |
|
| 444 | + ); |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + if (defined('_VAR_MODE') && _VAR_MODE == 'debug') { |
|
| 450 | + // compatibilite : avant on donnait le numero de ligne ou rien. |
|
| 451 | + $ligne = intval($contexte_compil[3] ?? $contexte_compil); |
|
| 452 | + $GLOBALS['debug_objets']['resultat'][$ligne] = $texte; |
|
| 453 | + } |
|
| 454 | + if ($echo) { |
|
| 455 | + echo $texte; |
|
| 456 | + } else { |
|
| 457 | + return $texte; |
|
| 458 | + } |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | function message_page_indisponible($page, $contexte) { |
| 462 | - static $deja = false; |
|
| 463 | - if ($deja) { |
|
| 464 | - return 'erreur'; |
|
| 465 | - } |
|
| 466 | - $codes = [ |
|
| 467 | - '404' => '404 Not Found', |
|
| 468 | - '503' => '503 Service Unavailable', |
|
| 469 | - ]; |
|
| 470 | - |
|
| 471 | - $contexte['status'] = ($page !== false) ? '404' : '503'; |
|
| 472 | - $contexte['code'] = $codes[$contexte['status']]; |
|
| 473 | - $contexte['fond'] = '404'; // gere les 2 erreurs |
|
| 474 | - if (!isset($contexte['lang'])) { |
|
| 475 | - include_spip('inc/lang'); |
|
| 476 | - $contexte['lang'] = $GLOBALS['spip_lang']; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - $deja = true; |
|
| 480 | - // passer aux plugins qui peuvent decider d'une page d'erreur plus pertinent |
|
| 481 | - // ex restriction d'acces => 401 |
|
| 482 | - $contexte = pipeline('page_indisponible', $contexte); |
|
| 483 | - |
|
| 484 | - // produire la page d'erreur |
|
| 485 | - $page = inclure_page($contexte['fond'], $contexte); |
|
| 486 | - if (!$page) { |
|
| 487 | - $page = inclure_page('404', $contexte); |
|
| 488 | - } |
|
| 489 | - $page['status'] = $contexte['status']; |
|
| 490 | - |
|
| 491 | - return $page; |
|
| 462 | + static $deja = false; |
|
| 463 | + if ($deja) { |
|
| 464 | + return 'erreur'; |
|
| 465 | + } |
|
| 466 | + $codes = [ |
|
| 467 | + '404' => '404 Not Found', |
|
| 468 | + '503' => '503 Service Unavailable', |
|
| 469 | + ]; |
|
| 470 | + |
|
| 471 | + $contexte['status'] = ($page !== false) ? '404' : '503'; |
|
| 472 | + $contexte['code'] = $codes[$contexte['status']]; |
|
| 473 | + $contexte['fond'] = '404'; // gere les 2 erreurs |
|
| 474 | + if (!isset($contexte['lang'])) { |
|
| 475 | + include_spip('inc/lang'); |
|
| 476 | + $contexte['lang'] = $GLOBALS['spip_lang']; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + $deja = true; |
|
| 480 | + // passer aux plugins qui peuvent decider d'une page d'erreur plus pertinent |
|
| 481 | + // ex restriction d'acces => 401 |
|
| 482 | + $contexte = pipeline('page_indisponible', $contexte); |
|
| 483 | + |
|
| 484 | + // produire la page d'erreur |
|
| 485 | + $page = inclure_page($contexte['fond'], $contexte); |
|
| 486 | + if (!$page) { |
|
| 487 | + $page = inclure_page('404', $contexte); |
|
| 488 | + } |
|
| 489 | + $page['status'] = $contexte['status']; |
|
| 490 | + |
|
| 491 | + return $page; |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | 494 | /** |
@@ -500,44 +500,44 @@ discard block |
||
| 500 | 500 | * @return mixed |
| 501 | 501 | */ |
| 502 | 502 | function arguments_balise_dyn_depuis_modele($arg, $operation = 'set') { |
| 503 | - static $balise_dyn_appellee_par_modele = null; |
|
| 504 | - switch ($operation) { |
|
| 505 | - case 'read': |
|
| 506 | - return $balise_dyn_appellee_par_modele; |
|
| 507 | - case 'reset': |
|
| 508 | - $balise_dyn_appellee_par_modele = null; |
|
| 509 | - return null; |
|
| 510 | - case 'set': |
|
| 511 | - default: |
|
| 512 | - $balise_dyn_appellee_par_modele = $arg; |
|
| 513 | - return $arg; |
|
| 514 | - } |
|
| 503 | + static $balise_dyn_appellee_par_modele = null; |
|
| 504 | + switch ($operation) { |
|
| 505 | + case 'read': |
|
| 506 | + return $balise_dyn_appellee_par_modele; |
|
| 507 | + case 'reset': |
|
| 508 | + $balise_dyn_appellee_par_modele = null; |
|
| 509 | + return null; |
|
| 510 | + case 'set': |
|
| 511 | + default: |
|
| 512 | + $balise_dyn_appellee_par_modele = $arg; |
|
| 513 | + return $arg; |
|
| 514 | + } |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | // temporairement ici : a mettre dans le futur inc/modeles |
| 518 | 518 | // creer_contexte_de_modele('left', 'autostart=true', ...) renvoie un array() |
| 519 | 519 | function creer_contexte_de_modele($args) { |
| 520 | - $contexte = []; |
|
| 521 | - foreach ($args as $var => $val) { |
|
| 522 | - if (is_int($var)) { // argument pas formate |
|
| 523 | - if (in_array($val, ['left', 'right', 'center'])) { |
|
| 524 | - $var = 'align'; |
|
| 525 | - $contexte[$var] = $val; |
|
| 526 | - } else { |
|
| 527 | - $args = explode('=', $val); |
|
| 528 | - if (count($args) >= 2) { // Flashvars=arg1=machin&arg2=truc genere plus de deux args |
|
| 529 | - $contexte[trim($args[0])] = substr($val, strlen($args[0]) + 1); |
|
| 530 | - } else // notation abregee |
|
| 531 | - { |
|
| 532 | - $contexte[trim($val)] = trim($val); |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - } else { |
|
| 536 | - $contexte[$var] = $val; |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - return $contexte; |
|
| 520 | + $contexte = []; |
|
| 521 | + foreach ($args as $var => $val) { |
|
| 522 | + if (is_int($var)) { // argument pas formate |
|
| 523 | + if (in_array($val, ['left', 'right', 'center'])) { |
|
| 524 | + $var = 'align'; |
|
| 525 | + $contexte[$var] = $val; |
|
| 526 | + } else { |
|
| 527 | + $args = explode('=', $val); |
|
| 528 | + if (count($args) >= 2) { // Flashvars=arg1=machin&arg2=truc genere plus de deux args |
|
| 529 | + $contexte[trim($args[0])] = substr($val, strlen($args[0]) + 1); |
|
| 530 | + } else // notation abregee |
|
| 531 | + { |
|
| 532 | + $contexte[trim($val)] = trim($val); |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + } else { |
|
| 536 | + $contexte[$var] = $val; |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + return $contexte; |
|
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | /** |
@@ -552,45 +552,45 @@ discard block |
||
| 552 | 552 | * @return string |
| 553 | 553 | */ |
| 554 | 554 | function styliser_modele($modele, $id, $contexte = null) { |
| 555 | - static $styliseurs = null; |
|
| 556 | - if (is_null($styliseurs)) { |
|
| 557 | - $tables_objet = lister_tables_objets_sql(); |
|
| 558 | - foreach ($tables_objet as $table => $desc) { |
|
| 559 | - if ( |
|
| 560 | - isset($desc['modeles']) |
|
| 561 | - && $desc['modeles'] |
|
| 562 | - && isset($desc['modeles_styliser']) |
|
| 563 | - && $desc['modeles_styliser'] |
|
| 564 | - && function_exists($desc['modeles_styliser']) |
|
| 565 | - ) { |
|
| 566 | - $primary = id_table_objet($table); |
|
| 567 | - foreach ($desc['modeles'] as $m) { |
|
| 568 | - $styliseurs[$m] = ['primary' => $primary, 'callback' => $desc['modeles_styliser']]; |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - if (isset($styliseurs[$modele])) { |
|
| 575 | - $styliseur = $styliseurs[$modele]['callback']; |
|
| 576 | - $primary = $styliseurs[$modele]['primary']; |
|
| 577 | - if (is_null($id) && $contexte) { |
|
| 578 | - if (isset($contexte['id'])) { |
|
| 579 | - $id = $contexte['id']; |
|
| 580 | - } elseif (isset($contexte[$primary])) { |
|
| 581 | - $id = $contexte[$primary]; |
|
| 582 | - } |
|
| 583 | - } |
|
| 584 | - if (is_null($id)) { |
|
| 585 | - $msg = "modeles/$modele : " . _T('zbug_parametres_inclus_incorrects', ['param' => "id/$primary"]); |
|
| 586 | - erreur_squelette($msg); |
|
| 587 | - // on passe id=0 au routeur pour tomber sur le modele par defaut et eviter une seconde erreur sur un modele inexistant |
|
| 588 | - $id = 0; |
|
| 589 | - } |
|
| 590 | - $modele = $styliseur($modele, $id); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - return $modele; |
|
| 555 | + static $styliseurs = null; |
|
| 556 | + if (is_null($styliseurs)) { |
|
| 557 | + $tables_objet = lister_tables_objets_sql(); |
|
| 558 | + foreach ($tables_objet as $table => $desc) { |
|
| 559 | + if ( |
|
| 560 | + isset($desc['modeles']) |
|
| 561 | + && $desc['modeles'] |
|
| 562 | + && isset($desc['modeles_styliser']) |
|
| 563 | + && $desc['modeles_styliser'] |
|
| 564 | + && function_exists($desc['modeles_styliser']) |
|
| 565 | + ) { |
|
| 566 | + $primary = id_table_objet($table); |
|
| 567 | + foreach ($desc['modeles'] as $m) { |
|
| 568 | + $styliseurs[$m] = ['primary' => $primary, 'callback' => $desc['modeles_styliser']]; |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + if (isset($styliseurs[$modele])) { |
|
| 575 | + $styliseur = $styliseurs[$modele]['callback']; |
|
| 576 | + $primary = $styliseurs[$modele]['primary']; |
|
| 577 | + if (is_null($id) && $contexte) { |
|
| 578 | + if (isset($contexte['id'])) { |
|
| 579 | + $id = $contexte['id']; |
|
| 580 | + } elseif (isset($contexte[$primary])) { |
|
| 581 | + $id = $contexte[$primary]; |
|
| 582 | + } |
|
| 583 | + } |
|
| 584 | + if (is_null($id)) { |
|
| 585 | + $msg = "modeles/$modele : " . _T('zbug_parametres_inclus_incorrects', ['param' => "id/$primary"]); |
|
| 586 | + erreur_squelette($msg); |
|
| 587 | + // on passe id=0 au routeur pour tomber sur le modele par defaut et eviter une seconde erreur sur un modele inexistant |
|
| 588 | + $id = 0; |
|
| 589 | + } |
|
| 590 | + $modele = $styliseur($modele, $id); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + return $modele; |
|
| 594 | 594 | } |
| 595 | 595 | |
| 596 | 596 | /** |
@@ -605,116 +605,116 @@ discard block |
||
| 605 | 605 | */ |
| 606 | 606 | function inclure_modele($type, $id, $params, $lien, string $connect = '', $env = []) { |
| 607 | 607 | |
| 608 | - static $compteur; |
|
| 609 | - if (++$compteur > 10) { |
|
| 610 | - return ''; |
|
| 611 | - } # ne pas boucler indefiniment |
|
| 612 | - |
|
| 613 | - $type = strtolower($type); |
|
| 614 | - $type = styliser_modele($type, $id); |
|
| 615 | - |
|
| 616 | - $fond = $class = ''; |
|
| 617 | - |
|
| 618 | - $params = array_filter(explode('|', $params)); |
|
| 619 | - if ($params) { |
|
| 620 | - $soustype = current($params); |
|
| 621 | - $soustype = strtolower(trim($soustype)); |
|
| 622 | - if (in_array($soustype, ['left', 'right', 'center', 'ajax'])) { |
|
| 623 | - $soustype = next($params); |
|
| 624 | - $soustype = strtolower($soustype); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - if (preg_match(',^[a-z0-9_]+$,', $soustype)) { |
|
| 628 | - if (!trouve_modele($fond = ($type . '_' . $soustype))) { |
|
| 629 | - $fond = ''; |
|
| 630 | - $class = $soustype; |
|
| 631 | - } |
|
| 632 | - // enlever le sous type des params |
|
| 633 | - $params = array_diff($params, [$soustype]); |
|
| 634 | - } |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - // Si ca marche pas en precisant le sous-type, prendre le type |
|
| 638 | - if (!$fond && !trouve_modele($fond = $type)) { |
|
| 639 | - spip_log("Modele $type introuvable", _LOG_INFO_IMPORTANTE); |
|
| 640 | - $compteur--; |
|
| 641 | - return false; |
|
| 642 | - } |
|
| 643 | - $fond = 'modeles/' . $fond; |
|
| 644 | - // Creer le contexte |
|
| 645 | - $contexte = $env; |
|
| 646 | - // securiser le contexte des modèles : tout ce qui arrive de _request() doit être sanitizé |
|
| 647 | - foreach ($contexte as $k => &$v) { |
|
| 648 | - if (!is_null(_request($k)) && (!is_scalar($v) || (_request($k) === $v))) { |
|
| 649 | - include_spip('inc/texte_mini'); |
|
| 650 | - if (is_scalar($v)) { |
|
| 651 | - $v = spip_securise_valeur_env_modele($v); |
|
| 652 | - } else { |
|
| 653 | - array_walk_recursive($v, function (&$value, $index) { |
|
| 654 | - $value = spip_securise_valeur_env_modele($value); |
|
| 655 | - }); |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $contexte['dir_racine'] = _DIR_RACINE; # eviter de mixer un cache racine et un cache ecrire (meme si pour l'instant les modeles ne sont pas caches, le resultat etant different il faut que le contexte en tienne compte |
|
| 661 | - |
|
| 662 | - // Le numero du modele est mis dans l'environnement |
|
| 663 | - // d'une part sous l'identifiant "id" |
|
| 664 | - // et d'autre part sous l'identifiant de la cle primaire |
|
| 665 | - // par la fonction id_table_objet, |
|
| 666 | - // (<article1> =>> article =>> id_article =>> id_article=1) |
|
| 667 | - $_id = id_table_objet($type); |
|
| 668 | - $contexte['id'] = $contexte[$_id] = $id; |
|
| 669 | - |
|
| 670 | - if (isset($class)) { |
|
| 671 | - $contexte['class'] = $class; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - // Si un lien a ete passe en parametre, ex: [<modele1>->url] ou [<modele1|title_du_lien{hreflang}->url] |
|
| 675 | - if ($lien) { |
|
| 676 | - # un eventuel guillemet (") sera reechappe par #ENV |
|
| 677 | - $contexte['lien'] = str_replace('"', '"', $lien['href']); |
|
| 678 | - $contexte['lien_class'] = $lien['class']; |
|
| 679 | - $contexte['lien_mime'] = $lien['mime']; |
|
| 680 | - $contexte['lien_title'] = $lien['title']; |
|
| 681 | - $contexte['lien_hreflang'] = $lien['hreflang']; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - // Traiter les parametres |
|
| 685 | - // par exemple : <img1|center>, <emb12|autostart=true> ou <doc1|lang=en> |
|
| 686 | - $arg_list = creer_contexte_de_modele($params); |
|
| 687 | - $contexte['args'] = $arg_list; // on passe la liste des arguments du modeles dans une variable args |
|
| 688 | - $contexte = array_merge($contexte, $arg_list); |
|
| 689 | - |
|
| 690 | - // Appliquer le modele avec le contexte |
|
| 691 | - $retour = recuperer_fond($fond, $contexte, [], $connect); |
|
| 692 | - |
|
| 693 | - // Regarder si le modele tient compte des liens (il *doit* alors indiquer |
|
| 694 | - // spip_lien_ok dans les classes de son conteneur de premier niveau ; |
|
| 695 | - // sinon, s'il y a un lien, on l'ajoute classiquement |
|
| 696 | - if ( |
|
| 697 | - strstr( |
|
| 698 | - ' ' . ($classes = extraire_attribut($retour, 'class')) . ' ', |
|
| 699 | - 'spip_lien_ok' |
|
| 700 | - ) |
|
| 701 | - ) { |
|
| 702 | - $retour = inserer_attribut( |
|
| 703 | - $retour, |
|
| 704 | - 'class', |
|
| 705 | - trim(str_replace(' spip_lien_ok ', ' ', " $classes ")) |
|
| 706 | - ); |
|
| 707 | - } else { |
|
| 708 | - if ($lien) { |
|
| 709 | - $retour = '<a href="' . $lien['href'] . '" class="' . $lien['class'] . '">' . $retour . '</a>'; |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - $compteur--; |
|
| 714 | - |
|
| 715 | - return (isset($arg_list['ajax']) && $arg_list['ajax'] == 'ajax') |
|
| 716 | - ? encoder_contexte_ajax($contexte, '', $retour) |
|
| 717 | - : $retour; |
|
| 608 | + static $compteur; |
|
| 609 | + if (++$compteur > 10) { |
|
| 610 | + return ''; |
|
| 611 | + } # ne pas boucler indefiniment |
|
| 612 | + |
|
| 613 | + $type = strtolower($type); |
|
| 614 | + $type = styliser_modele($type, $id); |
|
| 615 | + |
|
| 616 | + $fond = $class = ''; |
|
| 617 | + |
|
| 618 | + $params = array_filter(explode('|', $params)); |
|
| 619 | + if ($params) { |
|
| 620 | + $soustype = current($params); |
|
| 621 | + $soustype = strtolower(trim($soustype)); |
|
| 622 | + if (in_array($soustype, ['left', 'right', 'center', 'ajax'])) { |
|
| 623 | + $soustype = next($params); |
|
| 624 | + $soustype = strtolower($soustype); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + if (preg_match(',^[a-z0-9_]+$,', $soustype)) { |
|
| 628 | + if (!trouve_modele($fond = ($type . '_' . $soustype))) { |
|
| 629 | + $fond = ''; |
|
| 630 | + $class = $soustype; |
|
| 631 | + } |
|
| 632 | + // enlever le sous type des params |
|
| 633 | + $params = array_diff($params, [$soustype]); |
|
| 634 | + } |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + // Si ca marche pas en precisant le sous-type, prendre le type |
|
| 638 | + if (!$fond && !trouve_modele($fond = $type)) { |
|
| 639 | + spip_log("Modele $type introuvable", _LOG_INFO_IMPORTANTE); |
|
| 640 | + $compteur--; |
|
| 641 | + return false; |
|
| 642 | + } |
|
| 643 | + $fond = 'modeles/' . $fond; |
|
| 644 | + // Creer le contexte |
|
| 645 | + $contexte = $env; |
|
| 646 | + // securiser le contexte des modèles : tout ce qui arrive de _request() doit être sanitizé |
|
| 647 | + foreach ($contexte as $k => &$v) { |
|
| 648 | + if (!is_null(_request($k)) && (!is_scalar($v) || (_request($k) === $v))) { |
|
| 649 | + include_spip('inc/texte_mini'); |
|
| 650 | + if (is_scalar($v)) { |
|
| 651 | + $v = spip_securise_valeur_env_modele($v); |
|
| 652 | + } else { |
|
| 653 | + array_walk_recursive($v, function (&$value, $index) { |
|
| 654 | + $value = spip_securise_valeur_env_modele($value); |
|
| 655 | + }); |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $contexte['dir_racine'] = _DIR_RACINE; # eviter de mixer un cache racine et un cache ecrire (meme si pour l'instant les modeles ne sont pas caches, le resultat etant different il faut que le contexte en tienne compte |
|
| 661 | + |
|
| 662 | + // Le numero du modele est mis dans l'environnement |
|
| 663 | + // d'une part sous l'identifiant "id" |
|
| 664 | + // et d'autre part sous l'identifiant de la cle primaire |
|
| 665 | + // par la fonction id_table_objet, |
|
| 666 | + // (<article1> =>> article =>> id_article =>> id_article=1) |
|
| 667 | + $_id = id_table_objet($type); |
|
| 668 | + $contexte['id'] = $contexte[$_id] = $id; |
|
| 669 | + |
|
| 670 | + if (isset($class)) { |
|
| 671 | + $contexte['class'] = $class; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + // Si un lien a ete passe en parametre, ex: [<modele1>->url] ou [<modele1|title_du_lien{hreflang}->url] |
|
| 675 | + if ($lien) { |
|
| 676 | + # un eventuel guillemet (") sera reechappe par #ENV |
|
| 677 | + $contexte['lien'] = str_replace('"', '"', $lien['href']); |
|
| 678 | + $contexte['lien_class'] = $lien['class']; |
|
| 679 | + $contexte['lien_mime'] = $lien['mime']; |
|
| 680 | + $contexte['lien_title'] = $lien['title']; |
|
| 681 | + $contexte['lien_hreflang'] = $lien['hreflang']; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + // Traiter les parametres |
|
| 685 | + // par exemple : <img1|center>, <emb12|autostart=true> ou <doc1|lang=en> |
|
| 686 | + $arg_list = creer_contexte_de_modele($params); |
|
| 687 | + $contexte['args'] = $arg_list; // on passe la liste des arguments du modeles dans une variable args |
|
| 688 | + $contexte = array_merge($contexte, $arg_list); |
|
| 689 | + |
|
| 690 | + // Appliquer le modele avec le contexte |
|
| 691 | + $retour = recuperer_fond($fond, $contexte, [], $connect); |
|
| 692 | + |
|
| 693 | + // Regarder si le modele tient compte des liens (il *doit* alors indiquer |
|
| 694 | + // spip_lien_ok dans les classes de son conteneur de premier niveau ; |
|
| 695 | + // sinon, s'il y a un lien, on l'ajoute classiquement |
|
| 696 | + if ( |
|
| 697 | + strstr( |
|
| 698 | + ' ' . ($classes = extraire_attribut($retour, 'class')) . ' ', |
|
| 699 | + 'spip_lien_ok' |
|
| 700 | + ) |
|
| 701 | + ) { |
|
| 702 | + $retour = inserer_attribut( |
|
| 703 | + $retour, |
|
| 704 | + 'class', |
|
| 705 | + trim(str_replace(' spip_lien_ok ', ' ', " $classes ")) |
|
| 706 | + ); |
|
| 707 | + } else { |
|
| 708 | + if ($lien) { |
|
| 709 | + $retour = '<a href="' . $lien['href'] . '" class="' . $lien['class'] . '">' . $retour . '</a>'; |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + $compteur--; |
|
| 714 | + |
|
| 715 | + return (isset($arg_list['ajax']) && $arg_list['ajax'] == 'ajax') |
|
| 716 | + ? encoder_contexte_ajax($contexte, '', $retour) |
|
| 717 | + : $retour; |
|
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | /** |
@@ -728,15 +728,15 @@ discard block |
||
| 728 | 728 | * @return array|float|int|mixed|string|string[]|null |
| 729 | 729 | */ |
| 730 | 730 | function spip_securise_valeur_env_modele($valeur) { |
| 731 | - if (is_numeric($valeur) || is_bool($valeur) || is_null($valeur)) { |
|
| 732 | - return $valeur; |
|
| 733 | - } |
|
| 734 | - $valeur = (string)$valeur; |
|
| 735 | - if (str_starts_with($valeur, '@') && is_numeric(substr($valeur, 1))) { |
|
| 736 | - return $valeur; |
|
| 737 | - } |
|
| 738 | - // on laisse passer que les \w, les espaces et les -, le reste est supprimé |
|
| 739 | - return preg_replace(",[^\w\s-],", "", $valeur); |
|
| 731 | + if (is_numeric($valeur) || is_bool($valeur) || is_null($valeur)) { |
|
| 732 | + return $valeur; |
|
| 733 | + } |
|
| 734 | + $valeur = (string)$valeur; |
|
| 735 | + if (str_starts_with($valeur, '@') && is_numeric(substr($valeur, 1))) { |
|
| 736 | + return $valeur; |
|
| 737 | + } |
|
| 738 | + // on laisse passer que les \w, les espaces et les -, le reste est supprimé |
|
| 739 | + return preg_replace(",[^\w\s-],", "", $valeur); |
|
| 740 | 740 | } |
| 741 | 741 | |
| 742 | 742 | // Un inclure_page qui marche aussi pour l'espace prive |
@@ -745,99 +745,99 @@ discard block |
||
| 745 | 745 | // recuperer_fond($fond,$contexte,array('raw'=>true)) |
| 746 | 746 | function evaluer_fond($fond, $contexte = [], string $connect = '') { |
| 747 | 747 | |
| 748 | - $page = inclure_page($fond, $contexte, $connect); |
|
| 749 | - |
|
| 750 | - if (!$page) { |
|
| 751 | - return $page; |
|
| 752 | - } |
|
| 753 | - // eval $page et affecte $res |
|
| 754 | - include _ROOT_RESTREINT . 'public/evaluer_page.php'; |
|
| 755 | - |
|
| 756 | - // Lever un drapeau (global) si le fond utilise #SESSION |
|
| 757 | - // a destination de public/parametrer |
|
| 758 | - // pour remonter vers les inclusions appelantes |
|
| 759 | - // il faut bien lever ce drapeau apres avoir evalue le fond |
|
| 760 | - // pour ne pas faire descendre le flag vers les inclusions appelees |
|
| 761 | - if ( |
|
| 762 | - isset($page['invalideurs']) |
|
| 763 | - && isset($page['invalideurs']['session']) |
|
| 764 | - ) { |
|
| 765 | - $GLOBALS['cache_utilise_session'] = $page['invalideurs']['session']; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - return $page; |
|
| 748 | + $page = inclure_page($fond, $contexte, $connect); |
|
| 749 | + |
|
| 750 | + if (!$page) { |
|
| 751 | + return $page; |
|
| 752 | + } |
|
| 753 | + // eval $page et affecte $res |
|
| 754 | + include _ROOT_RESTREINT . 'public/evaluer_page.php'; |
|
| 755 | + |
|
| 756 | + // Lever un drapeau (global) si le fond utilise #SESSION |
|
| 757 | + // a destination de public/parametrer |
|
| 758 | + // pour remonter vers les inclusions appelantes |
|
| 759 | + // il faut bien lever ce drapeau apres avoir evalue le fond |
|
| 760 | + // pour ne pas faire descendre le flag vers les inclusions appelees |
|
| 761 | + if ( |
|
| 762 | + isset($page['invalideurs']) |
|
| 763 | + && isset($page['invalideurs']['session']) |
|
| 764 | + ) { |
|
| 765 | + $GLOBALS['cache_utilise_session'] = $page['invalideurs']['session']; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + return $page; |
|
| 769 | 769 | } |
| 770 | 770 | |
| 771 | 771 | |
| 772 | 772 | function page_base_href(&$texte) { |
| 773 | - static $set_html_base = null; |
|
| 774 | - if (is_null($set_html_base)) { |
|
| 775 | - if (!defined('_SET_HTML_BASE')) { |
|
| 776 | - // si la profondeur est superieure a 1 |
|
| 777 | - // est que ce n'est pas une url page ni une url action |
|
| 778 | - // activer par defaut |
|
| 779 | - $set_html_base = |
|
| 780 | - $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 781 | - && _request(_SPIP_PAGE) !== 'login' |
|
| 782 | - && !_request('action'); |
|
| 783 | - } else { |
|
| 784 | - $set_html_base = _SET_HTML_BASE; |
|
| 785 | - } |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - if ( |
|
| 789 | - $set_html_base |
|
| 790 | - && isset($GLOBALS['html']) |
|
| 791 | - && $GLOBALS['html'] |
|
| 792 | - && $GLOBALS['profondeur_url'] > 0 |
|
| 793 | - && ($poshead = strpos($texte, '</head>')) !== false |
|
| 794 | - ) { |
|
| 795 | - $head = substr($texte, 0, $poshead); |
|
| 796 | - $insert = false; |
|
| 797 | - $href_base = false; |
|
| 798 | - if (!str_contains($head, '<base')) { |
|
| 799 | - $insert = true; |
|
| 800 | - } else { |
|
| 801 | - // si aucun <base ...> n'a de href il faut en inserer un |
|
| 802 | - // sinon juste re-ecrire les ancres si besoin |
|
| 803 | - $insert = true; |
|
| 804 | - include_spip('inc/filtres'); |
|
| 805 | - $bases = extraire_balises($head, 'base'); |
|
| 806 | - foreach ($bases as $base) { |
|
| 807 | - if ($href_base = extraire_attribut($base, 'href')) { |
|
| 808 | - $insert = false; |
|
| 809 | - break; |
|
| 810 | - } |
|
| 811 | - } |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - if ($insert) { |
|
| 815 | - include_spip('inc/filtres_mini'); |
|
| 816 | - // ajouter un base qui reglera tous les liens relatifs |
|
| 817 | - $href_base = url_absolue('./'); |
|
| 818 | - $base = "\n<base href=\"$href_base\" />"; |
|
| 819 | - if (($pos = strpos($head, '<head>')) !== false) { |
|
| 820 | - $head = substr_replace($head, $base, $pos + 6, 0); |
|
| 821 | - } elseif (preg_match(',<head[^>]*>,i', $head, $r)) { |
|
| 822 | - $head = str_replace($r[0], $r[0] . $base, $head); |
|
| 823 | - } |
|
| 824 | - $texte = $head . substr($texte, $poshead); |
|
| 825 | - } |
|
| 826 | - if ($href_base) { |
|
| 827 | - // gerer les ancres |
|
| 828 | - $base = $_SERVER['REQUEST_URI']; |
|
| 829 | - // pas de guillemets ni < dans l'URL qu'on insere dans le HTML |
|
| 830 | - if (str_contains($base, "'") || str_contains($base, '"') || str_contains($base, '<')) { |
|
| 831 | - $base = str_replace(["'",'"','<'], ['%27','%22','%3C'], $base); |
|
| 832 | - } |
|
| 833 | - if (str_contains($texte, "href='#")) { |
|
| 834 | - $texte = str_replace("href='#", "href='$base#", $texte); |
|
| 835 | - } |
|
| 836 | - if (str_contains($texte, 'href="#')) { |
|
| 837 | - $texte = str_replace('href="#', "href=\"$base#", $texte); |
|
| 838 | - } |
|
| 839 | - } |
|
| 840 | - } |
|
| 773 | + static $set_html_base = null; |
|
| 774 | + if (is_null($set_html_base)) { |
|
| 775 | + if (!defined('_SET_HTML_BASE')) { |
|
| 776 | + // si la profondeur est superieure a 1 |
|
| 777 | + // est que ce n'est pas une url page ni une url action |
|
| 778 | + // activer par defaut |
|
| 779 | + $set_html_base = |
|
| 780 | + $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 781 | + && _request(_SPIP_PAGE) !== 'login' |
|
| 782 | + && !_request('action'); |
|
| 783 | + } else { |
|
| 784 | + $set_html_base = _SET_HTML_BASE; |
|
| 785 | + } |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + if ( |
|
| 789 | + $set_html_base |
|
| 790 | + && isset($GLOBALS['html']) |
|
| 791 | + && $GLOBALS['html'] |
|
| 792 | + && $GLOBALS['profondeur_url'] > 0 |
|
| 793 | + && ($poshead = strpos($texte, '</head>')) !== false |
|
| 794 | + ) { |
|
| 795 | + $head = substr($texte, 0, $poshead); |
|
| 796 | + $insert = false; |
|
| 797 | + $href_base = false; |
|
| 798 | + if (!str_contains($head, '<base')) { |
|
| 799 | + $insert = true; |
|
| 800 | + } else { |
|
| 801 | + // si aucun <base ...> n'a de href il faut en inserer un |
|
| 802 | + // sinon juste re-ecrire les ancres si besoin |
|
| 803 | + $insert = true; |
|
| 804 | + include_spip('inc/filtres'); |
|
| 805 | + $bases = extraire_balises($head, 'base'); |
|
| 806 | + foreach ($bases as $base) { |
|
| 807 | + if ($href_base = extraire_attribut($base, 'href')) { |
|
| 808 | + $insert = false; |
|
| 809 | + break; |
|
| 810 | + } |
|
| 811 | + } |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + if ($insert) { |
|
| 815 | + include_spip('inc/filtres_mini'); |
|
| 816 | + // ajouter un base qui reglera tous les liens relatifs |
|
| 817 | + $href_base = url_absolue('./'); |
|
| 818 | + $base = "\n<base href=\"$href_base\" />"; |
|
| 819 | + if (($pos = strpos($head, '<head>')) !== false) { |
|
| 820 | + $head = substr_replace($head, $base, $pos + 6, 0); |
|
| 821 | + } elseif (preg_match(',<head[^>]*>,i', $head, $r)) { |
|
| 822 | + $head = str_replace($r[0], $r[0] . $base, $head); |
|
| 823 | + } |
|
| 824 | + $texte = $head . substr($texte, $poshead); |
|
| 825 | + } |
|
| 826 | + if ($href_base) { |
|
| 827 | + // gerer les ancres |
|
| 828 | + $base = $_SERVER['REQUEST_URI']; |
|
| 829 | + // pas de guillemets ni < dans l'URL qu'on insere dans le HTML |
|
| 830 | + if (str_contains($base, "'") || str_contains($base, '"') || str_contains($base, '<')) { |
|
| 831 | + $base = str_replace(["'",'"','<'], ['%27','%22','%3C'], $base); |
|
| 832 | + } |
|
| 833 | + if (str_contains($texte, "href='#")) { |
|
| 834 | + $texte = str_replace("href='#", "href='$base#", $texte); |
|
| 835 | + } |
|
| 836 | + if (str_contains($texte, 'href="#')) { |
|
| 837 | + $texte = str_replace('href="#', "href=\"$base#", $texte); |
|
| 838 | + } |
|
| 839 | + } |
|
| 840 | + } |
|
| 841 | 841 | } |
| 842 | 842 | |
| 843 | 843 | |
@@ -847,7 +847,7 @@ discard block |
||
| 847 | 847 | * Ceux spécifiques à SPIP commencent par X-Spip |
| 848 | 848 | */ |
| 849 | 849 | function envoyer_entetes($entetes) { |
| 850 | - foreach ($entetes as $k => $v) { |
|
| 851 | - @header(strlen((string) $v) ? "$k: $v" : $k); |
|
| 852 | - } |
|
| 850 | + foreach ($entetes as $k => $v) { |
|
| 851 | + @header(strlen((string) $v) ? "$k: $v" : $k); |
|
| 852 | + } |
|
| 853 | 853 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | use Spip\Component\Hasher\Hash32; |
| 15 | 15 | |
| 16 | 16 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 17 | - return; |
|
| 17 | + return; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -23,24 +23,24 @@ discard block |
||
| 23 | 23 | * @internal Temporary fonction until DI in SPIP |
| 24 | 24 | */ |
| 25 | 25 | function cache_instance(): CacheInterface { |
| 26 | - static $cache = null; |
|
| 27 | - return $cache ??= new LimitedFilesystem('calcul', _DIR_CACHE); |
|
| 26 | + static $cache = null; |
|
| 27 | + return $cache ??= new LimitedFilesystem('calcul', _DIR_CACHE); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Returns a key cache (id) for this data |
| 32 | 32 | */ |
| 33 | 33 | function cache_key(array $contexte, array $page): string { |
| 34 | - static $hasher = null; |
|
| 35 | - $hasher ??= new Hash32(); |
|
| 36 | - return $hasher->hash([$contexte, $page]) . '.cache'; |
|
| 34 | + static $hasher = null; |
|
| 35 | + $hasher ??= new Hash32(); |
|
| 36 | + return $hasher->hash([$contexte, $page]) . '.cache'; |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Écrire le cache dans un casier |
| 41 | 41 | */ |
| 42 | 42 | function ecrire_cache(string $cache_key, array $valeur): bool { |
| 43 | - return cache_instance()->set($cache_key, ['cache_key' => $cache_key, 'valeur' => $valeur]); |
|
| 43 | + return cache_instance()->set($cache_key, ['cache_key' => $cache_key, 'valeur' => $valeur]); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * @return null|mixed null: probably cache miss |
| 50 | 50 | */ |
| 51 | 51 | function lire_cache(string $cache_key): mixed { |
| 52 | - return cache_instance()->get($cache_key)['valeur'] ?? null; |
|
| 52 | + return cache_instance()->get($cache_key)['valeur'] ?? null; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -58,16 +58,16 @@ discard block |
||
| 58 | 58 | * Parano : on signe le cache, afin d'interdire un hack d'injection dans notre memcache |
| 59 | 59 | */ |
| 60 | 60 | function cache_signature(&$page): string { |
| 61 | - if (!isset($GLOBALS['meta']['cache_signature'])) { |
|
| 62 | - include_spip('inc/acces'); |
|
| 63 | - ecrire_meta( |
|
| 64 | - 'cache_signature', |
|
| 65 | - hash('sha256', $_SERVER['DOCUMENT_ROOT'] . ($_SERVER['SERVER_SIGNATURE'] ?? '') . creer_uniqid()), |
|
| 66 | - 'non' |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return (new Hash32())->hash($GLOBALS['meta']['cache_signature'] . $page['texte']); |
|
| 61 | + if (!isset($GLOBALS['meta']['cache_signature'])) { |
|
| 62 | + include_spip('inc/acces'); |
|
| 63 | + ecrire_meta( |
|
| 64 | + 'cache_signature', |
|
| 65 | + hash('sha256', $_SERVER['DOCUMENT_ROOT'] . ($_SERVER['SERVER_SIGNATURE'] ?? '') . creer_uniqid()), |
|
| 66 | + 'non' |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return (new Hash32())->hash($GLOBALS['meta']['cache_signature'] . $page['texte']); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -79,14 +79,14 @@ discard block |
||
| 79 | 79 | * on positionne un flag gz si on comprime, pour savoir si on doit decompresser ou pas |
| 80 | 80 | */ |
| 81 | 81 | function gzip_page(array $page): array { |
| 82 | - if (function_exists('gzcompress') && strlen((string) $page['texte']) > 16 * 1024) { |
|
| 83 | - $page['gz'] = true; |
|
| 84 | - $page['texte'] = gzcompress((string) $page['texte']); |
|
| 85 | - } else { |
|
| 86 | - $page['gz'] = false; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return $page; |
|
| 82 | + if (function_exists('gzcompress') && strlen((string) $page['texte']) > 16 * 1024) { |
|
| 83 | + $page['gz'] = true; |
|
| 84 | + $page['texte'] = gzcompress((string) $page['texte']); |
|
| 85 | + } else { |
|
| 86 | + $page['gz'] = false; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return $page; |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
@@ -97,10 +97,10 @@ discard block |
||
| 97 | 97 | * de decompresser deux fois de suite un cache (ce qui echoue) |
| 98 | 98 | */ |
| 99 | 99 | function gunzip_page(array &$page): void { |
| 100 | - if ($page['gz']) { |
|
| 101 | - $page['texte'] = gzuncompress($page['texte']); |
|
| 102 | - $page['gz'] = false; // ne pas gzuncompress deux fois une meme page |
|
| 103 | - } |
|
| 100 | + if ($page['gz']) { |
|
| 101 | + $page['texte'] = gzuncompress($page['texte']); |
|
| 102 | + $page['gz'] = false; // ne pas gzuncompress deux fois une meme page |
|
| 103 | + } |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -114,72 +114,72 @@ discard block |
||
| 114 | 114 | * - -1 si il faut calculer sans stocker en cache |
| 115 | 115 | */ |
| 116 | 116 | function cache_valide(array &$page, int $date): int { |
| 117 | - $now = $_SERVER['REQUEST_TIME']; |
|
| 118 | - |
|
| 119 | - // Apparition d'un nouvel article post-date ? |
|
| 120 | - if ( |
|
| 121 | - isset($GLOBALS['meta']['post_dates']) |
|
| 122 | - && $GLOBALS['meta']['post_dates'] == 'non' |
|
| 123 | - && isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 124 | - && $now > $GLOBALS['meta']['date_prochain_postdate'] |
|
| 125 | - ) { |
|
| 126 | - spip_log('Un article post-date invalide le cache'); |
|
| 127 | - include_spip('inc/rubriques'); |
|
| 128 | - calculer_prochain_postdate(true); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if (defined('_VAR_NOCACHE') && _VAR_NOCACHE) { |
|
| 132 | - return -1; |
|
| 133 | - } |
|
| 134 | - if (isset($GLOBALS['meta']['cache_inhib']) && $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) { |
|
| 135 | - return -1; |
|
| 136 | - } |
|
| 137 | - if (defined('_NO_CACHE')) { |
|
| 138 | - return (_NO_CACHE == 0 && !isset($page['texte'])) ? 1 : _NO_CACHE; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache) |
|
| 142 | - if (!$page || !isset($page['texte']) || !isset($page['entetes']['X-Spip-Cache'])) { |
|
| 143 | - return _IS_BOT ? -1 : 1; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // controle de la signature |
|
| 147 | - if ($page['sig'] !== cache_signature($page)) { |
|
| 148 | - return _IS_BOT ? -1 : 1; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // #CACHE{n,statique} => on n'invalide pas avec derniere_modif |
|
| 152 | - // cf. ecrire/public/balises.php, balise_CACHE_dist() |
|
| 153 | - // Cache invalide par la meta 'derniere_modif' |
|
| 154 | - // sauf pour les bots, qui utilisent toujours le cache |
|
| 155 | - if ( |
|
| 156 | - (!isset($page['entetes']['X-Spip-Statique']) || $page['entetes']['X-Spip-Statique'] !== 'oui') |
|
| 157 | - && ( |
|
| 158 | - !_IS_BOT |
|
| 159 | - && $GLOBALS['derniere_modif_invalide'] |
|
| 160 | - && isset($GLOBALS['meta']['derniere_modif']) |
|
| 161 | - && $date < $GLOBALS['meta']['derniere_modif'] |
|
| 162 | - ) |
|
| 163 | - ) { |
|
| 164 | - return 1; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // Sinon comparer l'age du fichier a sa duree de cache |
|
| 168 | - $duree = (int) $page['entetes']['X-Spip-Cache']; |
|
| 169 | - $cache_mark = ($GLOBALS['meta']['cache_mark'] ?? 0); |
|
| 170 | - if ($duree == 0) { #CACHE{0} |
|
| 171 | - return -1; |
|
| 172 | - } // sauf pour les bots, qui utilisent toujours le cache |
|
| 173 | - else { |
|
| 174 | - if ( |
|
| 175 | - !_IS_BOT && $date + $duree < $now |
|
| 176 | - || $date < $cache_mark |
|
| 177 | - ) { |
|
| 178 | - return _IS_BOT ? -1 : 1; |
|
| 179 | - } else { |
|
| 180 | - return 0; |
|
| 181 | - } |
|
| 182 | - } |
|
| 117 | + $now = $_SERVER['REQUEST_TIME']; |
|
| 118 | + |
|
| 119 | + // Apparition d'un nouvel article post-date ? |
|
| 120 | + if ( |
|
| 121 | + isset($GLOBALS['meta']['post_dates']) |
|
| 122 | + && $GLOBALS['meta']['post_dates'] == 'non' |
|
| 123 | + && isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 124 | + && $now > $GLOBALS['meta']['date_prochain_postdate'] |
|
| 125 | + ) { |
|
| 126 | + spip_log('Un article post-date invalide le cache'); |
|
| 127 | + include_spip('inc/rubriques'); |
|
| 128 | + calculer_prochain_postdate(true); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if (defined('_VAR_NOCACHE') && _VAR_NOCACHE) { |
|
| 132 | + return -1; |
|
| 133 | + } |
|
| 134 | + if (isset($GLOBALS['meta']['cache_inhib']) && $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) { |
|
| 135 | + return -1; |
|
| 136 | + } |
|
| 137 | + if (defined('_NO_CACHE')) { |
|
| 138 | + return (_NO_CACHE == 0 && !isset($page['texte'])) ? 1 : _NO_CACHE; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache) |
|
| 142 | + if (!$page || !isset($page['texte']) || !isset($page['entetes']['X-Spip-Cache'])) { |
|
| 143 | + return _IS_BOT ? -1 : 1; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // controle de la signature |
|
| 147 | + if ($page['sig'] !== cache_signature($page)) { |
|
| 148 | + return _IS_BOT ? -1 : 1; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // #CACHE{n,statique} => on n'invalide pas avec derniere_modif |
|
| 152 | + // cf. ecrire/public/balises.php, balise_CACHE_dist() |
|
| 153 | + // Cache invalide par la meta 'derniere_modif' |
|
| 154 | + // sauf pour les bots, qui utilisent toujours le cache |
|
| 155 | + if ( |
|
| 156 | + (!isset($page['entetes']['X-Spip-Statique']) || $page['entetes']['X-Spip-Statique'] !== 'oui') |
|
| 157 | + && ( |
|
| 158 | + !_IS_BOT |
|
| 159 | + && $GLOBALS['derniere_modif_invalide'] |
|
| 160 | + && isset($GLOBALS['meta']['derniere_modif']) |
|
| 161 | + && $date < $GLOBALS['meta']['derniere_modif'] |
|
| 162 | + ) |
|
| 163 | + ) { |
|
| 164 | + return 1; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // Sinon comparer l'age du fichier a sa duree de cache |
|
| 168 | + $duree = (int) $page['entetes']['X-Spip-Cache']; |
|
| 169 | + $cache_mark = ($GLOBALS['meta']['cache_mark'] ?? 0); |
|
| 170 | + if ($duree == 0) { #CACHE{0} |
|
| 171 | + return -1; |
|
| 172 | + } // sauf pour les bots, qui utilisent toujours le cache |
|
| 173 | + else { |
|
| 174 | + if ( |
|
| 175 | + !_IS_BOT && $date + $duree < $now |
|
| 176 | + || $date < $cache_mark |
|
| 177 | + ) { |
|
| 178 | + return _IS_BOT ? -1 : 1; |
|
| 179 | + } else { |
|
| 180 | + return 0; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -193,58 +193,58 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | function creer_cache(&$page, &$cache_key) { |
| 195 | 195 | |
| 196 | - // Ne rien faire si on est en preview, debug, ou si une erreur |
|
| 197 | - // grave s'est presentee (compilation du squelette, MySQL, etc) |
|
| 198 | - // le cas var_nocache ne devrait jamais arriver ici (securite) |
|
| 199 | - // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable |
|
| 200 | - if ( |
|
| 201 | - defined('_VAR_NOCACHE') && _VAR_NOCACHE |
|
| 202 | - || defined('spip_interdire_cache') |
|
| 203 | - ) { |
|
| 204 | - return; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // Si la page a un invalideur de session, utiliser un cache_key spécifique |
|
| 208 | - if ( |
|
| 209 | - isset($page['invalideurs']) |
|
| 210 | - && isset($page['invalideurs']['session']) |
|
| 211 | - ) { |
|
| 212 | - // on verifie que le contenu du chemin cache indique seulement |
|
| 213 | - // "cache sessionne" ; sa date indique la date de validite |
|
| 214 | - // des caches sessionnes |
|
| 215 | - if (!$tmp = lire_cache($cache_key)) { |
|
| 216 | - spip_log('Creation cache sessionne ' . $cache_key); |
|
| 217 | - $tmp = [ |
|
| 218 | - 'invalideurs' => ['session' => ''], |
|
| 219 | - 'lastmodified' => $_SERVER['REQUEST_TIME'] |
|
| 220 | - ]; |
|
| 221 | - ecrire_cache($cache_key, $tmp); |
|
| 222 | - } |
|
| 223 | - $cache_key = cache_key( |
|
| 224 | - ['cache_key' => $cache_key], |
|
| 225 | - ['session' => $page['invalideurs']['session']] |
|
| 226 | - ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // ajouter la date de production dans le cache lui meme |
|
| 230 | - // (qui contient deja sa duree de validite) |
|
| 231 | - $page['lastmodified'] = $_SERVER['REQUEST_TIME']; |
|
| 232 | - |
|
| 233 | - // compresser le contenu si besoin |
|
| 234 | - $pagez = gzip_page($page); |
|
| 235 | - |
|
| 236 | - // signer le contenu |
|
| 237 | - $pagez['sig'] = cache_signature($pagez); |
|
| 238 | - |
|
| 239 | - // l'enregistrer, compresse ou non... |
|
| 240 | - $ok = ecrire_cache($cache_key, $pagez); |
|
| 241 | - |
|
| 242 | - spip_log((_IS_BOT ? 'Bot:' : '') . "Creation du cache $cache_key pour " |
|
| 243 | - . $page['entetes']['X-Spip-Cache'] . ' secondes' . ($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 244 | - |
|
| 245 | - // Inserer ses invalideurs |
|
| 246 | - include_spip('inc/invalideur'); |
|
| 247 | - maj_invalideurs($cache_key, $page); |
|
| 196 | + // Ne rien faire si on est en preview, debug, ou si une erreur |
|
| 197 | + // grave s'est presentee (compilation du squelette, MySQL, etc) |
|
| 198 | + // le cas var_nocache ne devrait jamais arriver ici (securite) |
|
| 199 | + // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable |
|
| 200 | + if ( |
|
| 201 | + defined('_VAR_NOCACHE') && _VAR_NOCACHE |
|
| 202 | + || defined('spip_interdire_cache') |
|
| 203 | + ) { |
|
| 204 | + return; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // Si la page a un invalideur de session, utiliser un cache_key spécifique |
|
| 208 | + if ( |
|
| 209 | + isset($page['invalideurs']) |
|
| 210 | + && isset($page['invalideurs']['session']) |
|
| 211 | + ) { |
|
| 212 | + // on verifie que le contenu du chemin cache indique seulement |
|
| 213 | + // "cache sessionne" ; sa date indique la date de validite |
|
| 214 | + // des caches sessionnes |
|
| 215 | + if (!$tmp = lire_cache($cache_key)) { |
|
| 216 | + spip_log('Creation cache sessionne ' . $cache_key); |
|
| 217 | + $tmp = [ |
|
| 218 | + 'invalideurs' => ['session' => ''], |
|
| 219 | + 'lastmodified' => $_SERVER['REQUEST_TIME'] |
|
| 220 | + ]; |
|
| 221 | + ecrire_cache($cache_key, $tmp); |
|
| 222 | + } |
|
| 223 | + $cache_key = cache_key( |
|
| 224 | + ['cache_key' => $cache_key], |
|
| 225 | + ['session' => $page['invalideurs']['session']] |
|
| 226 | + ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // ajouter la date de production dans le cache lui meme |
|
| 230 | + // (qui contient deja sa duree de validite) |
|
| 231 | + $page['lastmodified'] = $_SERVER['REQUEST_TIME']; |
|
| 232 | + |
|
| 233 | + // compresser le contenu si besoin |
|
| 234 | + $pagez = gzip_page($page); |
|
| 235 | + |
|
| 236 | + // signer le contenu |
|
| 237 | + $pagez['sig'] = cache_signature($pagez); |
|
| 238 | + |
|
| 239 | + // l'enregistrer, compresse ou non... |
|
| 240 | + $ok = ecrire_cache($cache_key, $pagez); |
|
| 241 | + |
|
| 242 | + spip_log((_IS_BOT ? 'Bot:' : '') . "Creation du cache $cache_key pour " |
|
| 243 | + . $page['entetes']['X-Spip-Cache'] . ' secondes' . ($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 244 | + |
|
| 245 | + // Inserer ses invalideurs |
|
| 246 | + include_spip('inc/invalideur'); |
|
| 247 | + maj_invalideurs($cache_key, $page); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -272,132 +272,132 @@ discard block |
||
| 272 | 272 | */ |
| 273 | 273 | function public_cacher_dist($contexte, &$use_cache, &$cache_key, &$page, &$lastmodified) { |
| 274 | 274 | |
| 275 | - # fonction de cache minimale : dire "non on ne met rien en cache" |
|
| 276 | - # $use_cache = -1; return; |
|
| 277 | - |
|
| 278 | - // Second appel, destine a l'enregistrement du cache sur le disque |
|
| 279 | - if (isset($cache_key)) { |
|
| 280 | - creer_cache($page, $cache_key); |
|
| 281 | - return; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - // Toute la suite correspond au premier appel |
|
| 285 | - $contexte_implicite = $page['contexte_implicite']; |
|
| 286 | - |
|
| 287 | - // Cas ignorant le cache car completement dynamique |
|
| 288 | - if ( |
|
| 289 | - !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' |
|
| 290 | - || _request('connect') |
|
| 291 | - ) { |
|
| 292 | - $use_cache = -1; |
|
| 293 | - $lastmodified = 0; |
|
| 294 | - $cache_key = ''; |
|
| 295 | - $page = []; |
|
| 296 | - |
|
| 297 | - return; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - // Controler l'existence d'un cache nous correspondant |
|
| 301 | - $cache_key = cache_key($contexte, $page); |
|
| 302 | - $lastmodified = 0; |
|
| 303 | - |
|
| 304 | - // charger le cache s'il existe (et si il a bien le bon hash = anticollision) |
|
| 305 | - if (!$page = lire_cache($cache_key)) { |
|
| 306 | - $page = []; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - // s'il est sessionne, charger celui correspondant a notre session |
|
| 310 | - if ( |
|
| 311 | - isset($page['invalideurs']) |
|
| 312 | - && isset($page['invalideurs']['session']) |
|
| 313 | - ) { |
|
| 314 | - $cache_key_session = cache_key( |
|
| 315 | - ['cache_key' => $cache_key], |
|
| 316 | - ['session' => spip_session()] |
|
| 317 | - ); |
|
| 318 | - if ( |
|
| 319 | - ($page_session = lire_cache($cache_key_session)) && $page_session['lastmodified'] >= $page['lastmodified'] |
|
| 320 | - ) { |
|
| 321 | - $page = $page_session; |
|
| 322 | - } else { |
|
| 323 | - $page = []; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ? |
|
| 329 | - // ne le faire que si la base est disponible |
|
| 330 | - $invalider = false; |
|
| 331 | - if (isset($GLOBALS['meta']['invalider']) && spip_connect()) { |
|
| 332 | - $invalider = true; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande, |
|
| 336 | - // on supprime le cache |
|
| 337 | - if ( |
|
| 338 | - defined('_VAR_MODE') |
|
| 339 | - && _VAR_MODE |
|
| 340 | - && ( |
|
| 341 | - isset($_COOKIE['spip_session']) |
|
| 342 | - || isset($_COOKIE['spip_admin']) |
|
| 343 | - || @file_exists(_ACCESS_FILE_NAME) |
|
| 344 | - ) |
|
| 345 | - ) { |
|
| 346 | - $page = ['contexte_implicite' => $contexte_implicite]; // ignorer le cache deja lu |
|
| 347 | - $invalider = true; |
|
| 348 | - } |
|
| 349 | - if ($invalider) { |
|
| 350 | - include_spip('inc/invalideur'); |
|
| 351 | - retire_caches($cache_key); # API invalideur inutile |
|
| 352 | - cache_instance()->delete($cache_key); |
|
| 353 | - if (isset($cache_key_session) && $cache_key_session) { |
|
| 354 | - cache_instance()->delete($cache_key_session); |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // $delais par defaut |
|
| 359 | - // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé |
|
| 360 | - // qui sont a cache nul par defaut |
|
| 361 | - if (!isset($GLOBALS['delais'])) { |
|
| 362 | - if (!defined('_DUREE_CACHE_DEFAUT')) { |
|
| 363 | - define('_DUREE_CACHE_DEFAUT', 24 * 3600); |
|
| 364 | - } |
|
| 365 | - $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - // determiner la validite de la page |
|
| 369 | - if ($page) { |
|
| 370 | - $use_cache = cache_valide($page, $page['lastmodified'] ?? 0); |
|
| 371 | - // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence |
|
| 372 | - // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul |
|
| 373 | - $page['contexte_implicite'] = $contexte_implicite; |
|
| 374 | - if (!$use_cache) { |
|
| 375 | - // $page est un cache utilisable |
|
| 376 | - gunzip_page($page); |
|
| 377 | - |
|
| 378 | - return; |
|
| 379 | - } |
|
| 380 | - } else { |
|
| 381 | - $page = ['contexte_implicite' => $contexte_implicite]; |
|
| 382 | - $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Si pas valide mais pas de connexion a la base, le garder quand meme |
|
| 386 | - if (!spip_connect()) { |
|
| 387 | - if (isset($page['texte'])) { |
|
| 388 | - gunzip_page($page); |
|
| 389 | - $use_cache = 0; |
|
| 390 | - } else { |
|
| 391 | - spip_log("Erreur base de donnees, impossible utiliser $cache_key"); |
|
| 392 | - include_spip('inc/minipres'); |
|
| 393 | - |
|
| 394 | - return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - if ($use_cache < 0) { |
|
| 399 | - $cache_key = ''; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - return; |
|
| 275 | + # fonction de cache minimale : dire "non on ne met rien en cache" |
|
| 276 | + # $use_cache = -1; return; |
|
| 277 | + |
|
| 278 | + // Second appel, destine a l'enregistrement du cache sur le disque |
|
| 279 | + if (isset($cache_key)) { |
|
| 280 | + creer_cache($page, $cache_key); |
|
| 281 | + return; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + // Toute la suite correspond au premier appel |
|
| 285 | + $contexte_implicite = $page['contexte_implicite']; |
|
| 286 | + |
|
| 287 | + // Cas ignorant le cache car completement dynamique |
|
| 288 | + if ( |
|
| 289 | + !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' |
|
| 290 | + || _request('connect') |
|
| 291 | + ) { |
|
| 292 | + $use_cache = -1; |
|
| 293 | + $lastmodified = 0; |
|
| 294 | + $cache_key = ''; |
|
| 295 | + $page = []; |
|
| 296 | + |
|
| 297 | + return; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + // Controler l'existence d'un cache nous correspondant |
|
| 301 | + $cache_key = cache_key($contexte, $page); |
|
| 302 | + $lastmodified = 0; |
|
| 303 | + |
|
| 304 | + // charger le cache s'il existe (et si il a bien le bon hash = anticollision) |
|
| 305 | + if (!$page = lire_cache($cache_key)) { |
|
| 306 | + $page = []; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + // s'il est sessionne, charger celui correspondant a notre session |
|
| 310 | + if ( |
|
| 311 | + isset($page['invalideurs']) |
|
| 312 | + && isset($page['invalideurs']['session']) |
|
| 313 | + ) { |
|
| 314 | + $cache_key_session = cache_key( |
|
| 315 | + ['cache_key' => $cache_key], |
|
| 316 | + ['session' => spip_session()] |
|
| 317 | + ); |
|
| 318 | + if ( |
|
| 319 | + ($page_session = lire_cache($cache_key_session)) && $page_session['lastmodified'] >= $page['lastmodified'] |
|
| 320 | + ) { |
|
| 321 | + $page = $page_session; |
|
| 322 | + } else { |
|
| 323 | + $page = []; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ? |
|
| 329 | + // ne le faire que si la base est disponible |
|
| 330 | + $invalider = false; |
|
| 331 | + if (isset($GLOBALS['meta']['invalider']) && spip_connect()) { |
|
| 332 | + $invalider = true; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande, |
|
| 336 | + // on supprime le cache |
|
| 337 | + if ( |
|
| 338 | + defined('_VAR_MODE') |
|
| 339 | + && _VAR_MODE |
|
| 340 | + && ( |
|
| 341 | + isset($_COOKIE['spip_session']) |
|
| 342 | + || isset($_COOKIE['spip_admin']) |
|
| 343 | + || @file_exists(_ACCESS_FILE_NAME) |
|
| 344 | + ) |
|
| 345 | + ) { |
|
| 346 | + $page = ['contexte_implicite' => $contexte_implicite]; // ignorer le cache deja lu |
|
| 347 | + $invalider = true; |
|
| 348 | + } |
|
| 349 | + if ($invalider) { |
|
| 350 | + include_spip('inc/invalideur'); |
|
| 351 | + retire_caches($cache_key); # API invalideur inutile |
|
| 352 | + cache_instance()->delete($cache_key); |
|
| 353 | + if (isset($cache_key_session) && $cache_key_session) { |
|
| 354 | + cache_instance()->delete($cache_key_session); |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // $delais par defaut |
|
| 359 | + // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé |
|
| 360 | + // qui sont a cache nul par defaut |
|
| 361 | + if (!isset($GLOBALS['delais'])) { |
|
| 362 | + if (!defined('_DUREE_CACHE_DEFAUT')) { |
|
| 363 | + define('_DUREE_CACHE_DEFAUT', 24 * 3600); |
|
| 364 | + } |
|
| 365 | + $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + // determiner la validite de la page |
|
| 369 | + if ($page) { |
|
| 370 | + $use_cache = cache_valide($page, $page['lastmodified'] ?? 0); |
|
| 371 | + // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence |
|
| 372 | + // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul |
|
| 373 | + $page['contexte_implicite'] = $contexte_implicite; |
|
| 374 | + if (!$use_cache) { |
|
| 375 | + // $page est un cache utilisable |
|
| 376 | + gunzip_page($page); |
|
| 377 | + |
|
| 378 | + return; |
|
| 379 | + } |
|
| 380 | + } else { |
|
| 381 | + $page = ['contexte_implicite' => $contexte_implicite]; |
|
| 382 | + $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Si pas valide mais pas de connexion a la base, le garder quand meme |
|
| 386 | + if (!spip_connect()) { |
|
| 387 | + if (isset($page['texte'])) { |
|
| 388 | + gunzip_page($page); |
|
| 389 | + $use_cache = 0; |
|
| 390 | + } else { |
|
| 391 | + spip_log("Erreur base de donnees, impossible utiliser $cache_key"); |
|
| 392 | + include_spip('inc/minipres'); |
|
| 393 | + |
|
| 394 | + return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + if ($use_cache < 0) { |
|
| 399 | + $cache_key = ''; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + return; |
|
| 403 | 403 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | function cache_key(array $contexte, array $page): string { |
| 34 | 34 | static $hasher = null; |
| 35 | 35 | $hasher ??= new Hash32(); |
| 36 | - return $hasher->hash([$contexte, $page]) . '.cache'; |
|
| 36 | + return $hasher->hash([$contexte, $page]).'.cache'; |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
@@ -62,12 +62,12 @@ discard block |
||
| 62 | 62 | include_spip('inc/acces'); |
| 63 | 63 | ecrire_meta( |
| 64 | 64 | 'cache_signature', |
| 65 | - hash('sha256', $_SERVER['DOCUMENT_ROOT'] . ($_SERVER['SERVER_SIGNATURE'] ?? '') . creer_uniqid()), |
|
| 65 | + hash('sha256', $_SERVER['DOCUMENT_ROOT'].($_SERVER['SERVER_SIGNATURE'] ?? '').creer_uniqid()), |
|
| 66 | 66 | 'non' |
| 67 | 67 | ); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - return (new Hash32())->hash($GLOBALS['meta']['cache_signature'] . $page['texte']); |
|
| 70 | + return (new Hash32())->hash($GLOBALS['meta']['cache_signature'].$page['texte']); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | // "cache sessionne" ; sa date indique la date de validite |
| 214 | 214 | // des caches sessionnes |
| 215 | 215 | if (!$tmp = lire_cache($cache_key)) { |
| 216 | - spip_log('Creation cache sessionne ' . $cache_key); |
|
| 216 | + spip_log('Creation cache sessionne '.$cache_key); |
|
| 217 | 217 | $tmp = [ |
| 218 | 218 | 'invalideurs' => ['session' => ''], |
| 219 | 219 | 'lastmodified' => $_SERVER['REQUEST_TIME'] |
@@ -239,8 +239,8 @@ discard block |
||
| 239 | 239 | // l'enregistrer, compresse ou non... |
| 240 | 240 | $ok = ecrire_cache($cache_key, $pagez); |
| 241 | 241 | |
| 242 | - spip_log((_IS_BOT ? 'Bot:' : '') . "Creation du cache $cache_key pour " |
|
| 243 | - . $page['entetes']['X-Spip-Cache'] . ' secondes' . ($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 242 | + spip_log((_IS_BOT ? 'Bot:' : '')."Creation du cache $cache_key pour " |
|
| 243 | + . $page['entetes']['X-Spip-Cache'].' secondes'.($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 244 | 244 | |
| 245 | 245 | // Inserer ses invalideurs |
| 246 | 246 | include_spip('inc/invalideur'); |