-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_user_inspect.php
More file actions
264 lines (244 loc) · 9.72 KB
/
Copy pathadmin_user_inspect.php
File metadata and controls
264 lines (244 loc) · 9.72 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
<?php
session_start();
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/includes/functions.php';
$user_id = (int)($_GET['user_id'] ?? 0);
if ($user_id <= 0) {
set_flash('Invalid user id.');
header('Location: admin_dashboard.php');
exit;
}
// Load user safely
try {
$uCols = $pdo->query("DESCRIBE users")->fetchAll(PDO::FETCH_COLUMN, 0);
} catch (PDOException $e) {
$uCols = [];
}
$hasLastLogin = in_array('last_login', $uCols, true);
$select = $hasLastLogin
? 'id, username, email, created_at, last_login'
: 'id, username, email, created_at, NULL AS last_login';
$stmt = $pdo->prepare("SELECT $select FROM users WHERE id = ? LIMIT 1");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if (!$user) {
set_flash('User not found.');
header('Location: admin_dashboard.php');
exit;
}
// Posts list (tolerant)
$posts = [];
try {
$pCols = $pdo->query("DESCRIBE posts")->fetchAll(PDO::FETCH_COLUMN, 0);
$postFk = in_array('user_id', $pCols, true) ? 'user_id' : (in_array('author_id', $pCols, true) ? 'author_id' : null);
if ($postFk) {
$hasPostDeleted = in_array('is_deleted', $pCols, true);
$pSelect = $hasPostDeleted ? 'id, title, created_at, is_deleted' : 'id, title, created_at, 0 AS is_deleted';
$pstmt = $pdo->prepare("SELECT $pSelect FROM posts WHERE $postFk = ? ORDER BY created_at DESC LIMIT 50");
$pstmt->execute([$user_id]);
$posts = $pstmt->fetchAll();
}
} catch (PDOException $e) {
$posts = [];
}
// Replies list (tolerant)
$replies = [];
try {
$rCols = $pdo->query('DESCRIBE replies')->fetchAll(PDO::FETCH_COLUMN, 0);
// detect reply text column
$textPriority = ['content', 'body', 'reply', 'message', 'text', 'comment'];
$replyCol = null;
foreach ($textPriority as $colName) {
if (in_array($colName, $rCols, true)) {
$replyCol = $colName;
break;
}
}
$replySelect = $replyCol ? ("r.`$replyCol` AS content,") : ("'' AS content,");
// detect replies user foreign key
$replyFkCandidates = ['user_id', 'author_id', 'uid', 'created_by', 'user'];
$replyFk = null;
foreach ($replyFkCandidates as $cand) {
if (in_array($cand, $rCols, true)) {
$replyFk = $cand;
break;
}
}
if ($replyFk) {
$hasReplyDeleted = in_array('is_deleted', $rCols, true);
$deletedSelect = $hasReplyDeleted ? 'r.is_deleted' : '0 AS is_deleted';
$rstmt = $pdo->prepare("SELECT r.id, r.post_id, $replySelect r.created_at, $deletedSelect, p.title AS post_title FROM replies r LEFT JOIN posts p ON p.id = r.post_id WHERE r.`$replyFk` = ? ORDER BY r.created_at DESC LIMIT 100");
$rstmt->execute([$user_id]);
$replies = $rstmt->fetchAll();
}
} catch (PDOException $e) {
$replies = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inspect User - <?= h($user['username']) ?> | Tech Forum</title>
<link rel="icon" href="assets/images/im.png" type="image/png">
<style>
body {
font-family: Arial, sans-serif;
background: #f5f5f5;
color: #333;
margin: 0;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.container {
max-width: 1100px;
margin: 0 auto;
padding: 1.5rem;
}
.card {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 1rem 1.5rem;
margin-bottom: 1rem;
}
.muted {
color: #666;
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #eee;
}
th {
background: #f8f9fa;
}
.pill {
padding: 2px 8px;
border-radius: 999px;
font-size: 12px;
}
.pill.del {
background: #fee;
color: #c33;
}
.pill.ok {
background: #e6ffed;
color: #2f855a;
}
.btn {
display: inline-block;
background: #667eea;
color: #fff;
padding: 6px 10px;
border-radius: 4px;
text-decoration: none;
font-size: 12px;
}
</style>
</head>
<body>
<div class="header">
<div style="display:flex;align-items:center;gap:10px;">
<a href="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? ('admin_user_inspect.php?user_id=' . (int)$user['id'])) ?>" title="Reload this page" style="display:inline-flex;align-items:center;">
<img src="assets/images/im.png" alt="Tech Forum" width="28" height="28" style="display:block;border-radius:6px;background:#fff;object-fit:contain;" />
</a>
<div>Inspect User: <?= h($user['username']) ?> (ID <?= (int)$user['id'] ?>)</div>
</div>
<div><a class="btn" href="admin_dashboard.php">Back to Dashboard</a></div>
</div>
<div class="container">
<div class="card">
<h3 style="margin-bottom:.5rem;">Credentials</h3>
<div class="muted">Email: <?= h($user['email']) ?></div>
<div class="muted">Joined: <?= date('M j, Y H:i', strtotime($user['created_at'])) ?></div>
<div class="muted">Last Login: <?= $user['last_login'] ? date('M j, Y H:i', strtotime($user['last_login'])) : '—' ?></div>
<p class="muted" style="margin-top:.5rem;">Note: Only non-sensitive user info is displayed. Password hashes are not shown.</p>
</div>
<div class="card">
<h3 style="margin-bottom:.5rem;">Recent Posts</h3>
<?php if (empty($posts)): ?>
<p class="muted">No posts found.</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Created</th>
<th>Status</th>
<?php if (!empty($_SESSION['admin_logged_in'])): ?>
<th>Actions</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $p): ?>
<tr>
<td><?= (int)$p['id'] ?></td>
<td><?= h($p['title']) ?></td>
<td><?= date('M j, Y H:i', strtotime($p['created_at'])) ?></td>
<td><?= $p['is_deleted'] ? '<span class="pill del">Deleted</span>' : '<span class="pill ok">Active</span>' ?></td>
<?php if (!empty($_SESSION['admin_logged_in'])): ?>
<td>
<?php if (empty($p['is_deleted'])): ?>
<form method="POST" action="admin_delete_post.php" style="display:inline;" onsubmit="return confirm('Delete this post? This is a soft delete and can hide the content from users.');">
<input type="hidden" name="post_id" value="<?= (int)$p['id'] ?>">
<input type="hidden" name="user_id" value="<?= (int)$user['id'] ?>">
<button type="submit" class="btn" style="background:#e53e3e;">Delete</button>
</form>
<?php else: ?>
<span class="muted">—</span>
<?php endif; ?>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<div class="card">
<h3 style="margin-bottom:.5rem;">Recent Replies</h3>
<?php if (empty($replies)): ?>
<p class="muted">No replies found.</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Post</th>
<th>Excerpt</th>
<th>Created</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($replies as $r): ?>
<tr>
<td><?= (int)$r['id'] ?></td>
<td><?= h($r['post_title'] ?? ('Post #' . (int)$r['post_id'])) ?></td>
<td><?= h(mb_strimwidth(strip_tags($r['content']), 0, 80, '…')) ?></td>
<td><?= date('M j, Y H:i', strtotime($r['created_at'])) ?></td>
<td><?= $r['is_deleted'] ? '<span class="pill del">Deleted</span>' : '<span class="pill ok">Active</span>' ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</body>
</html>