-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccountSettings.php
More file actions
522 lines (480 loc) · 21.5 KB
/
Copy pathaccountSettings.php
File metadata and controls
522 lines (480 loc) · 21.5 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
// Define a constant to protect included files from direct access
define('INCLUDED_VIA_APP', true);
// Include the initialization file (handles sessions and database connection)
require_once __DIR__ . '/includes/init.php';
// Include the database configuration file
global $mysqli, $conn;
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
die("<h2>Access Denied</h2><p>You must be <a href='/login.php'>logged in</a> to change your password.</p>");
} else {
// --- FETCH USER DETAILS ---
$user_id = $_SESSION['user_id'];
try {
$stmt = $mysqli->prepare("SELECT username, displayname, email, password, initials, email_notifications FROM users WHERE user_id = ?");
if ($stmt) {
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->bind_result($username, $displayname, $email, $hashed_password, $initials, $email_notifications);
if (!$stmt->fetch()) {
$stmt->close();
die("<h2>User not found.</h2>");
}
$stmt->close();
} else {
throw new Exception("Unable to prepare statement");
}
} catch (Throwable $e) {
// Fallback if email_notifications column doesn't exist yet
try {
$stmt = $mysqli->prepare("SELECT username, displayname, email, password, initials FROM users WHERE user_id = ?");
if ($stmt) {
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->bind_result($username, $displayname, $email, $hashed_password, $initials);
if (!$stmt->fetch()) {
$stmt->close();
die("<h2>User not found.</h2>");
}
$stmt->close();
$email_notifications = 0; // Default to 0 / off
} else {
die("<h2>Database Error</h2><p>Failed to prepare user query.</p>");
}
} catch (Throwable $ex) {
die("<h2>Database Error</h2><p>Failed to query user details: " . htmlspecialchars($ex->getMessage()) . "</p>");
}
}
}
// Generate CSRF token for security
$csrf_token = generateCSRFToken();
// Initialize error and success messages
$error = "";
$success = "";
$notif_error = "";
$notif_success = "";
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
if (isset($_POST['action']) && $_POST['action'] === 'update_notifications') {
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
$notif_error = "Security check failed. Please try again.";
} else {
$email_notif = isset($_POST['email_notifications']) ? 1 : 0;
$update = $mysqli->prepare("UPDATE users SET email_notifications = ? WHERE user_id = ?");
if (!$update) {
$notif_error = "Failed to update notification settings. (Database schema not updated yet)";
} else {
$update->bind_param('ii', $email_notif, $user_id);
if ($update->execute()) {
$notif_success = "Notification preferences updated successfully!";
$email_notifications = $email_notif; // Update local variable for page display
} else {
$notif_error = "Failed to update notification settings.";
}
$update->close();
}
}
} elseif (isset($_POST['action']) && $_POST['action'] === 'unsubscribe_all') {
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
$notif_error = "Security check failed. Please try again.";
} else {
$delete = $mysqli->prepare("DELETE FROM subscriptions WHERE user_id = ?");
if (!$delete) {
$notif_error = "Failed to unsubscribe. (Database schema not updated yet)";
} else {
$delete->bind_param('i', $user_id);
if ($delete->execute()) {
$notif_success = "Unsubscribed from all discussions.";
} else {
$notif_error = "Failed to unsubscribe. Please try again.";
}
$delete->close();
}
}
} elseif (isset($_POST['action']) && $_POST['action'] === 'unsubscribe_single') {
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
$notif_error = "Security check failed. Please try again.";
} else {
$item_id = isset($_POST['sub_item_id']) ? (int)$_POST['sub_item_id'] : 0;
$item_type = $_POST['sub_item_type'] ?? '';
$delete = $mysqli->prepare("DELETE FROM subscriptions WHERE user_id = ? AND item_id = ? AND item_type = ?");
if (!$delete) {
$notif_error = "Failed to unsubscribe. (Database schema not updated yet)";
} else {
$delete->bind_param('iis', $user_id, $item_id, $item_type);
if ($delete->execute()) {
$notif_success = "Successfully unsubscribed from discussion.";
} else {
$notif_error = "Failed to unsubscribe. Please try again.";
}
$delete->close();
}
}
} else {
// Password change form
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
$error = "Security check failed. Please try again.";
} else {
$old_password = $_POST['old_password'] ?? '';
$new_password = $_POST['new_password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
// Validation
if (empty($old_password) || empty($new_password) || empty($confirm_password)) {
$error = "All password fields are required.";
} elseif ($new_password !== $confirm_password) {
$error = "New password and confirmation do not match.";
} elseif (strlen($new_password) < 8) {
$error = "New password must be at least 8 characters long.";
} elseif (!password_verify($old_password, $hashed_password)) {
$error = "Current password is incorrect.";
} else {
// Update password
$new_hashed = password_hash($new_password, PASSWORD_DEFAULT);
$update = $mysqli->prepare("UPDATE users SET password = ? WHERE user_id = ?");
$update->bind_param('si', $new_hashed, $user_id);
if ($update->execute()) {
$success = "Password updated successfully!";
} else {
$error = "Failed to update password. Please try again.";
}
$update->close();
}
}
}
} catch (Throwable $e) {
$error = "An error occurred: " . htmlspecialchars($e->getMessage());
$notif_error = "An error occurred: " . htmlspecialchars($e->getMessage());
}
}
?>
<?php
$page_title = 'Dominik Mueller - Wine is my hobby';
$meta_desc = 'On this website, I share my wine cellar with a community of fellow fine wine enthusiasts.';
require_once 'includes/header.php';
?>
<div class="row">
<div class="column main">
<div class="card">
<h3>Change password</h3>
<?php
if ($error!="") {
echo "<div style='color:red;'>$error</div>";
} elseif ($success!="") {
echo "<div style='color:green;'>$success</div>";
}
?>
</div>
<div class="card">
<form method="post" autocomplete="off" accept-charset="UTF-8">
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
<label for="username">Username:</label>
<input type="text" id="username" value="<?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8') ?>" disabled readonly>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" value="<?= htmlspecialchars($email) ?>" disabled readonly>
<br><br>
<label for="displayname">Full name:</label>
<input type="text" id="displayname" value="<?= htmlspecialchars($displayname, ENT_QUOTES, 'UTF-8') ?>" disabled readonly>
<br><br>
<label for="initials">Initials:</label>
<input type="text" id="initials" value="<?= htmlspecialchars($initials, ENT_QUOTES, 'UTF-8') ?>" disabled readonly>
<p>If you like to change your email address, full display name or initials, please contact me via the details below.</p>
<hr><br>
<label for="old_password">Current Password:</label>
<input type="password" name="old_password" id="old_password" required>
<br><br>
<label for="new_password">New Password:</label>
<input type="password" name="new_password" id="new_password" required>
<br>
<label for="confirm_password">Confirm New Password:</label>
<input type="password" name="confirm_password" id="confirm_password" required>
<br><br>
<input type="checkbox" onclick="togglePasswords()"> Show Passwords
<br><br>
<button type="submit">Change Password</button>
</form>
</div>
<!-- Notification Preferences Card -->
<div class="card">
<h3>Notification Preferences</h3>
<?php
if ($notif_error != "") {
echo "<div style='color:red; margin-bottom:10px;'>$notif_error</div>";
} elseif ($notif_success != "") {
echo "<div style='color:green; margin-bottom:10px;'>$notif_success</div>";
}
?>
<form method="post" autocomplete="off" accept-charset="UTF-8">
<input type="hidden" name="action" value="update_notifications">
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
<label style="cursor: pointer; display: flex; align-items: center; gap: 8px;">
<input type="checkbox" name="email_notifications" value="1" <?= $email_notifications == 1 ? 'checked' : '' ?>>
Receive email alerts when there is a new comment on items I follow
</label>
<br>
<button type="submit">Save Notification Preferences</button>
</form>
</div>
<!-- Active Subscriptions Management Card -->
<div class="card">
<div style="display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:10px;">
<h3 style="margin:0;">My Subscriptions</h3>
<?php
// Check if there are subscriptions before showing the Unsubscribe All button
$sub_count = 0;
try {
$check_sub = $mysqli->prepare("SELECT COUNT(*) FROM subscriptions WHERE user_id = ?");
if ($check_sub) {
$check_sub->bind_param("i", $user_id);
$check_sub->execute();
$check_sub->bind_result($sub_count);
$check_sub->fetch();
$check_sub->close();
}
} catch (Throwable $e) {
$sub_count = 0;
}
if ($sub_count > 0):
?>
<form method="post" style="margin:0;" onsubmit="return confirm('Are you sure you want to unsubscribe from all discussions?');">
<input type="hidden" name="action" value="unsubscribe_all">
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
<button type="submit" class="btn-action">Unsubscribe from all</button>
</form>
<?php endif; ?>
</div>
<?php
// Fetch all subscriptions for rendering in settings
$sub_sql = "SELECT
s.item_id,
s.item_type,
s.created_at,
-- Wine details
w.vintage AS w_vintage,
wm.name AS w_name,
p.producer AS w_producer,
-- Tasting Note details
tn_w.vintage AS tn_vintage,
tn_wm.name AS tn_name,
tn_p.producer AS tn_producer,
-- Blog details
bp.title AS bp_title
FROM subscriptions s
LEFT JOIN wines w ON s.item_type = 'wine' AND s.item_id = w.wine_id
LEFT JOIN wines_master wm ON w.master_id = wm.master_id
LEFT JOIN producers p ON wm.producer_id = p.producer_id
LEFT JOIN tnotes tn ON s.item_type = 'tnote' AND s.item_id = tn.note_id
LEFT JOIN wines tn_w ON tn.wine_id = tn_w.wine_id
LEFT JOIN wines_master tn_wm ON tn_w.master_id = tn_wm.master_id
LEFT JOIN producers tn_p ON tn_wm.producer_id = tn_p.producer_id
LEFT JOIN blogposts bp ON s.item_type = 'blog' AND s.item_id = bp.blog_id
WHERE s.user_id = ?
ORDER BY s.created_at DESC";
$sub_result = null;
try {
$stmt = $mysqli->prepare($sub_sql);
if ($stmt) {
$stmt->bind_param("i", $user_id);
$stmt->execute();
$sub_result = $stmt->get_result();
}
} catch (Throwable $e) {
$notif_error = "Unable to fetch subscriptions: " . htmlspecialchars($e->getMessage());
}
if ($sub_result && $sub_result->num_rows > 0):
?>
<!-- Instant Search Box -->
<div style="margin: 15px 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;">
<input type="text" id="subSearchBox" onkeyup="filterSubscriptions()"
placeholder="🔍 Search subscriptions by topic, type, or date..."
style="padding: 8px 12px; font-family: Georgia, serif; border: 1px solid #ccc; border-radius: 4px; max-width: 350px; flex: 1; font-size: 14px;">
</div>
<table class="settings-table" id="subsTable">
<thead>
<tr>
<th>Discussion Topic</th>
<th>Type</th>
<th>Followed Since</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
while ($sub_row = $sub_result->fetch_assoc()) {
$item_id = $sub_row['item_id'];
$item_type = $sub_row['item_type'];
$created_at_dt = !empty($sub_row['created_at']) ? date_create($sub_row['created_at']) : false;
$sub_date = $created_at_dt ? date_format($created_at_dt, "j M Y") : 'N/A';
$item_name = "";
$item_url = "";
if ($item_type === 'wine') {
$vintage = $sub_row['w_vintage'] ?? 'NV';
$producer = $sub_row['w_producer'] ?? '';
$name = $sub_row['w_name'] ?? '';
$item_name = trim("$vintage $producer $name");
$item_url = "/wines.php?id=$item_id";
} elseif ($item_type === 'tnote') {
$vintage = $sub_row['tn_vintage'] ?? 'NV';
$producer = $sub_row['tn_producer'] ?? '';
$name = $sub_row['tn_name'] ?? '';
$item_name = "Tasting note on " . trim("$vintage $producer $name");
$item_url = "/tnotes.php?id=$item_id";
} elseif ($item_type === 'blog') {
$title = $sub_row['bp_title'] ?? '';
$item_name = "Story: " . trim($title);
$item_url = "/blog.php?id=$item_id";
}
$display_type = "";
if ($item_type === 'wine') {
$display_type = "Wine";
} elseif ($item_type === 'tnote') {
$display_type = "Tasting Note";
} elseif ($item_type === 'blog') {
$display_type = "Story";
}
?>
<tr class="sub-row"
data-name="<?= htmlspecialchars(strtolower($item_name), ENT_QUOTES, 'UTF-8') ?>"
data-type="<?= htmlspecialchars(strtolower($display_type), ENT_QUOTES, 'UTF-8') ?>"
data-date="<?= htmlspecialchars(strtolower($sub_date), ENT_QUOTES, 'UTF-8') ?>">
<td><a href="<?= htmlspecialchars($item_url) ?>"><?= htmlspecialchars($item_name, ENT_QUOTES, 'UTF-8') ?></a></td>
<td><?= $display_type ?></td>
<td><?= $sub_date ?></td>
<td>
<form method="post" style="margin:0; padding:0;" onsubmit="return confirm('Are you sure you want to unsubscribe from this discussion?');">
<input type="hidden" name="action" value="unsubscribe_single">
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
<input type="hidden" name="sub_item_id" value="<?= $item_id ?>">
<input type="hidden" name="sub_item_type" value="<?= $item_type ?>">
<button type="submit" class="btn-action btn-secondary" style="padding:4px 8px; font-size:11px;">Unsubscribe</button>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<!-- Dynamic Pagination Controls -->
<div id="subPagination" style="margin-top: 15px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; font-size: small; color: #64748b;">
<div id="paginationInfo">Showing 0-0 of 0 subscriptions</div>
<div style="display: flex; gap: 5px;">
<button id="btnPrevSub" class="btn-action btn-secondary" onclick="prevSubPage()" style="padding: 4px 10px; font-size: 12px;">Previous</button>
<button id="btnNextSub" class="btn-action btn-secondary" onclick="nextSubPage()" style="padding: 4px 10px; font-size: 12px;">Next</button>
</div>
</div>
<?php else: ?>
<p style="color:#64748b; margin-top:15px; margin-bottom:0;">You are not currently subscribed to any discussions.</p>
<?php
endif;
if ($stmt) {
$stmt->close();
}
?>
</div>
</div>
<div class="column side">
<div class="card">
<h3>Get in touch</h3>
<p>I control access to some parts of this website. My tasting notes and blog posts are reserved for members only - as is
my <em>carte des vins</em>, an interactive account of the wines in my personal cellar, which are ready to be drunk.
Registered users can also leave comments on my notes and enjoy a safe and well-behaved place to discuss their favourite
topic, wine. User accounts are free, I don't want to make money from this site, but usually only friends and family have
access. If you'd like to introduce yourself and connect with me, you may do so using my details below. I'm always happy
to hear and learn from other wine enthusiasts.</p>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>
<script>
function togglePasswords() {
var x = document.getElementById("old_password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
var x = document.getElementById("new_password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
var x = document.getElementById("confirm_password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
// Active Subscriptions Pagination and Filter Controller
let currentSubPage = 1;
const subsPerPage = 20;
let filteredRows = [];
function initSubscriptionsPagination() {
const table = document.getElementById('subsTable');
if (!table) return;
const rows = Array.from(table.querySelectorAll('.sub-row'));
filteredRows = rows;
showSubPage(1);
}
function showSubPage(page) {
const table = document.getElementById('subsTable');
if (!table) return;
const totalRows = filteredRows.length;
const totalPages = Math.max(1, Math.ceil(totalRows / subsPerPage));
if (page < 1) page = 1;
if (page > totalPages) page = totalPages;
currentSubPage = page;
const allRows = table.querySelectorAll('.sub-row');
allRows.forEach(row => row.style.display = 'none');
const startIndex = (currentSubPage - 1) * subsPerPage;
const endIndex = Math.min(startIndex + subsPerPage, totalRows);
for (let i = startIndex; i < endIndex; i++) {
filteredRows[i].style.display = '';
}
const info = document.getElementById('paginationInfo');
if (info) {
if (totalRows === 0) {
info.textContent = 'No matching subscriptions';
} else {
info.textContent = `Showing ${startIndex + 1}–${endIndex} of ${totalRows} subscriptions`;
}
}
const btnPrev = document.getElementById('btnPrevSub');
const btnNext = document.getElementById('btnNextSub');
if (btnPrev) btnPrev.disabled = (currentSubPage === 1 || totalRows === 0);
if (btnNext) btnNext.disabled = (currentSubPage === totalPages || totalRows === 0);
}
function prevSubPage() {
if (currentSubPage > 1) {
showSubPage(currentSubPage - 1);
}
}
function nextSubPage() {
const totalPages = Math.ceil(filteredRows.length / subsPerPage);
if (currentSubPage < totalPages) {
showSubPage(currentSubPage + 1);
}
}
function filterSubscriptions() {
const query = document.getElementById('subSearchBox').value.toLowerCase().trim();
const table = document.getElementById('subsTable');
if (!table) return;
const allRows = Array.from(table.querySelectorAll('.sub-row'));
if (query === '') {
filteredRows = allRows;
} else {
filteredRows = allRows.filter(row => {
const name = row.getAttribute('data-name') || '';
const type = row.getAttribute('data-type') || '';
const date = row.getAttribute('data-date') || '';
return name.includes(query) || type.includes(query) || date.includes(query);
});
}
showSubPage(1);
}
document.addEventListener('DOMContentLoaded', initSubscriptionsPagination);
</script>