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.

230 lines
6.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. * 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. * Administration
  29. */
  30. router.get(['/a', '/a/*'], (req, res, next) => {
  31. _.set(res.locals, 'pageMeta.title', 'Admin')
  32. res.render('admin')
  33. })
  34. /**
  35. * Create/Edit document
  36. */
  37. router.get(['/e', '/e/*'], async (req, res, next) => {
  38. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  39. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  40. return res.redirect(`/e/${pageArgs.locale}/${pageArgs.path}`)
  41. }
  42. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  43. if (pageHelper.isReservedPath(pageArgs.path)) {
  44. return next(new Error('Cannot create this page because it starts with a system reserved path.'))
  45. }
  46. let page = await WIKI.models.pages.getPageFromDb({
  47. path: pageArgs.path,
  48. locale: pageArgs.locale,
  49. userId: req.user.id,
  50. isPrivate: false
  51. })
  52. if (page) {
  53. if (!WIKI.auth.checkAccess(req.user, ['manage:pages'], pageArgs)) {
  54. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  55. return res.render('unauthorized', { action: 'edit' })
  56. }
  57. _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
  58. _.set(res.locals, 'pageMeta.description', page.description)
  59. page.mode = 'update'
  60. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  61. page.content = Buffer.from(page.content).toString('base64')
  62. } else {
  63. if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  64. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  65. return res.render('unauthorized', { action: 'create' })
  66. }
  67. _.set(res.locals, 'pageMeta.title', `New Page`)
  68. page = {
  69. path: pageArgs.path,
  70. localeCode: pageArgs.locale,
  71. editorKey: null,
  72. mode: 'create',
  73. content: null
  74. }
  75. }
  76. res.render('editor', { page })
  77. })
  78. /**
  79. * History
  80. */
  81. router.get(['/h', '/h/*'], async (req, res, next) => {
  82. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  83. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  84. return res.redirect(`/h/${pageArgs.locale}/${pageArgs.path}`)
  85. }
  86. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  87. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  88. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  89. return res.render('unauthorized', { action: 'history' })
  90. }
  91. const page = await WIKI.models.pages.getPageFromDb({
  92. path: pageArgs.path,
  93. locale: pageArgs.locale,
  94. userId: req.user.id,
  95. isPrivate: false
  96. })
  97. if (page) {
  98. _.set(res.locals, 'pageMeta.title', page.title)
  99. _.set(res.locals, 'pageMeta.description', page.description)
  100. res.render('history', { page })
  101. } else {
  102. res.redirect(`/${pageArgs.path}`)
  103. }
  104. })
  105. /**
  106. * Profile
  107. */
  108. router.get(['/p', '/p/*'], (req, res, next) => {
  109. _.set(res.locals, 'pageMeta.title', 'User Profile')
  110. res.render('profile')
  111. })
  112. /**
  113. * Source
  114. */
  115. router.get(['/s', '/s/*'], async (req, res, next) => {
  116. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  117. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  118. return res.redirect(`/s/${pageArgs.locale}/${pageArgs.path}`)
  119. }
  120. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  121. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  122. return res.render('unauthorized', { action: 'source' })
  123. }
  124. const page = await WIKI.models.pages.getPageFromDb({
  125. path: pageArgs.path,
  126. locale: pageArgs.locale,
  127. userId: req.user.id,
  128. isPrivate: false
  129. })
  130. if (page) {
  131. _.set(res.locals, 'pageMeta.title', page.title)
  132. _.set(res.locals, 'pageMeta.description', page.description)
  133. res.render('source', { page })
  134. } else {
  135. res.redirect(`/${pageArgs.path}`)
  136. }
  137. })
  138. /**
  139. * View document / asset
  140. */
  141. router.get('/*', async (req, res, next) => {
  142. const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`))
  143. const pageArgs = pageHelper.parsePath(req.path, { stripExt })
  144. const isPage = (stripExt || pageArgs.path.indexOf('.') === -1)
  145. if (isPage) {
  146. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  147. return res.redirect(`/${pageArgs.locale}/${pageArgs.path}`)
  148. }
  149. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
  150. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  151. return res.status(403).render('unauthorized', { action: 'view' })
  152. }
  153. try {
  154. const page = await WIKI.models.pages.getPage({
  155. path: pageArgs.path,
  156. locale: pageArgs.locale,
  157. userId: req.user.id,
  158. isPrivate: false
  159. })
  160. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  161. if (page) {
  162. _.set(res.locals, 'pageMeta.title', page.title)
  163. _.set(res.locals, 'pageMeta.description', page.description)
  164. const sidebar = await WIKI.models.navigation.getTree({ cache: true })
  165. const injectCode = {
  166. css: WIKI.config.theming.injectCSS,
  167. head: WIKI.config.theming.injectHead,
  168. body: WIKI.config.theming.injectBody
  169. }
  170. if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
  171. if (_.isString(page.toc)) {
  172. page.toc = JSON.parse(page.toc)
  173. }
  174. res.render('legacy', { page, sidebar, injectCode })
  175. } else {
  176. res.render('page', { page, sidebar, injectCode })
  177. }
  178. } else if (pageArgs.path === 'home') {
  179. _.set(res.locals, 'pageMeta.title', 'Welcome')
  180. res.render('welcome')
  181. } else {
  182. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  183. if (WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
  184. res.status(404).render('new', { pagePath: req.path })
  185. } else {
  186. res.status(404).render('notfound', { action: 'view' })
  187. }
  188. }
  189. } catch (err) {
  190. next(err)
  191. }
  192. } else {
  193. if (!WIKI.auth.checkAccess(req.user, ['read:assets'], pageArgs)) {
  194. return res.sendStatus(403)
  195. }
  196. await WIKI.models.assets.getAsset(pageArgs.path, res)
  197. }
  198. })
  199. module.exports = router