chore: initial commit

This commit is contained in:
2026-06-06 00:03:51 +01:00
commit 99d7f7d3a4
29 changed files with 5183 additions and 0 deletions
+128
View File
@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/db.php';
try {
match ($_SERVER['REQUEST_METHOD']) {
'GET' => handle_get(),
'POST' => handle_post(),
default => error_out('Method not allowed', 405),
};
} catch (PDOException $e) {
error_out('Database error: ' . $e->getMessage(), 500);
}
// ─── GET /api/admin.php ───────────────────────────────────────────────────────
function handle_get(): never
{
$db = db(); // migrations run inside db()
$sandbox_mode = (bool) (int) ($db->query(
"SELECT setting_value FROM `system` WHERE setting_name = 'sandbox_mode'"
)->fetchColumn());
$counts = $db->query(
"SELECT
COUNT(*) AS total,
SUM(is_sandbox = 1) AS sandbox_count,
SUM(is_sandbox = 0 AND status IN ('sent','delivered')) AS real_sent,
SUM(is_sandbox = 0 AND status = 'failed') AS real_failed
FROM delivery_log"
)->fetch();
json_out([
'sandbox_mode' => $sandbox_mode,
'delivery_log_total' => (int) ($counts['total'] ?? 0),
'delivery_log_sandbox' => (int) ($counts['sandbox_count'] ?? 0),
'delivery_log_real_sent' => (int) ($counts['real_sent'] ?? 0),
'delivery_log_real_failed' => (int) ($counts['real_failed'] ?? 0),
'attendees' => (int) $db->query('SELECT COUNT(*) FROM attendees')->fetchColumn(),
'templates' => (int) $db->query('SELECT COUNT(*) FROM sms_templates')->fetchColumn(),
'schedules' => (int) $db->query('SELECT COUNT(*) FROM scheduled_rules')->fetchColumn(),
]);
}
// ─── POST /api/admin.php ──────────────────────────────────────────────────────
function handle_post(): never
{
$body = json_body();
$action = (string) ($body['action'] ?? '');
match ($action) {
'toggle_sandbox' => action_toggle_sandbox(),
'mark_all_sandbox' => action_mark_all_sandbox(),
'purge' => action_purge($body),
default => error_out('Unknown action'),
};
}
function action_toggle_sandbox(): never
{
$db = db();
$current = (int) $db->query(
"SELECT setting_value FROM `system` WHERE setting_name = 'sandbox_mode'"
)->fetchColumn();
$new = $current ? '0' : '1';
$db->prepare(
"UPDATE `system` SET setting_value = :v WHERE setting_name = 'sandbox_mode'"
)->execute([':v' => $new]);
json_out(['sandbox_mode' => (bool) (int) $new]);
}
function action_mark_all_sandbox(): never
{
db()->exec('UPDATE delivery_log SET is_sandbox = 1');
json_out(['updated' => true]);
}
function action_purge(array $body): never
{
$target = (string) ($body['target'] ?? '');
$db = db();
match ($target) {
'delivery_log' => purge_delivery_log($db, $body),
'attendees' => purge_attendees($db),
'templates' => purge_templates($db),
'schedules' => purge_schedules($db),
default => error_out('Unknown purge target'),
};
}
function purge_delivery_log(PDO $db, array $body): never
{
$before = isset($body['before']) ? trim((string) $body['before']) : '';
if ($before !== '') {
if (strtotime($before) === false) {
error_out('Invalid date');
}
$stmt = $db->prepare('DELETE FROM delivery_log WHERE sent_at < :before');
$stmt->execute([':before' => $before]);
json_out(['purged' => $stmt->rowCount()]);
}
$db->exec('DELETE FROM delivery_log');
json_out(['purged' => true]);
}
function purge_attendees(PDO $db): never
{
$db->exec('DELETE FROM registrations');
$db->exec('DELETE FROM attendees');
$db->exec('DELETE FROM workshop_sessions');
json_out(['purged' => true]);
}
function purge_templates(PDO $db): never
{
$db->exec('DELETE FROM sms_templates');
json_out(['purged' => true]);
}
function purge_schedules(PDO $db): never
{
$db->exec('DELETE FROM scheduled_rules');
json_out(['purged' => true]);
}