#12503 changed unique ip in agent

This commit is contained in:
Daniel Cebrian 2023-12-29 13:43:28 +01:00
parent 58c9cc5b93
commit 0627ea07cd
4 changed files with 104 additions and 32 deletions

View File

@ -399,12 +399,12 @@ if ($new_agent === true) {
// Ip adress.
$tableAgent->data['caption_ip_address'] = __('IP Address');
$tableAgent->rowclass['ip_address'] = 'w540px';
$tableAgent->rowclass['ip_address'] = 'w400px';
$tableAgent->data['ip_address'][0] = html_print_input_text('direccion', $direccion_agente, '', 16, 100, true, false, false, '', 'w540px');
$tableAgent->data['ip_address'][1] = html_print_button(__('Check unique IP'), 'check_unique_ip', false, '', ['class' => 'secondary w130px'], true);
$tableAgent->data['message_check_ip'][0] = html_print_div(['id' => 'message_check_ip'], true);
$tableAgent->rowclass['additional_ip_address'] = 'subinput';
$tableAgent->data['additional_ip_address'][0] = html_print_checkbox_switch('unique_ip', 1, $config['unique_ip'], true);
$tableAgent->data['additional_ip_address'][1] = __('Unique IP');
$tableAgent->cellclass['additional_ip_address'][1] = 'w120px';
$tableAgent->data['additional_ip_address'][2] = html_print_input(
[
@ -1301,6 +1301,7 @@ ui_require_jquery_file('bgiframe');
?>
<script type="text/javascript">
let unique_ip_trigger = false;
// Show/Hide custom field row.
function show_custom_field_row(id){
if( $('#field-'+id).css('display') == 'none'){
@ -1464,23 +1465,40 @@ ui_require_jquery_file('bgiframe');
$("#text-agente").prop('readonly', true);
// Disable fixed ip button if empty.
if($("#text-direccion").val() == '') {
$("#fixed_ip").prop('disabled',true);
}
$("#text-direccion").on('input',function(e){
if($("#text-direccion").val() == '') {
$("#fixed_ip").prop('disabled',true);
} else {
$("#fixed_ip").prop('disabled',false);
$("#text-direccion").on('change',function(e){
const unique_ip_token = '<?php echo $config['unique_ip']; ?>';
unique_ip_trigger = false;
if (unique_ip_token == 1) {
check_unique_ip();
}
});
check_basic_options();
$('#id_os').on('change', function(){
check_basic_options();
})
});
$('#button-check_unique_ip').on('click', function() {
check_unique_ip();
});
$('#form_agent').on('submit', function(e) {
if (unique_ip_trigger) {
e.preventDefault();
const form = this;
confirmDialog(
{
title: '<?php echo __('Are you sure?'); ?>',
message: '<?php echo __('This IP address is in use. Are you sure you want to save it?'); ?>',
ok: '<?php echo __('Yes'); ?>',
cancel: '<?php echo __('Cancel'); ?>',
onAccept: function() {
form.submit();
}
}
);
}
});
});
function check_basic_options(){
@ -1490,4 +1508,40 @@ ui_require_jquery_file('bgiframe');
$('#basic_options').addClass('invisible');
}
}
function check_unique_ip() {
const direccion = $('#text-direccion').val();
let ip_all = <?php echo json_encode($ip_all); ?>;
if (!ip_all) {
ip_all = Object.keys(ip_all);
}
$.ajax({
method: "POST",
url: "<?php echo ui_get_full_url('ajax.php'); ?>",
dataType: 'json',
data: {
page: "include/ajax/agent",
check_unique_ip: 1,
direccion,
ip_all
},
success: function(data) {
if (data.success) {
$('#message_check_ip').attr('class', 'success');
} else {
$('#message_check_ip').attr('class', 'error');
}
if(data.exist_ip) {
unique_ip_trigger = true;
} else {
unique_ip_trigger = false;
}
$('#message_check_ip').html(data.message);
}
});
}
</script>

View File

@ -102,7 +102,6 @@ $alias_as_name = 0;
$direccion_agente = get_parameter('direccion', '');
$direccion_agente = trim(io_safe_output($direccion_agente));
$direccion_agente = io_safe_input($direccion_agente);
$unique_ip = 0;
$intervalo = SECONDS_5MINUTES;
$ff_interval = 0;
$quiet_module = 0;
@ -186,7 +185,6 @@ if ($create_agent) {
$alias = io_safe_input(trim(preg_replace('/[\/\\\|%#&$]/', '', $alias_safe_output)));
$alias_as_name = (int) get_parameter_post('alias_as_name', 0);
$direccion_agente = (string) get_parameter_post('direccion', '');
$unique_ip = (int) get_parameter_post('unique_ip', 0);
// Safe_output only validate ip.
$direccion_agente = trim(io_safe_output($direccion_agente));
@ -269,12 +267,7 @@ if ($create_agent) {
$nombre_agente = $alias;
}
if ($unique_ip && $direccion_agente != '') {
$sql = 'SELECT direccion FROM tagente WHERE direccion = "'.$direccion_agente.'"';
$exists_ip = db_get_row_sql($sql);
}
if (!$exists_alias && !$exists_ip) {
if (!$exists_alias) {
$id_agente = db_process_sql_insert(
'tagente',
[
@ -371,8 +364,6 @@ if ($create_agent) {
$agent_creation_error = __('Could not be created');
if ($exists_alias) {
$agent_creation_error = __('Could not be created, because name already exists');
} else if ($exists_ip) {
$agent_creation_error = __('Could not be created, because IP already exists');
}
}
}
@ -962,7 +953,6 @@ if ($update_agent) {
$alias = io_safe_input(trim(preg_replace('/[\/\\\|%#&$]/', '', $alias_safe_output)));
$alias_as_name = (int) get_parameter_post('alias_as_name', 0);
$direccion_agente = (string) get_parameter_post('direccion', '');
$unique_ip = (int) get_parameter_post('unique_ip', 0);
// Safe_output only validate ip.
$direccion_agente = trim(io_safe_output($direccion_agente));
@ -1097,18 +1087,11 @@ if ($update_agent) {
// If there is an agent with the same name, but a different ID.
}
if ($direccion_agente !== $address_list && (bool) $unique_ip === true && $direccion_agente != '') {
$sql = 'SELECT direccion FROM tagente WHERE direccion = "'.$direccion_agente.'"';
$exists_ip = db_get_row_sql($sql);
}
$old_group = agents_get_agent_group($id_agente);
if ($grupo <= 0) {
ui_print_error_message(__('The group id %d is incorrect.', $grupo));
} else if ($old_group !== $grupo && group_allow_more_agents($grupo, true, 'update') === false) {
ui_print_error_message(__('Agent cannot be updated due to the maximum agent limit for this group'));
} else if ($exists_ip) {
ui_print_error_message(__('Duplicate main IP address'));
} else {
// If different IP is specified than previous, add the IP.
if ($direccion_agente != ''

View File

@ -39,6 +39,7 @@ $get_agent_filters = get_parameter('get_agent_filters', 0);
$save_agent_filter = get_parameter('save_agent_filter', 0);
$update_agent_filter = get_parameter('update_agent_filter', 0);
$delete_agent_filter = get_parameter('delete_agent_filter', 0);
$check_unique_ip = (bool) get_parameter('check_unique_ip', 0);
if (https_is_running()) {
header('Content-type: application/json');
@ -1020,4 +1021,29 @@ $(document).ready(function() {
return;
}
if ($check_unique_ip === true) {
$direccion_agente = (string) get_parameter_post('direccion', '');
$ip_all = get_parameter_post('ip_all', '');
if (empty($direccion_agente) === true) {
echo json_encode(['success' => false, 'message' => __('Please enter an IP address.')]);
return;
}
$sql = 'SELECT direccion FROM tagente WHERE direccion = "'.$direccion_agente.'"';
$exists_ip = db_get_row_sql($sql);
if ($exists_ip !== false) {
if (is_array($ip_all) === true && in_array($direccion_agente, $ip_all) === true) {
echo json_encode(['success' => true, 'message' => __('Success! but this IP is already in the list.')]);
} else {
echo json_encode(['success' => false, 'message' => __('This IP is already being used'), 'exist_ip' => true]);
}
} else {
echo json_encode(['success' => true, 'message' => __('Success! this IP is available to be used.')]);
}
return;
}
return;

View File

@ -481,6 +481,11 @@ select:-internal-list-box {
max-width: 120px;
}
.w130px {
width: 130px;
max-width: 130px;
}
.w200px {
width: 200px;
max-width: 200px;
@ -598,6 +603,10 @@ select:-internal-list-box {
min-width: 120px;
}
.mw130px {
min-width: 130px;
}
.mw180px {
min-width: 180px;
}