<?php
require 'check_login.php';
// Endast admin får komma hit
if ($_SESSION['role'] !== 'admin') {
header("Location: user.php?msg=Du har inte rättigheter");
exit();
}
$filePath = __DIR__ . "/users.txt";
$kontoDir = __DIR__ . "/konton/";
if (!file_exists($filePath))
die("users.txt saknas");
// Hämta alla användare
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// Ta bort användare
if (isset($_GET['delete'])) {
$userToDelete = $_GET['delete'];
$newLines = [];
foreach ($lines as $line) {
list($u, $p, $r) = explode(";", $line);
if ($u !== $userToDelete)
$newLines[] = $line;
}
file_put_contents($filePath, implode("\n", $newLines) . "\n");
// Ta bort konto-fil också
$kontoFil = $kontoDir . $userToDelete . ".txt";
if (file_exists($kontoFil))
unlink($kontoFil);
header("Location: admin.php?msg=Användaren $userToDelete har tagits bort");
exit();
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Adminpanel</title>
<style>
body {
font-family: Arial;
background: #f0f2f5;
margin: 0;
}
header,
footer {
background: #333;
color: white;
text-align: center;
padding: 15px;
}
table {
border-collapse: collapse;
width: 90%;
margin: 20px auto;
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
}
th,
td {
padding: 12px;
border-bottom: 1px solid #ddd;
text-align: center;
}
th {
background: #1b91f8;
color: white;
}
tr:hover {
background: #f1f1f1;
}
a.btn {
background: #f44336;
color: white;
padding: 6px 10px;
border-radius: 4px;
text-decoration: none;
transition: 0.3s;
}
a.btn:hover {
background: #d32f2f;
}
.logout {
background: #555;
color: white;
padding: 8px 14px;
border-radius: 5px;
text-decoration: none;
}
.logout:hover {
background: #333;
}
.msg {
text-align: center;
margin-top: 10px;
color: green;
}
</style>
<script>
function confirmDelete(username) {
if (confirm("Är du säker på att du vill ta bort användaren '" + username + "'?")) {
window.location = "admin.php?delete=" + username;
}
}
</script>
</head>
<body>
<header>
<h1>Adminpanel</h1>
<p>Inloggad som <?= htmlspecialchars($_SESSION['username']) ?></p>
<a href="logout.php" class="logout">Logga ut</a>
</header>
<?php if (!empty($_GET['msg'])): ?>
<p class="msg"><?= htmlspecialchars($_GET['msg']) ?></p>
<?php endif; ?>
<table>
<tr>
<th>Användarnamn</th>
<th>Roll</th>
<th>Saldo</th>
<th>Åtgärder</th>
</tr>
<?php
foreach ($lines as $line):
list($u, $p, $r) = explode(";", $line);
if ($r === "admin")
continue;
$kontoFil = $kontoDir . $u . ".txt";
$saldo = file_exists($kontoFil)
? array_sum(array_map('intval', file($kontoFil, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)))
: 0;
?>
<tr>
<td><?= htmlspecialchars($u) ?></td>
<td><?= htmlspecialchars($r) ?></td>
<td><?= $saldo ?> kr</td>
<td><a href="#" class="btn" onclick="confirmDelete('<?= $u ?>')">Ta bort</a></td>
</tr>
<?php endforeach; ?>
</table>
<footer>
<p>© 2025 Din Bank – Adminpanel</p>
</footer>
</body>
</html>