-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgistjs.js
226 lines (185 loc) · 6.38 KB
/
gistjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
module.exports = (settings, app) => {
// Setting up the CMS.
// NPM dependancies
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const passport = require('passport');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session)
const flash = require('connect-flash');
const fs = require('fs');
const path = require('path');
const mongojs = require('mongojs');
const _ = require('lodash');
const filter = require('content-filter');
// file includes
const cmsConfig = require('./admin/cms.js');
const EventEmitter = require('events').EventEmitter;
const Events = require('./admin/events.js');
const APP = require('./admin/assets/js/core/app.js');
//const filter = require('./admin/requires/_content-filter.js'); TODO: finish up this module
app.use('/', express.static( path.join(__dirname, 'admin/assets/js/core/') ));
app.use('/gistjs-assets', express.static( path.join(__dirname, 'admin/assets') ));
app.use('/uploads', express.static( settings.uploadDir ));
app.use('/plugins', express.static( settings.pluginDir ));
app.use('/themes', express.static( settings.themeDir ));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const blackList = ['$','{','&&','||']
const filterMsg = 'URL contains filter errors';
const filterOptions = {
urlBlackList: blackList,
bodyBlackList: blackList,
urlMessage: filterMsg,
bodyMessage: filterMsg
}
app.use(filter(filterOptions));
// Use Helmet by default to make the CMS more secure.
// Disable by setting helmet: false in the settings.
if (settings.helmet !== false) {
let helmetSettings = {
noSniff: false // This is __REQUIRED__ to render the ejs files.
};
_.extend(helmetSettings, settings.helmet)
// TODO: add more settings to this, to give the user more control.....
app.use(helmet(helmetSettings));
}
// define the project root.
// Used to get the config file and such
settings.projectRoot = process.cwd();
CMS.init(settings, passport);
if (CMS.config.anyoneRegister) {
CMS.enableUserRegistration();
}
//CMS._utilities.themeFunctionFile();
// Initialize PassportJS for login
require('./admin/passport')(passport, CMS);
if (typeof CMS.config.dbHost !== 'undefined') {
// Initialize session store via MongoStore
const dbSessionsConf = {
db: {
url: 'mongodb://' + CMS.dbConn.sessions.url,
stringify: false,
autoRemove: 'interval', // TODO: make this a config setting
autoRemoveInterval: 10 // TODO: make this a config setting
},
secret: CMS.config.sessionCookieSecret,
sameSite: true,
cookieName: CMS.config.sessionCookieName,
cookieLength: new Date(Date.now() + (30 * 24 * 60 * 60 * 1000)) // TODO: make this a config setting
};
const sessionOpts = {
saveUninitialized: false,
resave: false,
store: new MongoStore(dbSessionsConf.db),
secret: dbSessionsConf.secret,
name: dbSessionsConf.cookieName,
cookie : { httpOnly: true, secure : false, sameSite: dbSessionsConf.sameSite, maxAge : dbSessionsConf.cookieLength} //Cookie for one month
};
// read cookies and set up sessions (needed for auth)
app.use(cookieParser(dbSessionsConf.secret));
app.use(session(sessionOpts));
app.use(passport.initialize());
app.use(passport.session());
global.cms = dbSessionsConf;
} else {
settings.install = true
}
router.use( (req, res, next) => {
let requestUrl = req.url;
CMS.setQueryVars(req.query);
CMS.req = req;
CMS.res = res;
if (CMS.forceSSL) {
const redirectDetails = CMS._security.redirectToSecure(requestUrl);
if (redirectDetails.redirect) {
res.redirect(redirectDetails.redirectTo);
return;
}
}
//filter(filterOptions, req, res, next)
// If redirect to install is neeed:
if (CMS._utilities.installRouting(requestUrl, settings)) {
if (req.method === 'POST') {
next();
return;
} else {
return;
}
}
// May remove this later --!!!!!!!!!!!!!!!!!
if (requestUrl === '/favicon.ico') {
CMS.sendResponse(res, 204);
return;
}
let paginateNumber = 0;
// Check if the requested URL is coming from the admin panel
if (CMS.passThroughUrl(requestUrl, req, res)) {
if (CMS.config.localEditing === false) {
CMS.error(res, 404, 'Page not found');
return;
}
next();
return;
}
// Check if maintenance mode is on
// Enable by setting: maintenance: true in the settings.
// The system may set this automatically some times, like during updates or heavy processes.
if (CMS.config.maintenance === true) {
let hasMaintenanceTemplate = false;
const activeThemeTemplates = CMS.getTemplates();
for (var i = 0; i < activeThemeTemplates.length; i++) {
if (activeThemeTemplates[i].filename === 'maintenance.ejs') {
hasMaintenanceTemplate = true;
break;
}
}
if (typeof CMS.activeTheme === 'undefined' || !hasMaintenanceTemplate) {
CMS.renderAdminTemplate('maintenance');
return;
}
let maintenanceDoc = {
template: 'maintenance',
statusCode: 503
}
CMS.renderTemplate(maintenanceDoc);
return;
}
// If not an admin panel request, look for valid url in db.
CMS.mainRender(requestUrl)
.then((render) => {
CMS.sendResponse(res, 200, render)
})
.catch((e) => {
CMS.sendResponse(res, 500, e.message)
})
});
process.on('unhandledRejection', function(reason, p) {
// TODO: Log this with winston
console.log('unhandledRejection', reason);
//CMS._utilities.catchError(reason, true);
// application specific logging, throwing an error, or other logic here
});
process.on('uncaughtException', (err) => {
CMS._exceptions.log(err);
});
process.on('exit', (code) => {
// TODO: Log this with winston
console.log(`About to exit with code: ${code}`);
});
process.on('rejectionHandled', (p) => {
// TODO: Log this with winston
console.log('rejection handled, check logging type and register an event there');
});
/*
----------------------------------------------
DO NOT CHANGE OR REMOVE THE FOLLOW REQUIRE BELOW!
These routes are what power the admin panel and the API.
----------------------------------------------
*/
require('./admin/routes.js')(CMS, router, passport, settings);
return router;
}