/**
  * @test
  */
 public function PeripheralUniqueSerialimport()
 {
     global $DB;
     $DB->connect();
     self::restore_database();
     $_SESSION['glpiactive_entity'] = 0;
     $_SESSION["plugin_fusioninventory_entity"] = 0;
     $_SESSION["glpiname"] = 'Plugin_FusionInventory';
     $pfConfig = new PluginFusioninventoryConfig();
     $pfiComputerLib = new PluginFusioninventoryInventoryComputerLib();
     $computer = new Computer();
     $GLPIlog = new GLPIlogs();
     $pxml = @simplexml_load_string($this->a_computer1_XML, 'SimpleXMLElement', LIBXML_NOCDATA);
     $arrayinventory = PluginFusioninventoryFormatconvert::XMLtoArray($pxml);
     $agent = new PluginFusioninventoryAgent();
     $agents_id = $agent->importToken($arrayinventory);
     $_SESSION['plugin_fusioninventory_agents_id'] = $agents_id;
     $pfInventoryComputerInventory = new PluginFusioninventoryInventoryComputerInventory();
     $pfInventoryComputerInventory->import('deviceid', $arrayinventory['CONTENT'], $arrayinventory);
     $computer->getFromDB(1);
     $this->assertEquals('ggheb7ne7', $computer->fields['serial'], 'Computer not updated correctly');
     $a_ref = array(1 => array('name' => 'Périphérique USB composite', 'id' => '1', 'serial' => '10075973', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '2', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0'), 2 => array('name' => 'H5321 gw Mobile Broadband Device', 'id' => '2', 'serial' => '187A047919938CM0', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '7', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0'), 3 => array('name' => 'Périphérique d’entrée USB', 'id' => '3', 'serial' => 'STM32_EMOTION2', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '8', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0'));
     $a_db_peripherals = getAllDatasFromTable('glpi_peripherals');
     foreach ($a_db_peripherals as $id => $data) {
         $data_temp = array('name' => $data['name'], 'id' => $data['id'], 'serial' => $data['serial'], 'peripheraltypes_id' => $data['peripheraltypes_id'], 'peripheralmodels_id' => $data['peripheralmodels_id'], 'manufacturers_id' => $data['manufacturers_id'], 'is_global' => $data['is_global'], 'is_deleted' => $data['is_deleted'], 'is_template' => $data['is_template'], 'is_dynamic' => $data['is_dynamic']);
         $a_db_peripherals[$id] = $data_temp;
     }
     $this->assertEquals($a_ref, $a_db_peripherals, 'List of peripherals');
     // Update computer and may not have new values in glpi_logs
     $query = "SELECT * FROM `glpi_logs`\n         ORDER BY `id` DESC LIMIT 1";
     $result = $DB->query($query);
     $data = $DB->fetch_assoc($result);
     $last_id = $data['id'];
     $pfInventoryComputerInventory->import('deviceid', $arrayinventory['CONTENT'], $arrayinventory);
     $data = getAllDatasFromTable('glpi_logs', "`id`>'" . $last_id . "'");
     $this->assertEquals(array(), $data, 'On update peripherals, may not have new lines in glpi_logs');
 }
    /**
     * @test
     */
    public function ConvertXMLtoArrayMultiVlan()
    {
        global $DB;
        $DB->connect();
        $xml_source = '<?xml version="1.0"?>
<REQUEST>
<CONTENT>
   <DEVICE>
      <INFO>
        <ID>123</ID>
        <NAME>FR-SW01</NAME>
        <SERIAL>CN536H7J</SERIAL>
        <TYPE>NETWORKING</TYPE>
      </INFO>
      <PORTS>
        <PORT>
          <IFDESCR>3</IFDESCR>
          <IFNAME>3</IFNAME>
          <IFNUMBER>3</IFNUMBER>
          <IFSTATUS>1</IFSTATUS>
          <IFTYPE>6</IFTYPE>
          <MAC>b4:39:d6:3b:22:bd</MAC>
          <VLANS>
            <VLAN>
              <NAME>VLAN160</NAME>
              <NUMBER>160</NUMBER>
            </VLAN>
            <VLAN>
              <NAME>VLAN161</NAME>
              <NUMBER>161</NUMBER>
            </VLAN>
          </VLANS>
        </PORT>
      </PORTS>
   </DEVICE>
</CONTENT>
</REQUEST>';
        $xml = simplexml_load_string($xml_source, 'SimpleXMLElement', LIBXML_NOCDATA);
        $pfFormatconvert = new PluginFusioninventoryFormatconvert();
        $a_return = $pfFormatconvert->XMLtoArray($xml);
        $a_reference = array('CONTENT' => array('DEVICE' => array(array('INFO' => array('ID' => '123', 'NAME' => 'FR-SW01', 'SERIAL' => 'CN536H7J', 'TYPE' => 'NETWORKING'), 'PORTS' => array('PORT' => array(array('IFDESCR' => '3', 'IFNAME' => '3', 'IFNUMBER' => '3', 'IFSTATUS' => '1', 'IFTYPE' => '6', 'MAC' => 'b4:39:d6:3b:22:bd', 'VLANS' => array('VLAN' => array(array('NAME' => 'VLAN160', 'NUMBER' => '160'), array('NAME' => 'VLAN161', 'NUMBER' => '161'))))))))));
        $this->assertEquals($a_reference, $a_return);
    }
    /**
     * @test
     */
    function DiscoveryDeviceConvert()
    {
        global $DB;
        $DB->connect();
        $sxml = '<?xml version="1.0" encoding="UTF-8" ?>
<REQUEST>
  <CONTENT>
    <DEVICE>
      <AUTHSNMP>1</AUTHSNMP>
      <DESCRIPTION>Eaton 5PX 1500</DESCRIPTION>
      <IP>192.168.20.196</IP>
      <MAC>00:20:85:f5:2d:19</MAC>
      <MANUFACTURER>Eaton</MANUFACTURER>
      <SNMPHOSTNAME>ups25</SNMPHOSTNAME>
      <TYPE>NETWORKING</TYPE>
    </DEVICE>
    <MODULEVERSION>2.2.0</MODULEVERSION>
    <PROCESSNUMBER>11</PROCESSNUMBER>
  </CONTENT>
  <DEVICEID>port004.bureau.siprossii.com-2012-12-20-16-27-27</DEVICEID>
  <QUERY>NETDISCOVERY</QUERY>
</REQUEST>';
        $xml = @simplexml_load_string($sxml, 'SimpleXMLElement', LIBXML_NOCDATA);
        PluginFusioninventoryFormatconvert::XMLtoArray($xml);
        $GLPIlog = new GLPIlogs();
        $GLPIlog->testSQLlogs();
        $GLPIlog->testPHPlogs();
    }
 /**
  * Manage communication with old protocol (XML over POST)
  *
  **/
 function handleOCSCommunication($rawdata, $xml = '', $output = 'ext')
 {
     // ***** For debug only ***** //
     //$rawdata = gzcompress('');
     // ********** End ********** //
     $config = new PluginFusioninventoryConfig();
     $user = new User();
     //      ob_start();
     if (!isset($_SESSION['glpiID'])) {
         $users_id = $config->getValue('users_id');
         $_SESSION['glpiID'] = $users_id;
         $user->getFromDB($users_id);
         Session::changeActiveEntities();
         $_SESSION["glpiname"] = $user->getField('name');
         $_SESSION['glpiactiveprofile'] = array();
         $_SESSION['glpiactiveprofile']['interface'] = 'central';
         $_SESSION['glpiactiveprofile']['internet'] = 'w';
         $_SESSION['glpiactiveprofile']['computer'] = 'w';
         $_SESSION['glpiactiveprofile']['monitor'] = 'w';
         $_SESSION['glpiactiveprofile']['printer'] = 'w';
         $_SESSION['glpiactiveprofile']['peripheral'] = 'w';
         $_SESSION['glpiactiveprofile']['networking'] = 'w';
         $_SESSION["glpi_plugin_fusioninventory_profile"]['unmanaged'] = 'w';
     }
     $communication = new PluginFusioninventoryCommunication();
     $pfToolbox = new PluginFusioninventoryToolbox();
     // identify message compression algorithm
     $agent = new PluginFusioninventoryAgent();
     $agent->disableDebug();
     $compressmode = '';
     if (!empty($xml)) {
         $compressmode = 'none';
     } else {
         if ($_SERVER['CONTENT_TYPE'] == "application/x-compress-zlib") {
             $xml = gzuncompress($rawdata);
             $compressmode = "zlib";
         } else {
             if ($_SERVER['CONTENT_TYPE'] == "application/x-compress-gzip") {
                 $xml = $pfToolbox->gzdecode($rawdata);
                 $compressmode = "gzip";
             } else {
                 if ($_SERVER['CONTENT_TYPE'] == "application/xml") {
                     $xml = $rawdata;
                     $compressmode = 'none';
                 } else {
                     # try each algorithm successively
                     if ($xml = gzuncompress($rawdata)) {
                         $compressmode = "zlib";
                     } else {
                         if ($xml = $pfToolbox->gzdecode($rawdata)) {
                             $compressmode = "gzip";
                         } else {
                             if ($xml = gzinflate(substr($rawdata, 2))) {
                                 // accept deflate for OCS agent 2.0 compatibility,
                                 // but use zlib for answer
                                 if (strstr($xml, "<QUERY>PROLOG</QUERY>") and !strstr($xml, "<TOKEN>")) {
                                     $compressmode = "zlib";
                                 } else {
                                     $compressmode = "deflate";
                                 }
                             } else {
                                 $xml = $rawdata;
                                 $compressmode = 'none';
                             }
                         }
                     }
                 }
             }
         }
     }
     $agent->restoreDebug();
     // check if we are in ssl only mode
     $ssl = $config->getValue('ssl_only');
     if ($ssl == "1" and (!isset($_SERVER["HTTPS"]) or $_SERVER["HTTPS"] != "on")) {
         if ($output == 'glpi') {
             Session::addMessageAfterRedirect('SSL REQUIRED BY SERVER', false, ERROR);
         } else {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n   <ERROR>SSL REQUIRED BY SERVER</ERROR>\n</REPLY>");
             $communication->sendMessage($compressmode);
         }
         return;
     }
     PluginFusioninventoryConfig::logIfExtradebug('pluginFusioninventory-dial' . uniqid(), $xml);
     // Check XML integrity
     $pxml = '';
     if ($pxml = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) {
     } else {
         if ($pxml = @simplexml_load_string(utf8_encode($xml), 'SimpleXMLElement', LIBXML_NOCDATA)) {
             $xml = utf8_encode($xml);
         } else {
             $xml = preg_replace('/<FOLDER>.*?<\\/SOURCE>/', '', $xml);
             $pxml = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
             if (!$pxml) {
                 if ($output == 'glpi') {
                     Session::addMessageAfterRedirect('XML not well formed!', false, ERROR);
                 } else {
                     $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n   <ERROR>XML not well formed!</ERROR>\n</REPLY>");
                     $communication->sendMessage($compressmode);
                 }
                 return;
             }
         }
     }
     $_SESSION['plugin_fusioninventory_compressmode'] = $compressmode;
     // Convert XML into PHP array
     $arrayinventory = PluginFusioninventoryFormatconvert::XMLtoArray($pxml);
     unset($pxml);
     $deviceid = '';
     if (isset($arrayinventory['DEVICEID'])) {
         $deviceid = $arrayinventory['DEVICEID'];
     }
     $agent = new PluginFusioninventoryAgent();
     $agents_id = $agent->importToken($arrayinventory);
     $_SESSION['plugin_fusioninventory_agents_id'] = $agents_id;
     if (!$communication->import($arrayinventory)) {
         if ($deviceid != '') {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n</REPLY>");
             $a_agent = $agent->InfosByKey($deviceid);
             // Get taskjob in waiting
             $communication->getTaskAgent($a_agent['id']);
             // ******** Send XML
             $communication->addInventory($a_agent['id']);
             $communication->addProlog();
             $communication->sendMessage($compressmode);
         }
     } else {
         if ($output == 'glpi') {
             Session::addMessageAfterRedirect('XML has been imported succesfully!');
         } else {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n</REPLY>");
             $communication->sendMessage($compressmode);
         }
     }
 }