136 lines
5.2 KiB
PHP
136 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
require_once __DIR__ . '/tenant-logo.php';
|
|
|
|
/**
|
|
* Assembles a full export of everything stored for one tenant (data
|
|
* portability / GDPR Art. 20). Excludes password_hash and token hashes;
|
|
* everything else that is tenant-scoped is included as-is.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
function app_export_tenant_data(PDO $pdo, int $tenantId): array
|
|
{
|
|
$stmt = $pdo->prepare('SELECT id, slug, paypal_inbox_token, name, customer_type, status, timezone, locale, currency_code, created_at, updated_at FROM tenants WHERE id = ?');
|
|
$stmt->execute([$tenantId]);
|
|
$tenant = $stmt->fetch();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM tenant_settings WHERE tenant_id = ?');
|
|
$stmt->execute([$tenantId]);
|
|
$settings = $stmt->fetch();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM participants WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$participants = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare(
|
|
'SELECT u.id, u.email, u.display_name, u.status, u.email_verified_at, u.last_login_at, u.created_at,
|
|
tm.role, tm.status AS membership_status, tm.invited_at, tm.joined_at
|
|
FROM tenant_memberships tm
|
|
JOIN users u ON u.id = tm.user_id
|
|
WHERE tm.tenant_id = ?
|
|
ORDER BY u.id'
|
|
);
|
|
$stmt->execute([$tenantId]);
|
|
$members = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM ledger_entries WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$ledgerEntries = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT id, message, valid_from, valid_until, deleted_at, created_at FROM notices WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$notices = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT id, original_filename, status, created_at, committed_at FROM payment_import_batches WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$importBatches = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare(
|
|
'SELECT r.id, r.batch_id, r.row_num, r.raw_name, r.amount_cents, r.booked_at, r.status
|
|
FROM payment_import_rows r
|
|
JOIN payment_import_batches b ON b.id = r.batch_id
|
|
WHERE b.tenant_id = ?
|
|
ORDER BY r.id'
|
|
);
|
|
$stmt->execute([$tenantId]);
|
|
$importRows = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT id, participant_id, template, subject, status, sent_at, error, created_at FROM outbound_emails WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$outboundEmails = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT id, actor_user_id, action, subject_type, subject_id, metadata_json, ip, created_at FROM audit_log WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$auditLog = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM faq_entries WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$faqEntries = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM paypal_payments WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$paypalPayments = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM tenant_billing WHERE tenant_id = ?');
|
|
$stmt->execute([$tenantId]);
|
|
$billing = $stmt->fetch();
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM tenant_features WHERE tenant_id = ? ORDER BY id');
|
|
$stmt->execute([$tenantId]);
|
|
$features = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare(
|
|
'SELECT document_type, document_version, context, metadata_json, accepted_at
|
|
FROM legal_acceptances WHERE tenant_id = ? ORDER BY id'
|
|
);
|
|
$stmt->execute([$tenantId]);
|
|
$legalAcceptances = $stmt->fetchAll();
|
|
|
|
$stmt = $pdo->prepare(
|
|
'SELECT reference_code, request_type, requester_name, requester_email,
|
|
contract_reference, request_reason, requested_end, status,
|
|
metadata_json, received_at, processed_at
|
|
FROM legal_requests WHERE tenant_id = ? ORDER BY id'
|
|
);
|
|
$stmt->execute([$tenantId]);
|
|
$legalRequests = $stmt->fetchAll();
|
|
|
|
$logos = [];
|
|
foreach (['brand_logo', 'pdf_watermark_logo'] as $logoField) {
|
|
$filename = (string)($settings[$logoField] ?? '');
|
|
$path = tenant_logo_path($filename);
|
|
if ($path !== null) {
|
|
$logos[$logoField] = [
|
|
'filename' => $filename,
|
|
'mime_type' => mime_content_type($path) ?: 'application/octet-stream',
|
|
'base64' => base64_encode((string)file_get_contents($path)),
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'exported_at' => date('c'),
|
|
'tenant' => $tenant ?: null,
|
|
'tenant_settings' => $settings ?: null,
|
|
'participants' => $participants,
|
|
'members' => $members,
|
|
'ledger_entries' => $ledgerEntries,
|
|
'notices' => $notices,
|
|
'payment_import_batches' => $importBatches,
|
|
'payment_import_rows' => $importRows,
|
|
'outbound_emails' => $outboundEmails,
|
|
'audit_log' => $auditLog,
|
|
'faq_entries' => $faqEntries,
|
|
'paypal_payments' => $paypalPayments,
|
|
'billing' => $billing ?: null,
|
|
'features' => $features,
|
|
'legal_acceptances' => $legalAcceptances,
|
|
'legal_requests' => $legalRequests,
|
|
'logos' => $logos,
|
|
];
|
|
}
|