-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupport.php
More file actions
430 lines (391 loc) · 20.6 KB
/
Copy pathsupport.php
File metadata and controls
430 lines (391 loc) · 20.6 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
<?php
// support.php — Resident-facing support center (ticket submission + history)
//
// Casperia Prime policy:
// - Guests may submit tickets (name + email required), so “can’t log in” issues can still be reported.
// - Ticket history remains available only to logged-in residents.
$title = "Support";
include_once __DIR__ . "/include/config.php";
include_once __DIR__ . "/include/" . HEADER_FILE;
// ------------------------------------------------------------
// Current user (logged-in or guest)
// ------------------------------------------------------------
$GUEST_UUID = '00000000-0000-0000-0000-000000000000';
$currentUserId = $GUEST_UUID;
$currentUserName = 'Guest';
$isLoggedIn = false;
if (isset($_SESSION['user']) && !empty($_SESSION['user']['principal_id'])) {
$currentUserId = (string)$_SESSION['user']['principal_id'];
$currentUserName = (string)($_SESSION['user']['display_name'] ?? $_SESSION['user']['name'] ?? 'Resident');
$isLoggedIn = true;
}
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
if (!function_exists('h')) {
function h($s): string {
return htmlspecialchars((string)$s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
}
function ws_has_column(mysqli $con, string $table, string $column): bool {
try {
$tableEsc = str_replace('`', '``', $table);
$colEsc = str_replace('`', '``', $column);
$q = "SHOW COLUMNS FROM `{$tableEsc}` LIKE '{$colEsc}'";
$res = mysqli_query($con, $q);
if (!$res) return false;
$ok = mysqli_num_rows($res) > 0;
mysqli_free_result($res);
return $ok;
} catch (Throwable $e) {
return false;
}
}
$allowedCategories = [
'account' => 'Account / Login',
'technical' => 'Technical Issue',
'region' => 'Region / Land',
'abuse' => 'Abuse Report',
'other' => 'Other'
];
$allowedStatuses = [
'open' => 'Open',
'in_progress' => 'In progress',
'closed' => 'Closed',
];
$flash = '';
$flashType = 'info';
// ------------------------------------------------------------
// DB connect
// ------------------------------------------------------------
$con = db();
if (!$con) {
echo '<div class="container-fluid mt-4 mb-4"><div class="row"><div class="col-12 col-lg-8 mx-auto">'
. '<div class="content-card shadow-sm p-3 p-md-4"><div class="alert alert-danger mb-0"><i class="bi bi-x-circle me-2"></i>'
. 'Database connection failed.</div></div></div></div></div>';
include_once __DIR__ . "/include/" . FOOTER_FILE;
exit;
}
// ------------------------------------------------------------
// Ensure ws_tickets table exists (surgical, self-contained)
// - Includes contact_email for new installs; existing installs remain unchanged.
// ------------------------------------------------------------
mysqli_query($con, "CREATE TABLE IF NOT EXISTS ws_tickets (
id INT AUTO_INCREMENT PRIMARY KEY,
user_uuid CHAR(36) NOT NULL,
user_name VARCHAR(64) NOT NULL DEFAULT '',
contact_email VARCHAR(190) NOT NULL DEFAULT '',
category VARCHAR(50) NOT NULL DEFAULT 'other',
subject VARCHAR(150) NOT NULL DEFAULT '',
message TEXT NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'open',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_user (user_uuid, status),
INDEX idx_status (status),
INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
// Detect whether contact_email exists on this installation
$hasContactEmail = ws_has_column($con, 'ws_tickets', 'contact_email');
// ------------------------------------------------------------
// Handle ticket creation (logged-in OR guest)
// ------------------------------------------------------------
$subject = '';
$message = '';
$category = 'other';
$guestName = '';
$guestEmail = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'create_ticket') {
// Honeypot (bots fill this)
if (!empty($_POST['website'] ?? '')) {
$flash = "Thanks! Your request has been received.";
$flashType = 'success';
} else {
// Simple throttle (per session)
$now = time();
$last = (int)($_SESSION['ws_last_ticket_ts'] ?? 0);
if ($last > 0 && ($now - $last) < 45) {
$flash = "Please wait a moment before submitting another ticket.";
$flashType = 'warning';
} else {
$category = trim((string)($_POST['category'] ?? 'other'));
$subject = trim((string)($_POST['subject'] ?? ''));
$message = trim((string)($_POST['message'] ?? ''));
if (!isset($allowedCategories[$category])) {
$category = 'other';
}
// Guest identity (if not logged in)
if (!$isLoggedIn) {
$guestName = trim((string)($_POST['guest_name'] ?? ''));
$guestEmail = trim((string)($_POST['guest_email'] ?? ''));
if ($guestName === '') {
$flash = "Please enter your name.";
$flashType = 'danger';
} elseif ($guestEmail === '' || !filter_var($guestEmail, FILTER_VALIDATE_EMAIL)) {
$flash = "Please enter a valid email address.";
$flashType = 'danger';
}
}
if (!$flash) {
if ($subject === '' || mb_strlen($subject) < 3) {
$flash = "Please enter a subject (at least 3 characters).";
$flashType = 'danger';
} elseif ($message === '' || mb_strlen($message) < 10) {
$flash = "Please enter a message (at least 10 characters).";
$flashType = 'danger';
} else {
$ticketUserUUID = $isLoggedIn ? $currentUserId : $GUEST_UUID;
$ticketUserName = $isLoggedIn ? $currentUserName : $guestName;
$ticketEmail = $isLoggedIn ? '' : $guestEmail;
// If we don't have a contact_email column, embed it into the message (admin can still see it)
$storeMessage = $message;
if (!$isLoggedIn && !$hasContactEmail) {
$storeMessage = "Contact Email: {$guestEmail}\n\n" . $message;
}
try {
if ($hasContactEmail) {
$sql = "INSERT INTO ws_tickets (user_uuid, user_name, contact_email, category, subject, message)
VALUES (?,?,?,?,?,?)";
$st = mysqli_prepare($con, $sql);
if ($st) {
mysqli_stmt_bind_param($st, "ssssss", $ticketUserUUID, $ticketUserName, $ticketEmail, $category, $subject, $storeMessage);
$ok = mysqli_stmt_execute($st);
mysqli_stmt_close($st);
} else {
$ok = false;
}
} else {
$sql = "INSERT INTO ws_tickets (user_uuid, user_name, category, subject, message)
VALUES (?,?,?,?,?)";
$st = mysqli_prepare($con, $sql);
if ($st) {
mysqli_stmt_bind_param($st, "sssss", $ticketUserUUID, $ticketUserName, $category, $subject, $storeMessage);
$ok = mysqli_stmt_execute($st);
mysqli_stmt_close($st);
} else {
$ok = false;
}
}
if (!empty($ok)) {
$_SESSION['ws_last_ticket_ts'] = $now;
$ticketId = (int)mysqli_insert_id($con);
if ($isLoggedIn) {
$flash = "Your ticket has been submitted. Reference #{$ticketId}.";
} else {
$flash = "Your ticket has been submitted. Reference #{$ticketId}. We will reply to {$guestEmail}.";
}
$flashType = 'success';
// Clear form fields after success
$subject = '';
$message = '';
$category = 'other';
$guestName = '';
$guestEmail = '';
} else {
$flash = "There was a problem saving your ticket. Please try again later.";
$flashType = 'danger';
}
} catch (Throwable $e) {
$flash = "There was a problem saving your ticket. Please try again later.";
$flashType = 'danger';
}
}
}
}
}
}
// ------------------------------------------------------------
// Load current user's tickets (logged-in only)
// ------------------------------------------------------------
$tickets = [];
if ($isLoggedIn) {
$sql = "SELECT id, category, subject, status, created_at, updated_at
FROM ws_tickets
WHERE user_uuid = ?
ORDER BY
CASE status
WHEN 'open' THEN 0
WHEN 'in_progress' THEN 1
ELSE 2
END,
created_at DESC
LIMIT 100";
if ($st = mysqli_prepare($con, $sql)) {
mysqli_stmt_bind_param($st, "s", $currentUserId);
mysqli_stmt_execute($st);
$res = mysqli_stmt_get_result($st);
if ($res) {
while ($row = mysqli_fetch_assoc($res)) {
$tickets[] = $row;
}
mysqli_free_result($res);
}
mysqli_stmt_close($st);
}
}
?>
<div class="container-fluid mt-4 mb-4">
<div class="row">
<div class="col-md-3">
<div class="card mb-3">
<div class="card-header">
<h5 class="mb-0"><i class="bi bi-life-preserver me-1"></i> Support</h5>
</div>
<div class="card-body small">
<p class="mb-2 text-body-secondary">
Submit a ticket for account, region, or website issues. You can also review your recent requests here.
</p>
<div class="d-grid gap-2">
<a href="help.php" class="btn btn-sm btn-outline-primary"><i class="bi bi-question-circle me-1"></i> Help & FAQ</a>
<a href="tos.php" class="btn btn-sm btn-outline-primary"><i class="bi bi-shield-check me-1"></i> Terms</a>
</div>
</div>
</div>
</div>
<div class="col-md-9">
<div class="content-card shadow-sm p-3 p-md-4">
<!-- CASPERIA_SUPPORT_TICKETS_PADDING_FIX_V4 -->
<div class="d-flex align-items-center justify-content-between mb-3">
<div>
<h1 class="mb-1"><i class="bi bi-life-preserver me-2"></i> Support</h1>
<p class="text-body-secondary mb-0">Open a support ticket or review your recent requests.</p>
</div>
<div class="text-end">
<?php if ($isLoggedIn): ?>
<span class="badge bg-secondary-subtle text-secondary-emphasis">
Logged in as <?php echo h($currentUserName); ?>
</span>
<?php else: ?>
<span class="badge bg-secondary-subtle text-secondary-emphasis">
Guest submission enabled
</span>
<?php endif; ?>
</div>
</div>
<?php if ($flash): ?>
<div class="alert alert-<?php echo h($flashType); ?> mb-4">
<?php echo h($flash); ?>
</div>
<?php endif; ?>
<div class="row g-4">
<!-- New ticket form -->
<div class="col-12 col-lg-6">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<h5 class="card-title mb-3"><i class="bi bi-plus-circle me-2"></i> Open a ticket</h5>
<form method="post" action="support.php" autocomplete="off">
<input type="hidden" name="action" value="create_ticket">
<!-- Honeypot -->
<div style="position:absolute; left:-9999px; width:1px; height:1px; overflow:hidden;">
<label>Website</label>
<input type="text" name="website" value="">
</div>
<?php if (!$isLoggedIn): ?>
<div class="row g-2 mb-2">
<div class="col-md-6">
<label class="form-label">Your name</label>
<input type="text" class="form-control" name="guest_name" value="<?php echo h($guestName); ?>" required>
</div>
<div class="col-md-6">
<label class="form-label">Email</label>
<input type="email" class="form-control" name="guest_email" value="<?php echo h($guestEmail); ?>" required>
</div>
</div>
<?php endif; ?>
<div class="mb-2">
<label class="form-label">Category</label>
<select class="form-select" name="category">
<?php foreach ($allowedCategories as $key => $label): ?>
<option value="<?php echo h($key); ?>" <?php echo ($category === $key) ? 'selected' : ''; ?>>
<?php echo h($label); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-2">
<label class="form-label">Subject</label>
<input type="text" class="form-control" name="subject" value="<?php echo h($subject); ?>" maxlength="150" required>
</div>
<div class="mb-3">
<label class="form-label">Message</label>
<textarea class="form-control" name="message" rows="6" required><?php echo h($message); ?></textarea>
</div>
<div class="d-flex gap-2">
<button type="submit" class="btn btn-primary">
<i class="bi bi-send me-2"></i> Submit ticket
</button>
<a href="support.php" class="btn btn-outline-secondary">Reset</a>
</div>
<?php if (!$isLoggedIn): ?>
<div class="small text-body-secondary mt-3">
We’ll reply to the email you provide. Ticket history is available after logging in.
</div>
<?php endif; ?>
</form>
</div>
</div>
</div>
<!-- Ticket history (logged-in only) -->
<div class="col-12 col-lg-6">
<div class="card border-0 shadow-sm h-100">
<div class="card-body">
<h5 class="card-title mb-3"><i class="bi bi-clock-history me-2"></i> Your recent tickets</h5>
<?php if (!$isLoggedIn): ?>
<div class="alert alert-secondary mb-0">
<i class="bi bi-info-circle me-2"></i>
Log in to view your ticket history.
</div>
<?php elseif (!$tickets): ?>
<div class="alert alert-secondary mb-0">
<i class="bi bi-info-circle me-2"></i>
You have not submitted any tickets yet.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-sm align-middle mb-0">
<thead>
<tr>
<th>#</th>
<th>Subject</th>
<th>Status</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<?php foreach ($tickets as $t): ?>
<tr>
<td><?php echo (int)$t['id']; ?></td>
<td>
<div class="fw-semibold"><?php echo h($t['subject']); ?></div>
<div class="small text-body-secondary"><?php echo h($allowedCategories[$t['category']] ?? $t['category']); ?></div>
</td>
<td>
<?php
$st = $t['status'];
$badge = 'secondary';
if ($st === 'open') $badge = 'success';
if ($st === 'in_progress') $badge = 'warning';
if ($st === 'closed') $badge = 'secondary';
?>
<span class="badge text-bg-<?php echo h($badge); ?>">
<?php echo h($allowedStatuses[$st] ?? $st); ?>
</span>
</td>
<td class="small text-body-secondary">
<?php echo h($t['updated_at']); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div><!-- /row -->
</div><!-- /content-card -->
</div>
</div>
</div>
<?php include_once __DIR__ . "/include/" . FOOTER_FILE; ?>