| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | var topicsController = require('./topics'), | 
					
						
							|  |  |  | 	categoriesController = require('./categories'), | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	usersController = require('./users'), | 
					
						
							|  |  |  | 	accountsController = require('./accounts'), | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 	staticController = require('./static'), | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:40 -05:00
										 |  |  | 	apiController = require('./api'), | 
					
						
							| 
									
										
										
										
											2014-03-03 16:35:21 -05:00
										 |  |  | 	adminController = require('./admin'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-02-27 17:16:06 -05:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							|  |  |  | 	auth = require('./../routes/authentication'), | 
					
						
							|  |  |  | 	meta = require('./../meta'), | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | 	user = require('./../user'), | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 	posts = require('./../posts'), | 
					
						
							|  |  |  | 	topics = require('./../topics'), | 
					
						
							| 
									
										
										
										
											2014-02-27 17:16:06 -05:00
										 |  |  | 	plugins = require('./../plugins'), | 
					
						
							|  |  |  | 	categories = require('./../categories'), | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 	privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | var Controllers = { | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 	topics: topicsController, | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 	categories: categoriesController, | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	users: usersController, | 
					
						
							|  |  |  | 	accounts: accountsController, | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:40 -05:00
										 |  |  | 	static: staticController, | 
					
						
							| 
									
										
										
										
											2014-03-03 16:35:21 -05:00
										 |  |  | 	api: apiController, | 
					
						
							|  |  |  | 	admin: adminController | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Controllers.home = function(req, res, next) { | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 		header: function (next) { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | 			res.locals.metaTags = [{ | 
					
						
							|  |  |  | 				name: "title", | 
					
						
							|  |  |  | 				content: meta.config.title || 'NodeBB' | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				name: "description", | 
					
						
							|  |  |  | 				content: meta.config.description || '' | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				property: 'og:title', | 
					
						
							|  |  |  | 				content: 'Index | ' + (meta.config.title || 'NodeBB') | 
					
						
							|  |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-04-03 18:15:09 -04:00
										 |  |  | 				property: 'og:type', | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | 				content: 'website' | 
					
						
							|  |  |  | 			}]; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-03 18:15:09 -04:00
										 |  |  | 			if(meta.config['brand:logo']) { | 
					
						
							|  |  |  | 				res.locals.metaTags.push({ | 
					
						
							|  |  |  | 					property: 'og:image', | 
					
						
							|  |  |  | 					content: meta.config['brand:logo'] | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 			next(null); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 		categories: function (next) { | 
					
						
							| 
									
										
										
										
											2014-03-24 15:58:02 -04:00
										 |  |  | 			var uid = req.user ? req.user.uid : 0; | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 			categories.getAllCategories(uid, function (err, data) { | 
					
						
							| 
									
										
										
										
											2014-03-24 15:58:02 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 				data.categories = data.categories.filter(function (category) { | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 					return !category.disabled; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 				function getRecentReplies(category, callback) { | 
					
						
							|  |  |  | 					categories.getRecentReplies(category.cid, uid, parseInt(category.numRecentReplies, 10), function (err, posts) { | 
					
						
							| 
									
										
										
										
											2014-05-19 19:24:06 -04:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return callback(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 						category.posts = posts; | 
					
						
							| 
									
										
										
										
											2014-05-19 19:24:06 -04:00
										 |  |  | 						callback(); | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 				async.filter(data.categories, function (category, next) { | 
					
						
							| 
									
										
										
										
											2014-05-16 15:47:04 -04:00
										 |  |  | 					privileges.categories.can('find', category.cid, uid, function(err, findable) { | 
					
						
							|  |  |  | 						next(!err && findable); | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}, function(visibleCategories) { | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 					data.categories = visibleCategories; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					async.each(data.categories, getRecentReplies, function (err) { | 
					
						
							|  |  |  | 						next(err, data.categories); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2014-03-09 19:54:16 -04:00
										 |  |  | 		res.render('home', data); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 17:16:53 -05:00
										 |  |  | Controllers.search = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 	var start = process.hrtime(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 	if (!req.params.term) { | 
					
						
							|  |  |  | 		return res.render('search', { | 
					
						
							|  |  |  | 			search_query: '', | 
					
						
							|  |  |  | 			posts: [], | 
					
						
							|  |  |  | 			topics: [] | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!plugins.hasListeners('filter:search.query')) { | 
					
						
							|  |  |  | 		return res.redirect('/404'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 	function search(index, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 		plugins.fireHook('filter:search.query', { | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 			index: index, | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 			query: req.params.term | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 		}, callback); | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		pids: function(next) { | 
					
						
							|  |  |  | 			search('post', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		tids: function(next) { | 
					
						
							|  |  |  | 			search('topic', next); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!results) { | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 			results = {pids:[], tids: []}; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 00:13:42 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			posts: function(next) { | 
					
						
							|  |  |  | 				posts.getPostSummaryByPids(results.pids, false, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			topics: function(next) { | 
					
						
							|  |  |  | 				topics.getTopicsByTids(results.tids, 0, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return res.render('search', { | 
					
						
							|  |  |  | 				time: process.elapsedTimeSince(start), | 
					
						
							|  |  |  | 				search_query: req.params.term, | 
					
						
							|  |  |  | 				posts: results.posts, | 
					
						
							|  |  |  | 				topics: results.topics, | 
					
						
							|  |  |  | 				post_matches : results.posts.length, | 
					
						
							|  |  |  | 				topic_matches : results.topics.length | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-03-03 17:16:53 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Controllers.reset = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-03-21 20:37:53 -04:00
										 |  |  | 	res.render(req.params.code ? 'reset_code' : 'reset', { | 
					
						
							| 
									
										
										
										
											2014-03-03 17:16:53 -05:00
										 |  |  | 		reset_code: req.params.code ? req.params.code : null | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-03-03 17:16:53 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:04:41 -05:00
										 |  |  | Controllers.login = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	var data = {}, | 
					
						
							|  |  |  | 		login_strategies = auth.get_login_strategies(), | 
					
						
							|  |  |  | 		num_strategies = login_strategies.length, | 
					
						
							|  |  |  | 		emailersPresent = plugins.hasListeners('action:email.send'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:48:13 -04:00
										 |  |  | 	data.alternate_logins = num_strategies > 0; | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	data.authentication = login_strategies; | 
					
						
							|  |  |  | 	data.token = res.locals.csrf_token; | 
					
						
							|  |  |  | 	data.showResetLink = emailersPresent; | 
					
						
							| 
									
										
										
										
											2014-04-08 15:29:51 -04:00
										 |  |  | 	data.allowLocalLogin = meta.config.allowLocalLogin === undefined || parseInt(meta.config.allowLocalLogin, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-05-07 11:46:24 -04:00
										 |  |  | 	if (req.query.next) { | 
					
						
							|  |  |  | 		data.previousUrl = req.query.next; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 	res.render('login', data); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:04:41 -05:00
										 |  |  | Controllers.register = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-03-11 04:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	var data = {}, | 
					
						
							|  |  |  | 		login_strategies = auth.get_login_strategies(), | 
					
						
							|  |  |  | 		num_strategies = login_strategies.length; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | 	if (num_strategies === 0) { | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 		data = { | 
					
						
							|  |  |  | 			'register_window:spansize': 'col-md-12', | 
					
						
							|  |  |  | 			'alternate_logins': false | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		data = { | 
					
						
							|  |  |  | 			'register_window:spansize': 'col-md-6', | 
					
						
							|  |  |  | 			'alternate_logins': true | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data.authentication = login_strategies; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data.token = res.locals.csrf_token; | 
					
						
							|  |  |  | 	data.minimumUsernameLength = meta.config.minimumUsernameLength; | 
					
						
							|  |  |  | 	data.maximumUsernameLength = meta.config.maximumUsernameLength; | 
					
						
							|  |  |  | 	data.minimumPasswordLength = meta.config.minimumPasswordLength; | 
					
						
							|  |  |  | 	data.termsOfUse = meta.config.termsOfUse; | 
					
						
							| 
									
										
										
										
											2014-03-11 04:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-19 12:29:52 -04:00
										 |  |  | 	plugins.fireHook('filter:register.build', req, res, data, function(err, req, res, data) { | 
					
						
							| 
									
										
										
										
											2014-05-15 03:06:47 -04:00
										 |  |  | 		if (err && process.env === 'development') { | 
					
						
							|  |  |  | 			winston.warn(JSON.stringify(err)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res.render('register', data); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:04:41 -05:00
										 |  |  | Controllers.confirmEmail = function(req, res, next) { | 
					
						
							|  |  |  | 	user.email.confirm(req.params.code, function (data) { | 
					
						
							|  |  |  | 		if (data.status === 'ok') { | 
					
						
							|  |  |  | 			data = { | 
					
						
							|  |  |  | 				'alert-class': 'alert-success', | 
					
						
							|  |  |  | 				title: 'Email Confirmed', | 
					
						
							|  |  |  | 				text: 'Thank you for vaidating your email. Your account is now fully activated.' | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			data = { | 
					
						
							|  |  |  | 				'alert-class': 'alert-danger', | 
					
						
							|  |  |  | 				title: 'An error occurred...', | 
					
						
							|  |  |  | 				text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.' | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 		res.render('confirm', data); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:04:41 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-27 15:06:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:16:06 -05:00
										 |  |  | Controllers.sitemap = function(req, res, next) { | 
					
						
							|  |  |  | 	var sitemap = require('./../sitemap.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sitemap.render(function(xml) { | 
					
						
							|  |  |  | 		res.header('Content-Type', 'application/xml'); | 
					
						
							|  |  |  | 		res.send(xml); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Controllers.robots = function (req, res) { | 
					
						
							|  |  |  | 	res.set('Content-Type', 'text/plain'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (meta.config["robots.txt"]) { | 
					
						
							|  |  |  | 		res.send(meta.config["robots.txt"]); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		res.send("User-agent: *\n" + | 
					
						
							| 
									
										
										
										
											2014-03-11 19:44:48 -04:00
										 |  |  | 			"Disallow: " + nconf.get('relative_path') + "/admin/\n" + | 
					
						
							| 
									
										
										
										
											2014-02-27 17:16:06 -05:00
										 |  |  | 			"Sitemap: " + nconf.get('url') + "/sitemap.xml"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | Controllers.outgoing = function(req, res, next) { | 
					
						
							|  |  |  | 	var url = req.query.url, | 
					
						
							|  |  |  | 		data = { | 
					
						
							|  |  |  | 			url: url, | 
					
						
							|  |  |  | 			title: meta.config.title | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (url) { | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 		res.render('outgoing', data); | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		res.status(404); | 
					
						
							|  |  |  | 		res.redirect(nconf.get('relative_path') + '/404'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = Controllers; |