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
|
<?php
declare(strict_types=1);
// Profile management: lets the logged-in user change username, email, password,
// and notification preferences.
namespace App\Controllers;
use App\Csrf;
use App\Flash;
use App\Mail;
use App\Models\User;
class ProfileController
{
private User $user;
public function __construct()
{
$this->user = new User();
}
public function show(): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
return;
}
$user = $this->user->findById($_SESSION['user_id']);
$content = __DIR__ . '/../Views/profile/edit.php';
include __DIR__ . '/../Views/layouts/main.php';
}
public function updateUsername(): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
return;
}
if (!Csrf::validate($_POST['csrf_token'] ?? '')) {
Flash::set('error', 'Invalid CSRF token.');
header('Location: /profile');
return;
}
$username = trim($_POST['username'] ?? '');
if (!preg_match('/^[a-zA-Z0-9_]{3,20}$/', $username)) {
Flash::set('error', 'Username must be 3-20 characters (letters, numbers, underscores).');
header('Location: /profile');
return;
}
$existing = $this->user->findByUsername($username);
if ($existing && $existing['id'] !== $_SESSION['user_id']) {
Flash::set('error', 'Username is already taken.');
header('Location: /profile');
return;
}
$this->user->updateUsername($_SESSION['user_id'], $username);
// Keep the session in sync so the nav bar shows the new name
$_SESSION['username'] = $username;
Flash::set('success', 'Username updated.');
header('Location: /profile');
}
public function updateEmail(): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
return;
}
if (!Csrf::validate($_POST['csrf_token'] ?? '')) {
Flash::set('error', 'Invalid CSRF token.');
header('Location: /profile');
return;
}
$email = trim($_POST['email'] ?? '');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
Flash::set('error', 'Invalid email address.');
header('Location: /profile');
return;
}
$existing = $this->user->findByEmail($email);
if ($existing && $existing['id'] !== $_SESSION['user_id']) {
Flash::set('error', 'Email is already registered.');
header('Location: /profile');
return;
}
// Changing email requires re-verification: the user must confirm
// they own the new address before they can log in again
$token = $this->user->updateEmail($_SESSION['user_id'], $email);
Mail::sendVerification($email, $token);
// Log the user out so they must re-verify
session_destroy();
// Start a fresh session for the flash message
session_start();
Flash::set('success', 'Email updated. Check your new email to re-verify your account.');
header('Location: /login');
}
public function updatePassword(): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
return;
}
if (!Csrf::validate($_POST['csrf_token'] ?? '')) {
Flash::set('error', 'Invalid CSRF token.');
header('Location: /profile');
return;
}
$currentPassword = $_POST['current_password'] ?? '';
$newPassword = $_POST['new_password'] ?? '';
$confirmPassword = $_POST['new_password_confirm'] ?? '';
$user = $this->user->findById($_SESSION['user_id']);
// Verify the current password so an attacker who steals a session
// can't silently change the password
if (!password_verify($currentPassword, $user['password_hash'])) {
Flash::set('error', 'Current password is incorrect.');
header('Location: /profile');
return;
}
if (\strlen($newPassword) < 8) {
Flash::set('error', 'New password must be at least 8 characters.');
header('Location: /profile');
return;
}
if ($newPassword !== $confirmPassword) {
Flash::set('error', 'New passwords do not match.');
header('Location: /profile');
return;
}
$this->user->updatePassword($user['id'], $newPassword);
Flash::set('success', 'Password updated.');
header('Location: /profile');
}
public function updateNotifications(): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
return;
}
if (!Csrf::validate($_POST['csrf_token'] ?? '')) {
Flash::set('error', 'Invalid CSRF token.');
header('Location: /profile');
return;
}
// Checkbox value: present in POST when checked, absent when unchecked
$notify = isset($_POST['notify_comments']);
$this->user->updateNotifyComments($_SESSION['user_id'], $notify);
Flash::set('success', 'Notification preferences updated.');
header('Location: /profile');
}
}
|