| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | var 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'), | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-22 15:23:19 -04:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	posts = require('../posts'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	plugins = require('../plugins'), | 
					
						
							|  |  |  | 	categories = require('../categories'), | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							|  |  |  | 	helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 22:12:08 -05:00
										 |  |  | var Controllers = { | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 	topics: require('./topics'), | 
					
						
							|  |  |  | 	categories: require('./categories'), | 
					
						
							| 
									
										
										
										
											2015-07-13 14:47:09 -04:00
										 |  |  | 	unread: require('./unread'), | 
					
						
							| 
									
										
										
										
											2015-08-17 16:38:05 -04:00
										 |  |  | 	recent: require('./recent'), | 
					
						
							|  |  |  | 	popular: require('./popular'), | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 	tags: require('./tags'), | 
					
						
							|  |  |  | 	search: require('./search'), | 
					
						
							|  |  |  | 	users: require('./users'), | 
					
						
							|  |  |  | 	groups: require('./groups'), | 
					
						
							|  |  |  | 	accounts: require('./accounts'), | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 	authentication: require('./authentication'), | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 	api: require('./api'), | 
					
						
							| 
									
										
										
										
											2015-03-18 03:39:42 +08:00
										 |  |  | 	admin: require('./admin') | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Controllers.home = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-09-12 17:08:12 -04:00
										 |  |  | 	var route = meta.config.homePageRoute || meta.config.homePageCustom || 'categories', | 
					
						
							| 
									
										
										
										
											2015-02-27 15:04:44 -05:00
										 |  |  | 		hook = 'action:homepage.get:' + route; | 
					
						
							| 
									
										
										
										
											2015-02-27 15:00:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (plugins.hasListeners(hook)) { | 
					
						
							|  |  |  | 		plugins.fireHook(hook, {req: req, res: res, next: next}); | 
					
						
							| 
									
										
										
										
											2015-02-17 13:25:13 -05:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-09-12 17:08:12 -04:00
										 |  |  | 		if (route === 'categories' || route === '/') { | 
					
						
							| 
									
										
										
										
											2015-02-27 15:00:38 -05:00
										 |  |  | 			Controllers.categories.list(req, res, next); | 
					
						
							|  |  |  | 		} else if (route === 'recent') { | 
					
						
							| 
									
										
										
										
											2015-08-18 18:43:14 -04:00
										 |  |  | 			Controllers.recent.get(req, res, next); | 
					
						
							| 
									
										
										
										
											2015-02-27 15:00:38 -05:00
										 |  |  | 		} else if (route === 'popular') { | 
					
						
							| 
									
										
										
										
											2015-08-18 18:43:14 -04:00
										 |  |  | 			Controllers.popular.get(req, res, next); | 
					
						
							| 
									
										
										
										
											2015-02-27 15:00:38 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-09-12 17:08:12 -04:00
										 |  |  | 			res.redirect(route); | 
					
						
							| 
									
										
										
										
											2015-02-27 15:00:38 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-02-13 18:16:36 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:14 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 17:16:53 -05:00
										 |  |  | Controllers.reset = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 	if (req.params.code) { | 
					
						
							| 
									
										
										
										
											2015-02-08 21:06:38 -05:00
										 |  |  | 		user.reset.validate(req.params.code, function(err, valid) { | 
					
						
							| 
									
										
										
										
											2015-03-17 15:53:05 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-02-08 21:06:38 -05:00
										 |  |  | 			res.render('reset_code', { | 
					
						
							|  |  |  | 				valid: valid, | 
					
						
							| 
									
										
										
										
											2015-04-01 17:26:22 -04:00
										 |  |  | 				displayExpiryNotice: req.session.passwordExpired, | 
					
						
							| 
									
										
										
										
											2015-03-17 15:53:05 -04:00
										 |  |  | 				code: req.params.code ? req.params.code : null, | 
					
						
							| 
									
										
										
										
											2015-08-26 15:54:54 -04:00
										 |  |  | 				breadcrumbs: helpers.buildBreadcrumbs([{text: '[[reset_password:reset_password]]', url: '/reset'}, {text: '[[reset_password:update_password]]'}]), | 
					
						
							|  |  |  | 				title: '[[pages:reset]]' | 
					
						
							| 
									
										
										
										
											2015-02-08 21:06:38 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-04-01 17:26:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			delete req.session.passwordExpired; | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		res.render('reset', { | 
					
						
							| 
									
										
										
										
											2015-03-17 15:53:05 -04:00
										 |  |  | 			code: req.params.code ? req.params.code : null, | 
					
						
							| 
									
										
										
										
											2015-08-26 15:54:54 -04:00
										 |  |  | 			breadcrumbs: helpers.buildBreadcrumbs([{text: '[[reset_password:reset_password]]'}]), | 
					
						
							|  |  |  | 			title: '[[pages:reset]]' | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05: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 = {}, | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		loginStrategies = require('../routes/authentication').getLoginStrategies(), | 
					
						
							| 
									
										
										
										
											2015-09-22 17:22:49 -04:00
										 |  |  | 		emailersPresent = plugins.hasListeners('filter:email.send'); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	var registrationType = meta.config.registrationType || 'normal'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 16:15:44 -05:00
										 |  |  | 	data.alternate_logins = loginStrategies.length > 0; | 
					
						
							|  |  |  | 	data.authentication = loginStrategies; | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 	data.showResetLink = emailersPresent; | 
					
						
							| 
									
										
										
										
											2015-04-22 12:13:14 -04:00
										 |  |  | 	data.allowLocalLogin = parseInt(meta.config.allowLocalLogin, 10) === 1 || parseInt(req.query.local, 10) === 1; | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	data.allowRegistration = registrationType === 'normal' || registrationType === 'admin-approval'; | 
					
						
							| 
									
										
										
										
											2015-02-19 13:24:06 -05:00
										 |  |  | 	data.allowLoginWith = '[[login:' + (meta.config.allowLoginWith || 'username-email') + ']]'; | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 	data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:login]]'}]); | 
					
						
							| 
									
										
										
										
											2014-08-31 22:41:13 -04:00
										 |  |  | 	data.error = req.flash('error')[0]; | 
					
						
							| 
									
										
										
										
											2015-08-26 15:54:54 -04:00
										 |  |  | 	data.title = '[[pages:login]]'; | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	var registrationType = meta.config.registrationType || 'normal'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (registrationType === 'disabled') { | 
					
						
							| 
									
										
										
										
											2015-08-28 14:31:35 -04:00
										 |  |  | 		return next(); | 
					
						
							| 
									
										
										
										
											2014-09-25 11:29:53 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-11 04:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 21:54:21 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							|  |  |  | 			if (registrationType === 'invite-only') { | 
					
						
							|  |  |  | 				user.verifyInvitation(req.query, next); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2015-08-18 11:27:22 -04:00
										 |  |  | 			plugins.fireHook('filter:parse.post', {postData: {content: meta.config.termsOfUse}}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(tos, next) { | 
					
						
							| 
									
										
										
										
											2015-06-28 21:54:21 -04:00
										 |  |  | 			var loginStrategies = require('../routes/authentication').getLoginStrategies(); | 
					
						
							|  |  |  | 			var data = { | 
					
						
							|  |  |  | 				'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12', | 
					
						
							|  |  |  | 				'alternate_logins': !!loginStrategies.length | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			data.authentication = loginStrategies; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			data.minimumUsernameLength = meta.config.minimumUsernameLength; | 
					
						
							|  |  |  | 			data.maximumUsernameLength = meta.config.maximumUsernameLength; | 
					
						
							|  |  |  | 			data.minimumPasswordLength = meta.config.minimumPasswordLength; | 
					
						
							| 
									
										
										
										
											2015-08-18 11:27:22 -04:00
										 |  |  | 			data.termsOfUse = tos.postData.content; | 
					
						
							| 
									
										
										
										
											2015-06-28 21:54:21 -04:00
										 |  |  | 			data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[register:register]]'}]); | 
					
						
							|  |  |  | 			data.regFormEntry = []; | 
					
						
							|  |  |  | 			data.error = req.flash('error')[0]; | 
					
						
							| 
									
										
										
										
											2015-08-26 15:54:54 -04:00
										 |  |  | 			data.title = '[[pages:register]]'; | 
					
						
							| 
									
										
										
										
											2015-06-28 21:54:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:register.build', {req: req, res: res, templateData: data}, next); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	], function(err, data) { | 
					
						
							|  |  |  | 		if (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
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 16:24:40 -04:00
										 |  |  | Controllers.compose = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-06-10 15:56:43 -04:00
										 |  |  | 	if (req.query.p && !res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2015-07-03 17:21:14 -04:00
										 |  |  | 		if (req.query.p.startsWith(nconf.get('relative_path'))) { | 
					
						
							|  |  |  | 			req.query.p = req.query.p.replace(nconf.get('relative_path'), ''); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-10 15:58:07 -04:00
										 |  |  | 		return helpers.redirect(res, req.query.p); | 
					
						
							| 
									
										
										
										
											2015-06-10 15:56:43 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 15:31:22 -04:00
										 |  |  | 	res.render('', {}); | 
					
						
							| 
									
										
										
										
											2015-05-21 16:24:40 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-27 16:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:04:41 -05:00
										 |  |  | Controllers.confirmEmail = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-02-25 14:10:00 -05:00
										 |  |  | 	user.email.confirm(req.params.code, function (err) { | 
					
						
							|  |  |  | 		res.render('confirm', { | 
					
						
							|  |  |  | 			error: err ? err.message : '' | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											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-12-01 20:28:36 -05:00
										 |  |  | 	if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) { | 
					
						
							| 
									
										
										
										
											2015-08-28 14:31:35 -04:00
										 |  |  | 		return next(); | 
					
						
							| 
									
										
										
										
											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 = { | 
					
						
							| 
									
										
										
										
											2015-07-30 18:53:20 +02:00
										 |  |  | 			url: validator.escape(url), | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 			title: meta.config.title, | 
					
						
							|  |  |  | 			breadcrumbs: helpers.buildBreadcrumbs([{text: '[[notifications:outgoing_link]]'}]) | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (url) { | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 		res.render('outgoing', data); | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-11-16 00:01:20 -05:00
										 |  |  | 		res.status(404).redirect(nconf.get('relative_path') + '/404'); | 
					
						
							| 
									
										
										
										
											2014-02-28 14:04:21 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 12:17:24 -05:00
										 |  |  | Controllers.termsOfUse = function(req, res, next) { | 
					
						
							|  |  |  | 	if (!meta.config.termsOfUse) { | 
					
						
							| 
									
										
										
										
											2015-08-28 14:31:35 -04:00
										 |  |  | 		return next(); | 
					
						
							| 
									
										
										
										
											2014-11-14 12:17:24 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	res.render('tos', {termsOfUse: meta.config.termsOfUse}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = Controllers; |