ExpressLocals.coffee 4.2 KB

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