2013-05-11 20:44:25 +04:00
|
|
|
// # Ghost Module
|
2013-08-06 23:27:56 +04:00
|
|
|
// Defines core methods required to build the application
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Module dependencies
|
2013-09-24 14:46:30 +04:00
|
|
|
var config = require('../config'),
|
|
|
|
when = require('when'),
|
|
|
|
express = require('express'),
|
|
|
|
errors = require('./server/errorHandling'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
nodefn = require('when/node/function'),
|
|
|
|
_ = require('underscore'),
|
2013-11-17 22:40:26 +04:00
|
|
|
url = require('url'),
|
2013-09-24 14:46:30 +04:00
|
|
|
Polyglot = require('node-polyglot'),
|
|
|
|
Mailer = require('./server/mail'),
|
|
|
|
models = require('./server/models'),
|
2013-07-11 23:02:18 +04:00
|
|
|
requireTree = require('./server/require-tree'),
|
2013-08-16 03:22:08 +04:00
|
|
|
permissions = require('./server/permissions'),
|
2013-09-24 14:46:30 +04:00
|
|
|
uuid = require('node-uuid'),
|
2013-08-06 23:27:56 +04:00
|
|
|
|
|
|
|
// Variables
|
2013-09-24 14:46:30 +04:00
|
|
|
appRoot = path.resolve(__dirname, '../'),
|
|
|
|
themePath = path.resolve(appRoot + '/content/themes'),
|
|
|
|
pluginPath = path.resolve(appRoot + '/content/plugins'),
|
|
|
|
themeDirectories = requireTree(themePath),
|
2013-07-01 23:24:48 +04:00
|
|
|
pluginDirectories = requireTree(pluginPath),
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
Ghost,
|
|
|
|
instance,
|
2013-09-18 04:35:46 +04:00
|
|
|
defaults;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
when.pipeline = require('when/pipeline');
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// ## Default values
|
|
|
|
/**
|
|
|
|
* A hash of default values to use instead of 'magic' numbers/strings.
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
defaults = {
|
|
|
|
filterPriority: 5,
|
|
|
|
maxPriority: 9
|
|
|
|
};
|
|
|
|
|
|
|
|
// ## Module Methods
|
|
|
|
/**
|
|
|
|
* @method Ghost
|
|
|
|
* @returns {*}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
Ghost = function () {
|
2013-09-06 19:54:50 +04:00
|
|
|
var polyglot;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
instance = this;
|
|
|
|
|
|
|
|
// Holds the filters
|
|
|
|
instance.filterCallbacks = [];
|
|
|
|
|
|
|
|
// Holds the filter hooks (that are built in to Ghost Core)
|
|
|
|
instance.filters = [];
|
|
|
|
|
|
|
|
// Holds the theme directories temporarily
|
|
|
|
instance.themeDirectories = {};
|
|
|
|
|
|
|
|
// Holds the plugin directories temporarily
|
|
|
|
instance.pluginDirectories = {};
|
|
|
|
|
2013-08-03 19:11:16 +04:00
|
|
|
// Holds the persistent notifications
|
|
|
|
instance.notifications = [];
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Holds the available plugins
|
2013-08-05 19:11:32 +04:00
|
|
|
instance.availablePlugins = {};
|
|
|
|
|
2013-08-24 04:02:01 +04:00
|
|
|
// Holds the dbhash (mainly used for cookie secret)
|
|
|
|
instance.dbHash = undefined;
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
polyglot = new Polyglot();
|
|
|
|
|
|
|
|
_.extend(instance, {
|
2013-09-11 19:23:07 +04:00
|
|
|
config: function () { return config[process.env.NODE_ENV]; },
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// there's no management here to be sure this has loaded
|
2013-09-15 21:52:37 +04:00
|
|
|
settings: function (key) {
|
|
|
|
if (key) {
|
|
|
|
return instance.settingsCache[key].value;
|
|
|
|
}
|
|
|
|
return instance.settingsCache;
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
dataProvider: models,
|
2013-09-02 21:27:26 +04:00
|
|
|
blogGlobals: function () {
|
2013-11-17 22:40:26 +04:00
|
|
|
var localPath = url.parse(instance.config().url).path;
|
|
|
|
|
|
|
|
// Remove trailing slash
|
|
|
|
if (localPath !== '/') {
|
|
|
|
localPath = localPath.replace(/\/$/, '');
|
|
|
|
}
|
|
|
|
|
2013-09-02 21:27:26 +04:00
|
|
|
/* this is a bit of a hack until we have a better way to combine settings and config
|
|
|
|
* this data is what becomes globally available to themes */
|
|
|
|
return {
|
2013-11-17 22:40:26 +04:00
|
|
|
url: instance.config().url.replace(/\/$/, ''),
|
|
|
|
path: localPath,
|
2013-09-15 21:52:37 +04:00
|
|
|
title: instance.settings('title'),
|
|
|
|
description: instance.settings('description'),
|
|
|
|
logo: instance.settings('logo'),
|
|
|
|
cover: instance.settings('cover')
|
2013-09-02 21:27:26 +04:00
|
|
|
};
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
polyglot: function () { return polyglot; },
|
2013-08-21 00:19:47 +04:00
|
|
|
mail: new Mailer(),
|
2013-06-25 15:43:15 +04:00
|
|
|
getPaths: function () {
|
|
|
|
return when.all([themeDirectories, pluginDirectories]).then(function (paths) {
|
|
|
|
instance.themeDirectories = paths[0];
|
|
|
|
instance.pluginDirectories = paths[1];
|
|
|
|
return;
|
2013-06-16 20:14:01 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
paths: function () {
|
|
|
|
return {
|
2013-08-12 18:13:15 +04:00
|
|
|
'appRoot': appRoot,
|
|
|
|
'themePath': themePath,
|
|
|
|
'pluginPath': pluginPath,
|
2013-09-24 14:46:30 +04:00
|
|
|
'activeTheme': path.join(themePath, !instance.settingsCache ? '' : instance.settingsCache.activeTheme.value),
|
2013-08-12 18:13:15 +04:00
|
|
|
'adminViews': path.join(appRoot, '/core/server/views/'),
|
|
|
|
'helperTemplates': path.join(appRoot, '/core/server/helpers/tpl/'),
|
|
|
|
'lang': path.join(appRoot, '/core/shared/lang/'),
|
2013-07-11 23:02:18 +04:00
|
|
|
'availableThemes': instance.themeDirectories,
|
2013-06-25 15:43:15 +04:00
|
|
|
'availablePlugins': instance.pluginDirectories
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
};
|
2013-06-16 20:14:01 +04:00
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Initialise the application
|
2013-06-25 15:43:15 +04:00
|
|
|
Ghost.prototype.init = function () {
|
|
|
|
var self = this;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-09-18 04:35:46 +04:00
|
|
|
function doFirstRun() {
|
|
|
|
var firstRunMessage = [
|
2013-09-24 07:37:36 +04:00
|
|
|
'Welcome to Ghost.',
|
|
|
|
'You\'re running under the <strong>',
|
2013-09-18 04:35:46 +04:00
|
|
|
process.env.NODE_ENV,
|
2013-09-24 07:37:36 +04:00
|
|
|
'</strong>environment.',
|
2013-09-18 04:35:46 +04:00
|
|
|
|
2013-09-24 07:37:36 +04:00
|
|
|
'Your URL is set to',
|
|
|
|
'<strong>' + self.config().url + '</strong>.',
|
|
|
|
'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.'
|
2013-09-18 04:35:46 +04:00
|
|
|
];
|
|
|
|
|
|
|
|
self.notifications.push({
|
|
|
|
type: 'info',
|
|
|
|
message: firstRunMessage.join(' '),
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'ghost-first-run'
|
|
|
|
});
|
|
|
|
return when.resolve();
|
|
|
|
}
|
2013-09-15 20:03:31 +04:00
|
|
|
|
2013-09-18 04:35:46 +04:00
|
|
|
function initDbHashAndFirstRun() {
|
2013-08-24 04:02:01 +04:00
|
|
|
return when(models.Settings.read('dbHash')).then(function (dbhash) {
|
|
|
|
// we already ran this, chill
|
|
|
|
self.dbHash = dbhash.attributes.value;
|
|
|
|
return dbhash.attributes.value;
|
|
|
|
}).otherwise(function (error) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-08-24 04:02:01 +04:00
|
|
|
// this is where all the "first run" functionality should go
|
|
|
|
var dbhash = uuid.v4();
|
2013-09-18 04:35:46 +04:00
|
|
|
return when(models.Settings.add({key: 'dbHash', value: dbhash, type: 'core'})).then(function () {
|
2013-08-24 04:02:01 +04:00
|
|
|
self.dbHash = dbhash;
|
|
|
|
return dbhash;
|
2013-09-18 04:35:46 +04:00
|
|
|
}).then(doFirstRun);
|
2013-08-24 04:02:01 +04:00
|
|
|
});
|
2013-09-18 04:35:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ### Initialisation
|
|
|
|
return when.join(
|
|
|
|
// Initialise the models
|
2013-09-24 07:37:36 +04:00
|
|
|
self.dataProvider.init(),
|
2013-09-18 04:35:46 +04:00
|
|
|
// Calculate paths
|
2013-09-24 07:37:36 +04:00
|
|
|
self.getPaths(),
|
2013-09-18 04:35:46 +04:00
|
|
|
// Initialise mail after first run
|
2013-09-24 07:37:36 +04:00
|
|
|
self.mail.init(self)
|
2013-09-18 04:35:46 +04:00
|
|
|
).then(function () {
|
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
// Initialize the settings cache
|
|
|
|
return self.updateSettingsCache();
|
|
|
|
}).then(function () {
|
|
|
|
return when.join(
|
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
|
|
|
// Initialize the permissions actions and objects
|
|
|
|
permissions.init()
|
|
|
|
);
|
|
|
|
}).otherwise(errors.logAndThrowError);
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Maintain the internal cache of the settings object
|
2013-06-25 15:43:15 +04:00
|
|
|
Ghost.prototype.updateSettingsCache = function (settings) {
|
|
|
|
var self = this;
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
settings = settings || {};
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!_.isEmpty(settings)) {
|
2013-09-15 20:03:31 +04:00
|
|
|
_.map(settings, function (setting, key) {
|
|
|
|
self.settingsCache[key].value = setting.value;
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
} else {
|
|
|
|
// TODO: this should use api.browse
|
2013-09-15 20:03:31 +04:00
|
|
|
return when(models.Settings.findAll()).then(function (result) {
|
|
|
|
return when(self.readSettingsResult(result)).then(function (s) {
|
|
|
|
self.settingsCache = s;
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-09-15 20:03:31 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-15 20:03:31 +04:00
|
|
|
Ghost.prototype.readSettingsResult = function (result) {
|
|
|
|
var settings = {};
|
|
|
|
return when(_.map(result.models, function (member) {
|
|
|
|
if (!settings.hasOwnProperty(member.attributes.key)) {
|
|
|
|
var val = {};
|
2013-09-15 21:52:37 +04:00
|
|
|
val.value = member.attributes.value;
|
|
|
|
val.type = member.attributes.type;
|
|
|
|
settings[member.attributes.key] = val;
|
2013-09-15 20:03:31 +04:00
|
|
|
}
|
|
|
|
})).then(function () {
|
|
|
|
return when(instance.paths().availableThemes).then(function (themes) {
|
|
|
|
var themeKeys = Object.keys(themes),
|
|
|
|
res = [],
|
|
|
|
i,
|
|
|
|
item;
|
|
|
|
for (i = 0; i < themeKeys.length; i += 1) {
|
|
|
|
//do not include hidden files
|
|
|
|
if (themeKeys[i].indexOf('.') !== 0) {
|
|
|
|
item = {};
|
|
|
|
item.name = themeKeys[i];
|
|
|
|
//data about files currently not used
|
|
|
|
//item.details = themes[themeKeys[i]];
|
|
|
|
if (themeKeys[i] === settings.activeTheme.value) {
|
|
|
|
item.active = true;
|
|
|
|
}
|
|
|
|
res.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.availableThemes = {};
|
|
|
|
settings.availableThemes.value = res;
|
|
|
|
settings.availableThemes.type = 'theme';
|
|
|
|
return settings;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-11 02:45:13 +04:00
|
|
|
// ## Template utils
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Compile a template for a handlebars helper
|
2013-07-11 02:45:13 +04:00
|
|
|
Ghost.prototype.compileTemplate = function (templatePath) {
|
|
|
|
return nodefn.call(fs.readFile, templatePath).then(function (templateContents) {
|
|
|
|
return hbs.handlebars.compile(templateContents.toString());
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Load a template for a handlebars helper
|
2013-07-11 02:45:13 +04:00
|
|
|
Ghost.prototype.loadTemplate = function (name) {
|
2013-08-02 21:51:33 +04:00
|
|
|
var self = this,
|
|
|
|
templateFileName = name + '.hbs',
|
|
|
|
// Check for theme specific version first
|
2013-09-24 14:46:30 +04:00
|
|
|
templatePath = path.join(this.paths().activeTheme, 'partials', templateFileName),
|
2013-08-02 21:51:33 +04:00
|
|
|
deferred = when.defer();
|
|
|
|
|
|
|
|
// Can't use nodefn here because exists just returns one parameter, true or false
|
|
|
|
|
|
|
|
fs.exists(templatePath, function (exists) {
|
|
|
|
if (!exists) {
|
|
|
|
// Fall back to helpers templates location
|
|
|
|
templatePath = path.join(self.paths().helperTemplates, templateFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.compileTemplate(templatePath).then(deferred.resolve, deferred.reject);
|
|
|
|
});
|
2013-07-11 02:45:13 +04:00
|
|
|
|
2013-08-02 21:51:33 +04:00
|
|
|
return deferred.promise;
|
2013-07-11 02:45:13 +04:00
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Register a handlebars helper for themes
|
2013-06-25 15:43:15 +04:00
|
|
|
Ghost.prototype.registerThemeHelper = function (name, fn) {
|
|
|
|
hbs.registerHelper(name, fn);
|
|
|
|
};
|
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
// Register an async handlebars helper for themes
|
|
|
|
Ghost.prototype.registerAsyncThemeHelper = function (name, fn) {
|
|
|
|
hbs.registerAsyncHelper(name, function (options, cb) {
|
|
|
|
// Wrap the function passed in with a when.resolve so it can
|
|
|
|
// return either a promise or a value
|
2013-10-31 06:05:24 +04:00
|
|
|
when.resolve(fn.call(this, options)).then(function (result) {
|
2013-09-15 01:34:12 +04:00
|
|
|
cb(result);
|
|
|
|
}).otherwise(function (err) {
|
|
|
|
errors.logAndThrowError(err, "registerAsyncThemeHelper: " + name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Register a new filter callback function
|
2013-06-25 15:43:15 +04:00
|
|
|
Ghost.prototype.registerFilter = function (name, priority, fn) {
|
|
|
|
// Curry the priority optional parameter to a default of 5
|
|
|
|
if (_.isFunction(priority)) {
|
|
|
|
fn = priority;
|
|
|
|
priority = defaults.filterPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.filterCallbacks[name] = this.filterCallbacks[name] || {};
|
|
|
|
this.filterCallbacks[name][priority] = this.filterCallbacks[name][priority] || [];
|
|
|
|
|
|
|
|
this.filterCallbacks[name][priority].push(fn);
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Unregister a filter callback function
|
2013-08-05 19:11:32 +04:00
|
|
|
Ghost.prototype.unregisterFilter = function (name, priority, fn) {
|
|
|
|
// Curry the priority optional parameter to a default of 5
|
|
|
|
if (_.isFunction(priority)) {
|
|
|
|
fn = priority;
|
|
|
|
priority = defaults.filterPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it even exists
|
|
|
|
if (this.filterCallbacks[name] && this.filterCallbacks[name][priority]) {
|
|
|
|
// Remove the function from the list of filter funcs
|
|
|
|
this.filterCallbacks[name][priority] = _.without(this.filterCallbacks[name][priority], fn);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Execute filter functions in priority order
|
2013-09-15 01:34:12 +04:00
|
|
|
Ghost.prototype.doFilter = function (name, args) {
|
|
|
|
var callbacks = this.filterCallbacks[name],
|
|
|
|
priorityCallbacks = [];
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// Bug out early if no callbacks by that name
|
|
|
|
if (!callbacks) {
|
2013-09-15 01:34:12 +04:00
|
|
|
return when.resolve(args);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
// For each priorityLevel
|
2013-06-25 15:43:15 +04:00
|
|
|
_.times(defaults.maxPriority + 1, function (priority) {
|
2013-09-15 01:34:12 +04:00
|
|
|
// Add a function that runs its priority level callbacks in a pipeline
|
|
|
|
priorityCallbacks.push(function (currentArgs) {
|
|
|
|
// Bug out if no handlers on this priority
|
|
|
|
if (!_.isArray(callbacks[priority])) {
|
|
|
|
return when.resolve(currentArgs);
|
2013-07-07 22:02:26 +04:00
|
|
|
}
|
2013-09-15 01:34:12 +04:00
|
|
|
|
|
|
|
// Call each handler for this priority level, allowing for promises or values
|
|
|
|
return when.pipeline(callbacks[priority], currentArgs);
|
2013-06-09 20:16:25 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
return when.pipeline(priorityCallbacks, args);
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
module.exports = Ghost;
|