-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.php
More file actions
321 lines (288 loc) · 14.8 KB
/
Copy pathgroup.php
File metadata and controls
321 lines (288 loc) · 14.8 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
<?php
require_once 'config.php';
require_login();
$user_id = $_SESSION['user_id'];
$group_id = $_GET['id'] ?? 0;
$csrf_token = generate_csrf_token();
// Get group info and verify membership
$stmt = $pdo->prepare("
SELECT g.*, gm.role
FROM groups g
JOIN group_members gm ON g.id = gm.group_id
WHERE g.id = ? AND gm.user_id = ?
");
$stmt->execute([$group_id, $user_id]);
$group = $stmt->fetch();
if (!$group) {
$_SESSION['error'] = 'Group not found or access denied';
header('Location: groups.php');
exit;
}
// Get group members
$stmt = $pdo->prepare("
SELECT u.id, u.username, u.email, gm.role, gm.joined_at
FROM group_members gm
JOIN users u ON gm.user_id = u.id
WHERE gm.group_id = ?
ORDER BY gm.role DESC, gm.joined_at ASC
");
$stmt->execute([$group_id]);
$members = $stmt->fetchAll();
// Handle member removal
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_member'])) {
if (!validate_csrf_token($_POST['csrf_token'])) {
$_SESSION['error'] = 'Security token validation failed';
header("Location: group.php?id=$group_id");
exit;
}
$member_id = $_POST['member_id'];
// Verify admin privileges
if ($group['role'] === 'admin') {
try {
$stmt = $pdo->prepare("DELETE FROM group_members WHERE group_id = ? AND user_id = ?");
$stmt->execute([$group_id, $member_id]);
$_SESSION['success'] = 'Member removed successfully';
} catch (PDOException $e) {
$_SESSION['error'] = 'Failed to remove member: ' . $e->getMessage();
}
} else {
$_SESSION['error'] = 'You need admin privileges to remove members';
}
header("Location: group.php?id=$group_id");
exit;
}
// Handle group deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_group'])) {
if (!validate_csrf_token($_POST['csrf_token'])) {
$_SESSION['error'] = 'Security token validation failed';
header("Location: groups.php");
exit;
}
// Verify admin privileges
if ($group['role'] === 'admin') {
try {
// Delete group messages first
$stmt = $pdo->prepare("DELETE FROM group_messages WHERE group_id = ?");
$stmt->execute([$group_id]);
// Delete group members
$stmt = $pdo->prepare("DELETE FROM group_members WHERE group_id = ?");
$stmt->execute([$group_id]);
// Delete the group
$stmt = $pdo->prepare("DELETE FROM groups WHERE id = ?");
$stmt->execute([$group_id]);
$_SESSION['success'] = 'Group deleted successfully';
header("Location: groups.php");
exit;
} catch (PDOException $e) {
$_SESSION['error'] = 'Failed to delete group: ' . $e->getMessage();
header("Location: group.php?id=$group_id");
exit;
}
} else {
$_SESSION['error'] = 'You need admin privileges to delete the group';
header("Location: group.php?id=$group_id");
exit;
}
}
// Get group messages
$stmt = $pdo->prepare("
SELECT gm.*, u.username
FROM group_messages gm
JOIN users u ON gm.user_id = u.id
WHERE gm.group_id = ?
ORDER BY gm.created_at DESC
LIMIT 50
");
$stmt->execute([$group_id]);
$messages = $stmt->fetchAll();
require_once 'header.php';
?>
<div class="max-w-4xl mx-auto">
<!-- Header -->
<div class="flex items-center justify-between mb-8">
<div>
<h1 class="text-3xl font-bold text-gray-800 mb-2"><?php echo htmlspecialchars($group['name']); ?></h1>
<p class="text-gray-600"><?php echo htmlspecialchars($group['description']); ?></p>
</div>
<div class="flex space-x-2">
<a href="groups.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 Groups
</a>
<a href="dashboard.php" class="bg-blue-100 text-blue-600 px-4 py-2 rounded-lg hover:bg-blue-200 transition">
<i class="fas fa-home mr-2"></i> Dashboard
</a>
</div>
</div>
<div class="flex flex-col lg:flex-row gap-6">
<!-- Group Chat -->
<div class="lg:w-2/3">
<div class="app-card p-6 h-full flex flex-col">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Group Chat</h2>
<!-- Messages Container - This will now expand to fill available space -->
<div class="flex-1 overflow-y-auto mb-4 space-y-4" style="max-height: 50vh;">
<?php if (empty($messages)): ?>
<div class="text-center py-8 text-gray-500">
<i class="fas fa-comments text-3xl text-gray-300 mb-3"></i>
<p>No messages yet. Start the conversation!</p>
</div>
<?php else: ?>
<?php foreach (array_reverse($messages) as $message): ?>
<div class="flex items-start space-x-3">
<div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center flex-shrink-0">
<i class="fas fa-user text-blue-500 text-sm"></i>
</div>
<div class="flex-1 min-w-0">
<div class="flex items-center space-x-2 mb-1">
<span class="font-semibold text-gray-800"><?php echo htmlspecialchars($message['username']); ?></span>
<span class="text-xs text-gray-500"><?php echo date('M j, g:i a', strtotime($message['created_at'])); ?></span>
</div>
<p class="text-gray-700 bg-gray-50 p-3 rounded-lg"><?php echo htmlspecialchars($message['message']); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Message Form -->
<form method="POST" action="group-actions.php" class="flex space-x-2 mt-auto">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="action" value="send_message">
<input type="hidden" name="group_id" value="<?php echo $group_id; ?>">
<input type="hidden" name="redirect" value="group.php?id=<?php echo $group_id; ?>">
<input type="text" name="message" placeholder="Type your message..."
class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500"
required>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-lg font-semibold hover:bg-blue-600 transition">
Send
</button>
</form>
</div>
</div>
<!-- Sidebar -->
<div class="space-y-6">
<!-- Members -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Members</h2>
<div class="space-y-3">
<?php foreach ($members as $member): ?>
<div class="flex items-center justify-between p-3 border rounded-lg hover:bg-gray-50 transition group">
<div class="flex items-center space-x-3">
<div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center">
<i class="fas fa-user text-blue-500 text-sm"></i>
</div>
<div>
<div class="flex items-center">
<a href="user-habits.php?user_id=<?php echo $member['id']; ?>"
class="font-medium text-gray-800 hover:text-blue-600">
<?php echo htmlspecialchars($member['username']); ?>
</a>
<?php if ($member['role'] === 'admin'): ?>
<span class="admin-badge ml-2">Admin</span>
<?php endif; ?>
</div>
<p class="text-xs text-gray-500"><?php echo $member['role']; ?></p>
</div>
</div>
<div class="flex items-center space-x-2">
<span class="text-xs text-gray-400">
<?php echo date('M j', strtotime($member['joined_at'])); ?>
</span>
<?php if ($group['role'] === 'admin' && $member['id'] != $user_id): ?>
<form method="POST" class="remove-member-form">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="remove_member" value="1">
<input type="hidden" name="member_id" value="<?php echo $member['id']; ?>">
<button type="button" onclick="confirmRemoveMember('<?php echo htmlspecialchars($member['username']); ?>', this.closest('form'))"
class="remove-member-btn" title="Remove member">
<i class="fas fa-times"></i>
</button>
</form>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if ($group['role'] === 'admin'): ?>
<div class="mt-4 pt-4 border-t">
<h3 class="font-semibold text-gray-700 mb-2">Add Member</h3>
<form method="POST" action="group-actions.php" class="flex space-x-2">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="action" value="add_member">
<input type="hidden" name="group_id" value="<?php echo $group_id; ?>">
<input type="hidden" name="redirect" value="group.php?id=<?php echo $group_id; ?>">
<input type="email" name="email" placeholder="Member's email"
class="flex-1 px-3 py-1 text-sm border border-gray-300 rounded focus:ring-blue-500 focus:border-blue-500"
required>
<button type="submit" class="bg-green-500 text-white px-3 py-1 rounded text-sm font-semibold hover:bg-green-600 transition">
Add
</button>
</form>
</div>
<?php endif; ?>
</div>
<!-- Group Info -->
<div class="app-card p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Group Info</h2>
<div class="space-y-2 text-sm">
<div class="flex justify-between">
<span class="text-gray-600">Created</span>
<span class="text-gray-800"><?php echo date('M j, Y', strtotime($group['created_at'])); ?></span>
</div>
<div class="flex justify-between">
<span class="text-gray-600">Members</span>
<span class="text-gray-800"><?php echo count($members); ?></span>
</div>
<div class="flex justify-between">
<span class="text-gray-600">Privacy</span>
<span class="text-gray-800"><?php echo $group['is_public'] ? 'Public' : 'Private'; ?></span>
</div>
<div class="flex justify-between">
<span class="text-gray-600">Your Role</span>
<span class="text-gray-800 capitalize"><?php echo $group['role']; ?></span>
</div>
</div>
<?php if ($group['allow_member_visibility']): ?>
<a href="group-members.php?group_id=<?php echo $group_id; ?>" class="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600 transition inline-block mt-4">
<i class="fas fa-users mr-2"></i> View Members' Habits
</a>
<?php endif; ?>
<?php if ($group['role'] === 'admin'): ?>
<div class="admin-controls">
<h3 class="font-semibold text-red-700 mb-3">Admin Controls</h3>
<form method="POST" class="delete-group-form">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="delete_group" value="1">
<button type="button" onclick="confirmDeleteGroup('<?php echo htmlspecialchars($group['name']); ?>', this.closest('form'))"
class="delete-group-btn w-full text-center">
<i class="fas fa-trash mr-2"></i>Delete Group
</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
// Confirm member removal
function confirmRemoveMember(username, form) {
if (confirm(`Are you sure you want to remove ${username} from the group? This action cannot be undone.`)) {
form.submit();
}
}
// Confirm group deletion
function confirmDeleteGroup(groupName, form) {
if (confirm(`WARNING: Are you sure you want to delete the group "${groupName}"? This will permanently delete all group data, messages, and member associations. This action cannot be undone!`)) {
form.submit();
}
}
// Show success/error messages
<?php if (isset($_SESSION['success'])): ?>
showToast('<?php echo addslashes($_SESSION['success']); ?>', 'success');
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
showToast('<?php echo addslashes($_SESSION['error']); ?>', 'error');
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
</script>
<?php require_once 'footer.php'; ?>