Skip to content

Commit

Permalink
Merge branch 'next' into dynamic-widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
ar2rsawseen committed Jun 18, 2018
2 parents d4fbc5f + 77d3e83 commit 0acd28c
Show file tree
Hide file tree
Showing 27 changed files with 472 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@ api/node_modules/
.idea
frontend/express/node_modules/
api/config.js
countly_marked_version.json
frontend/express/config.js
frontend/express/public/javascripts/countly/countly.config.js
frontend/express/certificates
Expand Down
39 changes: 39 additions & 0 deletions api/api.js
Expand Up @@ -2,19 +2,58 @@ const http = require('http');
const cluster = require('cluster');
const formidable = require('formidable');
const os = require('os');
const fs = require('fs');
const countlyConfig = require('./config', 'dont-enclose');
const plugins = require('../plugins/pluginManager.js');
const jobs = require('./parts/jobs');
const log = require('./utils/log.js')('core:api');
const common = require('./utils/common.js');
const {processRequest} = require('./utils/requestProcessor');
const versionInfo = require('../frontend/express/version.info');

var t = ["countly:", "api"];

if (cluster.isMaster) {
t.push("master");
t.push("node");
t.push(process.argv[1]);
//save current version in file
if(versionInfo && versionInfo.version)
{
var olderVersions=[];
var currentVersion =versionInfo.version;
var lastVersion="";
if (fs.existsSync(__dirname+"/../countly_marked_version.json"))//read form file(if exist);
{
try
{
var data = fs.readFileSync(__dirname+"/../countly_marked_version.json");
try { olderVersions = JSON.parse(data);}
catch (SyntaxError) {//unable to parse file
log.e(SyntaxError);
}
if(Array.isArray(olderVersions))
{
lastVersion = olderVersions[olderVersions.length-1].version;
}
else
olderVersions=[];

}catch(error)
{
log.e(error);
}
}
if(lastVersion=="" || lastVersion!=currentVersion)
{
olderVersions.push({version:currentVersion,updated:Date.now()});
try
{
fs.writeFileSync(__dirname+"/../countly_marked_version.json",JSON.stringify(olderVersions));
}
catch(error){log.e(error);}
}
}
}
else{
t.push("worker");
Expand Down
133 changes: 71 additions & 62 deletions api/parts/data/events.js
Expand Up @@ -59,36 +59,40 @@ var countlyEvents = {},

for (var segKey in currEvent.segmentation) {

if(plugins.internalOmitSegments[currEvent.key] && Array.isArray(plugins.internalOmitSegments[currEvent.key]) && plugins.internalOmitSegments[currEvent.key].indexOf(segKey)!=-1)//check if segment should be ommited
{
continue;
}

if(omitted_segments[currEvent.key] && Array.isArray(omitted_segments[currEvent.key]) && omitted_segments[currEvent.key].indexOf(segKey)!=-1)//check if segment should be ommited
{
continue;
}

if (plugins.getConfig("api").event_segmentation_limit &&
appSegments[currEvent.key] &&
appSegments[currEvent.key].indexOf(segKey) === -1 &&
appSegments[currEvent.key].length >= plugins.getConfig("api").event_segmentation_limit) {
continue;
}

if (plugins.getConfig("api").event_segmentation_limit &&
appSegments[currEvent.key] &&
appSegments[currEvent.key].indexOf(segKey) === -1 &&
appSegments[currEvent.key].length >= plugins.getConfig("api").event_segmentation_limit) {
continue;
}

var tmpSegVal = currEvent.segmentation[segKey] + "";
var tmpSegVal = currEvent.segmentation[segKey] + "";

if (tmpSegVal == "") {
continue;
}
if (tmpSegVal == "") {
continue;
}

// Mongodb field names can't start with $ or contain .
tmpSegVal = tmpSegVal.replace(/^\$/, "").replace(/\./g, ":");
// Mongodb field names can't start with $ or contain .
tmpSegVal = tmpSegVal.replace(/^\$/, "").replace(/\./g, ":");

if (forbiddenSegValues.indexOf(tmpSegVal) !== -1) {
tmpSegVal = "[CLY]" + tmpSegVal;
}
var postfix = common.crypto.createHash("md5").update(tmpSegVal).digest('base64')[0];
metaToFetch[eventCollectionName + "no-segment_" + common.getDateIds(params).zero + "_" + postfix] = {
coll: eventCollectionName,
id: "no-segment_" + common.getDateIds(params).zero + "_" + postfix
};
if (forbiddenSegValues.indexOf(tmpSegVal) !== -1) {
tmpSegVal = "[CLY]" + tmpSegVal;
}
var postfix = common.crypto.createHash("md5").update(tmpSegVal).digest('base64')[0];
metaToFetch[eventCollectionName + "no-segment_" + common.getDateIds(params).zero + "_" + postfix] = {
coll: eventCollectionName,
id: "no-segment_" + common.getDateIds(params).zero + "_" + postfix
};

}
}
Expand Down Expand Up @@ -207,59 +211,64 @@ var countlyEvents = {},
}

for (var segKey in currEvent.segmentation) {
if(plugins.internalOmitSegments[currEvent.key] && Array.isArray(plugins.internalOmitSegments[currEvent.key]) && plugins.internalOmitSegments[currEvent.key].indexOf(segKey)!=-1)//check if segment should be ommited
{
continue;
}

if(omitted_segments[currEvent.key] && Array.isArray(omitted_segments[currEvent.key]) && omitted_segments[currEvent.key].indexOf(segKey)!=-1)//check if segment should be ommited
{
continue;
}

if (plugins.getConfig("api").event_segmentation_limit &&
appSegments[currEvent.key] &&
appSegments[currEvent.key].indexOf(segKey) === -1 &&
appSegments[currEvent.key].length >= plugins.getConfig("api").event_segmentation_limit) {
continue;
}
if (plugins.getConfig("api").event_segmentation_limit &&
appSegments[currEvent.key] &&
appSegments[currEvent.key].indexOf(segKey) === -1 &&
appSegments[currEvent.key].length >= plugins.getConfig("api").event_segmentation_limit) {
continue;
}

tmpEventObj = {};
var tmpSegVal = currEvent.segmentation[segKey] + "";
tmpEventObj = {};
var tmpSegVal = currEvent.segmentation[segKey] + "";

if (tmpSegVal == "") {
continue;
}
if (tmpSegVal == "") {
continue;
}

// Mongodb field names can't start with $ or contain .
tmpSegVal = tmpSegVal.replace(/^\$/, "").replace(/\./g, ":");
// Mongodb field names can't start with $ or contain .
tmpSegVal = tmpSegVal.replace(/^\$/, "").replace(/\./g, ":");

if (forbiddenSegValues.indexOf(tmpSegVal) !== -1) {
tmpSegVal = "[CLY]" + tmpSegVal;
}
var postfix = common.crypto.createHash("md5").update(tmpSegVal).digest('base64')[0];

if (plugins.getConfig("api").event_segmentation_value_limit &&
appSgValues[eventCollectionName] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey].indexOf(tmpSegVal) === -1 &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey].length >= plugins.getConfig("api").event_segmentation_value_limit) {
continue;
}
if (forbiddenSegValues.indexOf(tmpSegVal) !== -1) {
tmpSegVal = "[CLY]" + tmpSegVal;
}

var postfix = common.crypto.createHash("md5").update(tmpSegVal).digest('base64')[0];

if (plugins.getConfig("api").event_segmentation_value_limit &&
appSgValues[eventCollectionName] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey] &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey].indexOf(tmpSegVal) === -1 &&
appSgValues[eventCollectionName]["no-segment" + "_" + dateIds.zero + "_" + postfix][segKey].length >= plugins.getConfig("api").event_segmentation_value_limit) {
continue;
}

if (currEvent.sum && common.isNumber(currEvent.sum)) {
common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['sum'], currEvent.sum);
}
if (currEvent.dur && common.isNumber(currEvent.dur)) {
common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['dur'], currEvent.dur);
}
if (currEvent.sum && common.isNumber(currEvent.sum)) {
common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['sum'], currEvent.sum);
}

if (currEvent.dur && common.isNumber(currEvent.dur)) {
common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['dur'], currEvent.dur);
}

common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['count'], currEvent.count);
common.fillTimeObjectMonth(params, tmpEventObj, tmpSegVal + '.' + common.dbMap['count'], currEvent.count);

if (!eventSegmentsZeroes[eventCollectionName]) {
eventSegmentsZeroes[eventCollectionName] = [];
common.arrayAddUniq(eventSegmentsZeroes[eventCollectionName], dateIds.zero + "." + postfix);
} else {
common.arrayAddUniq(eventSegmentsZeroes[eventCollectionName], dateIds.zero + "." + postfix);
}
if (!eventSegmentsZeroes[eventCollectionName]) {
eventSegmentsZeroes[eventCollectionName] = [];
common.arrayAddUniq(eventSegmentsZeroes[eventCollectionName], dateIds.zero + "." + postfix);
} else {
common.arrayAddUniq(eventSegmentsZeroes[eventCollectionName], dateIds.zero + "." + postfix);
}

if (!eventSegments[eventCollectionName + "." + dateIds.zero + "." + postfix]) {
eventSegments[eventCollectionName + "." + dateIds.zero + "." + postfix] = {};
Expand Down
1 change: 0 additions & 1 deletion api/utils/requestProcessor.js
Expand Up @@ -1323,7 +1323,6 @@ const processRequest = (params) => {
path: params.qstring.path,
data: params.qstring.data,
method: params.qstring.method,
post: params.qstring.post,
prop: params.qstring.prop,
type: params.qstring.type,
filename: params.qstring.filename
Expand Down
89 changes: 89 additions & 0 deletions bin/scripts/checking_versions.js
@@ -0,0 +1,89 @@
const fs = require('fs');
const versionInfo = require('../../frontend/express/version.info');
var versions=[];
var marked_version="";
var current_version="";

//get current version
if(versions && versionInfo.version)
current_version = versionInfo.version;

//load marked version
if (fs.existsSync(__dirname+"/../../countly_marked_version.json"))//read form file(if exist);
{
var olderVersions=[];
try
{
var data = fs.readFileSync(__dirname+"/../../countly_marked_version.json");
try { olderVersions = JSON.parse(data);}
catch (SyntaxError) {//unable to parse file
console.error(SyntaxError);
}
if(Array.isArray(olderVersions))
marked_version = olderVersions[olderVersions.length-1].version;
}catch(error){console.error(error);}
}
//reading version numbers from upgrade folder
var pattern = new RegExp(/^(([0-9])*\.)*[0-9]*$/);
try
{
var dir_items = fs.readdirSync("../upgrade");
for (var i=0; i<dir_items.length; i++)
{
if(dir_items[i] != '.')
{
try
{
stat = fs.statSync("../upgrade/"+dir_items[i]);
if(stat.isDirectory() && pattern.test(dir_items[i]))
{
var my_name = dir_items[i]
versions.push(dir_items[i]);
}
}catch(error)
{
console.error(error);
}
}
}

}catch(error)
{
console.error(error);
}
versions = versions.sort();

var from=0;
var til=versions.length-1;

if(current_version=="")
{
console.error("could not load current version.");
return;
}

if(marked_version=="")
{
console.error("Could not load marked version.");
return;
}
if(current_version==marked_version)
{
console.error("up to date");
return;
}
else
{
while(versions[from]<=marked_version && from<versions.length){ from++;}
while(versions[til]>current_version && til>=0){ til--;}

if(til==-1 || from ==versions.length)
{
console.error("version range not found");
return;
}
else{
versions = versions.slice(from,til+1);
console.log(versions.join(";"));
}
}
47 changes: 33 additions & 14 deletions frontend/express/app.js
Expand Up @@ -816,22 +816,41 @@ app.get(countlyConfig.path+'/reset/:prid', function (req, res, next) {
});

app.post(countlyConfig.path+'/reset', function (req, res, next) {
if (req.body.password && req.body.again && req.body.prid) {
req.body.prid += "";
var password = sha512Hash(req.body.password);

countlyDb.collection('password_reset').findOne({prid:req.body.prid}, function (err, passwordReset) {
countlyDb.collection('members').findAndModify({_id:passwordReset.user_id}, {}, {'$set':{ "password":password }}, function (err, member) {
member = member && member.ok ? member.value : null;
plugins.callMethod("passwordReset", {req:req, res:res, next:next, data:member});
req.flash('info', 'reset.result');
res.redirect(countlyConfig.path+'/login');
});
var result = validatePassword(req.body.password);

if (result === false) {
if (req.body.password && req.body.again && req.body.prid) {
req.body.prid += "";
var password = sha512Hash(req.body.password);

countlyDb.collection('password_reset').findOne({ prid: req.body.prid }, function (err, passwordReset) {
countlyDb.collection('members').findAndModify({ _id: passwordReset.user_id }, {}, { '$set': { "password": password } }, function (err, member) {
member = member && member.ok ? member.value : null;
plugins.callMethod("passwordReset", { req: req, res: res, next: next, data: member });
req.flash('info', 'reset.result');
res.redirect(countlyConfig.path + '/login');
});

countlyDb.collection('password_reset').remove({prid:req.body.prid}, function () {});
});
countlyDb.collection('password_reset').remove({ prid: req.body.prid }, function () { });
});
} else {
res.render('reset', { countlyFavicon: req.countly.favicon, countlyTitle: req.countly.title, countlyPage: req.countly.page, "csrf": req.csrfToken(), "prid": req.body.prid, "message": "", path: countlyConfig.path || "", cdn: countlyConfig.cdn || "", themeFiles: req.themeFiles, inject_template: req.template });
}
} else {
res.render('reset', { countlyFavicon:req.countly.favicon, countlyTitle:req.countly.title, countlyPage:req.countly.page, "csrf":req.csrfToken(), "prid":req.body.prid, "message":"", path:countlyConfig.path || "", cdn:countlyConfig.cdn || "", themeFiles:req.themeFiles, inject_template:req.template});
res.render('reset',
{
countlyFavicon: req.countly.favicon,
countlyTitle: req.countly.title,
countlyPage: req.countly.page,
"csrf": req.csrfToken(),
"prid": req.body.prid,
path: countlyConfig.path || "",
cdn: countlyConfig.cdn || "",
themeFiles: req.themeFiles,
inject_template: req.template,
message: result,
password_min: plugins.getConfig("security").password_min
});
}
});

Expand Down

0 comments on commit 0acd28c

Please sign in to comment.