| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 16:26:27 +03:00
										 |  |  | var db = require('../database'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var navigation = require('../navigation'); | 
					
						
							| 
									
										
										
										
											2017-06-26 17:27:48 -04:00
										 |  |  | var translator = require('../translator'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | var controllers = { | 
					
						
							|  |  |  | 	api: require('../controllers/api'), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 	helpers: require('../controllers/helpers'), | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (middleware) { | 
					
						
							|  |  |  | 	middleware.buildHeader = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		res.locals.renderHeader = true; | 
					
						
							|  |  |  | 		res.locals.isAPI = false; | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 				middleware.applyCSRF(req, res, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 				async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					config: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 						controllers.api.getConfig(req, res, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					plugins: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 						plugins.fireHook('filter:middleware.buildHeader', { req: req, locals: res.locals }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (results, next) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				res.locals.config = results.config; | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 		], next); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	middleware.renderHeader = function (req, res, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		var registrationType = meta.config.registrationType || 'normal'; | 
					
						
							| 
									
										
										
										
											2017-04-24 13:30:01 -04:00
										 |  |  | 		res.locals.config = res.locals.config || {}; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		var templateValues = { | 
					
						
							|  |  |  | 			title: meta.config.title || '', | 
					
						
							|  |  |  | 			description: meta.config.description || '', | 
					
						
							| 
									
										
										
										
											2017-01-14 22:36:10 -07:00
										 |  |  | 			'cache-buster': meta.config['cache-buster'] || '', | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			'brand:logo': meta.config['brand:logo'] || '', | 
					
						
							|  |  |  | 			'brand:logo:url': meta.config['brand:logo:url'] || '', | 
					
						
							|  |  |  | 			'brand:logo:alt': meta.config['brand:logo:alt'] || '', | 
					
						
							| 
									
										
										
										
											2016-10-13 11:42:29 +02:00
										 |  |  | 			'brand:logo:display': meta.config['brand:logo'] ? '' : 'hide', | 
					
						
							| 
									
										
										
										
											2016-07-25 09:56:29 -05:00
										 |  |  | 			allowRegistration: registrationType === 'normal' || registrationType === 'admin-approval' || registrationType === 'admin-approval-ip', | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			searchEnabled: plugins.hasListeners('filter:search.query'), | 
					
						
							|  |  |  | 			config: res.locals.config, | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 			relative_path: nconf.get('relative_path'), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			bodyClass: data.bodyClass, | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		templateValues.configJSON = JSON.stringify(res.locals.config); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					scripts: function (next) { | 
					
						
							|  |  |  | 						plugins.fireHook('filter:scripts.get', [], next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isAdmin: function (next) { | 
					
						
							|  |  |  | 						user.isAdministrator(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isGlobalMod: function (next) { | 
					
						
							|  |  |  | 						user.isGlobalModerator(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isModerator: function (next) { | 
					
						
							|  |  |  | 						user.isModeratorOfAnyCategory(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					user: function (next) { | 
					
						
							|  |  |  | 						var userData = { | 
					
						
							|  |  |  | 							uid: 0, | 
					
						
							|  |  |  | 							username: '[[global:guest]]', | 
					
						
							|  |  |  | 							userslug: '', | 
					
						
							|  |  |  | 							email: '', | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 							picture: user.getDefaultAvatar(), | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 							status: 'offline', | 
					
						
							|  |  |  | 							reputation: 0, | 
					
						
							| 
									
										
										
										
											2017-03-11 16:43:19 +03:00
										 |  |  | 							'email:confirmed': 0, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 						if (req.uid) { | 
					
						
							|  |  |  | 							user.getUserFields(req.uid, Object.keys(userData), next); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							next(null, userData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isEmailConfirmSent: function (next) { | 
					
						
							|  |  |  | 						if (!meta.config.requireEmailConfirmation || !req.uid) { | 
					
						
							|  |  |  | 							return next(null, false); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						db.get('uid:' + req.uid + ':confirm:email:sent', next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-06-26 17:27:48 -04:00
										 |  |  | 					languageDirection: function (next) { | 
					
						
							|  |  |  | 						translator.translate('[[language:dir]]', res.locals.config.userLang, function (translated) { | 
					
						
							|  |  |  | 							next(null, translated); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 					navigation: async.apply(navigation.get), | 
					
						
							| 
									
										
										
										
											2017-04-22 19:07:57 -04:00
										 |  |  | 					tags: async.apply(meta.tags.parse, req, res.locals.metaTags, res.locals.linkTags), | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 					banned: async.apply(user.isBanned, req.uid), | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 					banReason: async.apply(user.getBannedReason, req.uid), | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 			function (results, next) { | 
					
						
							|  |  |  | 				if (results.banned) { | 
					
						
							|  |  |  | 					req.logout(); | 
					
						
							| 
									
										
										
										
											2017-05-05 19:31:49 -04:00
										 |  |  | 					return res.redirect('/'); | 
					
						
							| 
									
										
										
										
											2016-07-29 16:26:27 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-01-18 15:32:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				results.user.isAdmin = results.isAdmin; | 
					
						
							|  |  |  | 				results.user.isGlobalMod = results.isGlobalMod; | 
					
						
							|  |  |  | 				results.user.isMod = !!results.isModerator; | 
					
						
							|  |  |  | 				results.user.uid = parseInt(results.user.uid, 10); | 
					
						
							|  |  |  | 				results.user.email = String(results.user.email).replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | 
					
						
							|  |  |  | 				results.user['email:confirmed'] = parseInt(results.user['email:confirmed'], 10) === 1; | 
					
						
							|  |  |  | 				results.user.isEmailConfirmSent = !!results.isEmailConfirmSent; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 00:46:11 +03:00
										 |  |  | 				setBootswatchCSS(templateValues, res.locals.config); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.browserTitle = controllers.helpers.buildTitle(data.title); | 
					
						
							|  |  |  | 				templateValues.navigation = results.navigation; | 
					
						
							|  |  |  | 				templateValues.metaTags = results.tags.meta; | 
					
						
							|  |  |  | 				templateValues.linkTags = results.tags.link; | 
					
						
							|  |  |  | 				templateValues.isAdmin = results.user.isAdmin; | 
					
						
							|  |  |  | 				templateValues.isGlobalMod = results.user.isGlobalMod; | 
					
						
							|  |  |  | 				templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod; | 
					
						
							|  |  |  | 				templateValues.user = results.user; | 
					
						
							|  |  |  | 				templateValues.userJSON = JSON.stringify(results.user); | 
					
						
							|  |  |  | 				templateValues.useCustomCSS = parseInt(meta.config.useCustomCSS, 10) === 1 && meta.config.customCSS; | 
					
						
							|  |  |  | 				templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : ''; | 
					
						
							|  |  |  | 				templateValues.useCustomJS = parseInt(meta.config.useCustomJS, 10) === 1; | 
					
						
							|  |  |  | 				templateValues.customJS = templateValues.useCustomJS ? meta.config.customJS : ''; | 
					
						
							|  |  |  | 				templateValues.maintenanceHeader = parseInt(meta.config.maintenanceMode, 10) === 1 && !results.isAdmin; | 
					
						
							|  |  |  | 				templateValues.defaultLang = meta.config.defaultLang || 'en-GB'; | 
					
						
							| 
									
										
										
										
											2017-04-21 23:55:58 -06:00
										 |  |  | 				templateValues.userLang = res.locals.config.userLang; | 
					
						
							| 
									
										
										
										
											2017-06-26 17:27:48 -04:00
										 |  |  | 				templateValues.languageDirection = results.languageDirection; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.privateUserInfo = parseInt(meta.config.privateUserInfo, 10) === 1; | 
					
						
							|  |  |  | 				templateValues.privateTagListing = parseInt(meta.config.privateTagListing, 10) === 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 				templateValues.template = { name: res.locals.template }; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.template[res.locals.template] = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				templateValues.scripts = results.scripts.map(function (script) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 					return { src: script }; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (req.route && req.route.path === '/') { | 
					
						
							|  |  |  | 					modifyTitle(templateValues); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				plugins.fireHook('filter:middleware.renderHeader', { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 					req: req, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 					res: res, | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 					templateValues: templateValues, | 
					
						
							|  |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (data, next) { | 
					
						
							|  |  |  | 				req.app.render('header', data.templateValues, next); | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	middleware.renderFooter = function (req, res, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				plugins.fireHook('filter:middleware.renderFooter', { | 
					
						
							|  |  |  | 					req: req, | 
					
						
							|  |  |  | 					res: res, | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 					templateValues: data, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (data, next) { | 
					
						
							|  |  |  | 				req.app.render('footer', data.templateValues, next); | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	function modifyTitle(obj) { | 
					
						
							|  |  |  | 		var title = controllers.helpers.buildTitle('[[pages:home]]'); | 
					
						
							|  |  |  | 		obj.browserTitle = title; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (obj.metaTags) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			obj.metaTags.forEach(function (tag, i) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				if (tag.property === 'og:title') { | 
					
						
							|  |  |  | 					obj.metaTags[i].content = title; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return title; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 00:46:11 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	function setBootswatchCSS(obj, config) { | 
					
						
							| 
									
										
										
										
											2017-03-07 13:09:14 +03:00
										 |  |  | 		if (config && config.bootswatchSkin !== 'noskin') { | 
					
						
							| 
									
										
										
										
											2017-03-05 00:46:11 +03:00
										 |  |  | 			var skinToUse = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (parseInt(meta.config.disableCustomUserSkins, 10) !== 1) { | 
					
						
							|  |  |  | 				skinToUse = config.bootswatchSkin; | 
					
						
							| 
									
										
										
										
											2017-03-07 13:09:14 +03:00
										 |  |  | 			} else if (meta.config.bootswatchSkin) { | 
					
						
							| 
									
										
										
										
											2017-03-05 00:46:11 +03:00
										 |  |  | 				skinToUse = meta.config.bootswatchSkin; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (skinToUse) { | 
					
						
							|  |  |  | 				obj.bootswatchCSS = '//maxcdn.bootstrapcdn.com/bootswatch/latest/' + skinToUse + '/bootstrap.min.css'; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 |