/**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfIPRange = new PluginFusioninventoryIPRange();
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer2', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer3', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Add IPRange
     $input = array('entities_id' => 0, 'name' => 'Office', 'ip_start' => '10.0.0.1', 'ip_end' => '10.0.0.254');
     $ipranges_id = $pfIPRange->add($input);
     $input = array('entities_id' => 0, 'name' => 'Office2', 'ip_start' => '10.0.2.1', 'ip_end' => '10.0.2.254');
     $ipranges_id2 = $pfIPRange->add($input);
     // Allow all agents to do network discovery
     $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules` " . " SET `is_active`='1' " . " WHERE `modulename`='NETWORKDISCOVERY'";
     $DB->query($query);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network discovery', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'discovery', 'method' => 'networkdiscovery', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"2"}]');
     $pfTaskjob->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network discovery2', 'is_active' => 1);
     $tasks2_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks2_id, 'entities_id' => 0, 'name' => 'discovery', 'method' => 'networkdiscovery', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id2 . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"3"}]');
     $pfTaskjob->add($input);
 }
 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;
 }
 /**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $entity = new Entity();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfIPRange = new PluginFusioninventoryIPRange();
     $networkEquipment = new NetworkEquipment();
     $networkPort = new NetworkPort();
     $printer = new Printer();
     $pfNetworkEquipment = new PluginFusioninventoryNetworkEquipment();
     // Create entities
     $_SESSION['glpiactive_entity'] = 0;
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (1, 'ent1', 0, 'Entité racine > ent1', 2)");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (2, 'ent2', 0, 'Entité racine > ent2', 2)");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (3, 'ent1.1', 1, 'Entité racine > ent1 > ent1.1', 3)");
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Create Network Equipments
     $input = array('name' => 'sw0', 'entities_id' => 0);
     $networkEquipment->add($input);
     $input = array('entities_id' => 0, 'name' => 'management', 'items_id' => 1, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.10'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 1, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw1', 'entities_id' => 1);
     $networkEquipment->add($input);
     $input = array('entities_id' => 1, 'name' => 'management', 'items_id' => 2, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.11'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 2, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw2', 'entities_id' => 2);
     $networkEquipment->add($input);
     $input = array('entities_id' => 2, 'name' => 'management', 'items_id' => 3, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.12'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 3, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw3/1.1', 'entities_id' => 3);
     $networkEquipment->add($input);
     $input = array('entities_id' => 3, 'name' => 'management', 'items_id' => 4, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.21'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 4, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     // Create Printers
     // Add IPRange
     $input = array('entities_id' => 1, 'name' => 'Office', 'ip_start' => '10.0.0.1', 'ip_end' => '10.0.0.254');
     $ipranges_id = $pfIPRange->add($input);
     // Allow all agents to do network discovery
     $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules` " . " SET `is_active`='1' " . " WHERE `modulename`='NETWORKINVENTORY'";
     $DB->query($query);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network inventory', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'inventory', 'method' => 'networkinventory', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"1"}]');
     $pfTaskjob->add($input);
 }
 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;
 }
  ------------------------------------------------------------------------

  @package   FusionInventory
  @author    David Durieux
  @co-author
  @copyright Copyright (c) 2010-2014 FusionInventory team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      http://www.fusioninventory.org/
  @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
$iprange = new PluginFusioninventoryIPRange();
Html::header(__('FusionInventory', 'fusioninventory'), $_SERVER["PHP_SELF"], "plugins", "pluginfusioninventorymenu", "iprange");
Session::checkRight('plugin_fusioninventory_iprange', READ);
PluginFusioninventoryMenu::displayMenu("mini");
if (isset($_POST["add"])) {
    Session::checkRight('plugin_fusioninventory_iprange', CREATE);
    if ($iprange->checkip($_POST)) {
        $_POST['ip_start'] = (int) $_POST['ip_start0'] . "." . (int) $_POST['ip_start1'] . ".";
        $_POST['ip_start'] .= (int) $_POST['ip_start2'] . "." . (int) $_POST['ip_start3'];
        $_POST['ip_end'] = (int) $_POST['ip_end0'] . "." . (int) $_POST['ip_end1'] . ".";
        $_POST['ip_end'] .= (int) $_POST['ip_end2'] . "." . (int) $_POST['ip_end3'];
        $iprange->add($_POST);
        Html::back();
    } else {
        Html::back();
    }