Visa Källkod

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

NamnTypStorlek
actions.phpPHP Fil7.9 KB
admin.phpPHP Fil13.7 KB
auth.phpPHP Fil4.5 KB
change_password.phpPHP Fil4.1 KB
check_login.phpPHP Fil911 B
index.phpPHP Fil4 KB
json_store.phpPHP Fil2.1 KB
transactions.jsonJSON Fil4.6 KB
users.jsonJSON Fil1.5 KB

actions.php

226 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
220
221
222
223
224
225
226
<?php
// börja session och sätta tidszon
session_start();
if (
function_exists('date_default_timezone_set')) {
    @
date_default_timezone_set('Europe/Stockholm');
}
// lägg till JSON-hanteringsfilen
require_once 'json_store.php';

// Kontrollera inloggning (krävs för att nå åtgärderna)
if (!isset($_SESSION['loggedin']) || !isset($_SESSION['username'])) {
    
header("Location: index.php?noaccess=1");
    exit;
}

// Hämta "action" från URL:en (GET) och "username" från sessionen
$action $_GET['action'] ?? '';
$username $_SESSION['username'];

// switch statements
switch ($action) {
    case 
'deposit':
        
// Insättning till valt konto
        
$accountName trim($_POST['account'] ?? '');
        
$amount = (int)($_POST['amount'] ?? 0);

        if (
$amount <= 0) {
            
$_SESSION['error'] = "Beloppet måste vara större än 0";
            
header("Location: admin.php");
            exit;
        }
        
        
// Kontrollera att kontot finns för användaren
        
$balances compute_balances_for_user($username);
        if (!
array_key_exists($accountName$balances)) {
            
$_SESSION['error'] = "Kontot hittades inte";
            
header("Location: admin.php");
            exit;
        }

        
// Skapa händelse (saldo beräknas alltid från händelser)
        
append_transaction([
            
'username' => $username,
            
'account' => $accountName,
            
'amount' => $amount,
            
'type' => 'deposit',
            
'date' => now_str(),
        ]);

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

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

        if (
$amount <= 0) {
            
$_SESSION['error'] = "Beloppet måste vara större än 0";
            
header("Location: admin.php");
            exit;
        }
        
        
// Kontrollera att kontot finns och att saldot räcker
        
$balances compute_balances_for_user($username);
        if (!
array_key_exists($accountName$balances)) {
            
$_SESSION['error'] = "Kontot hittades inte";
            
header("Location: admin.php");
            exit;
        }
        
        if (
$balances[$accountName] < $amount) {
            
$_SESSION['error'] = "Otillräckligt saldo. Du har " $balances[$accountName] . " kr på " htmlspecialchars($accountName);
            
header("Location: admin.php");
            exit;
        }

        
// Skapa händelse (negativt belopp för uttag)
        
append_transaction([
            
'username' => $username,
            
'account' => $accountName,
            
'amount' => -$amount,
            
'type' => 'withdraw',
            
'date' => now_str(),
        ]);
        
        
$_SESSION['message'] = "Uttag genomfört! -" $amount " kr";
        
header("Location: admin.php");
        exit;

    case 
'transfer':
        
// Överföring: välj från-konto alltid, till-konto bara om till dig själv
        
$fromAccount trim($_POST['from_account'] ?? '');
        
$toUser trim($_POST['to_user'] ?? '');
        
$toAccount trim($_POST['to_account'] ?? '');
        
$amount = (int)($_POST['amount'] ?? 0);

        if (
$amount <= 0) {
            
$_SESSION['error'] = "Beloppet måste vara större än 0";
            
header("Location: admin.php");
            exit;
        }
        
        if (
$fromAccount === '') {
            
$_SESSION['error'] = "Välj från vilket konto du vill överföra";
            
header("Location: admin.php");
            exit;
        }
        
        if (
$toUser === '') {
            
$_SESSION['error'] = "Ange mottagarens användarnamn";
            
header("Location: admin.php");
            exit;
        }

        
// Kontroll av avsändarkonto och saldo
        
$senderBalances compute_balances_for_user($username);
        if (!
array_key_exists($fromAccount$senderBalances)) {
            
$_SESSION['error'] = "Avsändarkontot hittades inte";
            
header("Location: admin.php");
            exit;
        }
        
        if (
$senderBalances[$fromAccount] < $amount) {
            
$_SESSION['error'] = "Otillräckligt saldo på " htmlspecialchars($fromAccount) . ". Du har " $senderBalances[$fromAccount] . " kr";
            
header("Location: admin.php");
            exit;
        }

        
// Om till dig själv: välj till-konto, annars alltid Huvudkonto
        
if ($toUser === $username) {
            if (
$toAccount === '') {
                
$_SESSION['error'] = "Välj till vilket av dina konton du vill överföra";
                
header("Location: admin.php");
                exit;
            }
            if (
$toAccount === $fromAccount) {
                
$_SESSION['error'] = "Du kan inte överföra till samma konto";
                
header("Location: admin.php");
                exit;
            }
            
$recipientUser $username;
            
$recipientAccount $toAccount;
        } else {
            
// Kontrollera att mottagaren finns
            
$allUsers load_users();
            
$recipientExists false;
            foreach (
$allUsers as $u) {
                if ((
$u['username'] ?? null) === $toUser) {
                    
$recipientExists true;
                    break;
                }
            }
            
            if (!
$recipientExists) {
                
$_SESSION['error'] = "Mottagaren finns inte";
                
header("Location: admin.php");
                exit;
            }
            
$recipientUser $toUser;
            
$recipientAccount 'Huvudkonto';
        }

        
// Skapa två händelser (en för avsändare, en för mottagare)
        
append_transaction([
            
'username' => $username,
            
'account' => $fromAccount,
            
'amount' => -$amount,
            
'type' => 'transfer-out',
            
'date' => now_str(),
        ]);
        
append_transaction([
            
'username' => $recipientUser,
            
'account' => $recipientAccount,
            
'amount' => $amount,
            
'type' => 'transfer-in',
            
'date' => now_str(),
        ]);
        
        
// Bekräftelsemeddelande för överföring
        
if ($recipientUser === $username) {
            
$_SESSION['message'] = "Överföring genomförd! " $amount " kr från $fromAccount till $recipientAccount";
        } else {
            
$_SESSION['message'] = "Överföring genomförd! " $amount " kr från $fromAccount till " .
                
htmlspecialchars($recipientUser) . " (Huvudkonto)";
        }
        
header("Location: admin.php");
        exit;

    case 
'open-account':
        
// Skapa ett nytt konto (0 kr) via händelse
        
$accountName trim($_POST['account_name'] ?? '');

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

        
// Kontrollera om kontot redan finns för användaren
        
$balances compute_balances_for_user($username);
        if (
array_key_exists($accountName$balances)) {
            
$_SESSION['error'] = "Ett konto med namnet '" htmlspecialchars($accountName) . "' finns redan";
            
header("Location: admin.php");
            exit;
        }

        
// Skapa kontot genom en händelse (0 kr).
        
append_transaction([
            
'username' => $username,
            
'account' => $accountName,
            
'amount' => 0,
            
'type' => 'account-open',
            
'date' => now_str(),
        ]);

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

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