$value) { $agent_alias = agents_get_alias($key); $agents_interfaces[$key]['agent_alias'] = $agent_alias; } echo json_encode($agents_interfaces); return; } if ($get_agents_group) { $id_group = (int) get_parameter('id_group', -1); $mode = (string) get_parameter('mode', 'json'); $id_server = (int) get_parameter('id_server', 0); $serialized = (bool) get_parameter('serialized'); $return = []; if ($id_group != -1) { $filter = []; if (is_metaconsole() && !empty($id_server)) { $filter['id_server'] = $id_server; } $return = agents_get_group_agents($id_group, $filter, 'none', false, false, $serialized); } switch ($mode) { case 'json': default: echo json_encode($return); break; } return; } if ($search_agents && (!is_metaconsole() || $force_local)) { $id_agent = (int) get_parameter('id_agent'); $string = (string) get_parameter('q'); $string = strtoupper($string); // q is what autocomplete plugin gives $id_group = (int) get_parameter('id_group', -1); $addedItems = html_entity_decode((string) get_parameter('add')); $addedItems = json_decode($addedItems); $all = (string) get_parameter('all', 'all'); $delete_offspring_agents = (int) get_parameter('delete_offspring_agents', 0); if ($addedItems != null) { foreach ($addedItems as $item) { echo $item."|\n"; } } $filter = []; if ($id_group != -1) { if ($id_group == 0) { $user_groups = users_get_groups($config['id_user'], 'AR', true); $filter['id_grupo'] = array_keys($user_groups); } else { $filter['id_grupo'] = $id_group; } } if ($all === 'enabled') { $filter['disabled'] = 1; } else { $filter['disabled'] = 0; } $data = []; // Get agents for only the alias. $filter_alias = $filter; $filter_alias[] = '(UPPER(alias) LIKE "%'.$string.'%")'; $agents = agents_get_agents($filter_alias, ['id_agente', 'nombre', 'direccion', 'alias']); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']), 'ip' => io_safe_output($agent['direccion']), 'filter' => 'alias', ]; } } // Get agents for only the name. $filter_agents = $filter; $filter_agents[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) LIKE "%'.$string.'%")'; $agents = agents_get_agents($filter_agents, ['id_agente', 'nombre', 'direccion', 'alias']); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']), 'ip' => io_safe_output($agent['direccion']), 'filter' => 'agent', ]; } } // Get agents for only the address. $filter_address = $filter; $filter_address[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) LIKE "%'.$string.'%")'; $agents = agents_get_agents($filter_address, ['id_agente', 'nombre', 'direccion', 'alias']); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']), 'ip' => io_safe_output($agent['direccion']), 'filter' => 'address', ]; } } // Get agents for only the description. $filter_description = $filter; $filter_description[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) NOT LIKE "%'.$string.'%" AND UPPER(comentarios) LIKE "%'.$string.'%")'; $agents = agents_get_agents($filter_description, ['id_agente', 'nombre', 'direccion', 'alias']); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']), 'ip' => io_safe_output($agent['direccion']), 'filter' => 'description', ]; } } if (empty($data) === false && $delete_offspring_agents !== 0) { // Gets offspring and deletes them, including himself. $agents_offspring = agents_get_offspring($delete_offspring_agents); if (empty($agents_offspring) === false) { foreach ($data as $key => $value) { if (isset($agents_offspring[$value['id']]) === true) { unset($data[$key]); } } } } echo json_encode($data); return; } else if ($search_agents && is_metaconsole()) { $id_agent = (int) get_parameter('id_agent'); $string = (string) get_parameter('q'); // Q is what autocomplete plugin gives. $id_group = (int) get_parameter('id_group', -1); $addedItems = html_entity_decode((string) get_parameter('add')); $addedItems = json_decode($addedItems); $all = (string) get_parameter('all', 'all'); if ($addedItems != null) { foreach ($addedItems as $item) { echo $item."|\n"; } } $data = []; $fields = [ 'id_tagente AS id_agente', 'nombre', 'alias', 'direccion', 'id_tmetaconsole_setup AS id_server', 'server_name', ]; $filter = []; if ($id_group != -1) { if ($id_group == 0) { $user_groups = users_get_groups($config['id_user'], 'AR', true); $filter['id_grupo'] = array_keys($user_groups); } else { $filter['id_grupo'] = $id_group; } } switch ($all) { case 'enabled': $filter['disabled'] = 0; break; default: // Not possible. break; } if (empty($id_agent) === false) { $filter['id_agente'] = $id_agent; } if (empty($string) === false) { // Get agents for only the alias. $filter_alias = $filter; $filter_alias[] = '(alias LIKE "%'.$string.'%")'; $agents = db_get_all_rows_filter( 'tmetaconsole_agent', $filter_alias, $fields ); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']).' ('.io_safe_output($agent['server_name']).')', 'ip' => io_safe_output($agent['direccion']), 'id_server' => $agent['id_server'], 'filter' => 'alias', ]; } } // Get agents for only the name. $filter_agents = $filter; $filter_agents[] = '(alias NOT LIKE "%'.$string.'%" AND nombre LIKE "%'.$string.'%")'; $agents = db_get_all_rows_filter( 'tmetaconsole_agent', $filter_agents, $fields ); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']).' ('.io_safe_output($agent['server_name']).')', 'ip' => io_safe_output($agent['direccion']), 'id_server' => $agent['id_server'], 'filter' => 'agent', ]; } } // Get agents for only the address. $filter_address = $filter; $filter_address[] = '(alias NOT LIKE "%'.$string.'%" AND nombre NOT LIKE "%'.$string.'%" AND direccion LIKE "%'.$string.'%")'; $agents = db_get_all_rows_filter( 'tmetaconsole_agent', $filter_address, $fields ); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']).' ('.io_safe_output($agent['server_name']).')', 'ip' => io_safe_output($agent['direccion']), 'id_server' => $agent['id_server'], 'filter' => 'address', ]; } } // Get agents for only the description. $filter_description = $filter; $filter_description[] = '(alias NOT LIKE "%'.$string.'%" AND nombre NOT LIKE "%'.$string.'%" AND direccion NOT LIKE "%'.$string.'%" AND comentarios LIKE "%'.$string.'%")'; $agents = db_get_all_rows_filter( 'tmetaconsole_agent', $filter_description, $fields ); if ($agents !== false) { foreach ($agents as $agent) { $data[] = [ 'id' => $agent['id_agente'], 'name' => io_safe_output($agent['nombre']), 'alias' => io_safe_output($agent['alias']).' ('.io_safe_output($agent['server_name']).')', 'ip' => io_safe_output($agent['direccion']), 'id_server' => $agent['id_server'], 'filter' => 'description', ]; } } } echo json_encode($data); return; } // Saves an event filter. if ($save_agent_filter) { $values = []; $values['id_name'] = get_parameter('id_name'); $values['group_id'] = get_parameter('group_id'); $values['recursion'] = get_parameter('recursion'); $values['status'] = get_parameter('status'); $values['search'] = get_parameter('search'); $values['id_os'] = get_parameter('id_os'); $values['policies'] = json_encode(get_parameter('policies')); $values['search_custom'] = get_parameter('search_custom'); $values['ag_custom_fields'] = get_parameter('ag_custom_fields'); $values['id_group_filter'] = get_parameter('id_group_filter'); $exists = (bool) db_get_value_filter( 'id_filter', 'tagent_filter', $values ); if ($exists === true) { echo 'duplicate'; } else { $result = db_process_sql_insert('tagent_filter', $values); if ($result === false) { echo 'error'; } else { echo $result; } } } if ($update_agent_filter) { $values = []; $id = get_parameter('id'); $values['group_id'] = get_parameter('group_id'); $values['recursion'] = get_parameter('recursion'); $values['status'] = get_parameter('status'); $values['search'] = get_parameter('search'); $values['id_os'] = get_parameter('id_os'); $values['policies'] = json_encode(get_parameter('policies')); $values['search_custom'] = get_parameter('search_custom'); $values['ag_custom_fields'] = get_parameter('ag_custom_fields'); $result = db_process_sql_update( 'tagent_filter', $values, ['id_filter' => $id] ); if ($result === false) { echo 'error'; } else { echo 'ok'; } } if ($delete_agent_filter) { $id = get_parameter('id'); $user_groups = users_get_groups( $config['id_user'], 'AW', users_can_manage_group_all('AW'), true ); $sql = 'DELETE FROM tagent_filter WHERE id_filter = '.$id.' AND id_group_filter IN ('.implode(',', array_keys($user_groups)).')'; $agent_filters = db_process_sql($sql); if ($agent_filters === false) { echo 'error'; } else { echo 'ok'; } } if ($get_agent_filters) { $user_groups = users_get_groups( $config['id_user'], 'AR', users_can_manage_group_all('AR'), true ); $sql = 'SELECT id_filter, id_name FROM tagent_filter WHERE id_group_filter IN ('.implode(',', array_keys($user_groups)).')'; $agent_filters = db_get_all_rows_sql($sql); $result = []; if ($agent_filters !== false) { foreach ($agent_filters as $agent_filter) { $result[$agent_filter['id_filter']] = $agent_filter['id_name']; } } echo io_json_mb_encode($result); } if ((int) $load_filter_modal === 1) { $user_groups = users_get_groups( $config['id_user'], 'AR', users_can_manage_group_all('AR'), true ); $sql = 'SELECT id_filter, id_name FROM tagent_filter WHERE id_group_filter IN ('.implode(',', array_keys($user_groups)).')'; $agent_filters = db_get_all_rows_sql($sql); $filters = []; foreach ($agent_filters as $agent_filter) { $filters[$agent_filter['id_filter']] = $agent_filter['id_name']; } echo '