-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_moderator.php
executable file
·209 lines (188 loc) · 7.21 KB
/
add_moderator.php
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
<?php
session_start();
ob_start(); // Start output buffering
include 'header.php'; // Ensure no output in header.php
require_once 'conn.php'; // Database connection settings
require_once 'admin_auth.php'; // Include admin authentication
require_non_player();
// Check if the user is logged in
if (!isset($_SESSION['username'])) {
die("Access denied: You must log in first.");
}
$username = $_SESSION['username'];
// Check if the user is listed in the authentication file
if (!array_key_exists($username, $adminAuth)) {
die("Access denied: You do not have the required permissions.");
}
// Secondary authentication
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['auth_submit'])) {
$provided_password = $_POST['auth_password'];
$stored_password = $adminAuth[$username];
if ($provided_password === $stored_password) {
$_SESSION['double_authenticated'] = true;
} else {
die("<p style='color: red;'>Invalid secondary password. Access denied.</p>");
}
}
// Display secondary authentication form if not authenticated
if (!isset($_SESSION['double_authenticated']) || $_SESSION['double_authenticated'] !== true) {
echo <<<HTML
<form method="POST" action="">
<h1>Admin Authentication</h1>
<label for="auth_password">Enter Secondary Password:</label>
<input type="password" id="auth_password" name="auth_password" required>
<button type="submit" name="auth_submit">Authenticate</button>
</form>
HTML;
ob_end_flush(); // Ensure output is flushed properly
exit;
}
// Authentication successful, proceed with the rest of the page
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch tournaments
$tournamentQuery = "
SELECT t.id, t.name AS tournament_name,
IFNULL(GROUP_CONCAT(u.username SEPARATOR ', '), '') AS moderators
FROM tournaments t
LEFT JOIN tournament_moderators tm ON t.id = tm.tournament_id
LEFT JOIN users u ON tm.user_id = u.id
GROUP BY t.id
ORDER BY t.name
";
$tournamentResult = $conn->query($tournamentQuery);
if (!$tournamentResult) {
die("Error fetching tournaments: " . $conn->error);
}
// Fetch users
$userQuery = "SELECT id, username FROM users ORDER BY username";
$userResult = $conn->query($userQuery);
if (!$userResult) {
die("Error fetching users: " . $conn->error);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['delete'])) {
$tournamentId = $_POST['tournament_id'];
$moderatorId = $_POST['moderator_id'];
$deleteQuery = "DELETE FROM tournament_moderators WHERE tournament_id = ? AND user_id = ?";
$stmt = $conn->prepare($deleteQuery);
$stmt->bind_param('ii', $tournamentId, $moderatorId);
if ($stmt->execute()) {
echo "<p style='color: green;'>Moderator removed from the tournament.</p>";
} else {
echo "<p style='color: red;'>Error removing moderator: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
} elseif (isset($_POST['edit'])) {
$tournamentId = $_POST['tournament_id'];
$moderatorId = $_POST['moderator_id'];
$insertQuery = "INSERT INTO tournament_moderators (tournament_id, user_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE user_id = user_id";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param('ii', $tournamentId, $moderatorId);
if ($stmt->execute()) {
echo "<p style='color: green;'>Moderator added to the tournament.</p>";
} else {
echo "<p style='color: red;'>Error adding moderator: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
}
// Refresh the page to reflect changes
header("Refresh:0");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assign Moderator</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Welcome, <?php echo htmlspecialchars($username); ?>! You have successfully authenticated.</h1>
<h2>Assign a Moderator to a Tournament</h2>
<form action="" method="POST">
<label for="tournament_id">Select Tournament:</label>
<select name="tournament_id" id="tournament_id" required>
<option value="">-- Select Tournament --</option>
<?php $tournamentResult->data_seek(0); while ($tournament = $tournamentResult->fetch_assoc()) { ?>
<option value="<?php echo $tournament['id']; ?>">
<?php echo htmlspecialchars($tournament['tournament_name']); ?>
</option>
<?php } ?>
</select>
<br><br>
<label for="moderator_id">Select Moderator:</label>
<select name="moderator_id" id="moderator_id" required>
<option value="">-- Select User --</option>
<?php while ($user = $userResult->fetch_assoc()) { ?>
<option value="<?php echo $user['id']; ?>">
<?php echo htmlspecialchars($user['username']); ?>
</option>
<?php } ?>
</select>
<br><br>
<button type="submit" name="edit">Add Moderator</button>
</form>
<h2>Tournaments and Moderators</h2>
<table>
<thead>
<tr>
<th>Tournament Name</th>
<th>Moderators</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$tournamentResult->data_seek(0);
while ($row = $tournamentResult->fetch_assoc()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['tournament_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['moderators']) . "</td>";
echo "<td>";
$moderators = $row['moderators'] !== '' ? explode(', ', $row['moderators']) : [];
foreach ($moderators as $moderator) {
$moderatorIdQuery = "SELECT id FROM users WHERE username = ?";
$stmt = $conn->prepare($moderatorIdQuery);
$stmt->bind_param('s', $moderator);
$stmt->execute();
$stmt->bind_result($moderatorId);
$stmt->fetch();
$stmt->close();
echo "<form action='' method='POST' style='display:inline;'>";
echo "<input type='hidden' name='tournament_id' value='" . $row['id'] . "'>";
echo "<input type='hidden' name='moderator_id' value='" . $moderatorId . "'>";
echo "<button type='submit' name='delete'>Remove " . htmlspecialchars($moderator) . "</button>";
echo "</form> ";
}
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
<?php
$conn->close();
ob_end_flush();
?>