2017-02-09 01:52:37 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2018-08-19 05:22:59 +00:00
|
|
|
const pageHelper = require('../helpers/page')
|
2018-12-24 22:38:34 +00:00
|
|
|
const _ = require('lodash')
|
2020-06-20 20:39:36 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
2020-06-21 02:08:59 +00:00
|
|
|
const moment = require('moment')
|
2018-08-19 05:22:59 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
2017-07-06 04:10:41 +00:00
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
const tmplCreateRegex = /^[0-9]+(,[0-9]+)?$/
|
|
|
|
|
2019-01-07 03:03:34 +00:00
|
|
|
/**
|
|
|
|
* Robots.txt
|
|
|
|
*/
|
|
|
|
router.get('/robots.txt', (req, res, next) => {
|
|
|
|
res.type('text/plain')
|
|
|
|
if (_.includes(WIKI.config.seo.robots, 'noindex')) {
|
2019-03-19 19:15:40 +00:00
|
|
|
res.send('User-agent: *\nDisallow: /')
|
2019-01-07 03:03:34 +00:00
|
|
|
} else {
|
|
|
|
res.status(200).end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-12 17:15:23 +00:00
|
|
|
/**
|
|
|
|
* Health Endpoint
|
|
|
|
*/
|
|
|
|
router.get('/healthz', (req, res, next) => {
|
|
|
|
if (WIKI.models.knex.client.pool.numFree() < 1 && WIKI.models.knex.client.pool.numUsed() < 1) {
|
|
|
|
res.status(503).json({ ok: false }).end()
|
|
|
|
} else {
|
|
|
|
res.status(200).json({ ok: true }).end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
/**
|
|
|
|
* Administration
|
|
|
|
*/
|
|
|
|
router.get(['/a', '/a/*'], (req, res, next) => {
|
2020-07-19 19:26:51 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, [
|
|
|
|
'manage:system',
|
|
|
|
'write:users',
|
|
|
|
'manage:users',
|
|
|
|
'write:groups',
|
|
|
|
'manage:groups',
|
|
|
|
'manage:navigation',
|
|
|
|
'manage:theme',
|
|
|
|
'manage:api'
|
|
|
|
])) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
2020-07-19 19:30:29 +00:00
|
|
|
return res.status(403).render('unauthorized', { action: 'view' })
|
2020-07-19 19:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Admin')
|
|
|
|
res.render('admin')
|
|
|
|
})
|
|
|
|
|
2020-02-25 02:10:43 +00:00
|
|
|
/**
|
|
|
|
* Download Page / Version
|
|
|
|
*/
|
|
|
|
router.get(['/d', '/d/*'], async (req, res, next) => {
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
|
2020-09-08 00:02:33 +00:00
|
|
|
|
2020-02-25 02:10:43 +00:00
|
|
|
const versionId = (req.query.v) ? _.toSafeInteger(req.query.v) : 0
|
|
|
|
|
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
|
|
|
|
pageArgs.tags = _.get(page, 'tags', [])
|
|
|
|
|
|
|
|
if (versionId > 0) {
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:history'], pageArgs)) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'downloadVersion' })
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:source'], pageArgs)) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'download' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
const fileName = _.last(page.path.split('/')) + '.' + pageHelper.getFileExtension(page.contentType)
|
|
|
|
res.attachment(fileName)
|
|
|
|
if (versionId > 0) {
|
|
|
|
const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: page.id, versionId })
|
|
|
|
res.send(pageHelper.injectPageMetadata(pageVersion))
|
|
|
|
} else {
|
|
|
|
res.send(pageHelper.injectPageMetadata(page))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.status(404).end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-02-11 05:20:17 +00:00
|
|
|
/**
|
|
|
|
* Create/Edit document
|
|
|
|
*/
|
2018-09-16 22:36:15 +00:00
|
|
|
router.get(['/e', '/e/*'], async (req, res, next) => {
|
2019-05-14 04:53:19 +00:00
|
|
|
const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
|
2019-04-20 00:45:05 +00:00
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
|
|
|
|
return res.redirect(`/e/${pageArgs.locale}/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:54:31 +00:00
|
|
|
req.i18n.changeLanguage(pageArgs.locale)
|
2020-09-08 00:02:33 +00:00
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> Set Editor Lang
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
2020-02-01 03:34:38 +00:00
|
|
|
_.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
|
2019-06-22 16:44:32 +00:00
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> Check for reserved path
|
2019-04-20 00:45:05 +00:00
|
|
|
if (pageHelper.isReservedPath(pageArgs.path)) {
|
|
|
|
return next(new Error('Cannot create this page because it starts with a system reserved path.'))
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> Get page data from DB
|
2018-09-16 22:36:15 +00:00
|
|
|
let page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
2019-07-14 20:12:26 +00:00
|
|
|
|
2020-01-31 21:57:35 +00:00
|
|
|
pageArgs.tags = _.get(page, 'tags', [])
|
|
|
|
|
2020-06-21 02:08:59 +00:00
|
|
|
// -> Effective Permissions
|
|
|
|
const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
|
|
|
|
|
2019-07-14 20:12:26 +00:00
|
|
|
const injectCode = {
|
|
|
|
css: WIKI.config.theming.injectCSS,
|
|
|
|
head: WIKI.config.theming.injectHead,
|
|
|
|
body: WIKI.config.theming.injectBody
|
|
|
|
}
|
|
|
|
|
2018-09-16 22:36:15 +00:00
|
|
|
if (page) {
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> EDIT MODE
|
2020-06-21 02:08:59 +00:00
|
|
|
if (!(effectivePermissions.pages.write || effectivePermissions.pages.manage)) {
|
2019-01-25 22:19:52 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
2019-03-19 19:15:40 +00:00
|
|
|
return res.render('unauthorized', { action: 'edit' })
|
2019-01-07 03:03:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> Get page tags
|
2019-09-01 22:38:09 +00:00
|
|
|
await page.$relatedQuery('tags')
|
|
|
|
page.tags = _.map(page.tags, 'tag')
|
|
|
|
|
2020-09-09 23:35:43 +00:00
|
|
|
// Handle missing extra field
|
|
|
|
page.extra = page.extra || { css: '', js: '' }
|
|
|
|
|
2020-06-20 20:39:36 +00:00
|
|
|
// -> Beautify Script CSS
|
|
|
|
if (!_.isEmpty(page.extra.css)) {
|
|
|
|
page.extra.css = new CleanCSS({ format: 'beautify' }).minify(page.extra.css).styles
|
|
|
|
}
|
|
|
|
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2018-09-16 22:36:15 +00:00
|
|
|
page.mode = 'update'
|
|
|
|
page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
|
|
|
|
page.content = Buffer.from(page.content).toString('base64')
|
|
|
|
} else {
|
2020-03-01 23:22:26 +00:00
|
|
|
// -> CREATE MODE
|
2020-06-21 02:08:59 +00:00
|
|
|
if (!effectivePermissions.pages.write) {
|
2019-01-25 22:19:52 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
2019-03-19 19:15:40 +00:00
|
|
|
return res.render('unauthorized', { action: 'create' })
|
2019-01-07 03:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', `New Page`)
|
2018-09-16 22:36:15 +00:00
|
|
|
page = {
|
|
|
|
path: pageArgs.path,
|
|
|
|
localeCode: pageArgs.locale,
|
|
|
|
editorKey: null,
|
|
|
|
mode: 'create',
|
2020-03-01 23:22:26 +00:00
|
|
|
content: null,
|
|
|
|
title: null,
|
2020-03-02 05:43:19 +00:00
|
|
|
description: null,
|
2020-06-21 18:47:11 +00:00
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
extra: {
|
|
|
|
css: '',
|
|
|
|
js: ''
|
|
|
|
}
|
2020-03-01 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -> From Template
|
|
|
|
if (req.query.from && tmplCreateRegex.test(req.query.from)) {
|
|
|
|
let tmplPageId = 0
|
|
|
|
let tmplVersionId = 0
|
|
|
|
if (req.query.from.indexOf(',')) {
|
|
|
|
const q = req.query.from.split(',')
|
|
|
|
tmplPageId = _.toSafeInteger(q[0])
|
|
|
|
tmplVersionId = _.toSafeInteger(q[1])
|
|
|
|
} else {
|
|
|
|
tmplPageId = _.toSafeInteger(req.query.from)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmplVersionId > 0) {
|
|
|
|
// -> From Page Version
|
|
|
|
const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: tmplPageId, versionId: tmplVersionId })
|
|
|
|
if (!pageVersion) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
|
|
|
return res.status(404).render('notfound', { action: 'template' })
|
|
|
|
}
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:history'], { path: pageVersion.path, locale: pageVersion.locale })) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'sourceVersion' })
|
|
|
|
}
|
|
|
|
page.content = Buffer.from(pageVersion.content).toString('base64')
|
|
|
|
page.editorKey = pageVersion.editor
|
|
|
|
page.title = pageVersion.title
|
|
|
|
page.description = pageVersion.description
|
|
|
|
} else {
|
|
|
|
// -> From Page Live
|
|
|
|
const pageOriginal = await WIKI.models.pages.query().findById(tmplPageId)
|
|
|
|
if (!pageOriginal) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
|
|
|
return res.status(404).render('notfound', { action: 'template' })
|
|
|
|
}
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:source'], { path: pageOriginal.path, locale: pageOriginal.locale })) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'source' })
|
|
|
|
}
|
|
|
|
page.content = Buffer.from(pageOriginal.content).toString('base64')
|
|
|
|
page.editorKey = pageOriginal.editorKey
|
|
|
|
page.title = pageOriginal.title
|
|
|
|
page.description = pageOriginal.description
|
|
|
|
}
|
2018-09-16 22:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 22:54:05 +00:00
|
|
|
|
|
|
|
res.render('editor', { page, injectCode, effectivePermissions })
|
2018-02-11 05:20:17 +00:00
|
|
|
})
|
|
|
|
|
2018-10-29 02:09:58 +00:00
|
|
|
/**
|
2018-11-25 06:28:20 +00:00
|
|
|
* History
|
2018-10-29 02:09:58 +00:00
|
|
|
*/
|
|
|
|
router.get(['/h', '/h/*'], async (req, res, next) => {
|
2019-05-14 04:53:19 +00:00
|
|
|
const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
|
2019-01-07 03:03:34 +00:00
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
|
|
|
|
return res.redirect(`/h/${pageArgs.locale}/${pageArgs.path}`)
|
|
|
|
}
|
2020-09-08 00:02:33 +00:00
|
|
|
|
2020-07-29 23:54:31 +00:00
|
|
|
req.i18n.changeLanguage(pageArgs.locale)
|
2019-07-02 05:48:19 +00:00
|
|
|
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
2020-02-01 03:34:38 +00:00
|
|
|
_.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
|
2019-06-22 16:44:32 +00:00
|
|
|
|
2018-11-25 06:28:20 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
2018-10-29 02:09:58 +00:00
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
2020-01-31 21:57:35 +00:00
|
|
|
|
2020-03-01 23:22:26 +00:00
|
|
|
if (!page) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
|
|
|
return res.status(404).render('notfound', { action: 'history' })
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:57:35 +00:00
|
|
|
pageArgs.tags = _.get(page, 'tags', [])
|
|
|
|
|
2020-06-21 02:08:59 +00:00
|
|
|
const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
|
|
|
|
|
|
|
|
if (!effectivePermissions.history.read) {
|
2020-01-31 21:57:35 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'history' })
|
|
|
|
}
|
|
|
|
|
2018-10-29 02:09:58 +00:00
|
|
|
if (page) {
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2020-06-19 22:54:05 +00:00
|
|
|
|
|
|
|
res.render('history', { page, effectivePermissions })
|
2018-10-29 02:09:58 +00:00
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-15 03:21:24 +00:00
|
|
|
/**
|
|
|
|
* Page ID redirection
|
|
|
|
*/
|
|
|
|
router.get(['/i', '/i/:id'], async (req, res, next) => {
|
|
|
|
const pageId = _.toSafeInteger(req.params.id)
|
|
|
|
if (pageId <= 0) {
|
|
|
|
return res.redirect('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
const page = await WIKI.models.pages.query().column(['path', 'localeCode', 'isPrivate', 'privateNS']).findById(pageId)
|
|
|
|
if (!page) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
|
|
|
return res.status(404).render('notfound', { action: 'view' })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:pages'], {
|
|
|
|
locale: page.localeCode,
|
|
|
|
path: page.path,
|
|
|
|
private: page.isPrivate,
|
|
|
|
privateNS: page.privateNS,
|
2020-01-31 21:57:35 +00:00
|
|
|
explicitLocale: false,
|
|
|
|
tags: page.tags
|
2020-01-15 03:21:24 +00:00
|
|
|
})) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'view' })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIKI.config.lang.namespacing) {
|
|
|
|
return res.redirect(`/${page.localeCode}/${page.path}`)
|
|
|
|
} else {
|
|
|
|
return res.redirect(`/${page.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
/**
|
|
|
|
* Profile
|
|
|
|
*/
|
|
|
|
router.get(['/p', '/p/*'], (req, res, next) => {
|
2020-04-05 22:51:48 +00:00
|
|
|
if (!req.user || req.user.id < 1 || req.user.id === 2) {
|
|
|
|
return res.render('unauthorized', { action: 'view' })
|
|
|
|
}
|
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'User Profile')
|
|
|
|
res.render('profile')
|
|
|
|
})
|
|
|
|
|
2018-11-25 06:28:20 +00:00
|
|
|
/**
|
|
|
|
* Source
|
|
|
|
*/
|
|
|
|
router.get(['/s', '/s/*'], async (req, res, next) => {
|
2019-05-14 04:53:19 +00:00
|
|
|
const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
|
2020-02-29 23:57:54 +00:00
|
|
|
const versionId = (req.query.v) ? _.toSafeInteger(req.query.v) : 0
|
|
|
|
|
2020-01-31 21:57:35 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
|
|
|
|
pageArgs.tags = _.get(page, 'tags', [])
|
2019-01-07 03:03:34 +00:00
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
|
|
|
|
return res.redirect(`/s/${pageArgs.locale}/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
|
2020-06-21 02:08:59 +00:00
|
|
|
// -> Effective Permissions
|
|
|
|
const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
|
|
|
|
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
2020-02-01 03:34:38 +00:00
|
|
|
_.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
|
2019-06-22 16:44:32 +00:00
|
|
|
|
2020-02-29 23:57:54 +00:00
|
|
|
if (versionId > 0) {
|
2020-06-21 02:08:59 +00:00
|
|
|
if (!effectivePermissions.history.read) {
|
2020-02-29 23:57:54 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'sourceVersion' })
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-21 02:08:59 +00:00
|
|
|
if (!effectivePermissions.source.read) {
|
2020-02-29 23:57:54 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.render('unauthorized', { action: 'source' })
|
|
|
|
}
|
2019-01-07 03:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 06:28:20 +00:00
|
|
|
if (page) {
|
2020-02-29 23:57:54 +00:00
|
|
|
if (versionId > 0) {
|
|
|
|
const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: page.id, versionId })
|
|
|
|
_.set(res.locals, 'pageMeta.title', pageVersion.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', pageVersion.description)
|
|
|
|
res.render('source', {
|
|
|
|
page: {
|
|
|
|
...page,
|
|
|
|
...pageVersion
|
2022-01-10 22:16:14 +00:00
|
|
|
},
|
|
|
|
effectivePermissions
|
2020-02-29 23:57:54 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2020-06-19 22:54:05 +00:00
|
|
|
|
|
|
|
res.render('source', { page, effectivePermissions })
|
2020-02-29 23:57:54 +00:00
|
|
|
}
|
2018-11-25 06:28:20 +00:00
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-01 22:33:36 +00:00
|
|
|
/**
|
|
|
|
* Tags
|
|
|
|
*/
|
|
|
|
router.get(['/t', '/t/*'], (req, res, next) => {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Tags')
|
|
|
|
res.render('tags')
|
|
|
|
})
|
|
|
|
|
2020-09-08 00:02:33 +00:00
|
|
|
/**
|
|
|
|
* User Avatar
|
|
|
|
*/
|
|
|
|
router.get('/_userav/:uid', async (req, res, next) => {
|
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:pages'])) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
const av = await WIKI.models.users.getUserAvatarData(req.params.uid)
|
|
|
|
if (av) {
|
|
|
|
res.set('Content-Type', 'image/jpeg')
|
|
|
|
res.send(av)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendStatus(404)
|
|
|
|
})
|
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
/**
|
2019-05-14 04:53:19 +00:00
|
|
|
* View document / asset
|
2016-08-17 03:56:08 +00:00
|
|
|
*/
|
2018-08-19 05:22:59 +00:00
|
|
|
router.get('/*', async (req, res, next) => {
|
2019-05-14 04:53:19 +00:00
|
|
|
const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`))
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path, { stripExt })
|
|
|
|
const isPage = (stripExt || pageArgs.path.indexOf('.') === -1)
|
2019-01-07 03:03:34 +00:00
|
|
|
|
2019-05-14 04:53:19 +00:00
|
|
|
if (isPage) {
|
2019-07-02 05:48:19 +00:00
|
|
|
if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
|
|
|
|
return res.redirect(`/${pageArgs.locale}/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
|
2019-07-14 00:27:31 +00:00
|
|
|
req.i18n.changeLanguage(pageArgs.locale)
|
|
|
|
|
2019-06-26 02:48:23 +00:00
|
|
|
try {
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Get Page from cache
|
2019-06-26 02:48:23 +00:00
|
|
|
const page = await WIKI.models.pages.getPage({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
2020-01-31 21:57:35 +00:00
|
|
|
pageArgs.tags = _.get(page, 'tags', [])
|
|
|
|
|
2020-06-21 02:08:59 +00:00
|
|
|
// -> Effective Permissions
|
|
|
|
const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Check User Access
|
2020-06-21 02:08:59 +00:00
|
|
|
if (!effectivePermissions.pages.read) {
|
2020-02-07 19:51:11 +00:00
|
|
|
if (req.user.id === 2) {
|
|
|
|
res.cookie('loginRedirect', req.path, {
|
|
|
|
maxAge: 15 * 60 * 1000
|
|
|
|
})
|
|
|
|
}
|
2020-02-07 19:32:45 +00:00
|
|
|
if (pageArgs.path === 'home' && req.user.id === 2) {
|
2020-01-31 21:57:35 +00:00
|
|
|
return res.redirect('/login')
|
|
|
|
}
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.status(403).render('unauthorized', {
|
|
|
|
action: 'view'
|
|
|
|
})
|
|
|
|
}
|
2019-06-26 02:48:23 +00:00
|
|
|
|
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
2020-02-01 03:34:38 +00:00
|
|
|
_.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
|
2019-06-26 02:48:23 +00:00
|
|
|
|
|
|
|
if (page) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2020-05-08 22:48:07 +00:00
|
|
|
|
2020-06-21 02:08:59 +00:00
|
|
|
// -> Check Publishing State
|
|
|
|
let pageIsPublished = page.isPublished
|
|
|
|
if (pageIsPublished && !_.isEmpty(page.publishStartDate)) {
|
|
|
|
pageIsPublished = moment(page.publishStartDate).isSameOrBefore()
|
|
|
|
}
|
|
|
|
if (pageIsPublished && !_.isEmpty(page.publishEndDate)) {
|
|
|
|
pageIsPublished = moment(page.publishEndDate).isSameOrAfter()
|
|
|
|
}
|
|
|
|
if (!pageIsPublished && !effectivePermissions.pages.write) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.status(403).render('unauthorized', {
|
|
|
|
action: 'view'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Build sidebar navigation
|
2020-05-08 22:48:07 +00:00
|
|
|
let sdi = 1
|
|
|
|
const sidebar = (await WIKI.models.navigation.getTree({ cache: true, locale: pageArgs.locale, groups: req.user.groups })).map(n => ({
|
|
|
|
i: `sdi-${sdi++}`,
|
|
|
|
k: n.kind,
|
|
|
|
l: n.label,
|
|
|
|
c: n.icon,
|
|
|
|
y: n.targetType,
|
|
|
|
t: n.target
|
|
|
|
}))
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Build theme code injection
|
2019-06-26 02:48:23 +00:00
|
|
|
const injectCode = {
|
|
|
|
css: WIKI.config.theming.injectCSS,
|
|
|
|
head: WIKI.config.theming.injectHead,
|
|
|
|
body: WIKI.config.theming.injectBody
|
|
|
|
}
|
2019-07-13 03:56:44 +00:00
|
|
|
|
2020-10-03 20:50:51 +00:00
|
|
|
// Handle missing extra field
|
|
|
|
page.extra = page.extra || { css: '', js: '' }
|
|
|
|
|
2020-06-20 20:39:36 +00:00
|
|
|
if (!_.isEmpty(page.extra.css)) {
|
|
|
|
injectCode.css = `${injectCode.css}\n${page.extra.css}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEmpty(page.extra.js)) {
|
|
|
|
injectCode.body = `${injectCode.body}\n${page.extra.js}`
|
|
|
|
}
|
|
|
|
|
2019-07-13 03:56:44 +00:00
|
|
|
if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
|
2020-05-17 22:24:48 +00:00
|
|
|
// -> Convert page TOC
|
|
|
|
if (_.isString(page.toc)) {
|
|
|
|
page.toc = JSON.parse(page.toc)
|
|
|
|
}
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Render legacy view
|
|
|
|
res.render('legacy/page', {
|
|
|
|
page,
|
|
|
|
sidebar,
|
|
|
|
injectCode,
|
|
|
|
isAuthenticated: req.user && req.user.id !== 2
|
|
|
|
})
|
2019-07-13 03:56:44 +00:00
|
|
|
} else {
|
2020-05-17 22:24:48 +00:00
|
|
|
// -> Convert page TOC
|
|
|
|
if (!_.isString(page.toc)) {
|
|
|
|
page.toc = JSON.stringify(page.toc)
|
|
|
|
}
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// -> Inject comments variables
|
2022-04-15 02:09:45 +00:00
|
|
|
const shouldDisplayComments = WIKI.config.features.featurePageComments && WIKI.data.commentProvider.codeTemplate
|
|
|
|
const commentTmpl = shouldDisplayComments ? {
|
|
|
|
codeTemplate: WIKI.data.commentProvider.codeTemplate,
|
|
|
|
head: WIKI.data.commentProvider.head,
|
|
|
|
body: WIKI.data.commentProvider.body,
|
|
|
|
main: WIKI.data.commentProvider.main
|
|
|
|
} : {
|
|
|
|
codeTemplate: false,
|
|
|
|
head: null,
|
|
|
|
body: null,
|
|
|
|
main: ''
|
|
|
|
}
|
|
|
|
if (shouldDisplayComments) {
|
2020-05-17 02:46:05 +00:00
|
|
|
[
|
|
|
|
{ key: 'pageUrl', value: `${WIKI.config.host}/i/${page.id}` },
|
|
|
|
{ key: 'pageId', value: page.id }
|
|
|
|
].forEach((cfg) => {
|
2022-04-15 02:09:45 +00:00
|
|
|
commentTmpl.head = _.replace(commentTmpl.head, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
|
|
|
|
commentTmpl.body = _.replace(commentTmpl.body, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
|
|
|
|
commentTmpl.main = _.replace(commentTmpl.main, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
|
2020-05-17 02:46:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Render view
|
|
|
|
res.render('page', {
|
|
|
|
page,
|
|
|
|
sidebar,
|
|
|
|
injectCode,
|
2022-04-15 02:09:45 +00:00
|
|
|
comments: commentTmpl,
|
2020-06-19 22:54:05 +00:00
|
|
|
effectivePermissions
|
2020-05-17 02:46:05 +00:00
|
|
|
})
|
2019-07-13 03:56:44 +00:00
|
|
|
}
|
2019-06-26 02:48:23 +00:00
|
|
|
} else if (pageArgs.path === 'home') {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Welcome')
|
2019-07-14 00:27:31 +00:00
|
|
|
res.render('welcome', { locale: pageArgs.locale })
|
2019-05-14 04:53:19 +00:00
|
|
|
} else {
|
2019-06-26 02:48:23 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
2020-06-21 02:08:59 +00:00
|
|
|
if (effectivePermissions.pages.write) {
|
2019-08-04 02:51:29 +00:00
|
|
|
res.status(404).render('new', { path: pageArgs.path, locale: pageArgs.locale })
|
2019-06-26 02:48:23 +00:00
|
|
|
} else {
|
|
|
|
res.status(404).render('notfound', { action: 'view' })
|
|
|
|
}
|
2019-05-14 04:53:19 +00:00
|
|
|
}
|
2019-06-26 02:48:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
next(err)
|
2019-01-26 02:29:34 +00:00
|
|
|
}
|
2018-08-19 05:22:59 +00:00
|
|
|
} else {
|
2019-05-14 04:53:19 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:assets'], pageArgs)) {
|
|
|
|
return res.sendStatus(403)
|
2019-01-12 23:33:30 +00:00
|
|
|
}
|
2019-05-14 04:53:19 +00:00
|
|
|
|
|
|
|
await WIKI.models.assets.getAsset(pageArgs.path, res)
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
2018-08-06 03:03:46 +00:00
|
|
|
})
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
module.exports = router
|