source: Klonkt/src/routes/admin-settings.js@ 6d3e2c1

main
Last change on this file since 6d3e2c1 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[6351545]1/**
[834bcc3]2 * Admin: global settings.
3 * - tenancy mode (Solo/Hub)
4 * - hub branding (name/tagline/intro/hero of the generic hub home page)
[6351545]5 *
[834bcc3]6 * GET /admin/settings -> show current settings
7 * POST /admin/settings -> save (god-only). Also accepts an uploaded
8 * hero image (multipart); an upload wins over the
9 * URL text field. Without an upload the URL field is leading.
[6351545]10 *
[834bcc3]11 * The hub page is generic (belonging to no user); this branding lives in
12 * global settings, not in a site.
[6351545]13 */
14
15import express from 'express';
[63edb0d]16import path from 'path';
17import fs from 'fs';
18import { fileURLToPath } from 'url';
19import multer from 'multer';
20import { v4 as uuid } from 'uuid';
[6351545]21import { renderPage } from '../middleware/render.js';
22import { requireGod } from '../middleware/auth.js';
[6e0249f]23import { getTenancy, setTenancy, getSetting, setSetting } from '../services/SettingsService.js';
[5e61b17]24import { SUPPORTED } from '../services/i18n.js';
[ca507ef]25import { mailerStatus, sendMail } from '../config/mailer.js';
[029f047]26import { entitlementStatus, premiumUnlocked } from '../services/PatreonService.js';
[2248cd2]27import { googleConfigured, redirectUri, currentClientId, clientSecretSet } from '../config/google.js';
[8f6225c]28import { toWebp } from '../services/ImageWebpService.js';
[6351545]29
30const router = express.Router();
31
[834bcc3]32// Hero dark overlay: percentage 0-100 (0 = no overlay, 100 = fully black).
33// Default 45 = the old hard-coded value, so existing hubs don't change appearance.
[63edb0d]34function clampOverlay(raw) {
35 const v = parseInt(raw, 10);
36 return Number.isFinite(v) ? Math.max(0, Math.min(100, v)) : 45;
37}
38
39const __dirname = path.dirname(fileURLToPath(import.meta.url));
[834bcc3]40// Hero uploads land in storage/media/hero → accessible as /media/hero/<file>
41// (the /media static handler serves storage/media). Same model as avatars.
[63edb0d]42const HERO_DIR = path.resolve(
43 process.env.HERO_PATH || path.join(__dirname, '..', '..', 'storage', 'media', 'hero')
44);
45fs.mkdirSync(HERO_DIR, { recursive: true });
46
[834bcc3]47// Only raster formats for upload. SVG is intentionally NOT allowed via upload
48// (raw SVG can contain scripts → stored-XSS when opened directly); an SVG hero
49// can still be set via the URL field (like the bundled demo placeholder).
[63edb0d]50const ALLOWED_HERO_EXT = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']);
51const MAX_HERO_BYTES = 5 * 1024 * 1024;
52
53const heroUpload = multer({
54 storage: multer.diskStorage({
55 destination: (req, file, cb) => cb(null, HERO_DIR),
56 filename: (req, file, cb) => {
57 const ext = path.extname(file.originalname).toLowerCase();
58 cb(null, `${uuid()}${ext}`);
59 },
60 }),
61 limits: { fileSize: MAX_HERO_BYTES },
62 fileFilter: (req, file, cb) => {
63 const ext = path.extname(file.originalname).toLowerCase();
64 if (!ALLOWED_HERO_EXT.has(ext)) {
65 return cb(new Error('Hero-afbeelding moet jpg/png/webp/gif zijn'));
66 }
67 cb(null, true);
68 },
69});
70
[6351545]71router.get('/', requireGod, (req, res) => {
72 renderPage(req, res, 'pages/admin-settings', {
73 pageTitle: 'Instellingen',
74 bodyClass: 'on-admin',
75 tenancy: getTenancy(),
[6e0249f]76 hubTitle: getSetting('hub_title') || '',
77 hubTagline: getSetting('hub_tagline') || '',
78 hubIntro: getSetting('hub_intro') || '',
[4c5169f]79 hubHeroImage: getSetting('hub_hero_image') || '',
[63edb0d]80 hubHeroOverlay: clampOverlay(getSetting('hub_hero_overlay')),
[5e61b17]81 defaultLang: getSetting('default_lang') || '',
[1b4d5dd]82 premium: entitlementStatus(),
[2248cd2]83 google: {
84 configured: googleConfigured(),
85 redirectUri: redirectUri(),
86 clientId: currentClientId(),
87 secretSet: clientSecretSet(),
88 },
[ca507ef]89 smtp: mailerStatus(),
[da6fd02]90 footerNewsletter: getSetting('footer_newsletter') === '1',
[6351545]91 success: req.query.success || null,
[63edb0d]92 error: req.query.error || null,
[6351545]93 });
94});
95
96router.post('/', requireGod, (req, res) => {
[834bcc3]97 // multer.single processes multipart (hub branding form). For a plain
98 // urlencoded POST (tenancy form) multer does nothing and req.body stays intact.
[63edb0d]99 heroUpload.single('hub_hero_file')(req, res, (err) => {
100 if (err) {
101 return res.redirect('/admin/settings?error=' + encodeURIComponent(err.message));
102 }
103
104 if (typeof req.body.tenancy !== 'undefined') {
[834bcc3]105 // Hub mode is a premium feature: only switch to hub if premium is
106 // unlocked (premium layer off = free; on = Patreon required). Staying on
107 // hub is always allowed, so an instance can never get stuck.
[029f047]108 if (req.body.tenancy === 'hub' && !premiumUnlocked() && getTenancy() !== 'hub') {
109 return res.redirect('/admin/settings?error=' + encodeURIComponent('Hub-modus is een premium-functie — koppel Patreon in Beheer → Instellingen.'));
110 }
[0b7ebbf]111 setTenancy(req.body.tenancy); // valideert naar solo | hub | circle
[63edb0d]112 }
[5e61b17]113 if (typeof req.body.default_lang !== 'undefined') {
[834bcc3]114 // Default language for visitors (empty = follow env/browser). Validated against NL/EN/DE.
[5e61b17]115 const dl = (req.body.default_lang || '').toString().toLowerCase();
116 setSetting('default_lang', SUPPORTED.includes(dl) ? dl : '');
117 }
[421c2d4]118 if (typeof req.body.timezone !== 'undefined') {
[834bcc3]119 // Site timezone (IANA, e.g. Europe/Amsterdam). Empty = server default (UTC).
120 // Validate with Intl so a nonsense value never breaks date rendering.
[421c2d4]121 const tz = (req.body.timezone || '').toString().trim();
122 let valid = '';
123 if (tz) { try { Intl.DateTimeFormat('en-US', { timeZone: tz }); valid = tz; } catch { valid = ''; } }
124 setSetting('timezone', valid);
125 }
[63edb0d]126 if (typeof req.body.hub_title !== 'undefined') {
127 setSetting('hub_title', (req.body.hub_title || '').toString().slice(0, 80).trim());
128 }
129 if (typeof req.body.hub_tagline !== 'undefined') {
130 setSetting('hub_tagline', (req.body.hub_tagline || '').toString().slice(0, 120).trim());
131 }
132 if (typeof req.body.hub_intro !== 'undefined') {
133 setSetting('hub_intro', (req.body.hub_intro || '').toString().slice(0, 400).trim());
134 }
135
[834bcc3]136 // Hero: an uploaded image wins; otherwise the URL text field.
[63edb0d]137 if (req.file) {
[8f6225c]138 const newUrl = `/media/hero/${toWebp(req.file)}`;
[834bcc3]139 // Clean up a previously uploaded hero (only if it came from our hero dir).
[63edb0d]140 const old = getSetting('hub_hero_image') || '';
141 if (old.startsWith('/media/hero/')) {
142 try { fs.unlinkSync(path.join(HERO_DIR, path.basename(old))); } catch {}
143 }
144 setSetting('hub_hero_image', newUrl);
145 } else if (typeof req.body.hub_hero_image !== 'undefined') {
146 setSetting('hub_hero_image', (req.body.hub_hero_image || '').toString().slice(0, 300).trim());
147 }
148
149 if (typeof req.body.hub_hero_overlay !== 'undefined') {
150 setSetting('hub_hero_overlay', String(clampOverlay(req.body.hub_hero_overlay)));
151 }
152
153 res.redirect('/admin/settings?success=' + encodeURIComponent('Opgeslagen'));
154 });
[09ecc5f]155});
156
[834bcc3]157// Google login on its own admin page (separate from the general settings).
[09ecc5f]158router.get('/google', requireGod, (req, res) => {
159 renderPage(req, res, 'pages/admin-google', {
160 pageTitle: 'Google-login',
161 bodyClass: 'on-admin',
162 google: {
163 configured: googleConfigured(),
164 redirectUri: redirectUri(),
165 clientId: currentClientId(),
166 secretSet: clientSecretSet(),
167 },
168 success: req.query.success || null,
169 error: req.query.error || null,
170 });
[6351545]171});
172
[834bcc3]173// Configure Google login (listeners) — Client ID + Secret in app_settings.
174// The redirect URI is derived from PUBLIC_BASE_URL (see config/google.js).
[2248cd2]175router.post('/google', requireGod, (req, res) => {
176 if (req.body.clear === '1') {
177 setSetting('google_client_id', '');
178 setSetting('google_client_secret', '');
179 return res.redirect('/admin/settings?success=' + encodeURIComponent('Google-login losgekoppeld'));
180 }
181 setSetting('google_client_id', (req.body.google_client_id || '').toString().trim());
[834bcc3]182 // Only overwrite the secret if a new value was entered (empty = leave as-is).
[2248cd2]183 const secret = (req.body.google_client_secret || '').toString().trim();
184 if (secret) setSetting('google_client_secret', secret);
185 res.redirect('/admin/settings?success=' + encodeURIComponent('Google-login opgeslagen'));
186});
187
[ca507ef]188// ── SMTP / e-mail-instellingen ────────────────────────────────────
189router.post('/smtp', requireGod, (req, res) => {
190 const b = req.body || {};
191 if (b.clear === '1') {
192 ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from'].forEach((k) => setSetting(k, ''));
193 return res.redirect('/admin/settings?success=' + encodeURIComponent('SMTP-instellingen gewist'));
194 }
195 setSetting('smtp_host', (b.smtp_host || '').toString().trim());
196 setSetting('smtp_port', (b.smtp_port || '').toString().trim());
197 setSetting('smtp_user', (b.smtp_user || '').toString().trim());
198 setSetting('smtp_from', (b.smtp_from || '').toString().trim());
[834bcc3]199 // Only overwrite the password if a new value was entered.
[ca507ef]200 const pass = (b.smtp_pass || '').toString();
201 if (pass) setSetting('smtp_pass', pass);
202 res.redirect('/admin/settings?success=' + encodeURIComponent('SMTP-instellingen opgeslagen'));
203});
204
[834bcc3]205// Newsletter sign-up in the footer on/off.
[da6fd02]206router.post('/footer', requireGod, (req, res) => {
207 setSetting('footer_newsletter', req.body.footer_newsletter ? '1' : '0');
208 res.redirect('/admin/settings?success=' + encodeURIComponent('Footer-instelling opgeslagen'));
209});
210
[834bcc3]211// Send a test email to a specified address (or the logged-in user).
[ca507ef]212router.post('/smtp/test', requireGod, async (req, res) => {
213 const to = ((req.body && req.body.to) || (req.session.user && req.session.user.email) || '').toString().trim();
214 if (!to || to.indexOf('@') === -1) {
215 return res.redirect('/admin/settings?error=' + encodeURIComponent('Geef een geldig test-e-mailadres op.'));
216 }
217 try {
218 await sendMail({
219 to,
220 subject: 'Klonkt — SMTP-test',
221 text: 'Gelukt! Je SMTP-instellingen werken. Dit is een testbericht van je Klonkt-site.',
222 html: '<p>Gelukt! Je <strong>SMTP-instellingen werken</strong>. Dit is een testbericht van je Klonkt-site.</p>',
223 });
224 res.redirect('/admin/settings?success=' + encodeURIComponent('Testmail verstuurd naar ' + to));
225 } catch (e) {
226 res.redirect('/admin/settings?error=' + encodeURIComponent('Testmail mislukt: ' + (e.message || e)));
227 }
228});
229
[6351545]230export default router;
Note: See TracBrowser for help on using the repository browser.