Files
maria_uslugi/includes/accepter_cookies.php
T
2026-03-05 10:25:39 +01:00

140 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// htdocs/includes/accepter_cookies.php — CookieConsent v3 (correctif)
// Si tu as des traductions dans lang/fr.php / lang/ru.php, on les lit (optionnel)
$lang = $_SESSION['lang'] ?? 'fr';
$langFile = __DIR__ . "/../lang/{$lang}.php";
if (is_file($langFile)) { include $langFile; }
$t = isset($translations) && is_array($translations) ? $translations : [];
function tt($k,$d=''){ global $t; return htmlspecialchars($t[$k]??$d, ENT_QUOTES, 'UTF-8'); }
?>
<!--<a id="cc-manage" href="#" style="display:none"><?= tt('cookies_manage_link','Paramètres cookies') ?></a>-->
<link id="cc-css" rel="stylesheet" href="/js/cookieconsent.css"
onerror="this.href='https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@3.1.0/dist/cookieconsent.css'">
<script>
(function(){
// Textes (prend ceux de tes fichiers de langue si présents)
const TXT = {
title: <?= json_encode($t['cookies_title'] ?? 'Cookies & confidentialité') ?>,
desc: <?= json_encode($t['cookies_desc'] ?? "Nous utilisons des cookies essentiels. Avec votre accord, nous activons aussi l'analyse et la personnalisation.") ?>,
acceptAll: <?= json_encode($t['cookies_accept_all'] ?? 'Tout accepter') ?>,
rejectAll: <?= json_encode($t['cookies_reject_all'] ?? 'Tout refuser') ?>,
settings: <?= json_encode($t['cookies_settings'] ?? 'Personnaliser') ?>,
necessary: <?= json_encode($t['cookies_necessary'] ?? 'Essentiels (toujours actifs)') ?>,
analytics: <?= json_encode($t['cookies_analytics'] ?? 'Mesure d’audience') ?>,
personalization: <?= json_encode($t['cookies_personalization'] ?? 'Personnalisation') ?>,
save: <?= json_encode($t['cookies_save'] ?? 'Enregistrer') ?>,
};
const SRCS = [
'/js/cookieconsent.umd.js', // local d'abord
'https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@3.1.0/dist/cookieconsent.umd.js',
'https://unpkg.com/vanilla-cookieconsent@3.1.0/dist/cookieconsent.umd.js'
];
function loadScriptSequential(i){
if(i >= SRCS.length){ console.error('[CC] Impossible de charger la lib'); return; }
const s = document.createElement('script');
s.src = SRCS[i];
s.onload = function(){
// v3 => CookieConsent.run(...)
if (window.CookieConsent && typeof CookieConsent.run === 'function'){ initCC(); }
else { console.warn('[CC] Script chargé mais API v3 absente → essai suivant'); loadScriptSequential(i+1); }
};
s.onerror = function(){ console.warn('[CC] Échec chargement', SRCS[i]); loadScriptSequential(i+1); };
document.head.appendChild(s);
}
function initCC(){
// Sécurité
if (!(window.CookieConsent && typeof CookieConsent.run === 'function')){
console.error('[CC] CookieConsent.run indisponible'); return;
}
CookieConsent.run({
cookie: {
name: 'cc_consent_v3',
path: '/',
sameSite: 'Lax',
expiresAfterDays: 180
},
guiOptions: {
consentModal: { layout: 'cloud', position: 'bottom center', equalWeightButtons: false, flipButtons: false },
preferencesModal: { layout: 'box', equalWeightButtons: true, flipButtons: false }
},
// v3: définir "categories" + "language" (obligatoires)
categories: {
necessary: { enabled: true, readOnly: true },
analytics: {},
personalization: {}
},
language: {
default: 'xx',
translations: {
xx: {
consentModal: {
title: TXT.title,
description: TXT.desc,
acceptAllBtn: TXT.acceptAll,
acceptNecessaryBtn: TXT.rejectAll,
showPreferencesBtn: TXT.settings
},
preferencesModal: {
title: TXT.title,
acceptAllBtn: TXT.acceptAll,
acceptNecessaryBtn: TXT.rejectAll,
savePreferencesBtn: TXT.save,
sections: [
{ title: TXT.necessary, description: '' },
{ title: TXT.analytics, description: '', linkedCategory: 'analytics' },
{ title: TXT.personalization, description: '', linkedCategory: 'personalization' }
]
}
}
}
},
// Hooks
onConsent: ({cookie}) => {
if (CookieConsent.acceptedCategory('analytics')) enableAnalytics();
},
onChange: ({changedCategories}) => {
if (changedCategories.includes('analytics')) {
CookieConsent.acceptedCategory('analytics') ? enableAnalytics() : disableAnalytics();
}
}
});
// Lien pour rouvrir
const manage = document.getElementById('cc-manage');
if (manage) {
manage.style.display = 'inline';
manage.addEventListener('click', (e)=>{ e.preventDefault(); CookieConsent.showPreferences(); });
}
}
// Tes hooks perso (ex: activer GA seulement si analytics accepté)
window.enableAnalytics = function(){
if (window.__gaLoaded) return; window.__gaLoaded = true;
// Exemple:
CookieConsent.loadScript('https://www.googletagmanager.com/gtag/js?id=G-XXXX').then(()=>{
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)}; gtag('js', new Date()); gtag('config','G-XXXX');
});
};
window.disableAnalytics = function(){ /* éventuellement reset */ };
// Lance le chargement (local → CDN fallback)
loadScriptSequential(0);
})();
</script>
<style>
#cc--main .cm__btn, #cc--main .pm__btn { border-radius: 999px; }
#cc-manage { position: fixed; right: 12px; bottom: 12px; font-size: 12px; opacity: .7; z-index: 99999; }
</style>