-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
578 lines (516 loc) · 17.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
const request = require('request-promise');
const uniq = require('lodash.uniq');
module.exports = {
name: 'apostrophe-forms',
label: 'Form',
extend: 'apostrophe-pieces',
seo: false,
openGraph: false,
moogBundle: {
directory: 'lib/modules',
modules: [
'apostrophe-forms-widgets',
'apostrophe-forms-base-field-widgets',
'apostrophe-forms-text-field-widgets',
'apostrophe-forms-select-field-widgets',
'apostrophe-forms-radio-field-widgets', // Extends apos-forms-select-field
'apostrophe-forms-checkboxes-field-widgets',
'apostrophe-forms-textarea-field-widgets',
'apostrophe-forms-file-field-widgets',
'apostrophe-forms-boolean-field-widgets',
'apostrophe-forms-conditional-widgets'
]
},
beforeConstruct: function (self, options) {
options.addFields = [
{
name: 'title',
label: 'Form Name',
type: 'string',
sortify: true,
required: true
},
{
name: 'contents',
label: 'Form Contents',
type: 'area',
contextual: false,
options: {
widgets: options.formWidgets || {
'apostrophe-forms-text-field': {},
'apostrophe-forms-textarea-field': {},
'apostrophe-forms-file-field': {},
'apostrophe-forms-boolean-field': {},
'apostrophe-forms-select-field': {},
'apostrophe-forms-radio-field': {},
'apostrophe-forms-checkboxes-field': {},
'apostrophe-forms-conditional': {},
'apostrophe-rich-text': {
toolbar: [
'Styles', 'Bold', 'Italic', 'Link', 'Anchor', 'Unlink',
'NumberedList', 'BulletedList'
]
}
}
}
},
{
name: 'submitLabel',
label: 'Submit Button Label',
type: 'string'
},
{
name: 'thankYouHeading',
label: 'Thank You Message Title',
type: 'string'
},
{
name: 'thankYouBody',
label: 'Thank You Message Content',
type: 'area',
options: {
widgets: options.thankYouWidgets || {
'apostrophe-rich-text': {
toolbar: [
'Styles', 'Bold', 'Italic', 'Link', 'Anchor', 'Unlink',
'NumberedList', 'BulletedList'
]
}
}
}
},
{
name: 'sendConfirmationEmail',
label: 'Send a Confirmation Email',
// NOTE: The confirmation email is in `views/emailConfirmation.html`.
// Edit the message there, adding any dynamic content as needed.
help: 'Enable this to send a message to the person who submits this form.',
type: 'boolean',
choices: [
{
value: true,
showFields: [
'emailConfirmationField'
]
}
]
},
{
name: 'emailConfirmationField',
label: 'Which is your confirmation email field?',
help: 'Enter the "name" value of the field where people with enter their email address.',
type: 'string',
required: true
},
{
name: 'enableQueryParams',
label: 'Enable Query Parameter Capture',
type: 'boolean',
htmlHelp: 'If enabled, <em>all</em> query parameters (the key/value pairs in a <a href="https://en.wikipedia.org/wiki/Query_string" target="_blank">query string</a>) will be collected when the form is submitted. You may also set list of specific parameter keys that you wish to collect.',
choices: [
{
label: 'Yes',
value: true,
showFields: [
'queryParamList'
]
}
]
},
{
name: 'queryParamList',
label: 'Query Parameter Keys',
type: 'array',
titleField: 'key',
required: true,
help: 'Create an array item for each query parameter value you wish to capture.',
schema: [
{
type: 'string',
name: 'key',
label: 'Key',
required: true
},
{
type: 'integer',
name: 'lengthLimit',
label: 'Limit Saved Parameter Value Length (characters)',
help: 'Enter a whole number to limit the length of the value saved.',
min: 1
}
]
}
].concat(options.emailSubmissions !== false ? [
{
name: 'emails',
label: 'Email Address(es) for Results',
type: 'array',
titleField: 'email',
schema: [
{
name: 'email',
type: 'email',
required: true,
label: 'Email Address for Results'
},
{
name: 'conditions',
label: 'Set Conditions for this Notification',
help: 'For example, if you only notify this email address if the "country" field is set to "Austria". All conditions must be met. Add the email again with another conditional set if needed."',
type: 'array',
titleField: 'value',
schema: [
{
name: 'field',
label: 'Enter a field to use as your condition.',
type: 'string',
help: 'Only select (drop-down) and checkbox fields can be used for this condition.'
},
{
name: 'value',
type: 'string',
label: 'Enter the value an end-user will enter to meet this conditional.',
htmlHelp: 'Use comma-separated values to check multiple values on this field (an OR relationship). Values that actually contain commas should be entered in double-quotation marks (e.g., <code>Proud Mary, The Best, "River Deep, Mountain High"</code>).',
choices: 'getConditionChoices'
}
]
}
]
},
{
name: 'email',
label: 'Primary internal email address',
type: 'string',
required: true,
help: 'You may enter one from the previous list. This is the address that will be used as the "from" address on any generated email messages.'
}
] : []).concat(options.addFields || []);
const afterSubmitFields = [
'thankYouHeading',
'thankYouBody',
'sendConfirmationEmail',
'emailConfirmationField'
].concat(options.emailSubmissions !== false ? [
'emails',
'email'
] : []);
options.arrangeFields = [
{
name: 'form',
label: 'Form',
fields: [ 'contents', 'submitLabel' ]
},
{
name: 'afterSubmit',
label: 'After-Submission',
fields: afterSubmitFields
},
{
name: 'advanced',
label: 'Advanced',
fields: [
'enableQueryParams',
'queryParamList'
]
}
].concat(options.arrangeFields || []);
},
afterConstruct: async function(self, callback) {
try {
await self.ensureCollection();
} catch (e) {
return callback(e);
}
return callback(null);
},
construct: function(self, options) {
require('./lib/query-parameters.js')(self, options);
self.ensureCollection = async function() {
self.db = self.apos.db.collection('aposFormSubmissions');
await self.db.ensureIndex({
formId: 1,
createdAt: 1
});
await self.db.ensureIndex({
formId: 1,
createdAt: -1
});
};
// Route to accept the submitted form.
self.apiRoute('post', 'submit', async (req, res, next) => {
const input = req.body;
const output = {};
const formErrors = [];
const overrideOptions = self.apos.modules['apostrophe-override-options'];
if (overrideOptions) {
// Make sure we can get reCAPTCHA configurations from the global object
// with self.getOption if needed.
overrideOptions.calculateOverrides(req);
}
const form = await self.find(req, {
_id: self.apos.launder.id(req.body._id)
}).toObject();
if (!form) {
return next('notfound');
}
const response = {};
try {
if (self.getOption(req, 'recaptchaSecret')) {
await self.checkRecaptcha(req, input, formErrors);
}
// Recursively walk the area and its sub-areas so we find
// fields nested in two-column widgets and the like
// walk is not an async function so build an array of them to start
const areas = [];
self.apos.areas.walk({
contents: form.contents
}, function(area) {
areas.push(area);
});
const fieldNames = [];
const conditionals = {};
const skipFields = [];
// Populate the conditionals object fully to clear disabled values
// before starting sanitization.
for (const area of areas) {
const widgets = area.items || [];
for (const widget of widgets) {
// Capture field names for the params check list.
fieldNames.push(widget.fieldName);
if (widget.type === 'apostrophe-forms-conditional') {
trackConditionals(conditionals, widget);
}
}
}
collectToSkip(input, conditionals, skipFields);
for (const area of areas) {
const widgets = area.items || [];
for (const widget of widgets) {
const manager = self.apos.areas.getWidgetManager(widget.type);
if (
manager && manager.sanitizeFormField &&
!skipFields.includes(widget.fieldName)
) {
try {
manager.checkRequired(req, form, widget, input);
await manager.sanitizeFormField(req, form, widget, input, output);
} catch (err) {
if (err.fieldError) {
formErrors.push(err.fieldError);
} else {
throw err;
}
}
}
}
}
if (formErrors.length > 0) {
return next('error', null, {
formErrors
});
}
if (form.enableQueryParams && form.queryParamList.length > 0) {
self.processQueryParams(req, form, input, output, fieldNames);
}
await self.emit('submission', req, form, output, response);
return next(null, response);
} catch (e) {
return next(e);
}
});
self.checkRecaptcha = async function (req, input, formErrors) {
const recaptchaSecret = self.getOption(req, 'recaptchaSecret');
if (!input.recaptcha) {
formErrors.push({
global: true,
error: 'recaptcha',
errorMessage: 'There was a problem submitting your reCAPTCHA verfication.'
});
}
try {
const url = 'https://www.google.com/recaptcha/api/siteverify';
const recaptchaResponse = JSON.parse(await request({
method: 'POST',
uri: `${url}?secret=${recaptchaSecret}&response=${input.recaptcha}`
}));
if (!recaptchaResponse.success) {
formErrors.push({
global: true,
error: 'recaptcha',
errorMessage: 'There was a problem validating your reCAPTCHA verfication submission.'
});
}
} catch (e) {
formErrors.push({
global: true,
error: 'recaptcha',
errorMessage: 'The reCAPTCHA verification system may be down or incorrectly configured. Please try again later or notify the site owner.'
});
}
};
// Should be handled async. Options are: form, data, from, to and subject
self.sendEmail = (req, emailTemplate, options) => {
const form = options.form;
const data = options.data;
return self.email(
req,
emailTemplate,
{
form: form,
input: data
},
{
from: options.from || form.email,
to: options.to,
subject: options.subject || form.title
}
);
};
self.sendEmailSubmissions = async function (req, form, data) {
if (self.options.emailSubmissions === false ||
!form.emails || form.emails.length === 0) {
return;
}
let emails = [];
form.emails.forEach(mailRule => {
if (!mailRule.conditions || mailRule.conditions.length === 0) {
emails.push(mailRule.email);
return;
}
let passed = true;
mailRule.conditions.forEach(condition => {
if (!condition.value) {
return;
}
let answer = data[condition.field];
if (!answer) {
passed = false;
} else {
// Regex for comma-separation from https://stackoverflow.com/questions/11456850/split-a-string-by-commas-but-ignore-commas-within-double-quotes-using-javascript/11457952#comment56094979_11457952
const regex = /(".*?"|[^",]+)(?=\s*,|\s*$)/g;
let acceptable = condition.value.match(regex);
acceptable = acceptable.map(value => {
// Remove leading/trailing white space and bounding double-quotes.
value = value.trim();
if (value[0] === '"' && value[value.length - 1] === '"') {
value = value.slice(1, -1);
}
return value.trim();
});
// If the value is stored as a string, convert to an array for checking.
if (!Array.isArray(answer)) {
answer = [ answer ];
}
if (!(answer.some(val => acceptable.includes(val)))) {
passed = false;
}
}
});
if (passed === true) {
emails.push(mailRule.email);
}
});
emails = uniq(emails);
if (self.options.testing) {
return emails;
}
if (emails.length === 0) {
return null;
}
for (const key in data) {
// Add some space to array lists.
if (Array.isArray(data[key])) {
data[key] = data[key].join(', ');
}
}
try {
const emailOptions = {
form,
data,
to: emails.join(',')
};
await self.sendEmail(req, 'emailSubmission', emailOptions);
return null;
} catch (err) {
self.apos.utils.error('⚠️ apostrophe-forms submission email notification error: ', err);
return null;
}
};
self.on('submission', 'emailSubmission', async function (req, form, data) {
await self.sendEmailSubmissions(req, form, data);
});
self.on('submission', 'emailConfirmation', async function(req, form, data) {
if (form.sendConfirmationEmail !== true || !form.emailConfirmationField) {
return;
}
// Email validation (Regex reference: https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript)
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!re.test(data[form.emailConfirmationField])) {
return null;
}
try {
const emailOptions = {
form,
data,
to: data[form.emailConfirmationField]
};
await self.sendEmail(req, 'emailConfirmation', emailOptions);
return null;
} catch (err) {
self.apos.utils.error('⚠️ apostrophe-forms submission email confirmation error: ', err);
return null;
}
});
self.on('submission', 'saveSubmission', async function(req, form, data) {
if (self.options.saveSubmissions === false) {
return;
}
return self.db.insert({
createdAt: new Date(),
formId: form._id,
data: data
});
});
var superPushAssets = self.pushAssets;
self.pushAssets = function() {
superPushAssets();
self.pushAsset('stylesheet', 'lean', { when: 'lean' });
};
function trackConditionals(conditionals = {}, widget) {
const conditionName = widget.conditionName;
const conditionValue = widget.conditionValue;
if (!widget || !widget.contents || !widget.contents.items) {
return;
}
conditionals[conditionName] = conditionals[conditionName] || {};
conditionals[conditionName][conditionValue] = conditionals[conditionName][conditionValue] || [];
widget.contents.items.forEach(item => {
conditionals[conditionName][conditionValue].push(item.fieldName);
});
// If there aren't any fields in the conditional group, don't bother
// tracking it.
if (conditionals[conditionName][conditionValue].length === 0) {
delete conditionals[conditionName][conditionValue];
}
}
function collectToSkip(input, conditionals, skipFields) {
// Check each field that controls a conditional group.
for (const name in conditionals) {
// For each value that a conditional group is looking for, check if the
// value matches in the output and, if not, remove the output properties
// for the conditional fields.
for (let value in conditionals[name]) {
// Booleans are tracked as true/false, but their field values are 'on'. TEMP?
if (input[name] === true && value === 'on') {
value = true;
}
if (input[name] !== value) {
conditionals[name][value].forEach(field => {
skipFields.push(field);
});
}
}
}
}
}
};