| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2017-10-25 09:08:03 -04:00
										 |  |  | var jsesc = require('jsesc'); | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 16:26:27 +03:00
										 |  |  | var db = require('../database'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | var user = require('../user'); | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | var topics = require('../topics'); | 
					
						
							|  |  |  | var messaging = require('../messaging'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var navigation = require('../navigation'); | 
					
						
							| 
									
										
										
										
											2017-06-26 17:27:48 -04:00
										 |  |  | var translator = require('../translator'); | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | var privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2019-01-17 12:25:53 -05:00
										 |  |  | var languages = require('../languages'); | 
					
						
							| 
									
										
										
										
											2017-07-06 14:09:59 -04:00
										 |  |  | var utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2018-12-17 17:11:35 -05:00
										 |  |  | 	middleware.buildHeader = function buildHeader(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) { | 
					
						
							| 
									
										
										
										
											2018-12-15 16:38:30 -05:00
										 |  |  | 				if (req.uid >= 0) { | 
					
						
							| 
									
										
										
										
											2018-12-14 23:38:05 -05:00
										 |  |  | 					middleware.applyCSRF(req, res, next); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					setImmediate(next); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2018-12-21 11:45:29 -05:00
										 |  |  | 				// Return no arguments
 | 
					
						
							|  |  |  | 				setImmediate(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
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 17:11:35 -05:00
										 |  |  | 	middleware.generateHeader = function generateHeader(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 || '', | 
					
						
							| 
									
										
										
										
											2017-08-22 10:28:35 -04:00
										 |  |  | 			'title:url': meta.config['title:url'] || '', | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			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
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 09:08:03 -04:00
										 |  |  | 		templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					isAdmin: function (next) { | 
					
						
							|  |  |  | 						user.isAdministrator(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isGlobalMod: function (next) { | 
					
						
							|  |  |  | 						user.isGlobalModerator(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isModerator: function (next) { | 
					
						
							|  |  |  | 						user.isModeratorOfAnyCategory(req.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 					privileges: function (next) { | 
					
						
							|  |  |  | 						privileges.global.get(req.uid, next); | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 					user: function (next) { | 
					
						
							| 
									
										
										
										
											2018-11-22 21:57:56 -05:00
										 |  |  | 						user.getUserData(req.uid, next); | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 					isEmailConfirmSent: function (next) { | 
					
						
							| 
									
										
										
										
											2018-11-22 21:57:56 -05:00
										 |  |  | 						if (!meta.config.requireEmailConfirmation || req.uid <= 0) { | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 							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-07-31 16:48:28 -04:00
										 |  |  | 					browserTitle: function (next) { | 
					
						
							|  |  |  | 						translator.translate(controllers.helpers.buildTitle(translator.unescape(data.title)), function (translated) { | 
					
						
							|  |  |  | 							next(null, translated); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2018-11-10 20:51:07 -05:00
										 |  |  | 					navigation: async.apply(navigation.get, req.uid), | 
					
						
							| 
									
										
										
										
											2017-07-11 13:44:51 -04:00
										 |  |  | 					tags: async.apply(meta.tags.parse, req, data, res.locals.metaTags, res.locals.linkTags), | 
					
						
							| 
									
										
										
										
											2019-04-05 21:14:49 +03:00
										 |  |  | 					banned: async.apply(user.bans.isBanned, req.uid), | 
					
						
							|  |  |  | 					banReason: async.apply(user.bans.getReason, req.uid), | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 					unreadData: async.apply(topics.getUnreadData, { uid: req.uid }), | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 					unreadChatCount: async.apply(messaging.getUnreadCount, req.uid), | 
					
						
							|  |  |  | 					unreadNotificationCount: async.apply(user.notifications.getUnreadCount, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 				const unreadData = { | 
					
						
							|  |  |  | 					'': {}, | 
					
						
							|  |  |  | 					new: {}, | 
					
						
							|  |  |  | 					watched: {}, | 
					
						
							|  |  |  | 					unreplied: {}, | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				results.user.unreadData = unreadData; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				results.user.isAdmin = results.isAdmin; | 
					
						
							|  |  |  | 				results.user.isGlobalMod = results.isGlobalMod; | 
					
						
							|  |  |  | 				results.user.isMod = !!results.isModerator; | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 				results.user.privileges = results.privileges; | 
					
						
							| 
									
										
										
										
											2018-10-10 22:46:36 -04:00
										 |  |  | 				results.user[results.user.status] = true; | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 09:08:03 -04:00
										 |  |  | 				results.user.email = String(results.user.email); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:02:59 -04:00
										 |  |  | 				results.user['email:confirmed'] = results.user['email:confirmed'] === 1; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				results.user.isEmailConfirmSent = !!results.isEmailConfirmSent; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-19 12:03:12 -05:00
										 |  |  | 				templateValues.bootswatchSkin = (parseInt(meta.config.disableCustomUserSkins, 10) !== 1 ? res.locals.config.bootswatchSkin : '') || meta.config.bootswatchSkin || ''; | 
					
						
							|  |  |  | 				templateValues.config.bootswatchSkin = templateValues.bootswatchSkin || 'noskin';	// TODO remove in v1.12.0+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 				const unreadCounts = results.unreadData.counts; | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 				var unreadCount = { | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 					topic: unreadCounts[''] || 0, | 
					
						
							|  |  |  | 					newTopic: unreadCounts.new || 0, | 
					
						
							|  |  |  | 					watchedTopic: unreadCounts.watched || 0, | 
					
						
							|  |  |  | 					unrepliedTopic: unreadCounts.unreplied || 0, | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 					chat: results.unreadChatCount || 0, | 
					
						
							|  |  |  | 					notification: results.unreadNotificationCount || 0, | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2018-09-24 12:58:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 				Object.keys(unreadCount).forEach(function (key) { | 
					
						
							|  |  |  | 					if (unreadCount[key] > 99) { | 
					
						
							|  |  |  | 						unreadCount[key] = '99+'; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 				const tidsByFilter = results.unreadData.tidsByFilter; | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 				results.navigation = results.navigation.map(function (item) { | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 					function modifyNavItem(item, route, filter, content) { | 
					
						
							| 
									
										
										
										
											2018-09-24 12:58:59 -04:00
										 |  |  | 						if (item && item.originalRoute === route) { | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 							unreadData[filter] = _.zipObject(tidsByFilter[filter], tidsByFilter[filter].map(() => true)); | 
					
						
							| 
									
										
										
										
											2018-09-24 12:58:59 -04:00
										 |  |  | 							item.content = content; | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 							if (unreadCounts[filter] > 0) { | 
					
						
							| 
									
										
										
										
											2018-09-24 12:58:59 -04:00
										 |  |  | 								item.iconClass += ' unread-count'; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-12-16 00:09:13 -05:00
										 |  |  | 					modifyNavItem(item, '/unread', '', unreadCount.topic); | 
					
						
							|  |  |  | 					modifyNavItem(item, '/unread?filter=new', 'new', unreadCount.newTopic); | 
					
						
							|  |  |  | 					modifyNavItem(item, '/unread?filter=watched', 'watched', unreadCount.watchedTopic); | 
					
						
							|  |  |  | 					modifyNavItem(item, '/unread?filter=unreplied', 'unreplied', unreadCount.unrepliedTopic); | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 					return item; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-31 16:48:28 -04:00
										 |  |  | 				templateValues.browserTitle = results.browserTitle; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.navigation = results.navigation; | 
					
						
							| 
									
										
										
										
											2017-12-20 13:56:14 -07:00
										 |  |  | 				templateValues.unreadCount = unreadCount; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				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; | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 				templateValues.canChat = results.canChat && meta.config.disableChat !== 1; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.user = results.user; | 
					
						
							| 
									
										
										
										
											2017-10-25 09:08:03 -04:00
										 |  |  | 				templateValues.userJSON = jsesc(JSON.stringify(results.user), { isScriptContext: true }); | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 				templateValues.useCustomCSS = meta.config.useCustomCSS && meta.config.customCSS; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : ''; | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 				templateValues.useCustomHTML = meta.config.useCustomHTML; | 
					
						
							| 
									
										
										
										
											2017-10-12 13:38:27 -04:00
										 |  |  | 				templateValues.customHTML = templateValues.useCustomHTML ? meta.config.customHTML : ''; | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 				templateValues.maintenanceHeader = meta.config.maintenanceMode && !results.isAdmin; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				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
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-11-07 12:34:12 -05:00
										 |  |  | 		], function (err, data) { | 
					
						
							|  |  |  | 			callback(err, data.templateValues); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 17:11:35 -05:00
										 |  |  | 	middleware.renderHeader = function renderHeader(req, res, data, callback) { | 
					
						
							| 
									
										
										
										
											2018-11-07 12:34:12 -05:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			async.apply(middleware.generateHeader, req, res, data), | 
					
						
							|  |  |  | 			function (templateValues, next) { | 
					
						
							|  |  |  | 				req.app.render('header', 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
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 17:11:35 -05:00
										 |  |  | 	middleware.renderFooter = function renderFooter(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) { | 
					
						
							| 
									
										
										
										
											2018-07-24 16:05:05 -04:00
										 |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					scripts: async.apply(plugins.fireHook, 'filter:scripts.get', []), | 
					
						
							| 
									
										
										
										
											2019-01-17 12:25:53 -05:00
										 |  |  | 					timeagoLocale: (next) => { | 
					
						
							|  |  |  | 						async.waterfall([ | 
					
						
							| 
									
										
										
										
											2019-01-18 15:32:34 -05:00
										 |  |  | 							async.apply(languages.listCodes), | 
					
						
							|  |  |  | 							(languageCodes, next) => { | 
					
						
							|  |  |  | 								const userLang = res.locals.config.userLang; | 
					
						
							|  |  |  | 								const timeagoCode = utils.userLangToTimeagoCode(userLang); | 
					
						
							| 
									
										
										
										
											2019-01-17 12:25:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-18 15:32:34 -05:00
										 |  |  | 								if (languageCodes.includes(userLang) && languages.timeagoCodes.includes(timeagoCode)) { | 
					
						
							|  |  |  | 									const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + timeagoCode + '.js'; | 
					
						
							|  |  |  | 									next(null, (nconf.get('relative_path') + '/assets' + pathToLocaleFile)); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									next(null, false); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2019-01-17 12:25:53 -05:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 						], next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2018-07-24 16:05:05 -04:00
										 |  |  | 				}, function (err, results) { | 
					
						
							|  |  |  | 					next(err, data, results); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (data, results, next) { | 
					
						
							| 
									
										
										
										
											2019-01-17 12:25:53 -05:00
										 |  |  | 				if (results.timeagoLocale) { | 
					
						
							|  |  |  | 					results.scripts.push(results.timeagoLocale); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-07-24 16:05:05 -04:00
										 |  |  | 				data.templateValues.scripts = results.scripts.map(function (script) { | 
					
						
							|  |  |  | 					return { src: script }; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 				data.templateValues.useCustomJS = meta.config.useCustomJS; | 
					
						
							| 
									
										
										
										
											2018-07-24 16:05:05 -04:00
										 |  |  | 				data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : ''; | 
					
						
							| 
									
										
										
										
											2018-12-15 16:38:30 -05:00
										 |  |  | 				data.templateValues.isSpider = req.uid === -1; | 
					
						
							| 
									
										
										
										
											2017-02-23 20:44:14 +03:00
										 |  |  | 				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) { | 
					
						
							| 
									
										
										
										
											2018-03-12 13:24:16 -04:00
										 |  |  | 		var title = controllers.helpers.buildTitle(meta.config.homePageTitle || '[[pages:home]]'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; |