function run($jobstate)
 {
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfIPRange = new PluginFusioninventoryIPRange();
     $pfConfigSecurity = new PluginFusioninventoryConfigSecurity();
     $pfToolbox = new PluginFusioninventoryToolbox();
     $pfAgent->getFromDB($jobstate->fields['plugin_fusioninventory_agents_id']);
     $sxml_option = $this->message->addChild('OPTION');
     $sxml_option->addChild('NAME', 'NETDISCOVERY');
     $a_versions = importArrayFromDB($pfAgent->fields["version"]);
     // * Disabled by David Durieux, I think it's not required now * //
     //      if (((isset($a_versions["NETWORKDISCOVERY"])) AND ($a_versions["NETWORKDISCOVERY"] >= 1.3))
     //              OR !isset($a_versions["NETWORKDISCOVERY"])) {
     //         if (!file_exists(GLPI_PLUGIN_DOC_DIR."/fusioninventory/discovery.xml")) {
     //            PluginFusioninventorySnmpmodelImportExport::exportDictionnaryFile(FALSE);
     //         }
     //         $sxml_option->addChild('DICOHASH',
     //                                md5_file(GLPI_PLUGIN_DOC_DIR."/fusioninventory/discovery.xml"));
     //      }
     //      if (($pfAgent->fields["senddico"] == "1")) {
     //
     //         if (((isset($a_versions["NETWORKDISCOVERY"]))
     //                 AND ($a_versions["NETWORKDISCOVERY"] >= 1.3))) {
     //
     //            $sxml_option->addChild('DICO',
     //                                   file_get_contents(
     //                                           GLPI_PLUGIN_DOC_DIR."/fusioninventory/discovery.xml"));
     //         }
     //         $input = array();
     //         $input['id'] = $pfAgent->fields['id'];
     //         $input["senddico"] = "0";
     //         $pfAgent->update($input);
     //      }
     $sxml_param = $sxml_option->addChild('PARAM');
     $sxml_param->addAttribute('THREADS_DISCOVERY', $pfAgent->fields["threads_networkdiscovery"]);
     $sxml_param->addAttribute('TIMEOUT', $pfAgent->fields["timeout_networkdiscovery"]);
     $sxml_param->addAttribute('PID', $jobstate->fields['id']);
     $changestate = 0;
     //foreach ($a_Taskjobstates as $taskjobstate) {
     $taskjobstatedatas = $jobstate->fields;
     $sxml_rangeip = $sxml_option->addChild('RANGEIP');
     $pfTaskjob->getFromDB($taskjobstatedatas['plugin_fusioninventory_taskjobs_id']);
     $pfTaskjobstate->getFromDB($taskjobstatedatas['id']);
     $pfIPRange->getFromDB($taskjobstatedatas['items_id']);
     $sxml_rangeip->addAttribute('ID', $pfIPRange->fields['id']);
     if (!is_null($pfTaskjobstate->fields['specificity'])) {
         $a_split = explode("-", $pfTaskjobstate->fields['specificity']);
         $first_ip = $pfIPRange->getIp2long($pfIPRange->fields["ip_start"]);
         $last_ip = long2ip($first_ip + $a_split[1]);
         $first_ip = long2ip($first_ip + $a_split[0]);
         if ($first_ip != '0.0.0.0' && $last_ip != '0.0.0.0') {
             $sxml_rangeip->addAttribute('IPSTART', $first_ip);
             $sxml_rangeip->addAttribute('IPEND', $last_ip);
         }
     } else {
         $sxml_rangeip->addAttribute('IPSTART', $pfIPRange->fields["ip_start"]);
         $sxml_rangeip->addAttribute('IPEND', $pfIPRange->fields["ip_end"]);
     }
     $sxml_rangeip->addAttribute('ENTITY', $pfIPRange->fields["entities_id"]);
     if ($changestate == '0') {
         $pfTaskjobstate->changeStatus($pfTaskjobstate->fields['id'], 1);
         $pfTaskjoblog->addTaskjoblog($pfTaskjobstate->fields['id'], '0', 'PluginFusioninventoryAgent', '1', $pfAgent->fields["threads_networkdiscovery"] . ' threads', $pfAgent->fields["timeout_networkdiscovery"] . ' timeout');
         $changestate = $pfTaskjobstate->fields['id'];
     } else {
         $pfTaskjobstate->changeStatusFinish($pfTaskjobstate->fields['id'], $taskjobstatedatas['items_id'], $taskjobstatedatas['itemtype'], 0, "Merged with " . $changestate);
     }
     //}
     $pfIPRange_ConfigSecurity = new PluginFusioninventoryIPRange_ConfigSecurity();
     $a_auths = $pfIPRange_ConfigSecurity->find("`plugin_fusioninventory_ipranges_id`='" . $pfIPRange->fields['id'] . "'", "rank");
     foreach ($a_auths as $dataAuth) {
         $pfToolbox->addAuth($sxml_option, $dataAuth['plugin_fusioninventory_configsecurities_id']);
     }
     return $this->message;
 }
 function getDevicesOfIPRange($items_id)
 {
     global $DB;
     $devicesList = array();
     $pfIPRange = new PluginFusioninventoryIPRange();
     // get all snmpauth
     $a_snmpauth = getAllDatasFromTable("glpi_plugin_fusioninventory_configsecurities");
     $pfIPRange->getFromDB($items_id);
     // Search NetworkEquipment
     $query = "SELECT `glpi_networkequipments`.`id` AS `gID`,\n                         `glpi_ipaddresses`.`name` AS `gnifaddr`,\n                         `plugin_fusioninventory_configsecurities_id`\n                  FROM `glpi_networkequipments`\n                  LEFT JOIN `glpi_plugin_fusioninventory_networkequipments`\n                       ON `networkequipments_id`=`glpi_networkequipments`.`id`\n                  LEFT JOIN `glpi_networkports`\n                       ON `glpi_networkports`.`items_id`=`glpi_networkequipments`.`id`\n                          AND `glpi_networkports`.`itemtype`='NetworkEquipment'\n                  LEFT JOIN `glpi_networknames`\n                       ON `glpi_networknames`.`items_id`=`glpi_networkports`.`id`\n                          AND `glpi_networknames`.`itemtype`='NetworkPort'\n                  LEFT JOIN `glpi_ipaddresses`\n                       ON `glpi_ipaddresses`.`items_id`=`glpi_networknames`.`id`\n                          AND `glpi_ipaddresses`.`itemtype`='NetworkName'\n                  WHERE `glpi_networkequipments`.`is_deleted`='0'\n                       AND `plugin_fusioninventory_configsecurities_id`!='0'";
     if ($pfIPRange->fields['entities_id'] != '-1') {
         $entities = "(" . $pfIPRange->fields['entities_id'];
         foreach (getAncestorsOf("glpi_entities", $pfIPRange->fields['entities_id']) as $parent) {
             $entities .= ",{$parent}";
         }
         $entities .= ")";
         $query .= " AND `glpi_networkequipments`.`entities_id` IN " . $entities . " ";
     }
     $query .= " AND inet_aton(`glpi_ipaddresses`.`name`)\n                      BETWEEN inet_aton('" . $pfIPRange->fields['ip_start'] . "')\n                      AND inet_aton('" . $pfIPRange->fields['ip_end'] . "') ";
     $query .= " GROUP BY `glpi_networkequipments`.`id`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         if (isset($a_snmpauth[$data['plugin_fusioninventory_configsecurities_id']])) {
             $devicesList[] = array('NetworkEquipment' => $data['gID']);
         }
     }
     // Search Printer
     $query = "SELECT `glpi_printers`.`id` AS `gID`,\n                      `glpi_ipaddresses`.`name` AS `gnifaddr`,\n                      `plugin_fusioninventory_configsecurities_id`\n               FROM `glpi_printers`\n               LEFT JOIN `glpi_plugin_fusioninventory_printers`\n                       ON `printers_id`=`glpi_printers`.`id`\n               LEFT JOIN `glpi_networkports`\n                    ON `glpi_networkports`.`items_id`=`glpi_printers`.`id`\n                       AND `glpi_networkports`.`itemtype`='Printer'\n               LEFT JOIN `glpi_networknames`\n                    ON `glpi_networknames`.`items_id`=`glpi_networkports`.`id`\n                       AND `glpi_networknames`.`itemtype`='NetworkPort'\n               LEFT JOIN `glpi_ipaddresses`\n                    ON `glpi_ipaddresses`.`items_id`=`glpi_networknames`.`id`\n                       AND `glpi_ipaddresses`.`itemtype`='NetworkName'\n               WHERE `glpi_printers`.`is_deleted`=0\n                     AND `plugin_fusioninventory_configsecurities_id`!='0'";
     if ($pfIPRange->fields['entities_id'] != '-1') {
         $entities = "(" . $pfIPRange->fields['entities_id'];
         foreach (getAncestorsOf("glpi_entities", $pfIPRange->fields['entities_id']) as $parent) {
             $entities .= ",{$parent}";
         }
         $entities .= ")";
         $query .= "AND `glpi_printers`.`entities_id` IN " . $entities . " ";
     }
     $query .= " AND inet_aton(`glpi_ipaddresses`.`name`)\n                   BETWEEN inet_aton('" . $pfIPRange->fields['ip_start'] . "')\n                   AND inet_aton('" . $pfIPRange->fields['ip_end'] . "') ";
     $query .= " GROUP BY `glpi_printers`.`id`";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         if (isset($a_snmpauth[$data['plugin_fusioninventory_configsecurities_id']])) {
             $devicesList[] = array('Printer' => $data['gID']);
         }
     }
     return $devicesList;
 }