prepare("SELECT * FROM invitations WHERE token=:t AND status='envoyee' LIMIT 1"); $q->execute(['t'=>$token]); $inv = $q->fetch(PDO::FETCH_ASSOC); } if (!$inv) { echo '
'.h($translations['inv_token_invalide'] ?? 'Lien invalide.').'
'; exit; } if (!empty($inv['expires_at']) && new DateTime($inv['expires_at']) < new DateTime()) { echo '
'.h($translations['inv_token_expire'] ?? 'Lien expiré.').'
'; exit; } $email = strtolower((string)$inv['email']); $praticien_id = (int)$inv['praticien_id']; $connectedRole = strtolower((string)($_SESSION['role'] ?? '')); $connectedId = (int)($_SESSION['compte_id'] ?? 0); $connectedEmail= strtolower((string)($_SESSION['email'] ?? '')); $done = false; $msg = ''; // ===================================================== // CAS 1 : Déjà connecté comme secrétaire // ===================================================== if ($connectedRole === 'secretaire' && $connectedId > 0) { if ($connectedEmail === $email) { // rattachement au praticien si pas encore lié $chk = $pdo->prepare("SELECT id FROM secretaire_praticien WHERE secretaire_user_id=:u AND praticien_id=:p"); $chk->execute(['u'=>$connectedId,'p'=>$praticien_id]); if (!$chk->fetch()) { $ins = $pdo->prepare("INSERT INTO secretaire_praticien (secretaire_user_id, praticien_id, can_manage_rdv, can_edit_slots, is_active) VALUES (:u,:p,1,1,1)"); $ins->execute(['u'=>$connectedId,'p'=>$praticien_id]); } $pdo->prepare("UPDATE invitations SET status='acceptee' WHERE id=:id")->execute(['id'=>$inv['id']]); $done = true; } else { // autre email → doit se connecter avec le bon compte $msg = sprintf( $translations['inv_email_diff'] ?? "Vous êtes connecté avec %s, mais l’invitation est pour %s. Veuillez vous connecter avec le bon compte.", h($connectedEmail), h($email) ); } } // ===================================================== // CAS 2 : POST (création compte si inexistant) // ===================================================== elseif (($_SERVER['REQUEST_METHOD']??'')==='POST') { $pwd = (string)($_POST['pwd'] ?? ''); $pwd2 = (string)($_POST['pwd2'] ?? ''); if (strlen($pwd) < 8 || $pwd !== $pwd2) { $msg = $translations['inv_pwd_invalid'] ?? 'Mot de passe invalide ou non identique.'; } else { // Vérifier si un compte existe déjà avec ce mail $chk = $pdo->prepare("SELECT id, role FROM compte WHERE email=:e LIMIT 1"); $chk->execute(['e'=>$email]); $acc = $chk->fetch(PDO::FETCH_ASSOC); if ($acc) { if ($acc['role'] !== 'secretaire') { $msg = $translations['inv_email_occupe'] ?? "Un compte avec cet email existe déjà avec un autre rôle."; } else { // compte secrétaire déjà existant → rediriger vers login AVEC token header("Location: /connexion.php?next=" . urlencode("/public/cabinet/accepter_invitation.php?token=".$token)); exit; } } else { // création nouveau compte secrétaire $ins = $pdo->prepare("INSERT INTO compte (email, mot_de_passe, role, reference_id, token_validation, email_valide) VALUES (:e, :pwd, 'secretaire', 0, NULL, 1)"); $ins->execute([ 'e' => $email, 'pwd' => password_hash($pwd, PASSWORD_BCRYPT) ]); $newId = (int)$pdo->lastInsertId(); $_SESSION['compte_id'] = $newId; $_SESSION['role'] = 'secretaire'; $_SESSION['reference_id'] = 0; $_SESSION['email'] = $email; $pdo->prepare("INSERT INTO secretaire_praticien (secretaire_user_id, praticien_id, can_manage_rdv, can_edit_slots, is_active) VALUES (:u,:p,1,1,1)") ->execute(['u'=>$newId,'p'=>$praticien_id]); $pdo->prepare("UPDATE invitations SET status='acceptee' WHERE id=:id")->execute(['id'=>$inv['id']]); $done = true; } } } // ===================================================== // CAS 3 : GET initial (ni connecté ni posté) // Vérifier si compte existe déjà // ===================================================== else { $chk = $pdo->prepare("SELECT id, role FROM compte WHERE email=:e LIMIT 1"); $chk->execute(['e'=>$email]); $acc = $chk->fetch(PDO::FETCH_ASSOC); if ($acc && $acc['role']==='secretaire') { echo '
'. h($translations['inv_compte_existe'] ?? 'Un compte secrétaire existe déjà, veuillez vous connecter.'). '
Connexion
'; exit; } } ?>

:

: #

👁️
👁️