36766-vm/profile.php
Flatlogic Bot 6c14b2436f 2.0
2025-12-18 09:40:37 +00:00

96 lines
3.7 KiB
PHP

<?php
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$pdo = db();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$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)) {
$error = 'All password fields are required.';
} elseif ($new_password !== $confirm_password) {
$error = 'New password and confirm password do not match.';
} else {
$stmt = $pdo->prepare("SELECT password_hash FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if ($user && password_verify($current_password, $user['password_hash'])) {
$password_hash = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password_hash = ? WHERE id = ?");
if ($stmt->execute([$password_hash, $user_id])) {
$success = 'Password updated successfully.';
} else {
$error = 'Failed to update password.';
}
} else {
$error = 'Incorrect current password.';
}
}
}
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
?>
<div class="container">
<h1 class="mt-5">Profile</h1>
<div class="card">
<div class="card-body">
<h5 class="card-title">Your Information</h5>
<p><strong>Username:</strong> <?php echo htmlspecialchars($user['username']); ?></p>
<?php if (!$user['is_admin']): ?>
<?php
$stmt_dealer = $pdo->prepare("SELECT * FROM dealers WHERE id = ?");
$stmt_dealer->execute([$user['dealer_id']]);
$dealer = $stmt_dealer->fetch();
?>
<p><strong>Dealer:</strong> <?php echo htmlspecialchars($dealer['name']); ?></p>
<p><strong>Email:</strong> <?php echo htmlspecialchars($dealer['email']); ?></p>
<?php endif; ?>
</div>
</div>
<div class="card mt-4">
<div class="card-body">
<h5 class="card-title">Change Password</h5>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<form method="POST" action="profile.php">
<div class="form-group">
<label for="current_password">Current Password</label>
<input type="password" class="form-control" id="current_password" name="current_password" required>
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" class="form-control" id="new_password" name="new_password" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
</div>
<button type="submit" class="btn btn-primary">Change Password</button>
</form>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>