From fcab1ae6128517026bd016a9302fa5ccbb8a261e Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 11 Mar 2019 15:09:31 +0100 Subject: [PATCH] WIP NetworkMap class Former-commit-id: da5ce8681313cdf5c5a7ec7a005c3ebd273fea43 --- .../include/class/NetworkMap.class.php | 899 +++++++++++++++++- 1 file changed, 876 insertions(+), 23 deletions(-) diff --git a/pandora_console/include/class/NetworkMap.class.php b/pandora_console/include/class/NetworkMap.class.php index a626fca23c..ead73b1254 100644 --- a/pandora_console/include/class/NetworkMap.class.php +++ b/pandora_console/include/class/NetworkMap.class.php @@ -46,6 +46,34 @@ class NetworkMap */ public $idMap; + /** + * Content of tmap. + * + * @var array + */ + public $map; + + /** + * Data origin, network. + * + * @var string + */ + public $network; + + /** + * Data origin, group id. + * + * @var integer + */ + public $idGroup; + + /** + * Data origin, Discovery task. + * + * @var integer + */ + public $idTask; + /** * Graph definition * @@ -87,23 +115,22 @@ class NetworkMap /** * Base constructor. * - * @param array $options Could define: - * id_map => target map to be painted. - * graph => target graph (already built) + * @param mixed $options Could define in array as: + * id_map => target discovery task id. + * id_group => target group. + * network => target CIDR. + * graph => target graph (already built). * nodes => target agents or nodes. * relations => target array of relationships. - * mode => simple (0) or advanced (1) - * map_options => ? + * mode => simple (0) or advanced (1). + * map_options => Map options. * * @return object New networkmap manager. */ - public function __construct($options=[]) + public function __construct($options=false) { + $recreate = true; if (is_array($options)) { - if (isset($options['id_map'])) { - $this->idMap = $options['id_map']; - } - if (isset($options['graph'])) { $this->graph = $options['graph']; } @@ -123,6 +150,33 @@ class NetworkMap if (isset($options['map_options'])) { $this->mapOptions = $options['map_options']; } + + // Load from Discovery task. + if ($options['id_map']) { + $this->idMap = $options['id_map']; + // Update nodes and relations. + $this->loadMap(); + + if (empty($this->nodes) + || empty($this->relations) + ) { + $this->createMap(); + } + } else { + if ($options['id_group']) { + $this->idGroup = $options['id_group']; + } + + if ($options['id_task']) { + $this->idTask = $options['id_task']; + } + + if ($options['network']) { + $this->network = $options['network']; + } + + $this->createMap(); + } } return $this; @@ -130,6 +184,814 @@ class NetworkMap } + /** + * Creates a new map based on a target. + * + * Target is specified from constructor arguments. + * options: + * - id_task => create a map from task. + * - id_group => create a map from group. + * - network => create a map from network. + * + * @return void + */ + public function createMap() + { + if ($this->idMap) { + $this->loadMap(); + + return; + } + + if ($this->network) { + $this->nodes = networkmap_get_new_nodes_from_ip_mask( + $this->network + ); + } + } + + + /** + * Loads a map from a target map ID. + * + * @return void. + */ + public function loadMap() + { + if ($this->idMap) { + $this->map = db_get_row('tmap', 'id', $this->idMap); + + // Retrieve or update nodes and relations. + $this->getNodes(); + $this->getRelations(); + + // Nodes and relations. + $this->graph = networkmap_process_networkmap($this->idMap); + } + } + + + /** + * Return nodes of current map. + * + * @return array Nodes. + */ + public function getNodes() + { + if ($this->nodes) { + return $this->nodes; + } + + if ($this->idMap !== false) { + if (enterprise_installed()) { + // Enterprise environment: LOAD. + $this->nodes = enterprise_hook( + 'get_nodes_from_db', + [$this->idMap] + ); + } + } + + return $this->nodes; + + } + + + /** + * Return relations of current map. + * + * @return array Relations. + */ + public function getRelations() + { + if ($this->relations) { + return $this->relations; + } + + if ($this->idMap !== false) { + if (enterprise_installed()) { + $this->relations = enterprise_hook( + 'get_relations_from_db', + [$this->idMap] + ); + } + } + + return $this->relations; + + } + + + /** + * Transform node information into JS data. + * + * @return string HTML code with JS data. + */ + public function loadMapData() + { + $networkmap = $this->map; + $networkmap['filter'] = json_decode( + $networkmap['filter'], + true + ); + + // Hardcoded. + $networkmap['filter']['holding_area'] = [ + 500, + 500, + ]; + /* + $this->graph['relations'] = clean_duplicate_links( + $this->graph['relations'] + ); + */ + $output .= ''; + + return $output; + } + + + /** + * Show an advanced interface to manage dialogs. + * + * @return string HTML code with dialogs. + */ + public function loadAdvanceInterface() + { + $list_networkmaps = get_networkmaps($this->idMap); + if (empty($list_networkmaps)) { + $list_networkmaps = []; + } + + $output .= ''; + + $output .= ''; + + $output .= ''; + + $output .= ''; + + return $output; + } + + + /** + * Loads advanced map controller (JS). + * + * @return string HTML code for advanced controller. + */ + public function loadController() + { + $output = ''; + + // Generate JS for advanced controller. + $output .= ' + +'; + + if ($return === false) { + echo $output; + } + + return $output; + + } + + + /** + * Load networkmap HTML skel and JS requires. + * + * @return string HTML code for skel. + */ + public function loadMapSkel() + { + global $config; + + ui_require_css_file('networkmap'); + ui_require_css_file('jquery.contextMenu', 'include/styles/js/'); + + $output = ''; + $hide_minimap = ''; + if ($dashboard_mode) { + $hide_minimap = 'none'; + } + + $networkmap = $this->map; + $networkmap['filter'] = json_decode($networkmap['filter'], true); + + $networkmap['filter']['l2_network_interfaces'] = 1; + + $output .= ''; + $output .= ''; + $output .= ''; + + // Open networkconsole_id div. + $output .= '
'; + $output .= ''; + $output .= ''; + $output .= ''; + $output .= '
'; + + $output .= '
idMap)) { - $graph = networkmap_process_networkmap($this->idMap); - - ob_start(); - - ui_require_css_file('networkmap'); - show_networkmap( - $this->idMap, - $user_readonly, - $graph, - get_parameter('pure', 0) - ); - - $output = ob_get_clean(); + $output .= $this->loadMapSkel(); + $output .= $this->loadMapData(); + $output .= $this->loadController(); + $output .= $this->loadAdvanceInterface(); } else if (isset($this->graph)) { // Build graph based on direct graph definition. }