137 lines
6.1 KiB
PHP
137 lines
6.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
$pdo = db();
|
|
$update_name_error = '';
|
|
$update_name_success = '';
|
|
$update_password_error = '';
|
|
$update_password_success = '';
|
|
|
|
// Obter dados do usuário
|
|
$stmt = $pdo->prepare("SELECT name, email FROM users WHERE id = :id");
|
|
$stmt->execute(['id' => $user_id]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// Lógica para lidar com o POST
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Atualizar Nome
|
|
if (isset($_POST['update_name'])) {
|
|
$name = $_POST['name'] ?? '';
|
|
if (empty($name)) {
|
|
$update_name_error = 'O nome não pode estar em branco.';
|
|
} else {
|
|
try {
|
|
$stmt = $pdo->prepare("UPDATE users SET name = :name WHERE id = :id");
|
|
$stmt->execute(['name' => $name, 'id' => $user_id]);
|
|
$user['name'] = $name; // Atualizar o nome na página
|
|
$update_name_success = 'Nome atualizado com sucesso!';
|
|
} catch (PDOException $e) {
|
|
$update_name_error = 'Erro ao atualizar o nome.';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Atualizar Senha
|
|
if (isset($_POST['update_password'])) {
|
|
$current_password = $_POST['current_password'] ?? '';
|
|
$new_password = $_POST['new_password'] ?? '';
|
|
$confirm_password = $_POST['confirm_password'] ?? '';
|
|
|
|
if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
|
|
$update_password_error = 'Todos os campos de senha são obrigatórios.';
|
|
} elseif ($new_password !== $confirm_password) {
|
|
$update_password_error = 'A nova senha e a confirmação não correspondem.';
|
|
} else {
|
|
try {
|
|
// Verificar senha atual
|
|
$stmt = $pdo->prepare("SELECT password_hash FROM users WHERE id = :id");
|
|
$stmt->execute(['id' => $user_id]);
|
|
$hash = $stmt->fetchColumn();
|
|
|
|
if (password_verify($current_password, $hash)) {
|
|
// Se a senha atual estiver correta, criar novo hash e atualizar
|
|
$new_hash = password_hash($new_password, PASSWORD_DEFAULT);
|
|
$stmt_update = $pdo->prepare("UPDATE users SET password_hash = :hash WHERE id = :id");
|
|
$stmt_update->execute(['hash' => $new_hash, 'id' => $user_id]);
|
|
$update_password_success = 'Senha atualizada com sucesso!';
|
|
} else {
|
|
$update_password_error = 'A senha atual está incorreta.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$update_password_error = 'Erro ao atualizar a senha.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
include __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<h1 class="mb-4">Meu Perfil</h1>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">Atualizar Informações</div>
|
|
<div class="card-body">
|
|
<?php if ($update_name_success): ?><div class="alert alert-success"><?php echo $update_name_success; ?></div><?php endif; ?>
|
|
<?php if ($update_name_error): ?><div class="alert alert-danger"><?php echo $update_name_error; ?></div><?php endif; ?>
|
|
|
|
<form method="POST" action="profile.php">
|
|
<input type="hidden" name="update_name" value="1">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Nome</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">E-mail</label>
|
|
<input type="email" class="form-control" id="email" value="<?php echo htmlspecialchars($user['email']); ?>" disabled>
|
|
<div class="form-text">O e-mail não pode ser alterado.</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Salvar Nome</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">Alterar Senha</div>
|
|
<div class="card-body">
|
|
<?php if ($update_password_success): ?><div class="alert alert-success"><?php echo $update_password_success; ?></div><?php endif; ?>
|
|
<?php if ($update_password_error): ?><div class="alert alert-danger"><?php echo $update_password_error; ?></div><?php endif; ?>
|
|
|
|
<form method="POST" action="profile.php">
|
|
<input type="hidden" name="update_password" value="1">
|
|
<div class="mb-3">
|
|
<label for="current_password" class="form-label">Senha Atual</label>
|
|
<input type="password" class="form-control" id="current_password" name="current_password" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="new_password" class="form-label">Nova Senha</label>
|
|
<input type="password" class="form-control" id="new_password" name="new_password" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="confirm_password" class="form-label">Confirmar Nova Senha</label>
|
|
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Alterar Senha</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|