You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.4 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const pageHelper = require('../helpers/page')
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Robots.txt
  8. */
  9. router.get('/robots.txt', (req, res, next) => {
  10. res.type('text/plain')
  11. if (_.includes(WIKI.config.seo.robots, 'noindex')) {
  12. res.send('User-agent: *\nDisallow: /')
  13. } else {
  14. res.status(200).end()
  15. }
  16. })
  17. /**
  18. * Health Endpoint
  19. */
  20. router.get('/healthz', (req, res, next) => {
  21. if (WIKI.models.knex.client.pool.numFree() < 1 && WIKI.models.knex.client.pool.numUsed() < 1) {
  22. res.status(503).json({ ok: false }).end()
  23. } else {
  24. res.status(200).json({ ok: true }).end()
  25. }
  26. })
  27. /**
  28. * Create/Edit document
  29. */
  30. router.get(['/e', '/e/*'], async (req, res, next) => {
  31. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  32. if (pageHelper.isReservedPath(pageArgs.path)) {
  33. return next(new Error('Cannot create this page because it starts with a system reserved path.'))
  34. }
  35. let page = await WIKI.models.pages.getPageFromDb({
  36. path: pageArgs.path,
  37. locale: pageArgs.locale,
  38. userId: req.user.id,
  39. isPrivate: false
  40. })
  41. if (page) {
  42. if (!WIKI.auth.checkAccess(req.user, ['manage:pages'], pageArgs)) {
  43. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  44. return res.render('unauthorized', { action: 'edit' })
  45. }
  46. _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
  47. _.set(res.locals, 'pageMeta.description', page.description)
  48. page.mode = 'update'
  49. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  50. page.content = Buffer.from(page.content).toString('base64')
  51. } else {
  52. if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  53. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  54. return res.render('unauthorized', { action: 'create' })
  55. }
  56. _.set(res.locals, 'pageMeta.title', `New Page`)
  57. page = {
  58. path: pageArgs.path,
  59. localeCode: pageArgs.locale,
  60. editorKey: null,
  61. mode: 'create',
  62. content: null
  63. }
  64. }
  65. res.render('editor', { page })
  66. })
  67. /**
  68. * Administration
  69. */
  70. router.get(['/a', '/a/*'], (req, res, next) => {
  71. _.set(res.locals, 'pageMeta.title', 'Admin')
  72. res.render('admin')
  73. })
  74. /**
  75. * Profile
  76. */
  77. router.get(['/p', '/p/*'], (req, res, next) => {
  78. _.set(res.locals, 'pageMeta.title', 'User Profile')
  79. res.render('profile')
  80. })
  81. /**
  82. * History
  83. */
  84. router.get(['/h', '/h/*'], async (req, res, next) => {
  85. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  86. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  87. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  88. return res.render('unauthorized', { action: 'history' })
  89. }
  90. const page = await WIKI.models.pages.getPageFromDb({
  91. path: pageArgs.path,
  92. locale: pageArgs.locale,
  93. userId: req.user.id,
  94. isPrivate: false
  95. })
  96. if (page) {
  97. _.set(res.locals, 'pageMeta.title', page.title)
  98. _.set(res.locals, 'pageMeta.description', page.description)
  99. res.render('history', { page })
  100. } else {
  101. res.redirect(`/${pageArgs.path}`)
  102. }
  103. })
  104. /**
  105. * Source
  106. */
  107. router.get(['/s', '/s/*'], async (req, res, next) => {
  108. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  109. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  110. return res.render('unauthorized', { action: 'source' })
  111. }
  112. const page = await WIKI.models.pages.getPageFromDb({
  113. path: pageArgs.path,
  114. locale: pageArgs.locale,
  115. userId: req.user.id,
  116. isPrivate: false
  117. })
  118. if (page) {
  119. _.set(res.locals, 'pageMeta.title', page.title)
  120. _.set(res.locals, 'pageMeta.description', page.description)
  121. res.render('source', { page })
  122. } else {
  123. res.redirect(`/${pageArgs.path}`)
  124. }
  125. })
  126. /**
  127. * View document / asset
  128. */
  129. router.get('/*', async (req, res, next) => {
  130. const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`))
  131. const pageArgs = pageHelper.parsePath(req.path, { stripExt })
  132. const isPage = (stripExt || pageArgs.path.indexOf('.') === -1)
  133. if (isPage) {
  134. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  135. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  136. return res.status(403).render('unauthorized', { action: 'view' })
  137. }
  138. const page = await WIKI.models.pages.getPage({
  139. path: pageArgs.path,
  140. locale: pageArgs.locale,
  141. userId: req.user.id,
  142. isPrivate: false
  143. })
  144. if (page) {
  145. _.set(res.locals, 'pageMeta.title', page.title)
  146. _.set(res.locals, 'pageMeta.description', page.description)
  147. const sidebar = await WIKI.models.navigation.getTree({ cache: true })
  148. const injectCode = {
  149. css: WIKI.config.theming.injectCSS,
  150. head: WIKI.config.theming.injectHead,
  151. body: WIKI.config.theming.injectBody
  152. }
  153. res.render('page', { page, sidebar, injectCode })
  154. } else if (pageArgs.path === 'home') {
  155. _.set(res.locals, 'pageMeta.title', 'Welcome')
  156. res.render('welcome')
  157. } else {
  158. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  159. if (WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  160. res.status(404).render('new', { pagePath: req.path })
  161. } else {
  162. res.status(404).render('notfound', { action: 'view' })
  163. }
  164. }
  165. } else {
  166. if (!WIKI.auth.checkAccess(req.user, ['read:assets'], pageArgs)) {
  167. return res.sendStatus(403)
  168. }
  169. await WIKI.models.assets.getAsset(pageArgs.path, res)
  170. }
  171. })
  172. module.exports = router