if (1) {
     $res .= "1'" . $errors . "'";
     $p_xml = gzuncompress($GLOBALS["HTTP_RAW_POST_DATA"]);
     $pxml = @simplexml_load_string($p_xml);
     if (isset($pxml->DEVICEID)) {
         $ptc->setXML("<?xml version='1.0' encoding='ISO-8859-1'?>\n<REPLY>\n</REPLY>");
         $pta = new PluginFusioninventoryAgents();
         $ptt = new PluginFusioninventoryTask();
         $ptcm = new PluginFusioninventoryConfigModules();
         $a_agent = $pta->InfosByKey($pxml->DEVICEID);
         $a_tasks = $ptt->find("`agent_id`='" . $a_agent['ID'] . "'", "date");
         $single = 0;
         $_SESSION['glpi_plugin_fusioninventory_addagentprocess'] = '0';
         foreach ($a_tasks as $task_id => $datas) {
             if ($a_tasks[$task_id]['action'] == 'INVENTORY' and $ptcm->isActivated('inventoryocs') and $a_agent['module_inventory'] == '1') {
                 $ptc->addInventory();
                 $input['ID'] = $task_id;
                 $ptt->delete($input);
                 $ocsinventory = '0';
                 $single = 1;
             }
             if ($a_tasks[$task_id]['action'] == 'NETDISCOVERY' and $ptcm->isActivated('netdiscovery') and $a_agent['module_netdiscovery'] == '1') {
                 $single = 1;
                 $ptc->addDiscovery($pxml, 1);
                 $input['ID'] = $task_id;
                 $ptt->delete($input);
             }
             if ($a_tasks[$task_id]['action'] == 'SNMPQUERY' and $ptcm->isActivated('snmp') and $a_agent['module_snmpquery'] == '1') {
                 $single = 1;
                 $ptc->addQuery($pxml, 1);
                 $input['ID'] = $task_id;
 function getTask($deviceid)
 {
     $pta = new PluginFusioninventoryAgents();
     $ptc = new PluginFusioninventoryCommunication();
     $a_agent = $pta->InfosByKey($deviceid);
     $a_tasks = $this->find("`agent_id`='" . $a_agent['ID'] . "'", "date");
     // TODO gest last
     foreach ($a_tasks as $task_id => $datas) {
         if ($a_tasks[$task_id]['action'] == 'INVENTORY') {
             $ptc->addInventory();
         }
     }
     // If not unique get all in addition ;)
 }
 /**
  * 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);
         }
     }
 }