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.

171 lines
4.6 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. * Create/Edit document
  19. */
  20. router.get(['/e', '/e/*'], async (req, res, next) => {
  21. const pageArgs = pageHelper.parsePath(req.path)
  22. let page = await WIKI.models.pages.getPageFromDb({
  23. path: pageArgs.path,
  24. locale: pageArgs.locale,
  25. userId: req.user.id,
  26. isPrivate: false
  27. })
  28. if (page) {
  29. if (!WIKI.auth.checkAccess(req.user, ['manage:pages'], pageArgs)) {
  30. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  31. return res.render('unauthorized', { action: 'edit' })
  32. }
  33. _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
  34. _.set(res.locals, 'pageMeta.description', page.description)
  35. page.mode = 'update'
  36. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  37. page.content = Buffer.from(page.content).toString('base64')
  38. } else {
  39. if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  40. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  41. return res.render('unauthorized', { action: 'create' })
  42. }
  43. _.set(res.locals, 'pageMeta.title', `New Page`)
  44. page = {
  45. path: pageArgs.path,
  46. localeCode: pageArgs.locale,
  47. editorKey: null,
  48. mode: 'create',
  49. content: null
  50. }
  51. }
  52. res.render('editor', { page })
  53. })
  54. /**
  55. * Administration
  56. */
  57. router.get(['/a', '/a/*'], (req, res, next) => {
  58. _.set(res.locals, 'pageMeta.title', 'Admin')
  59. res.render('admin')
  60. })
  61. /**
  62. * Profile
  63. */
  64. router.get(['/p', '/p/*'], (req, res, next) => {
  65. _.set(res.locals, 'pageMeta.title', 'User Profile')
  66. res.render('profile')
  67. })
  68. /**
  69. * History
  70. */
  71. router.get(['/h', '/h/*'], async (req, res, next) => {
  72. const pageArgs = pageHelper.parsePath(req.path)
  73. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  74. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  75. return res.render('unauthorized', { action: 'history' })
  76. }
  77. const page = await WIKI.models.pages.getPageFromDb({
  78. path: pageArgs.path,
  79. locale: pageArgs.locale,
  80. userId: req.user.id,
  81. isPrivate: false
  82. })
  83. if (page) {
  84. _.set(res.locals, 'pageMeta.title', page.title)
  85. _.set(res.locals, 'pageMeta.description', page.description)
  86. res.render('history', { page })
  87. } else {
  88. res.redirect(`/${pageArgs.path}`)
  89. }
  90. })
  91. /**
  92. * Source
  93. */
  94. router.get(['/s', '/s/*'], async (req, res, next) => {
  95. const pageArgs = pageHelper.parsePath(req.path)
  96. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  97. return res.render('unauthorized', { action: 'source' })
  98. }
  99. const page = await WIKI.models.pages.getPageFromDb({
  100. path: pageArgs.path,
  101. locale: pageArgs.locale,
  102. userId: req.user.id,
  103. isPrivate: false
  104. })
  105. if (page) {
  106. _.set(res.locals, 'pageMeta.title', page.title)
  107. _.set(res.locals, 'pageMeta.description', page.description)
  108. res.render('source', { page })
  109. } else {
  110. res.redirect(`/${pageArgs.path}`)
  111. }
  112. })
  113. /**
  114. * View document
  115. */
  116. router.get('/*', async (req, res, next) => {
  117. const pageArgs = pageHelper.parsePath(req.path)
  118. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  119. if (pageArgs.path === 'home') {
  120. return res.redirect('/login')
  121. } else {
  122. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  123. return res.render('unauthorized', { action: 'view' })
  124. }
  125. }
  126. const page = await WIKI.models.pages.getPage({
  127. path: pageArgs.path,
  128. locale: pageArgs.locale,
  129. userId: req.user.id,
  130. isPrivate: false
  131. })
  132. if (page) {
  133. _.set(res.locals, 'pageMeta.title', page.title)
  134. _.set(res.locals, 'pageMeta.description', page.description)
  135. const sidebar = await WIKI.models.navigation.getTree({ cache: true })
  136. const injectCode = {
  137. css: WIKI.config.theming.injectCSS,
  138. head: WIKI.config.theming.injectHead,
  139. body: WIKI.config.theming.injectBody
  140. }
  141. res.render('page', { page, sidebar, injectCode })
  142. } else if (pageArgs.path === 'home') {
  143. _.set(res.locals, 'pageMeta.title', 'Welcome')
  144. res.render('welcome')
  145. } else {
  146. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  147. if (WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  148. res.status(404).render('new', { pagePath: req.path })
  149. } else {
  150. res.render('notfound', { action: 'view' })
  151. }
  152. }
  153. })
  154. module.exports = router