Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
actions.phpPHP Fil8.5 KB
admin.phpPHP Fil11.1 KB
auth.phpPHP Fil4.5 KB
change_password.phpPHP Fil3.4 KB
check_login.phpPHP Fil211 B
config.phpPHP Fil411 B
index.phpPHP Fil4 KB

admin.php

272 lines UTF-8 BOM Windows (CRLF) - Type: PHP Fil
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
<?php
require "check_login.php";
require_once 
'config.php';

$username $_SESSION['username'];
$userId $_SESSION['user_id'];
$role $_SESSION['role'] ?? 'user';

global 
$pdo;

// Hämta alla konton för användaren
    
$Qstmts $pdo->prepare("
        SELECT id, account_name, balance, created_at 
        FROM accounts 
        WHERE user_id = ? 
        ORDER BY created_at ASC
    "
);
    
$Qstmts->execute([$userId]);
    
$accounts $Qstmts->fetchAll(PDO::FETCH_ASSOC);

    
// Bygg SQL-fråga med filter
    
$sql "
        SELECT t.id, t.type, t.amount, t.date, a.account_name
        FROM transactions t
        JOIN accounts a ON t.account_id = a.id
        WHERE t.user_id = ?"
;
    
    
$params = [$userId];
    
    
// Filtrera efter konto
    
if (!empty($_GET['account'])) {
        
$sql .= " AND a.account_name = ?";
        
$params[] = $_GET['account'];
    }
    
    
// Filtrera efter typ
    
if (!empty($_GET['type'])) {
        
$sql .= " AND t.type = ?";
        
$params[] = $_GET['type'];
    }
    
    
$sql .= " ORDER BY t.date DESC LIMIT 50";
    
    
$Qstmts $pdo->prepare($sql);
    
$Qstmts->execute($params);
    
$filteredTransactions $Qstmts->fetchAll(PDO::FETCH_ASSOC);

    
// Beräkna totalt saldo
    
$totalBalance array_sum(array_column($accounts'balance'));
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Min Bank</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="admin-style.css">
    <link rel="icon" href="../m02/Favicon-a.jpg" type="image/x-icon">
</head>
<body>
<header>
    <nav>
        <h1>Välkommen, <?= htmlspecialchars($username?></h1>
        <div class="nav-links">
            <a href="change_password.php" class="btn-secondary">Byt Lösenord</a>
            <a href="auth.php?action=logout" class="btn-log">Logga ut</a>
        </div>
    </nav>
</header>

<!-- MEDDELANDE-->
<?php if (isset($_SESSION['message'])): ?>
    <div class="notice success">
        <?= htmlspecialchars($_SESSION['message']) ?>
    </div>
    <?php unset($_SESSION['message']); ?>
<?php 
endif; ?>

<?php if (isset($_SESSION['error'])): ?>
    <div class="notice error">
        <?= htmlspecialchars($_SESSION['error']) ?>
    </div>
    <?php unset($_SESSION['error']); ?>
<?php 
endif; ?>

<!-- GET-parameter meddelanden -->
<?php if (isset($_GET['error']) && $_GET['error'] === 'invalid_amount'): ?>
    <div class="notice error">Ogiltigt belopp.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'insufficient'): ?>
    <div class="notice error">Otillräckligt saldo.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'invalid_request'): ?>
    <div class="notice error">Ogiltig begäran. Försök igen.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'same_account'): ?>
    <div class="notice error">Kan inte överföra till samma konto.</div>
<?php elseif (isset($_GET['success'])): ?>
    <div class="notice success">Transaktionen är sparad.</div>
<?php endif; ?>

<main class="container">
    <!-- Kontoöversikt -->
    <section class="accounts-section">
        <h2>Mina Konton</h2>
        <div class="accounts-grid" id="accountsGrid">
            <?php foreach ($accounts as $account): ?>
                <div class="account-card" data-account="<?= htmlspecialchars($account['account_name']) ?>">
                    <h3><?= htmlspecialchars($account['account_name']) ?></h3>
                    <p class="balance"><?= number_format($account['balance'], 2','' '?> kr</p>
                    <small>Skapat: <?= date('Y-m-d'strtotime($account['created_at'])) ?></small>
                </div>
            <?php endforeach; ?>
        </div>
        <div class="total-balance">
            <strong>Totalt saldo:</strong> <?= number_format($totalBalance2','' '?> kr
        </div>
    </section>

    <!-- Transaktionsformulär -->
    <section class="actions-section">
        <h2>Hantera Transaktioner</h2>
        <div class="form-tabs">
            <button class="tab-btn active" data-tab="deposit">Insättning</button>
            <button class="tab-btn" data-tab="withdraw">Uttag</button>
            <button class="tab-btn" data-tab="transfer">Överföring</button>
            <button class="tab-btn" data-tab="newaccount">Nytt Konto</button>
        </div>

        <!-- Insättning -->
        <form action="actions.php?action=deposit" method="post" class="tab-content active" id="deposit">
            <h3>Insättning</h3>
            <select name="account" required>
                <option value="">Välj konto</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= $acc['id'?>"><?= htmlspecialchars($acc['account_name']) ?></option>
                <?php endforeach; ?>
            </select>
            <input type="number" name="amount" placeholder="Belopp" step="0.01" min="0.01" required>
            <button type="submit">Sätt in</button>
        </form>

        <!-- Uttag -->
        <form action="actions.php?action=withdraw" method="post" class="tab-content" id="withdraw">
            <h3>Uttag</h3>
            <select name="account" required>
                <option value="">Välj konto</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= $acc['id'?>"><?= htmlspecialchars($acc['account_name']) ?></option>
                <?php endforeach; ?>
            </select>
            <input type="number" name="amount" placeholder="Belopp" step="0.01" min="0.01" required>
            <button type="submit">Ta ut</button>
        </form>

        <!-- Överföring -->
        <form action="actions.php?action=transfer" method="post" class="tab-content" id="transfer">
            <h3>Överföring mellan mina konton</h3>
            <select name="from_account" required>
                <option value="">Från konto</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= $acc['id'?>"><?= htmlspecialchars($acc['account_name']) ?></option>
                <?php endforeach; ?>
            </select>
            <select name="to_account" required>
                <option value="">Till konto</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= $acc['id'?>"><?= htmlspecialchars($acc['account_name']) ?></option>
                <?php endforeach; ?>
            </select>
            <input type="number" name="amount" placeholder="Belopp" step="0.01" min="0.01" required>
            <button type="submit">Överför</button>
        </form>

        <!-- Nytt konto -->
        <form action="actions.php?action=open-account" method="post" class="tab-content" id="newaccount">
            <h3>Skapa nytt konto</h3>
            <input type="text" name="account_name" placeholder="Kontonamn (t.ex. Sparkonto)" required>
            <button type="submit">Skapa konto</button>
        </form>
    </section>

    <!-- Transaktionshistorik -->
    <section class="transactions-section">
        <h2>Transaktionshistorik</h2>
        <form method="get" action="admin.php" class="filter-controls" style="margin-bottom: 1em;">
            <select name="account">
                <option value="">Alla konton</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= htmlspecialchars($acc['account_name']) ?><?= (($_GET['account'] ?? '') === $acc['account_name']) ? 'selected' '' ?>>
                        <?= htmlspecialchars($acc['account_name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <select name="type">
                <option value="">Alla typer</option>
                <option value="deposit" <?= (($_GET['type'] ?? '') === 'deposit') ? 'selected' '' ?>>Insättning
                </option>
                <option value="withdraw" <?= (($_GET['type'] ?? '') === 'withdraw') ? 'selected' '' ?>>Uttag</option>
                <option value="transfer-in" <?= (($_GET['type'] ?? '') === 'transfer-in') ? 'selected' '' ?>>
                    Överföring in
                </option>
                <option value="transfer-out" <?= (($_GET['type'] ?? '') === 'transfer-out') ? 'selected' '' ?>>
                    Överföring ut
                </option>
                <option value="account-open" <?= (($_GET['type'] ?? '') === 'account-open') ? 'selected' '' ?>>Öppnat
                    konto
                </option>
            </select>
            <button type="submit">Filtrera</button>
        </form>
        <div class="tx-table-wrap">
            <table id="transactionsTable">
                <thead>
                <tr>
                    <th>Datum</th>
                    <th>Konto</th>
                    <th>Typ</th>
                    <th>Belopp</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($filteredTransactions as $t): ?>
                    <tr data-account="<?= htmlspecialchars($t['account_name']) ?>"
                        data-type="<?= htmlspecialchars($t['type']) ?>">
                        <td><?= date('Y-m-d H:i'strtotime($t['date'])) ?></td>
                        <td><?= htmlspecialchars($t['account_name']) ?></td>
                        <td><?= htmlspecialchars($t['type']) ?></td>
                        <td class="<?= $t['amount'] < 'negative' 'positive' ?>">
                            <?= number_format($t['amount'], 2','' '?> kr
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>

    <!-- Radera konto -->
    <section class="danger-zone">
        <h3>Radera mitt konto</h3>
        <p>Varning: Detta raderar alla dina konton och transaktioner permanent!</p>
        <form action="auth.php?action=delete" method="post"
              onsubmit="return confirm('Är du säker? Detta går inte att ångra!');">
            <button type="submit" class="btn-log">Radera mitt konto</button>
        </form>
    </section>
</main>


<script>
    // Tab-switching 
    document.querySelectorAll('.tab-btn').forEach(btn => {
        btn.addEventListener('click', () => {
            document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
            document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
            btn.classList.add('active');
            document.getElementById(btn.dataset.tab).classList.add('active');
        });
    });

    // Auto-göm noticer
    document.addEventListener('DOMContentLoaded', function () {
        var notice = document.querySelector('.notice.success, .notice.error');
        if (notice) {
            setTimeout(function () {
                notice.classList.add('is-hidden');
            }, 1500);
        }
    });
</script>
</body>
</html>