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

actions.php

219 lines UTF-8 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
<?php
session_start
();
require_once 
'config.php';

// Kontrollera att användaren är inloggad
if (!isset($_SESSION['loggedin']) || !isset($_SESSION['user_id'])) {
    
header("Location: index.php?noaccess=1");
    exit;
}

$action $_GET['action'] ?? '';
$userId $_SESSION['user_id'];

global 
$pdo;

switch (
$action) {
    case 
'deposit':
            
// Insättning på konto
            
$accountId = (int)($_POST['account'] ?? 0);
            
$amount = (float)($_POST['amount'] ?? 0);

            
// Kontrollera att beloppet är giltigt
            
if ($amount <= 0) {
                
$_SESSION['error'] = "Beloppet måste vara större än 0";
                
header("Location: admin.php");
                exit;
            }

            
// Kontrollera att kontot tillhör användaren
            
$Qstmts $pdo->prepare("SELECT id FROM accounts WHERE id = ? AND user_id = ?");
            
$Qstmts->execute([$accountId$userId]);
            if (!
$Qstmts->fetch()) {
                
$_SESSION['error'] = "Kontot hittades inte";
                
header("Location: admin.php");
                exit;
            }

            
// Uppdatera saldo
            
$Qstmts $pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?");
            
$Qstmts->execute([$amount$accountId]);

            
// Skapa transaktion
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'deposit', ?, NOW())
            "
);
            
$Qstmts->execute([$userId$accountId$amount]);

            
$_SESSION['message'] = "Insättning genomförd! +" $amount " kr";
            
header("Location: admin.php");
            exit;

        case 
'withdraw':
            
// Uttag från konto
            
$accountId = (int)($_POST['account'] ?? 0);
            
$amount = (float)($_POST['amount'] ?? 0);

            
// Kontrollera att beloppet är giltigt
            
if ($amount <= 0) {
                
$_SESSION['error'] = "Beloppet måste vara större än 0";
                
header("Location: admin.php");
                exit;
            }

            
// Hämta nuvarande saldo
            
$Qstmts $pdo->prepare("SELECT balance, account_name FROM accounts WHERE id = ? AND user_id = ?");
            
$Qstmts->execute([$accountId$userId]);
            
$account $Qstmts->fetch();

            if (!
$account) {
                
$_SESSION['error'] = "Kontot hittades inte";
                
header("Location: admin.php");
                exit;
            }

            
// Kontrollera om tillräckligt saldo finns
            
if ($account['balance'] < $amount) {
                
$_SESSION['error'] = "Otillräckligt saldo. Du har " $account['balance'] . " kr på " htmlspecialchars($account['account_name']);
                
header("Location: admin.php");
                exit;
            }

            
// Uppdatera saldo
            
$Qstmts $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?");
            
$Qstmts->execute([$amount$accountId]);

            
// Skapa transaktion
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'withdraw', ?, NOW())
            "
);
            
$Qstmts->execute([$userId$accountId, -$amount]);

            
$_SESSION['message'] = "Uttag genomfört! -" $amount " kr";
            
header("Location: admin.php");
            exit;

        case 
'transfer':
            
// Överföring mellan konton
            
$fromAccountId = (int)($_POST['from_account'] ?? 0);
            
$toAccountId = (int)($_POST['to_account'] ?? 0);
            
$amount = (float)($_POST['amount'] ?? 0);

            
// Kontrollera att beloppet är giltigt
            
if ($amount <= 0) {
                
$_SESSION['error'] = "Beloppet måste vara större än 0";
                
header("Location: admin.php");
                exit;
            }

            
// Förhindra överföring till samma konto
            
if ($fromAccountId === $toAccountId) {
                
$_SESSION['error'] = "Kan inte överföra till samma konto";
                
header("Location: admin.php");
                exit;
            }

            
// Hämta avsändarkonto
            
$Qstmts $pdo->prepare("SELECT balance, account_name FROM accounts WHERE id = ? AND user_id = ?");
            
$Qstmts->execute([$fromAccountId$userId]);
            
$fromAccount $Qstmts->fetch();

            if (!
$fromAccount) {
                
$_SESSION['error'] = "Avsändarkontot hittades inte";
                
header("Location: admin.php");
                exit;
            }

            
// Kontrollera om tillräckligt saldo finns
            
if ($fromAccount['balance'] < $amount) {
                
$_SESSION['error'] = "Otillräckligt saldo på " htmlspecialchars($fromAccount['account_name']) . ". Du har " $fromAccount['balance'] . " kr";
                
header("Location: admin.php");
                exit;
            }

            
// Kontrollera att mottagarkontot tillhör användaren
            
$Qstmts $pdo->prepare("SELECT account_name FROM accounts WHERE id = ? AND user_id = ?");
            
$Qstmts->execute([$toAccountId$userId]);
            
$toAccount $Qstmts->fetch();

            if (!
$toAccount) {
                
$_SESSION['error'] = "Mottagarkontot hittades inte";
                
header("Location: admin.php");
                exit;
            }

            
// Dra från avsändarkonto
            
$Qstmts $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?");
            
$Qstmts->execute([$amount$fromAccountId]);

            
// Lägg till på mottagarkonto
            
$Qstmts $pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?");
            
$Qstmts->execute([$amount$toAccountId]);

            
// Skapa transaktion för avsändare
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'transfer-out', ?, NOW())
            "
);
            
$Qstmts->execute([$userId$fromAccountId, -$amount]);

            
// Skapa transaktion för mottagare
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'transfer-in', ?, NOW())
            "
);
            
$Qstmts->execute([$userId$toAccountId$amount]);

            
$_SESSION['message'] = "Överföring genomförd! " $amount " kr från " .
                
htmlspecialchars($fromAccount['account_name']) . " till " .
                
htmlspecialchars($toAccount['account_name']);
            
header("Location: admin.php");
            exit;

        case 
'open-account':
            
// Skapa nytt konto
            
$accountName trim($_POST['account_name'] ?? '');

            
// Kontrollera att kontonamn är ifyllt
            
if (empty($accountName)) {
                
$_SESSION['error'] = "Kontonamn får inte vara tomt";
                
header("Location: admin.php");
                exit;
            }

            
// Kontrollera om kontot redan finns
            
$Qstmts $pdo->prepare("SELECT id FROM accounts WHERE user_id = ? AND account_name = ?");
            
$Qstmts->execute([$userId$accountName]);
            if (
$Qstmts->fetch()) {
                
$_SESSION['error'] = "Ett konto med namnet '" htmlspecialchars($accountName) . "' finns redan";
                
header("Location: admin.php");
                exit;
            }

            
// Skapa konto
            
$Qstmts $pdo->prepare("
                INSERT INTO accounts (user_id, account_name, balance, created_at) 
                VALUES (?, ?, 0, NOW())
            "
);
            
$Qstmts->execute([$userId$accountName]);

            
$newAccountId $pdo->lastInsertId();

            
// Skapa transaktion för kontoöppning
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'account-open', 0, NOW())
            "
);
            
$Qstmts->execute([$userId$newAccountId]);

            
$_SESSION['message'] = "Nytt konto skapat! " htmlspecialchars($accountName);
            
header("Location: admin.php");
            exit;

        default:
            
header("Location: admin.php");
            exit;
}