Spaces:
Configuration error

lost-found-tracker-pro / profile.php
brucelloyd1's picture
You are a professional full-stack web developer assistant.
8df07b6 verified
Raw History Blame Contribute Delete
9.6 kB
```php
<?php
include 'includes/auth.php';
redirectIfNotLoggedIn();
include 'config/database.php';
$db = new Database();
$connection = $db->getConnection();
$userId = $_SESSION['user_id'];
$isAdmin = isAdmin();
// Get user profile
if (!$isAdmin) {
$query = "SELECT * FROM users WHERE userId = :userId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$query = "SELECT * FROM admins WHERE adminId = :adminId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':adminId', $_SESSION['admin_id']);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
}
// Handle profile update
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['update_profile'])) {
$name = $_POST['name'];
$email = $_POST['email'];
if ($isAdmin) {
$query = "UPDATE admins SET name = :name, username = :email WHERE adminId = :adminId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':adminId', $_SESSION['admin_id']);
} else {
$query = "UPDATE users SET name = :name, email = :email WHERE userId = :userId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':userId', $userId);
}
if ($stmt->execute()) {
$_SESSION['user_name'] = $name;
if (!$isAdmin) $_SESSION['email'] = $email;
$success = "Profile updated successfully!";
} else {
$error = "Error updating profile.";
}
} elseif (isset($_POST['change_password'])) {
$currentPassword = $_POST['current_password'];
$newPassword = $_POST['new_password'];
$confirmPassword = $_POST['confirm_password'];
if ($newPassword !== $confirmPassword) {
$error = "New passwords do not match.";
} else {
if ($isAdmin) {
// For demo, using simple password check
if ($currentPassword === 'admin123') {
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
$query = "UPDATE admins SET passwordHash = :password WHERE adminId = :adminId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':password', $hashedPassword);
$stmt->bindParam(':adminId', $_SESSION['admin_id']);
if ($stmt->execute()) {
$success = "Password changed successfully!";
} else {
$error = "Error changing password.";
}
} else {
$error = "Current password is incorrect.";
}
} else {
// For demo, using simple password check
if ($currentPassword === 'user123') {
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
$query = "UPDATE users SET passwordHash = :password WHERE userId = :userId";
$stmt = $connection->prepare($query);
$stmt->bindParam(':password', $hashedPassword);
$stmt->bindParam(':userId', $userId);
if ($stmt->execute()) {
$success = "Password changed successfully!";
} else {
$error = "Error changing password.";
}
} else {
$error = "Current password is incorrect.";
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile - Lost & Found</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<div class="container">
<nav>
<div class="logo">Lost & Found Pro</div>
<ul class="nav-links">
<li><a href="home.php">Home</a></li>
<li><a href="items.php">Browse Items</a></li>
<li><a href="dashboard.php">Dashboard</a></li>
<?php if (isAdmin()): ?>
<li><a href="admin.php">Admin</a></li>
<li><a href="verifications.php">Verifications</a></li>
<li><a href="storage.php">Storage</a></li>
<?php endif; ?>
<?php if (isFinder()): ?>
<li><a href="report_item.php">Report Item</a></li>
<?php endif; ?>
<li><a href="notifications.php">Notifications</a></li>
<li><a href="profile.php">Profile</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</div>
</header>
<main class="container">
<div style="max-width: 600px; margin: 2rem auto;">
<div class="card">
<div class="card-header">
<h2>Profile Settings</h2>
</div>
<?php if (isset($success)): ?>
<div style="background: #D1FAE5; color: #065F46; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
<?php echo $success; ?>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div style="background: #FEE2E2; color: #DC2626; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
<?php echo $error; ?>
</div>
<?php endif; ?>
<!-- Profile Update Form -->
<form method="POST">
<h3>Update Profile Information</h3>
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" id="name" name="name" class="form-control"
value="<?php echo htmlspecialchars($user['name']); ?>" required>
</div>
<div class="form-group">
<label for="email"><?php echo $isAdmin ? 'Username' : 'Email'; ?></label>
<input type="<?php echo $isAdmin ? 'text' : 'email'; ?>" id="email" name="email" class="form-control"
value="<?php echo htmlspecialchars($isAdmin ? $user['username'] : $user['email']); ?>" required>
</div>
<?php if (!$isAdmin): ?>
<div class="form-group">
<label>Role</label>
<input type="text" class="form-control" value="<?php echo ucfirst($user['role']); ?>" disabled>
</div>
<?php endif; ?>
<button type="submit" name="update_profile" class="btn btn-primary">Update Profile</button>
</form>
<hr class="my-4">
<!-- Password Change Form -->
<form method="POST">
<h3>Change Password</h3>
<div class="form-group">
<label for="current_password">Current Password</label>
<input type="password" id="current_password" name="current_password" class="form-control" required>
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" id="new_password" name="new_password" class="form-control" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" id="confirm_password" name="confirm_password" class="form-control" required>
</div>
<button type="submit" name="change_password" class="btn btn-primary">Change Password</button>
</form>
<div class="mt-4 p-3 bg-gray-100 rounded">
<h4>Demo Password Information</h4>
<p><strong>Current Demo Passwords:</strong></p>
<?php if ($isAdmin): ?>
<p>Admin: <code>admin123</code></p>
<?php else: ?>
<p>User: <code>user123</code></p>
<?php endif; ?>
<p class="text-sm text-gray-600">In a production environment, these would be properly hashed and verified.</p>
</div>
</div>
</div>
</main>
<footer>
<div class="container">
<div class="footer-content">
<div class="logo">Lost & Found Pro</div>
<p>&copy; 2024 Lost & Found Management System. All rights reserved.</p>
</div>
</div>
</footer>
</body>
</html>
```