function showForm($input = '')
 {
     global $DB, $LANG, $CFG_GLPI, $INFOFORM_PAGES;
     $CommonItem = new CommonItem();
     $np = new Netport();
     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'][1];
     echo "</th>";
     echo "<th>";
     echo $LANG['joblist'][0];
     echo "</th>";
     echo "<th>";
     echo $LANG['common'][1];
     echo "</th>";
     echo "</tr>";
     if (!isset($input['process_number'])) {
         $condition = '';
     } else {
         $condition = "WHERE `process_number`='" . $input['process_number'] . "'";
         if (isset($input['created'])) {
             $condition .= " AND `creation`='" . $input['created'] . "' ";
         }
     }
     $query = "SELECT * FROM `" . $this->table . "`\n         " . $condition . "\n         ORDER BY `date`DESC , `process_number` DESC";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             echo "<tr class='tab_bg_1 center'>";
             echo "<td>";
             echo "<a href='" . GLPI_ROOT . "/plugins/fusioninventory/front/agents.processes.form.php?h_process_number=" . $data['process_number'] . "'>" . $data['process_number'] . "</a>";
             echo "</td>";
             echo "<td>";
             echo convDateTime($data['date']);
             echo "</td>";
             echo "<td>";
             $np->getFromDB($data['FK_port_source']);
             $CommonItem->getFromDB($np->fields["device_type"], $np->fields["on_device"]);
             $link1 = $CommonItem->getLink(1);
             $link = "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networking.port.php?ID=" . $np->fields["ID"] . "\">";
             if (rtrim($np->fields["name"]) != "") {
                 $link .= $np->fields["name"];
             } else {
                 $link .= $LANG['common'][0];
             }
             $link .= "</a>";
             echo $link . " " . $LANG['networking'][25] . " " . $link1;
             echo "</td>";
             echo "<td>";
             if ($data['creation'] == '1') {
                 echo "<img src='" . GLPI_ROOT . "/plugins/fusioninventory/pics/connection_ok.png'/>";
             } else {
                 echo "<img src='" . GLPI_ROOT . "/plugins/fusioninventory/pics/connection_notok.png'/>";
             }
             echo "</td>";
             echo "<td>";
             $np->getFromDB($data['FK_port_destination']);
             $CommonItem->getFromDB($np->fields["device_type"], $np->fields["on_device"]);
             $link1 = $CommonItem->getLink(1);
             $link = "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networking.port.php?ID=" . $np->fields["ID"] . "\">";
             if (rtrim($np->fields["name"]) != "") {
                 $link .= $np->fields["name"];
             } else {
                 $link .= $LANG['common'][0];
             }
             $link .= "</a>";
             echo $link . " " . $LANG['networking'][25] . " " . $link1;
             echo "</td>";
             echo "</tr>";
         }
     }
     echo "</table>";
 }
 function hubNetwork($p_oPort)
 {
     global $DB;
     $nw = new Netwire();
     $np = new Netport();
     $nn = new NetworkPort_NetworkPort();
     // List of macs : $p_oPort->getPortsToConnect
     // recherche dans unknown_device table le hub
     // qui a un port connecte sur le $port_ID
     // Get port connected on switch port
     if ($ID = $nw->getOppositeContact($p_oPort->getValue('ID'))) {
         $np->getFromDB($ID);
         if ($np->fields["device_type"] == $this->type) {
             $this->getFromDB($np->fields["on_device"]);
             if ($this->fields["hub"] == "1") {
                 // We will update ports and wire
                 return;
             }
         }
     }
     // sinon on cree un nouveau unknown_device type hub
     // + creation des ports qui sont connectes aux mac
     $input = array();
     $input['hub'] = "1";
     $input['name'] = "hub";
     $id_unknown = $this->add($input);
     $input = array();
     $input["on_device"] = $id_unknown;
     $input["device_type"] = $this->type;
     $input["name"] = "Link";
     $id_port = $np->add($input);
     $nn->add(array('networkports_id_1' => $p_oPort->getValue('ID'), 'networkports_id_2' => $id_port));
     foreach ($p_oPort->getMacsToConnect() as $ifmac) {
         $input = array();
         $input["on_device"] = $id_unknown;
         $input["device_type"] = $this->type;
         $id_port = $np->add($input);
         // TODO : recherche le port qui a cet $ifmac
         $query = "SELECT * FROM `glpi_networking_ports`\n            WHERE `ifmac` = '" . $ifmac . "' ";
         $result = $DB->query($query);
         if ($DB->numrows($result) == 1) {
             $line = $DB->fetch_assoc($result);
             $nn->add(array('networkports_id_1' => $line['ID'], 'networkports_id_2' => $id_port));
         } else {
             // Create device inconnu
         }
     }
 }
 static function showHistory($ID_port)
 {
     global $DB, $LANG, $INFOFORM_PAGES, $CFG_GLPI;
     include GLPI_ROOT . "/plugins/fusioninventory/inc_constants/snmp.mapping.constant.php";
     $CommonItem = new CommonItem();
     $np = new Netport();
     $query = "\n         SELECT * FROM(\n            SELECT * FROM (\n               SELECT ID, date as date, process_number as process_number,\n               FK_port_source, FK_port_destination,\n               creation as Field, NULL as old_value, NULL as new_value\n\n               FROM glpi_plugin_fusioninventory_snmphistoryconnections\n               WHERE `FK_port_source`='" . $ID_port . "'\n                  OR `FK_port_destination`='" . $ID_port . "'\n               ORDER BY date DESC\n               LIMIT 0,30\n               )\n            AS DerivedTable1\n            UNION ALL\n            SELECT * FROM (\n               SELECT ID, date_mod as date, FK_process as process_number,\n               FK_ports AS FK_port_source, NULL as FK_port_destination,\n               Field, old_value, new_value\n\n               FROM glpi_plugin_fusioninventory_snmphistories\n               WHERE `FK_ports`='" . $ID_port . "'\n               ORDER BY date DESC\n               LIMIT 0,30\n               )\n            AS DerivedTable2)\n         AS MainTable\n         ORDER BY date DESC, ID DESC\n         LIMIT 0,30";
     //echo $query."<br/>";
     $text = "<table class='tab_cadre' cellpadding='5' width='950'>";
     $text .= "<tr class='tab_bg_1'>";
     $text .= "<th colspan='8'>";
     $text .= "Historique";
     $text .= "</th>";
     $text .= "</tr>";
     $text .= "<tr class='tab_bg_1'>";
     $text .= "<th>" . $LANG['plugin_fusioninventory']["snmp"][50] . "</th>";
     $text .= "<th>" . $LANG["common"][1] . "</th>";
     $text .= "<th>" . $LANG["event"][18] . "</th>";
     $text .= "<th></th>";
     $text .= "<th></th>";
     $text .= "<th></th>";
     $text .= "<th>" . $LANG["common"][27] . "</th>";
     $text .= "</tr>";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             $text .= "<tr class='tab_bg_1'>";
             if (!empty($data["FK_port_destination"])) {
                 // Connections and disconnections
                 if ($data['Field'] == '1') {
                     $text .= "<td align='center'><img src='" . GLPI_ROOT . "/plugins/fusioninventory/pics/connection_ok.png'/></td>";
                 } else {
                     $text .= "<td align='center'><img src='" . GLPI_ROOT . "/plugins/fusioninventory/pics/connection_notok.png'/></td>";
                 }
                 if ($ID_port == $data["FK_port_source"]) {
                     $np->getFromDB($data["FK_port_destination"]);
                     if (isset($np->fields["on_device"])) {
                         $CommonItem->getFromDB($np->fields["device_type"], $np->fields["on_device"]);
                         $link1 = $CommonItem->getLink(1);
                         $link = "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networking.port.php?ID=" . $np->fields["ID"] . "\">";
                         if (rtrim($np->fields["name"]) != "") {
                             $link .= $np->fields["name"];
                         } else {
                             $link .= $LANG['common'][0];
                         }
                         $link .= "</a>";
                         $text .= "<td align='center'>" . $link . " " . $LANG['networking'][25] . " " . $link1 . "</td>";
                     } else {
                         $text .= "<td align='center'><font color='#ff0000'>" . $LANG['common'][28] . "</font></td>";
                     }
                 } else {
                     if ($ID_port == $data["FK_port_destination"]) {
                         $np->getFromDB($data["FK_port_source"]);
                         if (isset($np->fields["on_device"])) {
                             $CommonItem->getFromDB($np->fields["device_type"], $np->fields["on_device"]);
                             $link1 = $CommonItem->getLink(1);
                             $link = "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networking.port.php?ID=" . $np->fields["ID"] . "\">";
                             if (rtrim($np->fields["name"]) != "") {
                                 $link .= $np->fields["name"];
                             } else {
                                 $link .= $LANG['common'][0];
                             }
                             $link .= "</a>";
                             $text .= "<td align='center'>" . $link . " " . $LANG['networking'][25] . " " . $link1 . "</td>";
                         } else {
                             $text .= "<td align='center'><font color='#ff0000'>" . $LANG['common'][28] . "</font></td>";
                         }
                     }
                 }
                 $text .= "<td align='center' colspan='4'></td>";
                 $text .= "<td align='center'>" . convDateTime($data["date"]) . "</td>";
             } else {
                 // Changes values
                 $text .= "<td align='center' colspan='2'></td>";
                 $text .= "<td align='center'>" . $FUSIONINVENTORY_MAPPING[NETWORKING_TYPE][$data["Field"]]['name'] . "</td>";
                 $text .= "<td align='center'>" . $data["old_value"] . "</td>";
                 $text .= "<td align='center'>-></td>";
                 $text .= "<td align='center'>" . $data["new_value"] . "</td>";
                 $text .= "<td align='center'>" . convDateTime($data["date"]) . "</td>";
             }
             $text .= "</tr>";
         }
     }
     /*
       $pthc = new PluginFusioninventorySnmphistoryconnection;
     
       $data_connections = $pthc->find('`FK_port_source`="'.$ID_port.'"
                                           OR `FK_port_destination `="'.$ID_port.'"',
                                        '`date` DESC',
                                        '0,30');
       $query = "SELECT *
                 FROM `glpi_plugin_fusioninventory_snmphistories`
                 WHERE `FK_ports`='".$ID_port."'
                 ORDER BY `date_mod` DESC
                 LIMIT 0,30;";
     
     
       $text = "<table class='tab_cadre' cellpadding='5' width='950'>";
     
       $text .= "<tr class='tab_bg_1'>";
       $text .= "<th colspan='8'>";
       $text .= "Historique";
       $text .= "</th>";
       $text .= "</tr>";
     
       $text .= "<tr class='tab_bg_1'>";
       $text .= "<th>".$LANG['plugin_fusioninventory']["snmp"][50]."</th>";
       $text .= "<th>".$LANG["common"][1]."</th>";
       $text .= "<th>".$LANG["networking"][15]."</th>";
       $text .= "<th>".$LANG["event"][18]."</th>";
       $text .= "<th></th>";
       $text .= "<th></th>";
       $text .= "<th></th>";
       $text .= "<th>".$LANG["common"][27]."</th>";
       $text .= "</tr>";
     
       if ($result=$DB->query($query)) {
          while ($data=$DB->fetch_array($result)) {
             $text .= "<tr class='tab_bg_1'>";
     
             if (($data["old_device_ID"] != "0") OR ($data["new_device_ID"] != "0")) {
                // Connections and disconnections
                if ($data["old_device_ID"] != "0") {
                   $text .= "<td align='center'>".$LANG['plugin_fusioninventory']["history"][2]."</td>";
                   $CommonItem->getFromDB($data["old_device_type"],$data["old_device_ID"]);
                   $text .= "<td align='center'>".$CommonItem->getLink(1)."</td>";
                   $text .= "<td align='center'>".$data["old_value"]."</td>";
                } else if ($data["new_device_ID"] != "0") {
                   $text .= "<td align='center'>".$LANG['plugin_fusioninventory']["history"][3]."</td>";
                   $CommonItem->getFromDB($data["new_device_type"],$data["new_device_ID"]);
                   $text .= "<td align='center'>".$CommonItem->getLink(1)."</td>";
                   $text .= "<td align='center'>".$data["new_value"]."</td>";
                }
                $text .= "<td align='center' colspan='4'></td>";
                $text .= "<td align='center'>".convDateTime($data["date_mod"])."</td>";
     
             } else if (($data["old_device_ID"] == "0") AND ($data["new_device_ID"] == "0") AND ($data["Field"] == "0")) {
                // Unknown Mac address
                if (!empty($data["old_value"])) {
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$LANG['plugin_fusioninventory']["history"][2]."</td>";
                   $CommonItem->getFromDB($data["old_device_type"],$data["old_device_ID"]);
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$CommonItem->getLink(1)."</td>";
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$data["old_value"]."</td>";
                } else if (!empty($data["new_value"])) {
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$LANG['plugin_fusioninventory']["history"][3]."</td>";
                   $CommonItem->getFromDB($data["new_device_type"],$data["new_device_ID"]);
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$CommonItem->getLink(1)."</td>";
                   $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".$data["new_value"]."</td>";
                }
                $text .= "<td align='center' colspan='4' background='#cf9b9b' class='tab_bg_1_2'></td>";
                $text .= "<td align='center' background='#cf9b9b' class='tab_bg_1_2'>".convDateTime($data["date_mod"])."</td>";
             } else {
                // Changes values
                $text .= "<td align='center' colspan='3'></td>";
                $text .= "<td align='center'>".$data["Field"]."</td>";
                $text .= "<td align='center'>".$data["old_value"]."</td>";
                $text .= "<td align='center'>-></td>";
                $text .= "<td align='center'>".$data["new_value"]."</td>";
                $text .= "<td align='center'>".convDateTime($data["date_mod"])."</td>";
             }
             $text .= "</tr>";
          }
       }
     */
     $text .= "<tr class='tab_bg_1'>";
     $text .= "<th colspan='8'>";
     $text .= "<a href='" . GLPI_ROOT . "/plugins/fusioninventory/report/switch_ports.history.php?FK_networking_ports=" . $ID_port . "'>Voir l'historique complet</a>";
     $text .= "</th>";
     $text .= "</tr>";
     $text .= "</table>";
     return $text;
 }
 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'));
         }
     }
 }
function plugin_fusioninventory_giveItem($type, $ID, $data, $num)
{
    global $CFG_GLPI, $DB, $INFOFORM_PAGES, $LINK_ID_TABLE, $LANG, $SEARCH_OPTION, $FUSIONINVENTORY_MAPPING;
    $table = $SEARCH_OPTION[$type][$ID]["table"];
    $field = $SEARCH_OPTION[$type][$ID]["field"];
    //	echo "GiveItem : ".$field."<br/>";
    switch ($type) {
        // * Computer List (front/computer.php)
        case COMPUTER_TYPE:
            switch ($table . '.' . $field) {
                // ** FusionInventory - switch
                case "glpi_plugin_fusioninventory_networking.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    $netport = new Netport();
                    $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                    foreach ($list as $numtmp => $vartmp) {
                        $netport->getDeviceData($vartmp, NETWORKING_TYPE);
                        $out .= "<a href=\"" . $CFG_GLPI["root_doc"] . "/" . $INFOFORM_PAGES[NETWORKING_TYPE] . "?ID=" . $vartmp . "\">";
                        $out .= $netport->device_name;
                        $out .= $vartmp;
                        if ($CFG_GLPI["view_ID"]) {
                            $out .= " (" . $vartmp . ")";
                        }
                        $out .= "</a><br/>";
                    }
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - switch port
                // ** FusionInventory - switch port
                case "glpi_plugin_fusioninventory_networking_ports.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    if (!empty($data["ITEM_{$num}"])) {
                        $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                        $np = new Netport();
                        foreach ($list as $numtmp => $vartmp) {
                            $np->getFromDB($vartmp);
                            $out .= "<a href='" . GLPI_ROOT . "/front/networking.port.php?ID=" . $vartmp . "'>" . $np->fields["name"] . "</a><br/>";
                        }
                    }
                    return "<center>" . $out . "</center>";
                    break;
            }
            break;
            // * Networking List (front/networking.php)
        // * Networking List (front/networking.php)
        case NETWORKING_TYPE:
            switch ($table . '.' . $field) {
                // ** FusionInventory - last inventory
                case "glpi_plugin_fusioninventory_networking.FK_networking":
                    $query = "SELECT *\n                         FROM `glpi_plugin_fusioninventory_networking`\n                         WHERE `FK_networking` = '" . $data["ID"] . "';";
                    if ($result = $DB->query($query)) {
                        $data2 = $DB->fetch_array($result);
                    }
                    $last_date = "";
                    if (isset($data2["last_fusioninventory_update"])) {
                        $last_date = $data2["last_fusioninventory_update"];
                    }
                    $out = "<div align='center'>" . convDateTime($last_date) . "</div>";
                    return $out;
                    break;
                    // ** FusionInventory - SNMP models
                // ** FusionInventory - SNMP models
                case "glpi_plugin_fusioninventory_model_infos.ID":
                    $plugin_fusioninventory_snmp = new PluginFusioninventorySNMP();
                    $FK_model_DB = $plugin_fusioninventory_snmp->GetSNMPModel($data["ID"], $type);
                    $out = "<a href=\"" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory/front/models.form.php?ID=" . $FK_model_DB . "\">";
                    $out .= Dropdown::getDropdownName("glpi_plugin_fusioninventory_model_infos", $FK_model_DB, 0);
                    $out .= "</a>";
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - SNMP authentification
                // ** FusionInventory - SNMP authentification
                case "glpi_plugin_fusioninventory_snmpauths.name":
                    $plugin_fusioninventory_snmp = new PluginFusioninventorySnmpauth();
                    $FK_auth_DB = $plugin_fusioninventory_snmp->GetSNMPAuth($data["ID"], $type);
                    $out = "<a href=\"" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory/front/snmp_auth.form.php?ID=" . $FK_auth_DB . "\">";
                    $out .= Dropdown::getDropdownName("glpi_plugin_fusioninventory_snmpauths", $FK_auth_DB, 0);
                    $out .= "</a>";
                    return "<center>" . $out . "</center>";
                    break;
            }
            break;
            // * Printer List (front/printer.php)
        // * Printer List (front/printer.php)
        case PRINTER_TYPE:
            switch ($table . '.' . $field) {
                // ** FusionInventory - switch
                case "glpi_plugin_fusioninventory_networking.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    $netport = new Netport();
                    $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                    foreach ($list as $numtmp => $vartmp) {
                        $netport->getDeviceData($vartmp, NETWORKING_TYPE);
                        $out .= "<a href=\"" . $CFG_GLPI["root_doc"] . "/" . $INFOFORM_PAGES[NETWORKING_TYPE] . "?ID=" . $vartmp . "\">";
                        $out .= $netport->device_name;
                        if ($CFG_GLPI["view_ID"]) {
                            $out .= " (" . $vartmp . ")";
                        }
                        $out .= "</a><br/>";
                    }
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - switch port
                // ** FusionInventory - switch port
                case "glpi_plugin_fusioninventory_networking_ports.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    if (!empty($data["ITEM_{$num}"])) {
                        $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                        $np = new Netport();
                        foreach ($list as $numtmp => $vartmp) {
                            $np->getFromDB($vartmp);
                            $out .= "<a href='" . GLPI_ROOT . "/front/networking.port.php?ID=" . $vartmp . "'>" . $np->fields["name"] . "</a><br/>";
                        }
                    }
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - last inventory
                // ** FusionInventory - last inventory
                case "glpi_plugin_fusioninventory_printers.FK_printers":
                    $query = "SELECT *\n                         FROM `glpi_plugin_fusioninventory_printers`\n                         WHERE `FK_printers` = '" . $data["ID"] . "';";
                    if ($result = $DB->query($query)) {
                        $data2 = $DB->fetch_array($result);
                    }
                    $last_date = "";
                    if (isset($data2["last_fusioninventory_update"])) {
                        $last_date = $data2["last_fusioninventory_update"];
                    }
                    $out = "<div align='center'>" . convDateTime($last_date) . "</div>";
                    return $out;
                    break;
                    // ** FusionInventory - SNMP models
                // ** FusionInventory - SNMP models
                case "glpi_plugin_fusioninventory_model_infos.ID":
                    $plugin_fusioninventory_snmp = new PluginFusioninventorySNMP();
                    $FK_model_DB = $plugin_fusioninventory_snmp->GetSNMPModel($data["ID"], $type);
                    $out = "<a href=\"" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory/front/models.form.php?ID=" . $FK_model_DB . "\">";
                    $out .= Dropdown::getDropdownName("glpi_plugin_fusioninventory_model_infos", $FK_model_DB, 0);
                    $out .= "</a>";
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - SNMP authentification
                // ** FusionInventory - SNMP authentification
                case "glpi_plugin_fusioninventory_snmpauths.ID":
                    $plugin_fusioninventory_snmp = new PluginFusioninventorySnmpauth();
                    $FK_auth_DB = $plugin_fusioninventory_snmp->GetSNMPAuth($data["ID"], $type);
                    $out = "<a href=\"" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory/front/snmp_auth.form.php?ID=" . $FK_auth_DB . "\">";
                    $out .= Dropdown::getDropdownName("glpi_plugin_fusioninventory_snmpauths", $FK_auth_DB, 0);
                    $out .= "</a>";
                    return "<center>" . $out . "</center>";
                    break;
            }
            break;
            // * Model List (plugins/fusioninventory/front/models.php)
        // * Model List (plugins/fusioninventory/front/models.php)
        case PLUGIN_FUSIONINVENTORY_MODEL:
            switch ($table . '.' . $field) {
                // ** Name of type of model (network, printer...)
                case "glpi_plugin_fusioninventory_model_infos.device_type":
                    $out = '<center> ';
                    switch ($data["ITEM_{$num}"]) {
                        case COMPUTER_TYPE:
                            $out .= $LANG["Menu"][0];
                            break;
                        case NETWORKING_TYPE:
                            $out .= $LANG["Menu"][1];
                            break;
                        case PRINTER_TYPE:
                            $out .= $LANG["Menu"][2];
                            break;
                        case PERIPHERAL_TYPE:
                            $out .= $LANG["Menu"][16];
                            break;
                        case PHONE_TYPE:
                            $out .= $LANG["Menu"][34];
                            break;
                    }
                    $out .= '</center>';
                    return $out;
                    break;
                    // ** Display pic / link for exporting model
                // ** Display pic / link for exporting model
                case "glpi_plugin_fusioninventory_model_infos.ID":
                    $out = "<div align='center'><form></form><form method='get' action='" . GLPI_ROOT . "/plugins/fusioninventory/front/models.export.php' target='_blank'>\n\t\t\t\t\t\t<input type='hidden' name='model' value='" . $data["ID"] . "' />\n\t\t\t\t\t\t<input name='export' src='" . GLPI_ROOT . "/pics/right.png' title='Exporter' value='Exporter' type='image'>\n\t\t\t\t\t\t</form></div>";
                    return "<center>" . $out . "</center>";
                    break;
            }
            break;
            // * Authentification List (plugins/fusioninventory/front/snmp_auth.php)
        // * Authentification List (plugins/fusioninventory/front/snmp_auth.php)
        case PLUGIN_FUSIONINVENTORY_SNMP_AUTH:
            switch ($table . '.' . $field) {
                // ** Hidden auth passphrase (SNMP v3)
                case "glpi_plugin_fusioninventory_snmpauths.auth_passphrase":
                    $out = "";
                    if (empty($data["ITEM_{$num}"])) {
                    } else {
                        $out = "********";
                    }
                    return $out;
                    break;
                    // ** Hidden priv passphrase (SNMP v3)
                // ** Hidden priv passphrase (SNMP v3)
                case "glpi_plugin_fusioninventory_snmpauths.priv_passphrase":
                    $out = "";
                    if (empty($data["ITEM_{$num}"])) {
                    } else {
                        $out = "********";
                    }
                    return $out;
                    break;
            }
            break;
            // * Unknown mac addresses connectd on switch - report (plugins/fusioninventory/report/unknown_mac.php)
        // * Unknown mac addresses connectd on switch - report (plugins/fusioninventory/report/unknown_mac.php)
        case PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN:
            switch ($table . '.' . $field) {
                // ** FusionInventory - switch
                case "glpi_plugin_fusioninventory_networking.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    $netport = new Netport();
                    $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                    foreach ($list as $numtmp => $vartmp) {
                        $netport->getDeviceData($vartmp, PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN);
                        $out .= "<a href=\"" . $CFG_GLPI["root_doc"] . "/" . $INFOFORM_PAGES[PLUGIN_FUSIONINVENTORY_MAC_UNKNOWN] . "?ID=" . $vartmp . "\">";
                        $out .= $netport->device_name;
                        if ($CFG_GLPI["view_ID"]) {
                            $out .= " (" . $vartmp . ")";
                        }
                        $out .= "</a><br/>";
                    }
                    return "<center>" . $out . "</center>";
                    break;
                    // ** FusionInventory - switch port
                // ** FusionInventory - switch port
                case "glpi_plugin_fusioninventory_networking_ports.ID":
                    $out = '';
                    include_once GLPI_ROOT . "/inc/networking.class.php";
                    if (!empty($data["ITEM_{$num}"])) {
                        $list = explode("\$\$\$\$", $data["ITEM_{$num}"]);
                        $np = new Netport();
                        foreach ($list as $numtmp => $vartmp) {
                            $np->getFromDB($vartmp);
                            $out .= "<a href='" . GLPI_ROOT . "/front/networking.port.php?ID=" . $vartmp . "'>" . $np->fields["name"] . "</a><br/>";
                        }
                    }
                    return "<center>" . $out . "</center>";
                    break;
                case "glpi_plugin_fusioninventory_unknown_device.type":
                    $out = '<center> ';
                    switch ($data["ITEM_{$num}"]) {
                        case COMPUTER_TYPE:
                            $out .= $LANG["Menu"][0];
                            break;
                        case NETWORKING_TYPE:
                            $out .= $LANG["Menu"][1];
                            break;
                        case PRINTER_TYPE:
                            $out .= $LANG["Menu"][2];
                            break;
                        case PERIPHERAL_TYPE:
                            $out .= $LANG["Menu"][16];
                            break;
                        case PHONE_TYPE:
                            $out .= $LANG["Menu"][34];
                            break;
                    }
                    $out .= '</center>';
                    return $out;
                    break;
            }
            break;
            // *
        // *
        case PLUGIN_FUSIONINVENTORY_SNMP_NETWORKING_PORTS:
            switch ($table . '.' . $field) {
            }
            break;
            // * Ports date connection - report (plugins/fusioninventory/report/ports_date_connections.php)
        // * Ports date connection - report (plugins/fusioninventory/report/ports_date_connections.php)
        case PLUGIN_FUSIONINVENTORY_SNMP_NETWORKING_PORTS2:
            switch ($table . '.' . $field) {
                // ** Name and link of networking device (switch)
                case "glpi_plugin_fusioninventory_networking_ports.ID":
                    $query = "SELECT `glpi_networking`.`name` AS `name`, `glpi_networking`.`ID` AS `ID`\n                         FROM `glpi_networking`\n                              LEFT JOIN `glpi_networking_ports`\n                                        ON `on_device` = `glpi_networking`.`ID`\n                              LEFT JOIN `glpi_plugin_fusioninventory_networking_ports`\n                                        ON `glpi_networking_ports`.`ID`=`FK_networking_ports`\n                         WHERE `glpi_plugin_fusioninventory_networking_ports`.`ID`='" . $data["ITEM_{$num}"] . "'\n                         LIMIT 0,1;";
                    $result = $DB->query($query);
                    $data2 = $DB->fetch_assoc($result);
                    $out = "<a href='" . GLPI_ROOT . "/front/networking.form.php?ID=" . $data2["ID"] . "'>" . $data2["name"] . "</a>";
                    return "<center>" . $out . "</center>";
                    break;
                    // ** Name and link of port of networking device (port of switch)
                // ** Name and link of port of networking device (port of switch)
                case "glpi_plugin_fusioninventory_networking_ports.FK_networking_ports":
                    $netport = new Netport();
                    $netport->getFromDB($data["ITEM_{$num}"]);
                    $name = "";
                    if (isset($netport->fields["name"])) {
                        $name = $netport->fields["name"];
                    }
                    $out = "<a href='" . GLPI_ROOT . "/front/networking.port.php?ID=" . $data["ITEM_{$num}"] . "'>" . $name . "</a>";
                    return "<center>" . $out . "</center>";
                    break;
                    // ** Location of switch
                // ** Location of switch
                case "glpi_dropdown_locations.ID":
                    $out = Dropdown::getDropdownName("glpi_dropdown_locations", $data["ITEM_{$num}"]);
                    return "<center>" . $out . "</center>";
                    break;
            }
            break;
            // * FusionInventory Agents list (plugins/fusioninventory/front/agents.php)
        // * FusionInventory Agents list (plugins/fusioninventory/front/agents.php)
        case PLUGIN_FUSIONINVENTORY_SNMP_AGENTS:
            break;
            // * range IP list (plugins/fusioninventory/front/rangeip.php)
        // * range IP list (plugins/fusioninventory/front/rangeip.php)
        case PLUGIN_FUSIONINVENTORY_SNMP_RANGEIP:
            switch ($table . '.' . $field) {
                // ** Display entity name
                case "glpi_entities.name":
                    if ($data["ITEM_{$num}"] == '') {
                        $out = Dropdown::getDropdownName("glpi_entities", $data["ITEM_{$num}"]);
                        return "<center>" . $out . "</center>";
                    }
                    break;
            }
            break;
            // * Detail of ports history (plugins/fusioninventory/report/switch_ports.history.php)
        // * Detail of ports history (plugins/fusioninventory/report/switch_ports.history.php)
        case PLUGIN_FUSIONINVENTORY_SNMP_HISTORY:
            switch ($table . '.' . $field) {
                // ** Display switch and Port
                case "glpi_networking_ports.ID":
                    $Array_device = PluginFusioninventoryPort::getUniqueObjectfieldsByportID($data["ITEM_{$num}"]);
                    $CommonItem = new CommonItem();
                    $CommonItem->getFromDB($Array_device["device_type"], $Array_device["on_device"]);
                    $out = "<div align='center'>" . $CommonItem->getLink(1);
                    $query = "SELECT *\n                         FROM `glpi_networking_ports`\n                         WHERE `ID`='" . $data["ITEM_{$num}"] . "';";
                    $result = $DB->query($query);
                    if ($DB->numrows($result) != "0") {
                        $out .= "<br/><a href='" . GLPI_ROOT . "/front/networking.port.php?ID=" . $data["ITEM_{$num}"] . "'>" . $DB->result($result, 0, "name") . "</a>";
                    }
                    $out .= "</td>";
                    return $out;
                    break;
                    // ** Display GLPI field of device
                // ** Display GLPI field of device
                case "glpi_plugin_fusioninventory_snmphistories.Field":
                    $out = $FUSIONINVENTORY_MAPPING[NETWORKING_TYPE][$data["ITEM_{$num}"]]['name'];
                    return $out;
                    break;
                    // ** Display Old Value (before changement of value)
                // ** Display Old Value (before changement of value)
                case "glpi_plugin_fusioninventory_snmphistories.old_value":
                    // TODO ADD LINK TO DEVICE
                    if (substr_count($data["ITEM_{$num}"], ":") == 5 and empty($data["ITEM_3"])) {
                        return "<center><b>" . $data["ITEM_{$num}"] . "</b></center>";
                    }
                    break;
                    // ** Display New Value (new value modified)
                // ** Display New Value (new value modified)
                case "glpi_plugin_fusioninventory_snmphistories.new_value":
                    if (substr_count($data["ITEM_{$num}"], ":") == 5 and empty($data["ITEM_3"])) {
                        return "<center><b>" . $data["ITEM_{$num}"] . "</b></center>";
                    }
                    break;
            }
            break;
    }
    return "";
}