Let admins upload their own custom fonts
Adds a new custom_fonts table (name, a server-generated collision-free
font_family identifier, file_path) with its own upload endpoint
(CustomFontController) separate from the generic image-only /uploads,
since it needs to accept WOFF2/WOFF/TTF/OTF instead. New management UI in
WebsiteSettings.jsx ("Eigene Schriftarten"): upload with a display name,
list with delete.
CustomFontsContext.jsx fetches the list once per layout (PublicLayout.jsx
for visitors, AdminLayout.jsx for the dashboard) and exposes it two ways:
useFontOptionsWithCustom() appends the uploaded fonts to the existing
static FONT_OPTIONS list for every font picker (RichTextEditor,
TextStyleWidget, the per-section and global site-font dropdowns), and a
new CustomFontFaces.jsx component renders the @font-face rules that
actually make an uploaded font usable - without it, picking a custom font
would just reference a name the browser has no file for. refresh() lets
WebsiteSettings.jsx update every picker immediately after an
upload/delete, not just on next page load.
Fonts::isValid() replaces the three places that validated a font_family
value against the fixed Fonts::VALUES list (site-wide default, per-section
override, per-field text style) - it now also accepts any uploaded
font's generated identifier, so those endpoints don't reject a custom
font as an unknown value.
Verified locally end-to-end: migration creates the table, a simulated
upload's font_family passes validation while a nonexistent one doesn't,
index() returns the expected shape, and destroy() removes both the row
and the file from uploads/.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
b690f7ef33
commit
326d124a60
20 changed files with 661 additions and 337 deletions
|
|
@ -3,6 +3,8 @@ import { Link, NavLink, Outlet, useNavigate } from 'react-router-dom';
|
|||
import { useAuth } from '../context/AuthContext.jsx';
|
||||
import { api } from '../api/client.js';
|
||||
import ThemeToggle from './ThemeToggle.jsx';
|
||||
import { CustomFontsProvider } from '../context/CustomFontsContext.jsx';
|
||||
import CustomFontFaces from './CustomFontFaces.jsx';
|
||||
import logo from '../assets/logo.png';
|
||||
import logoDark from '../assets/logo-dark.png';
|
||||
|
||||
|
|
@ -32,12 +34,18 @@ export default function AdminLayout() {
|
|||
const navigate = useNavigate();
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
const [newContactCount, setNewContactCount] = useState(0);
|
||||
const [customFonts, setCustomFonts] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasPermission('contact.manage')) return;
|
||||
api.get('/contact').then((rows) => setNewContactCount(rows.filter((r) => r.status === 'new').length)).catch(() => {});
|
||||
}, []);
|
||||
|
||||
const loadCustomFonts = () => api.get('/custom-fonts').then(setCustomFonts).catch(() => {});
|
||||
useEffect(() => {
|
||||
loadCustomFonts();
|
||||
}, []);
|
||||
|
||||
const navGroups = [
|
||||
{
|
||||
title: null,
|
||||
|
|
@ -128,7 +136,9 @@ export default function AdminLayout() {
|
|||
);
|
||||
|
||||
return (
|
||||
<CustomFontsProvider value={{ fonts: customFonts, refresh: loadCustomFonts }}>
|
||||
<div className="flex min-h-screen bg-neutral-50 text-neutral-900 dark:bg-neutral-950 dark:text-neutral-100">
|
||||
<CustomFontFaces />
|
||||
<aside className="hidden w-60 shrink-0 border-r border-neutral-200 p-4 dark:border-neutral-800 sm:block">
|
||||
<div className="mb-6 flex items-center gap-2 px-3">
|
||||
<Link to="/" className="rounded-lg px-2 py-1">
|
||||
|
|
@ -182,5 +192,6 @@ export default function AdminLayout() {
|
|||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</CustomFontsProvider>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
27
hifi-src/src/components/CustomFontFaces.jsx
Normal file
27
hifi-src/src/components/CustomFontFaces.jsx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { useCustomFonts } from '../context/CustomFontsContext.jsx';
|
||||
|
||||
function extensionOf(path) {
|
||||
const match = /\.([a-z0-9]+)$/i.exec(path || '');
|
||||
return match ? match[1].toLowerCase() : '';
|
||||
}
|
||||
|
||||
const FORMAT_BY_EXTENSION = { woff2: 'woff2', woff: 'woff', ttf: 'truetype', otf: 'opentype' };
|
||||
|
||||
// @font-face-Regeln fuer jede hochgeladene Schriftart - ohne das koennte font-family im Rest
|
||||
// der Seite zwar den generierten Bezeichner referenzieren, der Browser wuesste aber nicht,
|
||||
// woher die eigentliche Schriftdatei kommt. Ein <style>-Tag wirkt unabhaengig davon, wo im DOM
|
||||
// es steht (nicht auf <head> beschraenkt) - hier einmal pro Layout gerendert (siehe
|
||||
// PublicLayout.jsx/AdminLayout.jsx), damit die Regeln nur einmal pro Schriftart entstehen.
|
||||
export default function CustomFontFaces() {
|
||||
const customFonts = useCustomFonts();
|
||||
if (customFonts.length === 0) return null;
|
||||
|
||||
const css = customFonts
|
||||
.map((f) => {
|
||||
const format = FORMAT_BY_EXTENSION[extensionOf(f.file_path)] || 'woff2';
|
||||
return `@font-face { font-family: "${f.font_family}"; src: url("${f.file_path}") format("${format}"); font-display: swap; }`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return <style>{css}</style>;
|
||||
}
|
||||
|
|
@ -10,6 +10,8 @@ import GoogleAnalytics from './GoogleAnalytics.jsx';
|
|||
import { MaintenanceProvider } from '../context/MaintenanceContext.jsx';
|
||||
import { SiteSettingsProvider } from '../context/SiteSettingsContext.jsx';
|
||||
import { TextStylesProvider } from '../context/TextStylesContext.jsx';
|
||||
import { CustomFontsProvider } from '../context/CustomFontsContext.jsx';
|
||||
import CustomFontFaces from './CustomFontFaces.jsx';
|
||||
import { LanguageProvider } from '../context/LanguageContext.jsx';
|
||||
import { useAuth } from '../context/AuthContext.jsx';
|
||||
import { api } from '../api/client.js';
|
||||
|
|
@ -60,6 +62,7 @@ export default function PublicLayout() {
|
|||
const [status, setStatus] = useState(null);
|
||||
const [siteSettings, setSiteSettings] = useState(DEFAULT_SITE_SETTINGS);
|
||||
const [textStyles, setTextStyles] = useState({});
|
||||
const [customFonts, setCustomFonts] = useState([]);
|
||||
const { loading: authLoading, hasPermission } = useAuth();
|
||||
const location = useLocation();
|
||||
|
||||
|
|
@ -67,6 +70,7 @@ export default function PublicLayout() {
|
|||
api.get('/maintenance').then(setStatus).catch(() => setStatus(FALLBACK_STATUS));
|
||||
api.get('/site-settings').then(setSiteSettings).catch(() => {});
|
||||
api.get('/text-styles').then(setTextStyles).catch(() => {});
|
||||
api.get('/custom-fonts').then(setCustomFonts).catch(() => {});
|
||||
}, []);
|
||||
|
||||
// Globale Schriftart als CSS-Variable auf <html> - tailwind.config.js definiert font-sans
|
||||
|
|
@ -100,7 +104,9 @@ export default function PublicLayout() {
|
|||
<MaintenanceProvider value={{ ...status, bypass }}>
|
||||
<SiteSettingsProvider value={siteSettings}>
|
||||
<TextStylesProvider value={textStyles}>
|
||||
<CustomFontsProvider value={{ fonts: customFonts, refresh: () => {} }}>
|
||||
<div className="page-texture flex min-h-screen flex-col text-neutral-900 dark:text-neutral-100">
|
||||
<CustomFontFaces />
|
||||
{bypass && status.global.enabled && <MaintenanceBypassBanner />}
|
||||
<ScrollProgress />
|
||||
<Navbar />
|
||||
|
|
@ -111,6 +117,7 @@ export default function PublicLayout() {
|
|||
<CookieConsentBanner />
|
||||
<GoogleAnalytics />
|
||||
</div>
|
||||
</CustomFontsProvider>
|
||||
</TextStylesProvider>
|
||||
</SiteSettingsProvider>
|
||||
</MaintenanceProvider>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { Underline } from '@tiptap/extension-underline';
|
|||
import { Link } from '@tiptap/extension-link';
|
||||
import { TextStyle } from '@tiptap/extension-text-style';
|
||||
import { FontFamily } from '@tiptap/extension-font-family';
|
||||
import { FONT_OPTIONS } from '../utils/fontOptions.js';
|
||||
import { useFontOptionsWithCustom } from '../context/CustomFontsContext.jsx';
|
||||
|
||||
function ToolbarButton({ active, onClick, title, children }) {
|
||||
return (
|
||||
|
|
@ -25,6 +25,7 @@ function ToolbarButton({ active, onClick, title, children }) {
|
|||
}
|
||||
|
||||
export default function RichTextEditor({ value, onChange, label }) {
|
||||
const fontOptions = useFontOptionsWithCustom();
|
||||
const editor = useEditor({
|
||||
extensions: [StarterKit, Underline, TextStyle, FontFamily, Link.configure({ openOnClick: false })],
|
||||
content: value || '',
|
||||
|
|
@ -137,7 +138,7 @@ export default function RichTextEditor({ value, onChange, label }) {
|
|||
}}
|
||||
className="rounded border border-neutral-300 bg-white px-2 py-1 text-xs dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
{FONT_OPTIONS.map((f) => (
|
||||
{fontOptions.map((f) => (
|
||||
<option key={f.label} value={f.value}>
|
||||
{f.label}
|
||||
</option>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
import { api } from '../api/client.js';
|
||||
import { FONT_OPTIONS } from '../utils/fontOptions.js';
|
||||
import { useFontOptionsWithCustom } from '../context/CustomFontsContext.jsx';
|
||||
import { FONT_SIZE_OPTIONS } from '../utils/fontSizeOptions.js';
|
||||
|
||||
const EMPTY = { font_family: '', font_size: '', color: '' };
|
||||
|
|
@ -12,6 +12,7 @@ const EMPTY = { font_family: '', font_size: '', color: '' };
|
|||
* (siehe App\Support\TextStyleFields fuer gueltige Kombinationen).
|
||||
*/
|
||||
export default function TextStyleWidget({ fieldKey, previewText = 'Beispieltext', label = 'Stil' }) {
|
||||
const fontOptions = useFontOptionsWithCustom();
|
||||
const [style, setStyle] = useState(EMPTY);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
|
|
@ -57,7 +58,7 @@ export default function TextStyleWidget({ fieldKey, previewText = 'Beispieltext'
|
|||
onChange={(e) => save({ ...style, font_family: e.target.value })}
|
||||
className="rounded-md border border-neutral-300 bg-white px-2 py-1 text-xs dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
{FONT_OPTIONS.map((f) => (
|
||||
{fontOptions.map((f) => (
|
||||
<option key={f.label} value={f.value}>{f.label}</option>
|
||||
))}
|
||||
</select>
|
||||
|
|
|
|||
32
hifi-src/src/context/CustomFontsContext.jsx
Normal file
32
hifi-src/src/context/CustomFontsContext.jsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { createContext, useContext } from 'react';
|
||||
import { FONT_OPTIONS } from '../utils/fontOptions.js';
|
||||
|
||||
// Vom Admin selbst hochgeladene Schriftarten (siehe CustomFontController) - ein Request pro
|
||||
// Seitenaufruf (siehe PublicLayout.jsx/AdminLayout.jsx), von dort ueber Context verteilt statt
|
||||
// dass jede einzelne Schriftart-Auswahl (RichTextEditor, TextStyleWidget, ...) selbst laedt.
|
||||
// refresh() laedt neu, ohne die ganze Seite neu zu laden - nach Hochladen/Loeschen einer
|
||||
// Schriftart in WebsiteSettings.jsx, damit alle Schriftart-Auswahlen sofort den neuen Stand
|
||||
// zeigen (auch auf derselben Seite), nicht erst nach Navigation/Reload.
|
||||
const CustomFontsContext = createContext({ fonts: [], refresh: () => {} });
|
||||
|
||||
export const CustomFontsProvider = CustomFontsContext.Provider;
|
||||
|
||||
/** Die rohe Liste der hochgeladenen Schriftarten ({id, name, font_family, file_path}). */
|
||||
export function useCustomFonts() {
|
||||
return useContext(CustomFontsContext).fonts;
|
||||
}
|
||||
|
||||
/** Laedt die Liste neu - nach Hochladen/Loeschen einer eigenen Schriftart aufrufen. */
|
||||
export function useCustomFontsRefresh() {
|
||||
return useContext(CustomFontsContext).refresh;
|
||||
}
|
||||
|
||||
/** FONT_OPTIONS ergaenzt um die hochgeladenen Schriftarten - fuer jede Schriftart-Auswahl im
|
||||
* Dashboard, damit eigene Schriftarten dort ueberall gleichermassen zur Wahl stehen. */
|
||||
export function useFontOptionsWithCustom() {
|
||||
const customFonts = useCustomFonts();
|
||||
return [
|
||||
...FONT_OPTIONS,
|
||||
...customFonts.map((f) => ({ label: f.name, value: f.font_family })),
|
||||
];
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ import { api } from '../../api/client.js';
|
|||
import RichTextEditor from '../../components/RichTextEditor.jsx';
|
||||
import TextStyleWidget from '../../components/TextStyleWidget.jsx';
|
||||
import { dragAnchorStyle } from '../../utils/dragAnchorPosition.js';
|
||||
import { FONT_OPTIONS } from '../../utils/fontOptions.js';
|
||||
import { useFontOptionsWithCustom } from '../../context/CustomFontsContext.jsx';
|
||||
|
||||
const SECTIONS = [
|
||||
{ slug: 'hero', label: 'Hero (ganz oben)' },
|
||||
|
|
@ -39,6 +39,7 @@ function stripHtml(html) {
|
|||
}
|
||||
|
||||
export default function HomeSections() {
|
||||
const fontOptions = useFontOptionsWithCustom();
|
||||
const [sections, setSections] = useState(null);
|
||||
const [activeSlug, setActiveSlug] = useState('hero');
|
||||
const [activeLang, setActiveLang] = useState('de');
|
||||
|
|
@ -354,7 +355,7 @@ export default function HomeSections() {
|
|||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
<option value="">Standard (globale Schriftart)</option>
|
||||
{FONT_OPTIONS.filter((f) => f.value).map((f) => (
|
||||
{fontOptions.filter((f) => f.value).map((f) => (
|
||||
<option key={f.label} value={f.value}>{f.label}</option>
|
||||
))}
|
||||
</select>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
|||
import { api } from '../../../api/client.js';
|
||||
import ImageUploadField from '../../../components/ImageUploadField.jsx';
|
||||
import { dragAnchorStyle } from '../../../utils/dragAnchorPosition.js';
|
||||
import { FONT_OPTIONS } from '../../../utils/fontOptions.js';
|
||||
import { useCustomFonts, useCustomFontsRefresh, useFontOptionsWithCustom } from '../../../context/CustomFontsContext.jsx';
|
||||
|
||||
const DAYS = [
|
||||
{ key: 'mon', label: 'Montag' },
|
||||
|
|
@ -51,6 +51,40 @@ const PACKAGE_CARD_LAYOUTS = [
|
|||
];
|
||||
|
||||
export default function WebsiteSettings() {
|
||||
const fontOptions = useFontOptionsWithCustom();
|
||||
const customFonts = useCustomFonts();
|
||||
const refreshCustomFonts = useCustomFontsRefresh();
|
||||
const [fontName, setFontName] = useState('');
|
||||
const [fontFile, setFontFile] = useState(null);
|
||||
const [fontUploadBusy, setFontUploadBusy] = useState(false);
|
||||
const [fontError, setFontError] = useState('');
|
||||
|
||||
const handleUploadFont = async (e) => {
|
||||
e.preventDefault();
|
||||
setFontError('');
|
||||
setFontUploadBusy(true);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('name', fontName);
|
||||
formData.append('file', fontFile);
|
||||
await api.post('/custom-fonts', formData);
|
||||
setFontName('');
|
||||
setFontFile(null);
|
||||
e.target.reset();
|
||||
refreshCustomFonts();
|
||||
} catch (err) {
|
||||
setFontError(err.message);
|
||||
} finally {
|
||||
setFontUploadBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteFont = async (id) => {
|
||||
if (!confirm('Schriftart wirklich löschen? Felder, die sie noch verwenden, fallen danach auf die Standardschrift zurück.')) return;
|
||||
await api.delete(`/custom-fonts/${id}`);
|
||||
refreshCustomFonts();
|
||||
};
|
||||
|
||||
const [form, setForm] = useState(emptyForm);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
|
@ -283,12 +317,68 @@ export default function WebsiteSettings() {
|
|||
onChange={(e) => update('site_font', e.target.value)}
|
||||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
{FONT_OPTIONS.map((f) => (
|
||||
{fontOptions.map((f) => (
|
||||
<option key={f.label} value={f.value}>{f.label}</option>
|
||||
))}
|
||||
</select>
|
||||
</section>
|
||||
|
||||
<section className="rounded-xl border border-neutral-200 bg-white p-6 dark:border-neutral-800 dark:bg-neutral-900">
|
||||
<h2 className="mb-1 font-semibold text-neutral-900 dark:text-white">Eigene Schriftarten</h2>
|
||||
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
|
||||
Eigene Schriftart-Dateien (WOFF2, WOFF, TTF oder OTF) hochladen - stehen danach in
|
||||
jeder Schriftart-Auswahl im Dashboard zusätzlich zur Wahl.
|
||||
</p>
|
||||
|
||||
{customFonts.length > 0 && (
|
||||
<ul className="mb-4 space-y-2">
|
||||
{customFonts.map((f) => (
|
||||
<li key={f.id} className="flex items-center justify-between gap-2 rounded-md border border-neutral-200 px-3 py-2 text-sm dark:border-neutral-700">
|
||||
<span style={{ fontFamily: f.font_family }}>{f.name}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDeleteFont(f.id)}
|
||||
className="text-red-600 hover:underline"
|
||||
>
|
||||
Löschen
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleUploadFont} className="flex flex-wrap items-end gap-2">
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Name</label>
|
||||
<input
|
||||
required
|
||||
value={fontName}
|
||||
onChange={(e) => setFontName(e.target.value)}
|
||||
placeholder="z. B. Firmenschrift"
|
||||
className="rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Datei</label>
|
||||
<input
|
||||
required
|
||||
type="file"
|
||||
accept=".woff2,.woff,.ttf,.otf"
|
||||
onChange={(e) => setFontFile(e.target.files?.[0] || null)}
|
||||
className="text-sm"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={fontUploadBusy}
|
||||
className="rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600 disabled:opacity-50"
|
||||
>
|
||||
{fontUploadBusy ? 'Lädt hoch…' : 'Hochladen'}
|
||||
</button>
|
||||
</form>
|
||||
{fontError && <p className="mt-2 text-sm text-red-600">{fontError}</p>}
|
||||
</section>
|
||||
|
||||
<section className="rounded-xl border border-neutral-200 bg-white p-6 dark:border-neutral-800 dark:bg-neutral-900">
|
||||
<h2 className="mb-1 font-semibold text-neutral-900 dark:text-white">Kontaktdaten</h2>
|
||||
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
|
||||
|
|
|
|||
16
hifi/api/database/migration_custom_fonts.sql
Normal file
16
hifi/api/database/migration_custom_fonts.sql
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
USE hifi_shop;
|
||||
|
||||
-- Vom Admin selbst hochgeladene Schriftart-Dateien (WOFF2/WOFF/TTF/OTF), stehen danach neben
|
||||
-- der festen System-Schriftartenliste in jeder Schriftart-Auswahl zur Verfuegung. font_family
|
||||
-- ist ein serverseitig erzeugter, kollisionsfreier CSS-Bezeichner, nicht der Anzeigename.
|
||||
-- Additiv, nichts wird geloescht.
|
||||
-- Hinweis: Auf der Live-Seite reicht stattdessen ein Klick auf "Datenbankstruktur aktualisieren"
|
||||
-- unter Admin-Panel -> Einstellungen -> Datenbank.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS custom_fonts (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
font_family VARCHAR(50) NOT NULL UNIQUE,
|
||||
file_path VARCHAR(255) NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB;
|
||||
|
|
@ -152,6 +152,14 @@ CREATE TABLE text_styles (
|
|||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
CREATE TABLE custom_fonts (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
font_family VARCHAR(50) NOT NULL UNIQUE,
|
||||
file_path VARCHAR(255) NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
CREATE TABLE car_models (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
brand_id INT NOT NULL,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use App\Controllers\AnalyticsController;
|
|||
use App\Controllers\AuthController;
|
||||
use App\Controllers\BrandController;
|
||||
use App\Controllers\ContactController;
|
||||
use App\Controllers\CustomFontController;
|
||||
use App\Controllers\DatabaseConfigController;
|
||||
use App\Controllers\FaqController;
|
||||
use App\Controllers\GalleryBrandController;
|
||||
|
|
@ -181,6 +182,10 @@ $router->get('/text-styles', fn($p) => TextStyleController::index());
|
|||
$router->get('/text-styles/one', fn($p) => TextStyleController::show());
|
||||
$router->put('/text-styles', $admin(fn($p) => TextStyleController::update()));
|
||||
|
||||
$router->get('/custom-fonts', fn($p) => CustomFontController::index());
|
||||
$router->post('/custom-fonts', $perm('settings.manage', fn($p) => CustomFontController::store()));
|
||||
$router->delete('/custom-fonts/{id}', $perm('settings.manage', fn($p) => CustomFontController::destroy($p)));
|
||||
|
||||
// Nur der zuletzt gecachte Stand - kein Live-Aufruf bei Google pro Seitenbesuch
|
||||
// (Aktualisierung laeuft ausschliesslich ueber refresh_google_reviews.php).
|
||||
$router->get('/google-reviews', fn($p) => GoogleReviewsController::publicReviews());
|
||||
|
|
|
|||
85
hifi/api/src/Controllers/CustomFontController.php
Normal file
85
hifi/api/src/Controllers/CustomFontController.php
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Config\Database;
|
||||
use App\Support\Http;
|
||||
use App\Support\Uploads;
|
||||
|
||||
/**
|
||||
* Vom Admin selbst hochgeladene Schriftart-Dateien - stehen danach neben der festen
|
||||
* Fonts::VALUES-Liste in jeder Schriftart-Auswahl zur Verfuegung (siehe fontOptions.js/
|
||||
* CustomFontsContext.jsx). Eigener Upload-Endpunkt statt des generischen /uploads, da der
|
||||
* bewusst auf Bild-Dateitypen beschraenkt ist (UploadController::ALLOWED_MIME).
|
||||
*/
|
||||
class CustomFontController
|
||||
{
|
||||
private const ALLOWED_EXTENSIONS = ['woff2', 'woff', 'ttf', 'otf'];
|
||||
private const MAX_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
public static function index(): void
|
||||
{
|
||||
$stmt = Database::connection()->query(
|
||||
'SELECT id, name, font_family, file_path FROM custom_fonts ORDER BY name'
|
||||
);
|
||||
Http::send($stmt->fetchAll());
|
||||
}
|
||||
|
||||
public static function store(): void
|
||||
{
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
if ($name === '') {
|
||||
Http::error('Name erforderlich', 422);
|
||||
}
|
||||
if (empty($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
||||
Http::error('Keine Datei übermittelt', 422);
|
||||
}
|
||||
|
||||
$file = $_FILES['file'];
|
||||
if ($file['size'] > self::MAX_BYTES) {
|
||||
Http::error('Datei zu groß (max. 2 MB)', 422);
|
||||
}
|
||||
|
||||
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
|
||||
if (!in_array($extension, self::ALLOWED_EXTENSIONS, true)) {
|
||||
Http::error('Nur WOFF2, WOFF, TTF oder OTF erlaubt', 422);
|
||||
}
|
||||
|
||||
$filename = bin2hex(random_bytes(16)) . '.' . $extension;
|
||||
$destination = __DIR__ . '/../../uploads/' . $filename;
|
||||
if (!move_uploaded_file($file['tmp_name'], $destination)) {
|
||||
Http::error('Datei konnte nicht gespeichert werden', 500);
|
||||
}
|
||||
|
||||
$config = require __DIR__ . '/../../config/config.php';
|
||||
$prefix = rtrim($config['app']['base_path'], '/') . '/api/uploads/';
|
||||
$filePath = $prefix . $filename;
|
||||
|
||||
// Kollisionsfreier CSS-Bezeichner, unabhaengig vom (nicht garantiert eindeutigen)
|
||||
// Anzeigenamen - wird direkt als font-family-Wert verwendet, muss also ohne
|
||||
// Escaping/Anfuehrungszeichen sicher in CSS einsetzbar sein.
|
||||
$fontFamily = 'custom-font-' . bin2hex(random_bytes(4));
|
||||
|
||||
$db = Database::connection();
|
||||
$stmt = $db->prepare('INSERT INTO custom_fonts (name, font_family, file_path) VALUES (?, ?, ?)');
|
||||
$stmt->execute([$name, $fontFamily, $filePath]);
|
||||
|
||||
Http::send(['id' => (int) $db->lastInsertId(), 'name' => $name, 'font_family' => $fontFamily, 'file_path' => $filePath], 201);
|
||||
}
|
||||
|
||||
public static function destroy(array $params): void
|
||||
{
|
||||
$id = (int) $params['id'];
|
||||
$db = Database::connection();
|
||||
|
||||
$stmt = $db->prepare('SELECT file_path FROM custom_fonts WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
$filePath = $stmt->fetchColumn();
|
||||
|
||||
$db->prepare('DELETE FROM custom_fonts WHERE id = ?')->execute([$id]);
|
||||
|
||||
Uploads::deleteIfOrphaned($filePath ?: null, null);
|
||||
|
||||
Http::send(['ok' => true]);
|
||||
}
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ class HomeSectionController
|
|||
// Schriftart-Override dieser Sektion - leer/ungueltig = "Standard" (faellt in Home.jsx
|
||||
// auf die globale Website-Schriftart zurueck, siehe app_settings.site_font).
|
||||
$fontFamily = (string) ($body['font_family'] ?? '');
|
||||
if (!in_array($fontFamily, Fonts::VALUES, true)) {
|
||||
if (!Fonts::isValid($fontFamily)) {
|
||||
$fontFamily = '';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ class SiteSettingsController
|
|||
// Globale Standard-Schriftart der Website - faellt auf Systemschrift (leerer String)
|
||||
// zurueck, wenn ein unbekannter Wert ankommt, statt ihn ungeprueft zu speichern.
|
||||
$siteFont = (string) ($body['site_font'] ?? '');
|
||||
if (!in_array($siteFont, Fonts::VALUES, true)) {
|
||||
if (!Fonts::isValid($siteFont)) {
|
||||
$siteFont = '';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ class TextStyleController
|
|||
AuthMiddleware::requirePermission($entry['permission']);
|
||||
|
||||
$fontFamily = (string) ($body['font_family'] ?? '');
|
||||
if (!in_array($fontFamily, Fonts::VALUES, true)) {
|
||||
if (!Fonts::isValid($fontFamily)) {
|
||||
$fontFamily = '';
|
||||
}
|
||||
$fontSize = (string) ($body['font_size'] ?? '');
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
/**
|
||||
* Muss zu hifi-src/src/utils/fontOptions.js (FONT_OPTIONS) passen - dieselbe Liste wird dort
|
||||
* vom Rich-Text-Editor (Mail-Vorlagen/Rechtstexte), der globalen Website-Schriftart und der
|
||||
|
|
@ -14,4 +16,22 @@ class Fonts
|
|||
'"Times New Roman", Times, serif', 'Verdana, Geneva, sans-serif',
|
||||
'"Courier New", Courier, monospace',
|
||||
];
|
||||
|
||||
/**
|
||||
* Gueltig ist entweder einer der festen Werte oben, oder der font_family-Bezeichner einer
|
||||
* selbst hochgeladenen Schriftart (siehe CustomFontController) - jede Stelle, die bisher
|
||||
* gegen VALUES geprueft hat, prueft stattdessen hierueber.
|
||||
*/
|
||||
public static function isValid(string $value): bool
|
||||
{
|
||||
if (in_array($value, self::VALUES, true)) {
|
||||
return true;
|
||||
}
|
||||
if ($value === '') {
|
||||
return true;
|
||||
}
|
||||
$stmt = Database::connection()->prepare('SELECT 1 FROM custom_fonts WHERE font_family = ?');
|
||||
$stmt->execute([$value]);
|
||||
return (bool) $stmt->fetchColumn();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -183,6 +183,18 @@ class Schema
|
|||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB",
|
||||
|
||||
// Vom Admin selbst hochgeladene Schriftart-Dateien (WOFF2/WOFF/TTF/OTF), stehen danach
|
||||
// neben der festen Fonts::VALUES-Liste in jeder Schriftart-Auswahl zur Verfuegung.
|
||||
// font_family ist ein serverseitig erzeugter, kollisionsfreier CSS-Bezeichner
|
||||
// (z.B. "custom-font-a1b2c3d4"), NICHT der vom Admin eingegebene Anzeigename.
|
||||
'custom_fonts' => "CREATE TABLE custom_fonts (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
font_family VARCHAR(50) NOT NULL UNIQUE,
|
||||
file_path VARCHAR(255) NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB",
|
||||
|
||||
'car_models' => "CREATE TABLE car_models (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
brand_id INT NOT NULL,
|
||||
|
|
@ -471,6 +483,13 @@ class Schema
|
|||
'color' => 'VARCHAR(20) NULL',
|
||||
'updated_at' => 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
|
||||
],
|
||||
'custom_fonts' => [
|
||||
'id' => 'INT AUTO_INCREMENT PRIMARY KEY',
|
||||
'name' => 'VARCHAR(100) NOT NULL',
|
||||
'font_family' => 'VARCHAR(50) NOT NULL',
|
||||
'file_path' => 'VARCHAR(255) NOT NULL',
|
||||
'created_at' => 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
|
||||
],
|
||||
'car_models' => [
|
||||
'id' => 'INT AUTO_INCREMENT PRIMARY KEY',
|
||||
'brand_id' => 'INT NOT NULL',
|
||||
|
|
|
|||
326
hifi/assets/index-BVDYjPgo.js
Normal file
326
hifi/assets/index-BVDYjPgo.js
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -43,7 +43,7 @@
|
|||
"sameAs": ["https://www.youtube.com/@hifiplanet2812"]
|
||||
}
|
||||
</script>
|
||||
<script type="module" crossorigin src="/assets/index-kZ4ovh9r.js"></script>
|
||||
<script type="module" crossorigin src="/assets/index-BVDYjPgo.js"></script>
|
||||
<link rel="modulepreload" crossorigin href="/assets/lucide-icons-Cg2IxtAO.js">
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-lUKoqEad.css">
|
||||
</head>
|
||||
|
|
|
|||
Loading…
Reference in a new issue