Files
maria_uslugi/public/client/compte.php
T

162 lines
6.3 KiB
PHP

<?php
require_once __DIR__ . '/../includes/init_client.php';
$success = $erreur = '';
$id = $_SESSION['compte_id'];
$stmt = $pdo->prepare("SELECT email, mot_de_passe FROM compte WHERE id = ?");
$stmt->execute([$id]);
$compte = $stmt->fetch();
if (! $compte) {
die($translations['compte_introuvable']);
}
$email_actuel = $compte['email'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_post_csrf();
$nouvel_email = trim($_POST['email'] ?? '');
$mdpActuel = $_POST['mot_de_passe_actuel'] ?? '';
$mdp1 = $_POST['mot_de_passe'] ?? '';
$mdp2 = $_POST['confirm_mot_de_passe'] ?? '';
if (! password_verify($mdpActuel, (string) ($compte['mot_de_passe'] ?? ''))) {
$erreur = $translations['erreur_mdp_actuel_incorrect'] ?? 'Mot de passe actuel incorrect.';
} elseif (! filter_var($nouvel_email, FILTER_VALIDATE_EMAIL)) {
$erreur = $translations['erreur_email_non_valide'];
} elseif ($mdp1 && ($mdp1 !== $mdp2 || strlen($mdp1) < 8)) {
$erreur = $translations['erreur_mdp_invalides'];
} else {
$stmt = $pdo->prepare("SELECT id FROM compte WHERE email = ? AND id != ?");
$stmt->execute([$nouvel_email, $id]);
if ($stmt->fetch()) {
$erreur = $translations['err_email_deja_utilise'];
} else {
if ($mdp1) {
$hash = password_hash($mdp1, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE compte SET email = ?, mot_de_passe = ? WHERE id = ?");
$stmt->execute([$nouvel_email, $hash, $id]);
} else {
$stmt = $pdo->prepare("UPDATE compte SET email = ? WHERE id = ?");
$stmt->execute([$nouvel_email, $id]);
}
$success = $translations['success_compte_mis_a_jour'];
$email_actuel = $nouvel_email;
// garder la valeur en mémoire pour la suite de la page
$compte['mot_de_passe'] = $mdp1 ? $hash : $compte['mot_de_passe'];
}
}
}
require_once '../includes/header.php';
?>
<div class="main1">
<div class="container py-5">
<?php if ($erreur): ?>
<div class="alert alert-danger"><?php echo $erreur ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success ?></div>
<?php endif; ?>
<form method="POST" class="shadow p-4 rounded bg-light">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(ensure_csrf_token(), ENT_QUOTES, 'UTF-8') ?>">
<h2 class="text-center mb-4"> <?php echo $translations['mes_infos_connexion'] ?></h2>
<div class="mb-3">
<label class="form-label"><?php echo $translations['label_email'] ?></label>
<input type="email" name="email" value="<?php echo htmlspecialchars($email_actuel) ?>" required class="form-control">
</div>
<div class="mb-3">
<label class="form-label"><?php echo htmlspecialchars($translations['label_mdp_actuel'] ?? 'Mot de passe actuel') ?></label>
<div class="field-wrapper">
<input type="password" name="mot_de_passe_actuel" id="mot_de_passe_actuel" class="form-control"
placeholder="<?php echo htmlspecialchars($translations['placeholder_mdp_actuel'] ?? 'Entrez votre mot de passe actuel') ?>" required>
<span class="toggle-password" onclick="toggle('mot_de_passe_actuel')">👁️</span>
</div>
</div>
<div class="mb-3">
<label class="form-label"><?php echo $translations['label_nouveau_mdp_facultatif'] ?></label>
<div class="field-wrapper">
<input type="password" name="mot_de_passe" id="mot_de_passe" placeholder="<?php echo $translations['nouveau_mdp_placeholder'] ?>" class="form-control">
<span class="toggle-password" onclick="toggle('mot_de_passe')">👁️</span>
</div>
</div>
<div class="strength-bar mb-1">
<div class="strength-fill"></div>
<div class="strength-fill"></div>
<div class="strength-fill"></div>
</div>
<div id="niveau-securite" class="mb-3 text-muted"><?php echo $translations['securite_indetermine'] ?></div>
<div class="mb-4">
<label class="form-label"><?php echo $translations['label_confirmer_mdp'] ?></label>
<div class="field-wrapper">
<input type="password" name="confirm_mot_de_passe" id="confirm_mot_de_passe" placeholder="<?php echo $translations['confirm_mdp_placeholder'] ?>" class="form-control">
<span class="toggle-password" onclick="toggle('confirm_mot_de_passe')">👁️</span>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary"> <?php echo $translations['btn_mettre_a_jour'] ?></button>
</div>
</form>
</div>
<script>
function toggle(id) {
const el = document.getElementById(id);
el.type = el.type === 'password' ? 'text' : 'password';
}
document.getElementById('mot_de_passe').addEventListener('input', function () {
const val = this.value;
const segments = document.querySelectorAll(".strength-fill");
const niveauText = document.getElementById("niveau-securite");
segments.forEach(s => s.style.backgroundColor = '#e0e0e0');
niveauText.textContent = <?php echo json_encode($translations['securite_indetermine']) ?>;
niveauText.className = 'text-muted';
const longOk = val.length >= 8;
const lettre = /[A-Za-z]/.test(val);
const chiffre = /\d/.test(val);
const maj = /[A-Z]/.test(val);
const special = /[^A-Za-z0-9]/.test(val);
let force = 0;
if (longOk && lettre && chiffre) force++;
if (maj) force++;
if (special) force++;
if (force === 1) {
segments[0].style.backgroundColor = 'red';
niveauText.textContent = <?php echo json_encode($translations['securite_faible']) ?>;
niveauText.className = 'text-danger';
} else if (force === 2) {
segments[0].style.backgroundColor = 'orange';
segments[1].style.backgroundColor = 'orange';
niveauText.textContent = <?php echo json_encode($translations['securite_moyen']) ?>;
niveauText.className = 'text-warning';
} else if (force >= 3) {
segments.forEach(s => s.style.backgroundColor = 'green');
niveauText.textContent = <?php echo json_encode($translations['securite_fort']) ?>;
niveauText.className = 'text-success';
}
});
</script>
</div>
<footer class="site-footer">
<div class="container">
<p><?php echo $translations['copyr'] ?></p>
</div>
</footer>
</body>
</html>