Skip to content

Node.js based forum software built for the modern web

Merge remote-tracking branch 'origin/master' into webserver.js-refactor

psychobunnye84dd4fd94be
diff --cc public/templates/header.tpl
index 0000000000,a391d9fd04..25a5da4c57
mode 000000,100644..100644
--- a/public/templates/header.tpl
+++ b/public/templates/header.tpl
@@@ -1,0 -1,218 +1,218 @@@
-<!DOCTYPE html>
-<html>
-<head>
- <title>{browserTitle}</title>
- <!-- BEGIN metaTags -->
- <meta<!-- IF metaTags.name --> name="{metaTags.name}"<!-- ENDIF metaTags.name --><!-- IF metaTags.property --> property="{metaTags.property}"<!-- ENDIF metaTags.property --><!-- IF metaTags.content --> content="{metaTags.content}"<!-- ENDIF metaTags.content --> />
- <!-- END metaTags -->
- <link rel="stylesheet" href="{relative_path}/vendor/fontawesome/css/font-awesome.min.css">
- <link rel="stylesheet" type="text/css" href="{relative_path}/stylesheet.css?{cache-buster}" />
- <!-- IF bootswatchCSS --><link href="{bootswatchCSS}" rel="stylesheet" media="screen"><!-- ENDIF bootswatchCSS -->
- <!-- BEGIN linkTags -->
- <link<!-- IF linkTags.link --> link="{linkTags.link}"<!-- ENDIF linkTags.link --><!-- IF linkTags.rel --> rel="{linkTags.rel}"<!-- ENDIF linkTags.rel --><!-- IF linkTags.type --> type="{linkTags.type}"<!-- ENDIF linkTags.type --><!-- IF linkTags.href --> href="{linkTags.href}"<!-- ENDIF linkTags.href --> />
- <!-- END linkTags -->
- <!-- IF useCustomCSS -->
- <style type="text/css">{customCSS}</style>
- <!-- ENDIF useCustomCSS -->
-
- <!--[if lt IE 9]>
- <script src="//cdnjs.cloudflare.com/ajax/libs/es5-shim/2.3.0/es5-shim.min.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
- <script>__lt_ie_9__ = 1;</script>
- <![endif]-->
-
- <script>
- var RELATIVE_PATH = "{relative_path}";
- </script>
- <script src="{relative_path}/socket.io/socket.io.js"></script>
- <!-- BEGIN clientScripts -->
- <script src="{relative_path}/{clientScripts.script}?{cache-buster}"></script>
- <!-- END clientScripts -->
- <script>
- require.config({
- baseUrl: "{relative_path}/src/modules",
- waitSeconds: 3,
- urlArgs: "{cache-buster}",
- paths: {
- "forum": '../forum'
- }
- });
- </script>
-</head>
-
-<body>
- <div class="navbar navbar-inverse navbar-fixed-top header" role="navigation" id="header-menu">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <div>
- <a href="{relative_path}/">
- <img class="{brand:logo:display} forum-logo" src="{brand:logo}" />
- </a>
- <a href="{relative_path}/">
- <h1 class="navbar-brand forum-title">{title}</h1>
- </a>
-
- <div class="header-topic-title visible-xs">
- <span></span>
- </div>
-
- </div>
- </div>
-
- <div class="navbar-collapse collapse navbar-ex1-collapse">
- <ul id="main-nav" class="nav navbar-nav pull-left">
- <li class="nodebb-loggedin">
- <a href="{relative_path}/unread"><i id="unread-count" class="fa fa-fw fa-inbox" data-content="0" title="[[global:header.unread]]"></i><span class="visible-xs-inline"> [[global:header.unread]]</span></a>
- </li>
- <li>
- <a href="{relative_path}/recent"><i class="fa fa-fw fa-clock-o" title="[[global:header.recent]]"></i><span class="visible-xs-inline"> [[global:header.recent]]</span></a>
- </li>
- <li>
- <a href="{relative_path}/popular"><i class="fa fa-fw fa-fire" title="[[global:header.popular]]"></i><span class="visible-xs-inline"> [[global:header.popular]]</span></a>
- </li>
- <li>
- <a href="{relative_path}/users"><i class="fa fa-fw fa-users" title="[[global:header.users]]"></i><span class="visible-xs-inline"> [[global:header.users]]</span></a>
- </li>
- <!-- IF isAdmin -->
- <li>
- <a href="{relative_path}/admin"><i class="fa fa-fw fa-cogs" title="[[global:header.admin]]"></i><span class="visible-xs-inline"> [[global:header.admin]]</span></a>
- </li>
- <!-- ENDIF isAdmin -->
-
- <!-- IF searchEnabled -->
- <li class="visible-xs">
- <a id="mobile-search-button" href="{relative_path}/search"><i class="fa fa-search fa-fw" title="[[global:header.search]]"></i> [[global:header.search]]</a>
- </li>
- <!-- ENDIF searchEnabled -->
-
- <!-- BEGIN navigation -->
- <li class="{navigation.class}">
- <a href="{relative_path}{navigation.route}" title="{navigation.title}">
- <!-- IF navigation.iconClass -->
- <i class="fa fa-fw {navigation.iconClass}"></i>
- <!-- ENDIF navigation.iconClass -->
-
- <!-- IF navigation.text -->
- <span class="{navigation.textClass}">{navigation.text}</span>
- <!-- ENDIF navigation.text -->
- </a>
- </li>
- <!-- END navigation -->
- </ul>
-
- <ul id="logged-in-menu" class="nav navbar-nav navbar-right hide pull-right">
- <li>
- <a href="#" id="reconnect" class="hide" title="Connection to {title} has been lost, attempting to reconnect..."><i class="fa fa-check"></i></a>
- </li>
-
- <li class="notifications dropdown text-center hidden-xs">
- <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="notif_dropdown"><i class="fa fa-fw fa-bell-o" data-content="0" title="[[global:header.notifications]]"></i></a>
- <ul id="notif-list" class="dropdown-menu" aria-labelledby="notif_dropdown">
- <li>
- <a href="#"><i class="fa fa-refresh fa-spin"></i> [[global:notifications.loading]]</a>
- </li>
- </ul>
- </li>
- <li class="visible-xs">
- <a href="{relative_path}/notifications"><i class="fa fa-exclamation-triangle fa-fw" title="[[notifications:title]]"></i> [[notifications:title]]</a>
- </li>
-
- <li class="chats dropdown">
- <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="chat_dropdown"><i class="fa fa-comment-o fa-fw" title="[[global:header.chats]]"></i> <span class="visible-xs-inline">[[global:header.chats]]</span></a>
- <ul id="chat-list" class="dropdown-menu" aria-labelledby="chat_dropdown">
- <li>
- <a href="#"><i class="fa fa-refresh fa-spin"></i> [[global:chats.loading]]</a>
- </li>
- </ul>
- </li>
-
- <li id="user_label" class="dropdown">
- <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="user_dropdown" title="[[global:header.profile]]">
- <img src=""/>
- </a>
- <ul id="user-control-list" class="dropdown-menu" aria-labelledby="user_dropdown">
- <li>
- <a id="user-profile-link" href=""><i class="fa fa-circle status-offline"></i><span>[[global:header.profile]]</span></a>
- </li>
- <li id="logout-link">
- <a href="#">[[global:logout]]</a>
- </li>
- <li role="presentation" class="divider"></li>
- <li>
- <a href="#" class="user-status" data-status="online"><i class="fa fa-circle status online"></i><span> [[global:online]]</span></a>
- </li>
- <li>
- <a href="#" class="user-status" data-status="away"><i class="fa fa-circle status away"></i><span> [[global:away]]</span></a>
- </li>
- <li>
- <a href="#" class="user-status" data-status="dnd"><i class="fa fa-circle status dnd"></i><span> [[global:dnd]]</span></a>
- </li>
- <li>
- <a href="#" class="user-status" data-status="offline"><i class="fa fa-circle status offline"></i><span> [[global:invisible]]</span></a>
- </li>
- </ul>
- </li>
- </ul>
-
- <ul id="logged-out-menu" class="nav navbar-nav navbar-right pull-right">
- <!-- IF allowRegistration -->
- <li>
- <a href="{relative_path}/register">
- <i class="fa fa-pencil visible-xs-inline"></i>
- <span>[[global:register]]</span>
- </a>
- </li>
- <!-- ENDIF allowRegistration -->
- <li>
- <a href="{relative_path}/login">
- <i class="fa fa-sign-in visible-xs-inline"></i>
- <span>[[global:login]]</span>
- </a>
- </li>
- </ul>
-
- <!-- IF searchEnabled -->
- <ul id="logged-conditional-menu" class="nav navbar-nav navbar-right">
- <li>
- <form id="search-form" class="navbar-form navbar-right hidden-xs" role="search" method="GET" action="">
- <div class="hide" id="search-fields">
- <div class="form-group">
- <input type="text" class="form-control" placeholder="[[global:search]]" name="query" value="">
- </div>
- <button type="submit" class="btn btn-default hide">[[global:search]]</button>
- </div>
- <button id="search-button" type="button" class="btn btn-link hide"><i class="fa fa-search fa-fw" title="[[global:header.search]]"></i></button>
- </form>
- </li>
- </ul>
- <!-- ENDIF searchEnabled -->
-
- <ul class="nav navbar-nav navbar-right pagination-block hidden visible-lg visible-md">
- <li class="active">
- <a href="#">
- <i class="fa fa-chevron-up pointer"></i>
- <span id="pagination"></span>
- <i class="fa fa-chevron-down pointer"></i>
- <div class="progress-container">
- <div class="progress-bar"></div>
- </div>
- </a>
- </li>
- </ul>
-
- <div class="header-topic-title pull-right hidden-xs">
- <span></span>
- </div>
- </div>
- </div>
- </div>
-
- <input id="csrf_token" type="hidden" template-variable="csrf" value="{csrf}" />
-
- <div class="container" id="content">
++<!DOCTYPE html>
++<html>
++<head>
++ <title>{browserTitle}</title>
++ <!-- BEGIN metaTags -->
++ <meta<!-- IF metaTags.name --> name="{metaTags.name}"<!-- ENDIF metaTags.name --><!-- IF metaTags.property --> property="{metaTags.property}"<!-- ENDIF metaTags.property --><!-- IF metaTags.content --> content="{metaTags.content}"<!-- ENDIF metaTags.content --> />
++ <!-- END metaTags -->
++ <link rel="stylesheet" href="{relative_path}/vendor/fontawesome/css/font-awesome.min.css">
++ <link rel="stylesheet" type="text/css" href="{relative_path}/stylesheet.css?{cache-buster}" />
++ <!-- IF bootswatchCSS --><link href="{bootswatchCSS}" rel="stylesheet" media="screen"><!-- ENDIF bootswatchCSS -->
++ <!-- BEGIN linkTags -->
++ <link<!-- IF linkTags.link --> link="{linkTags.link}"<!-- ENDIF linkTags.link --><!-- IF linkTags.rel --> rel="{linkTags.rel}"<!-- ENDIF linkTags.rel --><!-- IF linkTags.type --> type="{linkTags.type}"<!-- ENDIF linkTags.type --><!-- IF linkTags.href --> href="{linkTags.href}"<!-- ENDIF linkTags.href --> />
++ <!-- END linkTags -->
++ <!-- IF useCustomCSS -->
++ <style type="text/css">{customCSS}</style>
++ <!-- ENDIF useCustomCSS -->
++
++ <!--[if lt IE 9]>
++ <script src="//cdnjs.cloudflare.com/ajax/libs/es5-shim/2.3.0/es5-shim.min.js"></script>
++ <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.js"></script>
++ <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
++ <script>__lt_ie_9__ = 1;</script>
++ <![endif]-->
++
++ <script>
++ var RELATIVE_PATH = "{relative_path}";
++ </script>
++ <script src="{relative_path}/socket.io/socket.io.js"></script>
++ <!-- BEGIN clientScripts -->
++ <script src="{relative_path}/{clientScripts.script}?{cache-buster}"></script>
++ <!-- END clientScripts -->
++ <script>
++ require.config({
++ baseUrl: "{relative_path}/src/modules",
++ waitSeconds: 3,
++ urlArgs: "{cache-buster}",
++ paths: {
++ "forum": '../forum'
++ }
++ });
++ </script>
++</head>
++
++<body>
++ <div class="navbar navbar-inverse navbar-fixed-top header" role="navigation" id="header-menu">
++ <div class="container">
++ <div class="navbar-header">
++ <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
++ <span class="icon-bar"></span>
++ <span class="icon-bar"></span>
++ <span class="icon-bar"></span>
++ </button>
++ <div>
++ <a href="{relative_path}/">
++ <img class="{brand:logo:display} forum-logo" src="{brand:logo}" />
++ </a>
++ <a href="{relative_path}/">
++ <h1 class="navbar-brand forum-title">{title}</h1>
++ </a>
++
++ <div class="header-topic-title visible-xs">
++ <span></span>
++ </div>
++
++ </div>
++ </div>
++
++ <div class="navbar-collapse collapse navbar-ex1-collapse">
++ <ul id="main-nav" class="nav navbar-nav pull-left">
++ <li class="nodebb-loggedin">
++ <a href="{relative_path}/unread"><i id="unread-count" class="fa fa-fw fa-inbox" data-content="0" title="[[global:header.unread]]"></i><span class="visible-xs-inline"> [[global:header.unread]]</span></a>
++ </li>
++ <li>
++ <a href="{relative_path}/recent"><i class="fa fa-fw fa-clock-o" title="[[global:header.recent]]"></i><span class="visible-xs-inline"> [[global:header.recent]]</span></a>
++ </li>
++ <li>
++ <a href="{relative_path}/popular"><i class="fa fa-fw fa-fire" title="[[global:header.popular]]"></i><span class="visible-xs-inline"> [[global:header.popular]]</span></a>
++ </li>
++ <li>
++ <a href="{relative_path}/users"><i class="fa fa-fw fa-users" title="[[global:header.users]]"></i><span class="visible-xs-inline"> [[global:header.users]]</span></a>
++ </li>
++ <!-- IF isAdmin -->
++ <li>
++ <a href="{relative_path}/admin"><i class="fa fa-fw fa-cogs" title="[[global:header.admin]]"></i><span class="visible-xs-inline"> [[global:header.admin]]</span></a>
++ </li>
++ <!-- ENDIF isAdmin -->
++
++ <!-- IF searchEnabled -->
++ <li class="visible-xs">
++ <a id="mobile-search-button" href="{relative_path}/search"><i class="fa fa-search fa-fw" title="[[global:header.search]]"></i> [[global:header.search]]</a>
++ </li>
++ <!-- ENDIF searchEnabled -->
++
++ <!-- BEGIN navigation -->
++ <li class="{navigation.class}">
++ <a href="{relative_path}{navigation.route}" title="{navigation.title}">
++ <!-- IF navigation.iconClass -->
++ <i class="fa fa-fw {navigation.iconClass}"></i>
++ <!-- ENDIF navigation.iconClass -->
++
++ <!-- IF navigation.text -->
++ <span class="{navigation.textClass}">{navigation.text}</span>
++ <!-- ENDIF navigation.text -->
++ </a>
++ </li>
++ <!-- END navigation -->
++ </ul>
++
++ <ul id="logged-in-menu" class="nav navbar-nav navbar-right hide pull-right">
++ <li>
++ <a href="#" id="reconnect" class="hide" title="Connection to {title} has been lost, attempting to reconnect..."><i class="fa fa-check"></i></a>
++ </li>
++
++ <li class="notifications dropdown text-center hidden-xs">
++ <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="notif_dropdown"><i class="fa fa-fw fa-bell-o" data-content="0" title="[[global:header.notifications]]"></i></a>
++ <ul id="notif-list" class="dropdown-menu" aria-labelledby="notif_dropdown">
++ <li>
++ <a href="#"><i class="fa fa-refresh fa-spin"></i> [[global:notifications.loading]]</a>
++ </li>
++ </ul>
++ </li>
++ <li class="visible-xs">
++ <a href="{relative_path}/notifications"><i class="fa fa-exclamation-triangle fa-fw" title="[[notifications:title]]"></i> [[notifications:title]]</a>
++ </li>
++
++ <li class="chats dropdown">
++ <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="chat_dropdown"><i class="fa fa-comment-o fa-fw" title="[[global:header.chats]]"></i> <span class="visible-xs-inline">[[global:header.chats]]</span></a>
++ <ul id="chat-list" class="dropdown-menu" aria-labelledby="chat_dropdown">
++ <li>
++ <a href="#"><i class="fa fa-refresh fa-spin"></i> [[global:chats.loading]]</a>
++ </li>
++ </ul>
++ </li>
++
++ <li id="user_label" class="dropdown">
++ <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="user_dropdown" title="[[global:header.profile]]">
++ <img src=""/>
++ </a>
++ <ul id="user-control-list" class="dropdown-menu" aria-labelledby="user_dropdown">
++ <li>
++ <a id="user-profile-link" href=""><i class="fa fa-circle status-offline"></i><span>[[global:header.profile]]</span></a>
++ </li>
++ <li id="logout-link">
++ <a href="#">[[global:logout]]</a>
++ </li>
++ <li role="presentation" class="divider"></li>
++ <li>
++ <a href="#" class="user-status" data-status="online"><i class="fa fa-circle status online"></i><span> [[global:online]]</span></a>
++ </li>
++ <li>
++ <a href="#" class="user-status" data-status="away"><i class="fa fa-circle status away"></i><span> [[global:away]]</span></a>
++ </li>
++ <li>
++ <a href="#" class="user-status" data-status="dnd"><i class="fa fa-circle status dnd"></i><span> [[global:dnd]]</span></a>
++ </li>
++ <li>
++ <a href="#" class="user-status" data-status="offline"><i class="fa fa-circle status offline"></i><span> [[global:invisible]]</span></a>
++ </li>
++ </ul>
++ </li>
++ </ul>
++
++ <ul id="logged-out-menu" class="nav navbar-nav navbar-right pull-right">
++ <!-- IF allowRegistration -->
++ <li>
++ <a href="{relative_path}/register">
++ <i class="fa fa-pencil visible-xs-inline"></i>
++ <span>[[global:register]]</span>
++ </a>
++ </li>
++ <!-- ENDIF allowRegistration -->
++ <li>
++ <a href="{relative_path}/login">
++ <i class="fa fa-sign-in visible-xs-inline"></i>
++ <span>[[global:login]]</span>
++ </a>
++ </li>
++ </ul>
++
++ <!-- IF searchEnabled -->
++ <ul id="logged-conditional-menu" class="nav navbar-nav navbar-right">
++ <li>
++ <form id="search-form" class="navbar-form navbar-right hidden-xs" role="search" method="GET" action="">
++ <div class="hide" id="search-fields">
++ <div class="form-group">
++ <input type="text" class="form-control" placeholder="[[global:search]]" name="query" value="">
++ </div>
++ <button type="submit" class="btn btn-default hide">[[global:search]]</button>
++ </div>
++ <button id="search-button" type="button" class="btn btn-link hide"><i class="fa fa-search fa-fw" title="[[global:header.search]]"></i></button>
++ </form>
++ </li>
++ </ul>
++ <!-- ENDIF searchEnabled -->
++
++ <ul class="nav navbar-nav navbar-right pagination-block hidden visible-lg visible-md">
++ <li class="active">
++ <a href="#">
++ <i class="fa fa-chevron-up pointer"></i>
++ <span id="pagination"></span>
++ <i class="fa fa-chevron-down pointer"></i>
++ <div class="progress-container">
++ <div class="progress-bar"></div>
++ </div>
++ </a>
++ </li>
++ </ul>
++
++ <div class="header-topic-title pull-right hidden-xs">
++ <span></span>
++ </div>
++ </div>
++ </div>
++ </div>
++
++ <input id="csrf_token" type="hidden" template-variable="csrf" value="{csrf}" />
++
++ <div class="container" id="content">
diff --cc src/middleware/middleware.js
index e2b0948cb2,0000000000..4407adc595
mode 100644,000000..100644
--- a/src/middleware/middleware.js
+++ b/src/middleware/middleware.js
@@@ -1,335 -1,0 +1,334 @@@
+"use strict";
+
+var app,
+ clientScripts,
+ middleware = {},
+ async = require('async'),
+ path = require('path'),
+ winston = require('winston'),
+ validator = require('validator'),
+ fs = require('fs'),
+ nconf = require('nconf'),
+ plugins = require('./../plugins'),
+ meta = require('./../meta'),
+ translator = require('./../../public/src/translator'),
+ user = require('./../user'),
+ db = require('./../database'),
+
+ controllers = {
+ api: require('./../controllers/api')
+ };
+
+middleware.authenticate = function(req, res, next) {
+ if(!req.user) {
+ if (res.locals.isAPI) {
+ return res.json(403, 'not-allowed');
+ } else {
+ return res.redirect('403');
+ }
+ } else {
+ next();
+ }
+};
+
+middleware.updateLastOnlineTime = function(req, res, next) {
+ if(req.user) {
+ user.updateLastOnlineTime(req.user.uid);
+ }
+
+ db.sortedSetAdd('ip:recent', Date.now(), req.ip || 'Unknown');
+
+ next();
+};
+
+middleware.prepareAPI = function(req, res, next) {
+ res.locals.isAPI = true;
+ next();
+};
+
+middleware.guestSearchingAllowed = function(req, res, next) {
+ if (!req.user && meta.config.allowGuestSearching !== '1') {
+ return res.redirect('/403');
+ }
+};
+
+middleware.checkGlobalPrivacySettings = function(req, res, next) {
+ var callerUID = req.user ? parseInt(req.user.uid, 10) : 0;
+
+ if (!callerUID && !!parseInt(meta.config.privateUserInfo, 10)) {
+ if (res.locals.isAPI) {
+ return res.json(403, 'not-allowed');
+ } else {
+ return res.redirect('403');
+ }
+ }
+
+ next();
+};
+
+middleware.checkAccountPermissions = function(req, res, next) {
+ var callerUID = req.user ? parseInt(req.user.uid, 10) : 0;
+
+ // this function requires userslug to be passed in. todo: /user/uploadpicture should pass in userslug I think
+ user.getUidByUserslug(req.params.userslug, function (err, uid) {
+ if (err) {
+ return next(err);
+ }
+
+ // not sure if this check really should belong here. also make sure we're not doing this check again in the actual method
+ if (!uid) {
+ if (res.locals.isAPI) {
+ return res.json(404);
+ } else {
+ return res.redirect('404');
+ }
+ }
+
+ if (parseInt(uid, 10) === callerUID) {
+ return next();
+ }
+
+ user.isAdministrator(callerUID, function(err, isAdmin) {
+ if(err) {
+ return next(err);
+ }
+
+ if(isAdmin) {
+ next();
+ }
+
+ if (res.locals.isAPI) {
+ return res.json(403, 'not-allowed');
+ } else {
+ return res.redirect('403');
+ }
+ });
+ });
+};
+
+middleware.buildHeader = function(req, res, next) {
+ async.parallel([
+ function(next) {
+ res.locals.renderHeader = true;
+ next();
+ },
+ function(next) {
+ controllers.api.getConfig(req, res, function(err, config) {
+ res.locals.config = config;
+ next();
+ });
+ },
+ function(next) {
+ // this is slower than the original implementation because the rendered template is not cached
+ // but I didn't bother to fix this because we will deprecate [filter:footer.build] in favour of the widgets system by 0.4x
+ plugins.fireHook('filter:footer.build', '', function(err, appendHTML) {
+ app.render('footer', {footerHTML: appendHTML}, function(err, template) {
+ translator.translate(template, function(parsedTemplate) {
+ res.locals.footer = template;
+ next(err);
+ });
+ });
+ });
+ }
+ ], function(err) {
+ next(err);
+ });
+};
+
+middleware.renderHeader = function(req, res, callback) {
+ var custom_header = {
+ 'navigation': []
+ };
+
+ plugins.fireHook('filter:header.build', custom_header, function(err, custom_header) {
+ var defaultMetaTags = [{
+ name: 'viewport',
+ content: 'width=device-width, initial-scale=1.0, user-scalable=no'
+ }, {
+ name: 'content-type',
+ content: 'text/html; charset=UTF-8'
+ }, {
+ name: 'apple-mobile-web-app-capable',
+ content: 'yes'
+ }, {
+ property: 'og:site_name',
+ content: meta.config.title || 'NodeBB'
+ }, {
+ property: 'keywords',
+ content: meta.config.keywords || ''
+ }],
+ defaultLinkTags = [{
+ rel: 'apple-touch-icon',
+ href: '/apple-touch-icon'
+ }],
+ templateValues = {
+ bootswatchCSS: meta.config['theme:src'],
- pluginCSS: plugins.cssFiles.map(function(file) { return { path: nconf.get('relative_path') + file.replace(/\\/g, '/') }; }),
+ title: meta.config.title || '',
+ description: meta.config.description || '',
+ 'brand:logo': meta.config['brand:logo'] || '',
+ 'brand:logo:display': meta.config['brand:logo']?'':'hide',
+ csrf: res.locals.csrf_token,
+ relative_path: nconf.get('relative_path'),
+ clientScripts: clientScripts,
+ navigation: custom_header.navigation,
+ 'cache-buster': meta.config['cache-buster'] ? 'v=' + meta.config['cache-buster'] : '',
+ allowRegistration: meta.config.allowRegistration === undefined || parseInt(meta.config.allowRegistration, 10) === 1,
+ searchEnabled: plugins.hasListeners('filter:search.query') ? true : false
+ },
+ escapeList = {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ "'": '&apos;',
+ '"': '&quot;'
+ };
+
+ for (var key in res.locals.config) {
+ if (res.locals.config.hasOwnProperty(key)) {
+ templateValues[key] = res.locals.config[key];
+ }
+ }
+
+ var uid = '0';
+
+ templateValues.metaTags = defaultMetaTags.concat(res.locals.metaTags || []).map(function(tag) {
+ if(!tag || typeof tag.content !== 'string') {
+ winston.warn('Invalid meta tag. ', tag);
+ return tag;
+ }
+
+ tag.content = tag.content.replace(/[&<>'"]/g, function(tag) {
+ return escapeList[tag] || tag;
+ });
+ return tag;
+ });
+
+ templateValues.linkTags = defaultLinkTags.concat(res.locals.linkTags || []);
+ templateValues.linkTags.push({
+ rel: "icon",
+ type: "image/x-icon",
+ href: nconf.get('relative_path') + '/favicon.ico'
+ });
+
+ if(req.user && req.user.uid) {
+ uid = req.user.uid;
+ }
+
+ templateValues.useCustomCSS = false;
+ if (meta.config.useCustomCSS === '1') {
+ templateValues.useCustomCSS = true;
+ templateValues.customCSS = meta.config.customCSS;
+ }
+
+ async.parallel([
+ function(next) {
+ translator.get('pages:' + path.basename(req.url), function(translated) {
+ var metaTitle = templateValues.metaTags.filter(function(tag) {
+ return tag.name === 'title';
+ });
+
+ if (translated) {
+ templateValues.browserTitle = translated;
+ } else if (metaTitle.length > 0 && metaTitle[0].content) {
+ templateValues.browserTitle = metaTitle[0].content;
+ } else {
+ templateValues.browserTitle = meta.config.browserTitle || 'NodeBB';
+ }
+
+ next();
+ });
+ },
+ function(next) {
+ user.isAdministrator(uid, function(err, isAdmin) {
+ templateValues.isAdmin = isAdmin || false;
+ next();
+ });
+ }
+ ], function() {
+ app.render('header', templateValues, function(err, template) {
+ callback(null, template);
+ });
+ });
+ });
+};
+
+middleware.processRender = function(req, res, next) {
+ // res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
+ var render = res.render;
+ res.render = function(template, options, fn) {
+ var self = this,
+ options = options || {},
+ req = this.req,
+ app = req.app,
+ defaultFn = function(err, str){
+ if (err) {
+ return req.next(err);
+ }
+
+ self.send(str);
+ };
+
+ if ('function' === typeof options) {
+ fn = options, options = {};
+ }
+
+ if ('function' !== typeof fn) {
+ fn = defaultFn;
+ }
+
+ render.call(self, template, options, function(err, str) {
+ if (res.locals.footer) {
+ str = str + res.locals.footer;
+ } else if (res.locals.adminFooter) {
+ str = str + res.locals.adminFooter;
+ }
+
+ if (res.locals.renderHeader) {
+ middleware.renderHeader(req, res, function(err, template) {
+ str = template + str;
+
+ translator.translate(str, function(translated) {
+ fn(err, translated);
+ });
+ });
+ } else if (res.locals.adminHeader) {
+ str = res.locals.adminHeader + str;
+ fn(err, str);
+ } else {
+ fn(err, str);
+ }
+ });
+ };
+
+ next();
+};
+
+middleware.routeTouchIcon = function(req, res) {
+ if (meta.config['brand:logo'] && validator.isURL(meta.config['brand:logo'])) {
+ return res.redirect(meta.config['brand:logo']);
+ } else {
+ return res.sendfile(path.join(__dirname, '../../public', meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'), {
+ maxAge: app.enabled('cache') ? 5184000000 : 0
+ });
+ }
+};
+
+module.exports = function(webserver) {
+ app = webserver;
+ middleware.admin = require('./admin')(webserver);
+
+ plugins.ready(function() {
+ // Minify client-side libraries
+ meta.js.get(function (err, scripts) {
+ clientScripts = scripts.map(function (script) {
+ script = {
+ script: script
+ };
+
+ return script;
+ });
+ });
+ });
+
+ return middleware;
+};
diff --cc src/routes/meta.js
index f2d2af8ad6,0629bc25a7..ad74931749
--- a/src/routes/meta.js
+++ b/src/routes/meta.js
@@@ -4,75 -2,71 +4,80 @@@ var path = require('path')
nconf = require('nconf'),
less = require('less'),
- meta = require('../meta'),
- db = require('../database'),
- plugins = require('../plugins');
+ meta = require('./../meta'),
+ db = require('./../database'),
+ plugins = require('./../plugins'),
-(function (Meta) {
- Meta.createRoutes = function(app) {
- app.get('/stylesheet.css', function(req, res) {
- if (meta.css.cache) {
- res.type('text/css').send(200, meta.css.cache);
- return;
- }
+ minificationEnabled = false;
+
+
+function sendMinifiedJS(req, res, next) {
+ function sendCached() {
+ return res.type('text/javascript').send(meta.js.cache);
+ }
- db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
- var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
- baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
- paths = [baseThemePath, path.join(__dirname, '../../node_modules')],
- source = '@import "./theme";',
- x, numLESS;
+ if (meta.js.cache) {
+ sendCached();
+ } else {
+ if (minificationEnabled) {
+ meta.js.minify(function() {
+ sendCached();
+ });
+ } else {
+ // Compress only
+ meta.js.concatenate(function() {
+ sendCached();
+ });
+ }
+ }
+}
- // Add the imports for each LESS file
- for(x=0,numLESS=plugins.lessFiles.length;x<numLESS;x++) {
- source += '\n@import "./' + plugins.lessFiles[x] + '";';
- }
+function sendStylesheet(req, res, next) {
+ if (meta.css.cache) {
+ res.type('text/css').send(200, meta.css.cache);
+ return;
+ }
- // ... and for each CSS file
- for(x=0,numCSS=plugins.cssFiles.length;x<numCSS;x++) {
- source += '\n@import (inline) "./' + plugins.cssFiles[x] + '";';
- }
+ db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
+ var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
+ baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
+ paths = [baseThemePath, path.join(__dirname, '../../node_modules')],
+ source = '@import "./theme";',
+ x, numLESS;
- var parser = new (less.Parser)({
- paths: paths
- });
+ // Add the imports for each LESS file
+ for(x=0,numLESS=plugins.lessFiles.length;x<numLESS;x++) {
+ source += '\n@import "./' + plugins.lessFiles[x] + '";';
+ }
- parser.parse(source, function(err, tree) {
- if (err) {
- res.send(500, err.message);
- return;
- }
++ // ... and for each CSS file
++ for(x=0,numCSS=plugins.cssFiles.length;x<numCSS;x++) {
++ source += '\n@import (inline) "./' + plugins.cssFiles[x] + '";';
++ }
+
- meta.css.cache = tree.toCSS({
- compress: true
- });
- res.type('text/css').send(200, meta.css.cache);
- });
+ var parser = new (less.Parser)({
+ paths: paths
});
- });
- app.get('/nodebb.min.js', function(req, res) {
- var sendCached = function() {
- return res.type('text/javascript').send(meta.js.cache);
- }
- if (meta.js.cache) {
- sendCached();
- } else {
- if (app.enabled('minification')) {
- meta.js.minify(function() {
- sendCached();
- });
- } else {
- // Compress only
- meta.js.concatenate(function() {
- sendCached();
- });
- }
+ parser.parse(source, function(err, tree) {
+ if (err) {
+ res.send(500, err.message);
+ return;
}
+
+ meta.css.cache = tree.toCSS({
+ compress: true
+ });
+ res.type('text/css').send(200, meta.css.cache);
});
- };
-})(exports);
+ });
+}
+
+module.exports = function(app, middleware, controllers) {
+ minificationEnabled = app.enabled('minification');
+
+ app.get('/stylesheet.css', sendStylesheet);
+ app.get('/nodebb.min.js', sendMinifiedJS);
+ app.get('/sitemap.xml', controllers.sitemap);
+ app.get('/robots.txt', controllers.robots);
- };
++};