Spaces:
Configuration error

File size: 9,598 Bytes
8df07b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
```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>
```