Files
reflector-dashboard/auth/theme_prefs_update.php
T

62 lines
2.2 KiB
PHP

<?php
/*
* Reflector Dashboard — web front-end for SvxReflector
* Copyright (C) 2026 SA7AUX (Björn)
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version. See the LICENSE file for details.
*/
declare(strict_types=1);
require __DIR__ . '/lib.php';
// Per-account web theme preference (so a signed-in operator's theme follows them
// across devices/browsers). Stored on the account hash — me.php returns the whole
// hash, so 'web_theme' + 'web_theme_colors' surface there automatically.
// web_theme: 'dark' | 'light' | 'color'
// web_theme_colors: compact JSON { bg,panel,fg,fg2,line,accent } of #hex values
// Anonymous visitors never reach this endpoint — they use localStorage only.
$uuid = require_login();
csrf_check();
$j = json_in();
$updated = [];
if (array_key_exists('theme', $j)) {
$t = is_string($j['theme']) ? strtolower(trim($j['theme'])) : '';
if (!in_array($t, ['dark', 'light', 'color'], true)) {
json_out(['ok' => false, 'error' => 'invalid_theme', 'allowed' => ['dark', 'light', 'color']], 400);
}
redis()->hSet(acct_key($uuid), 'web_theme', $t);
$updated['web_theme'] = $t;
}
if (array_key_exists('colors', $j)) {
$c = $j['colors'];
$allow = ['bg', 'panel', 'fg', 'fg2', 'line', 'accent'];
$clean = [];
if (is_array($c)) {
foreach ($allow as $k) {
if (isset($c[$k]) && is_string($c[$k]) && preg_match('/^#[0-9a-fA-F]{3,8}$/', $c[$k])) {
$clean[$k] = strtolower($c[$k]);
}
}
}
// Cast to object so an empty selection serialises as '{}', never '[]'.
$json = json_encode((object)$clean, JSON_UNESCAPED_SLASHES);
if ($json === false || strlen($json) > 500) {
json_out(['ok' => false, 'error' => 'invalid_colors'], 400);
}
redis()->hSet(acct_key($uuid), 'web_theme_colors', $json);
$updated['web_theme_colors'] = $clean;
}
if (empty($updated)) {
json_out(['ok' => false, 'error' => 'nothing_to_update'], 400);
}
json_out(['ok' => true, 'updated' => $updated]);