| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- logger = require 'logger-sharelatex'
- fs = require 'fs'
- crypto = require 'crypto'
- Settings = require('settings-sharelatex')
- SubscriptionFormatters = require('../Features/Subscription/SubscriptionFormatters')
- querystring = require('querystring')
- SystemMessageManager = require("../Features/SystemMessages/SystemMessageManager")
- AuthenticationController = require("../Features/Authentication/AuthenticationController")
- _ = require("underscore")
- async = require("async")
- Modules = require "./Modules"
- Url = require "url"
- PackageVersions = require "./PackageVersions"
- htmlEncoder = new require("node-html-encoder").Encoder("numerical")
- hashedFiles = {}
- Path = require 'path'
- Features = require "./Features"
- jsPath =
- if Settings.useMinifiedJs
- "/minjs/"
- else
- "/js/"
- ace = PackageVersions.lib('ace')
- pdfjs = PackageVersions.lib('pdfjs')
- fineuploader = PackageVersions.lib('fineuploader')
- getFileContent = (filePath)->
- filePath = Path.join __dirname, "../../../", "public#{filePath}"
- exists = fs.existsSync filePath
- if exists
- content = fs.readFileSync filePath, "UTF-8"
- return content
- else
- logger.log filePath:filePath, "file does not exist for hashing"
- return ""
- pathList = [
- "#{jsPath}libs/require.js"
- "#{jsPath}ide.js"
- "#{jsPath}main.js"
- "#{jsPath}libraries.js"
- "/stylesheets/style.css"
- "/stylesheets/ol-style.css"
- ]
- if !Settings.useMinifiedJs
- logger.log "not using minified JS, not hashing static files"
- else
- logger.log "Generating file hashes..."
- for path in pathList
- content = getFileContent(path)
- hash = crypto.createHash("md5").update(content).digest("hex")
-
- splitPath = path.split("/")
- filenameSplit = splitPath.pop().split(".")
- filenameSplit.splice(filenameSplit.length-1, 0, hash)
- splitPath.push(filenameSplit.join("."))
- hashPath = splitPath.join("/")
- hashedFiles[path] = hashPath
- fsHashPath = Path.join __dirname, "../../../", "public#{hashPath}"
- fs.writeFileSync(fsHashPath, content)
- logger.log "Finished hashing static content"
- cdnAvailable = Settings.cdn?.web?.host?
- darkCdnAvailable = Settings.cdn?.web?.darkHost?
- module.exports = (app, webRouter, privateApiRouter, publicApiRouter)->
- webRouter.use (req, res, next)->
- res.locals.session = req.session
- next()
- addSetContentDisposition = (req, res, next) ->
- res.setContentDisposition = (type, opts) ->
- directives = for k, v of opts
- "#{k}=\"#{encodeURIComponent(v)}\""
- contentDispositionValue = "#{type}; #{directives.join('; ')}"
- res.setHeader(
- 'Content-Disposition',
- contentDispositionValue
- )
- next()
- webRouter.use addSetContentDisposition
- privateApiRouter.use addSetContentDisposition
- publicApiRouter.use addSetContentDisposition
- webRouter.use (req, res, next)->
- req.externalAuthenticationSystemUsed = Features.externalAuthenticationSystemUsed
- res.locals.externalAuthenticationSystemUsed = Features.externalAuthenticationSystemUsed
- req.hasFeature = res.locals.hasFeature = Features.hasFeature
- next()
- webRouter.use (req, res, next)->
- cdnBlocked = req.query.nocdn == 'true' or req.session.cdnBlocked
- user_id = AuthenticationController.getLoggedInUserId(req)
- if cdnBlocked and !req.session.cdnBlocked?
- logger.log user_id:user_id, ip:req?.ip, "cdnBlocked for user, not using it and turning it off for future requets"
- req.session.cdnBlocked = true
- isDark = req.headers?.host?.slice(0,4)?.toLowerCase() == "dark"
- isSmoke = req.headers?.host?.slice(0,5)?.toLowerCase() == "smoke"
- isLive = !isDark and !isSmoke
- if cdnAvailable and isLive and !cdnBlocked
- staticFilesBase = Settings.cdn?.web?.host
- else if darkCdnAvailable and isDark
- staticFilesBase = Settings.cdn?.web?.darkHost
- else
- staticFilesBase = ""
- res.locals.jsPath = jsPath
- res.locals.fullJsPath = Url.resolve(staticFilesBase, jsPath)
- res.locals.lib = PackageVersions.lib
- res.locals.buildJsPath = (jsFile, opts = {})->
- path = Path.join(jsPath, jsFile)
- if opts.hashedPath && hashedFiles[path]?
- path = hashedFiles[path]
- if !opts.qs?
- opts.qs = {}
- if opts.cdn != false
- path = Url.resolve(staticFilesBase, path)
- qs = querystring.stringify(opts.qs)
- if opts.removeExtension == true
- path = path.slice(0,-3)
-
- if qs? and qs.length > 0
- path = path + "?" + qs
- return path
- res.locals.buildCssPath = (cssFile, opts)->
- path = Path.join("/stylesheets/", cssFile)
- if opts?.hashedPath && hashedFiles[path]?
- hashedPath = hashedFiles[path]
- return Url.resolve(staticFilesBase, hashedPath)
- return Url.resolve(staticFilesBase, path)
- res.locals.buildImgPath = (imgFile)->
- path = Path.join("/img/", imgFile)
- return Url.resolve(staticFilesBase, path)
- next()
- webRouter.use (req, res, next)->
- res.locals.settings = Settings
- next()
- webRouter.use (req, res, next)->
- res.locals.translate = (key, vars = {}, htmlEncode = false) ->
- vars.appName = Settings.appName
- str = req.i18n.translate(key, vars)
- if htmlEncode then htmlEncoder.htmlEncode(str) else str
- # Don't include the query string parameters, otherwise Google
- # treats ?nocdn=true as the canonical version
- res.locals.currentUrl = Url.parse(req.originalUrl).pathname
- next()
- webRouter.use (req, res, next)->
- res.locals.getSiteHost = ->
- Settings.siteUrl.substring(Settings.siteUrl.indexOf("//")+2)
- next()
- webRouter.use (req, res, next) ->
- res.locals.getUserEmail = ->
- user = AuthenticationController.getSessionUser(req)
- email = user?.email or ""
- return email
- next()
- webRouter.use (req, res, next)->
- res.locals.formatProjectPublicAccessLevel = (privilegeLevel)->
- formatedPrivileges = private:"Private", readOnly:"Public: Read Only", readAndWrite:"Public: Read and Write"
- return formatedPrivileges[privilegeLevel] || "Private"
- next()
- webRouter.use (req, res, next)->
- res.locals.buildReferalUrl = (referal_medium) ->
- url = Settings.siteUrl
- currentUser = AuthenticationController.getSessionUser(req)
- if currentUser? and currentUser?.referal_id?
- url+="?r=#{currentUser.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
- return url
- res.locals.getReferalId = ->
- currentUser = AuthenticationController.getSessionUser(req)
- if currentUser? and currentUser?.referal_id?
- return currentUser.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 ""
- res.locals.getLoggedInUserId = ->
- return AuthenticationController.getLoggedInUserId(req)
- res.locals.isUserLoggedIn = ->
- return AuthenticationController.isUserLoggedIn(req)
- res.locals.getSessionUser = ->
- return AuthenticationController.getSessionUser(req)
- next()
- webRouter.use (req, res, next) ->
- res.locals.csrfToken = req?.csrfToken()
- next()
- webRouter.use (req, res, next) ->
- res.locals.getReqQueryParam = (field)->
- return req.query?[field]
- next()
- webRouter.use (req, res, next)->
- res.locals.formatPrice = SubscriptionFormatters.formatPrice
- next()
- webRouter.use (req, res, next)->
- currentUser = AuthenticationController.getSessionUser(req)
- if currentUser?
- res.locals.user =
- email: currentUser.email
- first_name: currentUser.first_name
- last_name: currentUser.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
- res.locals.sentrySrc = Settings.sentry?.src
- res.locals.sentryPublicDSN = Settings.sentry?.publicDSN
- next()
- webRouter.use (req, res, next) ->
- if req.query? and req.query.scribtex_path?
- res.locals.lookingForScribtex = true
- res.locals.scribtexPath = req.query.scribtex_path
- next()
- webRouter.use (req, res, next) ->
- # 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])
- res.locals.templates = Settings.templateLinks
- if res.locals.nav.header
- console.error {}, "The `nav.header` setting is no longer supported, use `nav.header_extras` instead"
- next()
- webRouter.use (req, res, next) ->
- SystemMessageManager.getMessages (error, messages = []) ->
- res.locals.systemMessages = messages
- next()
- webRouter.use (req, res, next)->
- res.locals.query = req.query
- next()
- webRouter.use (req, res, next)->
- subdomain = _.find Settings.i18n.subdomainLang, (subdomain)->
- subdomain.lngCode == req.showUserOtherLng and !subdomain.hide
- res.locals.recomendSubdomain = subdomain
- res.locals.currentLngCode = req.lng
- next()
- webRouter.use (req, res, next) ->
- if Settings.reloadModuleViewsOnEachRequest
- Modules.loadViewIncludes()
- res.locals.moduleIncludes = Modules.moduleIncludes
- res.locals.moduleIncludesAvailable = Modules.moduleIncludesAvailable
- next()
- webRouter.use (req, res, next) ->
- isOl = (Settings.brandPrefix == 'ol-')
- res.locals.uiConfig =
- defaultResizerSizeOpen : if isOl then 2 else 24
- defaultResizerSizeClosed : if isOl then 2 else 24
- eastResizerCursor : if isOl then "ew-resize" else null
- westResizerCursor : if isOl then "ew-resize" else null
- chatResizerSizeOpen : if isOl then 2 else 12
- chatResizerSizeClosed : 0
- next()
|