| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var controllers = { | 
					
						
							|  |  |  | 	api: require('../controllers/api'), | 
					
						
							|  |  |  | 	helpers: require('../controllers/helpers') | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (middleware) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 						plugins.fireHook('filter:middleware.buildHeader', {req: req, locals: res.locals}, next); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, 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(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], 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'; | 
					
						
							|  |  |  | 		var templateValues = { | 
					
						
							|  |  |  | 			bootswatchCSS: meta.config['theme:src'], | 
					
						
							|  |  |  | 			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'), | 
					
						
							|  |  |  | 			bodyClass: data.bodyClass | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		templateValues.configJSON = JSON.stringify(res.locals.config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			scripts: function (next) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:24:42 +02:00
										 |  |  | 				plugins.fireHook('filter:scripts.get', [], next); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:32:49 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			isAdmin: function (next) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				user.isAdministrator(req.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			isGlobalMod: function (next) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:24:42 +02:00
										 |  |  | 				user.isGlobalModerator(req.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 14:43:28 -04:00
										 |  |  | 			isModerator: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-13 14:13:46 -04:00
										 |  |  | 				user.isModeratorOfAnyCategory(req.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			user: function (next) { | 
					
						
							| 
									
										
										
										
											2016-05-25 20:17:02 +03:00
										 |  |  | 				var userData = { | 
					
						
							|  |  |  | 					uid: 0, | 
					
						
							|  |  |  | 					username: '[[global:guest]]', | 
					
						
							|  |  |  | 					userslug: '', | 
					
						
							|  |  |  | 					email: '', | 
					
						
							|  |  |  | 					picture: meta.config.defaultAvatar, | 
					
						
							|  |  |  | 					status: 'offline', | 
					
						
							|  |  |  | 					reputation: 0, | 
					
						
							|  |  |  | 					'email:confirmed': false | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				if (req.uid) { | 
					
						
							| 
									
										
										
										
											2016-05-25 20:17:02 +03:00
										 |  |  | 					user.getUserFields(req.uid, Object.keys(userData), next); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2016-05-25 20:17:02 +03:00
										 |  |  | 					next(null, userData); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			isEmailConfirmSent: function (next) { | 
					
						
							| 
									
										
										
										
											2016-07-29 16:26:27 +03:00
										 |  |  | 				if (!meta.config.requireEmailConfirmation || !req.uid) { | 
					
						
							|  |  |  | 					return next(null, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				db.get('uid:' + req.uid + ':confirm:email:sent', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			navigation: async.apply(navigation.get), | 
					
						
							| 
									
										
										
										
											2016-10-27 14:14:54 -04:00
										 |  |  | 			tags: async.apply(meta.tags.parse, res.locals.metaTags, res.locals.linkTags), | 
					
						
							| 
									
										
										
										
											2016-10-31 11:35:11 -04:00
										 |  |  | 			banned: async.apply(user.isBanned, req.uid), | 
					
						
							|  |  |  | 			banReason: async.apply(user.getBannedReason, req.uid) | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 09:34:59 -04:00
										 |  |  | 			if (results.banned) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				req.logout(); | 
					
						
							| 
									
										
										
										
											2016-11-10 15:59:08 +03:00
										 |  |  | 				return res.redirect('/?banned=' + (results.banReason || 'no-reason')); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-11 23:20:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			results.user.isAdmin = results.isAdmin; | 
					
						
							| 
									
										
										
										
											2016-03-11 13:24:42 +02:00
										 |  |  | 			results.user.isGlobalMod = results.isGlobalMod; | 
					
						
							| 
									
										
										
										
											2016-10-13 14:13:46 -04:00
										 |  |  | 			results.user.isMod = !!results.isModerator; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			results.user.uid = parseInt(results.user.uid, 10); | 
					
						
							| 
									
										
										
										
											2016-09-30 18:42:19 +03:00
										 |  |  | 			results.user.email = String(results.user.email).replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			results.user['email:confirmed'] = parseInt(results.user['email:confirmed'], 10) === 1; | 
					
						
							| 
									
										
										
										
											2016-07-29 16:26:27 +03:00
										 |  |  | 			results.user.isEmailConfirmSent = !!results.isEmailConfirmSent; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:41:45 +03:00
										 |  |  | 			if (res.locals.config && parseInt(meta.config.disableCustomUserSkins, 10) !== 1 && res.locals.config.bootswatchSkin !== 'default') { | 
					
						
							| 
									
										
										
										
											2015-10-23 14:07:09 -04:00
										 |  |  | 				templateValues.bootswatchCSS = '//maxcdn.bootstrapcdn.com/bootswatch/latest/' + res.locals.config.bootswatchSkin + '/bootstrap.min.css'; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:20:57 -04:00
										 |  |  | 			templateValues.browserTitle = controllers.helpers.buildTitle(data.title); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			templateValues.navigation = results.navigation; | 
					
						
							|  |  |  | 			templateValues.metaTags = results.tags.meta; | 
					
						
							|  |  |  | 			templateValues.linkTags = results.tags.link; | 
					
						
							|  |  |  | 			templateValues.isAdmin = results.user.isAdmin; | 
					
						
							| 
									
										
										
										
											2016-03-11 13:24:42 +02:00
										 |  |  | 			templateValues.isGlobalMod = results.user.isGlobalMod; | 
					
						
							| 
									
										
										
										
											2016-10-13 14:13:46 -04:00
										 |  |  | 			templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			templateValues.user = results.user; | 
					
						
							|  |  |  | 			templateValues.userJSON = JSON.stringify(results.user); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:20:57 -04:00
										 |  |  | 			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 : ''; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			templateValues.maintenanceHeader = parseInt(meta.config.maintenanceMode, 10) === 1 && !results.isAdmin; | 
					
						
							| 
									
										
										
										
											2016-11-23 09:50:49 -07:00
										 |  |  | 			templateValues.defaultLang = meta.config.defaultLang || 'en-GB'; | 
					
						
							| 
									
										
										
										
											2016-02-10 12:53:39 +02:00
										 |  |  | 			templateValues.privateUserInfo = parseInt(meta.config.privateUserInfo, 10) === 1; | 
					
						
							|  |  |  | 			templateValues.privateTagListing = parseInt(meta.config.privateTagListing, 10) === 1; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			templateValues.template = {name: res.locals.template}; | 
					
						
							|  |  |  | 			templateValues.template[res.locals.template] = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			templateValues.scripts = results.scripts.map(function (script) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:24:42 +02:00
										 |  |  | 				return {src: script}; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:32:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			if (req.route && req.route.path === '/') { | 
					
						
							|  |  |  | 				modifyTitle(templateValues); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			plugins.fireHook('filter:middleware.renderHeader', {templateValues: templateValues, req: req, res: res}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 18:50:37 +03:00
										 |  |  | 				req.app.render('header', data.templateValues, callback); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	middleware.renderFooter = function (req, res, data, callback) { | 
					
						
							|  |  |  | 		plugins.fireHook('filter:middleware.renderFooter', {templateValues: data, req: req, res: res}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			req.app.render('footer', data.templateValues, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |