Phase1 Bearbeitung

This commit is contained in:
2026-06-15 18:36:57 +02:00
parent e4fa714419
commit 06645f1e9c
29 changed files with 3367 additions and 300 deletions
+408
View File
@@ -0,0 +1,408 @@
<?php
namespace App\Services;
use PDO;
use RuntimeException;
final class ExportService
{
private const CSV_DELIMITER = ';';
private const FLUSH_EVERY_ROWS = 250;
public function __construct(private readonly PDO $pdo)
{
}
public function streamMemberBalancesCsv(int $tenantId, ?string $filename = null): void
{
$this->assertTenantId($tenantId);
$statement = $this->pdo->prepare(
'SELECT
m.id AS member_id,
m.display_name,
m.email,
m.status,
m.created_at,
COALESCE(balance_summary.balance_cents, 0) AS balance_cents,
balance_summary.last_occurred_at
FROM members m
LEFT JOIN (
SELECT
ll.member_id,
SUM(ll.amount_cents) AS balance_cents,
MAX(le.occurred_at) AS last_occurred_at
FROM ledger_lines ll
INNER JOIN ledger_entries le
ON le.id = ll.ledger_entry_id
AND le.tenant_id = ll.tenant_id
WHERE ll.tenant_id = :balance_tenant_id
AND ll.account_code = "member_balance"
AND ll.member_id IS NOT NULL
GROUP BY ll.member_id
) balance_summary
ON balance_summary.member_id = m.id
WHERE m.tenant_id = :tenant_id
ORDER BY m.display_name ASC, m.id ASC'
);
$statement->execute([
'balance_tenant_id' => $tenantId,
'tenant_id' => $tenantId,
]);
$handle = $this->openCsvOutput($filename ?? $this->defaultFilename('member-balances', $tenantId));
try {
$this->writeCsvRow($handle, [
'mitglied_id',
'anzeigename',
'email',
'status',
'saldo_cents',
'saldo_eur',
'letzte_buchung_am',
'angelegt_am',
]);
$rowCount = 0;
while (($row = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
$balanceCents = (int) $row['balance_cents'];
$this->writeCsvRow($handle, [
(int) $row['member_id'],
(string) $row['display_name'],
$row['email'],
(string) $row['status'],
$balanceCents,
$this->formatMoneyFromCents($balanceCents),
$row['last_occurred_at'],
(string) $row['created_at'],
]);
$rowCount++;
$this->flushIfNeeded($rowCount);
}
} finally {
fclose($handle);
}
}
public function streamBookingsCsv(
int $tenantId,
?int $memberId = null,
?string $from = null,
?string $until = null,
?string $filename = null
): void {
$this->assertTenantId($tenantId);
if ($memberId !== null && $memberId <= 0) {
throw new RuntimeException('Die Mitglieds-ID muss groesser als 0 sein.');
}
$from = $this->normalizeDateTimeFilter($from, false);
$until = $this->normalizeDateTimeFilter($until, true);
$sql = 'SELECT
le.id AS booking_id,
le.type,
le.reference_type,
le.reference_id,
le.description,
le.occurred_at,
le.created_at AS entry_created_at,
le.member_id,
m.display_name AS member_name,
s.code AS source_code,
s.name AS source_name,
COALESCE(line_totals.member_balance_cents, 0) AS member_balance_cents,
COALESCE(line_totals.cashbox_cents, 0) AS cashbox_cents,
COALESCE(line_totals.coffee_revenue_cents, 0) AS coffee_revenue_cents,
c.id AS consumption_event_id,
c.quantity,
c.unit_price_cents,
c.total_cents,
c.effective_at,
c.recorded_at,
c.source_reference,
c.notes,
c.paper_sheet_line_id,
p.id AS product_id,
p.name AS product_name
FROM ledger_entries le
LEFT JOIN members m
ON m.id = le.member_id
AND m.tenant_id = le.tenant_id
LEFT JOIN capture_sources s
ON s.id = le.source_id
AND s.tenant_id = le.tenant_id
LEFT JOIN (
SELECT
ledger_entry_id,
SUM(CASE WHEN account_code = "member_balance" THEN amount_cents ELSE 0 END) AS member_balance_cents,
SUM(CASE WHEN account_code = "cashbox" THEN amount_cents ELSE 0 END) AS cashbox_cents,
SUM(CASE WHEN account_code = "coffee_revenue" THEN amount_cents ELSE 0 END) AS coffee_revenue_cents
FROM ledger_lines
WHERE tenant_id = :line_tenant_id
GROUP BY ledger_entry_id
) line_totals
ON line_totals.ledger_entry_id = le.id
LEFT JOIN consumption_events c
ON c.id = le.reference_id
AND c.tenant_id = le.tenant_id
AND le.reference_type = "consumption_event"
LEFT JOIN products p
ON p.id = c.product_id
AND p.tenant_id = c.tenant_id
WHERE le.tenant_id = :tenant_id';
$params = [
'line_tenant_id' => $tenantId,
'tenant_id' => $tenantId,
];
if ($memberId !== null) {
$sql .= ' AND le.member_id = :member_id';
$params['member_id'] = $memberId;
}
if ($from !== null) {
$sql .= ' AND le.occurred_at >= :from_occurred_at';
$params['from_occurred_at'] = $from;
}
if ($until !== null) {
$sql .= ' AND le.occurred_at <= :until_occurred_at';
$params['until_occurred_at'] = $until;
}
$sql .= ' ORDER BY le.occurred_at DESC, le.id DESC';
$statement = $this->pdo->prepare($sql);
$statement->execute($params);
$handle = $this->openCsvOutput($filename ?? $this->defaultFilename('bookings', $tenantId));
try {
$this->writeCsvRow($handle, [
'buchung_id',
'typ',
'buchungsdatum',
'mitglied_id',
'mitglied_name',
'beschreibung',
'quellcode',
'quelle',
'referenz_typ',
'referenz_id',
'saldo_delta_cents',
'saldo_delta_eur',
'kasse_delta_cents',
'kasse_delta_eur',
'umsatz_delta_cents',
'umsatz_delta_eur',
'produkt_id',
'produkt_name',
'menge',
'einzelpreis_cents',
'einzelpreis_eur',
'gesamt_cents',
'gesamt_eur',
'notizen',
'wirksam_am',
'erfasst_am',
'quellreferenz',
'papierlisten_zeile_id',
'angelegt_am',
]);
$rowCount = 0;
while (($row = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
$memberBalanceCents = (int) $row['member_balance_cents'];
$cashboxCents = (int) $row['cashbox_cents'];
$coffeeRevenueCents = (int) $row['coffee_revenue_cents'];
$unitPriceCents = $row['unit_price_cents'] !== null ? (int) $row['unit_price_cents'] : null;
$totalCents = $row['total_cents'] !== null ? (int) $row['total_cents'] : null;
$recordedAt = $row['recorded_at'] ?? $row['entry_created_at'];
$this->writeCsvRow($handle, [
(int) $row['booking_id'],
(string) $row['type'],
(string) $row['occurred_at'],
$row['member_id'] !== null ? (int) $row['member_id'] : '',
$row['member_name'],
(string) $row['description'],
$row['source_code'],
$row['source_name'],
$row['reference_type'],
$row['reference_id'],
$memberBalanceCents,
$this->formatMoneyFromCents($memberBalanceCents),
$cashboxCents,
$this->formatMoneyFromCents($cashboxCents),
$coffeeRevenueCents,
$this->formatMoneyFromCents($coffeeRevenueCents),
$row['product_id'] !== null ? (int) $row['product_id'] : '',
$row['product_name'],
$this->formatQuantity($row['quantity']),
$unitPriceCents ?? '',
$unitPriceCents !== null ? $this->formatMoneyFromCents($unitPriceCents) : '',
$totalCents ?? '',
$totalCents !== null ? $this->formatMoneyFromCents($totalCents) : '',
$row['notes'],
$row['effective_at'] ?? $row['occurred_at'],
$recordedAt,
$row['source_reference'],
$row['paper_sheet_line_id'],
(string) $row['entry_created_at'],
]);
$rowCount++;
$this->flushIfNeeded($rowCount);
}
} finally {
fclose($handle);
}
}
private function assertTenantId(int $tenantId): void
{
if ($tenantId <= 0) {
throw new RuntimeException('Die Tenant-ID muss groesser als 0 sein.');
}
}
private function defaultFilename(string $prefix, int $tenantId): string
{
return sprintf('%s-tenant-%d-%s.csv', $prefix, $tenantId, gmdate('Ymd-His'));
}
private function openCsvOutput(string $filename)
{
if (headers_sent($file, $line)) {
throw new RuntimeException(sprintf('CSV-Header koennen nicht mehr gesendet werden (%s:%d).', $file, $line));
}
$safeFilename = $this->sanitizeFilename($filename);
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $safeFilename . '"; filename*=UTF-8\'\'' . rawurlencode($safeFilename));
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');
header('X-Accel-Buffering: no');
header('X-Content-Type-Options: nosniff');
$handle = fopen('php://output', 'wb');
if ($handle === false) {
throw new RuntimeException('php://output konnte nicht geoeffnet werden.');
}
fwrite($handle, "\xEF\xBB\xBF");
return $handle;
}
private function sanitizeFilename(string $filename): string
{
$sanitized = preg_replace('/[^A-Za-z0-9._-]+/', '-', trim($filename)) ?? '';
$sanitized = trim($sanitized, '.-');
if ($sanitized === '') {
$sanitized = 'export';
}
if (!str_ends_with(strtolower($sanitized), '.csv')) {
$sanitized .= '.csv';
}
return $sanitized;
}
private function writeCsvRow($handle, array $row): void
{
$values = array_map(function (mixed $value): string {
return $this->sanitizeCsvValue($value);
}, $row);
if (fputcsv($handle, $values, self::CSV_DELIMITER) === false) {
throw new RuntimeException('Die CSV-Zeile konnte nicht geschrieben werden.');
}
}
private function sanitizeCsvValue(mixed $value): string
{
if ($value === null) {
return '';
}
if (is_bool($value)) {
return $value ? '1' : '0';
}
$string = str_replace(["\r\n", "\r"], "\n", (string) $value);
// Prevent spreadsheet formula execution for user-controlled text cells.
if (
$string !== ''
&& in_array($string[0], ['=', '+', '-', '@', "\t"], true)
&& !is_numeric($string)
) {
return "'" . $string;
}
return $string;
}
private function formatMoneyFromCents(int $amountCents): string
{
return number_format($amountCents / 100, 2, ',', '.');
}
private function formatQuantity(mixed $quantity): string
{
if ($quantity === null || $quantity === '') {
return '';
}
return rtrim(rtrim(number_format((float) $quantity, 2, '.', ''), '0'), '.');
}
private function normalizeDateTimeFilter(?string $value, bool $endOfDay): ?string
{
$value = trim((string) $value);
if ($value === '') {
return null;
}
$value = str_replace('T', ' ', $value);
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) === 1) {
return $value . ($endOfDay ? ' 23:59:59' : ' 00:00:00');
}
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $value) === 1) {
return $value . ':00';
}
return $value;
}
private function flushIfNeeded(int $rowCount): void
{
if ($rowCount % self::FLUSH_EVERY_ROWS !== 0) {
return;
}
if (ob_get_level() > 0) {
ob_flush();
}
flush();
}
}
+270
View File
@@ -29,6 +29,9 @@ final class LedgerService
throw new RuntimeException('Die Menge muss groesser als 0 sein.');
}
$this->assertTenantMembership($tenantId, $recordedByUserId);
$this->assertTenantMember($tenantId, $memberId);
$this->assertTenantProduct($tenantId, $productId);
$source = $this->findSource($tenantId, $sourceCode);
$priceCents = $this->resolvePriceCents($tenantId, $productId, $effectiveAt);
$totalCents = (int) round($priceCents * $quantity);
@@ -179,6 +182,8 @@ final class LedgerService
throw new RuntimeException('Der Einzahlungsbetrag muss groesser als 0 sein.');
}
$this->assertTenantMembership($tenantId, $recordedByUserId);
$this->assertTenantMember($tenantId, $memberId);
$source = $this->findSource($tenantId, 'admin_backoffice');
$entryInsert = $this->pdo->prepare(
@@ -270,6 +275,12 @@ final class LedgerService
?string $notes,
int $createdByUserId
): int {
$this->assertTenantMembership($tenantId, $createdByUserId);
if (trim($title) === '') {
throw new RuntimeException('Bitte einen Titel fuer die Papierliste angeben.');
}
$statement = $this->pdo->prepare(
'INSERT INTO paper_sheets (
tenant_id,
@@ -320,6 +331,10 @@ final class LedgerService
throw new RuntimeException('Die Menge muss groesser als 0 sein.');
}
$this->assertTenantMembership($tenantId, $actorUserId);
$this->assertTenantMember($tenantId, $memberId);
$this->assertTenantProduct($tenantId, $productId);
$sheet = $this->pdo->prepare(
'SELECT id, status FROM paper_sheets WHERE id = :id AND tenant_id = :tenant_id LIMIT 1'
);
@@ -375,6 +390,8 @@ final class LedgerService
public function postPaperSheet(int $tenantId, int $sheetId, int $actorUserId): void
{
$this->assertTenantMembership($tenantId, $actorUserId);
$sheetStatement = $this->pdo->prepare(
'SELECT id, status FROM paper_sheets WHERE id = :id AND tenant_id = :tenant_id LIMIT 1'
);
@@ -432,6 +449,170 @@ final class LedgerService
$this->audit->log($tenantId, $actorUserId, 'paper_sheet.posted', 'paper_sheet', $sheetId, 'success');
}
public function reverseLedgerEntry(int $tenantId, int $ledgerEntryId, int $actorUserId, ?string $reason = null): int
{
$this->assertTenantMembership($tenantId, $actorUserId);
$entry = $this->ledgerEntry($tenantId, $ledgerEntryId);
if ($this->hasReversal($tenantId, $ledgerEntryId)) {
throw new RuntimeException('Dieser Vorgang wurde bereits storniert.');
}
$linesStatement = $this->pdo->prepare(
'SELECT member_id, account_code, amount_cents
FROM ledger_lines
WHERE tenant_id = :tenant_id AND ledger_entry_id = :ledger_entry_id
ORDER BY id ASC'
);
$linesStatement->execute([
'tenant_id' => $tenantId,
'ledger_entry_id' => $ledgerEntryId,
]);
$lines = $linesStatement->fetchAll();
if ($lines === []) {
throw new RuntimeException('Es wurden keine Ledger-Zeilen fuer den Vorgang gefunden.');
}
$description = 'Storno: ' . $entry['description'];
if ($reason !== null && trim($reason) !== '') {
$description .= ' (' . trim($reason) . ')';
}
$this->pdo->beginTransaction();
try {
$entryInsert = $this->pdo->prepare(
'INSERT INTO ledger_entries (
tenant_id,
member_id,
source_id,
type,
reference_type,
reference_id,
description,
occurred_at,
created_by_user_id,
reversal_of_entry_id
) VALUES (
:tenant_id,
:member_id,
:source_id,
"correction",
"ledger_reversal",
:reference_id,
:description,
:occurred_at,
:created_by_user_id,
:reversal_of_entry_id
)'
);
$entryInsert->execute([
'tenant_id' => $tenantId,
'member_id' => $entry['member_id'],
'source_id' => $entry['source_id'],
'reference_id' => $ledgerEntryId,
'description' => $description,
'occurred_at' => gmdate('Y-m-d H:i:s'),
'created_by_user_id' => $actorUserId,
'reversal_of_entry_id' => $ledgerEntryId,
]);
$reversalId = (int) $this->pdo->lastInsertId();
$lineInsert = $this->pdo->prepare(
'INSERT INTO ledger_lines (
ledger_entry_id,
tenant_id,
member_id,
account_code,
amount_cents
) VALUES (
:ledger_entry_id,
:tenant_id,
:member_id,
:account_code,
:amount_cents
)'
);
foreach ($lines as $line) {
$lineInsert->execute([
'ledger_entry_id' => $reversalId,
'tenant_id' => $tenantId,
'member_id' => $line['member_id'] !== null ? (int) $line['member_id'] : null,
'account_code' => $line['account_code'],
'amount_cents' => 0 - (int) $line['amount_cents'],
]);
}
$this->pdo->commit();
} catch (\Throwable $throwable) {
$this->pdo->rollBack();
throw $throwable;
}
$this->audit->log($tenantId, $actorUserId, 'ledger_entry.reversed', 'ledger_entry', $reversalId, 'success', [
'reversal_of_entry_id' => $ledgerEntryId,
]);
return $reversalId;
}
public function reversePaperSheet(int $tenantId, int $sheetId, int $actorUserId, ?string $reason = null): int
{
$this->assertTenantMembership($tenantId, $actorUserId);
$statement = $this->pdo->prepare(
'SELECT DISTINCT le.id
FROM ledger_entries le
INNER JOIN consumption_events ce
ON ce.id = le.reference_id
AND le.reference_type = "consumption_event"
INNER JOIN paper_sheet_lines psl
ON psl.id = ce.paper_sheet_line_id
WHERE le.tenant_id = :tenant_id
AND psl.paper_sheet_id = :paper_sheet_id
ORDER BY le.id ASC'
);
$statement->execute([
'tenant_id' => $tenantId,
'paper_sheet_id' => $sheetId,
]);
$entryIds = $statement->fetchAll(PDO::FETCH_COLUMN);
if ($entryIds === []) {
throw new RuntimeException('Zu dieser Papierliste wurden keine verbuchten Eintraege gefunden.');
}
$reversed = 0;
foreach ($entryIds as $entryId) {
if ($this->hasReversal($tenantId, (int) $entryId)) {
continue;
}
$this->reverseLedgerEntry(
$tenantId,
(int) $entryId,
$actorUserId,
$reason !== null && trim($reason) !== '' ? $reason : 'Papierliste #' . $sheetId
);
$reversed++;
}
if ($reversed === 0) {
throw new RuntimeException('Diese Papierliste wurde bereits vollstaendig storniert.');
}
$this->audit->log($tenantId, $actorUserId, 'paper_sheet.reversed', 'paper_sheet', $sheetId, 'success', [
'reversed_entries' => $reversed,
]);
return $reversed;
}
private function resolvePriceCents(int $tenantId, int $productId, string $effectiveAt): int
{
$statement = $this->pdo->prepare(
@@ -475,4 +656,93 @@ final class LedgerService
return $source;
}
private function assertTenantMember(int $tenantId, int $memberId): void
{
$statement = $this->pdo->prepare(
'SELECT id FROM members WHERE id = :id AND tenant_id = :tenant_id AND status = "active" LIMIT 1'
);
$statement->execute([
'id' => $memberId,
'tenant_id' => $tenantId,
]);
if (!$statement->fetchColumn()) {
throw new RuntimeException('Das gewaehlte Mitglied gehoert nicht zu diesem Mandanten.');
}
}
private function assertTenantProduct(int $tenantId, int $productId): void
{
$statement = $this->pdo->prepare(
'SELECT id FROM products WHERE id = :id AND tenant_id = :tenant_id AND is_active = 1 LIMIT 1'
);
$statement->execute([
'id' => $productId,
'tenant_id' => $tenantId,
]);
if (!$statement->fetchColumn()) {
throw new RuntimeException('Das gewaehlte Produkt gehoert nicht zu diesem Mandanten.');
}
}
private function assertTenantMembership(int $tenantId, int $userId): void
{
$statement = $this->pdo->prepare(
'SELECT id
FROM tenant_memberships
WHERE tenant_id = :tenant_id
AND user_id = :user_id
AND status = "active"
LIMIT 1'
);
$statement->execute([
'tenant_id' => $tenantId,
'user_id' => $userId,
]);
if (!$statement->fetchColumn()) {
throw new RuntimeException('Der Benutzer ist fuer diesen Mandanten nicht aktiv.');
}
}
private function ledgerEntry(int $tenantId, int $ledgerEntryId): array
{
$statement = $this->pdo->prepare(
'SELECT *
FROM ledger_entries
WHERE id = :id
AND tenant_id = :tenant_id
LIMIT 1'
);
$statement->execute([
'id' => $ledgerEntryId,
'tenant_id' => $tenantId,
]);
$entry = $statement->fetch();
if (!$entry) {
throw new RuntimeException('Der angeforderte Vorgang wurde nicht gefunden.');
}
return $entry;
}
private function hasReversal(int $tenantId, int $ledgerEntryId): bool
{
$statement = $this->pdo->prepare(
'SELECT id
FROM ledger_entries
WHERE tenant_id = :tenant_id
AND reversal_of_entry_id = :reversal_of_entry_id
LIMIT 1'
);
$statement->execute([
'tenant_id' => $tenantId,
'reversal_of_entry_id' => $ledgerEntryId,
]);
return (bool) $statement->fetchColumn();
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace App\Services;
use RuntimeException;
final class MailerService
{
private readonly string $rootPath;
public function __construct(private readonly array $config = [])
{
$this->rootPath = dirname(__DIR__, 2);
}
public function send(string $to, string $subject, string $textBody, array $headers = []): bool
{
$to = trim($to);
$subject = trim($subject);
$textBody = trim($textBody);
if ($to === '' || !filter_var($to, FILTER_VALIDATE_EMAIL)) {
throw new RuntimeException('Die Zieladresse fuer den Mailversand ist ungueltig.');
}
if ($subject === '' || $textBody === '') {
throw new RuntimeException('Betreff und Inhalt fuer den Mailversand sind erforderlich.');
}
$subjectLine = $this->encodeSubject($this->sanitizeHeaderValue($this->subjectPrefix() . $subject));
$headerLines = $this->buildHeaders($headers);
$body = $this->normalizeBody($textBody);
$sent = false;
if (function_exists('mail')) {
$sent = @mail($to, $subjectLine, $body, implode("\r\n", $headerLines));
}
if ($sent) {
return true;
}
if (!$this->usesLogFallback()) {
throw new RuntimeException('Die E-Mail konnte nicht versendet werden.');
}
$this->writeFallbackLog([
'to' => $to,
'subject' => $this->subjectPrefix() . $subject,
'headers' => $headerLines,
'body' => $textBody,
]);
return false;
}
private function buildHeaders(array $headers): array
{
$headerLines = [
'MIME-Version: 1.0',
'Content-Type: text/plain; charset=UTF-8',
'Content-Transfer-Encoding: 8bit',
'X-Mailer: Kaffeeliste-Neustart',
];
$from = trim((string) ($this->config['from'] ?? ''));
if ($from !== '') {
$headerLines[] = 'From: ' . $this->sanitizeHeaderValue($from);
}
$replyTo = trim((string) ($this->config['reply_to'] ?? ''));
if ($replyTo !== '') {
$headerLines[] = 'Reply-To: ' . $this->sanitizeHeaderValue($replyTo);
}
foreach ($headers as $name => $value) {
if (is_int($name)) {
$line = trim((string) $value);
if ($line !== '' && str_contains($line, ':')) {
[$rawName, $rawValue] = array_pad(explode(':', $line, 2), 2, '');
$this->appendHeader($headerLines, $rawName, $rawValue);
}
continue;
}
$this->appendHeader($headerLines, (string) $name, (string) $value);
}
return $headerLines;
}
private function appendHeader(array &$headerLines, string $name, string $value): void
{
$name = trim($name);
if ($name === '' || preg_match('/[^A-Za-z0-9-]/', $name)) {
return;
}
$headerLines[] = $name . ': ' . $this->sanitizeHeaderValue($value);
}
private function writeFallbackLog(array $payload): void
{
$path = $this->resolveLogPath();
$directory = dirname($path);
if (!is_dir($directory) && !mkdir($directory, 0775, true) && !is_dir($directory)) {
throw new RuntimeException('Das Mail-Log-Verzeichnis konnte nicht erstellt werden.');
}
$encodedPayload = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if (!is_string($encodedPayload)) {
$encodedPayload = '{"error":"mail payload could not be encoded"}';
}
$entry = '[' . gmdate(DATE_ATOM) . "] mail_fallback\n" . $encodedPayload . "\n\n";
if (file_put_contents($path, $entry, FILE_APPEND | LOCK_EX) === false) {
throw new RuntimeException('Das Mail-Fallback-Log konnte nicht geschrieben werden.');
}
}
private function resolveLogPath(): string
{
$configuredPath = trim((string) ($this->config['log_file'] ?? ''));
if ($configuredPath === '') {
return $this->rootPath . '/storage/logs/mail.log';
}
if (str_starts_with($configuredPath, '/')) {
return $configuredPath;
}
return $this->rootPath . '/' . ltrim($configuredPath, '/');
}
private function usesLogFallback(): bool
{
return (bool) ($this->config['log_fallback'] ?? true);
}
private function subjectPrefix(): string
{
return (string) ($this->config['subject_prefix'] ?? '');
}
private function sanitizeHeaderValue(string $value): string
{
return trim(str_replace(["\r", "\n"], ' ', $value));
}
private function encodeSubject(string $subject): string
{
if ($subject === '') {
return $subject;
}
if (function_exists('mb_encode_mimeheader')) {
return mb_encode_mimeheader($subject, 'UTF-8', 'B', "\r\n");
}
return $subject;
}
private function normalizeBody(string $textBody): string
{
return str_replace("\n", "\r\n", str_replace(["\r\n", "\r"], "\n", $textBody));
}
}
+445
View File
@@ -0,0 +1,445 @@
<?php
namespace App\Services;
use DateTimeImmutable;
use DateTimeZone;
use PDO;
use RuntimeException;
final class PasswordResetService
{
private readonly DateTimeZone $utc;
public function __construct(
private readonly PDO $pdo,
private readonly MailerService $mailer,
private readonly array $config = []
) {
$this->utc = new DateTimeZone('UTC');
}
public function requestReset(string $email, array $context = []): void
{
$email = $this->normalizeEmail($email);
if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
}
$user = $this->findUserByEmail($email);
if (!$user) {
return;
}
$token = $this->createTokenForUser((int) $user['id'], $context);
$this->sendResetMail($token);
}
public function createTokenForUser(int $userId, array $context = []): array
{
if ($userId <= 0) {
throw new RuntimeException('Der Benutzer fuer den Passwort-Reset ist ungueltig.');
}
$user = $this->findUserById($userId);
if (!$user) {
throw new RuntimeException('Der Benutzer fuer den Passwort-Reset wurde nicht gefunden.');
}
$nowTs = time();
$now = $this->formatTimestamp($nowTs);
$expiresAt = $this->formatTimestamp($nowTs + ($this->ttlMinutes() * 60));
$token = bin2hex(random_bytes(32));
$tokenHash = hash('sha256', $token);
$ip = trim((string) ($_SERVER['REMOTE_ADDR'] ?? ''));
$userAgent = trim((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''));
$this->pdo->beginTransaction();
try {
$cleanupStatement = $this->pdo->prepare(
'DELETE FROM password_reset_tokens
WHERE user_id = :user_id
AND (consumed_at IS NOT NULL OR expires_at < :now)'
);
$cleanupStatement->execute([
'user_id' => $userId,
'now' => $now,
]);
$insertStatement = $this->pdo->prepare(
'INSERT INTO password_reset_tokens (
user_id,
email,
token_hash,
requested_ip_hash,
requested_user_agent_hash,
expires_at,
consumed_at,
created_at
) VALUES (
:user_id,
:email,
:token_hash,
:requested_ip_hash,
:requested_user_agent_hash,
:expires_at,
NULL,
:created_at
)'
);
$insertStatement->execute([
'user_id' => $userId,
'email' => $user['email'],
'token_hash' => $tokenHash,
'requested_ip_hash' => $ip !== '' ? hash('sha256', $ip) : null,
'requested_user_agent_hash' => $userAgent !== '' ? hash('sha256', $userAgent) : null,
'expires_at' => $expiresAt,
'created_at' => $now,
]);
$trimStatement = $this->pdo->prepare(
'SELECT id
FROM password_reset_tokens
WHERE user_id = :user_id
AND consumed_at IS NULL
AND expires_at >= :now
ORDER BY created_at DESC, id DESC'
);
$trimStatement->execute([
'user_id' => $userId,
'now' => $now,
]);
$tokenIds = array_map(
static fn (array $row): int => (int) $row['id'],
$trimStatement->fetchAll()
);
$idsToDelete = array_slice($tokenIds, $this->maxActiveTokens());
if ($idsToDelete !== []) {
$deleteStatement = $this->pdo->prepare(
'DELETE FROM password_reset_tokens
WHERE user_id = :user_id
AND id = :id'
);
foreach ($idsToDelete as $tokenId) {
$deleteStatement->execute([
'user_id' => $userId,
'id' => $tokenId,
]);
}
}
$this->pdo->commit();
} catch (\Throwable $throwable) {
if ($this->pdo->inTransaction()) {
$this->pdo->rollBack();
}
throw $throwable;
}
return [
'user_id' => $userId,
'email' => $user['email'],
'full_name' => $user['full_name'],
'token' => $token,
'expires_at' => $expiresAt,
'reset_url' => $this->buildResetUrl($token, $context),
];
}
public function findValidToken(string $token): ?array
{
$token = trim($token);
if ($token === '') {
return null;
}
$statement = $this->pdo->prepare(
'SELECT prt.id,
prt.user_id,
prt.email,
prt.expires_at,
prt.consumed_at,
prt.created_at,
u.full_name
FROM password_reset_tokens prt
INNER JOIN users u ON u.id = prt.user_id
WHERE prt.token_hash = :token_hash
LIMIT 1'
);
$statement->execute([
'token_hash' => hash('sha256', $token),
]);
$resetToken = $statement->fetch();
if (!$resetToken) {
return null;
}
$expiresAtTs = $this->timestampFromDatabase($resetToken['expires_at']);
if (
$resetToken['consumed_at'] !== null
|| $expiresAtTs === null
|| $expiresAtTs < time()
) {
return null;
}
return $resetToken;
}
public function resetPassword(string $token, string $newPassword): int
{
$token = trim($token);
$newPassword = (string) $newPassword;
if ($token === '') {
throw new RuntimeException('Der Reset-Link ist ungueltig oder abgelaufen.');
}
if (mb_strlen($newPassword) < 12) {
throw new RuntimeException('Das neue Passwort muss mindestens 12 Zeichen lang sein.');
}
$tokenHash = hash('sha256', $token);
$nowTs = time();
$now = $this->formatTimestamp($nowTs);
$this->pdo->beginTransaction();
try {
$statement = $this->pdo->prepare(
'SELECT id, user_id, expires_at, consumed_at
FROM password_reset_tokens
WHERE token_hash = :token_hash
LIMIT 1
FOR UPDATE'
);
$statement->execute(['token_hash' => $tokenHash]);
$resetToken = $statement->fetch();
if (!$resetToken) {
throw new RuntimeException('Der Reset-Link ist ungueltig oder abgelaufen.');
}
$expiresAtTs = $this->timestampFromDatabase($resetToken['expires_at']);
if (
$resetToken['consumed_at'] !== null
|| $expiresAtTs === null
|| $expiresAtTs < $nowTs
) {
throw new RuntimeException('Der Reset-Link ist ungueltig oder abgelaufen.');
}
$updateUserStatement = $this->pdo->prepare(
'UPDATE users
SET password_hash = :password_hash
WHERE id = :user_id'
);
$updateUserStatement->execute([
'password_hash' => secure_password_hash($newPassword),
'user_id' => $resetToken['user_id'],
]);
$consumeTokenStatement = $this->pdo->prepare(
'UPDATE password_reset_tokens
SET consumed_at = :consumed_at
WHERE id = :id'
);
$consumeTokenStatement->execute([
'consumed_at' => $now,
'id' => $resetToken['id'],
]);
$revokeOtherTokensStatement = $this->pdo->prepare(
'DELETE FROM password_reset_tokens
WHERE user_id = :user_id
AND id <> :id'
);
$revokeOtherTokensStatement->execute([
'user_id' => $resetToken['user_id'],
'id' => $resetToken['id'],
]);
$this->pdo->commit();
} catch (\Throwable $throwable) {
if ($this->pdo->inTransaction()) {
$this->pdo->rollBack();
}
throw $throwable;
}
return (int) $resetToken['user_id'];
}
public function revokeTokensForUser(int $userId): int
{
if ($userId <= 0) {
return 0;
}
$statement = $this->pdo->prepare(
'DELETE FROM password_reset_tokens
WHERE user_id = :user_id'
);
$statement->execute(['user_id' => $userId]);
return $statement->rowCount();
}
public function purgeExpiredTokens(): int
{
$statement = $this->pdo->prepare(
'DELETE FROM password_reset_tokens
WHERE consumed_at IS NOT NULL
OR expires_at < :now'
);
$statement->execute(['now' => $this->formatTimestamp(time())]);
return $statement->rowCount();
}
private function sendResetMail(array $token): void
{
$name = trim((string) ($token['full_name'] ?? ''));
$greeting = $name !== '' ? 'Hallo ' . $name . ',' : 'Hallo,';
$body = implode("\n\n", [
$greeting,
'fuer dein Konto wurde ein Passwort-Reset angefragt.',
'Bitte nutze diesen Link, um ein neues Passwort zu setzen:',
(string) $token['reset_url'],
'Der Link ist gueltig bis ' . (string) $token['expires_at'] . ' UTC.',
'Falls du den Reset nicht angefragt hast, kannst du diese Nachricht ignorieren.',
]);
$this->mailer->send(
(string) $token['email'],
$this->mailSubject(),
$body
);
}
private function findUserByEmail(string $email): ?array
{
$statement = $this->pdo->prepare(
'SELECT id, full_name, email
FROM users
WHERE email = :email
LIMIT 1'
);
$statement->execute(['email' => $email]);
$user = $statement->fetch();
return $user ?: null;
}
private function findUserById(int $userId): ?array
{
$statement = $this->pdo->prepare(
'SELECT id, full_name, email
FROM users
WHERE id = :id
LIMIT 1'
);
$statement->execute(['id' => $userId]);
$user = $statement->fetch();
return $user ?: null;
}
private function normalizeEmail(string $email): string
{
return mb_strtolower(trim($email));
}
private function buildResetUrl(string $token, array $context = []): string
{
$template = trim((string) ($this->config['reset_url'] ?? ''));
if ($template === '') {
$template = 'http://localhost:8080/reset-password?token={{token}}';
}
$query = [];
foreach ($context as $key => $value) {
if (!is_string($key) || trim($key) === '' || !is_scalar($value)) {
continue;
}
$normalizedValue = trim((string) $value);
if ($normalizedValue === '') {
continue;
}
$query[$key] = $normalizedValue;
}
if (str_contains($template, '{{token}}')) {
$url = str_replace('{{token}}', rawurlencode($token), $template);
if ($query === []) {
return $url;
}
$separator = str_contains($url, '?') ? '&' : '?';
return $url . $separator . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
$query['token'] = $token;
$separator = str_contains($template, '?') ? '&' : '?';
return $template . $separator . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
private function mailSubject(): string
{
$subject = trim((string) ($this->config['subject'] ?? ''));
return $subject !== '' ? $subject : 'Passwort zuruecksetzen';
}
private function ttlMinutes(): int
{
return max(5, (int) ($this->config['ttl_minutes'] ?? 60));
}
private function maxActiveTokens(): int
{
return max(1, (int) ($this->config['max_active_tokens'] ?? 3));
}
private function formatTimestamp(int $timestamp): string
{
return gmdate('Y-m-d H:i:s', $timestamp);
}
private function timestampFromDatabase(?string $value): ?int
{
if ($value === null || trim($value) === '') {
return null;
}
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $value, $this->utc);
if (!$date instanceof DateTimeImmutable) {
return null;
}
return $date->getTimestamp();
}
}
+272
View File
@@ -0,0 +1,272 @@
<?php
namespace App\Services;
use DateTimeImmutable;
use DateTimeZone;
use PDO;
use RuntimeException;
final class RateLimiterService
{
private readonly DateTimeZone $utc;
public function __construct(
private readonly PDO $pdo,
private readonly array $config = []
) {
$this->utc = new DateTimeZone('UTC');
}
public function inspect(string $action, string $scope): array
{
$policy = $this->resolvePolicy($action);
$statement = $this->pdo->prepare(
'SELECT attempts, window_start, last_attempt_at, blocked_until
FROM rate_limits
WHERE action_key = :action_key
AND bucket_key = :bucket_key
LIMIT 1'
);
$statement->execute([
'action_key' => $policy['action'],
'bucket_key' => $this->bucketKey($scope),
]);
return $this->buildState($policy, $statement->fetch() ?: null, time());
}
public function hit(string $action, string $scope): array
{
$policy = $this->resolvePolicy($action);
$bucketKey = $this->bucketKey($scope);
$nowTs = time();
$now = $this->formatTimestamp($nowTs);
$this->pdo->beginTransaction();
try {
$bootstrapStatement = $this->pdo->prepare(
'INSERT INTO rate_limits (
action_key,
bucket_key,
attempts,
window_start,
last_attempt_at,
blocked_until,
created_at
) VALUES (
:action_key,
:bucket_key,
0,
:window_start,
:last_attempt_at,
NULL,
:created_at
)
ON DUPLICATE KEY UPDATE
action_key = action_key'
);
$bootstrapStatement->execute([
'action_key' => $policy['action'],
'bucket_key' => $bucketKey,
'window_start' => $now,
'last_attempt_at' => $now,
'created_at' => $now,
]);
$selectStatement = $this->pdo->prepare(
'SELECT attempts, window_start, last_attempt_at, blocked_until
FROM rate_limits
WHERE action_key = :action_key
AND bucket_key = :bucket_key
LIMIT 1
FOR UPDATE'
);
$selectStatement->execute([
'action_key' => $policy['action'],
'bucket_key' => $bucketKey,
]);
$row = $selectStatement->fetch();
if (!$row) {
throw new RuntimeException('Der Rate-Limit-Zustand konnte nicht geladen werden.');
}
$windowStartTs = $this->timestampFromDatabase($row['window_start']);
$blockedUntilTs = $this->timestampFromDatabase($row['blocked_until']);
$isBlocked = $blockedUntilTs !== null && $blockedUntilTs > $nowTs;
$resetWindow = $windowStartTs === null
|| ($windowStartTs + $policy['window_seconds']) <= $nowTs
|| ($blockedUntilTs !== null && $blockedUntilTs <= $nowTs);
if ($isBlocked) {
$updatedRow = $row;
} else {
$attempts = $resetWindow ? 1 : ((int) $row['attempts'] + 1);
$windowStart = $resetWindow ? $now : (string) $row['window_start'];
$blockedUntil = $attempts >= $policy['max_attempts']
? $this->formatTimestamp($nowTs + $policy['block_seconds'])
: null;
$updateStatement = $this->pdo->prepare(
'UPDATE rate_limits
SET attempts = :attempts,
window_start = :window_start,
last_attempt_at = :last_attempt_at,
blocked_until = :blocked_until
WHERE action_key = :action_key
AND bucket_key = :bucket_key'
);
$updateStatement->execute([
'attempts' => $attempts,
'window_start' => $windowStart,
'last_attempt_at' => $now,
'blocked_until' => $blockedUntil,
'action_key' => $policy['action'],
'bucket_key' => $bucketKey,
]);
$updatedRow = [
'attempts' => $attempts,
'window_start' => $windowStart,
'last_attempt_at' => $now,
'blocked_until' => $blockedUntil,
];
}
$this->pdo->commit();
} catch (\Throwable $throwable) {
if ($this->pdo->inTransaction()) {
$this->pdo->rollBack();
}
throw $throwable;
}
return $this->buildState($policy, $updatedRow, $nowTs);
}
public function clear(string $action, string $scope): void
{
$policy = $this->resolvePolicy($action);
$statement = $this->pdo->prepare(
'DELETE FROM rate_limits
WHERE action_key = :action_key
AND bucket_key = :bucket_key'
);
$statement->execute([
'action_key' => $policy['action'],
'bucket_key' => $this->bucketKey($scope),
]);
}
public function tooManyAttempts(string $action, string $scope): bool
{
return !$this->inspect($action, $scope)['allowed'];
}
public function prune(int $staleAfterSeconds = 604800): int
{
$cutoff = $this->formatTimestamp(time() - max(3600, $staleAfterSeconds));
$statement = $this->pdo->prepare(
'DELETE FROM rate_limits
WHERE (blocked_until IS NULL AND last_attempt_at < :cutoff)
OR (blocked_until IS NOT NULL AND blocked_until < :cutoff)'
);
$statement->execute(['cutoff' => $cutoff]);
return $statement->rowCount();
}
private function buildState(array $policy, ?array $row, int $nowTs): array
{
$attempts = 0;
$blockedUntil = null;
$retryAfter = 0;
$allowed = true;
$resetAtTs = $nowTs + $policy['window_seconds'];
if ($row) {
$windowStartTs = $this->timestampFromDatabase($row['window_start']);
$blockedUntilTs = $this->timestampFromDatabase($row['blocked_until']);
if ($blockedUntilTs !== null && $blockedUntilTs > $nowTs) {
$attempts = (int) $row['attempts'];
$blockedUntil = (string) $row['blocked_until'];
$retryAfter = max(1, $blockedUntilTs - $nowTs);
$allowed = false;
$resetAtTs = $blockedUntilTs;
} elseif ($windowStartTs !== null && ($windowStartTs + $policy['window_seconds']) > $nowTs) {
$attempts = (int) $row['attempts'];
$resetAtTs = $windowStartTs + $policy['window_seconds'];
}
}
return [
'action' => $policy['action'],
'allowed' => $allowed,
'attempts' => $attempts,
'max_attempts' => $policy['max_attempts'],
'remaining_attempts' => max(0, $policy['max_attempts'] - $attempts),
'window_seconds' => $policy['window_seconds'],
'block_seconds' => $policy['block_seconds'],
'retry_after_seconds' => $retryAfter,
'reset_at' => $this->formatTimestamp($resetAtTs),
'blocked_until' => $blockedUntil,
];
}
private function resolvePolicy(string $action): array
{
$normalizedAction = mb_strtolower(trim($action));
if ($normalizedAction === '') {
throw new RuntimeException('Die Rate-Limit-Aktion ist erforderlich.');
}
$policy = $this->config[$normalizedAction] ?? null;
if (!is_array($policy)) {
throw new RuntimeException(sprintf('Fuer die Aktion "%s" ist kein Rate-Limit konfiguriert.', $normalizedAction));
}
return [
'action' => $normalizedAction,
'max_attempts' => max(1, (int) ($policy['max_attempts'] ?? 5)),
'window_seconds' => max(60, (int) ($policy['window_seconds'] ?? 900)),
'block_seconds' => max(60, (int) ($policy['block_seconds'] ?? 900)),
];
}
private function bucketKey(string $scope): string
{
$scope = trim($scope);
if ($scope === '') {
throw new RuntimeException('Der Rate-Limit-Scope ist erforderlich.');
}
return hash('sha256', $scope);
}
private function formatTimestamp(int $timestamp): string
{
return gmdate('Y-m-d H:i:s', $timestamp);
}
private function timestampFromDatabase(?string $value): ?int
{
if ($value === null || trim($value) === '') {
return null;
}
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $value, $this->utc);
if (!$date instanceof DateTimeImmutable) {
return null;
}
return $date->getTimestamp();
}
}
+17 -1
View File
@@ -22,7 +22,7 @@ final class RfidService
$token = bin2hex(random_bytes(20));
$statement = $this->pdo->prepare(
'INSERT INTO rfid_devices (tenant_id, name, location_label, device_token, status)
VALUES (:tenant_id, :name, :location_label, :device_token, "planned")'
VALUES (:tenant_id, :name, :location_label, :device_token, "active")'
);
$statement->execute([
'tenant_id' => $tenantId,
@@ -43,6 +43,18 @@ final class RfidService
throw new RuntimeException('Die Karten-UID darf nicht leer sein.');
}
$memberStatement = $this->pdo->prepare(
'SELECT id FROM members WHERE tenant_id = :tenant_id AND id = :id AND status = "active" LIMIT 1'
);
$memberStatement->execute([
'tenant_id' => $tenantId,
'id' => $memberId,
]);
if (!$memberStatement->fetchColumn()) {
throw new RuntimeException('Das ausgewaehlte Mitglied ist fuer RFID nicht verfuegbar.');
}
$statement = $this->pdo->prepare(
'INSERT INTO rfid_tags (tenant_id, member_id, uid_hash, label, status)
VALUES (:tenant_id, :member_id, :uid_hash, :label, "active")'
@@ -80,6 +92,10 @@ final class RfidService
throw new RuntimeException('Das RFID-Geraet ist unbekannt.');
}
if (($device['status'] ?? '') !== 'active') {
throw new RuntimeException('Das RFID-Geraet ist nicht aktiv.');
}
$status = 'received';
$tagStatement = $this->pdo->prepare(
'SELECT id FROM rfid_tags WHERE tenant_id = :tenant_id AND uid_hash = :uid_hash LIMIT 1'
+97
View File
@@ -0,0 +1,97 @@
<?php
namespace App\Services;
final class SecurityHeadersService
{
public function buildHeaders(?bool $isHttps = null): array
{
$isHttps = $isHttps ?? $this->isHttpsRequest();
$headers = [
'Content-Security-Policy' => $this->buildContentSecurityPolicy($isHttps),
'Referrer-Policy' => 'strict-origin-when-cross-origin',
'Permissions-Policy' => 'accelerometer=(), autoplay=(), camera=(), display-capture=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()',
'Cross-Origin-Opener-Policy' => 'same-origin',
'Cross-Origin-Resource-Policy' => 'same-origin',
'Origin-Agent-Cluster' => '?1',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'SAMEORIGIN',
'X-Permitted-Cross-Domain-Policies' => 'none',
];
if ($isHttps) {
$headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains';
}
return $headers;
}
public function send(array $overrides = [], ?bool $isHttps = null): void
{
if (headers_sent()) {
return;
}
$headers = $this->buildHeaders($isHttps);
foreach ($overrides as $name => $value) {
if (!is_string($name) || trim($name) === '') {
continue;
}
if ($value === null) {
unset($headers[$name]);
continue;
}
$headers[$name] = (string) $value;
}
header_remove('X-Powered-By');
foreach ($headers as $name => $value) {
header($name . ': ' . $value, true);
}
}
private function buildContentSecurityPolicy(bool $isHttps): string
{
$directives = [
"default-src 'self'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'self'",
"object-src 'none'",
"connect-src 'self'",
"font-src 'self' data:",
"img-src 'self' data:",
"manifest-src 'self'",
"script-src 'self'",
"style-src 'self'",
];
if ($isHttps) {
$directives[] = 'upgrade-insecure-requests';
}
return implode('; ', $directives);
}
private function isHttpsRequest(): bool
{
$https = $_SERVER['HTTPS'] ?? null;
if (is_string($https) && $https !== '' && strtolower($https) !== 'off') {
return true;
}
$forwardedProto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
if (is_string($forwardedProto) && strtolower($forwardedProto) === 'https') {
return true;
}
return (string) ($_SERVER['SERVER_PORT'] ?? '') === '443';
}
}
+62 -2
View File
@@ -43,6 +43,8 @@ final class SetupService
throw new RuntimeException('Das Admin-Passwort muss mindestens 12 Zeichen lang sein.');
}
$this->resolveMailFrom($input, $this->normalizeAppUrl((string) $input['app_url']));
$config = [
'host' => trim((string) $input['db_host']),
'port' => (int) $input['db_port'],
@@ -106,6 +108,8 @@ final class SetupService
private function buildEnvFile(array $input): string
{
$appUrl = $this->normalizeAppUrl((string) $input['app_url']);
$mailFrom = $this->resolveMailFrom($input, $appUrl);
$appKey = bin2hex(random_bytes(32));
$rfidSecret = bin2hex(random_bytes(24));
@@ -113,9 +117,11 @@ final class SetupService
'APP_NAME="Kaffeekasse SaaS"',
'APP_ENV=production',
'APP_DEBUG=0',
'APP_URL=' . trim((string) $input['app_url']),
'APP_URL=' . $appUrl,
'APP_TIMEZONE=Europe/Berlin',
'APP_KEY=' . $appKey,
'ALLOW_PUBLIC_REGISTRATION=1',
'RFID_INGEST_ENABLED=0',
'',
'DB_HOST=' . trim((string) $input['db_host']),
'DB_PORT=' . (int) $input['db_port'],
@@ -123,10 +129,64 @@ final class SetupService
'DB_USER=' . trim((string) $input['db_user']),
'DB_PASS=' . (string) ($input['db_pass'] ?? ''),
'',
'MAIL_FROM=' . trim((string) ($input['mail_from'] ?? 'noreply@example.com')),
'MAIL_FROM=' . $mailFrom,
'MAIL_REPLY_TO=' . $mailFrom,
'MAIL_SUBJECT_PREFIX="[Kaffeekasse] "',
'MAIL_LOG_FALLBACK=1',
'MAIL_LOG_FILE=storage/logs/mail.log',
'',
'PASSWORD_RESET_URL=' . $appUrl . '/reset-password?token={{token}}',
'PASSWORD_RESET_TTL_MINUTES=60',
'PASSWORD_RESET_MAX_ACTIVE_TOKENS=3',
'PASSWORD_RESET_SUBJECT="Passwort zuruecksetzen"',
'',
'RATE_LIMIT_LOGIN_MAX_ATTEMPTS=5',
'RATE_LIMIT_LOGIN_WINDOW_SECONDS=900',
'RATE_LIMIT_LOGIN_BLOCK_SECONDS=900',
'RATE_LIMIT_REGISTRATION_MAX_ATTEMPTS=3',
'RATE_LIMIT_REGISTRATION_WINDOW_SECONDS=3600',
'RATE_LIMIT_REGISTRATION_BLOCK_SECONDS=3600',
'',
'RFID_SHARED_SECRET=' . $rfidSecret,
];
return implode(PHP_EOL, $lines) . PHP_EOL;
}
private function normalizeAppUrl(string $appUrl): string
{
return rtrim(trim($appUrl), '/');
}
private function resolveMailFrom(array $input, string $appUrl): string
{
$mailFrom = mb_strtolower(trim((string) ($input['mail_from'] ?? '')));
if ($mailFrom === '' || $mailFrom === 'noreply@example.com') {
return $this->defaultMailFrom($appUrl);
}
if (!filter_var($mailFrom, FILTER_VALIDATE_EMAIL)) {
throw new RuntimeException('Die Mail-Absenderadresse ist ungueltig.');
}
return $mailFrom;
}
private function defaultMailFrom(string $appUrl): string
{
$host = parse_url($appUrl, PHP_URL_HOST);
if (!is_string($host)) {
return 'noreply@example.com';
}
$host = preg_replace('/^www\./i', '', mb_strtolower(trim($host))) ?? '';
if ($host === '' || !str_contains($host, '.')) {
return 'noreply@example.com';
}
return 'noreply@' . $host;
}
}