cleanup and refactor

This commit is contained in:
th committed 2026-04-09 19:39:56 +02:00
1 parent 5f9db690a4
commit 73da8fb849
10 files changed
+91 -97

No files matched your search

+9 -25
View File
@@ -196,7 +196,6 @@ if (str_starts_with($route, '/api/auth')) {
foreach ($reEncrypted as $ck => $val) $db->prepare("UPDATE settings SET value = :v WHERE key = :k")->execute(['v' => $val, 'k' => $ck]);
$db->prepare("UPDATE settings SET value = :h WHERE key = 'password_hash'")->execute(['h' => $newHash]);
$db->commit();
$db->exec("UPDATE cards SET caldav_etag = NULL, caldav_synced_at = NULL WHERE caldav_uid LIKE 'note:%'");
echo json_encode(['ok' => true, 'rekey' => true]);
}
exit;
@@ -773,10 +772,6 @@ if (str_starts_with($route, '/api/')) {
if ($route === '/api/caldav/sync' && $method === 'POST') {
echo json_encode($caldav->sync()); exit;
}
if ($route === '/api/caldav/log' && $method === 'GET') {
echo json_encode($db->query('SELECT * FROM caldav_sync_log ORDER BY id DESC LIMIT 50')->fetchAll()); exit;
}
// ── Relay ──
if ($route === '/api/relay/settings' && $method === 'GET') {
@@ -814,6 +809,13 @@ if (str_starts_with($route, '/api/')) {
$pulled = 0;
$encKey = substr(hash('sha256', $relayKey), 0, 32);
$decryptField = function($text) use ($encKey) {
if (!str_starts_with($text, 'HYPRCRD-RELAY-V1')) return $text;
$raw = base64_decode(trim(explode("\n", $text, 2)[1] ?? ''));
if (strlen($raw) < 17) return $text;
$plain = openssl_decrypt(substr($raw, 16), 'aes-256-cbc', $encKey, OPENSSL_RAW_DATA, substr($raw, 0, 16));
return $plain !== false ? $plain : $text;
};
// Load IDs we pushed ourselves
$ownIds = array_column($db->query('SELECT relay_msg_id FROM relay_pushed')->fetchAll(), 'relay_msg_id');
@@ -821,30 +823,12 @@ if (str_starts_with($route, '/api/')) {
foreach ($messages as $msg) {
if ($msg['fetched'] ?? false) continue;
if (in_array($msg['id'] ?? 0, $ownIds)) {
// Clean up tracking row since relay will delete it after 24h anyway
$db->prepare('DELETE FROM relay_pushed WHERE relay_msg_id = ?')->execute([$msg['id']]);
continue;
}
// Decrypt
$title = $msg['title'] ?? '';
$body = $msg['body'] ?? '';
if (str_starts_with($body, 'HYPRCRD-RELAY-V1')) {
$enc = trim(explode("\n", $body, 2)[1] ?? '');
$raw = base64_decode($enc);
if (strlen($raw) >= 17) {
$plain = openssl_decrypt(substr($raw, 16), 'aes-256-cbc', $encKey, OPENSSL_RAW_DATA, substr($raw, 0, 16));
if ($plain !== false) $body = $plain;
}
}
if (str_starts_with($title, 'HYPRCRD-RELAY-V1')) {
$enc = trim(explode("\n", $title, 2)[1] ?? '');
$raw = base64_decode($enc);
if (strlen($raw) >= 17) {
$plain = openssl_decrypt(substr($raw, 16), 'aes-256-cbc', $encKey, OPENSSL_RAW_DATA, substr($raw, 0, 16));
if ($plain !== false) $title = $plain;
}
}
$title = $decryptField($msg['title'] ?? '');
$body = $decryptField($msg['body'] ?? '');
if (!$title) $title = substr(strip_tags($body), 0, 60) ?: 'Relay message';
$body = trim($body);