This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (207 loc) · 7.63 KB
/
index.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
227
228
229
const config = require('./config.json')
const mysql = require('mysql2')
const pool = mysql.createPool(config.database)
const connection = pool.promise()
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const nodeMailer = require('nodemailer')
const transporter = nodeMailer.createTransport(config.mailer)
let mailOptions = {
from: config.mailfrom
}
const moment = require('moment')
const request = require('request-promise')
const Login = require('./lib/login.js')
let login = Login(connection)
app.set('view engine', 'pug')
app.use(express.static('static'))
const bodyParser = require('body-parser')
app.use(bodyParser.json())
var cookieParser = require('cookie-parser')
app.use(cookieParser())
server.listen(config.port)
console.log('forms running on ' + config.port)
app.use(function (req, res, next) {
console.log(Date.now() + ': ' + req.method + ' ' + req.url)
next()
})
if (config.https.active) {
var fs = require('fs')
var key = fs.readFileSync(config.https.key)
var cert = fs.readFileSync(config.https.cert)
var options = {
key: key,
cert: cert
}
var https = require('https')
https.createServer(options, app).listen(config.https.port)
console.log('forms running SECURE on ' + config.https.port)
app.use(function (req, res, next) {
if (req.secure) {
next()
} else {
res.redirect('https://' + req.headers.host.replace(config.port, config.https.port) + req.url)
}
})
}
async function isLoggedIn (req, res, next) {
req.token = req.query.token || req.headers.token || req.cookies.ftoken
if (login.isLoggedIn(req.token)) {
next()
} else {
res.redirect('/admin/login')
}
}
app.get('/form/:slug', async function (req, res) {
try {
let results = await connection.query('SELECT name, active, active_from, active_to FROM forms WHERE slug="' + req.params.slug + '"')
if (results[0] && results[0].length === 1) {
let form = results[0]
if (form.active === 0) {
return res.render('disabled', { state: 'inactive' })
}
if (moment(results[0].active_from).isAfter(moment())) {
return res.render('disabled', { state: 'early' })
}
if (moment(results[0].active_to).isBefore(moment())) {
return res.render('disabled', { state: 'late' })
}
let resultFields = await connection.query('SELECT `type`, `label`, `name`‚,`required` ,`visibleIf`,`validation` ,`invalidMessage` ,`additional` FROM form_fields WHERE form_id=' + form.id + ' ORDER BY order ASC') // TODO: Query to get fields for the form, order by order
return res.render('form', { title: results[0].name, sitekey: config.sitekey, slug: req.params.slug, fields: resultFields[0] })
} else {
return res.status(404).render('404')
}
} catch (error) {
res.status(501).json(error)
}
})
app.post('/form/:slug', async function (req, res) {
if (req.body['g-recaptcha-response'] === undefined || req.body['g-recaptcha-response'] === '' || req.body['g-recaptcha-response'] === null) {
return res.status(400).json({ 'responseCode': 1, 'responseDesc': 'Please select captcha' })
} else {
try {
var verificationUrl = 'https://www.google.com/recaptcha/api/siteverify?secret=' + config.secretKey + '&response=' + req.body['g-recaptcha-response'] + '&remoteip=' + req.connection.remoteAddress
let body = await request(verificationUrl)
body = JSON.parse(body)
if (body.success !== undefined && !body.success) {
return res.status(400).json({ 'responseCode': 1, 'responseDesc': 'Failed captcha verification' })
} else {
let results = await connection.query('SELECT id, name, thank_you FROM forms WHERE slug="' + req.params.slug + '"')
if (results.length !== 1) {
res.status(500).json(results)
} else {
let id = results[0].id
let name = results[0].name
let thankyou = results[0].thankyou
let json = JSON.parse(req.body.date)
await connection.query('INSERT INTO results (form_id, json) VALUES (' + id + ', "' + JSON.stringify(json) + '")')
mailOptions.text = 'eq.body.body' // TODO: plain text body <- strip html tags from html body
// TODO: HTML body from database
if (json.email) {
mailOptions.subject = 'Your Data for ' + name
mailOptions.to = json.email
await transporter.sendMail(mailOptions)
}
mailOptions.subject = 'New Entry for ' + name
mailOptions.to = config.mailformto
await transporter.sendMail(mailOptions)
res.render('thankyou', { title: name, data: json, message: thankyou })
}
}
} catch (error) {
res.status(500).json(error)
}
}
})
app.post('/admin/login', async function (req, res) {
try {
let token = await login.login(req.body.username, req.body.password)
res.status(200).json(token)
} catch (error) {
res.status(401).json({ error: error.message })
}
})
app.get('/admin/logout', isLoggedIn, async function (req, res) {
login.logout(req.token)
res.redirect('/admin/login')
})
app.get('/admin/login', function (req, res) {
res.render('admin/login')
})
app.get('/admin/', isLoggedIn, function (req, res) {
res.render('admin/index')
})
app.get('/admin/dashboard', isLoggedIn, function (req, res) {
res.render('admin/views/dashboard')
})
app.get('/admin/:formSlug', isLoggedIn, async function (req, res) {
try {
let results = await connection.query('SELECT name, active, active_from, active_to FROM forms WHERE slug="' + req.params.slug + '"')
res.render('admin/views/' + req.params.formSlug + '/index', { data: results })
} catch (error) {
res.status(500).json(error)
}
})
app.put('/admin/:formSlug', isLoggedIn, function (req, res) {
res.status(200) // TODO: update form settings, response status
})
app.get('/admin/:formSlug/:resultID', isLoggedIn, async function (req, res) {
try {
let results = await connection.query('SELECT f.name, r.id, r.form_id, r.json, r.entry_datetime FROM forms f, results r WHERE f.id = r.form_id AND r.id=' + req.params.resultID + ' AND f.slug="' + req.params.slug + '"')
res.render('admin/views/' + req.params.formSlug + '/details', { data: results })
} catch (error) {
res.status(500).json(error)
}
})
app.put('/admin/:formSlug/:resultID', isLoggedIn, function (req, res) {
res.status(200) // TODO: update result, response status
})
app.get('/admin/:formSlug/:resultID/print', isLoggedIn, async function (req, res) {
try {
let results = await connection.query('SELECT f.name AS form_name, r.id, r.form_id, r.json, r.entry_datetime FROM forms f, results r WHERE f.id = r.form_id AND r.id=' + req.params.resultID + ' AND f.slug="' + req.params.slug + '"')
res.render('admin/views/' + req.params.formSlug + '/print', { data: results })
} catch (error) {
res.status(500).json(error)
}
})
app.get('/api/dashboard', isLoggedIn, function (req, res) {
res.json([
{
form: 'Personal Registration',
subs_24: 10,
subs_7: 100,
subs_total: 1000,
slug: 'personal_registration'
},
{
form: 'Test 2',
subs_24: 20,
subs_7: 140,
subs_total: 2540,
slug: 'test-2'
}
])
// TODO: Form name, submissions (24h, 7d, total), form slug
})
app.get('/api/submissions/:slug', isLoggedIn, function (req, res) {
res.json([
{
id: '2',
timestamp: '2019-10-21T12:00:00',
json: {
name: 'test',
email: '[email protected]'
}
},
{
id: '3',
timestamp: '2019-10-21T14:00:00',
json: {
name: 'test2',
email: '[email protected]'
}
}
])
// TODO: get id, timestamp, json for slug
})