drink-it-cold/app/privacy.php

84 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
/**
* Datenschutz-Aufräumarbeiten.
*
* Wird beim täglichen Erinnerungslauf (Cron) ausgeführt und hält die
* Speicherdauer kurz, die in der Datenschutzerklärung zugesagt wird:
* Login-Fehlversuche, abgelaufene Remember-Tokens und alte Versandprotokolle.
*/
function privacy_cleanup(): array
{
$result = [
'login_attempts' => 0,
'auth_tokens' => 0,
'reminder_mail_log' => 0,
'reminder_push_log' => 0,
'log_rotated' => false,
];
// Ratenbegrenzung braucht nur das aktuelle Zeitfenster (max. 15 Minuten).
$result['login_attempts'] = privacy_delete(
'DELETE FROM login_attempts WHERE attempted_at < ?',
[utc_after(-86400)]
);
$result['auth_tokens'] = privacy_delete(
'DELETE FROM auth_tokens WHERE expires_at < ?',
[utc_now()]
);
$result['reminder_mail_log'] = privacy_delete(
'DELETE FROM reminder_mail_log WHERE sent_at < ?',
[utc_after(-365 * 86400)]
);
$result['reminder_push_log'] = privacy_delete(
'DELETE FROM reminder_push_log WHERE sent_at < ?',
[utc_after(-365 * 86400)]
);
$result['log_rotated'] = privacy_rotate_error_log();
return $result;
}
function privacy_delete(string $sql, array $params): int
{
$stmt = db()->prepare($sql);
$stmt->execute($params);
return $stmt->rowCount();
}
/**
* Rotiert storage/logs/php.log bei 5 MB oder nach 30 Tagen. Es bleibt
* höchstens eine Sicherung (php.log.1) erhalten.
*/
function privacy_rotate_error_log(): bool
{
$storage = (string) config('app.storage_path', DIC_ROOT . '/storage');
$path = $storage . '/logs/php.log';
if (!is_file($path)) {
return false;
}
$size = @filesize($path);
$mtime = @filemtime($path);
if ($size === false || $mtime === false) {
return false;
}
$maxAge = 30 * 86400;
$maxBytes = max(1, (int) config('legal.log_max_mb', 5)) * 1024 * 1024;
if ($size < $maxBytes && $mtime > time() - $maxAge) {
return false;
}
$rotated = $path . '.1';
if (is_file($rotated) && (int) @filemtime($rotated) < time() - $maxAge) {
@unlink($rotated);
}
if (!@rename($path, $rotated)) {
return false;
}
@touch($path);
@chmod($path, 0640);
return true;
}