File size: 12,406 Bytes
102fe5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
// Include database connection and authentication check
require_once 'includes/db_connect.php';
require_once 'includes/auth_check.php';

// Redirect to login if not logged in
requireLogin();

$error = '';
$success = '';
$user = null;

// Get user information
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM users WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 1) {
    $user = $result->fetch_assoc();
} else {
    $error = "User not found.";
}

// Process profile update form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
    $email = $conn->real_escape_string(trim($_POST['email']));
    
    // Validate email
    if (empty($email)) {
        $error = "Email is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Please enter a valid email address.";
    } else {
        // Check if email is already used by another user
        $check_query = "SELECT id FROM users WHERE email = ? AND id != ?";
        $check_stmt = $conn->prepare($check_query);
        $check_stmt->bind_param("si", $email, $user_id);
        $check_stmt->execute();
        $check_result = $check_stmt->get_result();
        
        if ($check_result->num_rows > 0) {
            $error = "Email is already in use by another account.";
        } else {
            // Update user profile
            $update_query = "UPDATE users SET email = ? WHERE id = ?";
            $update_stmt = $conn->prepare($update_query);
            $update_stmt->bind_param("si", $email, $user_id);
            
            if ($update_stmt->execute()) {
                $_SESSION['email'] = $email;
                $success = "Profile updated successfully.";
                
                // Refresh user data
                $stmt->execute();
                $result = $stmt->get_result();
                $user = $result->fetch_assoc();
            } else {
                $error = "Failed to update profile: " . $conn->error;
            }
        }
    }
}

// Process password change form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) {
    $current_password = trim($_POST['current_password']);
    $new_password = trim($_POST['new_password']);
    $confirm_password = trim($_POST['confirm_password']);
    
    // Validate inputs
    if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
        $error = "All password fields are required.";
    } elseif (strlen($new_password) < 6) {
        $error = "New password must be at least 6 characters long.";
    } elseif ($new_password !== $confirm_password) {
        $error = "New passwords do not match.";
    } elseif (!password_verify($current_password, $user['password'])) {
        $error = "Current password is incorrect.";
    } else {
        // Update password
        $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
        $update_query = "UPDATE users SET password = ? WHERE id = ?";
        $update_stmt = $conn->prepare($update_query);
        $update_stmt->bind_param("si", $hashed_password, $user_id);
        
        if ($update_stmt->execute()) {
            $success = "Password changed successfully.";
        } else {
            $error = "Failed to change password: " . $conn->error;
        }
    }
}

// Include header
include 'includes/header.php';
?>

<div class="row mb-4">

    <div class="col-md-12">

        <nav aria-label="breadcrumb">

            <ol class="breadcrumb">

                <li class="breadcrumb-item"><a href="index.php">Home</a></li>

                <li class="breadcrumb-item active">My Profile</li>

            </ol>

        </nav>

        <h2><i class="fas fa-id-card me-2"></i> My Profile</h2>

        <p class="lead">Manage your account settings and password</p>

    </div>

</div>



<?php if (!empty($error)): ?>

    <div class="alert alert-danger alert-dismissible fade show" role="alert">

        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>

        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

    </div>

<?php endif; ?>



<?php if (!empty($success)): ?>

    <div class="alert alert-success alert-dismissible fade show" role="alert">

        <i class="fas fa-check-circle me-2"></i> <?php echo $success; ?>

        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

    </div>

<?php endif; ?>



<div class="row">

    <div class="col-md-4 mb-4">

        <div class="card shadow-sm">

            <div class="card-header bg-primary text-white">

                <h5 class="mb-0"><i class="fas fa-user me-2"></i> Account Information</h5>

            </div>

            <div class="card-body">

                <div class="text-center mb-4">

                    <div class="avatar-circle mb-3">

                        <span class="avatar-text"><?php echo strtoupper(substr($user['username'], 0, 1)); ?></span>

                    </div>

                    <h5><?php echo htmlspecialchars($user['username']); ?></h5>

                    <span class="badge <?php echo $user['user_type'] === 'faculty' ? 'bg-danger' : 'bg-success'; ?> mb-2">

                        <?php echo ucfirst($user['user_type']); ?>

                    </span>

                </div>

                <ul class="list-group list-group-flush">

                    <li class="list-group-item">

                        <strong><i class="fas fa-envelope me-2"></i> Email:</strong>

                        <span class="float-end"><?php echo htmlspecialchars($user['email']); ?></span>

                    </li>

                    <li class="list-group-item">

                        <strong><i class="fas fa-clock me-2"></i> Joined:</strong>

                        <span class="float-end"><?php echo date('M d, Y', strtotime($user['created_at'])); ?></span>

                    </li>

                    <li class="list-group-item">

                        <strong><i class="fas fa-sign-in-alt me-2"></i> Last Login:</strong>

                        <span class="float-end">

                            <?php echo $user['last_login'] ? date('M d, Y H:i', strtotime($user['last_login'])) : 'Never'; ?>

                        </span>

                    </li>

                    <li class="list-group-item">

                        <strong><i class="fas fa-toggle-on me-2"></i> Status:</strong>

                        <span class="float-end">

                            <span class="badge <?php echo $user['status'] === 'active' ? 'bg-success' : 'bg-secondary'; ?>">

                                <?php echo ucfirst($user['status']); ?>

                            </span>

                        </span>

                    </li>

                </ul>

            </div>

        </div>

    </div>

    

    <div class="col-md-8">

        <div class="card shadow-sm mb-4">

            <div class="card-header bg-primary text-white">

                <h5 class="mb-0"><i class="fas fa-edit me-2"></i> Edit Profile</h5>

            </div>

            <div class="card-body">

                <form method="POST" action="profile.php" class="needs-validation" novalidate>

                    <div class="mb-3">

                        <label for="username" class="form-label">Username</label>

                        <input type="text" class="form-control" id="username" value="<?php echo htmlspecialchars($user['username']); ?>" readonly>

                        <div class="form-text">Username cannot be changed.</div>

                    </div>

                    <div class="mb-3">

                        <label for="email" class="form-label">Email Address</label>

                        <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>

                        <div class="invalid-feedback">

                            Please enter a valid email address.

                        </div>

                    </div>

                    <div class="mb-3">

                        <label for="user_type" class="form-label">Account Type</label>

                        <input type="text" class="form-control" id="user_type" value="<?php echo ucfirst($user['user_type']); ?>" readonly>

                        <div class="form-text">Account type cannot be changed.</div>

                    </div>

                    <button type="submit" name="update_profile" class="btn btn-primary">

                        <i class="fas fa-save me-2"></i> Update Profile

                    </button>

                </form>

            </div>

        </div>

        

        <div class="card shadow-sm">

            <div class="card-header bg-primary text-white">

                <h5 class="mb-0"><i class="fas fa-key me-2"></i> Change Password</h5>

            </div>

            <div class="card-body">

                <form method="POST" action="profile.php" class="needs-validation" novalidate>

                    <div class="mb-3">

                        <label for="current_password" class="form-label">Current Password</label>

                        <div class="input-group">

                            <input type="password" class="form-control" id="current_password" name="current_password" required>

                            <button class="btn btn-outline-secondary toggle-password" type="button" data-target="#current_password">

                                <i class="fas fa-eye"></i>

                            </button>

                        </div>

                        <div class="invalid-feedback">

                            Please enter your current password.

                        </div>

                    </div>

                    <div class="mb-3">

                        <label for="new_password" class="form-label">New Password</label>

                        <div class="input-group">

                            <input type="password" class="form-control" id="new_password" name="new_password" required minlength="6">

                            <button class="btn btn-outline-secondary toggle-password" type="button" data-target="#new_password">

                                <i class="fas fa-eye"></i>

                            </button>

                        </div>

                        <div class="form-text">Password must be at least 6 characters long.</div>

                        <div class="invalid-feedback">

                            Please enter a new password (minimum 6 characters).

                        </div>

                    </div>

                    <div class="mb-3">

                        <label for="confirm_password" class="form-label">Confirm New Password</label>

                        <div class="input-group">

                            <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>

                            <button class="btn btn-outline-secondary toggle-password" type="button" data-target="#confirm_password">

                                <i class="fas fa-eye"></i>

                            </button>

                        </div>

                        <div class="invalid-feedback">

                            Please confirm your new password.

                        </div>

                    </div>

                    <button type="submit" name="change_password" class="btn btn-warning">

                        <i class="fas fa-key me-2"></i> Change Password

                    </button>

                </form>

            </div>

        </div>

    </div>

</div>



<style>

.avatar-circle {
    width: 100px;
    height: 100px;
    background-color: #007bff;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto;
}

.avatar-text {
    font-size: 48px;
    color: white;
    font-weight: bold;
}
</style>

<?php
// Include footer
include 'includes/footer.php';

// Close connection
$conn->close();
?>