2015-07-15 19:01:23 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
api = require('../api'),
|
2017-02-27 18:53:04 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2015-07-15 19:01:23 +03:00
|
|
|
config = require('../config'),
|
2017-02-03 16:15:11 +03:00
|
|
|
utils = require('../utils'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../logging'),
|
2015-07-15 19:01:23 +03:00
|
|
|
errors = require('../errors'),
|
2016-10-06 15:27:35 +03:00
|
|
|
i18n = require('../i18n'),
|
2017-03-02 19:53:48 +03:00
|
|
|
themeList = require('../themes').list,
|
2015-07-15 19:01:23 +03:00
|
|
|
themeHandler;
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler = {
|
|
|
|
// ### configHbsForContext Middleware
|
|
|
|
// Setup handlebars for the current context (admin or theme)
|
|
|
|
configHbsForContext: function configHbsForContext(req, res, next) {
|
2017-02-03 16:15:11 +03:00
|
|
|
var themeData = {
|
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937)
* 🔥 kill apiUrl helper, use urlFor helper instead
More consistency of creating urls.
Creates an easier ability to add config changes.
Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs.
The url util need's refactoring anyway.
* 🔥 urlSSL
Remove all urlSSL usages.
Add TODO's for the next commit to re-add logic for deleted logic.
e.g.
- cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available
- theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag)
The changes in this commit doesn't have to be right, but it helped going step by step.
The next commit is the more interesting one.
* 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic
I wanted to remove the forceAdminSSL as separate commit, but was hard to realise.
That's why both changes are in one commit:
1. remove forceAdminSSL
2. add admin.url option
- fix TODO's from last commits
- rewrite the ssl middleware!
- create some private helper functions in the url helper to realise the changes
- rename some wordings and functions e.g. base === blog (we have so much different wordings)
- i would like to do more, but this would end in a non readable PR
- this commit contains the most important changes to offer admin.url option
* 🤖 adapt tests
IMPORTANT
- all changes in the routing tests were needed, because each routing test did not start the ghost server
- they just required the ghost application, which resulted in a random server port
- having a random server port results in a redirect, caused by the ssl/redirect middleware
* 😎 rename check-ssl middleware
* 🎨 fix theme-handler because of master rebase
2017-02-03 21:13:22 +03:00
|
|
|
url: utils.url.urlFor('home', {secure: req.secure}, true),
|
2017-02-03 16:15:11 +03:00
|
|
|
facebook: settingsCache.get('facebook'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
|
|
|
timezone: settingsCache.get('activeTimezone'),
|
|
|
|
navigation: settingsCache.get('navigation'),
|
|
|
|
posts_per_page: settingsCache.get('postsPerPage'),
|
|
|
|
icon: settingsCache.get('icon'),
|
|
|
|
cover: settingsCache.get('cover'),
|
|
|
|
logo: settingsCache.get('logo'),
|
|
|
|
amp: settingsCache.get('amp')
|
|
|
|
},
|
|
|
|
labsData = _.cloneDeep(settingsCache.get('labs')),
|
2015-10-11 23:21:54 +03:00
|
|
|
blogApp = req.app;
|
|
|
|
|
2017-02-03 16:15:11 +03:00
|
|
|
hbs.updateTemplateOptions({
|
|
|
|
data: {
|
|
|
|
blog: themeData,
|
|
|
|
labs: labsData
|
|
|
|
}
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-09-13 23:24:57 +03:00
|
|
|
if (config.getContentPath('themes') && blogApp.get('activeTheme')) {
|
|
|
|
blogApp.set('views', path.join(config.getContentPath('themes'), blogApp.get('activeTheme')));
|
2015-10-11 23:21:54 +03:00
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
// Pass 'secure' flag to the view engine
|
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937)
* 🔥 kill apiUrl helper, use urlFor helper instead
More consistency of creating urls.
Creates an easier ability to add config changes.
Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs.
The url util need's refactoring anyway.
* 🔥 urlSSL
Remove all urlSSL usages.
Add TODO's for the next commit to re-add logic for deleted logic.
e.g.
- cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available
- theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag)
The changes in this commit doesn't have to be right, but it helped going step by step.
The next commit is the more interesting one.
* 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic
I wanted to remove the forceAdminSSL as separate commit, but was hard to realise.
That's why both changes are in one commit:
1. remove forceAdminSSL
2. add admin.url option
- fix TODO's from last commits
- rewrite the ssl middleware!
- create some private helper functions in the url helper to realise the changes
- rename some wordings and functions e.g. base === blog (we have so much different wordings)
- i would like to do more, but this would end in a non readable PR
- this commit contains the most important changes to offer admin.url option
* 🤖 adapt tests
IMPORTANT
- all changes in the routing tests were needed, because each routing test did not start the ghost server
- they just required the ghost application, which resulted in a random server port
- having a random server port results in a redirect, caused by the ssl/redirect middleware
* 😎 rename check-ssl middleware
* 🎨 fix theme-handler because of master rebase
2017-02-03 21:13:22 +03:00
|
|
|
// so that templates can choose to render https or http 'url', see url utility
|
2015-10-11 23:21:54 +03:00
|
|
|
res.locals.secure = req.secure;
|
2016-07-21 14:26:16 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
next();
|
|
|
|
},
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
// ### Activate Theme
|
|
|
|
// Helper for updateActiveTheme
|
|
|
|
activateTheme: function activateTheme(blogApp, activeTheme) {
|
|
|
|
var hbsOptions,
|
2016-09-13 23:24:57 +03:00
|
|
|
themePartials = path.join(config.getContentPath('themes'), activeTheme, 'partials');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
// clear the view cache
|
|
|
|
blogApp.cache = {};
|
2016-09-30 14:13:16 +03:00
|
|
|
// reset the asset hash
|
|
|
|
config.assetHash = null;
|
2015-10-11 23:21:54 +03:00
|
|
|
|
|
|
|
// set view engine
|
|
|
|
hbsOptions = {
|
2016-09-13 18:41:14 +03:00
|
|
|
partialsDir: [config.get('paths').helperTemplates],
|
2015-10-11 23:21:54 +03:00
|
|
|
onCompile: function onCompile(exhbs, source) {
|
|
|
|
return exhbs.handlebars.compile(source, {preventIndent: true});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fs.stat(themePartials, function stat(err, stats) {
|
|
|
|
// Check that the theme has a partials directory before trying to use it
|
|
|
|
if (!err && stats && stats.isDirectory()) {
|
|
|
|
hbsOptions.partialsDir.push(themePartials);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
blogApp.engine('hbs', hbs.express3(hbsOptions));
|
|
|
|
|
|
|
|
// Set active theme variable on the express server
|
|
|
|
blogApp.set('activeTheme', activeTheme);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ### updateActiveTheme
|
|
|
|
// Updates the blogApp's activeTheme variable and subsequently
|
|
|
|
// activates that theme's views with the hbs templating engine if it
|
|
|
|
// is not yet activated.
|
2016-07-21 14:26:16 +03:00
|
|
|
updateActiveTheme: function updateActiveTheme(req, res, next) {
|
|
|
|
var blogApp = req.app;
|
|
|
|
|
|
|
|
api.settings.read({context: {internal: true}, key: 'activeTheme'}).then(function then(response) {
|
|
|
|
var activeTheme = response.settings[0];
|
|
|
|
|
|
|
|
// Check if the theme changed
|
|
|
|
if (activeTheme.value !== blogApp.get('activeTheme')) {
|
|
|
|
// Change theme
|
2017-03-02 19:53:48 +03:00
|
|
|
if (!themeList.get(activeTheme.value)) {
|
2016-07-21 14:26:16 +03:00
|
|
|
if (!res.isAdmin) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return next(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.middleware.themehandler.missingTheme', {theme: activeTheme.value})
|
|
|
|
}));
|
2015-07-15 19:01:23 +03:00
|
|
|
} else {
|
2016-07-21 14:26:16 +03:00
|
|
|
// At this point the activated theme is not present and the current
|
|
|
|
// request is for the admin client. In order to allow the user access
|
|
|
|
// to the admin client we set an hbs instance on the app so that middleware
|
|
|
|
// processing can continue.
|
|
|
|
blogApp.engine('hbs', hbs.express3());
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(i18n.t('errors.middleware.themehandler.missingTheme', {theme: activeTheme.value}));
|
2016-07-21 14:26:16 +03:00
|
|
|
return next();
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
2016-07-21 14:26:16 +03:00
|
|
|
} else {
|
|
|
|
themeHandler.activateTheme(blogApp, activeTheme.value);
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
2016-07-21 14:26:16 +03:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
}).catch(function handleError(err) {
|
|
|
|
// Trying to start up without the active theme present, setup a simple hbs instance
|
|
|
|
// and render an error page straight away.
|
|
|
|
blogApp.engine('hbs', hbs.express3());
|
|
|
|
next(err);
|
|
|
|
});
|
2015-10-11 23:21:54 +03:00
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themeHandler;
|