adasd
All checks were successful
Deploy / deploy (push) Successful in 58s

This commit is contained in:
2026-07-30 00:04:51 +02:00
parent 76591569b6
commit f6cb211859
10 changed files with 261 additions and 22 deletions

View File

@@ -8,6 +8,15 @@ use App\Avatar\Lorelei;
final class AccountPages
{
private const ENCRYPTED_PROFILE_FIELDS = [
'first_name',
'last_name',
'contact_phone',
'profession',
'languages',
'about',
];
public static function register(App $app): array
{
$flash = $app->flash()->get();
@@ -169,11 +178,17 @@ final class AccountPages
$action = $_POST['action'] ?? '';
try {
if ($action === 'profile') {
$crypto = self::requireCrypto($crypto, 'Profil');
$languages = $_POST['languages'] ?? '';
if (is_array($languages)) {
$languages = implode(', ', array_map('trim', $languages));
}
$phoneEnc = $crypto ? $crypto->encrypt(trim((string)$_POST['contact_phone'])) : trim((string)$_POST['contact_phone']);
$firstNameEnc = self::encryptOptionalProfileField($crypto, trim((string)$_POST['first_name']));
$lastNameEnc = self::encryptOptionalProfileField($crypto, trim((string)$_POST['last_name']));
$phoneEnc = self::encryptOptionalProfileField($crypto, trim((string)$_POST['contact_phone']));
$professionEnc = self::encryptOptionalProfileField($crypto, trim((string)$_POST['profession']));
$languagesEnc = self::encryptOptionalProfileField($crypto, trim((string)$languages));
$aboutEnc = self::encryptOptionalProfileField($crypto, trim((string)$_POST['about']));
$locationPreference = (string)($_POST['location_tracking_preference'] ?? 'prompt');
if ($profileSettings) {
$profileSettings->ensureSchema();
@@ -181,13 +196,13 @@ final class AccountPages
$stmt = $pdo?->prepare('UPDATE user_profiles SET display_name=:name, first_name=:fname, last_name=:lname, zip=:zip, city=:city, profession=:prof, languages=:langs, about=:about, contact_phone=:phone, location_tracking_preference=:locationPref, updated_at=NOW() WHERE user_id=:id');
$stmt?->execute([
'name' => trim((string)$_POST['display_name']),
'fname' => trim((string)$_POST['first_name']),
'lname' => trim((string)$_POST['last_name']),
'fname' => $firstNameEnc,
'lname' => $lastNameEnc,
'zip' => trim((string)$_POST['zip']),
'city' => trim((string)$_POST['city']),
'prof' => trim((string)$_POST['profession']),
'langs' => trim((string)$languages),
'about' => trim((string)$_POST['about']),
'prof' => $professionEnc,
'langs' => $languagesEnc,
'about' => $aboutEnc,
'phone' => $phoneEnc,
'locationPref' => in_array($locationPreference, ['disabled', 'prompt', 'enabled'], true) ? $locationPreference : 'prompt',
'id' => $userId,
@@ -208,12 +223,13 @@ final class AccountPages
}
$info = 'Einstellungen gespeichert.';
} elseif ($action === 'child_add' || $action === 'child_update') {
$crypto = self::requireCrypto($crypto, 'Kinder');
$childId = (int)($_POST['child_id'] ?? 0);
$firstName = trim((string)($_POST['first_name'] ?? ''));
$gender = (string)($_POST['gender'] ?? 'unknown');
$birthdate = trim((string)($_POST['birthdate'] ?? ''));
$ageYearsRaw = trim((string)($_POST['age_years'] ?? ''));
$ageYears = $ageYearsRaw !== '' ? max(0, min(18, (int)$ageYearsRaw)) : null;
$ageYears = $ageYearsRaw !== '' ? max(0, (int)$ageYearsRaw) : null;
$note = trim((string)($_POST['note'] ?? ''));
if ($firstName === '') {
@@ -222,9 +238,16 @@ final class AccountPages
if (!in_array($gender, ['male', 'female', 'diverse', 'unknown'], true)) {
$gender = 'unknown';
}
if ($birthdate !== '') {
$calculatedAge = self::calculateAgeFromBirthdate($birthdate);
if ($calculatedAge === null) {
throw new \RuntimeException('Bitte gib ein gueltiges Geburtsdatum an.');
}
$ageYears = $calculatedAge;
}
$firstNameEnc = $crypto ? $crypto->encrypt($firstName) : $firstName;
$noteEnc = $crypto ? $crypto->encrypt($note) : $note;
$firstNameEnc = $crypto->encrypt($firstName);
$noteEnc = $note !== '' ? $crypto->encrypt($note) : '';
if ($action === 'child_add') {
$stmt = $pdo?->prepare('INSERT INTO children (user_id, gender, birthdate, age_years, encrypted_first_name, note, created_at, updated_at) VALUES (:uid, :gender, :birthdate, :age, :name, :note, NOW(), NOW())');
@@ -409,8 +432,16 @@ final class AccountPages
$row = $stmt?->fetch(\PDO::FETCH_ASSOC);
if ($row) {
$profile = array_merge($profile, array_filter($row, fn($v) => $v !== null));
if ($crypto && !empty($profile['contact_phone'])) {
$profile['contact_phone'] = $crypto->decrypt((string)$profile['contact_phone']) ?: '';
foreach (self::ENCRYPTED_PROFILE_FIELDS as $field) {
$profile[$field] = self::decodeStoredProfileField(
$profile[$field] ?? '',
$crypto,
$pdo,
'user_profiles',
$field,
'user_id',
$userId
);
}
}
$profile = AvatarManager::normalizeProfile($profile, $userId);
@@ -422,9 +453,38 @@ final class AccountPages
$stmt?->execute(['id' => $userId]);
$childrenRaw = $stmt?->fetchAll(\PDO::FETCH_ASSOC) ?: [];
foreach ($childrenRaw as $c) {
if ($crypto) {
$c['first_name'] = $crypto->decrypt((string)$c['first_name']) ?: '';
$c['note'] = $crypto->decrypt((string)($c['note'] ?? '')) ?: '';
$c['first_name'] = self::decodeStoredProfileField(
$c['first_name'] ?? '',
$crypto,
$pdo,
'children',
'encrypted_first_name',
'id',
(int)$c['id']
);
$c['note'] = self::decodeStoredProfileField(
$c['note'] ?? '',
$crypto,
$pdo,
'children',
'note',
'id',
(int)$c['id']
);
if (!empty($c['birthdate'])) {
$calculatedAge = self::calculateAgeFromBirthdate((string)$c['birthdate']);
if ($calculatedAge !== null) {
$storedAge = isset($c['age_years']) && $c['age_years'] !== null ? (int)$c['age_years'] : null;
$c['age_years'] = $calculatedAge;
if ($storedAge !== $calculatedAge) {
$updateChildAgeStmt = $updateChildAgeStmt ?? $pdo?->prepare('UPDATE children SET age_years = :age, updated_at = NOW() WHERE id = :id AND user_id = :uid');
$updateChildAgeStmt?->execute([
'age' => $calculatedAge,
'id' => (int)$c['id'],
'uid' => $userId,
]);
}
}
}
$children[] = $c;
if ($editChildId > 0 && (int)$c['id'] === $editChildId) {
@@ -499,6 +559,65 @@ final class AccountPages
);
}
private static function calculateAgeFromBirthdate(string $birthdate): ?int
{
try {
$birthDateValue = new \DateTimeImmutable($birthdate);
} catch (\Throwable) {
return null;
}
$today = new \DateTimeImmutable('today');
if ($birthDateValue > $today) {
return null;
}
return $birthDateValue->diff($today)->y;
}
private static function requireCrypto(?Crypto $crypto, string $context): Crypto
{
if ($crypto instanceof Crypto) {
return $crypto;
}
throw new \RuntimeException($context . '-Daten koennen derzeit nicht sicher verarbeitet werden. Bitte spaeter erneut versuchen.');
}
private static function encryptOptionalProfileField(Crypto $crypto, string $value): string
{
return $value !== '' ? $crypto->encrypt($value) : '';
}
private static function decodeStoredProfileField(
mixed $value,
?Crypto $crypto,
?\PDO $pdo,
string $table,
string $column,
string $idColumn,
int $id
): string {
$rawValue = (string)$value;
if ($rawValue === '') {
return '';
}
if (Crypto::looksEncrypted($rawValue)) {
return $crypto ? ($crypto->decrypt($rawValue) ?: '') : '';
}
if ($crypto && $pdo && $id > 0) {
$stmt = $pdo->prepare(sprintf('UPDATE %s SET %s = :value, updated_at = NOW() WHERE %s = :id', $table, $column, $idColumn));
$stmt->execute([
'value' => $crypto->encrypt($rawValue),
'id' => $id,
]);
}
return $rawValue;
}
private static function geocodeAddress(?string $street, ?string $zip, ?string $city, ?string $region): array
{
$parts = array_filter([