update
This commit is contained in:
parent
ab2311587c
commit
9fcd228eb2
13 changed files with 398 additions and 14 deletions
65
tools/encrypt_ibans.php
Normal file
65
tools/encrypt_ibans.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
// Migrations-Skript: verschlüsselt vorhandene IBAN-Klartextwerte in der
|
||||
// `users`-Tabelle mit dem Schlüssel aus IBAN_KEY. Idempotent: bereits
|
||||
// verschlüsselte Werte (Präfix "enc:v1:") werden übersprungen.
|
||||
//
|
||||
// Aufruf (im Docker-Container, IBAN_KEY muss gesetzt sein):
|
||||
// docker compose exec app php encrypt_ibans.php
|
||||
|
||||
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
||||
$user = getenv('DB_USER') ?: 'root';
|
||||
$pass = getenv('DB_PASS') ?: '';
|
||||
$dbName = getenv('DB_NEW_NAME') ?: 'tippspiel2027';
|
||||
|
||||
// Crypto-Klasse laden (funktioniert im Container und direkt auf dem Host).
|
||||
$cryptoCandidates = [
|
||||
__DIR__ . '/../tippspiel/src/Core/Crypto.php',
|
||||
__DIR__ . '/src/Core/Crypto.php',
|
||||
];
|
||||
$cryptoFile = null;
|
||||
foreach ($cryptoCandidates as $candidate) {
|
||||
if (file_exists($candidate)) {
|
||||
$cryptoFile = $candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($cryptoFile === null) {
|
||||
die("Fehler: Crypto.php nicht gefunden.\n");
|
||||
}
|
||||
require_once $cryptoFile;
|
||||
|
||||
use App\Core\Crypto;
|
||||
|
||||
try {
|
||||
if (Crypto::key() === null) {
|
||||
die("Fehler: IBAN_KEY ist nicht gesetzt. Bitte zuerst einen Schlüssel erzeugen "
|
||||
. "(openssl rand -base64 32) und als Umgebungsvariable setzen.\n");
|
||||
}
|
||||
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$dbName;charset=utf8mb4", $user, $pass);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
$rows = $pdo->query("SELECT id, iban FROM users WHERE iban IS NOT NULL AND iban <> ''")->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$encrypted = 0;
|
||||
$skipped = 0;
|
||||
$stmt = $pdo->prepare("UPDATE users SET iban = ?, updated_at = NOW() WHERE id = ?");
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$iban = (string)$row['iban'];
|
||||
if (Crypto::isEncrypted($iban)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$stmt->execute([Crypto::encrypt($iban), $row['id']]);
|
||||
$encrypted++;
|
||||
}
|
||||
|
||||
echo "Fertig: {$encrypted} IBAN(s) verschlüsselt, {$skipped} bereits verschlüsselt übersprungen.\n";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
die("Datenbankfehler: " . $e->getMessage() . "\n");
|
||||
} catch (\Throwable $e) {
|
||||
die("Fehler: " . $e->getMessage() . "\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue