2016-08-26 18:50:37 +03:00
'use strict' ;
2018-02-20 14:51:41 -05:00
var os = require ( 'os' ) ;
2018-06-06 13:11:43 -04:00
var winston = require ( 'winston' ) ;
2018-12-07 11:29:20 -05:00
var _ = require ( 'lodash' ) ;
2020-02-06 15:52:37 -05:00
const nconf = require ( 'nconf' ) ;
2018-02-20 14:51:41 -05:00
2016-10-17 18:58:25 +03:00
var meta = require ( '../meta' ) ;
2018-12-07 11:29:20 -05:00
var languages = require ( '../languages' ) ;
2016-08-26 18:50:37 +03:00
2016-10-13 11:43:39 +02:00
module . exports = function ( middleware ) {
2018-12-17 16:23:38 -05:00
middleware . addHeaders = function addHeaders ( req , res , next ) {
2016-08-26 18:50:37 +03:00
var headers = {
2016-11-23 21:06:02 +03:00
'X-Powered-By' : encodeURI ( meta . config [ 'powered-by' ] || 'NodeBB' ) ,
'X-Frame-Options' : meta . config [ 'allow-from-uri' ] ? 'ALLOW-FROM ' + encodeURI ( meta . config [ 'allow-from-uri' ] ) : 'SAMEORIGIN' ,
'Access-Control-Allow-Methods' : encodeURI ( meta . config [ 'access-control-allow-methods' ] || '' ) ,
2017-02-17 19:31:21 -07:00
'Access-Control-Allow-Headers' : encodeURI ( meta . config [ 'access-control-allow-headers' ] || '' ) ,
2016-08-26 18:50:37 +03:00
} ;
2017-04-06 17:56:54 -04:00
if ( meta . config [ 'access-control-allow-origin' ] ) {
2018-03-20 12:24:55 -04:00
var origins = meta . config [ 'access-control-allow-origin' ] . split ( ',' ) ;
origins = origins . map ( function ( origin ) {
return origin && origin . trim ( ) ;
} ) ;
if ( origins . includes ( req . get ( 'origin' ) ) ) {
headers [ 'Access-Control-Allow-Origin' ] = encodeURI ( req . get ( 'origin' ) ) ;
}
}
2018-06-06 13:11:43 -04:00
if ( meta . config [ 'access-control-allow-origin-regex' ] ) {
var originsRegex = meta . config [ 'access-control-allow-origin-regex' ] . split ( ',' ) ;
originsRegex = originsRegex . map ( function ( origin ) {
try {
origin = new RegExp ( origin . trim ( ) ) ;
} catch ( err ) {
winston . error ( '[middleware.addHeaders] Invalid RegExp For access-control-allow-origin ' + origin ) ;
origin = null ;
}
return origin ;
} ) ;
originsRegex . forEach ( function ( regex ) {
if ( regex && regex . test ( req . get ( 'origin' ) ) ) {
headers [ 'Access-Control-Allow-Origin' ] = encodeURI ( req . get ( 'origin' ) ) ;
}
} ) ;
}
2018-03-20 12:24:55 -04:00
if ( meta . config [ 'access-control-allow-credentials' ] ) {
headers [ 'Access-Control-Allow-Credentials' ] = meta . config [ 'access-control-allow-credentials' ] ;
2017-04-06 17:56:54 -04:00
}
2018-02-20 14:51:41 -05:00
if ( process . env . NODE _ENV === 'development' ) {
headers [ 'X-Upstream-Hostname' ] = os . hostname ( ) ;
}
2020-02-18 16:06:00 -05:00
// Ensure that the session is valid. This block guards against edge-cases where the server-side session has
2020-05-15 16:41:05 -04:00
// been deleted (but client-side cookie still exists).
// req.session.flash is present if you visit register/login, so all logged-in users have it, but it is missing if your server-side session got destroyed.
if ( ! req . session . flash && ! req . session . meta && ! res . get ( 'Set-Cookie' ) ) {
2020-02-06 15:52:37 -05:00
res . clearCookie ( nconf . get ( 'sessionKey' ) , meta . configs . cookie . get ( ) ) ;
}
2016-10-17 18:58:25 +03:00
for ( var key in headers ) {
2016-11-23 21:06:02 +03:00
if ( headers . hasOwnProperty ( key ) && headers [ key ] ) {
2016-11-23 12:25:01 -05:00
res . setHeader ( key , headers [ key ] ) ;
2016-08-26 18:50:37 +03:00
}
}
next ( ) ;
} ;
2018-12-07 11:29:20 -05:00
let langs = [ ] ;
2018-12-17 16:23:38 -05:00
middleware . autoLocale = function autoLocale ( req , res , next ) {
2018-12-07 11:29:20 -05:00
if ( parseInt ( req . uid , 10 ) > 0 || ! meta . config . autoDetectLang ) {
return next ( ) ;
}
var lang = req . acceptsLanguages ( langs ) ;
if ( ! lang ) {
return next ( ) ;
}
req . query . lang = lang ;
next ( ) ;
} ;
languages . listCodes ( function ( err , codes ) {
if ( err ) {
winston . error ( '[middleware/autoLocale] Could not retrieve languages codes list!' ) ;
codes = [ ] ;
}
winston . verbose ( '[middleware/autoLocale] Retrieves languages list for middleware' ) ;
var defaultLang = meta . config . defaultLang || 'en-GB' ;
langs = _ . uniq ( [ defaultLang , ... codes ] ) ;
} ) ;
2016-08-26 18:50:37 +03:00
} ;