| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | var nconf = require('nconf'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	validator = require('validator'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 15:11:38 -04:00
										 |  |  | 	translator = require('../../public/src/modules/translator'), | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	categories = require('../categories'), | 
					
						
							| 
									
										
										
										
											2015-03-06 13:40:35 -05:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var helpers = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 21:07:23 -05:00
										 |  |  | helpers.notAllowed = function(req, res, error) { | 
					
						
							| 
									
										
										
										
											2015-04-01 00:23:57 -04:00
										 |  |  | 	if (req.uid) { | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2015-08-27 16:34:01 -04:00
										 |  |  | 			res.status(403).json({ | 
					
						
							|  |  |  | 				path: req.path.replace(/^\/api/, ''), | 
					
						
							|  |  |  | 				loggedIn: !!req.uid, error: error, | 
					
						
							|  |  |  | 				title: '[[global:403.title]]' | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-08-27 16:34:01 -04:00
										 |  |  | 			res.status(403).render('403', { | 
					
						
							|  |  |  | 				path: req.path, | 
					
						
							|  |  |  | 				loggedIn: !!req.uid, error: error, | 
					
						
							|  |  |  | 				title: '[[global:403.title]]' | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:16:17 -05:00
										 |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, ''); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 			res.status(401).json('not-authorized'); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:16:17 -05:00
										 |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 			res.redirect(nconf.get('relative_path') + '/login'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | helpers.redirect = function(res, url) { | 
					
						
							|  |  |  | 	if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2015-09-11 22:50:09 -04:00
										 |  |  | 		res.status(308).json(url); | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | 		res.redirect(nconf.get('relative_path') + encodeURI(url)); | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | helpers.buildCategoryBreadcrumbs = function(cid, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	var breadcrumbs = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.whilst(function() { | 
					
						
							|  |  |  | 		return parseInt(cid, 10); | 
					
						
							|  |  |  | 	}, function(next) { | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 		categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled'], function(err, data) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 			if (!parseInt(data.disabled, 10)) { | 
					
						
							|  |  |  | 				breadcrumbs.unshift({ | 
					
						
							|  |  |  | 					text: validator.escape(data.name), | 
					
						
							|  |  |  | 					url: nconf.get('relative_path') + '/category/' + data.slug | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cid = data.parentCid; | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 11:40:40 -04:00
										 |  |  | 		if (!meta.config.homePageRoute && meta.config.homePageCustom) { | 
					
						
							|  |  |  | 			breadcrumbs.unshift({ | 
					
						
							|  |  |  | 				text: '[[global:header.categories]]', | 
					
						
							|  |  |  | 				url: nconf.get('relative_path') + '/categories' | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 		breadcrumbs.unshift({ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							|  |  |  | 			url: nconf.get('relative_path') + '/' | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, breadcrumbs); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.buildBreadcrumbs = function(crumbs) { | 
					
						
							|  |  |  | 	var breadcrumbs = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							|  |  |  | 			url: nconf.get('relative_path') + '/' | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	crumbs.forEach(function(crumb) { | 
					
						
							|  |  |  | 		if (crumb) { | 
					
						
							|  |  |  | 			if (crumb.url) { | 
					
						
							|  |  |  | 				crumb.url = nconf.get('relative_path') + crumb.url; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			breadcrumbs.push(crumb); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return breadcrumbs; | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 01:59:13 -04:00
										 |  |  | helpers.buildTitle = function(pageTitle) { | 
					
						
							|  |  |  | 	var titleLayout = meta.config.titleLayout || '{pageTitle} | {browserTitle}'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var browserTitle = validator.escape(meta.config.browserTitle || meta.config.title || 'NodeBB'); | 
					
						
							|  |  |  | 	pageTitle = pageTitle || ''; | 
					
						
							| 
									
										
										
										
											2016-03-22 11:19:33 +02:00
										 |  |  | 	var title = titleLayout.replace('{pageTitle}', function() { | 
					
						
							|  |  |  | 		return pageTitle; | 
					
						
							|  |  |  | 	}).replace('{browserTitle}', function() { | 
					
						
							|  |  |  | 		return browserTitle; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-09-23 01:59:13 -04:00
										 |  |  | 	return title; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | module.exports = helpers; |