/**
  * @test
  */
 public function ImportNetworkEquipment()
 {
     global $DB;
     self::restore_database();
     // Load session rights
     $_SESSION['glpidefault_entity'] = 0;
     Session::initEntityProfiles(2);
     Session::changeProfile(4);
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     $networkEquipment = new NetworkEquipment();
     $networkPort = new NetworkPort();
     $networkName = new NetworkName();
     $iPAddress = new IPAddress();
     $input = array('name' => 'switch', 'entities_id' => 0, 'item_type' => 'NetworkEquipment', 'sysdescr' => 'Cisco machin chose', 'locations_id' => 1, 'is_dynamic' => 1, 'serial' => 'XXS6BEF3', 'comment' => 'this is a comment', 'plugin_fusioninventory_configsecurities_id' => 1);
     $unmanageds_id = $pfUnmanaged->add($input);
     // * Add networkport
     $input = array();
     $input['itemtype'] = 'PluginFusioninventoryUnmanaged';
     $input['items_id'] = $unmanageds_id;
     $input['instantiation_type'] = 'NetworkPortEthernet';
     $input['name'] = 'general';
     $input['mac'] = '00:00:00:43:ae:0f';
     $input['is_dynamic'] = 1;
     $networkports_id = $networkPort->add($input);
     $input = array();
     $input['items_id'] = $networkports_id;
     $input['itemtype'] = 'NetworkPort';
     $input['name'] = '';
     $input['is_dynamic'] = 1;
     $networknames_id = $networkName->add($input);
     $input = array();
     $input['entities_id'] = 0;
     $input['itemtype'] = 'NetworkName';
     $input['items_id'] = $networknames_id;
     $input['name'] = '192.168.20.1';
     $input['is_dynamic'] = 1;
     $iPAddress->add($input);
     $pfUnmanaged->import($unmanageds_id);
     $cnt = countElementsInTable("glpi_networkequipments");
     $this->assertEquals(1, $cnt, "May have network equipment added");
     $cnt = countElementsInTable("glpi_plugin_fusioninventory_unmanageds");
     $this->assertEquals(0, $cnt, "Unknown device may be deleted");
     $networkEquipment->getFromDB(1);
     $this->assertEquals('XXS6BEF3', $networkEquipment->fields['serial'], "Serial");
     $this->assertEquals('switch', $networkEquipment->fields['name'], "Name");
     $this->assertEquals(1, $networkEquipment->fields['is_dynamic'], "is_dynamic");
     $this->assertEquals(1, $networkEquipment->fields['locations_id'], "locations_id");
     $this->assertEquals('this is a comment', $networkEquipment->fields['comment'], "comment");
     $networkPort->getFromDB(1);
     $a_reference = array('name' => 'general', 'id' => '1', 'items_id' => '1', 'itemtype' => 'NetworkEquipment', 'entities_id' => '0', 'is_recursive' => '0', 'logical_number' => '0', 'instantiation_type' => 'NetworkPortEthernet', 'mac' => '00:00:00:43:ae:0f', 'comment' => '', 'is_deleted' => '0', 'is_dynamic' => '1');
     $this->assertEquals($a_reference, $networkPort->fields, "Networkport");
     $networkName->getFromDB(1);
     $a_reference = array('id' => '1', 'entities_id' => '0', 'items_id' => '1', 'itemtype' => 'NetworkPort', 'comment' => NULL, 'fqdns_id' => '0', 'is_deleted' => '0', 'is_dynamic' => '1', 'name' => '');
     $this->assertEquals($a_reference, $networkName->fields, "Networkname");
     $iPAddress->getFromDB(1);
     $a_reference = array('name' => '192.168.20.1', 'id' => '1', 'entities_id' => '0', 'items_id' => '1', 'itemtype' => 'NetworkName', 'version' => '4', 'binary_0' => '0', 'binary_1' => '0', 'binary_2' => '65535', 'binary_3' => '3232240641', 'is_deleted' => '0', 'is_dynamic' => '1', 'mainitems_id' => '1', 'mainitemtype' => 'NetworkEquipment');
     $this->assertEquals($a_reference, $iPAddress->fields, "IPAddress");
 }
 public function Cisco1Unmanaged()
 {
     global $DB;
     $DB->connect();
     self::restore_database();
     $a_lldp = array('ifdescr' => 'GigabitEthernet0/10', 'logical_number' => '', 'sysdescr' => '', 'model' => '', 'ip' => '192.168.200.124', 'mac' => '', 'name' => '');
     $pfINetworkEquipmentLib = new PluginFusioninventoryInventoryNetworkEquipmentLib();
     $networkEquipment = new NetworkEquipment();
     $networkport = new NetworkPort();
     $networkName = new NetworkName();
     $iPAddress = new IPAddress();
     $pfNetworkPort = new PluginFusioninventoryNetworkPort();
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     // Nortel switch
     $networkequipments_id = $networkEquipment->add(array('name' => 'cisco1', 'entities_id' => 0));
     $networkports_id = $networkport->add(array('itemtype' => 'NetworkEquipment', 'items_id' => $networkequipments_id, 'entities_id' => 0));
     // Unmanaged
     $unmanageds_id = $pfUnmanaged->add(array('name' => 'otherswitch', 'entities_id' => 0));
     $networkports_unknown_id = $networkport->add(array('itemtype' => 'PluginFusioninventoryUnmanaged', 'items_id' => $unmanageds_id, 'entities_id' => 0));
     $networknames_id = $networkName->add(array('entities_id' => 0, 'itemtype' => 'NetworkPort', 'items_id' => $networkports_unknown_id));
     $iPAddress->add(array('entities_id' => 0, 'itemtype' => 'NetworkName', 'items_id' => $networknames_id, 'name' => '192.168.200.124'));
     $pfINetworkEquipmentLib->importConnectionLLDP($a_lldp, $networkports_id);
     $a_portslinks = getAllDatasFromTable('glpi_networkports_networkports');
     $this->assertEquals(1, count($a_portslinks), 'May have 1 connection between 2 network ports');
     $a_networkports = getAllDatasFromTable('glpi_networkports');
     $this->assertEquals(3, count($a_networkports), 'May have 3 network ports (' . print_r($a_networkports, TRUE) . ')');
     $a_unkowns = getAllDatasFromTable('glpi_plugin_fusioninventory_unmanageds');
     $this->assertEquals(1, count($a_unkowns), 'May have only one unknown device (' . print_r($a_unkowns, TRUE) . ')');
     $a_networkport_ref = array('id' => '3', 'items_id' => $unmanageds_id, 'itemtype' => 'PluginFusioninventoryUnmanaged', 'entities_id' => '0', 'is_recursive' => '0', 'logical_number' => '0', 'name' => 'GigabitEthernet0/10', 'instantiation_type' => 'NetworkPortEthernet', 'mac' => NULL, 'comment' => NULL, 'is_deleted' => '0', 'is_dynamic' => '0');
     $networkport->getFromDB(3);
     $this->assertEquals($a_networkport_ref, $networkport->fields, 'New unknown port created');
     $a_ref = array('id' => 1, 'networkports_id_1' => $networkports_id, 'networkports_id_2' => 3);
     $this->assertEquals($a_ref, current($a_portslinks), 'Link port');
 }
 /**
  * Function used to detect if port has multiple mac connected
  */
 static function isPortHasMultipleMac($networkports_id)
 {
     $nw = new NetworkPort_NetworkPort();
     $networkPort = new NetworkPort();
     $is_multiple = FALSE;
     $opposite_port = $nw->getOppositeContact($networkports_id);
     if ($opposite_port != "" && $opposite_port != 0) {
         $networkPort->getFromDB($opposite_port);
         if ($networkPort->fields["itemtype"] == 'PluginFusioninventoryUnmanaged') {
             $pfUnmanaged = new PluginFusioninventoryUnmanaged();
             if ($pfUnmanaged->getFromDB($networkPort->fields['items_id'])) {
                 if ($pfUnmanaged->fields['hub'] == 1) {
                     $is_multiple = TRUE;
                 }
             }
         }
     }
     return $is_multiple;
 }
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     switch ($ma->getAction()) {
         case "import":
             $Import = 0;
             $NoImport = 0;
             $pfUnmanaged = new PluginFusioninventoryUnmanaged();
             foreach ($ids as $key) {
                 list($Import, $NoImport) = $pfUnmanaged->import($key, $Import, $NoImport);
                 $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
             }
             $ma->addMessage(__('Number of imported devices', 'fusioninventory') . " : " . $Import);
             $ma->addMessage(__('Number of devices not imported because type not defined', 'fusioninventory') . " : " . $NoImport);
             break;
     }
     return;
 }
 function getTypes()
 {
     global $CFG_GLPI;
     $types = array();
     foreach ($CFG_GLPI["state_types"] as $itemtype) {
         if (class_exists($itemtype)) {
             $item = new $itemtype();
             $types[$itemtype] = $item->getTypeName();
         }
     }
     $types["PluginFusioninventoryUnmanaged"] = PluginFusioninventoryUnmanaged::getTypeName();
     $types[""] = __('No itemtype defined', 'fusioninventory');
     return $types;
 }
 /**
  * After rules import device
  *
  * @param integer $items_id id of the device in GLPI DB (0 = created, other = merge)
  * @param varchar $itemtype itemtype of the device
  *
  * @return type
  */
 function rulepassed($items_id, $itemtype)
 {
     PluginFusioninventoryLogger::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n");
     PluginFusioninventoryLogger::logIfExtradebugAndDebugMode('fusioninventorycommunication', 'Function PluginFusinvsnmpCommunicationSNMPQuery->rulepassed().');
     $_SESSION["plugin_fusioninventory_entity"] = 0;
     PluginFusioninventoryConfig::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n");
     PluginFusioninventoryCommunication::addLog('Function PluginFusioninventoryCommunicationNetworkInventory->rulepassed().');
     $a_inventory = $_SESSION['SOURCE_XMLDEVICE'];
     $errors = '';
     $class = new $itemtype();
     if ($items_id == "0") {
         $input = array();
         $input['date_mod'] = date("Y-m-d H:i:s");
         if ($class->getFromDB($a_inventory[$a_inventory['itemtype']]['id'])) {
             $input['entities_id'] = $class->fields['entities_id'];
         } else {
             $input['entities_id'] = 0;
         }
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = "'" . $input['entities_id'] . "'";
         }
         $_SESSION["plugin_fusioninventory_entity"] = $input['entities_id'];
         $items_id = $class->add($input);
         if (isset($_SESSION['plugin_fusioninventory_rules_id'])) {
             $pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
             $inputrulelog = array();
             $inputrulelog['date'] = date('Y-m-d H:i:s');
             $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
             if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
                 $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
             }
             $inputrulelog['items_id'] = $items_id;
             $inputrulelog['itemtype'] = $itemtype;
             $inputrulelog['method'] = 'snmpinventory';
             $pfRulematchedlog->add($inputrulelog);
             $pfRulematchedlog->cleanOlddata($items_id, $itemtype);
             unset($_SESSION['plugin_fusioninventory_rules_id']);
         }
     }
     if ($itemtype == "PluginFusioninventoryUnmanaged") {
         $class->getFromDB($items_id);
         $input = array();
         $input['id'] = $class->fields['id'];
         if (!empty($a_inventory[$a_inventory['itemtype']]['name'])) {
             $input['name'] = $a_inventory[$a_inventory['itemtype']]['name'];
         }
         if (!empty($a_inventory[$a_inventory['itemtype']]['serial'])) {
             $input['serial'] = $a_inventory[$a_inventory['itemtype']]['serial'];
         }
         if (!empty($a_inventory['itemtype'])) {
             $input['itemtype'] = $a_inventory['itemtype'];
         }
         // TODO : add import ports
         PluginFusioninventoryToolbox::writeXML($items_id, serialize($_SESSION['SOURCE_XMLDEVICE']), 'PluginFusioninventoryUnmanaged');
         $class->update($input);
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==detail==] ==updatetheitem== Update ' . PluginFusioninventoryUnmanaged::getTypeName() . ' [[PluginFusioninventoryUnmanaged::' . $items_id . ']]';
         $this->addtaskjoblog();
     } else {
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==detail==] Update ' . $class->getTypeName() . ' [[' . $itemtype . '::' . $items_id . ']]';
         $this->addtaskjoblog();
         $errors .= $this->importDevice($itemtype, $items_id, $a_inventory);
     }
     return $errors;
 }
 function manageNetworkPort($inventory_networkports, $computers_id, $no_history)
 {
     global $DB;
     $networkPort = new NetworkPort();
     $networkName = new NetworkName();
     $iPAddress = new IPAddress();
     $iPNetwork = new IPNetwork();
     $networkPortEthernet = new NetworkPortEthernet();
     $item_DeviceNetworkCard = new Item_DeviceNetworkCard();
     foreach ($inventory_networkports as $a_networkport) {
         if ($a_networkport['mac'] != '') {
             $a_networkports = $networkPort->find("`mac`='" . $a_networkport['mac'] . "'\n               AND `itemtype`='PluginFusioninventoryUnmanaged'", "", 1);
             if (count($a_networkports) > 0) {
                 $input = current($a_networkports);
                 $unmanageds_id = $input['items_id'];
                 $input['logical_number'] = $a_networkport['logical_number'];
                 $input['itemtype'] = 'Computer';
                 $input['items_id'] = $computers_id;
                 $input['is_dynamic'] = 1;
                 $input['name'] = $a_networkport['name'];
                 $networkPort->update($input, !$no_history);
                 $pfUnmanaged = new PluginFusioninventoryUnmanaged();
                 $pfUnmanaged->delete(array('id' => $unmanageds_id), 1);
             }
         }
     }
     // end get port from unknwon device
     $db_networkport = array();
     if ($no_history === FALSE) {
         $query = "SELECT `id`, `name`, `mac`, `instantiation_type`, `logical_number`\n             FROM `glpi_networkports`\n             WHERE `items_id` = '{$computers_id}'\n               AND `itemtype`='Computer'\n               AND `is_dynamic`='1'";
         $result = $DB->query($query);
         while ($data = $DB->fetch_assoc($result)) {
             $idtmp = $data['id'];
             unset($data['id']);
             if (is_null($data['mac'])) {
                 $data['mac'] = '';
             }
             if (preg_match("/[^a-zA-Z0-9 \\-_\\(\\)]+/", $data['name'])) {
                 $data['name'] = Toolbox::addslashes_deep($data['name']);
             }
             $db_networkport[$idtmp] = array_map('strtolower', $data);
         }
     }
     $simplenetworkport = array();
     foreach ($inventory_networkports as $key => $a_networkport) {
         // Add ipnetwork if not exist
         if ($a_networkport['gateway'] != '' && $a_networkport['netmask'] != '' && $a_networkport['subnet'] != '') {
             if (countElementsInTable('glpi_ipnetworks', "`address`='" . $a_networkport['subnet'] . "'\n                                     AND `netmask`='" . $a_networkport['netmask'] . "'\n                                     AND `gateway`='" . $a_networkport['gateway'] . "'\n                                     AND `entities_id`='" . $_SESSION["plugin_fusioninventory_entity"] . "'") == 0) {
                 $input_ipanetwork = array('name' => $a_networkport['subnet'] . '/' . $a_networkport['netmask'] . ' - ' . $a_networkport['gateway'], 'network' => $a_networkport['subnet'] . ' / ' . $a_networkport['netmask'], 'gateway' => $a_networkport['gateway'], 'entities_id' => $_SESSION["plugin_fusioninventory_entity"]);
                 $iPNetwork->add($input_ipanetwork, array(), !$no_history);
             }
         }
         // End add ipnetwork
         $a_field = array('name', 'mac', 'instantiation_type');
         foreach ($a_field as $field) {
             if (isset($a_networkport[$field])) {
                 $simplenetworkport[$key][$field] = $a_networkport[$field];
             }
         }
     }
     foreach ($simplenetworkport as $key => $arrays) {
         $arrayslower = array_map('strtolower', $arrays);
         foreach ($db_networkport as $keydb => $arraydb) {
             $logical_number = $arraydb['logical_number'];
             unset($arraydb['logical_number']);
             if ($arrayslower == $arraydb) {
                 if ($inventory_networkports[$key]['logical_number'] != $logical_number) {
                     $input = array();
                     $input['id'] = $keydb;
                     $input['logical_number'] = $inventory_networkports[$key]['logical_number'];
                     $networkPort->update($input, !$no_history);
                 }
                 // Add / update instantiation_type
                 if (isset($inventory_networkports[$key]['instantiation_type'])) {
                     if ($inventory_networkports[$key]['instantiation_type'] == 'NetworkPortEthernet') {
                         $portsethernet = $networkPortEthernet->find("`networkports_id`='" . $keydb . "'", '', 1);
                         if (count($portsethernet) == 1) {
                             $portethernet = current($portsethernet);
                             $input = $portethernet;
                         } else {
                             $input = array('networkports_id' => $keydb);
                         }
                         if (isset($inventory_networkports[$key]['speed'])) {
                             $input['speed'] = $inventory_networkports[$key]['speed'];
                             $input['speed_other_value'] = $inventory_networkports[$key]['speed'];
                         }
                         if (isset($inventory_networkports[$key]['mac'])) {
                             $networkcards = $item_DeviceNetworkCard->find("`mac`='" . $inventory_networkports[$key]['mac'] . "' " . " AND `itemtype`='Computer'" . " AND `items_id`='" . $computers_id . "'", '', 1);
                             if (count($networkcards) == 1) {
                                 $networkcard = current($networkcards);
                                 $input['items_devicenetworkcards_id'] = $networkcard['id'];
                             }
                         }
                         $input['_no_history'] = $no_history;
                         if (isset($input['id'])) {
                             $networkPortEthernet->update($input);
                         } else {
                             $networkPortEthernet->add($input);
                         }
                     }
                 }
                 // Get networkname
                 $a_networknames_find = current($networkName->find("`items_id`='" . $keydb . "'\n                                                    AND `itemtype`='NetworkPort'", "", 1));
                 if (!isset($a_networknames_find['id'])) {
                     $a_networkport['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
                     $a_networkport['items_id'] = $computers_id;
                     $a_networkport['itemtype'] = "Computer";
                     $a_networkport['is_dynamic'] = 1;
                     $a_networkport['_no_history'] = $no_history;
                     $a_networkport['items_id'] = $keydb;
                     unset($a_networkport['_no_history']);
                     $a_networkport['is_recursive'] = 0;
                     $a_networkport['itemtype'] = 'NetworkPort';
                     unset($a_networkport['name']);
                     $a_networkport['_no_history'] = $no_history;
                     $a_networknames_id = $networkName->add($a_networkport, array(), !$no_history);
                     $a_networknames_find['id'] = $a_networknames_id;
                 }
                 // Same networkport, verify ipaddresses
                 $db_addresses = array();
                 $query = "SELECT `id`, `name` FROM `glpi_ipaddresses`\n                   WHERE `items_id` = '" . $a_networknames_find['id'] . "'\n                     AND `itemtype`='NetworkName'";
                 $result = $DB->query($query);
                 while ($data = $DB->fetch_assoc($result)) {
                     $db_addresses[$data['id']] = $data['name'];
                 }
                 $a_computerinventory_ipaddress = $inventory_networkports[$key]['ipaddress'];
                 foreach ($a_computerinventory_ipaddress as $key2 => $arrays2) {
                     foreach ($db_addresses as $keydb2 => $arraydb2) {
                         if ($arrays2 == $arraydb2) {
                             unset($a_computerinventory_ipaddress[$key2]);
                             unset($db_addresses[$keydb2]);
                             break;
                         }
                     }
                 }
                 if (count($a_computerinventory_ipaddress) == 0 and count($db_addresses) == 0) {
                     // Nothing to do
                 } else {
                     if (count($db_addresses) != 0) {
                         // Delete ip address in DB
                         foreach (array_keys($db_addresses) as $idtmp) {
                             $iPAddress->delete(array('id' => $idtmp), 1);
                         }
                     }
                     if (count($a_computerinventory_ipaddress) != 0) {
                         foreach ($a_computerinventory_ipaddress as $ip) {
                             $input = array();
                             $input['items_id'] = $a_networknames_find['id'];
                             $input['itemtype'] = 'NetworkName';
                             $input['name'] = $ip;
                             $input['is_dynamic'] = 1;
                             $iPAddress->add($input, array(), !$no_history);
                         }
                     }
                 }
                 unset($db_networkport[$keydb]);
                 unset($simplenetworkport[$key]);
                 unset($inventory_networkports[$key]);
                 break;
             }
         }
     }
     if (count($inventory_networkports) == 0 and count($db_networkport) == 0) {
         // Nothing to do
     } else {
         if (count($db_networkport) != 0) {
             // Delete networkport in DB
             foreach ($db_networkport as $idtmp => $data) {
                 $networkPort->delete(array('id' => $idtmp), 1);
             }
         }
         if (count($inventory_networkports) != 0) {
             foreach ($inventory_networkports as $a_networkport) {
                 $a_networkport['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
                 $a_networkport['items_id'] = $computers_id;
                 $a_networkport['itemtype'] = "Computer";
                 $a_networkport['is_dynamic'] = 1;
                 $a_networkport['_no_history'] = $no_history;
                 $a_networkport['items_id'] = $networkPort->add($a_networkport, array(), !$no_history);
                 unset($a_networkport['_no_history']);
                 $a_networkport['is_recursive'] = 0;
                 $a_networkport['itemtype'] = 'NetworkPort';
                 unset($a_networkport['name']);
                 $a_networkport['_no_history'] = $no_history;
                 $a_networknames_id = $networkName->add($a_networkport, array(), !$no_history);
                 foreach ($a_networkport['ipaddress'] as $ip) {
                     $input = array();
                     $input['items_id'] = $a_networknames_id;
                     $input['itemtype'] = 'NetworkName';
                     $input['name'] = $ip;
                     $input['is_dynamic'] = 1;
                     $input['_no_history'] = $no_history;
                     $iPAddress->add($input, array(), !$no_history);
                 }
                 if (isset($a_networkport['instantiation_type'])) {
                     if ($a_networkport['instantiation_type'] == 'NetworkPortEthernet') {
                         $input = array('networkports_id' => $a_networkport['items_id']);
                         if (isset($a_networkport['speed'])) {
                             $input['speed'] = $a_networkport['speed'];
                             $input['speed_other_value'] = $a_networkport['speed'];
                         }
                         if (isset($a_networkport['mac'])) {
                             $networkcards = $item_DeviceNetworkCard->find("`mac`='" . $a_networkport['mac'] . "' " . " AND `itemtype`='Computer'" . " AND `items_id`='" . $computers_id . "'", '', 1);
                             if (count($networkcards) == 1) {
                                 $networkcard = current($networkcards);
                                 $input['items_devicenetworkcards_id'] = $networkcard['id'];
                             }
                         }
                         $input['_no_history'] = $no_history;
                         $networkPortEthernet->add($input);
                     }
                 }
             }
         }
     }
 }
 /**
  * @test
  */
 public function NetworkPortConnection()
 {
     global $DB;
     $DB->connect();
     $networkPort = new NetworkPort();
     $networkPort_NetworkPort = new NetworkPort_NetworkPort();
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     $a_networkports = $networkPort->find("`logical_number`='10001'");
     $this->assertEquals(1, count($a_networkports), 'Number of networkport 10001 may be 1');
     $a_networkport = current($a_networkports);
     $opposites_id = $networkPort_NetworkPort->getOppositeContact($a_networkport['id']);
     $networkPort->getFromDB($opposites_id);
     $pfUnmanaged->getFromDB($networkPort->fields['items_id']);
     $this->assertEquals(0, $pfUnmanaged->fields['hub'], 'May not be a hub');
     $a_networkports = $networkPort->find("`items_id`='" . $pfUnmanaged->fields['id'] . "'\n         AND `itemtype`='PluginFusioninventoryUnmanaged'");
     $this->assertEquals(1, count($a_networkports), 'Number of networkport of unknown ports may be 1');
 }
function plugin_item_purge_fusioninventory($parm)
{
    switch (get_class($parm)) {
        case 'NetworkPort_NetworkPort':
            // If remove connection of a hub port (unknown device), we must delete this port too
            $NetworkPort = new NetworkPort();
            $NetworkPort_Vlan = new NetworkPort_Vlan();
            $pfUnmanaged = new PluginFusioninventoryUnmanaged();
            $networkPort_NetworkPort = new NetworkPort_NetworkPort();
            $a_hubs = array();
            $port_id = $NetworkPort->getContact($parm->getField('networkports_id_1'));
            $NetworkPort->getFromDB($parm->getField('networkports_id_1'));
            if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                if ($pfUnmanaged->fields['hub'] == '1') {
                    $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    $NetworkPort->delete($NetworkPort->fields);
                }
            }
            $NetworkPort->getFromDB($port_id);
            if ($port_id) {
                if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                    $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    }
                }
            }
            $port_id = $NetworkPort->getContact($parm->getField('networkports_id_2'));
            $NetworkPort->getFromDB($parm->getField('networkports_id_2'));
            if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                if ($pfUnmanaged->getFromDB($NetworkPort->fields['items_id'])) {
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_vlans = $NetworkPort_Vlan->getVlansForNetworkPort($NetworkPort->fields['id']);
                        foreach ($a_vlans as $vlan_id) {
                            $NetworkPort_Vlan->unassignVlan($NetworkPort->fields['id'], $vlan_id);
                        }
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                        $NetworkPort->delete($NetworkPort->fields);
                    }
                }
            }
            if ($port_id) {
                $NetworkPort->getFromDB($port_id);
                if ($NetworkPort->fields['itemtype'] == 'PluginFusioninventoryUnmanaged') {
                    $pfUnmanaged->getFromDB($NetworkPort->fields['items_id']);
                    if ($pfUnmanaged->fields['hub'] == '1') {
                        $a_hubs[$NetworkPort->fields['items_id']] = 1;
                    }
                }
            }
            // If hub have no port, delete it
            foreach (array_keys($a_hubs) as $unkowndevice_id) {
                $a_networkports = $NetworkPort->find("`itemtype`='PluginFusioninventoryUnmanaged'\n               AND `items_id`='" . $unkowndevice_id . "' ");
                if (count($a_networkports) < 2) {
                    $pfUnmanaged->delete(array('id' => $unkowndevice_id), 1);
                } else {
                    if (count($a_networkports) == '2') {
                        $switchPorts_id = 0;
                        $otherPorts_id = 0;
                        foreach ($a_networkports as $data) {
                            if ($data['name'] == 'Link') {
                                $switchPorts_id = $NetworkPort->getContact($data['id']);
                            } else {
                                if ($otherPorts_id == '0') {
                                    $otherPorts_id = $NetworkPort->getContact($data['id']);
                                } else {
                                    $switchPorts_id = $NetworkPort->getContact($data['id']);
                                }
                            }
                        }
                        $pfUnmanaged->disconnectDB($switchPorts_id);
                        // disconnect this port
                        $pfUnmanaged->disconnectDB($otherPorts_id);
                        // disconnect destination port
                        $networkPort_NetworkPort->add(array('networkports_id_1' => $switchPorts_id, 'networkports_id_2' => $otherPorts_id));
                    }
                }
            }
            break;
        case 'NetworkEquipment':
            // Delete all ports
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_networkequipments`\n                          WHERE `networkequipments_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_select = "SELECT `glpi_plugin_fusioninventory_networkports`.`id`,\n                              `glpi_networkports`.`id` as nid\n                          FROM `glpi_plugin_fusioninventory_networkports`\n                               LEFT JOIN `glpi_networkports`\n                                         ON `glpi_networkports`.`id` = `networkports_id`\n                          WHERE `items_id`='" . $parm->fields["id"] . "'\n                                AND `itemtype`='NetworkEquipment';";
            $result = $DB->query($query_select);
            while ($data = $DB->fetch_array($result)) {
                $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_networkports`\n                             WHERE `id`='" . $data["id"] . "';";
                $DB->query($query_delete);
                $query_delete = "DELETE FROM `glpi_plugin_fusinvsnmp_networkportlogs`\n                           WHERE `networkports_id`='" . $data['nid'] . "'";
                $DB->query($query_delete);
            }
            break;
        case "Printer":
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printers`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printercartridges`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            $query_delete = "DELETE FROM `glpi_plugin_fusioninventory_printerlogs`\n                          WHERE `printers_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            break;
        case 'PluginFusioninventoryUnmanaged':
            $query_delete = "DELETE FROM `glpi_plugin_fusinvsnmp_unmanageds`\n                          WHERE `plugin_fusioninventory_unmanageds_id`='" . $parm->fields["id"] . "';";
            $DB->query($query_delete);
            break;
    }
    return $parm;
}
 /**
  * @test
  * network discovery
  */
 public function IgnoreNetworkDiscoveryImport()
 {
     global $DB;
     $DB->connect();
     $a_inventory = array('DNSHOSTNAME' => 'pctest', 'ENTITY' => 0, 'IP' => '192.168.20.3');
     $pfCommunicationNetworkDiscovery = new PluginFusioninventoryCommunicationNetworkDiscovery();
     $computer = new Computer();
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     $pfIgnoredimportdevice = new PluginFusioninventoryIgnoredimportdevice();
     $GLPIlog = new GLPIlogs();
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = 1;
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = '1';
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'Computer';
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = 0;
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '';
     $pfCommunicationNetworkDiscovery->sendCriteria($a_inventory);
     $a_computers = $computer->find();
     $this->assertEquals(0, count($a_computers), 'Computer may not be added');
     $a_unknown = $pfUnmanaged->find();
     $this->assertEquals(0, count($a_unknown), 'Unmanaged may not be added');
     $a_ignored = $pfIgnoredimportdevice->find();
     $this->assertEquals(1, count($a_ignored), 'May have only one ignored device import');
 }
  ------------------------------------------------------------------------

  @package   FusionInventory
  @author    David Durieux
  @co-author
  @copyright Copyright (c) 2010-2015 FusionInventory team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      http://www.fusioninventory.org/
  @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
$pfUnmanaged = new PluginFusioninventoryUnmanaged();
$ptt = new PluginFusioninventoryTask();
Html::header(__('FusionInventory', 'fusioninventory'), $_SERVER["PHP_SELF"], "assets", "pluginfusioninventoryunmanaged");
Session::checkRight('plugin_fusioninventory_unmanaged', READ);
PluginFusioninventoryMenu::displayMenu("mini");
$id = "";
if (isset($_GET["id"])) {
    $id = $_GET["id"];
}
if (isset($_POST["add"])) {
    Session::checkRight('plugin_fusioninventory_unmanaged', CREATE);
    if (isset($_POST['items_id']) && $_POST['items_id'] != "0" and $_POST['items_id'] != "") {
        $_POST['itemtype'] = '1';
    }
    $pfUnmanaged->add($_POST);
    Html::back();
 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));
                     }
                 }
             }
         }
     }
 }