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.

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