/**
  * 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
  *
  * Old agent have networkport speed in b/s and new in Mb/s
  */
 public function ComputerNetworkportOldFormat()
 {
     global $DB;
     $DB->connect();
     $_SESSION["plugin_fusioninventory_entity"] = 0;
     $_SESSION["glpiname"] = 'Plugin_FusionInventory';
     $a_computer = array();
     $a_computer['HARDWARE'] = array('NAME' => 'vbox-winxp', 'ARCHNAME' => 'MSWin32-x86-multi-thread', 'CHASSIS_TYPE' => '', 'DESCRIPTION' => '', 'OSCOMMENTS' => 'Service Pack 3 BAD', 'OSNAME' => 'Microsoft Windows XP Professionnel BAD', 'OSVERSION' => '5.1.2600 BAD', 'VMSYSTEM' => 'VirtualBox', 'WINCOMPANY' => 'siprossii', 'WINLANG' => '1036', 'WINOWNER' => 'test', 'WINPRODID' => '76413-OEM-0054453-04701', 'WINPRODKEY' => 'BW728-6G2PM-2MCWP-VCQ79-DCWX3', 'WORKGROUP' => 'WORKGROUP');
     $a_computer['NETWORKS'] = array(0 => array('DESCRIPTION' => 'Intel(R) 82577LM Gigabit Network Connection', 'IPADDRESS' => '192.168.20.50', 'IPDHCP' => '192.168.20.1', 'IPGATEWAY' => '192.168.20.1', 'IPMASK' => '255.255.255.0', 'IPSUBNET' => '192.168.20.0', 'MACADDR' => '5C:26:0A:38:C4:8B', 'PNPDEVICEID' => 'PCI\\VEN_8086&DEV_10EA&SUBSYS_040B1028&REV_05\\3&11583659&0&C8', 'SPEED' => '100000000', 'STATUS' => 'Up', 'TYPE' => 'ethernet', 'VIRTUALDEV' => '0'));
     $pfFormatconvert = new PluginFusioninventoryFormatconvert();
     $a_return = $pfFormatconvert->computerInventoryTransformation($a_computer);
     $a_reference = array('Intel(R) 82577LM Gigabit Network Connection-5c:26:0a:38:c4:8b' => array('name' => 'Intel(R) 82577LM Gigabit Network Connection', 'mac' => '5c:26:0a:38:c4:8b', 'instantiation_type' => 'NetworkPortEthernet', 'ipaddress' => array('192.168.20.50'), 'virtualdev' => 0, 'subnet' => '192.168.20.0', 'ssid' => '', 'gateway' => '192.168.20.1', 'netmask' => '255.255.255.0', 'dhcpserver' => '192.168.20.1', 'speed' => 100, 'logical_number' => 1));
     $this->assertEquals($a_reference, $a_return['networkport']);
 }