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')
|
2018-08-19 05:22:59 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
2017-07-06 04:10:41 +00:00
|
|
|
|
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) => {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Admin')
|
|
|
|
res.render('admin')
|
|
|
|
})
|
|
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
|
|
|
|
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.'))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
2019-01-07 03:03:34 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['manage:pages'], pageArgs)) {
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2019-01-07 03:03:34 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
|
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',
|
|
|
|
content: null
|
|
|
|
}
|
|
|
|
}
|
2019-07-14 20:12:26 +00:00
|
|
|
res.render('editor', { page, injectCode })
|
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}`)
|
|
|
|
}
|
|
|
|
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
|
|
|
|
2019-01-07 03:03:34 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
|
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: 'history' })
|
2019-01-07 03:03:34 +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
|
|
|
|
})
|
|
|
|
if (page) {
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2018-10-29 02:09:58 +00:00
|
|
|
res.render('history', { page })
|
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-02 05:48:19 +00:00
|
|
|
/**
|
|
|
|
* Profile
|
|
|
|
*/
|
|
|
|
router.get(['/p', '/p/*'], (req, res, next) => {
|
|
|
|
_.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 })
|
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}`)
|
|
|
|
}
|
|
|
|
|
2019-06-22 16:44:32 +00:00
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
|
|
|
|
2019-01-07 03:03:34 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
|
2019-03-19 19:15:40 +00:00
|
|
|
return res.render('unauthorized', { action: 'source' })
|
2019-01-07 03:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 06:28:20 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
if (page) {
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
2018-11-25 06:28:20 +00:00
|
|
|
res.render('source', { page })
|
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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-05-14 04:53:19 +00:00
|
|
|
if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
|
|
|
return res.status(403).render('unauthorized', { action: 'view' })
|
|
|
|
}
|
2019-01-07 03:03:34 +00:00
|
|
|
|
2019-06-26 02:48:23 +00:00
|
|
|
try {
|
|
|
|
const page = await WIKI.models.pages.getPage({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
|
|
|
|
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
_.set(res.locals, 'pageMeta.title', page.title)
|
|
|
|
_.set(res.locals, 'pageMeta.description', page.description)
|
|
|
|
const sidebar = await WIKI.models.navigation.getTree({ cache: true })
|
|
|
|
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
|
|
|
|
|
|
|
if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
|
|
|
|
if (_.isString(page.toc)) {
|
|
|
|
page.toc = JSON.parse(page.toc)
|
|
|
|
}
|
2019-07-13 20:18:43 +00:00
|
|
|
res.render('legacy/page', { page, sidebar, injectCode, isAuthenticated: req.user && req.user.id !== 2 })
|
2019-07-13 03:56:44 +00:00
|
|
|
} else {
|
|
|
|
res.render('page', { page, sidebar, injectCode })
|
|
|
|
}
|
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')
|
|
|
|
if (WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
|
|
|
|
res.status(404).render('new', { pagePath: req.path })
|
|
|
|
} 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
|