integrated dcp templates, changed reset email to use templates

v1.18.x
psychobunny 12 years ago
parent 1b3b0c6c7e
commit 8f7b295480

@ -15,6 +15,10 @@
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
(function() { (function() {
document.getElementById('reset').onclick = function() {
socket.emit('user.send_reset', { email: document.getElementById('email').value });
};
socket.on('user.send_reset', function(data) { socket.on('user.send_reset', function(data) {
var inputEl = document.getElementById('email'), var inputEl = document.getElementById('email'),
submitEl = document.getElementById('reset'), submitEl = document.getElementById('reset'),

@ -6,16 +6,92 @@ var fs = require('fs');
function loadTemplates(templatesToLoad) { function loadTemplates(templatesToLoad) {
for (var t in templatesToLoad) { for (var t in templatesToLoad) {
(function(template) { (function(file) {
fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + template + '.tpl', function(err, html) { fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + file + '.tpl', function(err, html) {
global.templates[template] = html; var template = function() {
this.toString = function() {
return this.html;
};
}
template.prototype.parse = parse;
template.prototype.html = String(html);
global.templates[file] = new template;
}); });
}(templatesToLoad[t])); }(templatesToLoad[t]));
} }
} }
Templates.init = function() { Templates.init = function() {
loadTemplates(['header', 'footer', 'register', 'home', 'login', 'reset', 'reset_code']); loadTemplates(['header', 'footer', 'register', 'home', 'login', 'reset', 'reset_code', 'emails/reset']);
}
var parse = function(data) {
function replace(key, value, template) {
return template.replace("{" + key + "}", value);
}
function makeRegex(block) {
return new RegExp("<!-- BEGIN " + block + " -->[^]*<!-- END " + block + " -->", 'g');
}
function getBlock(regex, block, template) {
data = template.match(regex);
if (data == null) return;
data = data[0]
.replace("<!-- BEGIN " + block + " -->", "")
.replace("<!-- END " + block + " -->", "");
return data;
}
function setBlock(regex, block, template) {
return template.replace(regex, block);
}
var template = this.html, regex, block;
return (function parse(data, namespace, template) {
for (var d in data) {
if (data.hasOwnProperty(d)) {
if (data[d] instanceof String) {
continue;
} else if (data[d].constructor == Array) {
namespace += d;
regex = makeRegex(d),
block = getBlock(regex, namespace, template)
if (block == null) continue;
var numblocks = data[d].length - 1, i = 0, result = "";
do {
result += parse(data[d][i], namespace + '.', block);
} while (i++ < numblocks);
template = setBlock(regex, result, template);
} else if (data[d] instanceof Object) {
namespace += d + '.';
regex = makeRegex(d),
block = getBlock(regex, namespace, template)
if (block == null) continue;
block = parse(data[d], namespace, block);
template = setBlock(regex, block, template);
} else {
template = replace(namespace + d, data[d], template);
}
}
}
return template;
})(data, "", template);
} }
}(exports)); }(exports));

@ -116,26 +116,22 @@ var config = require('../config.js'),
var reset_code = utils.generateUUID(); var reset_code = utils.generateUUID();
RDB.set('user:reset:' + reset_code, uid); RDB.set('user:reset:' + reset_code, uid);
var reset_link = config.url + 'reset/' + reset_code,
reset_email = global.templates['emails/reset'].parse({'RESET_LINK': reset_link});
var message = emailjs.message.create({ var message = emailjs.message.create({
text: "Hello,\n\n" + text: reset_email,
"We received a request to reset your password, possibly because you have forgotten it. If this is not the case, please ignore this email.\n\n" +
"To continue with the password reset, please click on the following link:\n\n" +
"&nbsp;&nbsp;" + config.url + 'reset/' + reset_code + "\n\n\n" +
"Thanks!\nNodeBB",
from: config.mailer.from, from: config.mailer.from,
to: email, to: email,
subject: 'Password Reset Requested', subject: 'Password Reset Requested',
attachment: [ attachment: [
{ {
data: "<p>Hello,</p>" + data: reset_email,
"<p>We received a request to reset your password, possibly because you have forgotten it. If this is not the case, please ignore this email.</p>" +
"<p>To continue with the password reset, please click on the following link:</p>" +
"<blockquote>" + config.url + 'reset/' + reset_code + "</blockquote>" +
"<p>Thanks!<br /><strong>NodeBB</strong>",
alternative: true alternative: true
} }
] ]
}); });
emailjsServer.send(message, function(err, success) { emailjsServer.send(message, function(err, success) {
if (err === null) { if (err === null) {
global.socket.emit('user.send_reset', { global.socket.emit('user.send_reset', {
@ -144,6 +140,7 @@ var config = require('../config.js'),
email: email email: email
}); });
} }
else throw new Error(err);
}); });
} else { } else {
global.socket.emit('user.send_reset', { global.socket.emit('user.send_reset', {

Loading…
Cancel
Save