ExpressLocals.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. logger = require 'logger-sharelatex'
  2. fs = require 'fs'
  3. crypto = require 'crypto'
  4. Settings = require('settings-sharelatex')
  5. SubscriptionFormatters = require('../Features/Subscription/SubscriptionFormatters')
  6. querystring = require('querystring')
  7. SystemMessageManager = require("../Features/SystemMessages/SystemMessageManager")
  8. _ = require("underscore")
  9. Modules = require "./Modules"
  10. fingerprints = {}
  11. Path = require 'path'
  12. jsPath =
  13. if Settings.useMinifiedJs
  14. "/minjs/"
  15. else
  16. "/js/"
  17. logger.log "Generating file fingerprints..."
  18. for path in [
  19. "#{jsPath}libs/require.js",
  20. "#{jsPath}ide.js",
  21. "#{jsPath}main.js",
  22. "#{jsPath}libs.js",
  23. "#{jsPath}ace/ace.js",
  24. "#{jsPath}libs/pdf.js",
  25. "#{jsPath}libs/pdf.worker.js",
  26. "#{jsPath}libs/compatibility.js",
  27. "#{jsPath}libs/pdfjs-1.0.712/pdf.js",
  28. "#{jsPath}libs/pdfjs-1.0.712/pdf.worker.js",
  29. "#{jsPath}libs/pdfjs-1.0.712/compatibility.js",
  30. "/stylesheets/style.css"
  31. ]
  32. filePath = Path.join __dirname, "../../../", "public#{path}"
  33. exists = fs.existsSync filePath
  34. if exists
  35. content = fs.readFileSync filePath
  36. hash = crypto.createHash("md5").update(content).digest("hex")
  37. logger.log "#{filePath}: #{hash}"
  38. fingerprints[path] = hash
  39. else
  40. logger.log filePath:filePath, "file does not exist for fingerprints"
  41. module.exports = (app)->
  42. app.use (req, res, next)->
  43. res.locals.session = req.session
  44. next()
  45. app.use (req, res, next)->
  46. res.locals.jsPath = jsPath
  47. next()
  48. app.use (req, res, next)->
  49. res.locals.settings = Settings
  50. next()
  51. app.use (req, res, next)->
  52. res.locals.translate = req.i18n.translate
  53. res.locals.currentUrl = req.originalUrl
  54. next()
  55. app.use (req, res, next)->
  56. res.locals.getSiteHost = ->
  57. Settings.siteUrl.substring(Settings.siteUrl.indexOf("//")+2)
  58. next()
  59. app.use (req, res, next)->
  60. res.locals.formatProjectPublicAccessLevel = (privilegeLevel)->
  61. formatedPrivileges = private:"Private", readOnly:"Public: Read Only", readAndWrite:"Public: Read and Write"
  62. return formatedPrivileges[privilegeLevel] || "Private"
  63. next()
  64. app.use (req, res, next)->
  65. res.locals.buildReferalUrl = (referal_medium) ->
  66. url = Settings.siteUrl
  67. if req.session? and req.session.user? and req.session.user.referal_id?
  68. url+="?r=#{req.session.user.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
  69. return url
  70. res.locals.getReferalId = ->
  71. if req.session? and req.session.user? and req.session.user.referal_id
  72. return req.session.user.referal_id
  73. res.locals.getReferalTagLine = ->
  74. tagLines = [
  75. "Roar!"
  76. "Shout about us!"
  77. "Please recommend us"
  78. "Tell the world!"
  79. "Thanks for using ShareLaTeX"
  80. ]
  81. return tagLines[Math.floor(Math.random()*tagLines.length)]
  82. res.locals.getRedirAsQueryString = ->
  83. if req.query.redir?
  84. return "?#{querystring.stringify({redir:req.query.redir})}"
  85. return ""
  86. next()
  87. app.use (req, res, next) ->
  88. res.locals.csrfToken = req.session._csrf
  89. next()
  90. app.use (req, res, next)->
  91. res.locals.fingerprint = (path) ->
  92. if fingerprints[path]?
  93. return fingerprints[path]
  94. else
  95. logger.err "No fingerprint for file: #{path}"
  96. return ""
  97. next()
  98. app.use (req, res, next)->
  99. res.locals.formatPrice = SubscriptionFormatters.formatPrice
  100. next()
  101. app.use (req, res, next)->
  102. if req.session.user?
  103. res.locals.user =
  104. email: req.session.user.email
  105. first_name: req.session.user.first_name
  106. last_name: req.session.user.last_name
  107. if req.session.justRegistered
  108. res.locals.justRegistered = true
  109. delete req.session.justRegistered
  110. if req.session.justLoggedIn
  111. res.locals.justLoggedIn = true
  112. delete req.session.justLoggedIn
  113. res.locals.gaToken = Settings.analytics?.ga?.token
  114. res.locals.tenderUrl = Settings.tenderUrl
  115. res.locals.sentrySrc = Settings.sentry?.src
  116. res.locals.sentryPublicDSN = Settings.sentry?.publicDSN
  117. next()
  118. app.use (req, res, next) ->
  119. if req.query? and req.query.scribtex_path?
  120. res.locals.lookingForScribtex = true
  121. res.locals.scribtexPath = req.query.scribtex_path
  122. next()
  123. app.use (req, res, next) ->
  124. res.locals.nav = Settings.nav
  125. res.locals.templates = Settings.templateLinks
  126. next()
  127. app.use (req, res, next) ->
  128. SystemMessageManager.getMessages (error, messages = []) ->
  129. res.locals.systemMessages = messages
  130. next()
  131. app.use (req, res, next)->
  132. subdomain = _.find Settings.i18n.subdomainLang, (subdomain)->
  133. subdomain.lngCode == req.showUserOtherLng and !subdomain.hide
  134. res.locals.recomendSubdomain = subdomain
  135. res.locals.currentLngCode = req.lng
  136. next()
  137. app.use (req, res, next) ->
  138. if Settings.reloadModuleViewsOnEachRequest
  139. Modules.loadViewIncludes()
  140. res.locals.moduleIncludes = Modules.moduleIncludes
  141. next()