Källkodsbrowser

admin.php

171 lines UTF-8 Unix (LF)
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
<?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($filePathFILE_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($filePathimplode("\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($kontoFilFILE_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>&copy; 2025 Din Bank – Adminpanel</p>
    </footer>
</body>

</html>