| 
									
										
										
										
											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-05-21 16:13:46 -04:00
										 |  |  | 	tagsController = require('./tags'), | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	usersController = require('./users'), | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04:00
										 |  |  | 	groupsController = require('./groups'), | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	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'), | 
					
						
							| 
									
										
										
										
											2014-09-27 19:23:48 -04:00
										 |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04:00
										 |  |  | 	auth = require('../routes/authentication'), | 
					
						
							|  |  |  | 	meta = require('../meta'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	posts = require('../posts'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 	search = require('../search'), | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04: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-05-21 16:13:46 -04:00
										 |  |  | 	tags: tagsController, | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	users: usersController, | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04:00
										 |  |  | 	groups: groupsController, | 
					
						
							| 
									
										
										
										
											2014-02-28 14:08:06 -05:00
										 |  |  | 	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-08-15 15:45:01 -04:00
										 |  |  | 			categories.getCategoriesByPrivilege(uid, 'find', function (err, categoryData) { | 
					
						
							| 
									
										
										
										
											2014-03-24 15:58:02 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-16 16:10:02 -04:00
										 |  |  | 				var childCategories = []; | 
					
						
							| 
									
										
										
										
											2014-03-24 15:58:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 16:10:02 -04:00
										 |  |  | 				for(var i=categoryData.length - 1; i>=0; --i) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (Array.isArray(categoryData[i].children) && categoryData[i].children.length) { | 
					
						
							|  |  |  | 						childCategories.push.apply(childCategories, categoryData[i].children); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 18:59:11 -04:00
										 |  |  | 					if (categoryData[i].parent && categoryData[i].parent.cid) { | 
					
						
							| 
									
										
										
										
											2014-09-16 16:10:02 -04:00
										 |  |  | 						categoryData.splice(i, 1); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-15 10:50:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 16:10:02 -04:00
										 |  |  | 				async.parallel([ | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						categories.getRecentTopicReplies(categoryData, uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						categories.getRecentTopicReplies(childCategories, uid, next); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				], function(err) { | 
					
						
							| 
									
										
										
										
											2014-05-27 12:44:28 -04:00
										 |  |  | 					next(err, categoryData); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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-09 21:43:35 -04:00
										 |  |  | 	if (!req.params.term) { | 
					
						
							|  |  |  | 		return res.render('search', { | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 			time: 0, | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 			search_query: '', | 
					
						
							|  |  |  | 			posts: [], | 
					
						
							|  |  |  | 			topics: [] | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 	var uid = req.user ? req.user.uid : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 	if (!plugins.hasListeners('filter:search.query')) { | 
					
						
							|  |  |  | 		return res.redirect('/404'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 19:23:48 -04:00
										 |  |  | 	req.params.term = validator.escape(req.params.term); | 
					
						
							| 
									
										
										
										
											2014-09-03 14:40:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 	search.search(req.params.term, uid, function(err, results) { | 
					
						
							| 
									
										
										
										
											2014-03-09 21:43:35 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-07 17:36:10 -04:00
										 |  |  | 		return res.render('search', results); | 
					
						
							| 
									
										
										
										
											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 = {}, | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 		loginStrategies = auth.getLoginStrategies(), | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 		emailersPresent = plugins.hasListeners('action:email.send'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 	data.alternate_logins = loginStrategies.length > 0; | 
					
						
							|  |  |  | 	data.authentication = loginStrategies; | 
					
						
							| 
									
										
										
										
											2014-09-14 10:21:32 -04:00
										 |  |  | 	data.token = req.csrfToken(); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	data.showResetLink = emailersPresent; | 
					
						
							| 
									
										
										
										
											2014-10-03 11:34:07 -04:00
										 |  |  | 	data.allowLocalLogin = parseInt(meta.config.allowLocalLogin, 10) === 1; | 
					
						
							|  |  |  | 	data.allowRegistration = parseInt(meta.config.allowRegistration, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-08-31 22:41:13 -04:00
										 |  |  | 	data.error = req.flash('error')[0]; | 
					
						
							| 
									
										
										
										
											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-09-25 11:29:53 -04:00
										 |  |  | 	if(meta.config.allowRegistration !== undefined && parseInt(meta.config.allowRegistration, 10) === 0) { | 
					
						
							|  |  |  | 		return res.redirect(nconf.get('relative_path') + '/403'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-11 04:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	var data = {}, | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 		loginStrategies = auth.getLoginStrategies(); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 	if (loginStrategies.length === 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 	data.authentication = loginStrategies; | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-14 10:21:32 -04:00
										 |  |  | 	data.token = req.csrfToken(); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	data.minimumUsernameLength = meta.config.minimumUsernameLength; | 
					
						
							|  |  |  | 	data.maximumUsernameLength = meta.config.maximumUsernameLength; | 
					
						
							|  |  |  | 	data.minimumPasswordLength = meta.config.minimumPasswordLength; | 
					
						
							|  |  |  | 	data.termsOfUse = meta.config.termsOfUse; | 
					
						
							| 
									
										
										
										
											2014-07-22 22:00:39 -04:00
										 |  |  | 	data.regFormEntry = []; | 
					
						
							| 
									
										
										
										
											2014-03-11 04:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-08 14:51:05 -05:00
										 |  |  | 	plugins.fireHook('filter:register.build', {req: req, res: res, templateData: data}, function(err, data) { | 
					
						
							| 
									
										
										
										
											2014-05-15 03:06:47 -04:00
										 |  |  | 		if (err && process.env === 'development') { | 
					
						
							|  |  |  | 			winston.warn(JSON.stringify(err)); | 
					
						
							| 
									
										
										
										
											2014-11-08 14:51:05 -05:00
										 |  |  | 			return next(err); | 
					
						
							| 
									
										
										
										
											2014-05-15 03:06:47 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-08 14:51:05 -05:00
										 |  |  | 		res.render('register', data.templateData); | 
					
						
							| 
									
										
										
										
											2014-05-15 03:06:47 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2014-06-03 16:10:24 -04:00
										 |  |  | 		data.status = data.status === 'ok'; | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2014-07-17 01:38:20 -04:00
										 |  |  | 	if (meta.config['feeds:disableSitemap'] === '1') { | 
					
						
							| 
									
										
										
										
											2014-09-16 16:10:02 -04:00
										 |  |  | 		return res.redirect(nconf.get('relative_path') + '/404'); | 
					
						
							| 
									
										
										
										
											2014-07-17 01:38:20 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04:00
										 |  |  | 	var sitemap = require('../sitemap.js'); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:16:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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-11-14 12:17:24 -05:00
										 |  |  | Controllers.termsOfUse = function(req, res, next) { | 
					
						
							|  |  |  | 	if (!meta.config.termsOfUse) { | 
					
						
							|  |  |  | 		return categoriesController.notFound(req, res); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	res.render('tos', {termsOfUse: meta.config.termsOfUse}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = Controllers; |