ExpressLocals.coffee 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. Url = require "url"
  11. fingerprints = {}
  12. Path = require 'path'
  13. jsPath =
  14. if Settings.useMinifiedJs
  15. "/minjs/"
  16. else
  17. "/js/"
  18. logger.log "Generating file fingerprints..."
  19. for path in [
  20. "#{jsPath}libs/require.js",
  21. "#{jsPath}ide.js",
  22. "#{jsPath}main.js",
  23. "#{jsPath}libs.js",
  24. "#{jsPath}ace/ace.js",
  25. "#{jsPath}libs/pdfjs-1.3.91/pdf.js",
  26. "#{jsPath}libs/pdfjs-1.3.91/pdf.worker.js",
  27. "#{jsPath}libs/pdfjs-1.3.91/compatibility.js",
  28. "/stylesheets/style.css"
  29. ]
  30. filePath = Path.join __dirname, "../../../", "public#{path}"
  31. exists = fs.existsSync filePath
  32. if exists
  33. content = fs.readFileSync filePath
  34. hash = crypto.createHash("md5").update(content).digest("hex")
  35. logger.log "#{filePath}: #{hash}"
  36. fingerprints[path] = hash
  37. else
  38. logger.log filePath:filePath, "file does not exist for fingerprints"
  39. getFingerprint = (path) ->
  40. if fingerprints[path]?
  41. return fingerprints[path]
  42. else
  43. logger.err "No fingerprint for file: #{path}"
  44. return ""
  45. logger.log "Finished generating file fingerprints"
  46. cdnAvailable = Settings.cdn?.web?.host?
  47. darkCdnAvailable = Settings.cdn?.web?.darkHost?
  48. module.exports = (app, webRouter, apiRouter)->
  49. webRouter.use (req, res, next)->
  50. res.locals.session = req.session
  51. next()
  52. webRouter.use (req, res, next)->
  53. isDark = req.headers?.host?.slice(0,4)?.toLowerCase() == "dark"
  54. isSmoke = req.headers?.host?.slice(0,5)?.toLowerCase() == "smoke"
  55. isLive = !isDark and !isSmoke
  56. if cdnAvailable and isLive
  57. staticFilesBase = Settings.cdn?.web?.host
  58. else if darkCdnAvailable and isDark
  59. staticFilesBase = Settings.cdn?.web?.darkHost
  60. else
  61. staticFilesBase = ""
  62. res.locals.jsPath = jsPath
  63. res.locals.fullJsPath = Url.resolve(staticFilesBase, jsPath)
  64. res.locals.buildJsPath = (jsFile, opts = {})->
  65. path = Path.join(jsPath, jsFile)
  66. doFingerPrint = opts.fingerprint != false
  67. if !opts.qs?
  68. opts.qs = {}
  69. if !opts.qs?.fingerprint? and doFingerPrint
  70. opts.qs.fingerprint = getFingerprint(path)
  71. if opts.cdn != false
  72. path = Url.resolve(staticFilesBase, path)
  73. qs = querystring.stringify(opts.qs)
  74. if qs? and qs.length > 0
  75. path = path + "?" + qs
  76. return path
  77. res.locals.buildCssPath = (cssFile)->
  78. path = Path.join("/stylesheets/", cssFile)
  79. return Url.resolve(staticFilesBase, path) + "?fingerprint=" + getFingerprint(path)
  80. res.locals.buildImgPath = (imgFile)->
  81. path = Path.join("/img/", imgFile)
  82. return Url.resolve(staticFilesBase, path)
  83. next()
  84. webRouter.use (req, res, next)->
  85. res.locals.settings = Settings
  86. next()
  87. webRouter.use (req, res, next)->
  88. res.locals.translate = (key, vars = {}) ->
  89. vars.appName = Settings.appName
  90. req.i18n.translate(key, vars)
  91. res.locals.currentUrl = req.originalUrl
  92. next()
  93. webRouter.use (req, res, next)->
  94. res.locals.getSiteHost = ->
  95. Settings.siteUrl.substring(Settings.siteUrl.indexOf("//")+2)
  96. next()
  97. webRouter.use (req, res, next)->
  98. res.locals.getUserEmail = ->
  99. email = req?.session?.user?.email or ""
  100. return email
  101. next()
  102. webRouter.use (req, res, next)->
  103. res.locals.formatProjectPublicAccessLevel = (privilegeLevel)->
  104. formatedPrivileges = private:"Private", readOnly:"Public: Read Only", readAndWrite:"Public: Read and Write"
  105. return formatedPrivileges[privilegeLevel] || "Private"
  106. next()
  107. webRouter.use (req, res, next)->
  108. res.locals.buildReferalUrl = (referal_medium) ->
  109. url = Settings.siteUrl
  110. if req.session? and req.session.user? and req.session.user.referal_id?
  111. url+="?r=#{req.session.user.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
  112. return url
  113. res.locals.getReferalId = ->
  114. if req.session? and req.session.user? and req.session.user.referal_id
  115. return req.session.user.referal_id
  116. res.locals.getReferalTagLine = ->
  117. tagLines = [
  118. "Roar!"
  119. "Shout about us!"
  120. "Please recommend us"
  121. "Tell the world!"
  122. "Thanks for using ShareLaTeX"
  123. ]
  124. return tagLines[Math.floor(Math.random()*tagLines.length)]
  125. res.locals.getRedirAsQueryString = ->
  126. if req.query.redir?
  127. return "?#{querystring.stringify({redir:req.query.redir})}"
  128. return ""
  129. res.locals.getLoggedInUserId = ->
  130. return req.session.user?._id
  131. next()
  132. webRouter.use (req, res, next) ->
  133. res.locals.csrfToken = req?.csrfToken()
  134. next()
  135. webRouter.use (req, res, next) ->
  136. res.locals.getReqQueryParam = (field)->
  137. return req.query?[field]
  138. next()
  139. webRouter.use (req, res, next)->
  140. res.locals.fingerprint = getFingerprint
  141. next()
  142. webRouter.use (req, res, next)->
  143. res.locals.formatPrice = SubscriptionFormatters.formatPrice
  144. next()
  145. webRouter.use (req, res, next)->
  146. res.locals.externalAuthenticationSystemUsed = ->
  147. Settings.ldap?
  148. next()
  149. webRouter.use (req, res, next)->
  150. if req.session.user?
  151. res.locals.user =
  152. email: req.session.user.email
  153. first_name: req.session.user.first_name
  154. last_name: req.session.user.last_name
  155. if req.session.justRegistered
  156. res.locals.justRegistered = true
  157. delete req.session.justRegistered
  158. if req.session.justLoggedIn
  159. res.locals.justLoggedIn = true
  160. delete req.session.justLoggedIn
  161. res.locals.gaToken = Settings.analytics?.ga?.token
  162. res.locals.tenderUrl = Settings.tenderUrl
  163. res.locals.sentrySrc = Settings.sentry?.src
  164. res.locals.sentryPublicDSN = Settings.sentry?.publicDSN
  165. next()
  166. webRouter.use (req, res, next) ->
  167. if req.query? and req.query.scribtex_path?
  168. res.locals.lookingForScribtex = true
  169. res.locals.scribtexPath = req.query.scribtex_path
  170. next()
  171. webRouter.use (req, res, next) ->
  172. # Clone the nav settings so they can be modified for each request
  173. res.locals.nav = {}
  174. for key, value of Settings.nav
  175. res.locals.nav[key] = _.clone(Settings.nav[key])
  176. res.locals.templates = Settings.templateLinks
  177. next()
  178. webRouter.use (req, res, next) ->
  179. SystemMessageManager.getMessages (error, messages = []) ->
  180. res.locals.systemMessages = messages
  181. next()
  182. webRouter.use (req, res, next)->
  183. res.locals.query = req.query
  184. next()
  185. webRouter.use (req, res, next)->
  186. subdomain = _.find Settings.i18n.subdomainLang, (subdomain)->
  187. subdomain.lngCode == req.showUserOtherLng and !subdomain.hide
  188. res.locals.recomendSubdomain = subdomain
  189. res.locals.currentLngCode = req.lng
  190. next()
  191. webRouter.use (req, res, next) ->
  192. if Settings.reloadModuleViewsOnEachRequest
  193. Modules.loadViewIncludes()
  194. res.locals.moduleIncludes = Modules.moduleIncludes
  195. res.locals.moduleIncludesAvailable = Modules.moduleIncludesAvailable
  196. next()