/**
  * After rules import device
  *
  * @param integer $items_id id of the device in GLPI DB (0 = created, other = merge)
  * @param varchar $itemtype itemtype of the device
  *
  * @return type
  */
 function rulepassed($items_id, $itemtype)
 {
     PluginFusioninventoryLogger::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n");
     PluginFusioninventoryLogger::logIfExtradebugAndDebugMode('fusioninventorycommunication', 'Function PluginFusinvsnmpCommunicationSNMPQuery->rulepassed().');
     $_SESSION["plugin_fusioninventory_entity"] = 0;
     PluginFusioninventoryConfig::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n");
     PluginFusioninventoryCommunication::addLog('Function PluginFusioninventoryCommunicationNetworkInventory->rulepassed().');
     $a_inventory = $_SESSION['SOURCE_XMLDEVICE'];
     $errors = '';
     $class = new $itemtype();
     if ($items_id == "0") {
         $input = array();
         $input['date_mod'] = date("Y-m-d H:i:s");
         if ($class->getFromDB($a_inventory[$a_inventory['itemtype']]['id'])) {
             $input['entities_id'] = $class->fields['entities_id'];
         } else {
             $input['entities_id'] = 0;
         }
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = "'" . $input['entities_id'] . "'";
         }
         $_SESSION["plugin_fusioninventory_entity"] = $input['entities_id'];
         $items_id = $class->add($input);
         if (isset($_SESSION['plugin_fusioninventory_rules_id'])) {
             $pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
             $inputrulelog = array();
             $inputrulelog['date'] = date('Y-m-d H:i:s');
             $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
             if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
                 $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
             }
             $inputrulelog['items_id'] = $items_id;
             $inputrulelog['itemtype'] = $itemtype;
             $inputrulelog['method'] = 'snmpinventory';
             $pfRulematchedlog->add($inputrulelog);
             $pfRulematchedlog->cleanOlddata($items_id, $itemtype);
             unset($_SESSION['plugin_fusioninventory_rules_id']);
         }
     }
     if ($itemtype == "PluginFusioninventoryUnmanaged") {
         $class->getFromDB($items_id);
         $input = array();
         $input['id'] = $class->fields['id'];
         if (!empty($a_inventory[$a_inventory['itemtype']]['name'])) {
             $input['name'] = $a_inventory[$a_inventory['itemtype']]['name'];
         }
         if (!empty($a_inventory[$a_inventory['itemtype']]['serial'])) {
             $input['serial'] = $a_inventory[$a_inventory['itemtype']]['serial'];
         }
         if (!empty($a_inventory['itemtype'])) {
             $input['itemtype'] = $a_inventory['itemtype'];
         }
         // TODO : add import ports
         PluginFusioninventoryToolbox::writeXML($items_id, serialize($_SESSION['SOURCE_XMLDEVICE']), 'PluginFusioninventoryUnmanaged');
         $class->update($input);
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==detail==] ==updatetheitem== Update ' . PluginFusioninventoryUnmanaged::getTypeName() . ' [[PluginFusioninventoryUnmanaged::' . $items_id . ']]';
         $this->addtaskjoblog();
     } else {
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==detail==] Update ' . $class->getTypeName() . ' [[' . $itemtype . '::' . $items_id . ']]';
         $this->addtaskjoblog();
         $errors .= $this->importDevice($itemtype, $items_id, $a_inventory);
     }
     return $errors;
 }
 /**
  * Prepare data and send them to rule engine
  *
  * @param type $p_xml simpleXML object
  */
 function sendCriteria($arrayinventory)
 {
     PluginFusioninventoryCommunication::addLog('Function PluginFusioninventoryCommunicationNetworkDiscovery->sendCriteria().');
     if (isset($arrayinventory['MAC']) && $arrayinventory['MAC'] == "00:00:00:00:00:00") {
         unset($arrayinventory['MAC']);
     }
     $_SESSION['SOURCE_XMLDEVICE'] = $arrayinventory;
     $input = array();
     // Global criterias
     if (isset($arrayinventory['SERIAL']) && !empty($arrayinventory['SERIAL'])) {
         $input['serial'] = $arrayinventory['SERIAL'];
     }
     if (isset($arrayinventory['MAC']) && !empty($arrayinventory['MAC'])) {
         $input['mac'][] = $arrayinventory['MAC'];
     }
     if (isset($arrayinventory['IP']) && !empty($arrayinventory['IP'])) {
         $input['ip'][] = $arrayinventory['IP'];
     }
     if (isset($arrayinventory['MODELSNMP']) && !empty($arrayinventory['MODELSNMP'])) {
         $input['model'] = $arrayinventory['MODELSNMP'];
     }
     if (isset($arrayinventory['SNMPHOSTNAME']) && !empty($arrayinventory['SNMPHOSTNAME'])) {
         $input['name'] = $arrayinventory['SNMPHOSTNAME'];
     } else {
         if (isset($arrayinventory['NETBIOSNAME']) && !empty($arrayinventory['NETBIOSNAME'])) {
             $input['name'] = $arrayinventory['NETBIOSNAME'];
         } else {
             if (isset($arrayinventory['DNSHOSTNAME']) && !empty($arrayinventory['DNSHOSTNAME'])) {
                 if (strpos($arrayinventory['DNSHOSTNAME'], '.') !== false) {
                     $splitname = explode('.', $arrayinventory['DNSHOSTNAME']);
                     $input['name'] = $splitname[0];
                     if (!isset($arrayinventory['WORKGROUP'])) {
                         unset($splitname[0]);
                         $arrayinventory['WORKGROUP'] = implode('.', $splitname);
                         $_SESSION['SOURCE_XMLDEVICE'] = $arrayinventory;
                     }
                 } else {
                     $input['name'] = $arrayinventory['DNSHOSTNAME'];
                 }
             }
         }
     }
     if (!isset($arrayinventory['ENTITY'])) {
         $arrayinventory['ENTITY'] = 0;
     }
     $input['entities_id'] = $arrayinventory['ENTITY'];
     if (isset($arrayinventory['TYPE'])) {
         switch ($arrayinventory['TYPE']) {
             case '1':
             case 'COMPUTER':
                 $input['itemtype'] = "Computer";
                 // Computer
                 break;
             case '2':
             case 'NETWORKING':
                 $input['itemtype'] = "NetworkEquipment";
                 break;
             case '3':
             case 'PRINTER':
                 $input['itemtype'] = "Printer";
                 break;
         }
     }
     $_SESSION['plugin_fusinvsnmp_datacriteria'] = serialize($input);
     $_SESSION['plugin_fusioninventory_classrulepassed'] = "PluginFusioninventoryCommunicationNetworkDiscovery";
     $rule = new PluginFusioninventoryInventoryRuleImportCollection();
     $data = $rule->processAllRules($input, array());
     PluginFusioninventoryConfig::logIfExtradebug("pluginFusioninventory-rules", $data);
     if (isset($data['action']) && $data['action'] == PluginFusioninventoryInventoryRuleImport::LINK_RESULT_DENIED) {
         $a_text = '';
         foreach ($input as $key => $data) {
             if (is_array($data)) {
                 $a_text[] = "[" . $key . "]:" . implode(", ", $data);
             } else {
                 $a_text[] = "[" . $key . "]:" . $data;
             }
         }
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '==importdenied== ' . implode(", ", $a_text);
         $this->addtaskjoblog();
         $pfIgnoredimport = new PluginFusioninventoryIgnoredimportdevice();
         $inputdb = array();
         if (isset($input['name'])) {
             $inputdb['name'] = $input['name'];
         }
         $inputdb['date'] = date("Y-m-d H:i:s");
         if (isset($input['itemtype'])) {
             $inputdb['itemtype'] = $input['itemtype'];
         }
         if (isset($input['serial'])) {
             $input['serial'] = $input['serial'];
         }
         if (isset($input['ip'])) {
             $inputdb['ip'] = exportArrayToDB($input['ip']);
         }
         if (isset($input['mac'])) {
             $inputdb['mac'] = exportArrayToDB($input['mac']);
         }
         if (isset($input['uuid'])) {
             $inputdb['uuid'] = $input['uuid'];
         }
         $inputdb['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
         $inputdb['method'] = 'netdiscovery';
         $pfIgnoredimport->add($inputdb);
         unset($_SESSION['plugin_fusioninventory_rules_id']);
     }
     if (isset($data['_no_rule_matches']) and $data['_no_rule_matches'] == '1') {
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = "'" . $input['entities_id'] . "'";
         }
         if (isset($input['itemtype']) && isset($data['action']) && $data['action'] == PluginFusioninventoryInventoryRuleImport::LINK_RESULT_CREATE) {
             $this->rulepassed(0, $input['itemtype'], $input['entities_id']);
         } else {
             if (isset($input['itemtype']) and !isset($data['action'])) {
                 $this->rulepassed(0, $input['itemtype'], $input['entities_id']);
             } else {
                 $this->rulepassed(0, "PluginFusioninventoryUnmanaged", $input['entities_id']);
             }
         }
     }
 }
 function import_netdiscovery($arrayinventory, $agentKey)
 {
     PluginFusioninventoryCommunication::addLog('Function PluginFusioninventorySnmpmodelImportExport->import_netdiscovery().');
     $ptap = new PluginFusioninventoryStateDiscovery();
     $pta = new PluginFusioninventoryAgent();
     $agent = $pta->InfosByKey($agentKey);
     if (isset($arrayinventory['AGENT']['START'])) {
         $ptap->updateState($arrayinventory['PROCESSNUMBER'], array('start_time' => date("Y-m-d H:i:s")), $agent['id']);
     } else {
         if (isset($arrayinventory['AGENT']['END'])) {
             $ptap->updateState($arrayinventory['PROCESSNUMBER'], array('end_time' => date("Y-m-d H:i:s")), $agent['id']);
         } else {
             if (isset($arrayinventory['AGENT']['EXIT'])) {
                 $ptap->endState($arrayinventory['PROCESSNUMBER'], date("Y-m-d H:i:s"), $agent['id']);
             } else {
                 if (isset($arrayinventory['AGENT']['NBIP'])) {
                     $ptap->updateState($arrayinventory['PROCESSNUMBER'], array('nb_ip' => $arrayinventory['AGENT']['NBIP']), $agent['id']);
                 }
             }
         }
     }
     if (isset($arrayinventory['AGENT']['AGENTVERSION'])) {
         $agent['last_contact'] = date("Y-m-d H:i:s");
         $pta->update($agent);
     }
     $_SESSION['glpi_plugin_fusioninventory_agentid'] = $agent['id'];
     $count_discovery_devices = 0;
     if (isset($arrayinventory['DEVICE'])) {
         if (is_int(key($arrayinventory['DEVICE']))) {
             $count_discovery_devices = count($arrayinventory['DEVICE']);
         } else {
             $count_discovery_devices = 1;
         }
     }
     if ($count_discovery_devices != "0") {
         $ptap->updateState($_SESSION['glpi_plugin_fusioninventory_processnumber'], array('nb_found' => $count_discovery_devices), $agent['id']);
         if (is_int(key($arrayinventory['DEVICE']))) {
             foreach ($arrayinventory['DEVICE'] as $discovery) {
                 if (count($discovery) > 0) {
                     $pfCommunicationNetworkDiscovery = new PluginFusioninventoryCommunicationNetworkDiscovery();
                     $pfCommunicationNetworkDiscovery->sendCriteria($discovery);
                 }
             }
         } else {
             $pfCommunicationNetworkDiscovery = new PluginFusioninventoryCommunicationNetworkDiscovery();
             $pfCommunicationNetworkDiscovery->sendCriteria($arrayinventory['DEVICE']);
         }
     }
 }