The following files exist in this folder. Click to view.
| Namn | Typ | Storlek |
|---|---|---|
| actions.php | PHP Fil | 7.9 KB |
| admin.php | PHP Fil | 13.7 KB |
| auth.php | PHP Fil | 4.5 KB |
| change_password.php | PHP Fil | 4.1 KB |
| check_login.php | PHP Fil | 911 B |
| index.php | PHP Fil | 4 KB |
| json_store.php | PHP Fil | 2.1 KB |
| transactions.json | JSON Fil | 4.6 KB |
| users.json | JSON Fil | 1.5 KB |
actions.php226 lines UTF-8 Windows (CRLF) - Type: PHP Fil123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
<?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;
}
?>