Files
ms1inscription-v5/php/inc_cookie_consent.php
stephan 19679875df Implement cookie consent management across various scripts
This commit introduces a comprehensive cookie consent management system by integrating the `fxCookiesAllowMarketing()` function across multiple files. It ensures that marketing-related scripts, such as Facebook Pixel and Google Analytics, are only executed if the user has consented to cookies. Additionally, it enhances the cookie consent UI in `inc_footer.php` and `inc_header.php`, improving user experience and compliance with privacy regulations. The changes also include refactoring of cookie handling functions for better maintainability.
2026-06-27 14:44:44 -04:00

88 lines
2.4 KiB
PHP

<?php
function fxCookiesConsentAccepted(): bool
{
return isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true';
}
function fxCookiesConsentRejected(): bool
{
return isset($_COOKIE['cookiesRejected']) && $_COOKIE['cookiesRejected'] === 'true';
}
function fxCookiesConsentPending(): bool
{
return !fxCookiesConsentAccepted() && !fxCookiesConsentRejected();
}
function fxCookiesAllowMarketing(): bool
{
return fxCookiesConsentAccepted();
}
function fxCookiesConsentResetIfRequested(): void
{
if (!isset($_GET['cookies'])) {
return;
}
$past = time() - 3600;
setcookie('cookiesAccepted', '', $past, '/');
setcookie('cookiesRejected', '', $past, '/');
unset($_COOKIE['cookiesAccepted'], $_COOKIE['cookiesRejected']);
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: '/';
$query = [];
if (!empty($_SERVER['QUERY_STRING'])) {
parse_str($_SERVER['QUERY_STRING'], $query);
}
unset($query['cookies']);
$location = $path;
if ($query) {
$location .= '?' . http_build_query($query);
}
header('Location: ' . $location);
exit;
}
function fxCookieConsentText(string $clef, string $strLangue): string
{
$defaults = [
'experience_cookies' => [
'fr' => 'Nous utilisons des témoins (cookies) pour assurer le bon fonctionnement du site et, avec votre consentement, mesurer l\'audience. Vous pouvez accepter ou refuser les témoins non essentiels sans affecter votre inscription.',
'en' => 'We use cookies to ensure the site works properly and, with your consent, measure traffic. You may accept or refuse non-essential cookies without affecting your registration.',
],
'bouton_acceper_cookies' => [
'fr' => 'Tout accepter',
'en' => 'Accept all',
],
'bouton_refuser_cookies' => [
'fr' => 'Tout refuser',
'en' => 'Refuse all',
],
'politique_cookie' => [
'fr' => 'Politique de témoins',
'en' => 'Cookie policy',
],
];
$lang = ($strLangue === 'en') ? 'en' : 'fr';
$fallback = $defaults[$clef][$lang] ?? $clef;
if (!function_exists('afficheTexte')) {
return $fallback;
}
$text = afficheTexte($clef, 0, 1, 1);
if ($text === '' || $text === $clef || $text === '*' . $clef . '*') {
return $fallback;
}
return $text;
}