From 3b6fd47a61aa182b71c24d36d1a0c31985cfe75f Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 15 Apr 2016 22:27:45 -0700 Subject: [PATCH] Added new custom fields for GPG keys --- lib/models/fields.js | 14 ++++++++++++-- package.json | 1 + public/css/footer.css | 4 ++++ routes/lists.js | 21 +++++++++++++++++++++ views/lists/fields/create.hbs | 2 ++ views/lists/fields/edit.hbs | 2 ++ views/lists/subscription/add.hbs | 9 +++++++++ views/lists/subscription/edit.hbs | 9 +++++++++ views/subscription/manage.hbs | 9 +++++++++ views/subscription/subscribe.hbs | 9 +++++++++ 10 files changed, 78 insertions(+), 2 deletions(-) diff --git a/lib/models/fields.js b/lib/models/fields.js index 10857885..b9076439 100644 --- a/lib/models/fields.js +++ b/lib/models/fields.js @@ -12,6 +12,8 @@ module.exports.grouped = ['radio', 'checkbox', 'dropdown']; module.exports.types = { text: 'Text', website: 'Website', + longtext: 'Multi-line text', + gpg: 'GPG Public Key', number: 'Number', radio: 'Radio Buttons', checkbox: 'Checkboxes', @@ -26,6 +28,8 @@ module.exports.types = { module.exports.genericTypes = { text: 'string', website: 'string', + longtext: 'textarea', + gpg: 'textarea', number: 'number', 'date-us': 'date', 'date-eur': 'date', @@ -264,7 +268,7 @@ function addCustomField(listId, name, defaultValue, type, group, visible, callba let column = null; let key = slugify('merge ' + name, '_').toUpperCase(); - let allowedTypes = ['text', 'number', 'radio', 'checkbox', 'dropdown', 'date-us', 'date-eur', 'birthday-us', 'birthday-eur', 'website', 'option']; + let allowedTypes = ['text', 'number', 'radio', 'checkbox', 'dropdown', 'date-us', 'date-eur', 'birthday-us', 'birthday-eur', 'website', 'option', 'gpg', 'longtext']; if (allowedTypes.indexOf(type) < 0) { return callback(new Error('Unknown column type ' + type)); @@ -308,7 +312,11 @@ function addCustomField(listId, name, defaultValue, type, group, visible, callba switch (type) { case 'text': case 'website': - query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` VARCHAR(255) DEFAULT NULL'; + query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` VARCHAR(255) DEFAULT NULL, ADD INDEX (' + column + ')'; + break; + case 'gpg': + case 'longtext': + query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` TEXT DEFAULT NULL'; break; case 'number': query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` INT(11) DEFAULT NULL, ADD INDEX (' + column + ')'; @@ -358,6 +366,8 @@ module.exports.getRow = (fieldList, values, useDate, showAll) => { switch (field.type) { case 'text': case 'website': + case 'gpg': + case 'longtext': { let item = { type: field.type, diff --git a/package.json b/package.json index bb2b5ba0..c2edc989 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "mysql": "^2.10.2", "nodemailer": "^2.3.2", "npmlog": "^2.0.3", + "openpgp": "^2.2.1", "passport": "^0.3.2", "passport-local": "^1.0.0", "request": "^2.71.0", diff --git a/public/css/footer.css b/public/css/footer.css index bcf52f02..01f241aa 100644 --- a/public/css/footer.css +++ b/public/css/footer.css @@ -31,3 +31,7 @@ div.jumbotron{ .code-editor { height: 400px; } + +.gpg-text { + font-family: monospace; +} diff --git a/routes/lists.js b/routes/lists.js index 9e781fea..bb856834 100644 --- a/routes/lists.js +++ b/routes/lists.js @@ -1,5 +1,6 @@ 'use strict'; +let openpgp = require('openpgp'); let passport = require('../lib/passport'); let express = require('express'); let router = new express.Router(); @@ -156,6 +157,26 @@ router.post('/ajax/:id', (req, res) => { ].concat(fields.getRow(fieldList, row).map(cRow => { if (cRow.type === 'number') { return htmlescape(cRow.value && humanize.numberFormat(cRow.value, 0) || ''); + } else if (cRow.type === 'longtext') { + let value = (cRow.value || ''); + if (value.length > 50) { + value = value.substr(0, 47).trim() + '…'; + } + return htmlescape(value); + } else if (cRow.type === 'gpg') { + let value = (cRow.value || '').trim(); + try { + value = openpgp.key.readArmored(value); + if (value) { + value = value.keys && value.keys[0] && value.keys[0].primaryKey.fingerprint; + if (value) { + value = '0x' + value.substr(-16).toUpperCase(); + } + } + } catch (E) { + value = 'parse error'; + } + return htmlescape(value || ''); } else { return htmlescape(cRow.value || ''); } diff --git a/views/lists/fields/create.hbs b/views/lists/fields/create.hbs index d276fccc..5abd0a23 100644 --- a/views/lists/fields/create.hbs +++ b/views/lists/fields/create.hbs @@ -26,6 +26,8 @@ + + diff --git a/views/lists/fields/edit.hbs b/views/lists/fields/edit.hbs index 6dba82eb..bbb141e0 100644 --- a/views/lists/fields/edit.hbs +++ b/views/lists/fields/edit.hbs @@ -33,6 +33,8 @@ + + diff --git a/views/lists/subscription/add.hbs b/views/lists/subscription/add.hbs index 6b45acc3..5ab8aac1 100644 --- a/views/lists/subscription/add.hbs +++ b/views/lists/subscription/add.hbs @@ -50,6 +50,15 @@ {{/if}} + {{#if typeLongtext}} + + {{/if}} + + {{#if typeGpg}} + + Insert a GPG public key that will be used to encrypt messages sent this subscriber + {{/if}} + {{#if typeDateUs}}
diff --git a/views/lists/subscription/edit.hbs b/views/lists/subscription/edit.hbs index d22e9f50..b3cd5e97 100644 --- a/views/lists/subscription/edit.hbs +++ b/views/lists/subscription/edit.hbs @@ -63,6 +63,15 @@ {{/if}} + {{#if typeLongtext}} + + {{/if}} + + {{#if typeGpg}} + + Insert a GPG public key that will be used to encrypt messages sent this subscriber + {{/if}} + {{#if typeDateUs}}
diff --git a/views/subscription/manage.hbs b/views/subscription/manage.hbs index 9f6509aa..996af8ed 100644 --- a/views/subscription/manage.hbs +++ b/views/subscription/manage.hbs @@ -36,6 +36,15 @@ {{/if}} + {{#if typeLongtext}} + + {{/if}} + + {{#if typeGpg}} + + Insert your GPG public key here to encrypt messages sent to your address + {{/if}} + {{#if typeDateUs}}
diff --git a/views/subscription/subscribe.hbs b/views/subscription/subscribe.hbs index 8ed4e151..b8129989 100644 --- a/views/subscription/subscribe.hbs +++ b/views/subscription/subscribe.hbs @@ -33,6 +33,15 @@ {{/if}} + {{#if typeLongtext}} + + {{/if}} + + {{#if typeGpg}} + + Insert your GPG public key here to encrypt messages sent to your address + {{/if}} + {{#if typeDateUs}}