2015-04-20 17:56:43 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-03-09 19:52:48 +03:00
|
|
|
var async = require('async');
|
2016-06-03 11:20:53 +03:00
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var url = require('url');
|
|
|
|
|
var winston = require('winston');
|
2016-11-21 11:07:20 -05:00
|
|
|
var S = require('string');
|
2016-05-16 08:22:23 -04:00
|
|
|
|
2016-11-21 11:07:20 -05:00
|
|
|
var meta = require('../meta');
|
2016-02-23 13:08:38 +02:00
|
|
|
var cache = require('./cache');
|
|
|
|
|
var plugins = require('../plugins');
|
2017-04-08 20:22:21 -06:00
|
|
|
var translator = require('../translator');
|
2015-04-20 17:56:43 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Posts) {
|
2017-05-28 01:10:16 -04:00
|
|
|
|
|
|
|
|
Posts.urlRegex = {
|
|
|
|
|
regex: /href="([^"]+)"/g,
|
|
|
|
|
length: 6,
|
|
|
|
|
};
|
|
|
|
|
Posts.imgRegex = {
|
|
|
|
|
regex:/src="([^"]+)"/g,
|
|
|
|
|
length: 5,
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.parsePost = function (postData, callback) {
|
2017-03-09 19:52:48 +03:00
|
|
|
postData.content = String(postData.content || '');
|
2015-04-20 17:56:43 -04:00
|
|
|
|
2016-08-31 21:39:02 +03:00
|
|
|
if (postData.pid && cache.has(String(postData.pid))) {
|
|
|
|
|
postData.content = cache.get(String(postData.pid));
|
2015-04-20 17:56:43 -04:00
|
|
|
return callback(null, postData);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 19:52:48 +03:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('filter:parse.post', { postData: postData }, next);
|
|
|
|
|
},
|
|
|
|
|
function (data, next) {
|
|
|
|
|
data.postData.content = translator.escape(data.postData.content);
|
2016-03-17 11:38:21 +02:00
|
|
|
|
2017-03-09 19:52:48 +03:00
|
|
|
if (global.env === 'production' && data.postData.pid) {
|
|
|
|
|
cache.set(String(data.postData.pid), data.postData.content);
|
|
|
|
|
}
|
|
|
|
|
next(null, data.postData);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-04-20 17:56:43 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.parseSignature = function (userData, uid, callback) {
|
2016-11-21 11:07:20 -05:00
|
|
|
userData.signature = sanitizeSignature(userData.signature || '');
|
2017-02-18 12:30:49 -07:00
|
|
|
plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid }, callback);
|
2015-04-20 17:56:43 -04:00
|
|
|
};
|
2016-05-16 08:22:23 -04:00
|
|
|
|
2017-05-28 01:10:16 -04:00
|
|
|
Posts.relativeToAbsolute = function (content, regex) {
|
2016-05-16 08:22:23 -04:00
|
|
|
// Turns relative links in post body to absolute urls
|
2017-02-17 20:20:42 -07:00
|
|
|
var parsed;
|
2017-05-28 01:10:16 -04:00
|
|
|
var current = regex.regex.exec(content);
|
2017-02-17 20:20:42 -07:00
|
|
|
var absolute;
|
2017-02-18 13:51:26 -07:00
|
|
|
while (current !== null) {
|
2016-05-16 08:22:23 -04:00
|
|
|
if (current[1]) {
|
2016-06-03 11:20:53 +03:00
|
|
|
try {
|
|
|
|
|
parsed = url.parse(current[1]);
|
|
|
|
|
if (!parsed.protocol) {
|
|
|
|
|
if (current[1].startsWith('/')) {
|
|
|
|
|
// Internal link
|
2017-05-28 01:10:16 -04:00
|
|
|
absolute = nconf.get('base_url') + current[1];
|
2016-06-03 11:20:53 +03:00
|
|
|
} else {
|
|
|
|
|
// External link
|
|
|
|
|
absolute = '//' + current[1];
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-28 01:10:16 -04:00
|
|
|
content = content.slice(0, current.index + regex.length) + absolute + content.slice(current.index + regex.length + current[1].length);
|
2016-05-16 08:22:23 -04:00
|
|
|
}
|
2017-02-18 01:52:56 -07:00
|
|
|
} catch (err) {
|
2016-06-29 21:09:05 +03:00
|
|
|
winston.verbose(err.messsage);
|
|
|
|
|
}
|
2016-05-16 08:22:23 -04:00
|
|
|
}
|
2017-05-28 01:10:16 -04:00
|
|
|
current = regex.regex.exec(content);
|
2016-05-16 08:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
};
|
2016-11-21 11:07:20 -05:00
|
|
|
|
|
|
|
|
function sanitizeSignature(signature) {
|
2017-04-13 12:37:54 -06:00
|
|
|
signature = translator.escape(signature);
|
2017-03-09 19:52:48 +03:00
|
|
|
var string = S(signature);
|
2017-02-17 20:20:42 -07:00
|
|
|
var tagsToStrip = [];
|
2016-11-21 11:07:20 -05:00
|
|
|
|
|
|
|
|
if (parseInt(meta.config['signatures:disableLinks'], 10) === 1) {
|
|
|
|
|
tagsToStrip.push('a');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parseInt(meta.config['signatures:disableImages'], 10) === 1) {
|
|
|
|
|
tagsToStrip.push('img');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tagsToStrip.length ? string.stripTags.apply(string, tagsToStrip).s : signature;
|
|
|
|
|
}
|
2015-11-26 23:34:55 -05:00
|
|
|
};
|