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.

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