new system prototype
This commit is contained in:
@ -1,37 +1,50 @@
|
||||
<?php
|
||||
// /var/www/html/downloadFull.php
|
||||
require_once __DIR__ . '/common.php';
|
||||
require_once __DIR__ . '/db_init.php';
|
||||
|
||||
// Bearer Token
|
||||
$headers = function_exists('getallheaders') ? getallheaders() : [];
|
||||
$auth = $headers['Authorization'] ?? $headers['authorization'] ?? '';
|
||||
if (stripos($auth, 'Bearer ') !== 0) {
|
||||
http_response_code(403); echo "Kein Token übergeben"; exit;
|
||||
}
|
||||
$token = substr($auth, 7);
|
||||
if (!token_is_valid($token)) {
|
||||
http_response_code(403); echo "Ungültiger Token"; exit;
|
||||
}
|
||||
$tokenRec = require_valid_token();
|
||||
$token = $tokenRec['_token'];
|
||||
|
||||
// Master-verschlüsselte DB laden
|
||||
$dbPath = __DIR__ . '/uploads/questionnaire_database';
|
||||
if (!file_exists($dbPath)) { http_response_code(404); echo "Datenbank nicht gefunden"; exit; }
|
||||
$dbPath = QDB_PATH;
|
||||
if (!file_exists($dbPath)) { http_response_code(404); echo "Database not found"; exit; }
|
||||
$encMaster = file_get_contents($dbPath);
|
||||
if ($encMaster === false) { http_response_code(500); echo "Lesefehler"; exit; }
|
||||
if ($encMaster === false) { http_response_code(500); echo "Read error"; exit; }
|
||||
|
||||
// Mit Master-Key entschlüsseln
|
||||
$masterKey = get_master_key_bytes();
|
||||
try {
|
||||
$plain = aes256_cbc_decrypt_bytes($encMaster, $masterKey);
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500); echo "Entschlüsselung fehlgeschlagen"; exit;
|
||||
http_response_code(500); echo "Decryption failed"; exit;
|
||||
}
|
||||
|
||||
$role = $tokenRec['role'] ?? '';
|
||||
if ($role !== 'admin') {
|
||||
$tmpFilter = tempnam(sys_get_temp_dir(), 'qdb_filt_');
|
||||
file_put_contents($tmpFilter, $plain);
|
||||
try {
|
||||
$fpdo = new PDO("sqlite:$tmpFilter");
|
||||
$fpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$fpdo->exec("PRAGMA foreign_keys = OFF;");
|
||||
[$clause, $params] = rbac_client_filter($tokenRec);
|
||||
$delStmt = $fpdo->prepare("DELETE FROM client WHERE NOT ($clause)");
|
||||
foreach ($params as $k => $v) $delStmt->bindValue($k, $v);
|
||||
$delStmt->execute();
|
||||
$fpdo->exec("DELETE FROM client_answer WHERE clientCode NOT IN (SELECT clientCode FROM client)");
|
||||
$fpdo->exec("DELETE FROM completed_questionnaire WHERE clientCode NOT IN (SELECT clientCode FROM client)");
|
||||
// Remove sensitive tables that non-admin users should never receive
|
||||
foreach (['users', 'admin', 'supervisor', 'coach'] as $tbl) {
|
||||
$fpdo->exec("DELETE FROM $tbl");
|
||||
}
|
||||
$fpdo = null;
|
||||
$plain = file_get_contents($tmpFilter);
|
||||
} finally {
|
||||
@unlink($tmpFilter);
|
||||
}
|
||||
}
|
||||
|
||||
// Für diese Session neu verschlüsseln (Key aus Token via HKDF)
|
||||
$sessionKey = hkdf_session_key_from_token($token);
|
||||
$out = aes256_cbc_encrypt_bytes($plain, $sessionKey);
|
||||
|
||||
// Ausliefern
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Disposition: attachment; filename="questionnaire_database"');
|
||||
header('Content-Length: ' . strlen($out));
|
||||
|
||||
Reference in New Issue
Block a user