/**
  * Send Computer to inventoryruleimport
  *
  * @param $p_DEVICEID XML code to import
  * @param $p_CONTENT XML code of the Computer
  * @param $p_CONTENT XML code of all agent have sent
  *
  * @return nothing
  *
  **/
 function sendCriteria($p_DEVICEID, $arrayinventory)
 {
     if (isset($_SESSION['plugin_fusioninventory_entityrestrict'])) {
         unset($_SESSION['plugin_fusioninventory_entityrestrict']);
     }
     $this->device_id = $p_DEVICEID;
     // * Hacks
     // Hack to put OS in software
     if (isset($arrayinventory['CONTENT']['HARDWARE']['OSNAME'])) {
         $inputos = array();
         if (isset($arrayinventory['CONTENT']['HARDWARE']['OSCOMMENTS'])) {
             $inputos['COMMENTS'] = $arrayinventory['CONTENT']['HARDWARE']['OSCOMMENTS'];
         }
         $inputos['NAME'] = $arrayinventory['CONTENT']['HARDWARE']['OSNAME'];
         if (isset($arrayinventory['CONTENT']['HARDWARE']['OSVERSION'])) {
             $inputos['VERSION'] = $arrayinventory['CONTENT']['HARDWARE']['OSVERSION'];
         }
         if (isset($arrayinventory['CONTENT']['SOFTWARES']['VERSION'])) {
             $temparray = $arrayinventory['CONTENT']['SOFTWARES'];
             $arrayinventory['CONTENT']['SOFTWARES'] = array();
             $arrayinventory['CONTENT']['SOFTWARES'][] = $temparray;
         }
         $arrayinventory['CONTENT']['SOFTWARES'][] = $inputos;
     }
     // Hack for USB Printer serial
     if (isset($arrayinventory['CONTENT']['PRINTERS'])) {
         foreach ($arrayinventory['CONTENT']['PRINTERS'] as $key => $printer) {
             if (isset($printer['SERIAL']) and preg_match('/\\/$/', $printer['SERIAL'])) {
                 $arrayinventory['CONTENT']['PRINTERS'][$key]['SERIAL'] = preg_replace('/\\/$/', '', $printer['SERIAL']);
             }
         }
     }
     // Hack to remove Memories with Flash types see ticket
     // http://forge.fusioninventory.org/issues/1337
     if (isset($arrayinventory['CONTENT']['MEMORIES'])) {
         foreach ($arrayinventory['CONTENT']['MEMORIES'] as $key => $memory) {
             if (isset($memory['TYPE']) and preg_match('/Flash/', $memory['TYPE'])) {
                 unset($arrayinventory['CONTENT']['MEMORIES'][$key]);
             }
         }
     }
     // End hack
     $a_computerinventory = PluginFusioninventoryFormatconvert::computerInventoryTransformation($arrayinventory['CONTENT']);
     // Get tag is defined and put it in fusioninventory_agent table
     $tagAgent = "";
     if (isset($a_computerinventory['ACCOUNTINFO'])) {
         if (isset($a_computerinventory['ACCOUNTINFO']['KEYNAME']) && $a_computerinventory['ACCOUNTINFO']['KEYNAME'] == 'TAG') {
             if (isset($a_computerinventory['ACCOUNTINFO']['KEYVALUE']) && $a_computerinventory['ACCOUNTINFO']['KEYVALUE'] != '') {
                 $tagAgent = $a_computerinventory['ACCOUNTINFO']['KEYVALUE'];
             }
         }
     }
     $pfAgent = new PluginFusioninventoryAgent();
     $input = array();
     $input['id'] = $_SESSION['plugin_fusioninventory_agents_id'];
     $input['tag'] = $tagAgent;
     $pfAgent->update($input);
     $pfBlacklist = new PluginFusioninventoryInventoryComputerBlacklist();
     $a_computerinventory = $pfBlacklist->cleanBlacklist($a_computerinventory);
     if (isset($a_computerinventory['monitor'])) {
         foreach ($a_computerinventory['monitor'] as $num => $a_monit) {
             $a_computerinventory['monitor'][$num] = $pfBlacklist->cleanBlacklist($a_monit);
         }
     }
     $this->arrayinventory = $a_computerinventory;
     $input = array();
     // Global criterias
     if (isset($a_computerinventory['Computer']['serial']) and !empty($a_computerinventory['Computer']['serial'])) {
         $input['serial'] = $a_computerinventory['Computer']['serial'];
     }
     if (isset($a_computerinventory['Computer']['uuid']) and !empty($a_computerinventory['Computer']['uuid'])) {
         $input['uuid'] = $a_computerinventory['Computer']['uuid'];
     }
     foreach ($a_computerinventory['networkport'] as $network) {
         if (isset($network['virtualdev']) && $network['virtualdev'] != 1 or !isset($network['virtualdev'])) {
             if (isset($network['mac']) and !empty($network['mac'])) {
                 $input['mac'][] = $network['mac'];
             }
             foreach ($network['ipaddress'] as $ip) {
                 if ($ip != '127.0.0.1' && $ip != '::1') {
                     $input['ip'][] = $ip;
                 }
             }
             if (isset($network['subnet']) and !empty($network['subnet'])) {
                 $input['subnet'][] = $network['subnet'];
             }
         }
     }
     // Case of virtualmachines
     if (!isset($input['mac']) && !isset($input['ip'])) {
         foreach ($a_computerinventory['networkport'] as $network) {
             if (isset($network['mac']) and !empty($network['mac'])) {
                 $input['mac'][] = $network['mac'];
             }
             foreach ($network['ipaddress'] as $ip) {
                 if ($ip != '127.0.0.1' && $ip != '::1') {
                     $input['ip'][] = $ip;
                 }
             }
             if (isset($network['subnet']) and !empty($network['subnet'])) {
                 $input['subnet'][] = $network['subnet'];
             }
         }
     }
     if (isset($a_computerinventory['Computer']['os_license_number']) and !empty($a_computerinventory['Computer']['os_license_number'])) {
         $input['mskey'] = $a_computerinventory['Computer']['os_license_number'];
     }
     if (isset($a_computerinventory['Computer']['operatingsystems_id']) and !empty($a_computerinventory['Computer']['operatingsystems_id'])) {
         $input['osname'] = $a_computerinventory['Computer']['operatingsystems_id'];
     }
     if (isset($a_inventory['fusioninventorycomputer']['oscomment']) and !empty($a_inventory['fusioninventorycomputer']['oscomment'])) {
         $input['oscomment'] = $a_inventory['fusioninventorycomputer']['oscomment'];
     }
     if (isset($a_computerinventory['Computer']['computermodels_id']) and !empty($a_computerinventory['Computer']['computermodels_id'])) {
         $input['model'] = $a_computerinventory['Computer']['computermodels_id'];
     }
     if (isset($a_computerinventory['Computer']['domains_id']) and !empty($a_computerinventory['Computer']['domains_id'])) {
         $input['domains_id'] = $a_computerinventory['Computer']['domains_id'];
     }
     // TODO
     //         if (isset($arrayinventory['CONTENT']['STORAGES'])) {
     //            foreach($arrayinventory['CONTENT']['STORAGES'] as $storage) {
     //               if ((isset($storage['SERIALNUMBER'])) AND (!empty($storage['SERIALNUMBER']))) {
     //                  $input['partitionserial'][] = $storage['SERIALNUMBER'];
     //               }
     //            }
     //         }
     //         if (isset($arrayinventory['CONTENT']['computerdisk'])) {
     //            foreach($arrayinventory['CONTENT']['DRIVES'] as $drive) {
     //               if ((isset($drive['SERIAL'])) AND (!empty($drive['SERIAL']))) {
     //                  $input['hdserial'][] = $drive['SERIAL'];
     //               }
     //            }
     //         }
     $input['tag'] = $tagAgent;
     if (isset($a_computerinventory['Computer']['name']) and $a_computerinventory['Computer']['name'] != '') {
         $input['name'] = $a_computerinventory['Computer']['name'];
     } else {
         $input['name'] = '';
     }
     $input['itemtype'] = "Computer";
     // If transfer is disable, get entity and search only on this entity
     // (see http://forge.fusioninventory.org/issues/1503)
     $pfConfig = new PluginFusioninventoryConfig();
     $pfEntity = new PluginFusioninventoryEntity();
     // * entity rules
     $inputent = $input;
     if (isset($a_computerinventory['Computer']['domains_id']) and !empty($a_computerinventory['Computer']['domains_id'])) {
         $inputent['domain'] = $a_computerinventory['Computer']['domains_id'];
     }
     if (isset($inputent['serial'])) {
         $inputent['serialnumber'] = $inputent['serial'];
     }
     $ruleEntity = new PluginFusioninventoryInventoryRuleEntityCollection();
     // * Reload rules (required for unit tests)
     $ruleEntity->getCollectionPart();
     $dataEntity = $ruleEntity->processAllRules($inputent, array());
     if (isset($dataEntity['_ignore_import'])) {
         return;
     }
     if (isset($dataEntity['entities_id']) && $dataEntity['entities_id'] >= 0) {
         $_SESSION["plugin_fusioninventory_entity"] = $dataEntity['entities_id'];
         $input['entities_id'] = $dataEntity['entities_id'];
     } else {
         if (isset($dataEntity['entities_id']) && $dataEntity['entities_id'] == -1) {
             $input['entities_id'] = 0;
             $_SESSION["plugin_fusioninventory_entity"] = -1;
         } else {
             $input['entities_id'] = 0;
             $_SESSION["plugin_fusioninventory_entity"] = 0;
         }
     }
     if (isset($dataEntity['locations_id'])) {
         $_SESSION['plugin_fusioninventory_locations_id'] = $dataEntity['locations_id'];
     }
     // End entity rules
     $_SESSION['plugin_fusioninventory_classrulepassed'] = "PluginFusioninventoryInventoryComputerInventory";
     $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();
     // * Reload rules (required for unit tests)
     $ruleLocation->getCollectionPart();
     $dataLocation = $ruleLocation->processAllRules($input, array());
     if (isset($dataLocation['locations_id'])) {
         $_SESSION['plugin_fusioninventory_locations_id'] = $dataLocation['locations_id'];
     }
     $rule = new PluginFusioninventoryInventoryRuleImportCollection();
     // * Reload rules (required for unit tests)
     $rule->getCollectionPart();
     $data = $rule->processAllRules($input, array(), array('class' => $this));
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", $data);
     if (isset($data['_no_rule_matches']) and $data['_no_rule_matches'] == '1') {
         $this->rulepassed(0, "Computer");
     } else {
         if (!isset($data['found_equipment'])) {
             $pfIgnoredimportdevice = new PluginFusioninventoryIgnoredimportdevice();
             $inputdb = array();
             $inputdb['name'] = $input['name'];
             $inputdb['date'] = date("Y-m-d H:i:s");
             $inputdb['itemtype'] = "Computer";
             if (isset($a_computerinventory['Computer']['domains_id']) and !empty($a_computerinventory['Computer']['domains_id'])) {
                 $inputdb['domain'] = $a_computerinventory['Computer']['domains_id'];
             }
             if (isset($a_computerinventory['Computer']['serial'])) {
                 $inputdb['serial'] = $a_computerinventory['Computer']['serial'];
             }
             if (isset($a_computerinventory['Computer']['uuid'])) {
                 $inputdb['uuid'] = $a_computerinventory['Computer']['uuid'];
             }
             if (isset($input['ip'])) {
                 $inputdb['ip'] = $input['ip'];
             }
             if (isset($input['mac'])) {
                 $inputdb['mac'] = $input['mac'];
             }
             $inputdb['entities_id'] = $input['entities_id'];
             if (isset($input['ip'])) {
                 $inputdb['ip'] = exportArrayToDB($input['ip']);
             }
             if (isset($input['mac'])) {
                 $inputdb['mac'] = exportArrayToDB($input['mac']);
             }
             $inputdb['rules_id'] = $data['_ruleid'];
             $inputdb['method'] = 'inventory';
             $pfIgnoredimportdevice->add($inputdb);
         }
     }
 }
 /**
  * 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']);
             }
         }
     }
 }
 /**
  * Send XML of SNMP device to rules
  *
  * @param simplexml $p_CONTENT
  *
  * @return type
  */
 function sendCriteria($a_inventory)
 {
     PluginFusioninventoryCommunication::addLog('Function PluginFusioninventoryCommunicationNetworkInventory->sendCriteria().');
     $errors = '';
     // Manual blacklist
     if ($a_inventory[$a_inventory['itemtype']]['serial'] == 'null') {
         $a_inventory[$a_inventory['itemtype']]['serial'] = '';
     }
     // End manual blacklist
     $_SESSION['SOURCE_XMLDEVICE'] = $a_inventory;
     $input = array();
     // Global criterias
     if (!empty($a_inventory[$a_inventory['itemtype']]['serial'])) {
         $input['serial'] = $a_inventory[$a_inventory['itemtype']]['serial'];
     }
     if ($a_inventory['itemtype'] == 'NetworkEquipment') {
         if (!empty($a_inventory[$a_inventory['itemtype']]['mac'])) {
             $input['mac'][] = $a_inventory[$a_inventory['itemtype']]['mac'];
         }
         $input['itemtype'] = "NetworkEquipment";
     } else {
         if ($a_inventory['itemtype'] == 'Printer') {
             $input['itemtype'] = "Printer";
             if (isset($a_inventory['networkport'])) {
                 $a_ports = array();
                 if (is_int(key($a_inventory['networkport']))) {
                     $a_ports = $a_inventory['networkport'];
                 } else {
                     $a_ports[] = $a_inventory['networkport'];
                 }
                 foreach ($a_ports as $port) {
                     if (!empty($port['mac'])) {
                         $input['mac'][] = $port['mac'];
                     }
                     if (!empty($port['ip'])) {
                         $input['ip'][] = $port['ip'];
                     }
                 }
             }
         }
     }
     if (!empty($a_inventory[$a_inventory['itemtype']]['networkequipmentmodels_id'])) {
         $input['model'] = $a_inventory[$a_inventory['itemtype']]['networkequipmentmodels_id'];
     }
     if (!empty($a_inventory[$a_inventory['itemtype']]['name'])) {
         $input['name'] = $a_inventory[$a_inventory['itemtype']]['name'];
     }
     $_SESSION['plugin_fusinvsnmp_datacriteria'] = serialize($input);
     $_SESSION['plugin_fusioninventory_classrulepassed'] = "PluginFusioninventoryCommunicationNetworkInventory";
     $rule = new PluginFusioninventoryInventoryRuleImportCollection();
     PluginFusioninventoryConfig::logIfExtradebug("pluginFusioninventory-rules", "Input data : " . print_r($input, TRUE));
     $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();
         $pfIgnoredimportdevice = new PluginFusioninventoryIgnoredimportdevice();
         $inputdb = array();
         if (isset($input['name'])) {
             $inputdb['name'] = $input['name'];
         }
         $inputdb['date'] = date("Y-m-d H:i:s");
         $inputdb['itemtype'] = $input['itemtype'];
         if (isset($input['serial'])) {
             $input['serialnumber'] = $input['serial'];
         }
         if (isset($input['ip'])) {
             $inputdb['ip'] = exportArrayToDB($input['ip']);
         }
         if (isset($input['mac'])) {
             $inputdb['mac'] = exportArrayToDB($input['mac']);
         }
         $inputdb['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
         $inputdb['method'] = 'networkinventory';
         $pfIgnoredimportdevice->add($inputdb);
         unset($_SESSION['plugin_fusioninventory_rules_id']);
     }
     if (isset($data['_no_rule_matches']) and $data['_no_rule_matches'] == '1') {
         if (isset($input['itemtype']) && isset($data['action']) && $data['action'] == PluginFusioninventoryInventoryRuleImport::LINK_RESULT_CREATE) {
             $errors .= $this->rulepassed(0, $input['itemtype']);
         } else {
             if (isset($input['itemtype']) and !isset($data['action'])) {
                 $id_xml = $a_inventory[$a_inventory['itemtype']]['id'];
                 $classname = $input['itemtype'];
                 $class = new $classname();
                 if ($class->getFromDB($id_xml)) {
                     $errors .= $this->rulepassed($id_xml, $input['itemtype']);
                 } else {
                     $errors .= $this->rulepassed(0, $input['itemtype']);
                 }
             } else {
                 $errors .= $this->rulepassed(0, "PluginFusioninventoryUnmanaged");
             }
         }
     }
     return $errors;
 }
 /**
  * @test
  * network discovery
  */
 public function IgnoreNetworkDiscoveryImport()
 {
     global $DB;
     $DB->connect();
     $a_inventory = array('DNSHOSTNAME' => 'pctest', 'ENTITY' => 0, 'IP' => '192.168.20.3');
     $pfCommunicationNetworkDiscovery = new PluginFusioninventoryCommunicationNetworkDiscovery();
     $computer = new Computer();
     $pfUnmanaged = new PluginFusioninventoryUnmanaged();
     $pfIgnoredimportdevice = new PluginFusioninventoryIgnoredimportdevice();
     $GLPIlog = new GLPIlogs();
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = 1;
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = '1';
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'Computer';
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = 0;
     $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '';
     $pfCommunicationNetworkDiscovery->sendCriteria($a_inventory);
     $a_computers = $computer->find();
     $this->assertEquals(0, count($a_computers), 'Computer may not be added');
     $a_unknown = $pfUnmanaged->find();
     $this->assertEquals(0, count($a_unknown), 'Unmanaged may not be added');
     $a_ignored = $pfIgnoredimportdevice->find();
     $this->assertEquals(1, count($a_ignored), 'May have only one ignored device import');
 }