103 lines
3.0 KiB
PHP
103 lines
3.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
/**
|
|
* /public/includes/verifier_abonnement.php
|
|
*
|
|
* Rôle: praticien uniquement
|
|
* - Définit $alerte_abonnement (string|null) pour affichage dans header
|
|
* - Peut rediriger si abonnement expiré (selon règles)
|
|
*
|
|
* Pré-requis: session démarrée, $pdo dispo (via bootstrap_global.php / init_auth.php)
|
|
*/
|
|
|
|
$alerte_abonnement = null;
|
|
|
|
// --- Guard: uniquement praticien
|
|
$role = (string)($_SESSION['role'] ?? '');
|
|
if ($role !== 'praticien') {
|
|
return;
|
|
}
|
|
|
|
// --- PDO
|
|
$pdo = $GLOBALS['pdo'] ?? null;
|
|
if (!($pdo instanceof PDO)) {
|
|
return;
|
|
}
|
|
|
|
// --- IDs
|
|
$praticienId = (int)($_SESSION['reference_id'] ?? 0);
|
|
if ($praticienId <= 0) {
|
|
return;
|
|
}
|
|
|
|
// --- Traductions (fallbacks)
|
|
$translations = $GLOBALS['translations'] ?? [];
|
|
$t_expire = (string)($translations['alerte_abonnement_expire'] ?? "Votre abonnement est expiré.");
|
|
$t_resilie = (string)($translations['alerte_abonnement_resilie'] ?? "Votre abonnement est résilié. Il se termine le {date}.");
|
|
|
|
// --- Récupère abonnement
|
|
$stmt = $pdo->prepare("SELECT abonnement_id, date_expiration FROM praticien WHERE id = ?");
|
|
$stmt->execute([$praticienId]);
|
|
$p = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
|
|
|
|
$abonnement_id = $p['abonnement_id'] ?? null;
|
|
|
|
// date_expiration -> timestamp (0 si absent/invalide)
|
|
$expiration_ts = 0;
|
|
if (!empty($p['date_expiration'])) {
|
|
$ts = strtotime((string)$p['date_expiration']);
|
|
$expiration_ts = ($ts !== false) ? $ts : 0;
|
|
}
|
|
|
|
$now_ts = time();
|
|
|
|
// --- Pages autorisées même si expiré
|
|
$page_actuelle = basename((string)($_SERVER['PHP_SELF'] ?? ''));
|
|
$pages_autorisees = [
|
|
'choisir_abonnement.php',
|
|
'souscrire_abonnement.php',
|
|
'resiliation.php',
|
|
'resiliation_action.php',
|
|
'deconnexion.php',
|
|
'paiement_simule.php',
|
|
'create_checkout.php',
|
|
];
|
|
|
|
// --- État abonnement
|
|
$is_expired = ($expiration_ts > 0 && $expiration_ts < $now_ts);
|
|
|
|
// ⚠️ Cas "résilié mais encore actif": abonnement_id NULL mais expiration future
|
|
$is_cancelled_but_active = (!$abonnement_id && $expiration_ts >= $now_ts && $expiration_ts > 0);
|
|
|
|
// --- Bannière (affichage)
|
|
if ($is_cancelled_but_active) {
|
|
$alerte_abonnement = strtr($t_resilie, [
|
|
'{date}' => date('d/m/Y', $expiration_ts),
|
|
]);
|
|
} elseif ($is_expired) {
|
|
$alerte_abonnement = $t_expire;
|
|
}
|
|
|
|
// (Optionnel) expose aussi un état global utile
|
|
$GLOBALS['abonnement_state'] = [
|
|
'abonnement_id' => $abonnement_id,
|
|
'expiration_ts' => $expiration_ts,
|
|
'is_expired' => $is_expired,
|
|
'is_cancelled_but_active' => $is_cancelled_but_active,
|
|
];
|
|
|
|
// --- Redirection si expiré (uniquement si expiration connue)
|
|
if ($is_expired && !in_array($page_actuelle, $pages_autorisees, true)) {
|
|
|
|
// Important: éviter "headers already sent"
|
|
if (!headers_sent()) {
|
|
header('Location: /public/choisir_abonnement.php', true, 302);
|
|
exit;
|
|
}
|
|
|
|
// Si headers déjà envoyés, on ne peut pas header() -> fallback JS
|
|
echo '<script>location.href="/public/choisir_abonnement.php";</script>';
|
|
exit;
|
|
}
|
|
|