| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var navigation = require('../navigation'); | 
					
						
							|  |  |  | var translator = require('../../public/src/modules/translator'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var controllers = { | 
					
						
							|  |  |  | 	api: require('../controllers/api'), | 
					
						
							|  |  |  | 	helpers: require('../controllers/helpers') | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(app, middleware) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	middleware.buildHeader = function(req, res, next) { | 
					
						
							|  |  |  | 		res.locals.renderHeader = true; | 
					
						
							|  |  |  | 		res.locals.isAPI = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		middleware.applyCSRF(req, res, function() { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				config: function(next) { | 
					
						
							|  |  |  | 					controllers.api.getConfig(req, res, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				footer: function(next) { | 
					
						
							|  |  |  | 					app.render('footer', {loggedIn: (req.user ? parseInt(req.user.uid, 10) !== 0 : false)}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				plugins: function(next) { | 
					
						
							|  |  |  | 					plugins.fireHook('filter:middleware.buildHeader', {req: req, locals: res.locals}, next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, results) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				res.locals.config = results.config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				translator.translate(results.footer, results.config.defaultLang, function(parsedTemplate) { | 
					
						
							|  |  |  | 					res.locals.footer = parsedTemplate; | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	middleware.renderHeader = function(req, res, data, callback) { | 
					
						
							|  |  |  | 		var registrationType = meta.config.registrationType || 'normal'; | 
					
						
							|  |  |  | 		var templateValues = { | 
					
						
							|  |  |  | 			bootswatchCSS: meta.config['theme:src'], | 
					
						
							|  |  |  | 			title: meta.config.title || '', | 
					
						
							|  |  |  | 			description: meta.config.description || '', | 
					
						
							|  |  |  | 			'cache-buster': meta.config['cache-buster'] ? 'v=' + meta.config['cache-buster'] : '', | 
					
						
							|  |  |  | 			'brand:logo': meta.config['brand:logo'] || '', | 
					
						
							|  |  |  | 			'brand:logo:url': meta.config['brand:logo:url'] || '', | 
					
						
							|  |  |  | 			'brand:logo:alt': meta.config['brand:logo:alt'] || '', | 
					
						
							|  |  |  | 			'brand:logo:display': meta.config['brand:logo']?'':'hide', | 
					
						
							|  |  |  | 			allowRegistration: registrationType === 'normal' || registrationType === 'admin-approval', | 
					
						
							|  |  |  | 			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-01-18 15:32:49 -05:00
										 |  |  | 			scripts: function(next) { | 
					
						
							|  |  |  | 				plugins.fireHook('filter:scripts.get', [], function(err, scripts) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					var arr = []; | 
					
						
							|  |  |  | 					scripts.forEach(function(script) { | 
					
						
							|  |  |  | 						arr.push({src: script}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					next(null, arr); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			isAdmin: function(next) { | 
					
						
							|  |  |  | 				user.isAdministrator(req.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			user: function(next) { | 
					
						
							|  |  |  | 				if (req.uid) { | 
					
						
							| 
									
										
										
										
											2015-10-29 16:35:44 -04:00
										 |  |  | 					user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'status', 'email:confirmed', 'banned'], next); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(null, { | 
					
						
							|  |  |  | 						username: '[[global:guest]]', | 
					
						
							|  |  |  | 						userslug: '', | 
					
						
							| 
									
										
										
										
											2015-10-25 07:33:59 -04:00
										 |  |  | 						picture: meta.config.defaultAvatar, | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 						status: 'offline', | 
					
						
							|  |  |  | 						banned: false, | 
					
						
							|  |  |  | 						uid: 0 | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			navigation: async.apply(navigation.get), | 
					
						
							|  |  |  | 			tags: async.apply(meta.tags.parse, res.locals.metaTags, res.locals.linkTags) | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (results.user && parseInt(results.user.banned, 10) === 1) { | 
					
						
							|  |  |  | 				req.logout(); | 
					
						
							|  |  |  | 				return res.redirect('/'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-11 23:20:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			results.user.isAdmin = results.isAdmin; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			results.user.uid = parseInt(results.user.uid, 10); | 
					
						
							|  |  |  | 			results.user['email:confirmed'] = parseInt(results.user['email:confirmed'], 10) === 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 14:07:09 -04:00
										 |  |  | 			if (res.locals.config.bootswatchSkin !== 'default') { | 
					
						
							|  |  |  | 				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; | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 			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-01-18 15:32:49 -05:00
										 |  |  | 			templateValues.scripts = results.scripts; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 			if (req.route && req.route.path === '/') { | 
					
						
							|  |  |  | 				modifyTitle(templateValues); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:middleware.renderHeader', {templateValues: templateValues, req: req, res: res}, function(err, data) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				app.render('header', data.templateValues, callback); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function modifyTitle(obj) { | 
					
						
							|  |  |  | 		var title = controllers.helpers.buildTitle('[[pages:home]]'); | 
					
						
							|  |  |  | 		obj.browserTitle = title; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (obj.metaTags) { | 
					
						
							|  |  |  | 			obj.metaTags.forEach(function(tag, i) { | 
					
						
							|  |  |  | 				if (tag.property === 'og:title') { | 
					
						
							|  |  |  | 					obj.metaTags[i].content = title; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return title; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |