From 1b74ca3bef4c0c6ea403555e1738748e43918ffa Mon Sep 17 00:00:00 2001 From: Esteban Sanchez Date: Tue, 17 Feb 2009 12:17:10 +0000 Subject: [PATCH] 2009-02-17 Esteban Sanchez * godmode/agentes/module_manager_editor.php: Added get_module_components operation to AJAX interface. Avoid error reporting on snmprealwalk so the JSON doesn't fail. Added support for component group selection. Do nothing if the selected component is zero. * godmode/agentes/module_manager_editor_common.php: Added add_component_selection() which makes the task of adding a module component to the editor easier and common. It also now filters based on component groups first. * godmode/agentes/module_manager_editor_network.php, godmode/agentes/module_manager_editor_wmi.php: Use add_component_selection() instead of duping the code. * include/styles/pandora.css: Added style to component elements. * include/functions_db.php: Avoid numeric field names on format_array_to_where_clause_sql(). * include/functions_modules.php: Added get_network_components() and get_network_component_groups(). git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1462 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 25 ++++++ .../godmode/agentes/module_manager_editor.php | 54 ++++++++++- .../agentes/module_manager_editor_common.php | 35 ++++++++ .../agentes/module_manager_editor_network.php | 18 +--- .../agentes/module_manager_editor_wmi.php | 16 +--- pandora_console/include/functions_db.php | 4 + pandora_console/include/functions_modules.php | 90 +++++++++++++++++++ pandora_console/include/styles/pandora.css | 13 ++- 8 files changed, 224 insertions(+), 31 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 5daf94c6d1..f948887717 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,28 @@ +2009-02-17 Esteban Sanchez + + * godmode/agentes/module_manager_editor.php: Added + get_module_components operation to AJAX interface. Avoid error + reporting on snmprealwalk so the JSON doesn't fail. Added support for + component group selection. Do nothing if the selected component is + zero. + + * godmode/agentes/module_manager_editor_common.php: Added + add_component_selection() which makes the task of adding a module + component to the editor easier and common. It also now filters based + on component groups first. + + * godmode/agentes/module_manager_editor_network.php, + godmode/agentes/module_manager_editor_wmi.php: Use + add_component_selection() instead of duping the code. + + * include/styles/pandora.css: Added style to component elements. + + * include/functions_db.php: Avoid numeric field names on + format_array_to_where_clause_sql(). + + * include/functions_modules.php: Added get_network_components() and + get_network_component_groups(). + 2009-02-16 Raul Mateos * include/config_process.php: Update build after last changes. diff --git a/pandora_console/godmode/agentes/module_manager_editor.php b/pandora_console/godmode/agentes/module_manager_editor.php index 7e63601fcd..4675806f61 100644 --- a/pandora_console/godmode/agentes/module_manager_editor.php +++ b/pandora_console/godmode/agentes/module_manager_editor.php @@ -20,6 +20,7 @@ if (defined ('AJAX')) { $get_network_component = (bool) get_parameter ('get_network_component'); $snmp_walk = (bool) get_parameter ('snmp_walk'); $get_module_component = (bool) get_parameter ('get_module_component'); + $get_module_components = (bool) get_parameter ('get_module_components'); if ($get_module_component) { $id_component = (int) get_parameter ('id_module_component'); @@ -30,12 +31,24 @@ if (defined ('AJAX')) { return; } + if ($get_module_components) { + require_once ('include/functions_modules.php'); + $id_module_group = (int) get_parameter ('id_module_component_group'); + $id_module_component = (int) get_parameter ('id_module_component_type'); + + $components = get_network_components ($id_module_component, + array ('id_module_group' => $id_module_group)); + + echo json_encode ($components); + return; + } + if ($snmp_walk) { $ip_target = (string) get_parameter ('ip_target'); $snmp_community = (string) get_parameter ('snmp_community'); snmp_set_quick_print (1); - $snmpwalk = snmprealwalk ($ip_target, $snmp_community, NULL); + $snmpwalk = @snmprealwalk ($ip_target, $snmp_community, NULL); if ($snmpwalk === false) { echo json_encode ($snmpwalk); return; @@ -232,7 +245,44 @@ $(document).ready (function () { $("#text-tcp_port").removeAttr ("disabled"); } }); + + $("#network_component_group").change (function () { + var $select = $("#network_component").hide (); + $("#component").hide (); + if (this.value == 0) + return; + $("#component_loading").show (); + $(".error, #no_component").hide (); + $("option[value!=0]", $select).remove (); + jQuery.post ("ajax.php", + {"page" : "godmode/agentes/module_manager_editor", + "get_module_components" : 1, + "id_module_component_group" : this.value, + "id_module_component_type" : $("#hidden-id_module_component_type").attr ("value") + }, + function (data, status) { + if (data == false) { + $("#component_loading").hide (); + $("span#no_component").show (); + return; + } + jQuery.each (data, function (i, val) { + option = $("") + .attr ("value", val['id_nc']) + .append (val['name']); + $select.append (option); + }); + $("#component_loading").hide (); + $select.show (); + $("#component").show (); + }, + "json" + ); + }); + $("#network_component").change (function () { + if (this.value == 0) + return; $("#component_loading").show (); $(".error").hide (); jQuery.post ("ajax.php", @@ -298,7 +348,7 @@ $(document).ready (function () { }, function (data, status) { if (data == false) { - $("span.error").show (); + $("span#no_snmp").show (); $("#oid_loading").hide (); $("#edit_oid").hide (); return false; diff --git a/pandora_console/godmode/agentes/module_manager_editor_common.php b/pandora_console/godmode/agentes/module_manager_editor_common.php index 5f34c25bbd..e8498e19b4 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_common.php +++ b/pandora_console/godmode/agentes/module_manager_editor_common.php @@ -64,6 +64,41 @@ function push_table_advanced ($row, $id = false) { $table_advanced->data = array_merge ($table_advanced->data, $data); } +function add_component_selection ($id_network_component_type) { + global $table_simple; + + $data = array (); + $data[0] = __('Using module component').' '; + $data[0] .= pandora_help ('network_component', true); + + $component_groups = get_network_component_groups ($id_network_component_type); + $data[1] = ''; + $data[1] .= __('Group').'
'; + $data[1] .= print_select ($component_groups, + 'network_component_group', '', '', '--'.__('Manual setup').'--', 0, + true, false, false); + $data[1] .= '
'; + $data[1] .= print_input_hidden ('id_module_component_type', $id_network_component_type, true); + $data[1] .= ''; + $data[1] .= ''; + $data[1] .= ' '; + + $table_simple->colspan['module_component'][1] = 3; + $table_simple->rowstyle['module_component'] = 'background-color: #D4DDC6'; + + prepend_table_simple ($data, 'module_component'); +} + +require_once ('include/functions_modules.php'); + $update_module_id = (int) get_parameter_get ('update_module'); $table_simple->id = 'simple'; diff --git a/pandora_console/godmode/agentes/module_manager_editor_network.php b/pandora_console/godmode/agentes/module_manager_editor_network.php index 7064ef783a..38a157fa33 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_network.php +++ b/pandora_console/godmode/agentes/module_manager_editor_network.php @@ -25,24 +25,14 @@ if (!isset ($id_agente)) { $extra_title = __('Network server module'); -$data = array (); -$data[0] = __('Using module component').' '; -$data[0] .= pandora_help ('network_component', true); +define ('ID_NETWORK_COMPONENT_TYPE', 2); if (empty ($update_module_id)) { - $data[1] = print_select_from_sql ('SELECT id_nc, name FROM tnetwork_component WHERE id_modulo = 2', - 'network_component', '', '', '---'.__('Manual setup').'---', 0, true); - $data[1] .= ' '; + /* Function in module_manager_editor_common.php */ + add_component_selection (ID_NETWORK_COMPONENT_TYPE); } else { /* TODO: Print network component if available */ - $data[1] = 'TODO'; } -$table_simple->colspan['module_component'][1] = 3; -$table_simple->rowstyle['module_component'] = 'background-color: #D4DDC6'; - -prepend_table_simple ($data, 'module_component'); $data = array (); $data[0] = __('Target IP'); @@ -79,7 +69,7 @@ $data[1] .= ''; -$data[1] .= ''; +$data[1] .= ''; $data[1] .= ' '; diff --git a/pandora_console/godmode/agentes/module_manager_editor_wmi.php b/pandora_console/godmode/agentes/module_manager_editor_wmi.php index 48ae945170..4d78cb0139 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_wmi.php +++ b/pandora_console/godmode/agentes/module_manager_editor_wmi.php @@ -23,24 +23,14 @@ if (!isset ($id_agente)) { $extra_title = __('WMI server module'); -$data = array (); -$data[0] = __('Using module component').' '; -$data[0] .= pandora_help ('network_component', true); +define ('ID_NETWORK_COMPONENT_TYPE', 6); if (empty ($update_module_id)) { - $data[1] = print_select_from_sql ('SELECT id_nc, name FROM tnetwork_component WHERE id_modulo = 6', - 'network_component', '', '', '---'.__('Manual setup').'---', 0, true); - $data[1] .= ' '; + /* Function in module_manager_editor_common.php */ + add_component_selection (ID_NETWORK_COMPONENT_TYPE); } else { /* TODO: Print network component if available */ - $data[1] = 'TODO'; } -$table_simple->colspan['module_component'][1] = 3; -$table_simple->rowstyle['module_component'] = 'background-color: #D4DDC6'; - -prepend_table_simple ($data, 'module_component'); $data = array (); $data[0] = __('Target IP'); diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index e9d0b846f9..93afcee3f6 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1712,6 +1712,10 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') { $i = 1; $max = count ($values); foreach ($values as $field => $value) { + if (is_numeric ($field)) + /* Avoid numeric field names */ + continue; + if ($field[0] != "`") { $field = "`".$field."`"; } diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index d095b7663b..f03ca5a806 100644 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -72,4 +72,94 @@ function copy_agent_module_to_agent ($id_agent_module, $id_destiny_agent) { return $id_new_module; } +/** + * Get a list of network components. + * + * @param int Module type id of the requested components. + * @param mixed Aditional filters to the components. It can be an indexed array + * (keys would be the field name and value the expected value, and would be + * joined with an AND operator). Examples: + +$components = get_network_components ($id_module, array ('id_module_group', 10)); +$components = get_network_components ($id_module, array ('id_module_group', 10)); + + * + * @return array A list of network components matching. Empty array is returned + * if none matches. + */ +function get_network_components ($id_module, $filter = false) { + if (empty ($id_module)) + return array (); + + $where = ''; + if (is_array ($filter)) { + $where = ' AND '; + $where .= format_array_to_where_clause_sql ($filter); + } + + $sql = sprintf ('SELECT * FROM tnetwork_component + WHERE `id_modulo` = %d %s', + $id_module, $where); + + $components = get_db_all_rows_sql ($sql); + if ($components === false) + return array (); + return $components; +} + +/** + * Get a list of network component groups. + * + * The values returned can be passed directly to print_select(). Child groups + * are indented, so ordering on print_select() is NOT recommendable. + * + * @param int If provided, groups must have at least one compoent of the module + * provided. Parents will be included in that case even if they don't have + * components directly. + * + * @return array An ordered list of component groups with childs indented. + */ +function get_network_component_groups ($id_module_components = 0) { + /* Special vars to keep track of indentation level */ + static $level = 0; + static $id_parent = 0; + + $groups = get_db_all_rows_field_filter ('tnetwork_component_group', + 'parent', $id_parent); + if ($groups === false) + return array (); + + $retval = array (); + /* Magic indentation is here */ + $prefix = str_repeat (' ', $level * 3); + foreach ($groups as $group) { + $level++; + $tmp = $id_parent; + $id_parent = $group['id_sg']; + $childs = get_network_component_groups ($id_module_components); + $id_parent = $tmp; + $level--; + + if (! empty ($childs) || $id_module_components == 0) { + $retval[$group['id_sg']] = $prefix.$group['name']; + $retval = $retval + $childs; + } else { + /* If components id module is provided, only groups with components + that belongs to this id module are returned */ + if ($id_module_components) { + $sql = sprintf ('SELECT COUNT(*) + FROM tnetwork_component + WHERE id_module_group = %d + AND id_modulo = %d', + $group['id_sg'], $id_module_components); + $count = get_db_sql ($sql); + if ($count > 0) + $retval[$group['id_sg']] = $prefix.$group['name']; + } + } + } + + return $retval; +} + ?> diff --git a/pandora_console/include/styles/pandora.css b/pandora_console/include/styles/pandora.css index 82968bcfac..b4173ea27a 100644 --- a/pandora_console/include/styles/pandora.css +++ b/pandora_console/include/styles/pandora.css @@ -764,6 +764,15 @@ table#filter_compound_table select, table#filter_compound_table #text-search { width: 100%; } +table#simple select#network_component_group, +table#simple select#network_component { + width: 90%; +} +table#simple span#component_group, +table#simple span#component { + width: 45%; + font-style: italic; +} .clickable { cursor: pointer; } @@ -845,8 +854,8 @@ ol.steps li.visited { } /* Common useful styles */ -.left { clear: left; } -.right { clear: right; } +.left { clear: left; float: left;} +.right { clear: right; float: right;} .clear { clear:both; } .top { vertical-align: top;