['label' => 'Dark', 'scheme' => 'dark', 'colors' => [ 'ink' => '#0a0a12', 'panel' => '#12121d', 'panel2' => '#191927', 'line' => '#26263a', 'muted' => '#a3a3b8', 'text' => '#f4f4f8', 'brand' => '#f43676', 'brand_alt' => '#ff6b9d', ]], 'light' => ['label' => 'Light', 'scheme' => 'light', 'colors' => [ 'ink' => '#f4f6f8', 'panel' => '#ffffff', 'panel2' => '#eceff3', 'line' => '#d9dee5', 'muted' => '#5d6a73', 'text' => '#192c27', 'brand' => '#009ba3', 'brand_alt' => '#007c83', ]], ]; /** @return array> */ public function all(): array { $themes = self::BUILT_IN; foreach ((array) config('manager.themes', []) as $key => $theme) { $themes[(string) $key] = $theme; } $path = config('manager.themes_path') ?: (function_exists('resource_path') ? resource_path('manager-themes') : null); foreach ($path && is_dir($path) ? glob($path.'/*.json') : [] as $file) { $theme = json_decode((string) file_get_contents($file), true); if (is_array($theme)) { $themes[basename($file, '.json')] = $theme; } } $clean = []; foreach ($themes as $key => $theme) { if (preg_match('/^[a-z0-9][a-z0-9-]{0,40}$/', (string) $key) && ($safe = $this->sanitize($key, $theme))) { $clean[$key] = $safe; } } return $clean; } /** The key of the theme this account sees: its own choice when allowed, else the site's. */ public function keyFor(?object $user): string { $all = $this->all(); $default = array_key_exists(config('manager.theme'), $all) ? config('manager.theme') : 'dark'; if ($user !== null && config('manager.user_themes') && $user->getKey() !== null) { $chosen = UserSetting::query()->where('user_id', $user->getKey())->value('theme'); if ($chosen !== null && isset($all[$chosen])) { return $chosen; } } return $default; } /** @return array|null */ public function sanitize(string $key, mixed $theme): ?array { if (! is_array($theme)) { return null; } $base = self::BUILT_IN[($theme['scheme'] ?? 'dark') === 'light' ? 'light' : 'dark']; $colors = $base['colors']; foreach (self::COLORS as $name) { $value = $theme['colors'][$name] ?? null; if (is_string($value) && preg_match('/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $value)) { $colors[$name] = $value; } } $radius = $theme['radius'] ?? '0.5rem'; $font = $theme['font'] ?? null; $display = $theme['font_display'] ?? null; $logo = $theme['logo'] ?? null; $name = $theme['name'] ?? null; $label = $theme['label'] ?? ucfirst(str_replace('-', ' ', $key)); return [ 'label' => is_string($label) ? mb_substr(strip_tags($label), 0, 60) : $key, 'scheme' => $base['scheme'], 'colors' => $colors, 'radius' => is_string($radius) && preg_match('/^\d{1,2}(\.\d{1,2})?(px|rem)$/', $radius) ? $radius : '0.5rem', 'font' => is_string($font) && preg_match('/^[\w ,\'"-]{1,120}$/', $font) ? $font : null, 'font_display' => is_string($display) && preg_match('/^[\w ,\'"-]{1,120}$/', $display) ? $display : null, 'logo' => is_string($logo) && preg_match('#^(/|https://)[\w\-./%~]{1,300}$#', $logo) ? $logo : null, 'name' => is_string($name) ? mb_substr(strip_tags($name), 0, 80) : null, ]; } /** The CSS variables of one theme, ready for a style block (values are already checked). */ public function css(array $theme): string { $vars = []; foreach ($theme['colors'] as $name => $value) { $vars[] = '--m-'.str_replace('_', '-', $name).':'.$value; } $vars[] = '--m-radius:'.$theme['radius']; $theme['font'] && $vars[] = '--m-font:'.$theme['font']; $theme['font_display'] && $vars[] = '--m-font-display:'.$theme['font_display']; $vars[] = 'color-scheme:'.$theme['scheme']; return ':root{'.implode(';', $vars).'}'; } }