2014-02-12 10:23:40 +00:00
|
|
|
logger = require 'logger-sharelatex'
|
|
|
|
fs = require 'fs'
|
|
|
|
crypto = require 'crypto'
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
SubscriptionFormatters = require('../Features/Subscription/SubscriptionFormatters')
|
|
|
|
querystring = require('querystring')
|
2014-07-24 12:24:08 +00:00
|
|
|
SystemMessageManager = require("../Features/SystemMessages/SystemMessageManager")
|
2014-08-13 11:31:14 +00:00
|
|
|
_ = require("underscore")
|
2014-09-08 14:40:46 +00:00
|
|
|
Modules = require "./Modules"
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
fingerprints = {}
|
|
|
|
Path = require 'path'
|
|
|
|
jsPath =
|
|
|
|
if Settings.useMinifiedJs
|
|
|
|
"/minjs/"
|
|
|
|
else
|
|
|
|
"/js/"
|
|
|
|
|
|
|
|
logger.log "Generating file fingerprints..."
|
|
|
|
for path in [
|
2014-12-01 16:48:40 +00:00
|
|
|
"#{jsPath}libs/require.js",
|
|
|
|
"#{jsPath}ide.js",
|
|
|
|
"#{jsPath}main.js",
|
2014-07-18 11:08:35 +00:00
|
|
|
"#{jsPath}libs.js",
|
|
|
|
"#{jsPath}ace/ace.js",
|
2016-02-03 15:07:06 +00:00
|
|
|
"#{jsPath}libs/pdfjs-1.3.91/pdf.js",
|
|
|
|
"#{jsPath}libs/pdfjs-1.3.91/pdf.worker.js",
|
|
|
|
"#{jsPath}libs/pdfjs-1.3.91/compatibility.js",
|
2014-12-01 16:48:40 +00:00
|
|
|
"/stylesheets/style.css"
|
2014-02-12 10:23:40 +00:00
|
|
|
]
|
|
|
|
filePath = Path.join __dirname, "../../../", "public#{path}"
|
2014-03-13 12:38:16 +00:00
|
|
|
exists = fs.existsSync filePath
|
|
|
|
if exists
|
|
|
|
content = fs.readFileSync filePath
|
|
|
|
hash = crypto.createHash("md5").update(content).digest("hex")
|
|
|
|
logger.log "#{filePath}: #{hash}"
|
|
|
|
fingerprints[path] = hash
|
|
|
|
else
|
|
|
|
logger.log filePath:filePath, "file does not exist for fingerprints"
|
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2015-06-30 13:38:32 +00:00
|
|
|
module.exports = (app, webRouter, apiRouter)->
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.session = req.session
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.jsPath = jsPath
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.settings = Settings
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2015-03-09 12:14:30 +00:00
|
|
|
res.locals.translate = (key, vars = {}) ->
|
|
|
|
vars.appName = Settings.appName
|
|
|
|
req.i18n.translate(key, vars)
|
2014-08-06 14:05:13 +00:00
|
|
|
res.locals.currentUrl = req.originalUrl
|
2014-03-24 17:18:58 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.getSiteHost = ->
|
|
|
|
Settings.siteUrl.substring(Settings.siteUrl.indexOf("//")+2)
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-04-07 19:46:58 +00:00
|
|
|
res.locals.formatProjectPublicAccessLevel = (privilegeLevel)->
|
|
|
|
formatedPrivileges = private:"Private", readOnly:"Public: Read Only", readAndWrite:"Public: Read and Write"
|
|
|
|
return formatedPrivileges[privilegeLevel] || "Private"
|
2014-02-12 10:23:40 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.buildReferalUrl = (referal_medium) ->
|
|
|
|
url = Settings.siteUrl
|
|
|
|
if req.session? and req.session.user? and req.session.user.referal_id?
|
|
|
|
url+="?r=#{req.session.user.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
|
|
|
|
return url
|
|
|
|
res.locals.getReferalId = ->
|
|
|
|
if req.session? and req.session.user? and req.session.user.referal_id
|
|
|
|
return req.session.user.referal_id
|
|
|
|
res.locals.getReferalTagLine = ->
|
|
|
|
tagLines = [
|
|
|
|
"Roar!"
|
|
|
|
"Shout about us!"
|
|
|
|
"Please recommend us"
|
|
|
|
"Tell the world!"
|
|
|
|
"Thanks for using ShareLaTeX"
|
|
|
|
]
|
|
|
|
return tagLines[Math.floor(Math.random()*tagLines.length)]
|
|
|
|
res.locals.getRedirAsQueryString = ->
|
|
|
|
if req.query.redir?
|
|
|
|
return "?#{querystring.stringify({redir:req.query.redir})}"
|
|
|
|
return ""
|
2015-11-17 15:54:59 +00:00
|
|
|
|
|
|
|
res.locals.getLoggedInUserId = ->
|
|
|
|
return req.session.user?._id
|
2014-02-12 10:23:40 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 13:38:32 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
|
|
|
res.locals.csrfToken = req?.csrfToken()
|
2014-02-12 10:23:40 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
2015-04-30 10:59:44 +00:00
|
|
|
res.locals.getReqQueryParam = (field)->
|
|
|
|
return req.query?[field]
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.fingerprint = (path) ->
|
|
|
|
if fingerprints[path]?
|
|
|
|
return fingerprints[path]
|
|
|
|
else
|
|
|
|
logger.err "No fingerprint for file: #{path}"
|
|
|
|
return ""
|
|
|
|
next()
|
2014-06-20 20:35:42 +00:00
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
res.locals.formatPrice = SubscriptionFormatters.formatPrice
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2015-02-24 13:39:30 +00:00
|
|
|
res.locals.externalAuthenticationSystemUsed = ->
|
|
|
|
Settings.ldap?
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-02-12 10:23:40 +00:00
|
|
|
if req.session.user?
|
|
|
|
res.locals.user =
|
|
|
|
email: req.session.user.email
|
|
|
|
first_name: req.session.user.first_name
|
|
|
|
last_name: req.session.user.last_name
|
|
|
|
if req.session.justRegistered
|
|
|
|
res.locals.justRegistered = true
|
|
|
|
delete req.session.justRegistered
|
|
|
|
if req.session.justLoggedIn
|
|
|
|
res.locals.justLoggedIn = true
|
|
|
|
delete req.session.justLoggedIn
|
|
|
|
res.locals.gaToken = Settings.analytics?.ga?.token
|
|
|
|
res.locals.tenderUrl = Settings.tenderUrl
|
2014-12-12 13:58:07 +00:00
|
|
|
res.locals.sentrySrc = Settings.sentry?.src
|
|
|
|
res.locals.sentryPublicDSN = Settings.sentry?.publicDSN
|
2014-02-12 10:23:40 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
if req.query? and req.query.scribtex_path?
|
|
|
|
res.locals.lookingForScribtex = true
|
|
|
|
res.locals.scribtexPath = req.query.scribtex_path
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
2015-11-05 16:52:50 +00:00
|
|
|
# Clone the nav settings so they can be modified for each request
|
|
|
|
res.locals.nav = {}
|
|
|
|
for key, value of Settings.nav
|
|
|
|
res.locals.nav[key] = _.clone(Settings.nav[key])
|
2014-08-20 13:47:27 +00:00
|
|
|
res.locals.templates = Settings.templateLinks
|
2014-06-20 20:35:42 +00:00
|
|
|
next()
|
2014-07-24 12:24:08 +00:00
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
2014-07-24 12:24:08 +00:00
|
|
|
SystemMessageManager.getMessages (error, messages = []) ->
|
|
|
|
res.locals.systemMessages = messages
|
|
|
|
next()
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2015-06-01 11:43:42 +00:00
|
|
|
res.locals.query = req.query
|
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next)->
|
2014-08-13 11:31:14 +00:00
|
|
|
subdomain = _.find Settings.i18n.subdomainLang, (subdomain)->
|
2014-08-21 16:58:25 +00:00
|
|
|
subdomain.lngCode == req.showUserOtherLng and !subdomain.hide
|
2014-08-13 11:31:14 +00:00
|
|
|
res.locals.recomendSubdomain = subdomain
|
2014-08-13 22:06:15 +00:00
|
|
|
res.locals.currentLngCode = req.lng
|
2014-08-13 11:31:14 +00:00
|
|
|
next()
|
|
|
|
|
2015-06-30 14:36:39 +00:00
|
|
|
webRouter.use (req, res, next) ->
|
2014-10-08 11:39:36 +00:00
|
|
|
if Settings.reloadModuleViewsOnEachRequest
|
2014-09-08 14:40:46 +00:00
|
|
|
Modules.loadViewIncludes()
|
|
|
|
res.locals.moduleIncludes = Modules.moduleIncludes
|
2015-02-05 18:20:25 +00:00
|
|
|
res.locals.moduleIncludesAvailable = Modules.moduleIncludesAvailable
|
2014-09-08 14:40:46 +00:00
|
|
|
next()
|
|
|
|
|
2015-11-17 15:54:59 +00:00
|
|
|
|