/** * Manage a hub (many mac on a port mean you have a hub) * * @param $pfNetworkport object Informations of the network port (switch port) * * @return bool * **/ function hubNetwork($pfNetworkport, $a_mac) { $nn = new NetworkPort_NetworkPort(); $Netport = new NetworkPort(); // Get port connected on switch port $hub_id = 0; $ID = $nn->getOppositeContact($pfNetworkport->fields['networkports_id']); if ($ID) { $Netport->getFromDB($ID); if ($Netport->fields["itemtype"] == $this->getType()) { $this->getFromDB($Netport->fields["items_id"]); if ($this->fields["hub"] == "1") { // It's a hub connected, so will update connections //$this->releaseHub($this->fields['id'], $p_oPort); $hub_id = $this->fields['id']; } else { // It's a direct connection, so disconnect and create a hub $this->disconnectDB($ID); $hub_id = $this->createHub($pfNetworkport, $a_mac); } } else { // It's a direct connection, so disconnect and create a hub $this->disconnectDB($ID); $hub_id = $this->createHub($pfNetworkport, $a_mac); } } else { // No connections found and create a hub $hub_id = $this->createHub($pfNetworkport, $a_mac); } // State : Now we have hub and it's id // Add source port id in comment of hub $h_input = array(); $h_input['id'] = $hub_id; $h_input['comment'] = "Port : " . $pfNetworkport->fields['networkports_id']; $this->update($h_input); // Get all ports connected to this hub $a_portglpi = array(); $a_ports = $Netport->find("`items_id`='" . $hub_id . "'\n AND `itemtype`='" . $this->getType() . "'"); foreach ($a_ports as $data) { $id = $nn->getOppositeContact($data['id']); if ($id) { $a_portglpi[$id] = $data['id']; } } foreach ($a_mac as $ifmac) { $a_ports = $Netport->find("`mac`='" . $ifmac . "'", "", 1); if (count($a_ports) == "1") { if (!$this->searchIfmacOnHub($a_ports, $a_portglpi)) { // Connect port (port found in GLPI) $this->connectPortToHub($a_ports, $hub_id); } } else { if (count($a_ports) == "0") { // Port don't exist // Create unmanaged device $input = array(); $a_manufacturer = array(0 => PluginFusioninventoryInventoryExternalDB::getManufacturerWithMAC($ifmac)); $a_manufacturer = PluginFusioninventoryFormatconvert::cleanArray($a_manufacturer); $input['name'] = $a_manufacturer[0]; if (isset($_SESSION["plugin_fusioninventory_entity"])) { $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"]; } $unmanageds_id = $this->add($input); $input = array(); $input["items_id"] = $unmanageds_id; $input["itemtype"] = $this->getType(); $input["mac"] = $ifmac; $input['instantiation_type'] = "NetworkPortEthernet"; $id_port = $Netport->add($input); $Netport->getFromDB($id_port); $this->connectPortToHub(array($id_port => $Netport->fields), $hub_id); } } } }
function importConnectionMac($a_portconnection, $networkports_id) { $wire = new NetworkPort_NetworkPort(); $networkPort = new NetworkPort(); $pfNetworkPort = new PluginFusioninventoryNetworkPort(); $pfUnmanaged = new PluginFusioninventoryUnmanaged(); $a_snmpports = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", "", 1)); $pfNetworkPort->getFromDB($a_snmpports['id']); $count = count($a_portconnection); $pfNetworkPort->loadNetworkport($networkports_id); if ($pfNetworkPort->getValue('trunk') != '1') { if ($count == '2') { // detect if phone IP is one of the 2 devices $phonecase = 0; $macNotPhone_id = 0; $macNotPhone = ''; $phonePort_id = 0; foreach ($a_portconnection as $ifmac) { $a_ports = $networkPort->find("`mac`='" . $ifmac . "'", "", 1); $a_port = current($a_ports); if ($a_port['itemtype'] == 'Phone') { // Connect phone on switch port and other (computer..) in this phone $phonePort_id = $a_port['id']; $phonecase++; } else { $macNotPhone_id = $a_port['id']; $macNotPhone = $ifmac; } } if ($phonecase == '1') { $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $phonePort_id)); $networkPort->getFromDB($phonePort_id); $Phone = new Phone(); $Phone->getFromDB($networkPort->fields['items_id']); $a_portsPhone = $networkPort->find("`items_id`='" . $networkPort->fields['items_id'] . "'\n AND `itemtype`='Phone'\n AND `name`='Link'", '', 1); $portLink_id = 0; if (count($a_portsPhone) == '1') { $a_portPhone = current($a_portsPhone); $portLink_id = $a_portPhone['id']; } else { // Create Port Link $input = array(); $input['name'] = 'Link'; $input['itemtype'] = 'Phone'; $input['items_id'] = $Phone->fields['id']; $input['entities_id'] = $Phone->fields['entities_id']; $portLink_id = $networkPort->add($input); } $opposite_id = FALSE; if ($opposite_id == $wire->getOppositeContact($portLink_id)) { if ($opposite_id != $macNotPhone_id) { $pfNetworkPort->disconnectDB($portLink_id); // disconnect this port $pfNetworkPort->disconnectDB($macNotPhone_id); // disconnect destination port } } if (!isset($macNotPhone_id)) { // Create unmanaged ports $unmanagedn_infos = array(); $unmanagedn_infos["name"] = ''; if (isset($_SESSION["plugin_fusioninventory_entity"])) { $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"]; } $newID = $pfUnmanaged->add($unmanagedn_infos); // Add networking_port $port_add = array(); $port_add["items_id"] = $newID; $port_add["itemtype"] = 'PluginFusioninventoryUnmanaged'; $port_add['mac'] = $macNotPhone; $port_add['instantiation_type'] = "NetworkPortEthernet"; $macNotPhone_id = $networkPort->add($port_add); } $wire->add(array('networkports_id_1' => $portLink_id, 'networkports_id_2' => $macNotPhone_id)); } else { $pfUnmanaged->hubNetwork($pfNetworkPort, $a_portconnection); } } else { if ($count > 1) { // MultipleMac $pfUnmanaged->hubNetwork($pfNetworkPort, $a_portconnection); } else { // One mac on port foreach ($a_portconnection as $ifmac) { //Only 1 time $a_ports = $networkPort->find("`mac`='" . $ifmac . "' AND `logical_number`='1'", "", 1); if (count($a_ports) == 0) { $a_ports = $networkPort->find("`mac`='" . $ifmac . "'", "", 1); } if (count($a_ports) > 0) { $a_port = current($a_ports); $hub = 0; $id = $networkPort->getContact($a_port['id']); if ($id and $networkPort->getFromDB($id)) { if ($networkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') { $pfUnmanaged->getFromDB($networkPort->fields['items_id']); if ($pfUnmanaged->fields['hub'] == '1') { $hub = 1; } } } $direct_id = $networkPort->getContact($networkports_id); if ($id and $id != $networkports_id and $hub == '0') { $directconnect = 0; if (!$direct_id) { $directconnect = 1; } else { $networkPort->getFromDB($direct_id); if ($networkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') { // 1. Hub connected to this switch port $pfUnmanaged->connectPortToHub(array($a_port), $networkPort->fields['items_id']); } else { // 2. direct connection $directconnect = 1; } } if ($directconnect == '1') { $pfNetworkPort->disconnectDB($networkports_id); // disconnect this port $pfNetworkPort->disconnectDB($a_port['id']); // disconnect destination port $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id'])); } } else { if ($id and $hub == '1') { $directconnect = 0; if (!$direct_id) { $directconnect = 1; } else { $networkPort->getFromDB($direct_id); $ddirect = $networkPort->fields; $networkPort->getFromDB($id); if ($ddirect['items_id'] == $networkPort->fields['items_id'] and $ddirect['itemtype'] == $networkPort->fields['itemtype']) { // 1.The hub where this device is connected is yet connected // to this switch port // => Do nothing } else { // 2. The hub where this device is connected to is not connected // to this switch port if ($ddirect['itemtype'] == 'PluginFusioninventoryUnmanaged') { // b. We have a hub connected to the switch port $pfUnmanaged->connectPortToHub(array($a_port), $ddirect['items_id']); } else { // a. We have a direct connexion to another device // (on the switch port) $directconnect = 1; } } } if ($directconnect == '1') { $pfNetworkPort->disconnectDB($networkports_id); // disconnect this port $pfNetworkPort->disconnectDB($a_port['id']); // disconnect destination port $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id'])); } } else { if ($id) { // Yet connected } else { // Not connected $pfNetworkPort->disconnectDB($networkports_id); // disconnect this port $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $a_port['id'])); } } } } else { // Create unmanaged device $pfUnmanaged = new PluginFusioninventoryUnmanaged(); $input = array(); $manufacturer = PluginFusioninventoryInventoryExternalDB::getManufacturerWithMAC($ifmac); $input['name'] = $manufacturer; if (isset($_SESSION["plugin_fusioninventory_entity"])) { $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"]; } $newID = $pfUnmanaged->add($input); $input['itemtype'] = "PluginFusioninventoryUnmanaged"; $input['items_id'] = $newID; $input['mac'] = $ifmac; $input['instantiation_type'] = "NetworkPortEthernet"; $newPortID = $networkPort->add($input); $pfNetworkPort->disconnectDB($networkports_id); // disconnect this port $wire->add(array('networkports_id_1' => $networkports_id, 'networkports_id_2' => $newPortID)); } } } } } }