function addComponentToItems($componentscatalogs_id, $components_id)
 {
     global $DB;
     $pmService = new PluginMonitoringService();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmNetworkport = new PluginMonitoringNetworkport();
     $pluginMonitoringNetworkport = 0;
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='PluginMonitoringNetworkport'\n            AND `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'\n         LIMIT 1";
     $result = $DB->query($query);
     if ($DB->numrows($result) > 0) {
         $pluginMonitoringNetworkport = 1;
     }
     $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n         WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $itemtype = $data['itemtype'];
         $item = new $itemtype();
         $item->getFromDB($data['items_id']);
         if ($pluginMonitoringNetworkport == '0') {
             $input = array();
             $input['entities_id'] = $item->fields['entities_id'];
             $input['plugin_monitoring_componentscatalogs_hosts_id'] = $data['id'];
             $input['plugin_monitoring_components_id'] = $components_id;
             $input['name'] = Dropdown::getDropdownName("glpi_plugin_monitoring_components", $components_id);
             $input['state'] = 'WARNING';
             $input['state_type'] = 'HARD';
             $pmService->add($input);
         } else {
             if ($pluginMonitoringNetworkport == '1') {
                 $a_services_created = array();
                 $querys = "SELECT * FROM `glpi_plugin_monitoring_services`\n               WHERE `plugin_monitoring_components_id`='" . $components_id . "'\n                  AND `plugin_monitoring_componentscatalogs_hosts_id`='" . $data['id'] . "'";
                 $results = $DB->query($querys);
                 while ($datas = $DB->fetch_array($results)) {
                     $a_services_created[$datas['networkports_id']] = $datas['id'];
                 }
                 $a_ports = $pmNetworkport->find("`itemtype`='" . $data['itemtype'] . "'\n               AND `items_id`='" . $data['items_id'] . "'");
                 foreach ($a_ports as $datap) {
                     if (isset($a_services_created[$datap["id"]])) {
                         unset($a_services_created[$datap["id"]]);
                     } else {
                         $input = array();
                         $input['networkports_id'] = $datap['networkports_id'];
                         $input['entities_id'] = $item->fields['entities_id'];
                         $input['plugin_monitoring_componentscatalogs_hosts_id'] = $data['id'];
                         $input['plugin_monitoring_components_id'] = $components_id;
                         $input['name'] = Dropdown::getDropdownName("glpi_plugin_monitoring_components", $components_id);
                         $input['state'] = 'WARNING';
                         $input['state_type'] = 'HARD';
                         $pmService->add($input);
                     }
                 }
                 foreach ($a_services_created as $id) {
                     $_SESSION['plugin_monitoring_hosts'] = $data;
                     $pmService->delete(array('id' => $id));
                 }
             }
         }
     }
 }
 static function getItemsDynamicly($parm)
 {
     global $DB;
     $pmCc_Rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmSearch = new PluginMonitoringSearch();
     $pmService = new PluginMonitoringService();
     $devices_present = array();
     $devicesnetworkport_present = array();
     if ($pmCc_Rule->getFromDB($parm->fields['id'])) {
         // Load right entity
         $pmComponentscatalog->getFromDB($pmCc_Rule->fields['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $get_tmp = '';
         $itemtype = $pmCc_Rule->fields['itemtype'];
         if (isset($_GET)) {
             $get_tmp = $_GET;
         }
         if (isset($_SESSION["glpisearchcount"][$pmCc_Rule->fields['itemtype']])) {
             unset($_SESSION["glpisearchcount"][$pmCc_Rule->fields['itemtype']]);
         }
         if (isset($_SESSION["glpisearchcount2"][$pmCc_Rule->fields['itemtype']])) {
             unset($_SESSION["glpisearchcount2"][$pmCc_Rule->fields['itemtype']]);
         }
         $_GET = importArrayFromDB($pmCc_Rule->fields['condition']);
         $_GET["glpisearchcount"] = count($_GET['field']);
         if (isset($_GET['field2'])) {
             $_GET["glpisearchcount2"] = count($_GET['field2']);
         }
         Search::manageGetValues($pmCc_Rule->fields['itemtype']);
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $pmCc_Rule->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
         $queryd = "SELECT `glpi_plugin_monitoring_services`.`id` FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               LEFT JOIN `glpi_plugin_monitoring_services`\n                  ON `plugin_monitoring_componentscatalogs_hosts_id` = \n                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='NetworkEquipment'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devicesnetworkport_present[$data['id']] = 1;
         }
         $glpilist_limit = $_SESSION['glpilist_limit'];
         $_SESSION['glpilist_limit'] = 500000;
         $result = $pmSearch->constructSQL($itemtype, $_GET);
         $_SESSION['glpilist_limit'] = $glpilist_limit;
         while ($data = $DB->fetch_array($result)) {
             $networkports_id = 0;
             $itemtype_device = $pmCc_Rule->fields['itemtype'];
             $items_id_device = $data['id'];
             if ($itemtype_device == 'PluginMonitoringNetworkport') {
                 //               $queryh = "SELECT `itemtype`, `items_id`,
                 //                     `glpi_plugin_monitoring_services`.`id`,
                 //                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id` as hid
                 //                     FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
                 //                  LEFT JOIN `glpi_plugin_monitoring_services`
                 //                     ON `plugin_monitoring_componentscatalogs_hosts_id` =
                 //                        `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`
                 //                  WHERE `plugin_monitoring_componentscalalog_id`='".$pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"]."'
                 //                     AND `itemtype`='".$itemtype_device."'
                 //                     AND `items_id`='".$items_id_device."'
                 //                     AND `glpi_plugin_monitoring_services`.`id` IS NULL";
                 //               $resulth = $DB->query($queryh);
                 //               while ($datah=$DB->fetch_array($resulth)) {
                 //                  $pmComponentscatalog_Host->delete(array('id'=>$datah['hid']));
                 //               }
                 $pmNetworkport = new PluginMonitoringNetworkport();
                 $pmNetworkport->getFromDB($data['id']);
                 $itemtype_device = $pmNetworkport->fields['itemtype'];
                 $items_id_device = $pmNetworkport->fields['items_id'];
                 $networkports_id = $pmNetworkport->fields['networkports_id'];
                 $networkPort = new NetworkPort();
                 if ($networkPort->getFromDB($networkports_id)) {
                     $querynet = "SELECT `itemtype`, `items_id`,\n                        `glpi_plugin_monitoring_services`.`id`               \n                        FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                     LEFT JOIN `glpi_plugin_monitoring_services`\n                        ON `plugin_monitoring_componentscatalogs_hosts_id` = \n                           `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n                     WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                        AND `itemtype`='" . $itemtype_device . "'\n                        AND `items_id`='" . $items_id_device . "'\n                        AND `networkports_id`='" . $networkports_id . "'\n                           LIMIT 1";
                     $resultnet = $DB->query($querynet);
                     if ($DB->numrows($resultnet) == 0) {
                         $input = array();
                         $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                         $input['is_static'] = '0';
                         $input['items_id'] = $items_id_device;
                         $input['itemtype'] = $itemtype_device;
                         $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                         $pmComponentscatalog_Host->linkComponentsToItem($pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"], $componentscatalogs_hosts_id, $networkports_id);
                     } else {
                         $data2 = $DB->fetch_assoc($resultnet);
                         // modify entity of services (if entity of device is changed)
                         $itemtype = $data2['itemtype'];
                         $item = new $itemtype();
                         $item->getFromDB($data2['items_id']);
                         $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                           SET `entities_id`='" . $item->fields['entities_id'] . "'\n                              WHERE `id`='" . $data2['id'] . "'";
                         $DB->query($queryu);
                         unset($devicesnetworkport_present[$data2['id']]);
                     }
                 } else {
                     $pmNetworkport->delete($pmNetworkport->fields);
                 }
                 // Reload current entity
                 Session::changeActiveEntities($default_entity, $entities_isrecursive);
             } else {
                 $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                  WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                     AND `itemtype`='" . $itemtype_device . "'\n                     AND `items_id`='" . $items_id_device . "'\n                        LIMIT 1";
                 $resulth = $DB->query($queryh);
                 if ($DB->numrows($resulth) == '0') {
                     $input = array();
                     $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                     $input['is_static'] = '0';
                     $input['items_id'] = $items_id_device;
                     $input['itemtype'] = $itemtype_device;
                     $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                     $pmComponentscatalog_Host->linkComponentsToItem($pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"], $componentscatalogs_hosts_id, $networkports_id);
                 } else {
                     $data2 = $DB->fetch_assoc($resulth);
                     // modify entity of services (if entity of device is changed)
                     $itemtype = $data2['itemtype'];
                     $item = new $itemtype();
                     $item->getFromDB($data2['items_id']);
                     $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                        SET `entities_id`='" . $item->fields['entities_id'] . "'\n                           WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                     $DB->query($queryu);
                     unset($devices_present[$data2['id']]);
                 }
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
         foreach ($devicesnetworkport_present as $id => $num) {
             $_SESSION['plugin_monitoring_hosts']['itemtype'] = $itemtype_device;
             $_SESSION['plugin_monitoring_hosts']['items_id'] = $items_id_device;
             $pmService->delete(array('id' => $id));
         }
     } else {
         // Purge
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $parm->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $parm->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
     }
     foreach ($devices_present as $id => $num) {
         $pmComponentscatalog_Host->delete(array('id' => $id));
     }
     return true;
 }
 static function deleteNetworkPort($parm)
 {
     global $DB;
     if ($parm->fields['itemtype'] == 'NetworkEquipment') {
         $query = "SELECT * FROM `glpi_plugin_monitoring_networkports`\n            WHERE `networkports_id`='" . $parm->fields['id'] . "'";
         $result = $DB->query($query);
         while ($data = $DB->fetch_array($result)) {
             $pmNetworkPort = new PluginMonitoringNetworkport();
             $pmNetworkPort->delete($data);
         }
     }
 }
 /**
  * Display detail networkport based on glpi core networkport and fusioninventory
  * networkport
  *
  * @param array $data with id ant fusionid
  * @param boolean $monitoring true if monitoring installed && actived
  * @param boolean $aggrega true if this port is aggregate port
  *
  * @return nothing
  */
 function showNetworkPortDetail($data, $monitoring, $aggrega = 0)
 {
     global $CFG_GLPI, $DB;
     $nw = new NetworkPort_NetworkPort();
     $networkName = new NetworkName();
     $networkPort = new NetworkPort();
     $pfNetworkPort = new PluginFusioninventoryNetworkPort();
     $iPAddress = new IPAddress();
     $networkPort->getFromDB($data['id']);
     $pfNetworkPort->getFromDB($data['fusionid']);
     $background_img = "";
     if ($pfNetworkPort->fields["trunk"] == "1" && (strstr($pfNetworkPort->fields["ifstatus"], "up") || $pfNetworkPort->fields["ifstatus"] == 1)) {
         $background_img = " style='background-image: url(\"" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/port_trunk.png\"); '";
     } else {
         if (PluginFusioninventoryNetworkPort::isPortHasMultipleMac($data['id']) && (strstr($pfNetworkPort->fields["ifstatus"], "up") || $pfNetworkPort->fields["ifstatus"] == 1)) {
             $background_img = " style='background-image: url(\"" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/multiple_mac_addresses.png\"); '";
         } else {
             if (strstr($pfNetworkPort->fields["ifstatus"], "up") || $pfNetworkPort->fields["ifstatus"] == 1) {
                 $background_img = " style='background-image: url(\"" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/connected_trunk.png\"); '";
             }
         }
     }
     echo "<tr class='tab_bg_1 center' height='40'" . $background_img . ">";
     if ($aggrega) {
         echo "<td style='background-color: #f2f2f2;'></td><td>";
     }
     if (!$aggrega) {
         if ($networkPort->fields['instantiation_type'] == 'NetworkPortAggregate') {
             echo "<td>";
         } else {
             echo "<td colspan='2'>";
         }
     }
     echo "<a href='networkport.form.php?id=" . $networkPort->fields["id"] . "'>" . $networkPort->fields["name"] . "</a>";
     Html::showToolTip($pfNetworkPort->fields['ifdescr']);
     if (!$aggrega) {
         if ($networkPort->fields['instantiation_type'] == 'NetworkPortAggregate') {
             echo "<td><i><font style='color: grey'>" . __('Aggregation port') . "</font></i></td>";
         }
     }
     if ($monitoring == '1') {
         echo "<td>";
         $state = PluginMonitoringNetworkport::isMonitoredNetworkport($data['id']);
         if (Session::haveRight("plugin_monitoring_componentscatalog", UPDATE)) {
             $checked = '';
             if ($state) {
                 $checked = 'checked';
             }
             echo "<input type='checkbox' name='networkports_id[]' value='" . $data['id'] . "' " . $checked . "/>";
         } else {
             if (Session::haveRight("plugin_monitoring_componentscatalog", READ)) {
                 echo Dropdown::getYesNo($state);
             }
         }
         echo "</td>";
     }
     $a_pref = DisplayPreference::getForTypeUser('PluginFusioninventoryNetworkport', Session::getLoginUserID());
     foreach ($a_pref as $data_array) {
         switch ($data_array) {
             case 3:
                 echo "<td>" . $pfNetworkPort->fields["ifmtu"] . "</td>";
                 break;
             case 5:
                 echo "<td>" . $this->byteSize($pfNetworkPort->fields["ifspeed"], 1000) . "bps</td>";
                 break;
             case 6:
                 echo "<td>";
                 if (strstr($pfNetworkPort->fields["ifstatus"], "up") || strstr($pfNetworkPort->fields["ifinternalstatus"], "1")) {
                     echo "<img src='" . $CFG_GLPI['root_doc'] . "/pics/greenbutton.png'/>";
                 } else {
                     if (strstr($pfNetworkPort->fields["ifstatus"], "down") || strstr($pfNetworkPort->fields["ifinternalstatus"], "2")) {
                         echo "<img src='" . $CFG_GLPI['root_doc'] . "/pics/redbutton.png'/>";
                     } else {
                         if (strstr($pfNetworkPort->fields["ifstatus"], "testing") || strstr($pfNetworkPort->fields["ifinternalstatus"], "3")) {
                             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/yellowbutton.png'/>";
                         }
                     }
                 }
                 echo "</td>";
                 break;
             case 7:
                 echo "<td>" . $pfNetworkPort->fields["iflastchange"] . "</td>";
                 break;
             case 8:
                 echo "<td>";
                 if ($pfNetworkPort->fields["ifinoctets"] == "0") {
                     echo "-";
                 } else {
                     echo $this->byteSize($pfNetworkPort->fields["ifinoctets"], 1000) . "o";
                 }
                 echo " / ";
                 if ($pfNetworkPort->fields["ifinoctets"] == "0") {
                     echo "-";
                 } else {
                     echo $this->byteSize($pfNetworkPort->fields["ifoutoctets"], 1000) . "o";
                 }
                 echo "</td>";
                 break;
             case 9:
                 $color = '';
                 if ($pfNetworkPort->fields["ifinerrors"] != "0" || $pfNetworkPort->fields["ifouterrors"] != "0") {
                     $color = "background='#cf9b9b' class='tab_bg_1_2'";
                 }
                 if ($pfNetworkPort->fields["ifinerrors"] == "0") {
                     echo "<td " . $color . ">-";
                 } else {
                     echo "<td " . $color . ">";
                     echo $pfNetworkPort->fields["ifinerrors"];
                 }
                 echo " / ";
                 if ($pfNetworkPort->fields["ifouterrors"] == "0") {
                     echo "-";
                 } else {
                     echo $pfNetworkPort->fields["ifouterrors"];
                 }
                 echo "</td>";
                 break;
             case 10:
                 if ($pfNetworkPort->fields["portduplex"] == 2) {
                     echo "<td background='#cf9b9b' class='tab_bg_1_2'>";
                     echo __('Half', 'fusioninventory');
                     echo '</td>';
                 } else {
                     if ($pfNetworkPort->fields["portduplex"] == 3) {
                         echo '<td>';
                         echo __('Full', 'fusioninventory');
                         echo '</td>';
                     } else {
                         echo "<td></td>";
                     }
                 }
                 break;
             case 11:
                 // ** internal mac
                 echo "<td>" . $networkPort->fields["mac"] . "</td>";
                 break;
             case 13:
                 // ** Mac address and link to device which are connected to this port
                 $opposite_port = $nw->getOppositeContact($data["id"]);
                 if ($opposite_port != "" && $opposite_port != 0) {
                     $networkPortOpposite = new NetworkPort();
                     if ($networkPortOpposite->getFromDB($opposite_port)) {
                         $data_device = $networkPortOpposite->fields;
                         $item = new $data_device["itemtype"]();
                         $item->getFromDB($data_device["items_id"]);
                         $link1 = $item->getLink(1);
                         $link = str_replace($item->getName(0), $data_device["mac"], $item->getLink());
                         // * GetIP
                         $a_networknames = current($networkName->find("`itemtype`='NetworkPort'\n                                          AND `items_id`='" . $item->getID() . "'", "", 1));
                         $a_ipaddresses = current($iPAddress->find("`itemtype`='NetworkName'\n                                          AND `items_id`='" . $a_networknames['id'] . "'", "", 1));
                         $link2 = str_replace($item->getName(0), $a_ipaddresses['name'], $item->getLink());
                         if ($data_device["itemtype"] == 'PluginFusioninventoryUnmanaged') {
                             $icon = $this->getItemtypeIcon($item->fields["item_type"]);
                             if ($item->getField("accepted") == "1") {
                                 echo "<td style='background:#bfec75'\n                                     class='tab_bg_1_2'>" . $icon . $link1;
                             } else {
                                 echo "<td background='#cf9b9b'\n                                     class='tab_bg_1_2'>" . $icon . $link1;
                             }
                             if (!empty($link)) {
                                 echo "<br/>" . $link;
                             }
                             if (!empty($link2)) {
                                 echo "<br/>" . $link2;
                             }
                             if ($item->getField("hub") == "1") {
                                 $this->displayHubConnections($data_device["items_id"], $background_img);
                             }
                             echo "</td>";
                         } else {
                             $icon = $this->getItemtypeIcon($data_device["itemtype"]);
                             echo "<td>" . $icon . $link1;
                             if (!empty($link)) {
                                 echo "<br/>" . $link;
                             }
                             if (!empty($link2)) {
                                 echo "<br/>" . $link2;
                             }
                             if ($data_device["itemtype"] == 'Phone') {
                                 $query_devicephone = "SELECT *\n                                   FROM `glpi_networkports`\n                                   WHERE `itemtype`='Phone'\n                                       AND `items_id`='" . $data_device["items_id"] . "'\n                                       AND `id`!='" . $data_device["id"] . "'\n                                   LIMIT 1";
                                 $result_devicephone = $DB->query($query_devicephone);
                                 if ($DB->numrows($result_devicephone) > 0) {
                                     $data_devicephone = $DB->fetch_assoc($result_devicephone);
                                     $computer_ports_id = $nw->getOppositeContact($data_devicephone["id"]);
                                     if ($computer_ports_id) {
                                         $networkport = new NetworkPort();
                                         $networkport->getFromDB($computer_ports_id);
                                         if ($networkport->fields['itemtype'] == 'Computer') {
                                             echo "<hr/>";
                                             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/computer_icon.png' " . "style='float:left'/> ";
                                             $computer = new Computer();
                                             $computer->getFromDB($networkport->fields["items_id"]);
                                             $link1 = $computer->getLink(1);
                                             $link = str_replace($computer->getName(0), $networkport->fields["mac"], $computer->getLink());
                                             $link2 = str_replace($computer->getName(0), $networkport->fields["ip"], $computer->getLink());
                                             echo $icon . $link1;
                                             if (!empty($link)) {
                                                 echo "<br/>" . $link;
                                             }
                                             if (!empty($link2)) {
                                                 echo "<br/>" . $link2;
                                             }
                                         }
                                     }
                                 }
                             }
                             echo "</td>";
                         }
                     } else {
                         echo "<td></td>";
                     }
                 } else {
                     echo "<td></td>";
                 }
                 break;
             case 14:
                 // ** Connection status
                 echo "<td>";
                 if (strstr($pfNetworkPort->fields["ifstatus"], "up") || strstr($pfNetworkPort->fields["ifstatus"], "1")) {
                     echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/wired_on.png'/>";
                 } else {
                     if (strstr($pfNetworkPort->fields["ifstatus"], "down") || strstr($pfNetworkPort->fields["ifstatus"], "2")) {
                         echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/wired_off.png'/>";
                     } else {
                         if (strstr($pfNetworkPort->fields["ifstatus"], "testing") || strstr($pfNetworkPort->fields["ifstatus"], "3")) {
                             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/yellowbutton.png'/>";
                         } else {
                             if (strstr($pfNetworkPort->fields["ifstatus"], "dormant") || strstr($pfNetworkPort->fields["ifstatus"], "5")) {
                                 echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/orangebutton.png'/>";
                             }
                         }
                     }
                 }
                 echo "</td>";
                 break;
             case 12:
                 echo "<td>";
                 $canedit = Session::haveRight('networking', UPDATE);
                 $used = array();
                 $query_vlan = "SELECT * FROM glpi_networkports_vlans\n                              WHERE networkports_id='" . $data["id"] . "'";
                 $result_vlan = $DB->query($query_vlan);
                 if ($DB->numrows($result_vlan) > 0) {
                     echo "<table cellpadding='0' cellspacing='0'>";
                     while ($line = $DB->fetch_array($result_vlan)) {
                         $used[] = $line["vlans_id"];
                         $vlan = new Vlan();
                         $vlan->getFromDB($line["vlans_id"]);
                         if ($line['tagged'] == '1') {
                             $state = 'T';
                         } else {
                             $state = 'U';
                         }
                         echo "<tr><td>" . $vlan->fields['name'] . " [" . $vlan->fields['tag'] . "] " . $state;
                         echo "</td><td>";
                         if ($canedit) {
                             echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/networkport.form.php?unassign_vlan=unassigned&amp;id=" . $line["id"] . "'>";
                             echo "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/delete.png\" alt='" . __('Delete', 'fusioninventory') . "' title='" . __('Delete', 'fusioninventory') . "'></a>";
                         } else {
                             echo "&nbsp;";
                         }
                         echo "</td>";
                         echo "</tr>";
                     }
                     echo "</table>";
                 } else {
                     echo "&nbsp;";
                 }
                 echo "</td>";
                 break;
             case 15:
                 echo "<td align='center'>";
                 if ($pfNetworkPort->fields['ifstatus'] == 1) {
                     echo __('Connected');
                 } else {
                     if ($pfNetworkPort->fields['lastup'] == "0000-00-00 00:00:00") {
                         echo '-';
                     } else {
                         $time = strtotime(date('Y-m-d H:i:s')) - strtotime($pfNetworkPort->fields['lastup']);
                         echo Html::timestampToString($time, FALSE);
                     }
                 }
                 echo "</td>";
                 break;
             case 16:
                 echo "<td>" . $pfNetworkPort->fields["ifalias"] . "</td>";
                 break;
         }
     }
     echo "</tr>";
 }
  You should have received a copy of the GNU Affero General Public License
  along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

  ------------------------------------------------------------------------

  @package   Plugin Monitoring for GLPI
  @author    David Durieux
  @co-author 
  @comment   
  @copyright Copyright (c) 2011-2012 Plugin Monitoring for GLPI team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      https://forge.indepnet.net/projects/monitoring/
  @since     2011

  ------------------------------------------------------------------------
*/
define('GLPI_ROOT', '../../..');
include GLPI_ROOT . "/inc/includes.php";
PluginMonitoringProfile::checkRight("componentscatalog", 'w');
simpleHeader($LANG['plugin_monitoring']['title'][0]);
if (isset($_POST['update'])) {
    $pmNetworkport = new PluginMonitoringNetworkport();
    //   $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
    //   $networkEquipment = new NetworkEquipment();
    $pmNetworkport->updateNetworkports();
    //   $networkEquipment->getFromDB($_POST['items_id']);
    //   $pmComponentscatalog_rule->isThisItemCheckRuleNetworkport($networkEquipment);
    glpi_header($_SERVER['HTTP_REFERER']);
}
commonFooter();
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
//set_error_handler('userErrorHandlerDebug');
$DB = new DB();
if (!$DB->connected) {
    die("No DB connection\n");
}
// Exit if plugin monitoring not activated
$Plugin = new Plugin();
if (!$Plugin->isActivated('monitoring')) {
    echo "Plugin monitoring not activated!\n";
    exit;
}
// * used to clean networkports
echo "Delete orphaned networkports ";
$pmNetworkport = new PluginMonitoringNetworkport();
$query = "SELECT `glpi_plugin_monitoring_networkports`.* \n           FROM `glpi_plugin_monitoring_networkports`\n           LEFT JOIN `glpi_networkports`\n               ON `glpi_plugin_monitoring_networkports`.`networkports_id`\n                  = `glpi_networkports`.`id`\n           WHERE `glpi_networkports`.`id` IS NULL";
$result = $DB->query($query);
while ($data = $DB->fetch_array($result)) {
    $pmNetworkport->delete($data);
    echo ".";
}
echo " done\n";
// * Clean services
echo "Delete orphaned services ";
$pmService = new PluginMonitoringService();
$query = "SELECT `glpi_plugin_monitoring_services`.* \n           FROM `glpi_plugin_monitoring_services`\n           LEFT JOIN `glpi_plugin_monitoring_componentscatalogs_hosts`\n               ON `plugin_monitoring_componentscatalogs_hosts_id`\n                  = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n           WHERE `glpi_plugin_monitoring_componentscatalogs_hosts`.`id` IS NULL";
$result = $DB->query($query);
while ($data = $DB->fetch_array($result)) {
    $pmService->delete($data);
    echo ".";
 static function getItemsDynamicly($parm)
 {
     global $DB;
     $pmCc_Rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmService = new PluginMonitoringService();
     $devices_present = array();
     $devicesnetworkport_present = array();
     if ($pmCc_Rule->getFromDB($parm->fields['id'])) {
         // Load right entity
         $pmComponentscatalog->getFromDB($pmCc_Rule->fields['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         if (!isset($_SESSION['glpiactiveprofile']['entities'])) {
             $_SESSION['glpiactiveprofile']['entities'] = array();
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $get_tmp = '';
         $itemtype = $pmCc_Rule->fields['itemtype'];
         $condition = importArrayFromDB($pmCc_Rule->fields['condition']);
         $params = Search::manageParams($itemtype, $condition, FALSE);
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $pmCc_Rule->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
         $queryd = "SELECT `glpi_plugin_monitoring_services`.`id` FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               LEFT JOIN `glpi_plugin_monitoring_services`\n                  ON `plugin_monitoring_componentscatalogs_hosts_id` =\n                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='NetworkEquipment'\n               AND `networkports_id`!='0'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devicesnetworkport_present[$data['id']] = 1;
         }
         $data = Search::prepareDatasForSearch($itemtype, $params);
         Search::constructSQL($data);
         $DBread = DBConnection::getReadConnection();
         $DBread->query("SET SESSION group_concat_max_len = 16384;");
         $result = $DBread->query($data['sql']['search']);
         /// Check group concat limit : if warning : increase limit
         if ($result2 = $DBread->query('SHOW WARNINGS')) {
             if ($DBread->numrows($result2) > 0) {
                 $res = $DBread->fetch_assoc($result2);
                 if ($res['Code'] == 1260) {
                     $DBread->query("SET SESSION group_concat_max_len = 4194304;");
                     $result = $DBread->query($data['sql']['search']);
                 }
             }
         }
         while ($data = $DB->fetch_array($result)) {
             $networkports_id = 0;
             $itemtype_device = $pmCc_Rule->fields['itemtype'];
             $items_id_device = $data['id'];
             if ($itemtype_device == 'PluginMonitoringNetworkport') {
                 $pmNetworkport = new PluginMonitoringNetworkport();
                 $pmNetworkport->getFromDB($data['id']);
                 $itemtype_device = $pmNetworkport->fields['itemtype'];
                 $items_id_device = $pmNetworkport->fields['items_id'];
                 $networkports_id = $pmNetworkport->fields['networkports_id'];
                 $networkPort = new NetworkPort();
                 if ($networkPort->getFromDB($networkports_id)) {
                     $querynet = "SELECT `itemtype`, `items_id`,\n                        `glpi_plugin_monitoring_services`.`id`\n                        FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                     LEFT JOIN `glpi_plugin_monitoring_services`\n                        ON `plugin_monitoring_componentscatalogs_hosts_id` =\n                           `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n                     WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                        AND `itemtype`='" . $itemtype_device . "'\n                        AND `items_id`='" . $items_id_device . "'\n                        AND `networkports_id`='" . $networkports_id . "'\n                           LIMIT 1";
                     $resultnet = $DB->query($querynet);
                     if ($DB->numrows($resultnet) == 0) {
                         $input = array();
                         $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                         $input['is_static'] = '0';
                         $input['items_id'] = $items_id_device;
                         $input['itemtype'] = $itemtype_device;
                         $input['networkports_id'] = $networkports_id;
                         $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                     } else {
                         $data2 = $DB->fetch_assoc($resultnet);
                         // modify entity of services (if entity of device is changed)
                         $itemtype = $data2['itemtype'];
                         $item = new $itemtype();
                         $item->getFromDB($data2['items_id']);
                         $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                           SET `entities_id`='" . $item->fields['entities_id'] . "'\n                              WHERE `id`='" . $data2['id'] . "'";
                         $DB->query($queryu);
                         unset($devicesnetworkport_present[$data2['id']]);
                     }
                 } else {
                     $pmNetworkport->delete($pmNetworkport->fields);
                 }
                 // Reload current entity
                 Session::changeActiveEntities($default_entity, $entities_isrecursive);
             } else {
                 $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                  WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                     AND `itemtype`='" . $itemtype_device . "'\n                     AND `items_id`='" . $items_id_device . "'\n                        LIMIT 1";
                 $resulth = $DB->query($queryh);
                 if ($DB->numrows($resulth) == '0') {
                     $input = array();
                     $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                     $input['is_static'] = '0';
                     $input['items_id'] = $items_id_device;
                     $input['itemtype'] = $itemtype_device;
                     $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                 } else {
                     $data2 = $DB->fetch_assoc($resulth);
                     // modify entity of services (if entity of device is changed)
                     $itemtype = $data2['itemtype'];
                     $item = new $itemtype();
                     $item->getFromDB($data2['items_id']);
                     $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                        SET `entities_id`='" . $item->fields['entities_id'] . "'\n                           WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                     $DB->query($queryu);
                     unset($devices_present[$data2['id']]);
                 }
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
         foreach ($devicesnetworkport_present as $id => $num) {
             $_SESSION['plugin_monitoring_hosts']['itemtype'] = $itemtype_device;
             $_SESSION['plugin_monitoring_hosts']['items_id'] = $items_id_device;
             $pmService->delete(array('id' => $id));
         }
     } else {
         // Purge
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $parm->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $parm->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
     }
     foreach ($devices_present as $id => $num) {
         $pmComponentscatalog_Host->delete(array('id' => $id));
     }
     return true;
 }