/**
  * 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);
         }
     }
 }
 /**
  * @test
  */
 public function TwoRegexpEntitiesTest()
 {
     global $DB;
     $DB->connect();
     $DB->query('DELETE FROM glpi_entities where id>0');
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`, `tag`)\n         VALUES (1, 'entity A', 0, 'Entité racine > entity A', 2, 'entA')");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`, `tag`)\n         VALUES (2, 'entity B', 0, 'Entité racine > entity B', 2, 'entB')");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`, `tag`)\n         VALUES (3, 'entity C', 0, 'Entité racine > entity C', 2, 'entC')");
     // Add a rule for get entity tag (1)
     $rule = new Rule();
     $input = array('is_active' => 1, 'name' => 'entity rule 1', 'match' => 'AND', 'sub_type' => 'PluginFusioninventoryInventoryRuleEntity', 'ranking' => 1);
     $rule1_id = $rule->add($input);
     // Add criteria
     $rulecriteria = new RuleCriteria();
     $input = array('rules_id' => $rule1_id, 'criteria' => "name", 'pattern' => "/^([A-Za-z0-9]*) - ([A-Za-z0-9]*) - (.*)\$/", 'condition' => PluginFusioninventoryInventoryRuleEntity::REGEX_MATCH);
     $rulecriteria->add($input);
     // Add action
     $ruleaction = new RuleAction();
     $input = array('rules_id' => $rule1_id, 'action_type' => 'regex_result', 'field' => '_affect_entity_by_tag', 'value' => '#2');
     $ruleaction->add($input);
     // Add a rule for get entity tag (2)
     $rule = new Rule();
     $input = array('is_active' => 1, 'name' => 'entity rule 2', 'match' => 'AND', 'sub_type' => 'PluginFusioninventoryInventoryRuleEntity', 'ranking' => 2);
     $rule2_id = $rule->add($input);
     // Add criteria
     $rulecriteria = new RuleCriteria();
     $input = array('rules_id' => $rule2_id, 'criteria' => "name", 'pattern' => "/^([A-Za-z0-9]*) - (.*)\$/", 'condition' => PluginFusioninventoryInventoryRuleEntity::REGEX_MATCH);
     $rulecriteria->add($input);
     // Add action
     $ruleaction = new RuleAction();
     $input = array('rules_id' => $rule2_id, 'action_type' => 'regex_result', 'field' => '_affect_entity_by_tag', 'value' => '#1');
     $ruleaction->add($input);
     $input = array('name' => 'computer01 - entC');
     $ruleEntity = new PluginFusioninventoryInventoryRuleEntityCollection();
     $ruleEntity->getCollectionPart();
     $ent = $ruleEntity->processAllRules($input, array());
     $a_references = array('entities_id' => 3, '_ruleid' => $rule2_id);
     $this->assertEquals($a_references, $ent, 'Entity C');
     $input = array('name' => 'computer01 - blabla - entB');
     $ruleEntity = new PluginFusioninventoryInventoryRuleEntityCollection();
     $ruleEntity->getCollectionPart();
     $ent = $ruleEntity->processAllRules($input, array());
     $a_references = array('entities_id' => 2, '_ruleid' => $rule1_id);
     $this->assertEquals($a_references, $ent, 'Entity B');
 }