Exemple #1
0
 /**
  * @covers NetworkPort::prepareInputForAdd
  * @covers NetworkPort::post_addItem
  */
 public function testAddCompleteNetworkPort()
 {
     $this->Login();
     $computer1 = getItemByTypeName('Computer', '_test_pc01');
     // Do some installations
     $ins = new NetworkPort();
     // Be sure added
     $nb_log = countElementsInTable('glpi_logs');
     $this->assertGreaterThan(0, $ins->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 3, 'mac' => '00:24:81:eb:c6:d2', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em3', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test1.me', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.1']]));
     $this->assertGreaterThan($nb_log, countElementsInTable('glpi_logs'));
     // be sure added and have no logs
     $nb_log = countElementsInTable('glpi_logs');
     $this->assertGreaterThan(0, $ins->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 4, 'mac' => '00:24:81:eb:c6:d4', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em4', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test2.me', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.2']], [], false));
     $this->assertEquals($nb_log, countElementsInTable('glpi_logs'));
 }
 /**
  * @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");
 }
 /**
  * @covers NetworkPort::prepareInputForAdd
  * @covers NetworkPort::post_addItem
  */
 public function testAddCompleteNetworkPort()
 {
     $this->Login();
     $computer1 = getItemByTypeName('Computer', '_test_pc01');
     // Do some installations
     $networkport = new NetworkPort();
     // Be sure added
     $nb_log = countElementsInTable('glpi_logs');
     $new_id = $networkport->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 3, 'mac' => '00:24:81:eb:c6:d2', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em3', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test1', 'NetworkName_comment' => 'test1 comment', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.1'], '_create_children' => true]);
     $this->assertGreaterThan(0, $new_id);
     $this->assertGreaterThan($nb_log, countElementsInTable('glpi_logs'));
     // check data in db
     // 1 -> NetworkPortEthernet
     $all_netportethernets = getAllDatasFromTable('glpi_networkportethernets', '', false, 'id');
     $networkportethernet = end($all_netportethernets);
     unset($networkportethernet['id']);
     unset($networkportethernet['date_mod']);
     unset($networkportethernet['date_creation']);
     $expected = ['networkports_id' => $new_id, 'items_devicenetworkcards_id' => '0', 'netpoints_id' => '0', 'type' => 'T', 'speed' => '1000'];
     $this->assertEquals($expected, $networkportethernet);
     // 2 -> NetworkName
     $all_networknames = getAllDatasFromTable('glpi_networknames', '', false, 'id');
     $networkname = end($all_networknames);
     $networknames_id = $networkname['id'];
     unset($networkname['id']);
     unset($networkname['date_mod']);
     unset($networkname['date_creation']);
     $expected = ['entities_id' => $computer1->fields['entities_id'], 'items_id' => $new_id, 'itemtype' => 'NetworkPort', 'name' => 'test1', 'comment' => 'test1 comment', 'fqdns_id' => 0, 'is_deleted' => 0, 'is_dynamic' => 0];
     $this->assertEquals($expected, $networkname);
     // 3 -> IPAddress
     $all_ipadresses = getAllDatasFromTable('glpi_ipaddresses', '', false, 'id');
     $ipadress = end($all_ipadresses);
     unset($ipadress['id']);
     unset($ipadress['date_mod']);
     unset($ipadress['date_creation']);
     $expected = ['entities_id' => $computer1->fields['entities_id'], 'items_id' => $networknames_id, 'itemtype' => 'NetworkName', 'version' => '4', 'name' => '192.168.20.1', 'binary_0' => '0', 'binary_1' => '0', 'binary_2' => '65535', 'binary_3' => '3232240641', 'is_deleted' => '0', 'is_dynamic' => '0', 'mainitems_id' => $computer1->getID(), 'mainitemtype' => 'Computer'];
     $this->assertEquals($expected, $ipadress);
     // be sure added and have no logs
     $nb_log = countElementsInTable('glpi_logs');
     $new_id = $networkport->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 4, 'mac' => '00:24:81:eb:c6:d4', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em4', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test2', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.2']], [], false);
     $this->assertGreaterThan(0, $new_id);
     $this->assertEquals($nb_log, countElementsInTable('glpi_logs'));
 }
 /**
  * @test
  */
 public function PrinterToInventoryWithoutIP()
 {
     self::restore_database();
     $printer = new Printer();
     $networkport = new NetworkPort();
     $networkName = new NetworkName();
     $pfPrinter = new PluginFusioninventoryPrinter();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $communication = new PluginFusioninventoryCommunication();
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Create printer
     $input = array('name' => 'printer 001', 'entities_id' => 0);
     $printers_id = $printer->add($input);
     // Add port
     $networkports_id = $networkport->add(array('itemtype' => 'Printer', 'instantiation_type' => 'NetworkPortEthernet', 'items_id' => $printers_id, 'entities_id' => 0));
     $networknames_id = $networkName->add(array('entities_id' => 0, 'itemtype' => 'NetworkPort', 'items_id' => $networkports_id));
     $input = array('printers_id' => 1, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfPrinter->add($input);
     // Add task
     // create task
     $input = array('entities_id' => 0, 'name' => 'network inventory', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'inventory', 'method' => 'networkinventory', 'targets' => '[{"Printer":"' . $printers_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"1"}]');
     $pfTaskjob->add($input);
     PluginFusioninventoryTask::cronTaskscheduler();
     $data = $pfTask->getJoblogs(array(1));
     // Task is prepared
     // Agent will get data
     $communication->getTaskAgent(1);
     $message = $communication->getMessage();
     $json = json_encode($message);
     $array = json_decode($json, TRUE);
     $ref = array();
     $this->assertEquals($ref, $array, 'XML of SNMP inventory task');
 }
 /**
  * @test
  */
 public function Cisco2Switch()
 {
     global $DB;
     $DB->connect();
     self::restore_database();
     $a_lldp = array('ifdescr' => 'ge-0/0/1.0', 'logical_number' => '504', 'sysdescr' => 'Juniper Networks, Inc. ex2200-24t-4g , version 10.1R1.8 Build date: 2010-02-12 16:59:31 UTC ', 'model' => '', 'ip' => '', 'mac' => '2c:6b:f5:98:f9:70', 'name' => 'juniperswitch3');
     $pfINetworkEquipmentLib = new PluginFusioninventoryInventoryNetworkEquipmentLib();
     $networkEquipment = new NetworkEquipment();
     $networkport = new NetworkPort();
     $pfNetworkPort = new PluginFusioninventoryNetworkPort();
     // Cisco switch
     $networkequipments_id = $networkEquipment->add(array('name' => 'cisco2', 'entities_id' => 0));
     $networkports_id = $networkport->add(array('itemtype' => 'NetworkEquipment', 'items_id' => $networkequipments_id, 'entities_id' => 0));
     // Another switch
     $networkequipments_other_id = $networkEquipment->add(array('name' => 'juniperswitch3', 'entities_id' => 0));
     // Port ge-0/0/1.0
     $networkports_other_id = $networkport->add(array('itemtype' => 'NetworkEquipment', 'items_id' => $networkequipments_other_id, 'entities_id' => 0, 'mac' => '2c:6b:f5:98:f9:70', 'logical_number' => 504));
     $pfNetworkPort->add(array('networkports_id' => $networkports_other_id, 'ifdescr' => 'ge-0/0/1.0'));
     $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(2, count($a_networkports), 'May have 2 network ports (' . print_r($a_networkports, TRUE) . ')');
     $a_ref = array('id' => 1, 'networkports_id_1' => $networkports_id, 'networkports_id_2' => $networkports_other_id);
     $this->assertEquals($a_ref, current($a_portslinks), 'Link port');
 }
$npv = new NetworkPort_Vlan();
if (!isset($_GET["id"])) {
    $_GET["id"] = "";
}
if (isset($_POST["add"])) {
    // Is a preselected mac adress selected ?
    if (isset($_POST['pre_mac'])) {
        if (!empty($_POST['pre_mac'])) {
            $_POST['mac'] = $_POST['pre_mac'];
        }
        unset($_POST['pre_mac']);
    }
    if (!isset($_POST["several"])) {
        $np->check(-1, 'w', $_POST);
        $np->splitInputForElements($_POST);
        $newID = $np->add($_POST);
        $np->updateDependencies(1);
        Event::log($newID, "networkport", 5, "inventory", sprintf(__('%s adds an item'), $_SESSION["glpiname"]));
        Html::back();
    } else {
        Session::checkRight("networking", "w");
        $input = $_POST;
        unset($input['several']);
        unset($input['from_logical_number']);
        unset($input['to_logical_number']);
        for ($i = $_POST["from_logical_number"]; $i <= $_POST["to_logical_number"]; $i++) {
            $add = "";
            if ($i < 10) {
                $add = "0";
            }
            $input["logical_number"] = $i;
 function getPortIDfromSysmacandPortnumber($sysmac, $ifnumber, $params = array())
 {
     global $DB;
     $PortID = '';
     $queryPort = "SELECT *\n         FROM `glpi_networkports`\n         WHERE `mac`='" . $sysmac . "'\n            AND `itemtype`='NetworkEquipment'\n            AND `logical_number`='" . $ifnumber . "'\n         LIMIT 1";
     $resultPort = $DB->query($queryPort);
     if ($DB->numrows($resultPort) == "1") {
         $dataPort = $DB->fetch_assoc($resultPort);
         $PortID = $dataPort['id'];
     }
     if ($PortID == '') {
         // case where mac is of switch and not of the port (like Procurve)
         $queryPort = "SELECT *\n            FROM `glpi_plugin_fusioninventory_networkports`\n            LEFT JOIN `glpi_networkports`\n               ON `glpi_plugin_fusioninventory_networkports`.`networkports_id`=\n                  `glpi_networkports`.`id`\n            LEFT JOIN `glpi_networkequipments`\n               ON `glpi_networkports`.`items_id`=\n                  `glpi_networkequipments`.`id`\n            WHERE `glpi_networkports`.`mac`='" . $sysmac . "'\n               AND `glpi_networkports`.`itemtype`='NetworkEquipment'\n               AND `logical_number`='" . $ifnumber . "'\n            LIMIT 1";
         $resultPort = $DB->query($queryPort);
         $dataPort = $DB->fetch_assoc($resultPort);
         if ($DB->numrows($resultPort) == "1") {
             $PortID = $dataPort['networkports_id'];
         }
     }
     // In case of mac is mac of switch, not a port
     if ($PortID == '') {
         $queryPort = "SELECT *\n            FROM `glpi_networkports`\n            WHERE `logical_number`='" . $ifnumber . "'\n               AND `itemtype`='NetworkEquipment'\n               AND `items_id` IN\n               (SELECT `items_id`\n                FROM `glpi_networkports`\n                WHERE `instantiation_type`='NetworkPortAggregate'\n                  AND `mac`='" . $sysmac . "')\n            LIMIT 1";
         $resultPort = $DB->query($queryPort);
         $dataPort = $DB->fetch_assoc($resultPort);
         if (isset($dataPort['id'])) {
             $PortID = $dataPort['id'];
         }
     }
     if ($PortID == "") {
         $NetworkPort = new NetworkPort();
         $PluginFusioninventoryUnmanaged = new PluginFusioninventoryUnmanaged();
         $query = "SELECT *\n             FROM `glpi_networkports`\n             WHERE `itemtype`='PluginFusioninventoryUnmanaged'\n               AND `mac`='" . $sysmac . "'\n             LIMIT 1";
         $result = $DB->query($query);
         if ($DB->numrows($result) == "1") {
             $data = $DB->fetch_assoc($result);
             return $data['id'];
         }
         // Add unmanaged device because not find device
         $input = array();
         $input['mac'] = $sysmac;
         if (isset($params['sysname'])) {
             $input['name'] = $params['sysname'];
         }
         if (isset($_SESSION["plugin_fusioninventory_entity"])) {
             $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
         }
         if (isset($params['sysdescr'])) {
             $input['sysdescr'] = $params['sysdescr'];
         }
         $unkonwn_id = $PluginFusioninventoryUnmanaged->add($input);
         // Add port
         $input = array();
         $input['items_id'] = $unkonwn_id;
         $input['itemtype'] = 'PluginFusioninventoryUnmanaged';
         $input['mac'] = $sysmac;
         if (isset($params['ifdescr'])) {
             $input['name'] = $params['ifdescr'];
         }
         $input['instantiation_type'] = 'NetworkPortEthernet';
         $PortID = $NetworkPort->add($input);
         return $PortID;
     }
     return $PortID;
 }
 function _updateNetworkInfo($arrayinventory, $item_type, $id, $instanciation_type, $check_addresses)
 {
     $NetworkPort = new NetworkPort();
     $port = current($NetworkPort->find("`itemtype`='{$item_type}' AND `items_id`='{$id}'" . " AND `instantiation_type`='{$instanciation_type}'", "", 1));
     $port_id = 0;
     if (isset($port['id'])) {
         if (isset($arrayinventory['MAC']) and !empty($arrayinventory['MAC'])) {
             $input = array();
             $input['id'] = $port['id'];
             $input['mac'] = $arrayinventory['MAC'];
             $NetworkPort->update($input);
         }
         $port_id = $port['id'];
     } else {
         $input = array();
         $input['itemtype'] = $item_type;
         $input['items_id'] = $id;
         $input['instantiation_type'] = $instanciation_type;
         $input['name'] = "management";
         if (isset($arrayinventory['MAC']) && !empty($arrayinventory['MAC'])) {
             $input['mac'] = $arrayinventory['MAC'];
         }
         $port_id = $NetworkPort->add($input);
     }
     $NetworkName = new NetworkName();
     $name = current($NetworkName->find("`itemtype`='NetworkPort' AND `items_id`='" . $port_id . "'", "", 1));
     $name_id = 0;
     if (isset($name['id'])) {
         $name_id = $name['id'];
     } else {
         $input = array();
         $input['itemtype'] = 'NetworkPort';
         $input['items_id'] = $port_id;
         $name_id = $NetworkName->add($input);
     }
     if (isset($arrayinventory['IP'])) {
         $IPAddress = new IPAddress();
         if ($check_addresses) {
             $addresses = $IPAddress->find("`itemtype`='NetworkName'\n               AND `items_id`='" . $name_id . "'", '', 1);
         } else {
             // Case of NetworkEquipment
             $a_ips = $IPAddress->find("`itemtype`='NetworkName'\n               AND `items_id`='" . $name_id . "'\n               AND `name`='" . $arrayinventory['IP'] . "'", '', 1);
             if (count($a_ips) > 0) {
                 $addresses = $a_ips;
             } else {
                 $addresses = array();
             }
         }
         if (count($addresses) == 0) {
             $input = array();
             $input['itemtype'] = 'NetworkName';
             $input['items_id'] = $name_id;
             $input['name'] = $arrayinventory['IP'];
             $IPAddress->add($input);
         } else {
             $address = current($addresses);
             if ($address['name'] != $arrayinventory['IP']) {
                 $input = array();
                 $input['id'] = $address['id'];
                 $input['name'] = $arrayinventory['IP'];
                 $IPAddress->update($input);
             }
         }
     }
 }
/**  Generate bigdump : Create networkport ethernet
 *
 * @since version 0.84
 *
 * @param $itemtype        item type
 * @param $items_id        item ID
 * @param $entities_id     item entity ID
 * @param $locations_id    ID of the location trying to link with network equipment (default 0)
**/
function addNetworkEthernetPort($itemtype, $items_id, $entities_id, $locations_id=0) {
   global $NET_LOC, $NET_PORT, $MAX, $VLAN_LOC;

   // Add networking ports
   $newIP   = getNextIP();
   $newMAC  = getNextMAC();

   if (($itemtype == 'NetworkEquipment') && $locations_id) {
      // Find father locations_id;
      $loc = new Location();
      if ($loc->getFromDB($locations_id)) {
         $locations_id = $loc->getField('locations_id');
      } else {
         $locations_id = 0;
      }
   }

   //insert netpoint
   $netpoint   = new NetPoint();
   $netpointID = $netpoint->add(toolbox::addslashes_deep(
                                array('entities_id'  => $entities_id,
                                      'locations_id' => $locations_id,
                                      'name'         => getNextNETPOINT(),
                                      'comment'      => "comment 'netpoint $locations_id")));

   if ($locations_id && !isset($VLAN_LOC[$locations_id])) {
      $vlanID                  = mt_rand(1,$MAX["vlan"]);
      $VLAN_LOC[$locations_id] = $vlanID;
   }
   if (!isset($NET_PORT[$itemtype][$items_id])) {
      $NET_PORT[$itemtype][$items_id]=0;
   }
   $np          = new NetworkPort();
   $nv          = new NetworkPort_Vlan();
   $newportname = "port of $itemtype-$items_id";
   $refportID   = 0;

   if ($locations_id && isset($NET_LOC[$locations_id]) && $NET_LOC[$locations_id]) {
      $refportname  = "link 'port to  $itemtype-$items_id";
      $newportname .= " link to 'NetworkEquipment' -".$NET_LOC[$locations_id];
      $newMAC2      = getNextMAC();
      $newIP2      = getNextIP();

      // Create new port on ref item
      $param = toolbox::addslashes_deep(
               array('itemtype'                 => 'NetworkEquipment',
                     'items_id'                 => $NET_LOC[$locations_id],
                     'entities_id'              => $entities_id,
                     'logical_number'  => $NET_PORT['NetworkEquipment'][$NET_LOC[$locations_id]]++,
                     'name'                     => "name '$refportname",
                     'instantiation_type'       => 'NetworkPortEthernet',
                     'mac'                      => $newMAC2,
                     'comment'                  => "comment '$refportname",
                     'netpoints_id'             => $netpointID,
                     'NetworkName_name'         => "NetworkEquipment$itemtype-$items_id-$entities_id",
                     'NetworkName__ipaddresses' => array(-100 => $newIP2['ip']),
                     ));

                     $np->splitInputForElements($param);
      $refportID = $np->add($param);
      $np->updateDependencies(1);
      if (isset($VLAN_LOC[$locations_id]) && $refportID) {
         $nv->add(array('networkports_id' => $refportID,
                        'vlans_id'        => $VLAN_LOC[$locations_id]));
      }
   }

//    $query = "INSERT INTO `glpi_networkports`
//                VALUES (NULL, '$netwID', 'NetworkEquipment', '$ID_entity', '0',
//                      '".$NET_PORT['NetworkEquipment'][$netwID]++."',
//                      'link port to netw ".$NET_LOC[$data['locations_id']]."',
//                      '".$newIP['ip']."', '$newMAC', '$iface', '$netpointID',
//                      '".$newIP['netwmask']."', '".$newIP['gateway']."',
//                      '".$newIP['subnet']."','comment')";
//    $DB->query($query) or die("PB REQUETE ".$query);

   $param = toolbox::addslashes_deep(
            array('itemtype'                 => $itemtype,
                  'items_id'                 => $items_id,
                  'entities_id'              => $entities_id,
                  'logical_number'           => $NET_PORT[$itemtype][$items_id]++,
                  'name'                     => "name '$newportname",
                  'instantiation_type'       => 'NetworkPortEthernet',
                  'mac'                      => $newMAC,
                  'comment'                  => "comment '$newportname",
                  'netpoints_id'             => $netpointID,
                  'NetworkName_name'         => "$itemtype-$items_id-$entities_id",
                  'NetworkName__ipaddresses' => array(-100 => $newIP['ip']),
                  ));

                  $np->splitInputForElements($param);
   $newportID = $np->add($param);

   $np->updateDependencies(1);
   if (isset($VLAN_LOC[$locations_id]) && $newportID) {
      $nv->add(array('networkports_id' => $newportID,
                     'vlans_id'        => $VLAN_LOC[$locations_id]));
   }
   if ($locations_id && $refportID && $newportID) {
      // link ports
      $nn = new Networkport_Networkport();
      $nn->add(array('networkports_id_1' => $refportID,
                     'networkports_id_2' => $newportID,));
   } else {
      if ($locations_id) {
      }
   }
}
$nn = new NetworkPort_NetworkPort();
$npv = new NetworkPort_Vlan();
if (!isset($_GET["id"])) {
    $_GET["id"] = "";
}
if (isset($_POST["add"])) {
    // Is a preselected mac adress selected ?
    if (isset($_POST['pre_mac'])) {
        if (!empty($_POST['pre_mac'])) {
            $_POST['mac'] = $_POST['pre_mac'];
        }
        unset($_POST['pre_mac']);
    }
    if (!isset($_POST["several"])) {
        $np->check(-1, 'w', $_POST);
        $np->add($_POST);
        Event::log(0, "networkport", 5, "inventory", $_SESSION["glpiname"] . " " . $LANG['log'][70]);
        glpi_header($_SERVER['HTTP_REFERER']);
    } else {
        checkRight("networking", "w");
        $input = $_POST;
        unset($input['several']);
        unset($input['from_logical_number']);
        unset($input['to_logical_number']);
        for ($i = $_POST["from_logical_number"]; $i <= $_POST["to_logical_number"]; $i++) {
            $add = "";
            if ($i < 10) {
                $add = "0";
            }
            $input["logical_number"] = $i;
            $input["name"] = $_POST["name"] . $add . $i;
 /**
  * @param $values
  * @param $add                (true by default)
  * @param $rights    array
  */
 function processAfterInsertOrUpdate($values, $add = true, $rights = array())
 {
     if (isset($values['NetworkEquipment']['nb_ports'])) {
         for ($i = 1; $i <= $values['NetworkEquipment']['nb_ports']; $i++) {
             $input = array();
             $netport = new NetworkPort();
             $add = "";
             if ($i < 10) {
                 $add = "0";
             }
             $input["logical_number"] = $i;
             $input["name"] = $add . $i;
             $input["items_id"] = $values['NetworkEquipment']['id'];
             $input["itemtype"] = 'NetworkEquipment';
             $input["entities_id"] = $values['NetworkEquipment']['entities_id'];
             $netport->add($input);
         }
     }
 }
 function importPortAggregate($a_ports, $networkports_id, $networkequipments_id)
 {
     $networkPort = new NetworkPort();
     $networkPortAggregate = new NetworkPortAggregate();
     $a_aggregates = $networkPortAggregate->find("`networkports_id`='" . $networkports_id . "'", "", 1);
     $input = array();
     if (count($a_aggregates) == 1) {
         $input = current($a_aggregates);
     } else {
         $input['networkports_id'] = $networkports_id;
         $input['networkports_id_list'] = exportArrayToDB(array());
         $input['id'] = $networkPortAggregate->add($input);
     }
     $a_ports_db_tmp = array();
     foreach ($a_ports as $logical_number) {
         $a_networkports_DB = current($networkPort->find("`itemtype`='NetworkEquipment'\n                       AND `items_id`='" . $networkequipments_id . "'\n                       AND `instantiation_type`='NetworkPortEthernet'\n                       AND `logical_number` = '" . $logical_number . "'", '', 1));
         if (!isset($a_networkports_DB['id'])) {
             // Add port
             $a_port['instantiation_type'] = 'NetworkPortEthernet';
             $a_port['items_id'] = $networkequipments_id;
             $a_port['itemtype'] = 'NetworkEquipment';
             $a_port['logical_number'] = $logical_number;
             $networkports_id = $networkPort->add($a_port);
         } else {
             $networkports_id = $a_networkports_DB['id'];
         }
         $a_ports_db_tmp[] = $networkports_id;
     }
     $input['networkports_id_list'] = $a_ports_db_tmp;
     $networkPortAggregate->update($input);
 }
 /**
  * import ipdiscover object
  * @param type $ipDiscoveryObject array
  * @param type $plugin_ocsinventoryng_ocsservers_id integer
  * @param $subnet
  * @return array
  */
 static function importIpDiscover($ipDiscoveryObject, $plugin_ocsinventoryng_ocsservers_id, $subnet)
 {
     global $DB;
     $id = null;
     $identify = false;
     $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id);
     if (isset($ipDiscoveryObject["ocsItemType"]) && $ipDiscoveryObject["ocsItemType"] == Dropdown::EMPTY_VALUE) {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if ($ipDiscoveryObject["itemDescription"] == '') {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if ($ipDiscoveryObject["itemName"] == "") {
         $ipDiscoveryObject["itemName"] = $ipDiscoveryObject["itemDescription"];
     }
     switch ($ipDiscoveryObject["glpiItemType"]) {
         //empty dropdown value
         case '0':
             return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if (isset($ipDiscoveryObject["ocsItemType"])) {
         $identify = true;
     }
     $mac = $ipDiscoveryObject["macAdress"];
     $netPort = new NetworkPort();
     $netPort->getFromDBByQuery("WHERE `mac` = '{$mac}' ");
     if (count($netPort->fields) < 1) {
         $input = array('is_dynamic' => 1, 'locations_id' => 0, 'domains_id' => 0, 'entities_id' => $ipDiscoveryObject["entity"], 'name' => $ipDiscoveryObject["itemName"], 'comment' => $ipDiscoveryObject["itemDescription"]);
         $device = new $ipDiscoveryObject["glpiItemType"]();
         $id = $device->add($input, array(), $cfg_ocs['history_devices']);
         //ipdiscover link
         $date = date("Y-m-d H:i:s");
         $glpiType = $ipDiscoveryObject["glpiItemType"];
         $ip = $ipDiscoveryObject["itemIp"];
         $glpiQuery = "INSERT INTO `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`\n                       (`items_id`,`itemtype`,`macaddress`,`last_update`,`subnet`,`plugin_ocsinventoryng_ocsservers_id`)\n                       VALUES('{$id}','{$glpiType}','{$mac}','{$date}','{$subnet}','{$plugin_ocsinventoryng_ocsservers_id}')";
         $DB->query($glpiQuery);
         //add port
         $port_input = array('name' => $ipDiscoveryObject["itemName"] . "-" . $ip, 'mac' => $mac, 'items_id' => $id, 'itemtype' => $glpiType, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $ipDiscoveryObject["entity"], "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0);
         $netPort->add($port_input, array(), $cfg_ocs['history_network']);
         /* $netPortInput = array(
                       "itemtype"           => $glpiType,
                       "items_id"           => $id,
                       'entities_id'        => $ipDiscoveryObject["entity"],
                       "name"               => $ipDiscoveryObject["itemName"] . "-" . $ip,
                       "instantiation_type" => "NetworkPortEthernet",
                       "mac"                => $mac
                   );
         
                   $netPort->splitInputForElements($netPortInput);
                   $NewNetPortId = $netPort->add($netPortInput);
                   $netPort->updateDependencies(1);
                   //make link to IPAdress manualy
         
                   //add ipAdress
                   $networkName= new NetworkName();
                   $networkNameId=$networkName->add(array("items_id"=>$NewNetPortId,"itemtype"=>"NetworkPort"));
                   $ipAdresses = new IPAddress();
         
                   $input = array('name'        => $ip,
                               'itemtype'    => 'NetworkName',
                               'items_id'    => $networkNameId,
                               'is_deleted'  => 0,
                                'mainitems_id'=>$id,
                                'mainitemtype'=>$glpiType);
                   $ipAdresses->add($input);*/
     }
     if ($id && $identify) {
         //identify object
         //WAS IS DAS ? CHMA
         $userId = Session::getLoginUserID();
         $query = "SELECT `glpi_users`.`name` \n                         FROM `glpi_users`\n                         WHERE glpi_users.id like '{$userId}'";
         $queryResult = $DB->query($query);
         $userAssoc = $DB->fetch_assoc($queryResult);
         if ($userAssoc) {
             $ocsClient = new PluginOcsinventoryngOcsServer();
             $DBOCS = $ocsClient->getDBocs($plugin_ocsinventoryng_ocsservers_id)->getDB();
             $ocsType = $ipDiscoveryObject["ocsItemType"];
             $description = $ipDiscoveryObject["itemDescription"];
             $user = $userAssoc["name"];
             $ocsQuery = "INSERT INTO `network_devices` (`description`,`type`,`macaddr`,`user`)\n                            VALUES('{$description}','{$ocsType}','{$mac}','{$user}')";
             $DBOCS->query($ocsQuery);
         }
     }
     if ($id) {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_IMPORTED);
     } else {
         Session::addMessageAfterRedirect($mac . " : " . __('Unable to add. an object with same MAC address already exists.', 'ocsinventoryng'), false, ERROR);
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
 }
 /**
  * @param $mac
  * @param $name
  * @param $computers_id
  * @param $instantiation_type
  * @param $inst_input
  * @param $ips
  * @param $check_name
  * @param $dohistory
  * @param $already_known_ports
  * @return ID
  */
 private static function updateNetworkPort($mac, $name, $computers_id, $instantiation_type, $inst_input, $ips, $check_name, $dohistory, $already_known_ports)
 {
     global $DB;
     $network_port = new NetworkPort();
     // Then, find or create the base NetworkPort
     $query = "SELECT `id`, `is_dynamic`\n                FROM `glpi_networkports`\n                WHERE `itemtype` = 'Computer'\n                   AND `items_id` = '{$computers_id}'\n                   AND `mac` = '{$mac}'";
     // If there is virtual ports, then, filter by port's name
     if ($check_name) {
         $query .= " AND `name` = '{$name}'";
     }
     if (count($already_known_ports) > 0) {
         $query .= " AND `id` NOT IN (" . implode(',', $already_known_ports) . ")";
     }
     // We order by is_dynamic to be sure to get the static ones first !
     $query .= " ORDER BY `is_dynamic`, `id`";
     $ports = $DB->request($query);
     if ($ports->numrows() == 0) {
         $port_input = array('name' => $name, 'mac' => $mac, 'items_id' => $computers_id, 'itemtype' => 'Computer', '_no_history' => !$dohistory, 'instantiation_type' => $instantiation_type, '_create_children' => 1, 'is_dynamic' => 1, 'is_deleted' => 0);
         $networkports_id = $network_port->add($port_input, array(), $dohistory);
         if ($networkports_id === false) {
             return -1;
         }
         $inst_input['networkports_id'] = $networkports_id;
         $instantiation = $network_port->getInstantiation();
         $instantiation->update($inst_input);
         unset($instantiation);
     } else {
         $line = $ports->next();
         $networkports_id = $line['id'];
         $network_port->getFromDB($networkports_id);
         if (!$check_name && $network_port->fields['name'] != $name) {
             $port_input = array('id' => $network_port->getID(), 'name' => $name, 'is_dynamic' => 1);
             $network_port->update($port_input);
         }
         if ($network_port->fields['instantiation_type'] != $instantiation_type && $network_port->fields['is_dynamic'] == 1) {
             $network_port->switchInstantiationType($instantiation_type);
             $inst_input['networkports_id'] = $network_port->getID();
             $instantiation = $network_port->getInstantiation();
             $instantiation->add($inst_input);
             unset($instantiation);
         }
         if ($network_port->fields['instantiation_type'] == $instantiation_type) {
             $instantiation = $network_port->getInstantiation();
             $inst_input['id'] = $instantiation->getID();
             $inst_input['networkports_id'] = $network_port->getID();
             $instantiation->update($inst_input);
             unset($instantiation);
         }
     }
     if ($network_port->isNewItem()) {
         return -1;
     }
     $network_name = new NetworkName();
     $query = "SELECT `id`, `is_dynamic`\n                FROM `glpi_networknames`\n                WHERE `itemtype` = 'NetworkPort'\n                   AND `items_id` = '{$networkports_id}'\n                ORDER BY `is_dynamic`";
     if (!$ips || count($ips) == 0) {
         foreach ($DB->request($query) as $line) {
             if ($line['is_dynamic']) {
                 $network_name->delete($line, true, $dohistory);
             }
         }
     } else {
         $names = $DB->request($query);
         if ($names->numrows() == 0) {
             $name_input = array('itemtype' => 'NetworkPort', 'items_id' => $networkports_id, 'is_dynamic' => 1, 'is_deleted' => 0, '_no_history' => !$dohistory, 'name' => 'OCS-INVENTORY-NG');
             $networknames_id = $network_name->add($name_input);
         } else {
             $line = $names->next();
             $networknames_id = $line['id'];
             foreach ($names as $line) {
                 if ($line['is_dynamic'] == 1 && $line['id'] != $networknames_id) {
                     $network_port->delete($line, true, $dohistory);
                 }
             }
         }
         $ip_address = new IPAddress();
         $already_known_addresses = array();
         $query = "SELECT `id`, `name`, `is_dynamic`, `mainitems_id`\n                   FROM `glpi_ipaddresses`\n                   WHERE `itemtype` = 'NetworkName'\n                     AND `items_id` = '{$networknames_id}'\n                   ORDER BY `is_dynamic`";
         foreach ($DB->request($query) as $line) {
             if (in_array($line['name'], $ips) && !empty($line['mainitems_id'])) {
                 $already_known_addresses[] = $line['id'];
                 $ips = array_diff($ips, array($line['name']));
             } elseif ($line['is_dynamic'] == 1) {
                 $ip_address->delete($line, true, $dohistory);
             }
         }
     }
     if ($ips) {
         foreach ($ips as $ip) {
             $ip_input = array('name' => $ip, 'itemtype' => 'NetworkName', 'items_id' => $networknames_id, '_no_history' => !$dohistory, 'is_dynamic' => 1, 'is_deleted' => 0);
             $ip_address->add($ip_input);
         }
     }
     return $network_port->getID();
 }
 /**
  * @param $plugin_ocsinventoryng_ocsservers_id
  * @param $itemtype
  * @param int $ID
  * @param $ocsSnmp
  * @param $loc_id
  * @param $dom_id
  * @param $action
  * @param bool $linked
  * @return int
  */
 static function addOrUpdateOther($plugin_ocsinventoryng_ocsservers_id, $itemtype, $ID = 0, $ocsSnmp, $loc_id, $dom_id, $action, $linked = false)
 {
     $snmpDevice = new $itemtype();
     $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id);
     $input = array("is_dynamic" => 1, "entities_id" => isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0);
     if ($cfg_ocs['importsnmp_name'] && $action == "add" || $cfg_ocs['linksnmp_name'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["name"] = $ocsSnmp['META']['NAME'];
     }
     if ($cfg_ocs['importsnmp_contact'] && $action == "add" || $cfg_ocs['linksnmp_contact'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["contact"] = $ocsSnmp['META']['CONTACT'];
     }
     if ($cfg_ocs['importsnmp_comment'] && $action == "add" || $cfg_ocs['linksnmp_comment'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["comment"] = $ocsSnmp['META']['DESCRIPTION'];
     }
     if ($loc_id > 0) {
         $input["locations_id"] = $loc_id;
     }
     if ($dom_id > 0 && $itemtype != "Phone") {
         $input["domains_id"] = $dom_id;
     }
     $id_item = 0;
     if ($action == "add") {
         $id_item = $snmpDevice->add($input, array('unicity_error_message' => true), $cfg_ocs['history_hardware']);
     } else {
         $input["id"] = $ID;
         $id_item = $ID;
         if ($snmpDevice->getFromDB($id_item)) {
             $input["entities_id"] = $snmpDevice->fields['entities_id'];
         }
         $snmpDevice->update($input, $cfg_ocs['history_hardware'], array('unicity_error_message' => false, '_no_history' => !$cfg_ocs['history_hardware']));
     }
     if ($id_item > 0 && ($cfg_ocs['importsnmp_createport'] && $action == "add" || $cfg_ocs['linksnmp_createport'] && $linked || $action == "update" && $cfg_ocs['importsnmp_createport'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_createport'] && $linked)) {
         //Add network port
         $ip = $ocsSnmp['META']['IPADDR'];
         $mac = $ocsSnmp['META']['MACADDR'];
         $np = new NetworkPort();
         $np->getFromDBByQuery("WHERE `mac` LIKE '{$mac}' AND `items_id` = '{$id_item}' AND `itemtype` LIKE '{$itemtype}' ");
         if (count($np->fields) < 1) {
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_item)) {
                 $entity = $item->fields['entities_id'];
             }
             $port_input = array('name' => $ocsSnmp['META']['NAME'], 'mac' => $mac, 'items_id' => $id_item, 'itemtype' => $itemtype, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $entity, "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0);
             $np->add($port_input, array(), $cfg_ocs['history_network']);
         }
     }
     return $id_item;
 }
Exemple #16
0
 /**
  * Transfer network links
  *
  * @param $itemtype     original type of transfered item
  * @param $ID           original ID of the item
  * @param $newID        new ID of the item
  **/
 function transferNetworkLink($itemtype, $ID, $newID)
 {
     global $DB;
     /// TODO manage with new network system
     $np = new NetworkPort();
     $nn = new NetworkPort_NetworkPort();
     $query = "SELECT `glpi_networkports`.*, `glpi_networkportethernets`.`netpoints_id`\n                FROM `glpi_networkports`\n                LEFT JOIN `glpi_networkportethernets`\n                  ON (`glpi_networkports`.`id` = `glpi_networkportethernets`.`networkports_id`)\n                WHERE `glpi_networkports`.`items_id` = '{$ID}'\n                      AND `glpi_networkports`.`itemtype` = '{$itemtype}'";
     if ($result = $DB->query($query)) {
         if ($DB->numrows($result) != 0) {
             switch ($this->options['keep_networklink']) {
                 // Delete netport
                 case 0:
                     // Not a copy -> delete
                     if ($ID == $newID) {
                         while ($data = $DB->fetch_assoc($result)) {
                             $np->delete(array('id' => $data['id']));
                         }
                     }
                     // Copy -> do nothing
                     break;
                     // Disconnect
                 // Disconnect
                 case 1:
                     // Not a copy -> disconnect
                     if ($ID == $newID) {
                         while ($data = $DB->fetch_assoc($result)) {
                             if ($nn->getFromDBForNetworkPort($data['id'])) {
                                 $nn->delete($data);
                             }
                             if ($data['netpoints_id']) {
                                 $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']);
                                 $input['id'] = $data['id'];
                                 $input['netpoints_id'] = $netpointID;
                                 $np->update($input);
                             }
                         }
                     } else {
                         // Copy -> copy netports
                         while ($data = $DB->fetch_assoc($result)) {
                             $data = Toolbox::addslashes_deep($data);
                             unset($data['id']);
                             $data['items_id'] = $newID;
                             $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']);
                             unset($np->fields);
                             $np->add(toolbox::addslashes_deep($data));
                         }
                     }
                     break;
                     // Keep network links
                 // Keep network links
                 default:
                     // Copy -> Copy netpoints (do not keep links)
                     if ($ID != $newID) {
                         while ($data = $DB->fetch_assoc($result)) {
                             unset($data['id']);
                             $data['items_id'] = $newID;
                             $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']);
                             unset($np->fields);
                             $np->add(toolbox::addslashes_deep($data));
                         }
                     } else {
                         while ($data = $DB->fetch_assoc($result)) {
                             // Not a copy -> only update netpoint
                             if ($data['netpoints_id']) {
                                 $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']);
                                 $input['id'] = $data['id'];
                                 $input['netpoints_id'] = $netpointID;
                                 $np->update($input);
                             }
                         }
                     }
             }
         }
     }
 }
 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);
                     }
                 }
             }
         }
     }
 }
 function importPorts($a_inventory, $items_id)
 {
     $networkPort = new NetworkPort();
     $pfNetworkPort = new PluginFusioninventoryNetworkPort();
     $networkports_id = 0;
     foreach ($a_inventory['networkport'] as $a_port) {
         $a_ports_DB = current($networkPort->find("`itemtype`='Printer'\n                       AND `items_id`='" . $items_id . "'\n                       AND `instantiation_type`='NetworkPortEthernet'\n                       AND `logical_number` = '" . $a_port['logical_number'] . "'", '', 1));
         if (!isset($a_ports_DB['id'])) {
             // Add port
             $a_port['instantiation_type'] = 'NetworkPortEthernet';
             $a_port['items_id'] = $items_id;
             $a_port['itemtype'] = 'Printer';
             $networkports_id = $networkPort->add($a_port);
             unset($a_port['id']);
             $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1));
             $a_port['id'] = $a_pfnetworkport_DB['id'];
             $pfNetworkPort->update($a_port);
         } else {
             // Update port
             $networkports_id = $a_ports_DB['id'];
             $a_port['id'] = $a_ports_DB['id'];
             $networkPort->update($a_port);
             unset($a_port['id']);
             // Check if pfnetworkport exist.
             $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1));
             $a_port['networkports_id'] = $networkports_id;
             if (isset($a_pfnetworkport_DB['id'])) {
                 $a_port['id'] = $a_pfnetworkport_DB['id'];
                 $pfNetworkPort->update($a_port);
             } else {
                 $a_port['networkports_id'] = $networkports_id;
                 $pfNetworkPort->add($a_port);
             }
         }
     }
 }
 /**
  * Creation of a hub
  *
  * @param $pfNetworkport object Informations of the network port
  *
  * @return id of the hub (unmanageddevice)
  *
  **/
 function createHub($pfNetworkport, $a_mac)
 {
     $Netport = new NetworkPort();
     $nn = new NetworkPort_NetworkPort();
     //$pfAgentsProcesses = new PluginFusionInventoryAgentsProcesses;
     // Find in the mac connected to the if they are in hub without link port connected
     foreach ($a_mac as $ifmac) {
         $a_ports = $Netport->find("`mac`='" . $ifmac . "'");
         foreach ($a_ports as $data) {
             $ID = $nn->getOppositeContact($pfNetworkport->getNetworkPorts_id());
             if ($ID) {
                 $Netport->getFromDB($ID);
                 if ($Netport->fields["itemtype"] == $this->getType()) {
                     if ($this->fields["hub"] == "1") {
                         $a_portLink = $Netport->find("`name`='Link'\n                        AND `items_id`='" . $this->fields['id'] . "'\n                        AND `itemtype`='" . $this->getType() . "'");
                         foreach ($a_portLink as $dataLink) {
                             if ($nn->getOppositeContact($dataLink['id'])) {
                             } else {
                                 // We have founded a hub orphelin
                                 $this->disconnectDB($pfNetworkport->fields['networkports_id']);
                                 $this->disconnectDB($dataLink['id']);
                                 $nn->add(array('networkports_id_1' => $pfNetworkport->fields['networkports_id'], 'networkports_id_2' => $dataLink['id']));
                                 $this->releaseHub($this->fields['id'], $pfNetworkport, $a_mac);
                                 return $this->fields['id'];
                             }
                         }
                     }
                 }
             }
         }
     }
     // Not found, creation hub and link port
     $input = array();
     $input['hub'] = "1";
     $input['name'] = "hub";
     if (isset($_SESSION["plugin_fusioninventory_entity"])) {
         $input['entities_id'] = $_SESSION["plugin_fusioninventory_entity"];
     }
     $hub_id = $this->add($input);
     $input = array();
     $input["items_id"] = $hub_id;
     $input["itemtype"] = $this->getType();
     $input["name"] = "Link";
     $input['instantiation_type'] = "NetworkPortEthernet";
     $port_id = $Netport->add($input);
     $this->disconnectDB($pfNetworkport->fields['networkports_id']);
     $this->disconnectDB($port_id);
     if ($nn->add(array('networkports_id_1' => $pfNetworkport->fields['networkports_id'], 'networkports_id_2' => $port_id))) {
     }
     return $hub_id;
 }
 /**
  * Transfer network links
  *
  * @param $itemtype original type of transfered item
  * @param $ID original ID of the item
  * @param $newID new ID of the item
  * @param $ocs_computer if computer type OCS ID of the item if available
  **/
 function transferNetworkLink($itemtype, $ID, $newID, $ocs_computer = false)
 {
     global $DB;
     $np = new NetworkPort();
     $nn = new NetworkPort_NetworkPort();
     $query = "SELECT *\n                FROM `glpi_networkports`\n                WHERE `items_id` = '{$ID}'\n                      AND `itemtype` = '{$itemtype}'";
     if ($result = $DB->query($query)) {
         if ($DB->numrows($result) != 0) {
             switch ($this->options['keep_networklink']) {
                 // Delete netport
                 case 0:
                     // Not a copy -> delete
                     if ($ID == $newID) {
                         while ($data = $DB->fetch_array($result)) {
                             $np->delete(array('id' => $data['id']));
                         }
                         // Only case of ocs link update is needed (if netports are keep nothing to do)
                         if ($ocs_computer) {
                             $query = "UPDATE `glpi_ocslinks`\n                                  SET `import_ip` = NULL\n                                  WHERE `computers_id` = '{$ID}'";
                             $DB->query($query);
                         }
                     }
                     // Copy -> do nothing
                     break;
                     // Disconnect
                 // Disconnect
                 case 1:
                     // Not a copy -> disconnect
                     if ($ID == $newID) {
                         while ($data = $DB->fetch_array($result)) {
                             if ($nn->getFromDBForNetworkPort($data['id'])) {
                                 $nn->delete($data);
                             }
                             if ($data['netpoints_id']) {
                                 $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']);
                                 $input['id'] = $data['id'];
                                 $input['netpoints_id'] = $netpointID;
                                 $np->update($input);
                             }
                         }
                     } else {
                         // Copy -> copy netports
                         while ($data = $DB->fetch_array($result)) {
                             $data = addslashes_deep($data);
                             unset($data['id']);
                             $data['items_id'] = $newID;
                             $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']);
                             unset($np->fields);
                             $np->add($data);
                         }
                     }
                     break;
                     // Keep network links
                 // Keep network links
                 default:
                     // Copy -> Copy netpoints (do not keep links)
                     if ($ID != $newID) {
                         while ($data = $DB->fetch_array($result)) {
                             unset($data['id']);
                             $data['items_id'] = $newID;
                             $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']);
                             unset($np->fields);
                             $np->add($data);
                         }
                     } else {
                         while ($data = $DB->fetch_array($result)) {
                             // Not a copy -> only update netpoint
                             if ($data['netpoints_id']) {
                                 $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']);
                                 $input['id'] = $data['id'];
                                 $input['netpoints_id'] = $netpointID;
                                 $np->update($input);
                             }
                         }
                     }
             }
         }
     }
 }
 /**
  * Import the devices for a computer
  *
  * @param $devicetype integer : device type
  * @param $computers_id integer : glpi computer id.
  * @param $ocsid integer : ocs computer id (ID).
  * @param $ocsservers_id integer : ocs server id
  * @param $cfg_ocs array : ocs config
  * @param $import_device array : already imported devices
  * @param $import_ip array : already imported ip
  * @param $dohistory boolean : log changes ?
  *
  * @return Nothing (void).
  **/
 static function updateDevices($devicetype, $computers_id, $ocsid, $ocsservers_id, $cfg_ocs, $import_device, $import_ip, $dohistory)
 {
     global $DBocs, $DB;
     $prevalue = $devicetype . self::FIELD_SEPARATOR;
     self::checkOCSconnection($ocsservers_id);
     $types = Computer_Device::getDeviceTypes();
     $CompDevice = new Computer_Device($types[$devicetype]);
     $do_clean = false;
     switch ($devicetype) {
         case self::RAM_DEVICE:
             //Memoire
             if ($cfg_ocs["import_device_memory"]) {
                 $do_clean = true;
                 $query2 = "SELECT *\n                          FROM `memories`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     // Drop all memories and force no history
                     if (!in_array(self::IMPORT_TAG_078, $import_device)) {
                         self::addToOcsArray($computers_id, array(0 => self::IMPORT_TAG_078), "import_device");
                         // Clean memories for this computer
                         if (count($import_device)) {
                             $dohistory = false;
                             foreach ($import_device as $key => $val) {
                                 $tmp = explode(self::FIELD_SEPARATOR, $key);
                                 if (isset($tmp[1]) && $tmp[0] == self::RAM_DEVICE) {
                                     $CompDevice->delete(array('id' => $tmp[1], '_no_history' => true, '_itemtype' => 'DeviceMemory'));
                                     self::deleteInOcsArray($computers_id, $key, "import_device");
                                     unset($import_device[$key]);
                                 }
                             }
                         }
                     }
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         if (!empty($line2["CAPACITY"]) && $line2["CAPACITY"] != "No") {
                             $ram["designation"] = "";
                             if ($line2["TYPE"] != "Empty Slot" && $line2["TYPE"] != "Unknown") {
                                 $ram["designation"] = $line2["TYPE"];
                             }
                             if ($line2["DESCRIPTION"]) {
                                 if (!empty($ram["designation"])) {
                                     $ram["designation"] .= " - ";
                                 }
                                 $ram["designation"] .= $line2["DESCRIPTION"];
                             }
                             if (!is_numeric($line2["CAPACITY"])) {
                                 $line2["CAPACITY"] = 0;
                             }
                             $ram["specif_default"] = $line2["CAPACITY"];
                             if (!in_array(stripslashes($prevalue . $ram["designation"]), $import_device)) {
                                 $ram["frequence"] = $line2["SPEED"];
                                 $ram["devicememorytypes_id"] = Dropdown::importExternal('DeviceMemoryType', $line2["TYPE"]);
                                 $DeviceMemory = new DeviceMemory();
                                 $ram_id = $DeviceMemory->import($ram);
                                 if ($ram_id) {
                                     $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceMemory', 'devicememories_id' => $ram_id, 'specificity' => $line2["CAPACITY"], '_no_history' => !$dohistory));
                                     self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $ram["designation"]), "import_device");
                                 }
                             } else {
                                 $tmp = array_search(stripslashes($prevalue . $ram["designation"]), $import_device);
                                 list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                                 $CompDevice->update(array('id' => $id, 'specificity' => $line2["CAPACITY"], '_itemtype' => 'DeviceMemory'));
                                 unset($import_device[$tmp]);
                             }
                         }
                     }
                 }
             }
             break;
         case self::HDD_DEVICE:
             //Disque Dur
             if ($cfg_ocs["import_device_hdd"]) {
                 $do_clean = true;
                 $query2 = "SELECT *\n                          FROM `storages`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         if (!empty($line2["DISKSIZE"]) && preg_match("/disk|spare\\sdrive/i", $line2["TYPE"])) {
                             if ($line2["NAME"]) {
                                 $dd["designation"] = $line2["NAME"];
                             } else {
                                 if ($line2["MODEL"]) {
                                     $dd["designation"] = $line2["MODEL"];
                                 } else {
                                     $dd["designation"] = "Unknown";
                                 }
                             }
                             if (!is_numeric($line2["DISKSIZE"])) {
                                 $line2["DISKSIZE"] = 0;
                             }
                             if (!in_array(stripslashes($prevalue . $dd["designation"]), $import_device)) {
                                 $dd["specif_default"] = $line2["DISKSIZE"];
                                 $DeviceHardDrive = new DeviceHardDrive();
                                 $dd_id = $DeviceHardDrive->import($dd);
                                 if ($dd_id) {
                                     $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceHardDrive', 'deviceharddrives_id' => $dd_id, 'specificity' => $line2["DISKSIZE"], '_no_history' => !$dohistory));
                                     self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $dd["designation"]), "import_device");
                                 }
                             } else {
                                 $tmp = array_search(stripslashes($prevalue . $dd["designation"]), $import_device);
                                 list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                                 $CompDevice->update(array('id' => $id, 'specificity' => $line2["DISKSIZE"], '_itemtype' => 'DeviceHardDrive'));
                                 unset($import_device[$tmp]);
                             }
                         }
                     }
                 }
             }
             break;
         case self::DRIVE_DEVICE:
             //lecteurs
             if ($cfg_ocs["import_device_drive"]) {
                 $do_clean = true;
                 $query2 = "SELECT *\n                          FROM `storages`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         if (empty($line2["DISKSIZE"]) || !preg_match("/disk/i", $line2["TYPE"])) {
                             if ($line2["NAME"]) {
                                 $stor["designation"] = $line2["NAME"];
                             } else {
                                 if ($line2["MODEL"]) {
                                     $stor["designation"] = $line2["MODEL"];
                                 } else {
                                     $stor["designation"] = "Unknown";
                                 }
                             }
                             if (!in_array(stripslashes($prevalue . $stor["designation"]), $import_device)) {
                                 $stor["specif_default"] = $line2["DISKSIZE"];
                                 $DeviceDrive = new DeviceDrive();
                                 $stor_id = $DeviceDrive->import($stor);
                                 if ($stor_id) {
                                     $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceDrive', 'devicedrives_id' => $stor_id, '_no_history' => !$dohistory));
                                     self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $stor["designation"]), "import_device");
                                 }
                             } else {
                                 $tmp = array_search(stripslashes($prevalue . $stor["designation"]), $import_device);
                                 unset($import_device[$tmp]);
                             }
                         }
                     }
                 }
             }
             break;
         case self::PCI_DEVICE:
             //Modems
             if ($cfg_ocs["import_device_modem"]) {
                 $do_clean = true;
                 $query2 = "SELECT *\n                          FROM `modems`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         $mdm["designation"] = $line2["NAME"];
                         if (!in_array(stripslashes($prevalue . $mdm["designation"]), $import_device)) {
                             if (!empty($line2["DESCRIPTION"])) {
                                 $mdm["comment"] = $line2["TYPE"] . "\r\n" . $line2["DESCRIPTION"];
                             }
                             $DevicePci = new DevicePci();
                             $mdm_id = $DevicePci->import($mdm);
                             if ($mdm_id) {
                                 $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DevicePci', 'devicepcis_id' => $mdm_id, '_no_history' => !$dohistory));
                                 self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $mdm["designation"]), "import_device");
                             }
                         } else {
                             $tmp = array_search(stripslashes($prevalue . $mdm["designation"]), $import_device);
                             unset($import_device[$tmp]);
                         }
                     }
                 }
             }
             //Ports
             if ($cfg_ocs["import_device_port"]) {
                 $query2 = "SELECT *\n                          FROM `ports`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         $port["designation"] = "";
                         if ($line2["TYPE"] != "Other") {
                             $port["designation"] .= $line2["TYPE"];
                         }
                         if ($line2["NAME"] != "Not Specified") {
                             $port["designation"] .= " " . $line2["NAME"];
                         } else {
                             if ($line2["CAPTION"] != "None") {
                                 $port["designation"] .= " " . $line2["CAPTION"];
                             }
                         }
                         if (!empty($port["designation"])) {
                             if (!in_array(stripslashes($prevalue . $port["designation"]), $import_device)) {
                                 if (!empty($line2["DESCRIPTION"]) && $line2["DESCRIPTION"] != "None") {
                                     $port["comment"] = $line2["DESCRIPTION"];
                                 }
                                 $DevicePci = new DevicePci();
                                 $port_id = $DevicePci->import($port);
                                 if ($port_id) {
                                     $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DevicePci', 'devicepcis_id' => $port_id, '_no_history' => !$dohistory));
                                     self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $port["designation"]), "import_device");
                                 }
                             } else {
                                 $tmp = array_search(stripslashes($prevalue . $port["designation"]), $import_device);
                                 unset($import_device[$tmp]);
                             }
                         }
                     }
                 }
             }
             break;
         case self::PROCESSOR_DEVICE:
             //Processeurs :
             if ($cfg_ocs["import_device_processor"]) {
                 $do_clean = true;
                 $query = "SELECT *\n                         FROM `hardware`\n                         WHERE `ID` = '{$ocsid}'\n                         ORDER BY `ID`";
                 $result = $DBocs->query($query);
                 if ($DBocs->numrows($result) == 1) {
                     $line = $DBocs->fetch_array($result);
                     $line = clean_cross_side_scripting_deep(addslashes_deep($line));
                     for ($i = 0; $i < $line["PROCESSORN"]; $i++) {
                         $processor = array();
                         $processor["designation"] = $line["PROCESSORT"];
                         if (!is_numeric($line["PROCESSORS"])) {
                             $line["PROCESSORS"] = 0;
                         }
                         $processor["specif_default"] = $line["PROCESSORS"];
                         if (!in_array(stripslashes($prevalue . $processor["designation"]), $import_device)) {
                             $DeviceProcessor = new DeviceProcessor();
                             $proc_id = $DeviceProcessor->import($processor);
                             if ($proc_id) {
                                 $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceProcessor', 'deviceprocessors_id' => $proc_id, 'specificity' => $line["PROCESSORS"], '_no_history' => !$dohistory));
                                 self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $processor["designation"]), "import_device");
                             }
                         } else {
                             $tmp = array_search(stripslashes($prevalue . $processor["designation"]), $import_device);
                             list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                             $CompDevice->update(array('id' => $id, 'specificity' => $line["PROCESSORS"], '_itemtype' => 'DeviceProcessor'));
                             unset($import_device[$tmp]);
                         }
                     }
                 }
             }
             break;
         case self::NETWORK_DEVICE:
             //Carte reseau
             if ($cfg_ocs["import_device_iface"] || $cfg_ocs["import_ip"]) {
                 $do_clean = true;
                 //If import_ip doesn't contain _VERSION_072_, then migrate it to the new architecture
                 if (!in_array(self::IMPORT_TAG_072, $import_ip)) {
                     $import_ip = self::migrateImportIP($computers_id, $import_ip);
                 }
                 $query2 = "SELECT *\n                          FROM `networks`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 $i = 0;
                 $manually_link = false;
                 //Count old ip in GLPI
                 $count_ip = count($import_ip);
                 // Add network device
                 if ($DBocs->numrows($result2) > 0) {
                     $mac_already_imported = array();
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         if ($cfg_ocs["import_device_iface"]) {
                             $network["designation"] = $line2["DESCRIPTION"];
                             if (!in_array($line2["MACADDR"], $mac_already_imported)) {
                                 $mac_already_imported[] = $line2["MACADDR"];
                                 if (!in_array(stripslashes($prevalue . $network["designation"]), $import_device)) {
                                     if (!empty($line2["SPEED"])) {
                                         $network["bandwidth"] = $line2["SPEED"];
                                     }
                                     $DeviceNetworkCard = new DeviceNetworkCard();
                                     $net_id = $DeviceNetworkCard->import($network);
                                     if ($net_id) {
                                         $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceNetworkCard', 'devicenetworkcards_id' => $net_id, 'specificity' => $line2["MACADDR"], '_no_history' => !$dohistory));
                                         self::addToOcsArray($computers_id, array($prevalue . $devID > $prevalue . $network["designation"]), "import_device");
                                     }
                                 } else {
                                     $tmp = array_search(stripslashes($prevalue . $network["designation"]), $import_device);
                                     list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                                     $CompDevice->update(array('id' => $id, 'specificity' => $line2["MACADDR"], '_itemtype' => 'DeviceNetworkCard'));
                                     unset($import_device[$tmp]);
                                 }
                             }
                         }
                         if (!empty($line2["IPADDRESS"]) && $cfg_ocs["import_ip"]) {
                             $ocs_ips = explode(",", $line2["IPADDRESS"]);
                             $ocs_ips = array_unique($ocs_ips);
                             sort($ocs_ips);
                             //if never imported (only 0.72 tag in array), check if existing ones match
                             if ($count_ip == 1) {
                                 //get old IP in DB
                                 $querySelectIDandIP = "SELECT `id`, `ip`\n                                                  FROM `glpi_networkports`\n                                                  WHERE `itemtype` = 'Computer'\n                                                        AND `items_id` = '{$computers_id}'\n                                                        AND `mac` = '" . $line2["MACADDR"] . "'\n                                                        AND `name` = '" . $line2["DESCRIPTION"] . "'";
                                 $result = $DB->query($querySelectIDandIP);
                                 if ($DB->numrows($result) > 0) {
                                     while ($data = $DB->fetch_array($result)) {
                                         //Upate import_ip column and import_ip array
                                         self::addToOcsArray($computers_id, array($data["id"] => $data["ip"] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip");
                                         $import_ip[$data["id"]] = $data["ip"] . self::FIELD_SEPARATOR . $line2["MACADDR"];
                                     }
                                 }
                             }
                             $netport = array();
                             $netport["mac"] = $line2["MACADDR"];
                             $netport["networkinterfaces_id"] = Dropdown::importExternal('NetworkInterface', $line2["TYPE"]);
                             $netport["name"] = $line2["DESCRIPTION"];
                             $netport["items_id"] = $computers_id;
                             $netport["itemtype"] = 'Computer';
                             $netport["netmask"] = $line2["IPMASK"];
                             $netport["gateway"] = $line2["IPGATEWAY"];
                             $netport["subnet"] = $line2["IPSUBNET"];
                             $np = new NetworkPort();
                             for ($j = 0; $j < count($ocs_ips); $j++) {
                                 //First search : look for the same port (same IP and same MAC)
                                 $id_ip = array_search($ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"], $import_ip);
                                 //Second search : IP may have change, so look only for mac address
                                 if (!$id_ip) {
                                     //Browse the whole import_ip array
                                     foreach ($import_ip as $ID => $ip) {
                                         if ($ID > 0) {
                                             $tmp = explode(self::FIELD_SEPARATOR, $ip);
                                             //Port was found by looking at the mac address
                                             if (isset($tmp[1]) && $tmp[1] == $line2["MACADDR"]) {
                                                 //Remove port in import_ip
                                                 self::deleteInOcsArray($computers_id, $ID, "import_ip");
                                                 self::addToOcsArray($computers_id, array($ID => $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip");
                                                 $import_ip[$ID] = $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"];
                                                 $id_ip = $ID;
                                                 break;
                                             }
                                         }
                                     }
                                 }
                                 $netport['_no_history'] = !$dohistory;
                                 //Update already in DB
                                 if ($id_ip > 0) {
                                     $netport["ip"] = $ocs_ips[$j];
                                     $netport["logical_number"] = $j;
                                     $netport["id"] = $id_ip;
                                     $np->update($netport);
                                     unset($import_ip[$id_ip]);
                                     $count_ip++;
                                 } else {
                                     //If new IP found
                                     unset($np->fields["netpoints_id"]);
                                     unset($netport["id"]);
                                     unset($np->fields["id"]);
                                     $netport["ip"] = $ocs_ips[$j];
                                     $netport["logical_number"] = $j;
                                     $newID = $np->add($netport);
                                     //ADD to array
                                     self::addToOcsArray($computers_id, array($newID => $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip");
                                     $count_ip++;
                                 }
                             }
                         }
                     }
                 }
             }
             break;
         case self::GFX_DEVICE:
             //carte graphique
             if ($cfg_ocs["import_device_gfxcard"]) {
                 $do_clean = true;
                 $query2 = "SELECT DISTINCT(`NAME`) AS NAME,\n                                 `MEMORY`\n                          FROM `videos`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                                AND `NAME` != ''\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         $video["designation"] = $line2["NAME"];
                         if (!is_numeric($line2["MEMORY"])) {
                             $line2["MEMORY"] = 0;
                         }
                         if (!in_array(stripslashes($prevalue . $video["designation"]), $import_device)) {
                             $video["specif_default"] = $line2["MEMORY"];
                             $DeviceGraphicCard = new DeviceGraphicCard();
                             $video_id = $DeviceGraphicCard->import($video);
                             if ($video_id) {
                                 $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceGraphicCard', 'devicegraphiccards_id' => $video_id, 'specificity' => $line2["MEMORY"], '_no_history' => !$dohistory));
                                 self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $video["designation"]), "import_device");
                             }
                         } else {
                             $tmp = array_search(stripslashes($prevalue . $video["designation"]), $import_device);
                             list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                             $CompDevice->update(array('id' => $id, 'specificity' => $line2["MEMORY"], '_itemtype' => 'DeviceGraphicCard'));
                             unset($import_device[$tmp]);
                         }
                     }
                 }
             }
             break;
         case self::SND_DEVICE:
             //carte son
             if ($cfg_ocs["import_device_sound"]) {
                 $do_clean = true;
                 $query2 = "SELECT DISTINCT(`NAME`) AS NAME,\n                                 `DESCRIPTION`\n                          FROM `sounds`\n                          WHERE `HARDWARE_ID` = '{$ocsid}'\n                                AND `NAME` != ''\n                          ORDER BY `ID`";
                 $result2 = $DBocs->query($query2);
                 if ($DBocs->numrows($result2) > 0) {
                     while ($line2 = $DBocs->fetch_array($result2)) {
                         $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
                         if (!$cfg_ocs["ocs_db_utf8"] && !seems_utf8($line2["NAME"])) {
                             $line2["NAME"] = encodeInUtf8($line2["NAME"]);
                         }
                         $snd["designation"] = $line2["NAME"];
                         if (!in_array(stripslashes($prevalue . $snd["designation"]), $import_device)) {
                             if (!empty($line2["DESCRIPTION"])) {
                                 $snd["comment"] = $line2["DESCRIPTION"];
                             }
                             $DeviceSoundCard = new DeviceSoundCard();
                             $snd_id = $DeviceSoundCard->import($snd);
                             if ($snd_id) {
                                 $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceSoundCard', 'devicesoundcards_id' => $snd_id, '_no_history' => !$dohistory));
                                 self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $snd["designation"]), "import_device");
                             }
                         } else {
                             $id = array_search(stripslashes($prevalue . $snd["designation"]), $import_device);
                             unset($import_device[$id]);
                         }
                     }
                 }
             }
             break;
     }
     // Delete Unexisting Items not found in OCS
     if ($do_clean && count($import_device)) {
         foreach ($import_device as $key => $val) {
             if (!(strpos($key, $devicetype . '$$') === false)) {
                 list($type, $id) = explode(self::FIELD_SEPARATOR, $key);
                 $CompDevice->delete(array('id' => $id, '_itemtype' => $types[$devicetype], '_no_history' => !$dohistory));
                 self::deleteInOcsArray($computers_id, $key, "import_device");
             }
         }
     }
     if ($do_clean && count($import_ip) && $devicetype == self::NETWORK_DEVICE) {
         foreach ($import_ip as $key => $val) {
             if ($key > 0) {
                 $netport = new NetworkPort();
                 $netport->delete(array('id' => $key));
                 self::deleteInOcsArray($computers_id, $key, "import_ip");
             }
         }
     }
     //Alimentation
     //Carte mere
 }