You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

421 lines
12 KiB
JavaScript

8 years ago
'use strict';
var async = require('async');
var nconf = require('nconf');
var validator = require('validator');
7 years ago
var request = require('request');
var meta = require('../meta');
var user = require('../user');
var plugins = require('../plugins');
Squashed commit of the following: commit 9c86d9b2904e14927cd7e9679b92aec0951d1063 Merge: ebfa63a 5a7f811 Author: Julian Lam <[email protected]> Date: Thu Jul 20 08:41:39 2017 -0400 Merge branch 'noscript-login' of https://github.com/An-dz/NodeBB into noscript commit 5a7f81185e8f9bd7d2d011c3d495988be7e437a3 Author: André Zanghelini <an_dz@simutrans-forum> Date: Mon Jul 17 23:07:14 2017 -0300 Rename clashing variable 'next' commit ebfa63a984073a58c17aa408c363cdb03ef89985 Merge: c1801cd f159d0d Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:40 2017 -0400 Merge branch 'noscript-logout' of https://github.com/An-dz/NodeBB into noscript commit c1801cda14e6363491e30b659902e2ae71f7e1f7 Merge: 7a5f9f3 9fd542d Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:31 2017 -0400 Merge branch 'noscript-register' of https://github.com/An-dz/NodeBB into noscript commit 7a5f9f35abc834bb72ddddc9ca07d34f2fde8353 Merge: 44851f9 d37b95c Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:10 2017 -0400 Merge branch 'noscript-compose' of https://github.com/An-dz/NodeBB into noscript commit f159d0d9ef1b7f600e830a96fdb4b9c87c79bb4a Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 12:16:38 2017 -0300 Prevent form submit Required for theme change commit d37b95cb71d32d4483190609798e244c331db165 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 01:49:52 2017 -0300 Prevent link action with scripts Required for the theme change that changes the buttons to `a` tags. commit 9fd542d8970b7d1a4126f4edc4b44eab7d708fb0 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:57:56 2017 -0300 Fix tests commit cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:09:17 2017 -0300 Update error handling commit 4ff11cd136a4fb98483f837e2cebc741380dfe76 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 17:29:08 2017 -0300 Remove async waterfall commit df01d44e821a70c984b89e9585a325c3e02c6e37 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:43 2017 -0300 Set noscript compose as noscript at start commit 4bcc380da72239b8315cc849a77a3036e06e4a12 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:12 2017 -0300 Remove last useless next commit b5eac6fea11e209934c0648a7e75ad07a2167123 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:35:08 2017 -0300 Last function requires no next commit 20a5cce6e6e32a454c304c448383707ec44c75a8 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:06:58 2017 -0300 Remove more useless next calls commit 85ee22a79bcbbb1995106f43d4c74d6ba9206cab Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 17:46:07 2017 -0300 Remove useless next calls commit 7d984c47ad24faac1fe537dee4a5a7d697e8634c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:45:31 2017 -0300 Support old themes commit 4a09dfbd08253115c342a9e829c4e6940cecb8cc Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:37:23 2017 -0300 Moved all error handling into helpers function commit 391aa6e67ef9ab67304005e14ac0633cdb630713 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jun 8 15:37:37 2017 -0300 ESLint - Fix mixed conditionals commit 80ccc6fd581d791f31e7ab62de8de611837bfc3c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:08:15 2017 -0300 Compose without scripts commit 2aca811256721238ca0cede4954213d369009885 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:00:44 2017 -0300 Register without scripts commit 097bb51577fb26f8e22f86dc274cb670ab606a8a Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:42:15 2017 -0300 Logout without scripts commit d497e08109891079656fee1c145043a9c0e55f2e Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:27:10 2017 -0300 Login without script
8 years ago
var topics = require('../topics');
var helpers = require('./helpers');
11 years ago
var Controllers = module.exports;
Controllers.topics = require('./topics');
Controllers.posts = require('./posts');
Controllers.categories = require('./categories');
Controllers.category = require('./category');
Controllers.unread = require('./unread');
Controllers.recent = require('./recent');
Controllers.popular = require('./popular');
Controllers.tags = require('./tags');
Controllers.search = require('./search');
8 years ago
Controllers.user = require('./user');
Controllers.users = require('./users');
Controllers.groups = require('./groups');
Controllers.accounts = require('./accounts');
Controllers.authentication = require('./authentication');
Controllers.api = require('./api');
Controllers.admin = require('./admin');
Controllers.globalMods = require('./globalmods');
Controllers.mods = require('./mods');
Controllers.sitemap = require('./sitemap');
8 years ago
Controllers.osd = require('./osd');
Controllers['404'] = require('./404');
Controllers.errors = require('./errors');
11 years ago
Controllers.home = function (req, res, next) {
var route = meta.config.homePageRoute || (meta.config.homePageCustom || '').replace(/^\/+/, '') || 'categories';
7 years ago
async.waterfall([
function (next) {
user.getSettings(req.uid, next);
},
function (settings, next) {
if (parseInt(meta.config.allowUserHomePage, 10) === 1 && settings.homePageRoute !== 'undefined' && settings.homePageRoute !== 'none') {
route = settings.homePageRoute || route;
}
7 years ago
var hook = 'action:homepage.get:' + route;
9 years ago
7 years ago
if (plugins.hasListeners(hook)) {
return plugins.fireHook(hook, {
req: req,
res: res,
next: next,
});
}
7 years ago
if (route === 'categories' || route === '/') {
Controllers.categories.list(req, res, next);
} else if (route === 'unread') {
Controllers.unread.get(req, res, next);
} else if (route === 'recent') {
Controllers.recent.get(req, res, next);
} else if (route === 'popular') {
Controllers.popular.get(req, res, next);
9 years ago
} else {
7 years ago
var match = /^category\/(\d+)\/(.*)$/.exec(route);
if (match) {
req.params.topic_index = '1';
req.params.category_id = match[1];
req.params.slug = match[2];
Controllers.category.get(req, res, next);
} else {
7 years ago
request.get(nconf.get('url') + '/' + route).pipe(res);
7 years ago
}
}
7 years ago
},
], next);
11 years ago
};
Controllers.reset = function (req, res, next) {
10 years ago
if (req.params.code) {
7 years ago
async.waterfall([
function (next) {
user.reset.validate(req.params.code, next);
},
function (valid) {
res.render('reset_code', {
valid: valid,
displayExpiryNotice: req.session.passwordExpired,
code: req.params.code,
minimumPasswordLength: parseInt(meta.config.minimumPasswordLength, 10),
breadcrumbs: helpers.buildBreadcrumbs([
{
text: '[[reset_password:reset_password]]',
url: '/reset',
},
{
text: '[[reset_password:update_password]]',
},
]),
title: '[[pages:reset]]',
});
delete req.session.passwordExpired;
},
], next);
10 years ago
} else {
res.render('reset', {
8 years ago
code: null,
8 years ago
breadcrumbs: helpers.buildBreadcrumbs([{
text: '[[reset_password:reset_password]]',
}]),
title: '[[pages:reset]]',
10 years ago
});
}
};
11 years ago
Controllers.login = function (req, res, next) {
9 years ago
var data = {};
var loginStrategies = require('../routes/authentication').getLoginStrategies();
var registrationType = meta.config.registrationType || 'normal';
var allowLoginWith = (meta.config.allowLoginWith || 'username-email');
var returnTo = (req.headers['x-return-to'] || '').replace(nconf.get('base_url'), '');
10 years ago
9 years ago
var errorText;
if (req.query.error === 'csrf-invalid') {
errorText = '[[error:csrf-invalid]]';
} else if (req.query.error) {
errorText = validator.escape(String(req.query.error));
9 years ago
}
8 years ago
if (returnTo) {
req.session.returnTo = returnTo;
}
10 years ago
data.alternate_logins = loginStrategies.length > 0;
data.authentication = loginStrategies;
10 years ago
data.allowLocalLogin = parseInt(meta.config.allowLocalLogin, 10) === 1 || parseInt(req.query.local, 10) === 1;
data.allowRegistration = registrationType === 'normal' || registrationType === 'admin-approval' || registrationType === 'admin-approval-ip';
9 years ago
data.allowLoginWith = '[[login:' + allowLoginWith + ']]';
8 years ago
data.breadcrumbs = helpers.buildBreadcrumbs([{
text: '[[global:login]]',
}]);
9 years ago
data.error = req.flash('error')[0] || errorText;
data.title = '[[pages:login]]';
9 years ago
if (!data.allowLocalLogin && !data.allowRegistration && data.alternate_logins && data.authentication.length === 1) {
9 years ago
if (res.locals.isAPI) {
return helpers.redirect(res, {
external: nconf.get('relative_path') + data.authentication[0].url,
9 years ago
});
}
return res.redirect(nconf.get('relative_path') + data.authentication[0].url);
9 years ago
}
9 years ago
if (req.uid) {
user.getUserFields(req.uid, ['username', 'email'], function (err, user) {
9 years ago
if (err) {
return next(err);
}
data.username = allowLoginWith === 'email' ? user.email : user.username;
data.alternate_logins = false;
9 years ago
res.render('login', data);
});
} else {
res.render('login', data);
}
};
Controllers.register = function (req, res, next) {
10 years ago
var registrationType = meta.config.registrationType || 'normal';
if (registrationType === 'disabled') {
10 years ago
return next();
11 years ago
}
11 years ago
9 years ago
var errorText;
if (req.query.error === 'csrf-invalid') {
errorText = '[[error:csrf-invalid]]';
}
10 years ago
async.waterfall([
function (next) {
if (registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
10 years ago
user.verifyInvitation(req.query, next);
} else {
next();
}
},
function (next) {
8 years ago
plugins.fireHook('filter:parse.post', {
postData: {
content: meta.config.termsOfUse || '',
},
}, next);
},
], function (err, termsOfUse) {
10 years ago
if (err) {
return next(err);
}
9 years ago
var loginStrategies = require('../routes/authentication').getLoginStrategies();
var data = {
'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12',
alternate_logins: !!loginStrategies.length,
9 years ago
};
data.authentication = loginStrategies;
data.minimumUsernameLength = parseInt(meta.config.minimumUsernameLength, 10);
data.maximumUsernameLength = parseInt(meta.config.maximumUsernameLength, 10);
data.minimumPasswordLength = parseInt(meta.config.minimumPasswordLength, 10);
8 years ago
data.minimumPasswordStrength = parseInt(meta.config.minimumPasswordStrength || 0, 10);
9 years ago
data.termsOfUse = termsOfUse.postData.content;
8 years ago
data.breadcrumbs = helpers.buildBreadcrumbs([{
text: '[[register:register]]',
}]);
9 years ago
data.regFormEntry = [];
9 years ago
data.error = req.flash('error')[0] || errorText;
9 years ago
data.title = '[[pages:register]]';
res.render('register', data);
});
};
Controllers.registerInterstitial = function (req, res, next) {
if (!req.session.hasOwnProperty('registration')) {
return res.redirect(nconf.get('relative_path') + '/register');
}
async.waterfall([
function (next) {
plugins.fireHook('filter:register.interstitial', {
userData: req.session.registration,
interstitials: [],
}, next);
},
function (data, next) {
if (!data.interstitials.length) {
// No interstitials, redirect to home
delete req.session.registration;
return res.redirect('/');
}
var renders = data.interstitials.map(function (interstitial) {
return async.apply(req.app.render.bind(req.app), interstitial.template, interstitial.data || {});
});
async.parallel(renders, next);
},
function (sections) {
var errors = req.flash('error');
res.render('registerComplete', {
9 years ago
title: '[[pages:registration-complete]]',
errors: errors,
sections: sections,
});
},
], next);
};
Controllers.compose = function (req, res, next) {
plugins.fireHook('filter:composer.build', {
req: req,
res: res,
next: next,
templateData: {},
}, function (err, data) {
if (err) {
return next(err);
}
10 years ago
if (data.templateData.disabled) {
res.render('', {
title: '[[modules:composer.compose]]',
});
} else {
data.templateData.title = '[[modules:composer.compose]]';
res.render('compose', data.templateData);
}
});
10 years ago
};
Squashed commit of the following: commit 9c86d9b2904e14927cd7e9679b92aec0951d1063 Merge: ebfa63a 5a7f811 Author: Julian Lam <[email protected]> Date: Thu Jul 20 08:41:39 2017 -0400 Merge branch 'noscript-login' of https://github.com/An-dz/NodeBB into noscript commit 5a7f81185e8f9bd7d2d011c3d495988be7e437a3 Author: André Zanghelini <an_dz@simutrans-forum> Date: Mon Jul 17 23:07:14 2017 -0300 Rename clashing variable 'next' commit ebfa63a984073a58c17aa408c363cdb03ef89985 Merge: c1801cd f159d0d Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:40 2017 -0400 Merge branch 'noscript-logout' of https://github.com/An-dz/NodeBB into noscript commit c1801cda14e6363491e30b659902e2ae71f7e1f7 Merge: 7a5f9f3 9fd542d Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:31 2017 -0400 Merge branch 'noscript-register' of https://github.com/An-dz/NodeBB into noscript commit 7a5f9f35abc834bb72ddddc9ca07d34f2fde8353 Merge: 44851f9 d37b95c Author: Julian Lam <[email protected]> Date: Mon Jul 17 16:30:10 2017 -0400 Merge branch 'noscript-compose' of https://github.com/An-dz/NodeBB into noscript commit f159d0d9ef1b7f600e830a96fdb4b9c87c79bb4a Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 12:16:38 2017 -0300 Prevent form submit Required for theme change commit d37b95cb71d32d4483190609798e244c331db165 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 01:49:52 2017 -0300 Prevent link action with scripts Required for the theme change that changes the buttons to `a` tags. commit 9fd542d8970b7d1a4126f4edc4b44eab7d708fb0 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:57:56 2017 -0300 Fix tests commit cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:09:17 2017 -0300 Update error handling commit 4ff11cd136a4fb98483f837e2cebc741380dfe76 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 17:29:08 2017 -0300 Remove async waterfall commit df01d44e821a70c984b89e9585a325c3e02c6e37 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:43 2017 -0300 Set noscript compose as noscript at start commit 4bcc380da72239b8315cc849a77a3036e06e4a12 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:12 2017 -0300 Remove last useless next commit b5eac6fea11e209934c0648a7e75ad07a2167123 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:35:08 2017 -0300 Last function requires no next commit 20a5cce6e6e32a454c304c448383707ec44c75a8 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:06:58 2017 -0300 Remove more useless next calls commit 85ee22a79bcbbb1995106f43d4c74d6ba9206cab Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 17:46:07 2017 -0300 Remove useless next calls commit 7d984c47ad24faac1fe537dee4a5a7d697e8634c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:45:31 2017 -0300 Support old themes commit 4a09dfbd08253115c342a9e829c4e6940cecb8cc Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:37:23 2017 -0300 Moved all error handling into helpers function commit 391aa6e67ef9ab67304005e14ac0633cdb630713 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jun 8 15:37:37 2017 -0300 ESLint - Fix mixed conditionals commit 80ccc6fd581d791f31e7ab62de8de611837bfc3c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:08:15 2017 -0300 Compose without scripts commit 2aca811256721238ca0cede4954213d369009885 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:00:44 2017 -0300 Register without scripts commit 097bb51577fb26f8e22f86dc274cb670ab606a8a Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:42:15 2017 -0300 Logout without scripts commit d497e08109891079656fee1c145043a9c0e55f2e Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:27:10 2017 -0300 Login without script
8 years ago
Controllers.composePost = function (req, res) {
var body = req.body;
var data = {
uid: req.uid,
req: req,
timestamp: Date.now(),
content: body.content,
};
req.body.noscript = 'true';
if (!data.content) {
return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
}
if (body.tid) {
data.tid = body.tid;
topics.reply(data, function (err, result) {
if (err) {
return helpers.noScriptErrors(req, res, err.message, 400);
}
user.updateOnlineUsers(result.uid);
res.redirect(nconf.get('relative_path') + '/post/' + result.pid);
});
} else if (body.cid) {
data.cid = body.cid;
data.title = body.title;
data.tags = [];
data.thumb = '';
topics.post(data, function (err, result) {
if (err) {
return helpers.noScriptErrors(req, res, err.message, 400);
}
res.redirect(nconf.get('relative_path') + '/topic/' + result.topicData.slug);
});
}
};
Controllers.confirmEmail = function (req, res) {
user.email.confirm(req.params.code, function (err) {
res.render('confirm', {
9 years ago
error: err ? err.message : '',
title: '[[pages:confirm]]',
});
});
};
11 years ago
Controllers.robots = function (req, res) {
res.set('Content-Type', 'text/plain');
8 years ago
if (meta.config['robots:txt']) {
res.send(meta.config['robots:txt']);
} else {
8 years ago
res.send('User-agent: *\n' +
'Disallow: ' + nconf.get('relative_path') + '/admin/\n' +
'Sitemap: ' + nconf.get('url') + '/sitemap.xml');
}
};
Controllers.manifest = function (req, res) {
var manifest = {
8 years ago
name: meta.config.title || 'NodeBB',
start_url: nconf.get('relative_path') + '/',
display: 'standalone',
orientation: 'portrait',
icons: [],
8 years ago
};
if (meta.config['brand:touchIcon']) {
manifest.icons.push({
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-36.png',
sizes: '36x36',
type: 'image/png',
density: 0.75,
}, {
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-48.png',
sizes: '48x48',
type: 'image/png',
density: 1.0,
}, {
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-72.png',
sizes: '72x72',
type: 'image/png',
density: 1.5,
}, {
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-96.png',
sizes: '96x96',
type: 'image/png',
density: 2.0,
}, {
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-144.png',
sizes: '144x144',
type: 'image/png',
density: 3.0,
}, {
src: nconf.get('relative_path') + '/assets/uploads/system/touchicon-192.png',
sizes: '192x192',
type: 'image/png',
density: 4.0,
});
}
res.status(200).json(manifest);
};
Controllers.outgoing = function (req, res, next) {
9 years ago
var url = req.query.url || '';
var allowedProtocols = ['http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal'];
var parsed = require('url').parse(url);
if (!url || !allowedProtocols.includes(parsed.protocol.slice(0, -1))) {
return next();
}
res.render('outgoing', {
9 years ago
outgoing: validator.escape(String(url)),
title: meta.config.title,
8 years ago
breadcrumbs: helpers.buildBreadcrumbs([{
text: '[[notifications:outgoing_link]]',
}]),
});
};
Controllers.termsOfUse = function (req, res, next) {
10 years ago
if (!meta.config.termsOfUse) {
10 years ago
return next();
10 years ago
}
8 years ago
res.render('tos', {
termsOfUse: meta.config.termsOfUse,
});
10 years ago
};