2019-07-24 21:26:56 +02:00
|
|
|
const path = require('path')
|
2021-09-30 11:13:44 +02:00
|
|
|
const URL = require('url')
|
2019-07-24 21:26:56 +02:00
|
|
|
const fs = require('fs')
|
2019-12-04 00:50:15 +01:00
|
|
|
const crypto = require('crypto')
|
2021-09-30 11:13:44 +02:00
|
|
|
const { promisify } = require('util')
|
2020-07-07 21:58:52 +02:00
|
|
|
const sharp = require('sharp')
|
2021-09-30 11:13:44 +02:00
|
|
|
const config = require('../../config')
|
|
|
|
const generateKeyPair = promisify(crypto.generateKeyPair)
|
2021-03-05 14:17:10 +01:00
|
|
|
const log = require('../../log')
|
2022-11-29 14:40:19 +01:00
|
|
|
// const locales = require('../../../locales/index')
|
2022-02-07 12:28:38 +01:00
|
|
|
const escape = require('lodash/escape')
|
2022-08-09 18:32:47 +02:00
|
|
|
const pluginController = require('./plugins')
|
2021-09-30 11:13:44 +02:00
|
|
|
|
|
|
|
let defaultHostname
|
|
|
|
try {
|
|
|
|
defaultHostname = new URL.URL(config.baseurl).hostname
|
|
|
|
} catch (e) {}
|
|
|
|
|
2019-12-04 00:50:15 +01:00
|
|
|
const defaultSettings = {
|
2021-10-21 12:17:41 +02:00
|
|
|
title: config.title || 'Gancio',
|
|
|
|
description: config.description || 'A shared agenda for local communities',
|
2021-09-30 11:13:44 +02:00
|
|
|
baseurl: config.baseurl || '',
|
|
|
|
hostname: defaultHostname,
|
2019-12-04 00:50:15 +01:00
|
|
|
instance_timezone: 'Europe/Rome',
|
2020-02-20 18:37:10 +01:00
|
|
|
instance_locale: 'en',
|
2021-09-30 11:13:44 +02:00
|
|
|
instance_name: 'gancio',
|
2020-03-18 10:11:24 +01:00
|
|
|
instance_place: '',
|
2019-12-04 00:50:15 +01:00
|
|
|
allow_registration: true,
|
|
|
|
allow_anon_event: true,
|
2022-12-05 23:03:10 +01:00
|
|
|
allow_multidate_event: true,
|
2019-12-04 00:50:15 +01:00
|
|
|
allow_recurrent_event: false,
|
|
|
|
recurrent_event_visible: false,
|
2022-11-15 17:37:13 +01:00
|
|
|
allow_geolocation: true,
|
2022-12-06 00:03:53 +01:00
|
|
|
geocoding_provider_type: 'Nominatim',
|
2022-12-06 00:20:42 +01:00
|
|
|
geocoding_provider: 'https://nominatim.openstreetmap.org/search',
|
2022-12-02 11:25:43 +01:00
|
|
|
geocoding_countrycodes: [],
|
|
|
|
tilelayer_provider: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
|
|
tilelayer_provider_attribution: "<a target=\"_blank\" href=\"http://osm.org/copyright\">OpenStreetMap</a> contributors",
|
2019-12-04 00:50:15 +01:00
|
|
|
enable_federation: true,
|
|
|
|
enable_resources: false,
|
2020-03-14 18:45:20 +01:00
|
|
|
hide_boosts: true,
|
2020-03-18 10:11:24 +01:00
|
|
|
enable_trusted_instances: true,
|
2020-08-05 17:08:01 +02:00
|
|
|
trusted_instances: [],
|
2021-10-21 16:18:41 +02:00
|
|
|
'theme.is_dark': true,
|
2020-08-05 17:08:01 +02:00
|
|
|
'theme.primary': '#FF4500',
|
2022-11-21 00:52:02 +01:00
|
|
|
trusted_instances_label: '',
|
2022-11-09 10:17:52 +01:00
|
|
|
hide_thumbs: false,
|
|
|
|
hide_calendar: false,
|
2020-08-05 17:08:01 +02:00
|
|
|
footerLinks: [
|
2022-11-25 09:47:46 +01:00
|
|
|
{ href: '/', label: 'common.home' },
|
|
|
|
{ href: '/about', label: 'common.about' }
|
2021-09-30 11:13:44 +02:00
|
|
|
],
|
2022-07-15 20:42:27 +02:00
|
|
|
plugins: [],
|
2021-10-18 15:46:38 +02:00
|
|
|
admin_email: config.admin_email || '',
|
2022-05-09 20:45:54 +02:00
|
|
|
smtp: config.smtp || {}
|
2019-12-04 00:50:15 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:34:14 +02:00
|
|
|
/**
|
|
|
|
* Settings controller: store instance settings
|
|
|
|
*/
|
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
const settingsController = {
|
2019-06-25 01:05:38 +02:00
|
|
|
settings: { initialized: false },
|
2019-07-26 23:51:32 +02:00
|
|
|
user_locale: {},
|
2019-06-25 01:05:38 +02:00
|
|
|
secretSettings: {},
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2019-12-04 00:50:15 +01:00
|
|
|
async load () {
|
2022-08-02 16:10:15 +02:00
|
|
|
if (config.status !== 'CONFIGURED') {
|
2021-09-27 10:42:17 +02:00
|
|
|
settingsController.settings = defaultSettings
|
|
|
|
return
|
|
|
|
}
|
2021-11-09 12:49:20 +01:00
|
|
|
if (settingsController.settings.initialized) return
|
|
|
|
settingsController.settings.initialized = true
|
|
|
|
// initialize instance settings from db
|
|
|
|
// note that this is done only once when the server starts
|
|
|
|
// and not for each request
|
2021-09-27 10:42:17 +02:00
|
|
|
const Setting = require('../models/setting')
|
2021-11-09 12:49:20 +01:00
|
|
|
const settings = await Setting.findAll()
|
|
|
|
settingsController.settings = defaultSettings
|
|
|
|
settings.forEach(s => {
|
|
|
|
if (s.is_secret) {
|
|
|
|
settingsController.secretSettings[s.key] = s.value
|
|
|
|
} else {
|
|
|
|
settingsController.settings[s.key] = s.value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// add pub/priv instance key if needed
|
|
|
|
if (!settingsController.settings.publicKey) {
|
|
|
|
log.info('Instance priv/pub key not found, generating....')
|
|
|
|
const { publicKey, privateKey } = await generateKeyPair('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem'
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem'
|
2019-06-25 01:05:38 +02:00
|
|
|
}
|
|
|
|
})
|
2019-07-26 23:51:32 +02:00
|
|
|
|
2021-11-09 12:49:20 +01:00
|
|
|
await settingsController.set('publicKey', publicKey)
|
|
|
|
await settingsController.set('privateKey', privateKey, true)
|
|
|
|
}
|
2019-09-26 22:46:04 +02:00
|
|
|
|
2021-11-09 12:49:20 +01:00
|
|
|
// initialize user_locale
|
2022-11-29 14:40:19 +01:00
|
|
|
// if (config.user_locale && fs.existsSync(path.resolve(config.user_locale))) {
|
|
|
|
// const user_locales_files = fs.readdirSync(path.resolve(config.user_locale))
|
|
|
|
// user_locales_files.forEach( f => {
|
|
|
|
// const locale = path.basename(f ,'.json')
|
|
|
|
// if (locales[locale]) {
|
|
|
|
// log.info(`Adding custom locale ${locale}`)
|
|
|
|
// settingsController.user_locale[locale] = require(path.resolve(config.user_locale, f)).default
|
|
|
|
// } else {
|
|
|
|
// log.warning(`Unknown custom user locale: ${locale} [valid locales are ${locales}]`)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
2021-12-02 12:39:55 +01:00
|
|
|
|
2022-08-09 18:32:47 +02:00
|
|
|
pluginController._load()
|
2019-06-21 23:52:18 +02:00
|
|
|
},
|
|
|
|
|
2019-11-06 11:31:56 +01:00
|
|
|
async set (key, value, is_secret = false) {
|
2021-09-27 10:42:17 +02:00
|
|
|
const Setting = require('../models/setting')
|
2021-05-20 11:19:26 +02:00
|
|
|
log.info(`SET ${key} ${is_secret ? '*****' : value}`)
|
2019-06-21 23:52:18 +02:00
|
|
|
try {
|
2020-07-05 23:50:10 +02:00
|
|
|
const [setting, created] = await Setting.findOrCreate({
|
2019-06-21 23:52:18 +02:00
|
|
|
where: { key },
|
|
|
|
defaults: { value, is_secret }
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
2020-07-06 00:52:32 +02:00
|
|
|
if (!created) { setting.update({ value, is_secret }) }
|
2019-11-06 11:31:56 +01:00
|
|
|
settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value
|
2019-06-21 23:52:18 +02:00
|
|
|
return true
|
2019-11-06 11:31:56 +01:00
|
|
|
} catch (e) {
|
2021-07-08 20:41:56 +02:00
|
|
|
log.error('[SETTING SET]', e)
|
2019-06-21 23:52:18 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-11-06 11:31:56 +01:00
|
|
|
async setRequest (req, res) {
|
2019-06-21 23:52:18 +02:00
|
|
|
const { key, value, is_secret } = req.body
|
|
|
|
const ret = await settingsController.set(key, value, is_secret)
|
2019-11-06 11:31:56 +01:00
|
|
|
if (ret) { res.sendStatus(200) } else { res.sendStatus(400) }
|
2019-06-21 23:52:18 +02:00
|
|
|
},
|
|
|
|
|
2021-10-18 15:46:38 +02:00
|
|
|
async testSMTP (req, res) {
|
|
|
|
const smtp = req.body
|
|
|
|
await settingsController.set('smtp', smtp.smtp)
|
|
|
|
const mail = require('../mail')
|
|
|
|
try {
|
2022-02-07 12:28:38 +01:00
|
|
|
await mail._send(settingsController.settings.admin_email, 'test')
|
|
|
|
|
2021-10-18 15:46:38 +02:00
|
|
|
return res.sendStatus(200)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2022-02-07 12:28:38 +01:00
|
|
|
return res.status(400).send(escape(String(e)))
|
2021-10-18 15:46:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-07-18 10:05:59 +02:00
|
|
|
getSMTPSettings (_req, res) {
|
|
|
|
return res.json(settingsController['settings']['smtp'])
|
|
|
|
},
|
|
|
|
|
2022-07-29 11:30:15 +02:00
|
|
|
getAll (_req, res) {
|
|
|
|
return res.json(settingsController.settings)
|
|
|
|
},
|
|
|
|
|
2020-07-07 21:58:52 +02:00
|
|
|
setLogo (req, res) {
|
2020-02-01 22:39:31 +01:00
|
|
|
if (!req.file) {
|
2021-04-28 12:44:26 +02:00
|
|
|
settingsController.set('logo', false)
|
|
|
|
return res.status(200)
|
2020-02-01 22:39:31 +01:00
|
|
|
}
|
2020-07-05 23:53:37 +02:00
|
|
|
|
2020-07-28 12:24:39 +02:00
|
|
|
const uploadedPath = path.join(req.file.destination, req.file.filename)
|
|
|
|
const baseImgPath = path.resolve(config.upload_path, 'logo')
|
2020-07-07 21:58:52 +02:00
|
|
|
|
|
|
|
// convert and resize to png
|
2021-07-28 10:02:04 +02:00
|
|
|
return sharp(uploadedPath)
|
2020-07-07 21:58:52 +02:00
|
|
|
.resize(400)
|
|
|
|
.png({ quality: 90 })
|
2022-05-09 20:45:54 +02:00
|
|
|
.toFile(baseImgPath + '.png', (err) => {
|
2021-03-05 14:17:10 +01:00
|
|
|
if (err) {
|
2021-07-28 10:02:04 +02:00
|
|
|
log.error('[LOGO] ' + err)
|
2021-03-05 14:17:10 +01:00
|
|
|
}
|
2020-07-28 12:24:39 +02:00
|
|
|
settingsController.set('logo', baseImgPath)
|
2020-07-07 21:58:52 +02:00
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
2022-10-28 12:01:59 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setFallbackImage (req, res) {
|
|
|
|
if (!req.file) {
|
2022-11-09 10:17:52 +01:00
|
|
|
settingsController.set('fallback_image', false)
|
2022-10-28 12:01:59 +02:00
|
|
|
return res.status(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadedPath = path.join(req.file.destination, req.file.filename)
|
|
|
|
const baseImgPath = path.resolve(config.upload_path, 'fallbackImage.png')
|
|
|
|
|
|
|
|
// convert and resize to png
|
|
|
|
return sharp(uploadedPath)
|
|
|
|
.resize(600)
|
|
|
|
.png({ quality: 99 })
|
|
|
|
.toFile(baseImgPath, (err) => {
|
|
|
|
if (err) {
|
|
|
|
log.error('[FALLBACK IMAGE] ' + err)
|
|
|
|
}
|
2022-11-09 10:17:52 +01:00
|
|
|
settingsController.set('fallback_image', baseImgPath)
|
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
setHeaderImage (req, res) {
|
|
|
|
if (!req.file) {
|
|
|
|
settingsController.set('header_image', false)
|
|
|
|
return res.status(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadedPath = path.join(req.file.destination, req.file.filename)
|
|
|
|
const baseImgPath = path.resolve(config.upload_path, 'fallbackImage.png')
|
|
|
|
|
|
|
|
// convert and resize to png
|
|
|
|
return sharp(uploadedPath)
|
|
|
|
.resize(600)
|
|
|
|
.png({ quality: 99 })
|
|
|
|
.toFile(baseImgPath, (err) => {
|
|
|
|
if (err) {
|
|
|
|
log.error('[HEADER IMAGE] ' + err)
|
|
|
|
}
|
|
|
|
settingsController.set('header_image', baseImgPath)
|
2022-10-28 12:01:59 +02:00
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
2019-11-06 11:31:56 +01:00
|
|
|
}
|
2022-10-28 12:01:59 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = settingsController
|