/* global $ jQuery */
/* exported load_modal */
var ENTERPRISE_DIR = "enterprise";
/* Function to hide/unhide a specific Div id */
function toggleDiv(divid) {
if (document.getElementById(divid).style.display == "none") {
document.getElementById(divid).style.display = "block";
} else {
document.getElementById(divid).style.display = "none";
}
}
function winopeng(url, wid) {
open(
url,
wid,
"width=1000,height=550,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes"
);
// WARNING !! Internet Explorer DOESNT SUPPORT "-" CARACTERS IN WINDOW HANDLE VARIABLE
status = wid;
}
function winopeng_var(url, wid, width, height) {
open(
url,
wid,
"width=" +
width +
",height=" +
height +
",status=no,toolbar=no,menubar=no,scrollbar=yes"
);
// WARNING !! Internet Explorer DOESNT SUPPORT "-" CARACTERS IN WINDOW HANDLE VARIABLE
status = wid;
}
function newTabjs(content) {
content = atob(content);
var printWindow = window.open("");
printWindow.document.body.innerHTML += "
" + content + "
";
}
function open_help(url) {
if (!navigator.onLine) {
alert(
"The help system could not be started. Please, check your network connection."
);
return;
}
if (url == "") {
alert(
"The help system is currently under maintenance. Sorry for the inconvenience."
);
return;
}
open(
url,
"pandorahelp",
"width=650,height=500,status=0,toolbar=0,menubar=0,scrollbars=1,location=0"
);
}
/**
* Decode HTML entities into characters. Useful when receiving something from AJAX
*
* @param str String to convert
*
* @retval str with entities decoded
*/
function js_html_entity_decode(str) {
if (!str) return "";
str2 = str
.replace(//g, ">")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\/g, "\\")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&/g, "&")
.replace(/ /g, " ")
.replace(/
/g, "\r")
.replace(/
/g, "\n");
return str2;
}
function truncate_string(str, str_length, separator) {
if (str.length <= str_length) {
return str;
}
separator = separator || "...";
var separator_length = separator.length,
chars_to_show = str_length - separator_length,
front_chars = Math.ceil(chars_to_show / 2),
tail_chars = Math.floor(chars_to_show / 2);
return (
str.substr(0, front_chars) + separator + str.substr(str.length - tail_chars)
);
}
/**
* Function to search an element in an array.
*
* Extends the array object to use it like a method in an array object. Example:
*
a = Array (4, 7, 9);
alert (a.in_array (4)); // true
alert (a.in_array (5)); // false
*/
Array.prototype.in_array = function() {
for (var j in this) {
if (this[j] == arguments[0]) return true;
}
return false;
};
/**
* Util for check is empty object
*
* @param obj the object to check
* @returns {Boolean} True it is empty
*/
function isEmptyObject(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) return false;
}
return true;
}
/**
* Fill up select box with id "module" with modules after agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function agent_changed_by_multiple_agents(event, id_agent, selected) {
// Hack to avoid certain module types
var module_types_excluded = [];
if (typeof $("input.module_types_excluded") !== "undefined") {
try {
$("input.module_types_excluded").each(function(index, el) {
var module_type = parseInt($(el).val());
if (isNaN(module_type) == false)
module_types_excluded.push(module_type);
});
} catch (error) {}
}
var module_status = -1;
if (typeof $("#status_module") !== "undefined") {
try {
module_status = $("#status_module").val();
} catch (error) {}
}
// Module name
var module_name = $("#text-module_filter").val();
var idAgents = Array();
jQuery.each($("#id_agents option:selected"), function(i, val) {
//val() because the var is same
idAgents.push($(val).val());
});
var tags_to_search = $("#tags1").val();
//Hack to find only enabled modules
//Pass a flag as global var
find_modules = "all";
if (
typeof show_only_enabled_modules !== "undefined" &&
show_only_enabled_modules
) {
find_modules = "enabled";
}
var selection_mode = $("#modules_selection_mode").val();
if (typeof selection_mode === "undefined") {
selection_mode = "common";
}
var serialized = $("#hidden-serialized").val();
if (typeof serialized === "undefined") {
serialized = "";
}
var id_group = null;
if (typeof $("#filter_group") !== "undefined") {
try {
id_group = $("#filter_group").val();
} catch (error) {}
}
$("#module")
.prop("disabled", true)
.empty()
.append(
$("")
.html("Loading...")
.attr("value", 0)
);
// Check if homedir was received like a JSON
var homedir = ".";
var id_server = 0;
if (typeof event !== "undefined" && typeof event.data !== "undefined") {
if (event.data != null) {
if (typeof event.data !== "undefined") {
if (typeof event.data.homedir !== "undefined") {
homedir = event.data.homedir;
}
if (
typeof event.data.metaconsole !== "undefined" &&
event.data.metaconsole
) {
id_server = $("#" + event.data.id_server).val();
}
}
}
}
jQuery.post(
homedir + "/ajax.php",
{
page: "operation/agentes/ver_agente",
get_agent_modules_json_for_multiple_agents: 1,
"id_agent[]": idAgents,
"tags[]": tags_to_search,
all: find_modules,
"module_types_excluded[]": module_types_excluded,
name: module_name,
selection_mode: selection_mode,
serialized: serialized,
id_server: id_server,
status_module: module_status,
id_group: id_group
},
function(data) {
$("#module").empty();
if (isEmptyObject(data)) {
//Trick for catch the translate text.
var noneText =
$("#id_agents").val() === null
? $("#select_agent_first_text").html()
: $("#none_text").html();
if (noneText == null) {
noneText = "None";
}
$("#module").append(
$("")
.html(noneText)
.attr("None", "")
.prop("selected", true)
);
return;
}
if (typeof $(document).data("text_for_module") != "undefined") {
$("#module").append(
$("")
.html($(document).data("text_for_module"))
.attr("value", 0)
.prop("selected", true)
);
} else {
if (typeof data["any_text"] != "undefined") {
$("#module").append(
$("")
.html(data["any_text"])
.attr("value", 0)
.prop("selected", true)
);
} else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = "Any";
}
$("#module").append(
$("")
.html(anyText)
.attr("value", 0)
.prop("selected", true)
);
}
}
var all_common_modules = [];
$.each(data, function(i, val) {
var s = js_html_entity_decode(val);
s = s.replace(/"/g, """).replace(/'/g, "'");
i = i.replace(/"/g, """).replace(/'/g, "'");
$("#module").append(
$('').text(val)
);
all_common_modules.push(i);
$("#module").fadeIn("normal");
});
$("#hidden-all_common_modules").val(all_common_modules.toString());
if (typeof selected !== "undefined") $("#module").attr("value", selected);
$("#module")
.css("max-width", "")
.prop("disabled", false);
if (typeof function_hook_loaded_module_list == "function") {
function_hook_loaded_module_list();
}
},
"json"
);
}
/**
* Fill up select box with id "module" with modules with alerts of one template
* after agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function agent_changed_by_multiple_agents_with_alerts(
event,
id_agent,
selected
) {
var idAgents = Array();
jQuery.each($("#id_agents option:selected"), function(i, val) {
//val() because the var is same
idAgents.push($(val).val());
});
var selection_mode = $("#modules_selection_mode").val();
if (selection_mode == undefined) {
selection_mode = "common";
}
template = $("#id_alert_template option:selected").val();
$("#module").attr("disabled", 1);
$("#module").empty();
$("#module").append(
$("")
.html("Loading...")
.attr("value", 0)
);
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_agent_modules_multiple_alerts_json_for_multiple_agents: 1,
template: template,
"id_agent[]": idAgents,
selection_mode: selection_mode
},
function(data) {
$("#module").empty();
if (typeof $(document).data("text_for_module") != "undefined") {
$("#module").append(
$("")
.html($(document).data("text_for_module"))
.attr("value", 0)
.prop("selected", true)
);
} else {
if (typeof data["any_text"] != "undefined") {
$("#module").append(
$("")
.html(data["any_text"])
.attr("value", 0)
.prop("selected", true)
);
} else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = "Any";
}
$("#module").append(
$("")
.html(anyText)
.attr("value", 0)
.prop("selected", true)
);
}
}
jQuery.each(data, function(i, val) {
var s = js_html_entity_decode(val);
s = s.replace(/"/g, """).replace(/'/g, "'");
$("#module").append($('').text(val));
$("#module").fadeIn("normal");
});
if (selected != undefined) $("#module").attr("value", selected);
$("#module").removeAttr("disabled");
},
"json"
);
}
/**
* Fill up select box with id "module" with modules with alerts of one or more templates
* before agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function alert_templates_changed_by_multiple_agents_with_alerts(
event,
id_agent,
selected
) {
var idAgents = Array();
jQuery.each($("#id_agents option:selected"), function(i, val) {
//val() because the var is same
idAgents.push($(val).val());
});
var selection_mode = $("#modules_selection_mode").val();
if (selection_mode == undefined) {
selection_mode = "common";
}
templates = Array();
jQuery.each($("#id_alert_templates option:selected"), function(i, val) {
//val() because the var is same
templates.push($(val).val());
});
$("#module").attr("disabled", 1);
$("#module").empty();
$("#module").append(
$("")
.html("Loading...")
.attr("value", 0)
);
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_agent_modules_alerts_json_for_multiple_agents: 1,
"templates[]": templates,
"id_agent[]": idAgents,
selection_mode: selection_mode
},
function(data) {
$("#module").empty();
if (typeof $(document).data("text_for_module") != "undefined") {
$("#module").append(
$("")
.html($(document).data("text_for_module"))
.attr("value", 0)
.prop("selected", true)
);
} else {
if (typeof data["any_text"] != "undefined") {
$("#module").append(
$("")
.html(data["any_text"])
.attr("value", 0)
.prop("selected", true)
);
} else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = "Any";
}
$("#module").append(
$("")
.html(anyText)
.attr("value", 0)
.prop("selected", true)
);
}
}
jQuery.each(data, function(i, val) {
var decoded_val = js_html_entity_decode(val);
decoded_val = decoded_val
.replace(/"/g, """)
.replace(/'/g, "'");
$("#module").append(
$(
''
).text(val)
);
$("#module").fadeIn("normal");
});
if (selected != undefined) $("#module").attr("value", selected);
$("#module").removeAttr("disabled");
},
"json"
);
}
/**
* Fill up select box with id "agent" with agents after module has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_module Module ID that has been selected
* @param selected Which agent(s) have to be selected
*/
function module_changed_by_multiple_modules(event, id_module, selected) {
var idModules = Array();
jQuery.each($("#module_name option:selected"), function(i, val) {
//val() because the var is same
idModules.push($(val).val());
});
$("#agents").attr("disabled", 1);
$("#agents").empty();
$("#agents").append(
$("")
.html("Loading...")
.attr("value", 0)
);
var status_module = -1;
if (typeof $("#status_module") !== "undefined") {
try {
status_module = $("#status_module").val();
} catch (error) {}
}
var selection_mode = $("#agents_selection_mode").val();
if (selection_mode == undefined) {
selection_mode = "common";
}
var tags_selected = [];
var tags_to_search = $("#tags").val();
if (tags_to_search != null) {
if (tags_to_search[0] != -1) {
tags_selected = tags_to_search;
}
}
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_agents_json_for_multiple_modules: 1,
truncate_agent_names: 1,
status_module: status_module,
"module_name[]": idModules,
selection_mode: selection_mode,
tags: tags_selected
},
function(data) {
$("#agents").append(
$("")
.html("Loading...")
.attr("value", 0)
);
$("#agents").empty();
if (isEmptyObject(data)) {
var noneText = $("#none_text").html(); //Trick for catch the translate text.
if (noneText == null) {
noneText = "None";
}
$("#agents").append(
$("")
.html(noneText)
.attr("None", "")
.prop("selected", true)
);
return;
}
if (typeof $(document).data("text_for_module") != "undefined") {
$("#agents").append(
$("")
.html($(document).data("text_for_module"))
.attr("value", 0)
.prop("selected", true)
);
} else {
if (typeof data["any_text"] != "undefined") {
$("#agents").append(
$("")
.html(data["any_text"])
.attr("value", 0)
.prop("selected", true)
);
} else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = "Any";
}
$("#agents").append(
$("")
.html(anyText)
.attr("value", 0)
.prop("selected", true)
);
}
}
jQuery.each(data, function(i, val) {
s = js_html_entity_decode(val);
$("#agents").append(
$("")
.html(truncate_string(s, 30, "..."))
.attr({ value: i, title: s })
);
$("#agents").fadeIn("normal");
});
if (selected != undefined) $("#agents").attr("value", selected);
$("#agents").removeAttr("disabled");
},
"json"
);
}
/**
* Fill up select box with id "module" with modules after agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function agent_changed_by_multiple_agents_id(event, id_agent, selected) {
var idAgents = Array();
jQuery.each($("#id_agents option:selected"), function(i, val) {
//val() because the var is same
idAgents.push($(val).val());
});
$("#module").attr("disabled", 1);
$("#module").empty();
$("#module").append(
$("")
.html("Loading...")
.attr("value", 0)
);
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_agent_modules_json_for_multiple_agents_id: 1,
"id_agent[]": idAgents
},
function(data) {
$("#module").empty();
if (typeof $(document).data("text_for_module") != "undefined") {
$("#module").append(
$("")
.html($(document).data("text_for_module"))
.attr("value", 0)
.prop("selected", true)
);
} else {
if (typeof data["any_text"] != "undefined") {
$("#module").append(
$("")
.html(data["any_text"])
.attr("value", 0)
.prop("selected", true)
);
} else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = "Any";
}
$("#module").append(
$("")
.html(anyText)
.attr("value", 0)
.prop("selected", true)
);
}
}
jQuery.each(data, function(i, val) {
s = js_html_entity_decode(val["nombre"]);
//$('#module').append ($('').html (s).attr ("value", val));
$("#module").append(
$("")
.html(s)
.attr("value", val["id_agente_modulo"])
);
$("#module").fadeIn("normal");
});
if (selected != undefined) $("#module").attr("value", selected);
$("#module").removeAttr("disabled");
},
"json"
);
}
function post_process_select_init(name) {
// Manual mode is hidden by default
$("#" + name + "_manual").hide();
$("#" + name + "_default").show();
}
function post_process_select_init_inv(name) {
$("#" + name + "_manual").show();
$("#" + name + "_default").hide();
}
function post_process_select_init_unit(name, selected) {
// Manual mode is hidden by default
$("#" + name + "_manual").hide();
$("#" + name + "_default").show();
if (selected != "") {
var select_or_text = false;
$("#" + name + "_select option").each(function(i, item) {
if ($(item).val() == selected) {
select_or_text = true;
return false;
}
});
if (select_or_text) {
$("#" + name + "_select option[value='" + selected + "']").attr(
"selected",
true
);
$("#text-" + name + "_text").val("");
} else {
$("#" + name + "_select option[value=0]").attr("selected", true);
$("#" + name + "_default").hide();
$("#" + name + "_manual").show();
}
} else {
$("#" + name + "_select option[value=0]").attr("selected", true);
$("#" + name + "_default").hide();
$("#" + name + "_manual").show();
}
$("#" + name + "_select").change(function() {
var value = $("#" + name + "_select").val();
$("#" + name + "_select option[value='" + value + "']").attr(
"selected",
true
);
});
}
function post_process_select_events_unit(name, selected) {
$("." + name + "_toggler").click(function() {
var value = $("#text-" + name + "_text").val();
var count = $("#" + name + "_select option").filter(function(i, item) {
if ($(item).val() == value) return true;
else return false;
}).length;
if (count != 1) {
$("#" + name + "_select").append(
$("")
.attr("value", id)
.html(value);
$("#" + name).append(option);
});
}
},
"json"
);
}
// eslint-disable-next-line no-unused-vars
function selection_multiple_change(info) {
info = JSON.parse(atob(info));
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_modules_group_json: 1,
id_agents: $("#" + info.agent_name.replace("[]", "")).val(),
selection: $("#" + info.selectionModulesNameId).val(),
select_mode: 1
},
function(data) {
var name = info.modules_name.replace("[]", "");
$("#" + name).html("");
// Check module all.
$("#checkbox-" + name + "-check-all").prop("checked", false);
if (data) {
jQuery.each(data, function(id, value) {
var option = $("")
.attr("value", id)
.html(value);
$("#" + name).append(option);
});
}
},
"json"
);
}
/*
* Creates a progressbar.
* @param id the id of the div we want to transform in a progressbar.
* @param duration the duration of the timer example: '10s'.
* @param iteration.
* @param callback, optional function which is called when the progressbar reaches 0.
*/
function createProgressTimeBar(id, duration, iteration, callback) {
// We select the div that we want to turn into a progressbar
var progressbar = document.getElementById(id);
progressbar.className = "progressbar";
// We create the div that changes width to show progress
var progressbarinner = document.createElement("div");
progressbarinner.className = "inner";
// Now we set the animation parameters
progressbarinner.style.animationDuration = duration;
progressbarinner.style.animationIterationCount = iteration;
// Eventually couple a callback
if (typeof callback === "function") {
if (iteration === "infinite") {
progressbarinner.addEventListener("animationiteration", callback);
} else {
progressbarinner.addEventListener("animationend", callback);
}
}
// Append the progressbar to the main progressbardiv
progressbar.appendChild(progressbarinner);
// When everything is set up we start the animation
progressbarinner.style.animationPlayState = "running";
return progressbarinner;
}
function progressTimeBar(id, interval, iteration, callback) {
var progress = createProgressTimeBar(id, interval + "s", iteration, callback);
var controls = {
start: function() {
progress.style.animationPlayState = "running";
},
paused: function() {
progress.style.animationPlayState = "paused";
}
};
return controls;
}
/**
* Filter selector item by text based on a text input.
*
* @param {string} textbox Text input.
*
* @return {void}
*/
$.fn.filterByText = function(textbox) {
var select = this;
$(textbox).bind("change keyup", function() {
var search = $.trim($(textbox).val());
var regex = new RegExp(search, "gi");
$(select)
.find("option")
.each(function() {
if (
$(this)
.text()
.match(regex) !== null
) {
$(this).show();
} else {
$(this).hide();
}
});
});
};
function loadPasswordConfig(id, value) {
$.ajax({
url: "ajax.php",
data: {
page: "include/ajax/config.ajax",
token_name: `${value}`,
no_boolean: 1
},
type: "GET",
dataType: "json",
success: function(data) {
$(`#${id}`).val(data);
}
});
}