| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | var nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2016-07-08 17:09:47 -04:00
										 |  |  | var validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2017-02-02 13:11:27 +03:00
										 |  |  | var winston = require('winston'); | 
					
						
							| 
									
										
										
										
											2016-04-30 10:47:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2017-04-08 20:22:21 -06:00
										 |  |  | var translator = require('../translator'); | 
					
						
							| 
									
										
										
										
											2017-07-10 12:54:45 -04:00
										 |  |  | var widgets = require('../widgets'); | 
					
						
							| 
									
										
										
										
											2017-12-01 11:11:37 -05:00
										 |  |  | var utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (middleware) { | 
					
						
							|  |  |  | 	middleware.processRender = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
 | 
					
						
							|  |  |  | 		var render = res.render; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		res.render = function (template, options, fn) { | 
					
						
							| 
									
										
										
										
											2016-04-30 10:47:28 +03:00
										 |  |  | 			var self = this; | 
					
						
							|  |  |  | 			var req = this.req; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			var defaultFn = function (err, str) { | 
					
						
							| 
									
										
										
										
											2016-04-30 10:47:28 +03:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				self.send(str); | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 10:47:28 +03:00
										 |  |  | 			options = options || {}; | 
					
						
							| 
									
										
										
										
											2017-02-18 14:00:29 -07:00
										 |  |  | 			if (typeof options === 'function') { | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 				fn = options; | 
					
						
							|  |  |  | 				options = {}; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-18 14:00:29 -07:00
										 |  |  | 			if (typeof fn !== 'function') { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 				fn = defaultFn; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 			var ajaxifyData; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 					options.loggedIn = !!req.uid; | 
					
						
							|  |  |  | 					options.relative_path = nconf.get('relative_path'); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					options.template = { name: template }; | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 					options.template[template] = true; | 
					
						
							| 
									
										
										
										
											2017-11-07 12:39:34 -05:00
										 |  |  | 					options.url = (req.baseUrl + req.path.replace(/^\/api/, '')); | 
					
						
							| 
									
										
										
										
											2017-12-01 11:11:37 -05:00
										 |  |  | 					options.bodyClass = buildBodyClass(req, options); | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options }, next); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:22:37 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							| 
									
										
										
										
											2017-06-07 20:18:04 -04:00
										 |  |  | 					plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: data.templateData }, next); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:22:37 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							|  |  |  | 					options = data.templateData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-10 12:54:45 -04:00
										 |  |  | 					widgets.render(req.uid, { | 
					
						
							|  |  |  | 						template: template + '.tpl', | 
					
						
							|  |  |  | 						url: options.url, | 
					
						
							|  |  |  | 						templateData: options, | 
					
						
							|  |  |  | 						req: req, | 
					
						
							|  |  |  | 						res: res, | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							|  |  |  | 					options.widgets = data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 					res.locals.template = template; | 
					
						
							|  |  |  | 					options._locals = undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (res.locals.isAPI) { | 
					
						
							|  |  |  | 						if (req.route && req.route.path === '/api/') { | 
					
						
							|  |  |  | 							options.title = '[[pages:home]]'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return res.json(options); | 
					
						
							| 
									
										
										
										
											2016-04-30 10:47:28 +03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 					ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 						header: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 							renderHeaderFooter('renderHeader', req, res, options, next); | 
					
						
							|  |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 						content: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 							render.call(self, template, options, next); | 
					
						
							|  |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 						footer: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 							renderHeaderFooter('renderFooter', req, res, options, 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) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 					var str = results.header + | 
					
						
							|  |  |  | 						(res.locals.postHeader || '') + | 
					
						
							| 
									
										
										
										
											2017-02-12 20:36:12 +03:00
										 |  |  | 						results.content + '<script id="ajaxify-data"></script>' + | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 						(res.locals.preFooter || '') + | 
					
						
							|  |  |  | 						results.footer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					translate(str, req, res, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (translated, next) { | 
					
						
							| 
									
										
										
										
											2017-02-13 22:42:47 +03:00
										 |  |  | 					translated = translated.replace('<script id="ajaxify-data"></script>', function () { | 
					
						
							| 
									
										
										
										
											2017-02-13 22:24:17 +03:00
										 |  |  | 						return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>'; | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2017-02-12 20:36:12 +03:00
										 |  |  | 					next(null, translated); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 			], fn); | 
					
						
							| 
									
										
										
										
											2015-10-11 23:05:33 -04:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		next(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 	function renderHeaderFooter(method, req, res, options, next) { | 
					
						
							|  |  |  | 		if (res.locals.renderHeader) { | 
					
						
							|  |  |  | 			middleware[method](req, res, options, next); | 
					
						
							|  |  |  | 		} else if (res.locals.renderAdminHeader) { | 
					
						
							|  |  |  | 			middleware.admin[method](req, res, options, next); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			next(null, ''); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function translate(str, req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:51:11 -07:00
										 |  |  | 		var language = (res.locals.config && res.locals.config.userLang) || 'en-GB'; | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 		language = req.query.lang ? validator.escape(String(req.query.lang)) : language; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		translator.translate(str, language, function (translated) { | 
					
						
							| 
									
										
										
										
											2016-10-05 15:22:35 +03:00
										 |  |  | 			next(null, translator.unescape(translated)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 11:11:37 -05:00
										 |  |  | 	function buildBodyClass(req, templateData) { | 
					
						
							| 
									
										
										
										
											2016-05-05 22:44:14 -04:00
										 |  |  | 		var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, ''); | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 		var parts = clean.split('/').slice(0, 3); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		parts.forEach(function (p, index) { | 
					
						
							| 
									
										
										
										
											2017-02-02 13:11:27 +03:00
										 |  |  | 			try { | 
					
						
							|  |  |  | 				p = decodeURIComponent(p); | 
					
						
							|  |  |  | 			} catch (err) { | 
					
						
							| 
									
										
										
										
											2017-11-01 18:58:44 -06:00
										 |  |  | 				winston.error(err); | 
					
						
							| 
									
										
										
										
											2017-02-02 13:11:27 +03:00
										 |  |  | 				p = ''; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-03-09 20:18:42 +03:00
										 |  |  | 			p = validator.escape(String(p)); | 
					
						
							| 
									
										
										
										
											2015-10-30 15:43:04 -04:00
										 |  |  | 			parts[index] = index ? parts[0] + '-' + p : 'page-' + (p || 'home'); | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-01 11:11:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (templateData.template.topic) { | 
					
						
							|  |  |  | 			parts.push('page-topic-category-' + templateData.category.cid); | 
					
						
							|  |  |  | 			parts.push('page-topic-category-' + utils.slugify(templateData.category.name)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 18:43:40 -04:00
										 |  |  | 		return parts.join(' '); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | }; |