ExpressLocals.coffee 4.9 KB

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