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.

570 lines
17 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const pageHelper = require('../helpers/page')
  4. const _ = require('lodash')
  5. const CleanCSS = require('clean-css')
  6. const moment = require('moment')
  7. /* global WIKI */
  8. const tmplCreateRegex = /^[0-9]+(,[0-9]+)?$/
  9. /**
  10. * Robots.txt
  11. */
  12. router.get('/robots.txt', (req, res, next) => {
  13. res.type('text/plain')
  14. if (_.includes(WIKI.config.seo.robots, 'noindex')) {
  15. res.send('User-agent: *\nDisallow: /')
  16. } else {
  17. res.status(200).end()
  18. }
  19. })
  20. /**
  21. * Health Endpoint
  22. */
  23. router.get('/healthz', (req, res, next) => {
  24. if (WIKI.models.knex.client.pool.numFree() < 1 && WIKI.models.knex.client.pool.numUsed() < 1) {
  25. res.status(503).json({ ok: false }).end()
  26. } else {
  27. res.status(200).json({ ok: true }).end()
  28. }
  29. })
  30. /**
  31. * Administration
  32. */
  33. router.get(['/a', '/a/*'], (req, res, next) => {
  34. if (!WIKI.auth.checkAccess(req.user, [
  35. 'manage:system',
  36. 'write:users',
  37. 'manage:users',
  38. 'write:groups',
  39. 'manage:groups',
  40. 'manage:navigation',
  41. 'manage:theme',
  42. 'manage:api'
  43. ])) {
  44. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  45. return res.status(403).render('unauthorized', { action: 'view' })
  46. }
  47. _.set(res.locals, 'pageMeta.title', 'Admin')
  48. res.render('admin')
  49. })
  50. /**
  51. * Download Page / Version
  52. */
  53. router.get(['/d', '/d/*'], async (req, res, next) => {
  54. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  55. const versionId = (req.query.v) ? _.toSafeInteger(req.query.v) : 0
  56. const page = await WIKI.models.pages.getPageFromDb({
  57. path: pageArgs.path,
  58. locale: pageArgs.locale,
  59. userId: req.user.id,
  60. isPrivate: false
  61. })
  62. pageArgs.tags = _.get(page, 'tags', [])
  63. if (versionId > 0) {
  64. if (!WIKI.auth.checkAccess(req.user, ['read:history'], pageArgs)) {
  65. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  66. return res.render('unauthorized', { action: 'downloadVersion' })
  67. }
  68. } else {
  69. if (!WIKI.auth.checkAccess(req.user, ['read:source'], pageArgs)) {
  70. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  71. return res.render('unauthorized', { action: 'download' })
  72. }
  73. }
  74. if (page) {
  75. const fileName = _.last(page.path.split('/')) + '.' + pageHelper.getFileExtension(page.contentType)
  76. res.attachment(fileName)
  77. if (versionId > 0) {
  78. const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: page.id, versionId })
  79. res.send(pageHelper.injectPageMetadata(pageVersion))
  80. } else {
  81. res.send(pageHelper.injectPageMetadata(page))
  82. }
  83. } else {
  84. res.status(404).end()
  85. }
  86. })
  87. /**
  88. * Create/Edit document
  89. */
  90. router.get(['/e', '/e/*'], async (req, res, next) => {
  91. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  92. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  93. return res.redirect(`/e/${pageArgs.locale}/${pageArgs.path}`)
  94. }
  95. req.i18n.changeLanguage(pageArgs.locale)
  96. // -> Set Editor Lang
  97. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  98. _.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
  99. // -> Check for reserved path
  100. if (pageHelper.isReservedPath(pageArgs.path)) {
  101. return next(new Error('Cannot create this page because it starts with a system reserved path.'))
  102. }
  103. // -> Get page data from DB
  104. let page = await WIKI.models.pages.getPageFromDb({
  105. path: pageArgs.path,
  106. locale: pageArgs.locale,
  107. userId: req.user.id,
  108. isPrivate: false
  109. })
  110. pageArgs.tags = _.get(page, 'tags', [])
  111. // -> Effective Permissions
  112. const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
  113. const injectCode = {
  114. css: WIKI.config.theming.injectCSS,
  115. head: WIKI.config.theming.injectHead,
  116. body: WIKI.config.theming.injectBody
  117. }
  118. if (page) {
  119. // -> EDIT MODE
  120. if (!(effectivePermissions.pages.write || effectivePermissions.pages.manage)) {
  121. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  122. return res.render('unauthorized', { action: 'edit' })
  123. }
  124. // -> Get page tags
  125. await page.$relatedQuery('tags')
  126. page.tags = _.map(page.tags, 'tag')
  127. // Handle missing extra field
  128. page.extra = page.extra || { css: '', js: '' }
  129. // -> Beautify Script CSS
  130. if (!_.isEmpty(page.extra.css)) {
  131. page.extra.css = new CleanCSS({ format: 'beautify' }).minify(page.extra.css).styles
  132. }
  133. _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
  134. _.set(res.locals, 'pageMeta.description', page.description)
  135. page.mode = 'update'
  136. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  137. page.content = Buffer.from(page.content).toString('base64')
  138. } else {
  139. // -> CREATE MODE
  140. if (!effectivePermissions.pages.write) {
  141. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  142. return res.render('unauthorized', { action: 'create' })
  143. }
  144. _.set(res.locals, 'pageMeta.title', `New Page`)
  145. page = {
  146. path: pageArgs.path,
  147. localeCode: pageArgs.locale,
  148. editorKey: null,
  149. mode: 'create',
  150. content: null,
  151. title: null,
  152. description: null,
  153. updatedAt: new Date().toISOString(),
  154. extra: {
  155. css: '',
  156. js: ''
  157. }
  158. }
  159. // -> From Template
  160. if (req.query.from && tmplCreateRegex.test(req.query.from)) {
  161. let tmplPageId = 0
  162. let tmplVersionId = 0
  163. if (req.query.from.indexOf(',')) {
  164. const q = req.query.from.split(',')
  165. tmplPageId = _.toSafeInteger(q[0])
  166. tmplVersionId = _.toSafeInteger(q[1])
  167. } else {
  168. tmplPageId = _.toSafeInteger(req.query.from)
  169. }
  170. if (tmplVersionId > 0) {
  171. // -> From Page Version
  172. const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: tmplPageId, versionId: tmplVersionId })
  173. if (!pageVersion) {
  174. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  175. return res.status(404).render('notfound', { action: 'template' })
  176. }
  177. if (!WIKI.auth.checkAccess(req.user, ['read:history'], { path: pageVersion.path, locale: pageVersion.locale })) {
  178. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  179. return res.render('unauthorized', { action: 'sourceVersion' })
  180. }
  181. page.content = Buffer.from(pageVersion.content).toString('base64')
  182. page.editorKey = pageVersion.editor
  183. page.title = pageVersion.title
  184. page.description = pageVersion.description
  185. } else {
  186. // -> From Page Live
  187. const pageOriginal = await WIKI.models.pages.query().findById(tmplPageId)
  188. if (!pageOriginal) {
  189. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  190. return res.status(404).render('notfound', { action: 'template' })
  191. }
  192. if (!WIKI.auth.checkAccess(req.user, ['read:source'], { path: pageOriginal.path, locale: pageOriginal.locale })) {
  193. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  194. return res.render('unauthorized', { action: 'source' })
  195. }
  196. page.content = Buffer.from(pageOriginal.content).toString('base64')
  197. page.editorKey = pageOriginal.editorKey
  198. page.title = pageOriginal.title
  199. page.description = pageOriginal.description
  200. }
  201. }
  202. }
  203. res.render('editor', { page, injectCode, effectivePermissions })
  204. })
  205. /**
  206. * History
  207. */
  208. router.get(['/h', '/h/*'], async (req, res, next) => {
  209. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  210. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  211. return res.redirect(`/h/${pageArgs.locale}/${pageArgs.path}`)
  212. }
  213. req.i18n.changeLanguage(pageArgs.locale)
  214. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  215. _.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
  216. const page = await WIKI.models.pages.getPageFromDb({
  217. path: pageArgs.path,
  218. locale: pageArgs.locale,
  219. userId: req.user.id,
  220. isPrivate: false
  221. })
  222. if (!page) {
  223. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  224. return res.status(404).render('notfound', { action: 'history' })
  225. }
  226. pageArgs.tags = _.get(page, 'tags', [])
  227. const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
  228. if (!effectivePermissions.history.read) {
  229. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  230. return res.render('unauthorized', { action: 'history' })
  231. }
  232. if (page) {
  233. _.set(res.locals, 'pageMeta.title', page.title)
  234. _.set(res.locals, 'pageMeta.description', page.description)
  235. res.render('history', { page, effectivePermissions })
  236. } else {
  237. res.redirect(`/${pageArgs.path}`)
  238. }
  239. })
  240. /**
  241. * Page ID redirection
  242. */
  243. router.get(['/i', '/i/:id'], async (req, res, next) => {
  244. const pageId = _.toSafeInteger(req.params.id)
  245. if (pageId <= 0) {
  246. return res.redirect('/')
  247. }
  248. const page = await WIKI.models.pages.query().column(['path', 'localeCode', 'isPrivate', 'privateNS']).findById(pageId)
  249. if (!page) {
  250. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  251. return res.status(404).render('notfound', { action: 'view' })
  252. }
  253. if (!WIKI.auth.checkAccess(req.user, ['read:pages'], {
  254. locale: page.localeCode,
  255. path: page.path,
  256. private: page.isPrivate,
  257. privateNS: page.privateNS,
  258. explicitLocale: false,
  259. tags: page.tags
  260. })) {
  261. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  262. return res.render('unauthorized', { action: 'view' })
  263. }
  264. if (WIKI.config.lang.namespacing) {
  265. return res.redirect(`/${page.localeCode}/${page.path}`)
  266. } else {
  267. return res.redirect(`/${page.path}`)
  268. }
  269. })
  270. /**
  271. * Profile
  272. */
  273. router.get(['/p', '/p/*'], (req, res, next) => {
  274. if (!req.user || req.user.id < 1 || req.user.id === 2) {
  275. return res.render('unauthorized', { action: 'view' })
  276. }
  277. _.set(res.locals, 'pageMeta.title', 'User Profile')
  278. res.render('profile')
  279. })
  280. /**
  281. * Source
  282. */
  283. router.get(['/s', '/s/*'], async (req, res, next) => {
  284. const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
  285. const versionId = (req.query.v) ? _.toSafeInteger(req.query.v) : 0
  286. const page = await WIKI.models.pages.getPageFromDb({
  287. path: pageArgs.path,
  288. locale: pageArgs.locale,
  289. userId: req.user.id,
  290. isPrivate: false
  291. })
  292. pageArgs.tags = _.get(page, 'tags', [])
  293. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  294. return res.redirect(`/s/${pageArgs.locale}/${pageArgs.path}`)
  295. }
  296. // -> Effective Permissions
  297. const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
  298. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  299. _.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
  300. if (versionId > 0) {
  301. if (!effectivePermissions.history.read) {
  302. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  303. return res.render('unauthorized', { action: 'sourceVersion' })
  304. }
  305. } else {
  306. if (!effectivePermissions.source.read) {
  307. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  308. return res.render('unauthorized', { action: 'source' })
  309. }
  310. }
  311. if (page) {
  312. if (versionId > 0) {
  313. const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: page.id, versionId })
  314. _.set(res.locals, 'pageMeta.title', pageVersion.title)
  315. _.set(res.locals, 'pageMeta.description', pageVersion.description)
  316. res.render('source', {
  317. page: {
  318. ...page,
  319. ...pageVersion
  320. }
  321. })
  322. } else {
  323. _.set(res.locals, 'pageMeta.title', page.title)
  324. _.set(res.locals, 'pageMeta.description', page.description)
  325. res.render('source', { page, effectivePermissions })
  326. }
  327. } else {
  328. res.redirect(`/${pageArgs.path}`)
  329. }
  330. })
  331. /**
  332. * Tags
  333. */
  334. router.get(['/t', '/t/*'], (req, res, next) => {
  335. _.set(res.locals, 'pageMeta.title', 'Tags')
  336. res.render('tags')
  337. })
  338. /**
  339. * User Avatar
  340. */
  341. router.get('/_userav/:uid', async (req, res, next) => {
  342. if (!WIKI.auth.checkAccess(req.user, ['read:pages'])) {
  343. return res.sendStatus(403)
  344. }
  345. const av = await WIKI.models.users.getUserAvatarData(req.params.uid)
  346. if (av) {
  347. res.set('Content-Type', 'image/jpeg')
  348. res.send(av)
  349. }
  350. return res.sendStatus(404)
  351. })
  352. /**
  353. * View document / asset
  354. */
  355. router.get('/*', async (req, res, next) => {
  356. const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`))
  357. const pageArgs = pageHelper.parsePath(req.path, { stripExt })
  358. const isPage = (stripExt || pageArgs.path.indexOf('.') === -1)
  359. if (isPage) {
  360. if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
  361. return res.redirect(`/${pageArgs.locale}/${pageArgs.path}`)
  362. }
  363. req.i18n.changeLanguage(pageArgs.locale)
  364. try {
  365. // -> Get Page from cache
  366. const page = await WIKI.models.pages.getPage({
  367. path: pageArgs.path,
  368. locale: pageArgs.locale,
  369. userId: req.user.id,
  370. isPrivate: false
  371. })
  372. pageArgs.tags = _.get(page, 'tags', [])
  373. // -> Effective Permissions
  374. const effectivePermissions = WIKI.auth.getEffectivePermissions(req, pageArgs)
  375. // -> Check User Access
  376. if (!effectivePermissions.pages.read) {
  377. if (req.user.id === 2) {
  378. res.cookie('loginRedirect', req.path, {
  379. maxAge: 15 * 60 * 1000
  380. })
  381. }
  382. if (pageArgs.path === 'home' && req.user.id === 2) {
  383. return res.redirect('/login')
  384. }
  385. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  386. return res.status(403).render('unauthorized', {
  387. action: 'view'
  388. })
  389. }
  390. _.set(res, 'locals.siteConfig.lang', pageArgs.locale)
  391. _.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
  392. if (page) {
  393. _.set(res.locals, 'pageMeta.title', page.title)
  394. _.set(res.locals, 'pageMeta.description', page.description)
  395. // -> Check Publishing State
  396. let pageIsPublished = page.isPublished
  397. if (pageIsPublished && !_.isEmpty(page.publishStartDate)) {
  398. pageIsPublished = moment(page.publishStartDate).isSameOrBefore()
  399. }
  400. if (pageIsPublished && !_.isEmpty(page.publishEndDate)) {
  401. pageIsPublished = moment(page.publishEndDate).isSameOrAfter()
  402. }
  403. if (!pageIsPublished && !effectivePermissions.pages.write) {
  404. _.set(res.locals, 'pageMeta.title', 'Unauthorized')
  405. return res.status(403).render('unauthorized', {
  406. action: 'view'
  407. })
  408. }
  409. // -> Build sidebar navigation
  410. let sdi = 1
  411. const sidebar = (await WIKI.models.navigation.getTree({ cache: true, locale: pageArgs.locale, groups: req.user.groups })).map(n => ({
  412. i: `sdi-${sdi++}`,
  413. k: n.kind,
  414. l: n.label,
  415. c: n.icon,
  416. y: n.targetType,
  417. t: n.target
  418. }))
  419. // -> Build theme code injection
  420. const injectCode = {
  421. css: WIKI.config.theming.injectCSS,
  422. head: WIKI.config.theming.injectHead,
  423. body: WIKI.config.theming.injectBody
  424. }
  425. // Handle missing extra field
  426. page.extra = page.extra || { css: '', js: '' }
  427. if (!_.isEmpty(page.extra.css)) {
  428. injectCode.css = `${injectCode.css}\n${page.extra.css}`
  429. }
  430. if (!_.isEmpty(page.extra.js)) {
  431. injectCode.body = `${injectCode.body}\n${page.extra.js}`
  432. }
  433. if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
  434. // -> Convert page TOC
  435. if (_.isString(page.toc)) {
  436. page.toc = JSON.parse(page.toc)
  437. }
  438. // -> Render legacy view
  439. res.render('legacy/page', {
  440. page,
  441. sidebar,
  442. injectCode,
  443. isAuthenticated: req.user && req.user.id !== 2
  444. })
  445. } else {
  446. // -> Convert page TOC
  447. if (!_.isString(page.toc)) {
  448. page.toc = JSON.stringify(page.toc)
  449. }
  450. // -> Inject comments variables
  451. if (WIKI.config.features.featurePageComments && WIKI.data.commentProvider.codeTemplate) {
  452. [
  453. { key: 'pageUrl', value: `${WIKI.config.host}/i/${page.id}` },
  454. { key: 'pageId', value: page.id }
  455. ].forEach((cfg) => {
  456. WIKI.data.commentProvider.head = _.replace(WIKI.data.commentProvider.head, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
  457. WIKI.data.commentProvider.body = _.replace(WIKI.data.commentProvider.body, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
  458. WIKI.data.commentProvider.main = _.replace(WIKI.data.commentProvider.main, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value)
  459. })
  460. }
  461. // -> Render view
  462. res.render('page', {
  463. page,
  464. sidebar,
  465. injectCode,
  466. comments: WIKI.data.commentProvider,
  467. effectivePermissions
  468. })
  469. }
  470. } else if (pageArgs.path === 'home') {
  471. _.set(res.locals, 'pageMeta.title', 'Welcome')
  472. res.render('welcome', { locale: pageArgs.locale })
  473. } else {
  474. _.set(res.locals, 'pageMeta.title', 'Page Not Found')
  475. if (effectivePermissions.pages.write) {
  476. res.status(404).render('new', { path: pageArgs.path, locale: pageArgs.locale })
  477. } else {
  478. res.status(404).render('notfound', { action: 'view' })
  479. }
  480. }
  481. } catch (err) {
  482. next(err)
  483. }
  484. } else {
  485. if (!WIKI.auth.checkAccess(req.user, ['read:assets'], pageArgs)) {
  486. return res.sendStatus(403)
  487. }
  488. await WIKI.models.assets.getAsset(pageArgs.path, res)
  489. }
  490. })
  491. module.exports = router