/**
  * Update devices with values get by SNMP
  *
  * @param $ID_Device : ID of device
  * @param $type : type of device (NETWORKING_TYPE, PRINTER_TYPE ...)
  * @param $oidsModel : oid list from model SNMP
  * @param $oidvalues : list of values from agent query
  * @param $Array_Object_TypeNameConstant : array with oid => constant in relation with fields to update
  *
  * @return $oidList : array with ports object name and oid
  *
  **/
 static function updateGLPIDevice($ID_Device, $type, $oidsModel, $oidvalues, $Array_Object_TypeNameConstant)
 {
     global $DB, $LANG, $CFG_GLPI, $FUSIONINVENTORY_MAPPING;
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs = new PluginFusioninventoryLogs();
     }
     if ($_SESSION['fusioninventory_logs'] == "1") {
         $logs->write("fusioninventory_fullsync", ">>>>>>>>>> Update devices values <<<<<<<<<<", $type, $ID_Device, 1);
     }
     // Update 'last_fusioninventory_update' field
     $query = "UPDATE ";
     if ($type == NETWORKING_TYPE) {
         $query .= "`glpi_plugin_fusioninventory_networking`\n             SET `last_fusioninventory_update`='" . date("Y-m-d H:i:s") . "',\n                 `last_PID_update`='" . $_SESSION['FK_process'] . "'\n             WHERE `FK_networking`='" . $ID_Device . "';";
     }
     if ($type == PRINTER_TYPE) {
         $query .= "`glpi_plugin_fusioninventory_printers`\n             SET `last_fusioninventory_update`='" . date("Y-m-d H:i:s") . "'\n             WHERE `FK_printers`='" . $ID_Device . "';";
     }
     $DB->query($query);
     foreach ($Array_Object_TypeNameConstant as $oid => $link) {
         if (!preg_match("/\\.\$/", $oid)) {
             // SNMPGet ONLY
             if (isset($oidvalues[$oid][""])) {
                 if (isset($FUSIONINVENTORY_MAPPING[$type][$link]['dropdown']) and !empty($FUSIONINVENTORY_MAPPING[$type][$link]['dropdown'])) {
                     $oidvalues[$oid][""] = PluginFusioninventorySNMP::hex_to_string($oidvalues[$oid][""]);
                     if ($FUSIONINVENTORY_MAPPING[$type][$link]['dropdown'] == "glpi_dropdown_model_networking") {
                         $oidvalues[$oid][""] = Dropdown::importExternal("NetworkEquipmentModel", $oidvalues[$oid][""], 0, array("manufacturer" => $oidvalues[$oid][""]));
                     } else {
                         $oidvalues[$oid][""] = Dropdown::importExternal(getItemTypeForTable($FUSIONINVENTORY_MAPPING[$type][$link]['dropdown']), $oidvalues[$oid][""], 0);
                     }
                 }
                 switch ($type) {
                     case NETWORKING_TYPE:
                         $Field = "FK_networking";
                         if ($FUSIONINVENTORY_MAPPING[$type][$link]['table'] == "glpi_networking") {
                             $Field = "ID";
                         }
                         break;
                     case PRINTER_TYPE:
                         $Field = "FK_printers";
                         if ($FUSIONINVENTORY_MAPPING[$type][$link]['table'] == "glpi_printers") {
                             $Field = "ID";
                         }
                         break;
                 }
                 if ($_SESSION['fusioninventory_logs'] == "1") {
                     $logs->write("fusioninventory_fullsync", $link . " = " . $oidvalues[$oid][""], $type, $ID_Device, 1);
                 }
                 // * Memory
                 if ($link == "ram" or $link == "memory") {
                     $oidvalues[$oid][""] = ceil($oidvalues[$oid][""] / 1024 / 1024);
                     if ($type == PRINTER_TYPE) {
                         $oidvalues[$oid][""] .= " MB";
                     }
                 }
                 if ($link == 'macaddr') {
                     $MacAddress = PluginFusioninventoryIfmac::ifmacwalk_ifmacaddress($oidvalues[$oid][""]);
                     $oidvalues[$oid][""] = $MacAddress;
                 }
                 // Convert hexa in string
                 $oidvalues[$oid][""] = PluginFusioninventorySNMP::hex_to_string($oidvalues[$oid][""]);
                 if (strstr($oidvalues[$oid][""], "noSuchName")) {
                     // NO Update field in GLPI
                 } else {
                     if ($FUSIONINVENTORY_MAPPING[$type][$link]['table'] == "glpi_plugin_fusioninventory_printers_cartridges") {
                         // * Printers cartridges
                         $object_name_clean = str_replace("MAX", "", $link);
                         $object_name_clean = str_replace("REMAIN", "", $object_name_clean);
                         if (strstr($link, "MAX")) {
                             $printer_cartridges_max_remain[$object_name_clean]["MAX"] = $oidvalues[$oid][""];
                         }
                         if (strstr($link, "REMAIN")) {
                             $printer_cartridges_max_remain[$object_name_clean]["REMAIN"] = $oidvalues[$oid][""];
                         }
                         if (isset($printer_cartridges_max_remain[$object_name_clean]["MAX"]) and isset($printer_cartridges_max_remain[$object_name_clean]["REMAIN"])) {
                             $pourcentage = ceil(100 * $printer_cartridges_max_remain[$object_name_clean]["REMAIN"] / $printer_cartridges_max_remain[$object_name_clean]["MAX"]);
                             // Test existance of row in MySQl
                             $query_sel = "SELECT *\n                                FROM " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                WHERE " . $Field . "='" . $ID_Device . "'\n                                      AND `object_name`='" . $object_name_clean . "';";
                             $result_sel = $DB->query($query_sel);
                             if ($DB->numrows($result_sel) == "0") {
                                 $queryInsert = "INSERT INTO " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                                 (" . $Field . ",object_name)\n                                     VALUES('" . $ID_Device . "', '" . $object_name_clean . "');";
                                 $DB->query($queryInsert);
                             }
                             $queryUpdate = "UPDATE " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                  SET " . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "='" . $pourcentage . "'\n                                  WHERE " . $Field . "='" . $ID_Device . "'\n                                        AND `object_name`='" . $object_name_clean . "';";
                             $DB->query($queryUpdate);
                             unset($printer_cartridges_max_remain[$object_name_clean]["MAX"]);
                             unset($printer_cartridges_max_remain[$object_name_clean]["REMAIN"]);
                         } else {
                             // Test existance of row in MySQl
                             $query_sel = "SELECT *\n                                FROM " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                WHERE " . $Field . "='" . $ID_Device . "'\n                                      AND `object_name`='" . $link . "';";
                             $result_sel = $DB->query($query_sel);
                             if ($DB->numrows($result_sel) == "0") {
                                 $queryInsert = "INSERT INTO " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                                 (" . $Field . ",object_name)\n                                     VALUES('" . $ID_Device . "', '" . $link . "');";
                                 $DB->query($queryInsert);
                             }
                             $queryUpdate = "UPDATE " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                  SET " . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "='" . $oidvalues[$oid][""] . "'\n                                  WHERE " . $Field . "='" . $ID_Device . "'\n                                        AND `object_name`='" . $link . "';";
                             $DB->query($queryUpdate);
                         }
                     } else {
                         if (strstr($link, "pagecounter")) {
                             // Detect if the script has wroten a line for the counter today
                             // (if yes, don't touch, else add line)
                             $today = strftime("%Y-%m-%d", time());
                             $query_line = "SELECT *\n                              FROM `glpi_plugin_fusioninventory_printers_history`\n                              WHERE `date` LIKE '" . $today . "%'\n                                    AND `FK_printers`='" . $ID_Device . "';";
                             $result_line = $DB->query($query_line);
                             if ($DB->numrows($result_line) == "0") {
                                 if (empty($oidvalues[$oid][""])) {
                                     $oidvalues[$oid][""] = 0;
                                 }
                                 $queryInsert = "INSERT INTO " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                              (" . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "," . $Field . ", `date`)\n                                  VALUES('" . $oidvalues[$oid][""] . "','" . $ID_Device . "', '" . $today . "');";
                                 $DB->query($queryInsert);
                             } else {
                                 $data_line = $DB->fetch_assoc($result_line);
                                 if ($data_line[$FUSIONINVENTORY_MAPPING[$type][$link]['field']] == "0") {
                                     if (empty($oidvalues[$oid][""])) {
                                         $oidvalues[$oid][""] = 0;
                                     }
                                     $queryUpdate = "UPDATE " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                     SET " . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "='" . $oidvalues[$oid][""] . "'\n                                     WHERE " . $Field . "='" . $ID_Device . "'\n                                           AND `date` LIKE '" . $today . "%';";
                                     $DB->query($queryUpdate);
                                 }
                             }
                         } else {
                             if ($link == "cpuuser" or $link == "cpusystem") {
                                 if ($object_name == "cpuuser") {
                                     $cpu_values['cpuuser'] = $oidvalues[$oid][""];
                                 }
                                 if ($object_name == "cpusystem") {
                                     $cpu_values['cpusystem'] = $oidvalues[$oid][""];
                                 }
                                 if (isset($cpu_values['cpuuser']) and isset($cpu_values['cpusystem'])) {
                                     $queryUpdate = "UPDATE " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                                  SET " . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "='" . ($cpu_values['cpuuser'] + $cpu_values['cpusystem']) . "'\n                                  WHERE " . $Field . "='" . $ID_Device . "';";
                                     $DB->query($queryUpdate);
                                     unset($cpu_values);
                                 }
                             } else {
                                 if ($FUSIONINVENTORY_MAPPING[$type][$link]['table'] != "") {
                                     if ($FUSIONINVENTORY_MAPPING[$type][$link]['field'] == "cpu" and empty($oidvalues[$oid][""])) {
                                         $SNMPValue = 0;
                                     }
                                     if (strstr($FUSIONINVENTORY_MAPPING[$type][$link]['table'], "glpi_plugin_fusioninventory")) {
                                         $queryUpdate = "UPDATE " . $FUSIONINVENTORY_MAPPING[$type][$link]['table'] . "\n                  SET " . $FUSIONINVENTORY_MAPPING[$type][$link]['field'] . "='" . $oidvalues[$oid][""] . "'\n                  WHERE " . $Field . "='" . $ID_Device . "'";
                                         $DB->query($queryUpdate);
                                     } else {
                                         $commonitem = new commonitem();
                                         $commonitem->setType($type, true);
                                         $tableau[$Field] = $ID_Device;
                                         $tableau[$FUSIONINVENTORY_MAPPING[$type][$link]['field']] = $oidvalues[$oid][""];
                                         $commonitem->obj->update($tableau);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 function ShowProcesses()
 {
     global $DB, $LANG;
     $ci = new commonitem();
     echo "<table class='tab_cadre' cellpadding='5' width='1150'>";
     $this->ShowHeader();
     $i = 0;
     $query = "SELECT * FROM `" . $this->table . "`\n         ORDER BY `process_number` DESC";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             $i++;
             if ($i == '8') {
                 echo "<tr class='tab_bg_2'>";
                 echo "<td colspan='14' height='5'></td>";
                 echo "</tr>";
                 $this->ShowHeader();
                 $i = 0;
             }
             echo "<tr class='tab_bg_2'>";
             echo "<td colspan='14' height='5'></td>";
             echo "</tr>";
             echo "<tr class='tab_bg_1 center'>";
             echo "<td rowspan='2'>";
             echo $data['process_number'];
             echo "</td>";
             echo "<td rowspan='2'>";
             $ci->getFromDB(PLUGIN_FUSIONINVENTORY_SNMP_AGENTS, $data['FK_agent']);
             echo $ci->getLink(1);
             echo "</td>";
             echo "<td rowspan='2'>";
             switch ($data['status']) {
                 case 3:
                     echo "<img src='../pics/export.png' />";
                     break;
                 case 2:
                     echo "<img src='../pics/wait.png' />";
                     break;
                 case 1:
                     echo "<img src='../pics/ok2.png' />";
                     break;
             }
             echo "</td>";
             echo "<td rowspan='2'>";
             echo convDateTime($data['start_time']);
             echo "</td>";
             echo "<td rowspan='2'>";
             if ($data['start_time'] != '0000-00-00 00:00:00' and $data['end_time'] != '0000-00-00 00:00:00') {
                 $duree_timestamp = strtotime($data['end_time']) - strtotime($data['start_time']);
                 echo timestampToString($duree_timestamp);
             } else {
                 echo '-';
             }
             echo "</td>";
             echo "<td height='28'>";
             if ($data['start_time_discovery'] != '0000-00-00 00:00:00') {
                 if ($data['end_time_discovery'] != '0000-00-00 00:00:00') {
                     echo "<img src='../pics/export.png' />";
                 } else {
                     echo "<img src='../pics/wait.png' />";
                 }
             } else {
                 if ($data['discovery_core'] != '0') {
                     echo "<img src='../pics/ok2.png' />";
                 }
             }
             echo "</td>";
             echo "<td>";
             if ($data['start_time_discovery'] != '0000-00-00 00:00:00') {
                 echo convDateTime($data['start_time_discovery']);
             } else {
                 echo '-';
             }
             echo "</td>";
             echo "<td>";
             if ($data['start_time_discovery'] != '0000-00-00 00:00:00' and $data['end_time_discovery'] != '0000-00-00 00:00:00') {
                 $duree_timestamp = strtotime($data['end_time_discovery']) - strtotime($data['start_time_discovery']);
                 echo timestampToString($duree_timestamp);
             } else {
                 echo '-';
             }
             echo "</td>";
             echo "<td>";
             echo $data['discovery_core'];
             echo "</td>";
             echo "<td>";
             echo $data['discovery_threads'];
             echo "</td>";
             echo "<td>";
             echo $data['discovery_nb_ip'];
             echo "</td>";
             echo "<td>";
             echo $data['discovery_nb_found'];
             if ($data['discovery_nb_error'] > 0) {
                 echo " / <a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php?process_number=" . $data['process_number'] . "&agent_type=NETDISCOVERY'>\n                  <font color='#ff0000'>" . $data['discovery_nb_error'] . "</font></a>";
             } else {
                 echo " / " . $data['discovery_nb_error'];
             }
             echo "</td>";
             echo "<td>";
             echo $data['discovery_nb_exists'];
             echo "</td>";
             echo "<td>";
             echo $data['discovery_nb_import'];
             echo "</td>";
             echo "</tr>";
             echo "<tr class='tab_bg_1 center'>";
             echo "<td height='28'>";
             if ($data['start_time_query'] != '0000-00-00 00:00:00') {
                 if ($data['end_time_query'] != '0000-00-00 00:00:00') {
                     echo "<img src='../pics/export.png' />";
                 } else {
                     echo "<img src='../pics/wait.png' />";
                 }
             } else {
                 if ($data['query_core'] != '0') {
                     echo "<img src='../pics/ok2.png' />";
                 }
             }
             echo "</td>";
             echo "<td>";
             if ($data['start_time_query'] != '0000-00-00 00:00:00') {
                 echo convDateTime($data['start_time_query']);
             } else {
                 echo '-';
             }
             echo "</td>";
             echo "<td>";
             if ($data['start_time_query'] != '0000-00-00 00:00:00' and $data['end_time_query'] != '0000-00-00 00:00:00') {
                 $duree_timestamp = strtotime($data['end_time_query']) - strtotime($data['start_time_query']);
                 echo timestampToString($duree_timestamp);
             } else {
                 echo '-';
             }
             echo "</td>";
             echo "<td>";
             echo $data['query_core'];
             echo "</td>";
             echo "<td>";
             echo $data['query_threads'];
             echo "</td>";
             echo "<td>";
             echo $data['query_nb_query'];
             echo "</td>";
             echo "<td>";
             if ($data['query_nb_error'] > 0) {
                 echo "<a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php?process_number=" . $data['process_number'] . "&agent_type=SNMPQUERY'>\n                  <font color='#ff0000'>" . $data['query_nb_error'] . "</font></a>";
             } else {
                 echo $data['query_nb_error'];
             }
             echo "</td>";
             echo "<td>";
             if ($data['query_nb_connections_created'] > 0) {
                 echo "<a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php?process_number=" . $data['process_number'] . "&amp;created=1'>\n                  " . $data['query_nb_connections_created'] . "</a>";
             } else {
                 echo $data['query_nb_connections_created'];
             }
             echo "</td>";
             echo "<td>";
             if ($data['query_nb_connections_deleted'] > 0) {
                 echo "<a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php?process_number=" . $data['process_number'] . "&amp;created=0'>\n                  " . $data['query_nb_connections_deleted'] . "</a>";
             } else {
                 echo $data['query_nb_connections_deleted'];
             }
             echo "</td>";
             echo "</tr>";
         }
     }
     echo "<tr class='tab_bg_2'>";
     echo "<td colspan='14' height='5'></td>";
     echo "</tr>";
     echo "</table>";
 }
 function ShowErrors($input = '')
 {
     global $DB, $LANG;
     $ci = new commonitem();
     echo "<table class='tab_cadre' cellpadding='5' width='950'>";
     echo "<tr class='tab_bg_1'>";
     echo "<th>";
     echo $LANG['plugin_fusioninventory']["processes"][1];
     echo " <a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php'>(" . $LANG['common'][66] . ")</a>";
     echo "</th>";
     echo "<th>";
     echo $LANG['common'][27];
     echo "</th>";
     echo "<th>";
     echo $LANG['common'][17];
     echo "</th>";
     echo "<th>";
     echo $LANG['common'][1];
     echo "</th>";
     echo "<th>";
     echo $LANG['plugin_fusioninventory']["errors"][104];
     echo "</th>";
     echo "</tr>";
     $condition = "";
     if (isset($input['process_number'])) {
         $condition = "WHERE `process_number`='" . $input['process_number'] . "'";
         if (isset($input['agent_type'])) {
             $condition .= " AND agent_type='" . $input['agent_type'] . "' ";
         }
     }
     $query = "SELECT * FROM `" . $this->table . "`\n         " . $condition . "\n         ORDER BY `process_number` DESC";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             echo "<tr class='tab_bg_1 center'>";
             echo "<td>";
             echo $data['process_number'];
             echo "</td>";
             echo "<td>";
             echo convDateTime($data['date']);
             echo "</td>";
             echo "<td>";
             if ($data['agent_type'] == "SNMPQUERY") {
                 echo $LANG['plugin_fusioninventory']["processes"][27];
             } else {
                 if ($data['agent_type'] == "NETDISCOVERY") {
                     echo $LANG['plugin_fusioninventory']["processes"][26];
                 }
             }
             echo "</td>";
             echo "<td>";
             $ci->getFromDB($data['device_type'], $data['on_device']);
             echo $ci->getLink(1);
             echo "</td>";
             echo "<td>";
             echo $data['error_message'];
             echo "</td>";
             echo "</tr>";
         }
     }
     echo "</table>";
 }
 function import_netdiscovery($p_xml, $agentKey)
 {
     global $DB, $LANG;
     $test = '';
     $p_criteria = array();
     $walks = new PluginFusioninventoryWalk();
     $ptap = new PluginFusioninventoryAgentsProcesses();
     $pta = new PluginFusioninventoryAgents();
     $config_discovery = new PluginFusioninventoryConfig();
     $np = new Netport();
     $ptud = new PluginFusioninventoryUnknownDevice();
     if (isset($p_xml->AGENT->START)) {
         $ptap->updateProcess($p_xml->PROCESSNUMBER, array('start_time_discovery' => date("Y-m-d H:i:s")));
     } else {
         if (isset($p_xml->AGENT->END)) {
             $ptap->updateProcess($p_xml->PROCESSNUMBER, array('end_time_discovery' => date("Y-m-d H:i:s")));
         } else {
             if (isset($p_xml->AGENT->EXIT)) {
                 $ptap->endProcess($p_xml->PROCESSNUMBER, date("Y-m-d H:i:s"));
             } else {
                 if (isset($p_xml->AGENT->NBIP)) {
                     $ptap->updateProcess($p_xml->PROCESSNUMBER, array('discovery_nb_ip' => $p_xml->AGENT->NBIP));
                 }
             }
         }
     }
     if (isset($p_xml->AGENT->AGENTVERSION)) {
         $agent = $pta->InfosByKey($agentKey);
         $agent['fusioninventory_agent_version'] = $p_xml->AGENT->AGENTVERSION;
         $agent['last_agent_update'] = date("Y-m-d H:i:s");
         $pta->update($agent);
     }
     $walkdata = '';
     $count_discovery_devices = 0;
     foreach ($p_xml->DEVICE as $discovery) {
         $count_discovery_devices++;
     }
     $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_found' => $count_discovery_devices));
     foreach ($p_xml->DEVICE as $discovery) {
         if ($discovery->MODELSNMP != "") {
             $query = "SELECT *\n                      FROM `glpi_plugin_fusioninventory_model_infos`\n                      WHERE `discovery_key`='" . $discovery->MODELSNMP . "'\n                      LIMIT 0,1;";
             $result = $DB->query($query);
             $data = $DB->fetch_assoc($result);
             $FK_model = $data['ID'];
         } else {
             $FK_model = 0;
         }
         $discovery->MAC = strtolower($discovery->MAC);
         if (empty($FK_model)) {
             $FK_model = 0;
         }
         unset($p_criteria);
         $p_criteria['ip'] = $discovery->IP;
         if (!empty($discovery->NETBIOSNAME)) {
             $p_criteria['name'] = $discovery->NETBIOSNAME;
         } else {
             if (!empty($discovery->SNMPHOSTNAME)) {
                 $p_criteria['name'] = $discovery->SNMPHOSTNAME;
             }
         }
         $p_criteria['serial'] = $discovery->SERIAL;
         $p_criteria['macaddr'] = $discovery->MAC;
         $discovery_criteria = PluginFusioninventoryDiscovery::criteria($p_criteria);
         if (!$discovery_criteria) {
             $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_import' => '1'));
             // Add in unknown device
             $ptud->getEmpty();
             if (!empty($discovery->NETBIOSNAME)) {
                 $ptud->fields['name'] = $discovery->NETBIOSNAME;
             } else {
                 if (!empty($discovery->SNMPHOSTNAME)) {
                     $ptud->fields['name'] = $discovery->SNMPHOSTNAME;
                 }
             }
             $ptud->fields['dnsname'] = $discovery->DNSHOSTNAME;
             $ptud->fields['FK_entities'] = $discovery->ENTITY;
             $ptud->fields['serial'] = $discovery->SERIAL;
             $ptud->fields['contact'] = $discovery->USERSESSION;
             if (!empty($discovery->WORKGROUP)) {
                 $ptud->fields['domain'] = Dropdown::importExternal("Domain", $discovery->WORKGROUP, $discovery->ENTITY);
             }
             $ptud->fields['comments'] = $discovery->DESCRIPTION;
             $ptud->fields['type'] = $discovery->TYPE;
             $ptud->fields['FK_model_infos'] = $FK_model;
             $ptud->fields['FK_snmp_connection'] = $discovery->AUTHSNMP;
             if ($discovery->AUTHSNMP != "") {
                 $ptud->fields['snmp'] = 1;
             }
             $ptud->fields['location'] = 0;
             $ptud->fields['deleted'] = 0;
             if ($ptud->fields['domain'] == '') {
                 $ptud->fields['domain'] = 0;
             }
             if ($ptud->fields['type'] == '') {
                 $ptud->fields['type'] = 0;
             }
             if ($ptud->fields['snmp'] == '') {
                 $ptud->fields['snmp'] = 0;
             }
             if ($ptud->fields['FK_model_infos'] == '') {
                 $ptud->fields['FK_model_infos'] = 0;
             }
             if ($ptud->fields['FK_snmp_connection'] == '') {
                 $ptud->fields['FK_snmp_connection'] = 0;
             }
             if ($ptud->fields['accepted'] == '') {
                 $ptud->fields['accepted'] = 0;
             }
             $explodeprocess = explode("/", $_SESSION['glpi_plugin_fusioninventory_processnumber']);
             $ptud->fields['FK_agent'] = intval($explodeprocess[1]);
             $ptud->fields['hub'] = 0;
             $data = $ptud->fields;
             unset($data['ID']);
             $newID = $ptud->add($data);
             unset($data);
             // Add networking_port
             $port_add["on_device"] = $newID;
             $port_add["device_type"] = PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN;
             $port_add["ifaddr"] = $discovery->IP;
             $port_add['ifmac'] = $discovery->MAC;
             $port_add['name'] = $discovery->NETPORTVENDOR;
             $port_ID = $np->add($port_add);
             unset($port_add);
         } else {
             # Update device
             //echo "discovery_criteria :".$discovery_criteria;
             $a_device = explode("||", $discovery_criteria);
             // $a_device[0] == id, $a_device[1] = type
             $ci = new commonitem();
             $ci->getFromDB($a_device[1], $a_device[0]);
             $a_lockable = PluginFusioninventoryLock::getLockFields($a_device[1], $a_device[0]);
             $data = array();
             $data['ID'] = $ci->getField('ID');
             if ($a_device[1] == PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN) {
                 $data['FK_snmp_connection'] = 0;
                 if ($ci->getField('name') && !in_array('name', $a_lockable)) {
                     if (!empty($discovery->NETBIOSNAME)) {
                         $data['name'] = $discovery->NETBIOSNAME;
                     } else {
                         if (!empty($discovery->SNMPHOSTNAME)) {
                             $data['name'] = $discovery->SNMPHOSTNAME;
                         }
                     }
                 }
                 if ($ci->getField('dnsname') && !in_array('dnsname', $a_lockable)) {
                     $data['dnsname'] = $discovery->DNSHOSTNAME;
                 }
                 if ($ci->getField('FK_entities') && !in_array('FK_entities', $a_lockable)) {
                     $data['FK_entities'] = $discovery->ENTITY;
                 }
                 if ($ci->getField('serial') && !in_array('serial', $a_lockable)) {
                     $data['serial'] = $discovery->SERIAL;
                 }
                 if ($ci->getField('contact') && !in_array('contact', $a_lockable)) {
                     $data['contact'] = $discovery->USERSESSION;
                 }
                 if ($ci->getField('domain') && !in_array('domain', $a_lockable)) {
                     $data['domain'] = 0;
                     if (!empty($discovery->WORKGROUP)) {
                         $data['domain'] = Dropdown::importExternal("Domain", $discovery->WORKGROUP, $discovery->ENTITY);
                     }
                 }
                 if ($ci->getField('FK_snmp_connection') && !in_array('FK_snmp_connection', $a_lockable)) {
                     $data['FK_snmp_connection'] = $discovery->AUTHSNMP;
                 }
                 if ($ci->getField('snmp') && !in_array('snmp', $a_lockable)) {
                     $data['snmp'] = 0;
                     if ($discovery->AUTHSNMP != "") {
                         $data['snmp'] = 1;
                     }
                 }
                 $data['type'] = $discovery->TYPE;
                 $explodeprocess = explode("/", $_SESSION['glpi_plugin_fusioninventory_processnumber']);
                 $data['FK_agent'] = intval($explodeprocess[1]);
             }
             if ($ci->getField('FK_model_infos') && !in_array('FK_model_infos', $a_lockable)) {
                 $data['FK_model_infos'] = $FK_model;
             }
             if ($ci->getField('comments') && !in_array('comments', $a_lockable)) {
                 $data['comments'] = $discovery->DESCRIPTION;
             }
             if ($a_device[1] == NETWORKING_TYPE) {
                 if (!in_array('ifaddr', $a_lockable)) {
                     $data["ifaddr"] = $discovery->IP;
                 }
                 if (!in_array('ifmac', $a_lockable)) {
                     $data['ifmac'] = $discovery->MAC;
                 }
             } else {
                 // TODO: manage ports
                 $np = new Netport();
                 $query = "SELECT ID FROM glpi_networking_ports\n                  WHERE (on_device = '" . $a_device[0] . "' AND device_type = '" . $a_device[1] . "')\n                     AND `ifaddr` NOT IN ('', '127.0.0.1')\n                  ORDER BY name, logical_number";
                 if ($result = $DB->query($query)) {
                     if ($DB->numrows($result) == 1) {
                         $data2 = $DB->fetch_assoc($result);
                         $np->getFromDB($data2["ID"]);
                         $port = array();
                         $port['ID'] = $data2["ID"];
                         $port["ifaddr"] = $discovery->IP;
                         $port['ifmac'] = $discovery->MAC;
                         $port['name'] = $discovery->NETPORTVENDOR;
                         $np->update($port);
                     } else {
                         if ($DB->numrows($result) > 1) {
                             $ptae = new PluginFusioninventoryAgentsErrors();
                             $error_input['ID'] = $a_device[0];
                             $error_input['TYPE'] = $a_device[1];
                             $error_input['MESSAGE'] = 'Unable to determine network port of device to update with values : ' . $discovery->IP . '(ip),
                     ' . $discovery->MAC . '(mac), ' . $discovery->NETPORTVENDOR . '(name)';
                             $error_input['agent_type'] = 'NETDISCOVERY';
                             $ptae->addError($error_input);
                         }
                     }
                 }
             }
             $ci->obj->update($data);
             $ptap->updateProcess($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('discovery_nb_exists' => '1'));
         }
     }
 }