ExpressLocals.coffee 4.4 KB

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