update
This commit is contained in:
parent
ab2311587c
commit
9fcd228eb2
13 changed files with 398 additions and 14 deletions
127
tippspiel/src/Core/Crypto.php
Normal file
127
tippspiel/src/Core/Crypto.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Symmetrische Verschlüsselung für sensible Felder (aktuell die IBAN).
|
||||
*
|
||||
* Der Schlüssel liegt NICHT in der Datenbank, sondern in der Umgebungsvariable
|
||||
* IBAN_KEY (base64-codiert, 32 Byte). Damit enthalten DB-Dumps, Backups und
|
||||
* ein direkter DB-Lesezugriff keine IBAN im Klartext.
|
||||
*
|
||||
* Werte werden mit dem Präfix "enc:v1:" gespeichert, damit Altbestand im
|
||||
* Klartext (vor der Einführung der Verschlüsselung) von verschlüsselten Werten
|
||||
* unterscheidbar bleibt und schrittweise migriert werden kann.
|
||||
*/
|
||||
class Crypto {
|
||||
|
||||
/** Präfix, mit dem verschlüsselte Werte markiert werden. */
|
||||
const PREFIX = 'enc:v1:';
|
||||
|
||||
/**
|
||||
* Liefert den 32-Byte-Schlüssel aus IBAN_KEY (base64) oder null, wenn keiner
|
||||
* konfiguriert ist.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function key() {
|
||||
$key = getenv('IBAN_KEY');
|
||||
if ($key === false || $key === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$decoded = base64_decode($key, true);
|
||||
if ($decoded === false || strlen($decoded) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
|
||||
throw new \RuntimeException(
|
||||
'IBAN_KEY ist ungültig: erwartet wird ein base64-codierter 32-Byte-Schlüssel.'
|
||||
);
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob ein Wert bereits verschlüsselt gespeichert ist.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public static function isEncrypted($value) {
|
||||
return is_string($value) && str_starts_with($value, self::PREFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verschlüsselt einen Klartext. Leere Werte bleiben leer, bereits
|
||||
* verschlüsselte Werte werden unverändert zurückgegeben.
|
||||
*
|
||||
* Ohne konfigurierten Schlüssel wird der Klartext unverändert zurückgegeben
|
||||
* (Abwärtskompatibilität) und eine Warnung ins Log geschrieben.
|
||||
*
|
||||
* @param string $plaintext
|
||||
* @return string
|
||||
*/
|
||||
public static function encrypt($plaintext) {
|
||||
$plaintext = trim((string)$plaintext);
|
||||
if ($plaintext === '' || self::isEncrypted($plaintext)) {
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
$key = self::key();
|
||||
if ($key === null) {
|
||||
error_log('Crypto: IBAN_KEY nicht gesetzt - Wert wird unverschlüsselt gespeichert.');
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);
|
||||
return self::PREFIX . base64_encode($nonce . $ciphertext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Entschlüsselt einen Wert. Unverschlüsselte Werte (Altbestand im Klartext)
|
||||
* werden unverändert zurückgegeben.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function decrypt($value) {
|
||||
$value = (string)$value;
|
||||
if ($value === '' || !self::isEncrypted($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$key = self::key();
|
||||
if ($key === null) {
|
||||
throw new \RuntimeException(
|
||||
'Crypto: IBAN_KEY fehlt - verschlüsselte Werte können nicht gelesen werden.'
|
||||
);
|
||||
}
|
||||
|
||||
$data = base64_decode(substr($value, strlen(self::PREFIX)), true);
|
||||
if ($data === false || strlen($data) < SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) {
|
||||
throw new \RuntimeException('Crypto: verschlüsselter Wert ist beschädigt.');
|
||||
}
|
||||
|
||||
$nonce = substr($data, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||
$ciphertext = substr($data, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
|
||||
if ($plaintext === false) {
|
||||
throw new \RuntimeException('Crypto: Entschlüsselung fehlgeschlagen (falscher Schlüssel?).');
|
||||
}
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maskiert eine IBAN für die Anzeige: nur die letzten 4 Zeichen bleiben sichtbar.
|
||||
*
|
||||
* @param string $iban
|
||||
* @return string
|
||||
*/
|
||||
public static function maskIban($iban) {
|
||||
$iban = trim((string)$iban);
|
||||
if ($iban === '') {
|
||||
return '';
|
||||
}
|
||||
$visible = substr($iban, -4);
|
||||
return str_repeat('*', max(0, strlen($iban) - 4)) . $visible;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Crypto;
|
||||
use App\Core\Database;
|
||||
|
||||
/**
|
||||
|
|
@ -314,27 +315,53 @@ class Ranking {
|
|||
* Geldrangliste: Summe aller Ausschüttungen je Spieler, absteigend sortiert.
|
||||
*
|
||||
* @param array $result Ergebnis von compute()
|
||||
* @return array Liste von ['user_id', 'name', 'amount', 'iban', 'bankname', 'blz', 'kontonr']
|
||||
* @param bool $withBankData Bankdaten (IBAN etc.) nur für Admins mitliefern/entschlüsseln
|
||||
* @return array Liste von ['user_id', 'name', 'amount'] und optional
|
||||
* ['iban', 'iban_masked', 'bankname', 'blz', 'kontonr']
|
||||
*/
|
||||
public static function moneyRanking(array $result) {
|
||||
public static function moneyRanking(array $result, $withBankData = false) {
|
||||
$list = [];
|
||||
foreach (self::payoutItems($result) as $userId => $items) {
|
||||
$user = $result['users'][$userId];
|
||||
$list[] = [
|
||||
$row = [
|
||||
'user_id' => $userId,
|
||||
'name' => $user['name'],
|
||||
'amount' => array_sum(array_column($items, 'amount')),
|
||||
'iban' => $user['iban'],
|
||||
'bankname' => $user['bankname'],
|
||||
'blz' => $user['blz'],
|
||||
'kontonr' => $user['kontonr'],
|
||||
];
|
||||
|
||||
if ($withBankData) {
|
||||
$iban = self::readableIban($user['iban']);
|
||||
$row['iban'] = $iban;
|
||||
$row['iban_masked'] = Crypto::maskIban($iban);
|
||||
$row['bankname'] = $user['bankname'];
|
||||
$row['blz'] = $user['blz'];
|
||||
$row['kontonr'] = $user['kontonr'];
|
||||
}
|
||||
|
||||
$list[] = $row;
|
||||
}
|
||||
|
||||
usort($list, fn($a, $b) => [$b['amount'], $a['name']] <=> [$a['amount'], $b['name']]);
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Entschlüsselt eine gespeicherte IBAN. Bei einem Fehler (z.B. fehlender
|
||||
* Schlüssel) wird eine leere Zeichenkette zurückgegeben und geloggt, damit
|
||||
* die Kontoliste nicht komplett abbricht.
|
||||
*
|
||||
* @param mixed $iban
|
||||
* @return string
|
||||
*/
|
||||
private static function readableIban($iban) {
|
||||
try {
|
||||
return Crypto::decrypt($iban);
|
||||
} catch (\Throwable $e) {
|
||||
error_log('Ranking: IBAN konnte nicht entschlüsselt werden: ' . $e->getMessage());
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tagessieger einer Runde.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Crypto;
|
||||
use App\Core\Database;
|
||||
|
||||
class User {
|
||||
|
|
@ -223,7 +224,7 @@ class User {
|
|||
foreach (self::BANK_FIELDS as $field) {
|
||||
if (array_key_exists($field, $data)) {
|
||||
$sql .= ", `$field` = :$field";
|
||||
$params[":$field"] = trim((string)$data[$field]);
|
||||
$params[":$field"] = self::prepareBankValue($field, $data[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -258,7 +259,7 @@ class User {
|
|||
foreach (self::BANK_FIELDS as $field) {
|
||||
if (array_key_exists($field, $data)) {
|
||||
$sql .= ", `$field` = :$field";
|
||||
$params[":$field"] = trim((string)$data[$field]);
|
||||
$params[":$field"] = self::prepareBankValue($field, $data[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -312,4 +313,17 @@ class User {
|
|||
$stmt = $db->prepare("UPDATE users SET deleted = NULL, updated_at = NOW() WHERE id = ?");
|
||||
return $stmt->execute([$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bereitet einen Bank-Feldwert für das Speichern vor: die IBAN wird
|
||||
* verschlüsselt, alle übrigen Felder werden nur getrimmt.
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
private static function prepareBankValue($field, $value) {
|
||||
$value = trim((string)$value);
|
||||
return $field === 'iban' ? Crypto::encrypt($value) : $value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,18 @@ $euro = fn($amount) => number_format($amount, 2, ',', '.') . ' €';
|
|||
<p><?php echo htmlspecialchars($currentSeason['name']); ?>, Stand: <?php echo htmlspecialchars($result['rounds'][$result['max_round_id']]['round']['name']); ?>
|
||||
<?php if (!$result['is_final']): ?>(ohne Platzierungsgeld, das erst nach der letzten Runde feststeht)<?php endif; ?></p>
|
||||
|
||||
<?php
|
||||
$anyPayout = false;
|
||||
foreach ($moneyRanking as $entry) {
|
||||
if ($entry['amount'] > 0) {
|
||||
$anyPayout = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($anyPayout): ?>
|
||||
<button type="button" id="iban-toggle-all">IBANs anzeigen</button>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="table-container">
|
||||
<table class="accounts-table">
|
||||
<thead>
|
||||
|
|
@ -30,14 +42,22 @@ $euro = fn($amount) => number_format($amount, 2, ',', '.') . ' €';
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($moneyRanking as $entry):
|
||||
$missingAccount = $entry['amount'] > 0 && empty($entry['iban']) && empty($entry['kontonr']);
|
||||
$hasPayout = $entry['amount'] > 0;
|
||||
$missingAccount = $hasPayout && empty($entry['iban']) && empty($entry['kontonr']);
|
||||
?>
|
||||
<tr <?php if ($entry['amount'] <= 0) echo 'class="no-payout"'; ?>>
|
||||
<tr <?php if (!$hasPayout) echo 'class="no-payout"'; ?>>
|
||||
<td><a href="/index.php?page=money_ranking&user_id=<?php echo (int)$entry['user_id']; ?>"><?php echo htmlspecialchars($entry['name']); ?></a></td>
|
||||
<td class="amount"><?php echo $euro($entry['amount']); ?></td>
|
||||
<td><?php echo htmlspecialchars((string)$entry['bankname']); ?></td>
|
||||
<td class="iban <?php if ($missingAccount) echo 'missing'; ?>">
|
||||
<?php echo $missingAccount ? 'fehlt' : htmlspecialchars((string)$entry['iban']); ?>
|
||||
<?php if ($missingAccount): ?>
|
||||
fehlt
|
||||
<?php else: ?>
|
||||
<span class="iban-masked"><?php echo htmlspecialchars((string)$entry['iban_masked']); ?></span>
|
||||
<?php if ($hasPayout): ?>
|
||||
<span class="iban-full" hidden><?php echo htmlspecialchars((string)$entry['iban']); ?></span>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo htmlspecialchars((string)$entry['blz']); ?></td>
|
||||
<td><?php echo htmlspecialchars((string)$entry['kontonr']); ?></td>
|
||||
|
|
@ -61,6 +81,23 @@ $euro = fn($amount) => number_format($amount, 2, ',', '.') . ' €';
|
|||
.accounts-table td.missing { color: #c00; font-family: inherit; font-weight: bold; }
|
||||
.accounts-table tr.no-payout td { color: #999; }
|
||||
.accounts-table tr.no-payout td a { color: #999; }
|
||||
#iban-toggle-all { margin-bottom: 0.75em; cursor: pointer; }
|
||||
</style>
|
||||
|
||||
<script>
|
||||
var ibanToggleAll = document.getElementById('iban-toggle-all');
|
||||
if (ibanToggleAll) {
|
||||
ibanToggleAll.addEventListener('click', function () {
|
||||
var show = ibanToggleAll.getAttribute('data-visible') !== '1';
|
||||
document.querySelectorAll('.iban-full').forEach(function (full) {
|
||||
full.hidden = !show;
|
||||
var masked = full.parentElement.querySelector('.iban-masked');
|
||||
if (masked) masked.hidden = show;
|
||||
});
|
||||
ibanToggleAll.setAttribute('data-visible', show ? '1' : '0');
|
||||
ibanToggleAll.textContent = show ? 'IBANs verbergen' : 'IBANs anzeigen';
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<?php require __DIR__ . '/../partials/footer.php'; ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue