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.

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