-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
277 lines (241 loc) · 13.2 KB
/
Copy pathsettings.php
File metadata and controls
277 lines (241 loc) · 13.2 KB
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
<?php
require_once 'config.php';
require_login();
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$csrf_token = generate_csrf_token();
// Handle form submissions
$message = '';
$error = '';
$toast_message = '';
$toast_type = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'update_profile') {
$new_username = trim($_POST['username']);
$new_email = trim($_POST['email']);
// Validate inputs
if (empty($new_username)) {
$error = 'Username is required';
} elseif (empty($new_email) || !filter_var($new_email, FILTER_VALIDATE_EMAIL)) {
$error = 'Valid email is required';
} else {
try {
// Check if email already exists (excluding current user)
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ? AND id != ?");
$stmt->execute([$new_email, $user_id]);
if ($stmt->rowCount() > 0) {
$error = 'Email already exists';
} else {
// Update user profile
$stmt = $pdo->prepare("UPDATE users SET username = ?, email = ? WHERE id = ?");
$stmt->execute([$new_username, $new_email, $user_id]);
// Update session
$_SESSION['username'] = $new_username;
$_SESSION['email'] = $new_email;
$message = 'Profile updated successfully!';
}
} catch (PDOException $e) {
$error = 'Error updating profile: ' . $e->getMessage();
}
}
} elseif ($action === 'change_password') {
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
// Validate inputs
if (empty($current_password)) {
$toast_message = 'Current password is required';
$toast_type = 'error';
} elseif (empty($new_password)) {
$toast_message = 'New password is required';
$toast_type = 'error';
} elseif (strlen($new_password) < 8) {
$toast_message = 'New password must be at least 8 characters';
$toast_type = 'error';
} elseif ($new_password !== $confirm_password) {
$toast_message = 'New passwords do not match';
$toast_type = 'error';
} else {
try {
// Verify current password
$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'])) {
// Update password
$new_password_hash = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password_hash = ? WHERE id = ?");
$stmt->execute([$new_password_hash, $user_id]);
$toast_message = 'Password changed successfully!';
$toast_type = 'success';
} else {
$toast_message = 'Current password is incorrect';
$toast_type = 'error';
}
} catch (PDOException $e) {
$toast_message = 'Error changing password';
$toast_type = 'error';
}
}
}
}
require_once 'header.php';
?>
<div class="max-w-4xl mx-auto">
<div class="flex items-center justify-between mb-8">
<div>
<h1 class="text-3xl font-bold text-gray-800 mb-2">Settings</h1>
<p class="text-gray-600">Manage your account settings and preferences</p>
</div>
<a href="dashboard.php" class="bg-gray-100 text-gray-600 px-4 py-2 rounded-lg hover:bg-gray-200 transition">
<i class="fas fa-arrow-left mr-2"></i> Back to Dashboard
</a>
</div>
<?php if ($message && (!isset($_POST['action']) || $_POST['action'] !== 'change_password')): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<?php if ($error && (!isset($_POST['action']) || $_POST['action'] !== 'change_password')): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6">
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<!-- Profile Settings -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
<i class="fas fa-user text-blue-500 mr-2"></i>
Profile Settings
</h2>
<form method="POST" class="space-y-4">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="action" value="update_profile">
<div>
<label for="username" class="block text-sm font-medium text-gray-700 mb-1">Username</label>
<input type="text" id="username" name="username" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500"
value="<?php echo htmlspecialchars($username); ?>" required>
</div>
<div>
<label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email Address</label>
<input type="email" id="email" name="email" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500"
value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<button type="submit" class="w-full bg-blue-500 text-white py-2 px-4 rounded-lg font-semibold hover:bg-blue-600 transition">
Update Profile
</button>
</form>
</div>
<!-- Password Change -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
<i class="fas fa-lock text-blue-500 mr-2"></i>
Change Password
</h2>
<form method="POST" class="space-y-4">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="action" value="change_password">
<div>
<label for="current_password" class="block text-sm font-medium text-gray-700 mb-1">Current Password</label>
<input type="password" id="current_password" name="current_password" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500" required>
</div>
<div>
<label for="new_password" class="block text-sm font-medium text-gray-700 mb-1">New Password</label>
<input type="password" id="new_password" name="new_password" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500" required>
<p class="text-xs text-gray-500 mt-1">Must be at least 8 characters long</p>
</div>
<div>
<label for="confirm_password" class="block text-sm font-medium text-gray-700 mb-1">Confirm New Password</label>
<input type="password" id="confirm_password" name="confirm_password" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500" required>
</div>
<button type="submit" class="w-full bg-blue-500 text-white py-2 px-4 rounded-lg font-semibold hover:bg-blue-600 transition">
Change Password
</button>
</form>
</div>
<!-- Notification Settings -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
<i class="fas fa-bell text-blue-500 mr-2"></i>
Notification Settings
</h2>
<div class="space-y-3">
<div class="flex items-center justify-between">
<div>
<div class="font-medium text-gray-800">Email Notifications</div>
<div class="text-sm text-gray-600">Receive email updates about your habits</div>
</div>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" class="sr-only peer" checked>
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-500"></div>
</label>
</div>
<div class="flex items-center justify-between">
<div>
<div class="font-medium text-gray-800">Habit Reminders</div>
<div class="text-sm text-gray-600">Get reminders for incomplete habits</div>
</div>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" class="sr-only peer" checked>
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-500"></div>
</label>
</div>
<div class="flex items-center justify-between">
<div>
<div class="font-medium text-gray-800">Achievement Alerts</div>
<div class="text-sm text-gray-600">Notify when you earn achievements</div>
</div>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" class="sr-only peer" checked>
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-500"></div>
</label>
</div>
</div>
<button class="w-full bg-blue-500 text-white py-2 px-4 rounded-lg font-semibold hover:bg-blue-600 transition mt-4">
Save Notification Settings
</button>
</div>
<!-- Account Management -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
<i class="fas fa-cog text-blue-500 mr-2"></i>
Account Management
</h2>
<div class="space-y-4">
<!--
<div>
<h3 class="font-medium text-gray-800 mb-2">Export Data</h3>
<p class="text-sm text-gray-600 mb-3">Download all your habit data in CSV format</p>
<a href="export-data.php" class="bg-gray-100 text-gray-600 px-4 py-2 rounded-lg font-semibold hover:bg-gray-200 transition inline-flex items-center">
<i class="fas fa-download mr-2"></i> Export My Data
</a>
</div>
-->
<div class="pt-4 border-t">
<h3 class="font-medium text-red-800 mb-2">Danger Zone</h3>
<p class="text-sm text-gray-600 mb-3">Permanently delete your account and all data</p>
<button onclick="confirmAccountDeletion()" class="bg-red-500 text-white px-4 py-2 rounded-lg font-semibold hover:bg-red-600 transition">
<i class="fas fa-trash mr-2"></i> Delete Account
</button>
</div>
</div>
</div>
</div>
</div>
<script>
function confirmAccountDeletion() {
if (confirm('Are you sure you want to delete your account? This action cannot be undone. All your data will be permanently deleted.')) {
// Implement account deletion here
alert('Account deletion would be processed here. In a real application, this would redirect to a deletion confirmation page.');
}
}
<?php if (!empty($toast_message)): ?>
// Show toast notification after page load
document.addEventListener('DOMContentLoaded', function() {
showToast('<?php echo addslashes($toast_message); ?>', '<?php echo $toast_type; ?>');
});
<?php endif; ?>
</script>
<?php require_once 'footer.php'; ?>