<commit_before><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2012 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author <NAME>
   @copyright Copyright (c) 2010-2012 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2010
 
   ------------------------------------------------------------------------
 */

if (!defined('GLPI_ROOT')) {
   die("Sorry. You can't access directly to this file");
}

class PluginFusinvsnmpCommunicationSNMPQuery {
//   private $sxml, $deviceId, $ptd, $type='', $logFile;
   private $sxml, $ptd, $agent, $unknownDeviceCDP;
   private $a_ports = array();

   /**
    * Import data
    *
    *@param $p_DEVICEID XML code to import
    *@param $p_CONTENT XML code to import
    *@return "" (import ok) / error string (import ko)
    **/
   function import($p_DEVICEID, $p_CONTENT, $p_xml) {

      //$_SESSION['SOURCEXML'] = $p_xml;

      $result = false;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->import().'
      );

      $pfAgent = new PluginFusioninventoryAgent();
      $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();

      $this->agent = $pfAgent->InfosByKey($p_DEVICEID);

      $this->sxml = $p_xml;
      $errors = '';

      $_SESSION['glpi_plugin_fusioninventory_processnumber'] = $p_CONTENT->PROCESSNUMBER;
      if ($pfTaskjobstate->getFromDB($p_CONTENT->PROCESSNUMBER)) {
         if ($pfTaskjobstate->fields['state'] != "3") {
            $pfTaskjobstate->changeStatus($p_CONTENT->PROCESSNUMBER, 2);
            if ((!isset($p_CONTENT->AGENT->START)) AND (!isset($p_CONTENT->AGENT->END))) {
               $nb_devices = 0;
               $segs=$p_CONTENT->xpath('//DEVICE');
               $nb_devices = count($segs);
                  
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = $p_CONTENT->PROCESSNUMBER;
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = $this->agent['id'];
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'PluginFusioninventoryAgent';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = '6';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = $nb_devices.' ==fusinvsnmp::1==';
               $this->addtaskjoblog();

            }
            $errors.=$this->importContent($p_CONTENT);
            $result=true;
            if ($errors != '') {
               if (isset($_SESSION['glpi_plugin_fusioninventory_processnumber'])) {
                  $result=true;
               } else {
                  // It's PROLOG
                  $result=false;
               }
            }
            if (isset($p_CONTENT->AGENT->END)) {
               $pfTaskjobstate->changeStatusFinish($p_CONTENT->PROCESSNUMBER,
                                                         $this->agent['id'],
                                                         'PluginFusioninventoryAgent');
            }
            if (isset($p_CONTENT->AGENT->START)) {
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = $p_CONTENT->PROCESSNUMBER;
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = $this->agent['id'];
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'PluginFusioninventoryAgent';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = '6';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '==fusinvsnmp::6==';
               $this->addtaskjoblog();
            }
         }
      } else {
         $errors.=$this->importContent($p_CONTENT);
      }
      return $errors;
   }


   
   /**
    * Import the content (where have all devices)
    *@param $p_content CONTENT code to import
    *
    *@return errors string to be alimented if import ko / '' if ok
    **/
   function importContent($p_content) {
      global $LANG;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importContent().'
      );
      $pfAgent = new PluginFusioninventoryAgent();
      
      $errors='';
      $nbDevices = 0;

      foreach ($p_content->children() as $child) {
         PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
            'fusioninventorycommunication',
            $child->getName()
         );
         switch ($child->getName()) {
            
            case 'DEVICE' :
               if (isset($child->ERROR)) {
                  $itemtype = "";
                  if ((string)$child->ERROR->TYPE == "NETWORKING") {
                     $itemtype = "NetworkEquipment";
                  } else if ((string)$child->ERROR->TYPE == "PRINTER") {
                     $itemtype = "Printer";
                  }
                  $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.(string)$child->ERROR->MESSAGE.' [['.$itemtype.'::'.$child->ERROR->ID.']]';
                  $this->addtaskjoblog();
               } else if (!isset($child->INFO->COMMENTS)
                       AND !isset($child->INFO->NAME)
                       AND !isset($child->INFO->SERIAL)) {
                  $itemtype = "";
                  if ((string)$child->TYPE == "NETWORKING") {
                     $itemtype = "NetworkEquipment";
                  } else if ((string)$child->TYPE == "PRINTER") {
                     $itemtype = "Printer";
                  }
                  $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] No informations [['.$itemtype.'::'.$child->ID.']]';
                  $this->addtaskjoblog();
               } else {
                  if (count($child) > 0) {
                     $errors .= $this->sendCriteria($this->sxml->DEVICEID, $child);
                     $nbDevices++;
                  }
               }
               break;

            case 'AGENT' :
               if (isset($this->sxml->CONTENT->AGENT->AGENTVERSION)) {
                  $agent = $pfAgent->InfosByKey($this->sxml->DEVICEID);
                  $agent['fusioninventory_agent_version'] = $this->sxml->CONTENT->AGENT->AGENTVERSION;
                  $agent['last_agent_update'] = date("Y-m-d H:i:s");
                  $pfAgent->update($agent);
               }
               break;

            case 'PROCESSNUMBER' :
               break;

            case 'MODULEVERSION' :
               break;
            
            default :
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.$LANG['plugin_fusioninventory']['errors'][22].' CONTENT : '.$child->getName();
               $this->addtaskjoblog();
         }
      }
      return $errors;
   }



   /**
    * Import one device
    *
    * @global type $LANG
    * @param type $itemtype
    * @param type $items_id
    * 
    * @return errors string to be alimented if import ko / '' if ok 
    */
   function importDevice($itemtype, $items_id) {
      global $LANG;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importDevice().'
      );

      $p_xml = simplexml_load_string($_SESSION['SOURCE_XMLDEVICE'],'SimpleXMLElement', LIBXML_NOCDATA);

      // Write XML file
      if (isset($p_xml)) {
         $folder = substr($items_id,0,-1);
         if (empty($folder)) {
            $folder = '0';
         }
         if (!file_exists(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder)) {
            mkdir(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder, 0777, true);
         }
         $fileopen = fopen(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder."/".$items_id, 'w');
         fwrite($fileopen, $p_xml->asXML());
         fclose($fileopen);
       }

      $errors='';
      $this->deviceId=$items_id;
      switch ($itemtype) {
         
         case 'Printer':
            $this->type = 'Printer';
            break;
         
         case 'NetworkEquipment':
            $this->type = 'NetworkEquipment';
            break;
         
         default:
            $errors.=$LANG['plugin_fusioninventory']['errors'][22].' TYPE : '
                              .$p_xml->INFO->TYPE."\n";
      }
      if (!isset($p_xml->ERROR)) {
         $errors.=$this->importInfo($itemtype, $items_id);
         if ($this->deviceId!='') {
            foreach ($p_xml->children() as $child) {
               switch ($child->getName()) {
                  
                  case 'INFO': // already managed
                     break;
                  
                  case 'PORTS':
                     $errors.=$this->importPorts($child);
                     break;
                  
                  case 'CARTRIDGES':
                     if ($this->type == 'Printer') {
                        $errors.=$this->importCartridges($child);
                        break;
                     }
                  
                  case 'PAGECOUNTERS':
                     if ($this->type == 'Printer') {
                        $errors.=$this->importPageCounters($child);
                        break;
                     }
                  
                  default:
                     $errors.=$LANG['plugin_fusioninventory']['errors'][22].' DEVICE : '
                              .$child->getName()."\n";
               }
            }
            if ($errors=='') {
               $this->ptd->updateDB();
            }
         }
      }
      return $errors;
   }



   /**
    * Import INFO (Node info of the device
    *
    * @param type $itemtype
    * @param type $items_id
    * 
    * @return errors string to be alimented if import ko / '' if ok 
    */
   function importInfo($itemtype, $items_id) {

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importInfo().'
      );
      $errors='';
      $xml = simplexml_load_string($_SESSION['SOURCE_XMLDEVICE'],'SimpleXMLElement', LIBXML_NOCDATA);
      if ($itemtype == 'NetworkEquipment') {
         $errors.=$this->importInfoNetworking($xml->INFO);
      } elseif ($itemtype == 'Printer') {
         $errors.=$this->importInfoPrinter($xml->INFO);
      }
      if (!empty($errors)) {
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.$errors.' [['.$itemtype.'::'.$items_id.']]';
         $this->addtaskjoblog();
      }
      return $errors;
   }


   
   /**
    * Import INFO:Networking
    * 
    * @param $p_info INFO code to import
    *
    * @return errors string to be alimented if import ko / '' if ok
    **/
   function importInfoNetworking($p_info) {
      global $LANG;
      
      $errors='';
      $this->ptd = new PluginFusinvsnmpNetworkEquipment();
      $this->ptd->load($this->deviceId);

      $_SESSION["plugin_fusinvinventory_entity"] = $this->ptd->getValue('entities_id');
      if (!isset($_SESSION['glpiactiveentities_string'])) {
         $_SESSION['glpiactiveentities_string'] = "'".$this->ptd->getValue('entities_id')."'";
      }

      $a_lockable = PluginFusioninventoryLock::getLockFields('glpi_networkequipments', $this->ptd->getValue('id'));

      foreach ($p_info->children() as $child) {
         switch ($child->getName()) {
            
            case 'ID': // already managed
               break;
            
            case 'TYPE': // already managed
               break;
            
            case 'COMMENTS':
               $this->ptd->setValue('sysdescr', $p_info->COMMENTS[0]);
               break;
            
            case 'CPU':
               $this->ptd->setValue('cpu', $p_info->CPU[0]);
               break;
            
            case 'FIRMWARE':
               if (!in_array('networkequipmentfirmwares_id', $a_lockable)) {
                  $firmware = (string)$p_info->FIRMWARE;
                  if (strstr($firmware, "CW_VERSION")
                          OR strstr($firmware, "CW_INTERIM_VERSION")) {
                     $explode = explode("$", $firmware);
                     if (isset($explode[1])) {
                        $firmware = $explode[1];
                     }
                  }
                  $NetworkEquipmentFirmware = new NetworkEquipmentFirmware();
                  $this->ptd->setValue('networkequipmentfirmwares_id', $NetworkEquipmentFirmware->import(array('name' => $firmware)));
               }
               break;
               
            case 'MAC':
               if (!in_array('mac', $a_lockable)) {
                  $this->ptd->setValue('mac', $p_info->MAC[0]);
               }
               break;
            
            case 'MEMORY':
               if (!in_array('memory', $a_lockable)) {
                  $this->ptd->setValue('memory', $p_info->MEMORY[0]);
               }
               break;
               
            case 'MODEL':
               $NetworkEquipmentModel = new NetworkEquipmentModel();
               if (!in_array('networkequipmentmodels_id', $a_lockable)) {
                  $networkequipmentmodels_id = $NetworkEquipmentModel->import(array('name'=>(string)$p_info->MODEL));
                  $this->ptd->setValue('networkequipmentmodels_id', $networkequipmentmodels_id);
               }
               break;
               
            case 'LOCATION':
               if (!in_array('locations_id', $a_lockable)) {
                  $Location = new Location();
                  $this->ptd->setValue('locations_id', $Location->import(array('name' => (string)$p_info->LOCATION,
                                                                    'entities_id' => $this->ptd->getValue('entities_id'))));
               }
               break;
               
            case 'NAME':
               if (!in_array('name', $a_lockable)) {
                  $this->ptd->setValue('name', $p_info->NAME[0]);
               }
               break;
               
            case 'RAM':
               $this->ptd->setValue('ram', $p_info->RAM[0]);
               break;
            
            case 'SERIAL':
               if (!in_array('serial', $a_lockable)) {
                  $this->ptd->setValue('serial', $p_info->SERIAL[0]);
               }
               break;
               
            case 'UPTIME':
               $this->ptd->setValue('uptime', $p_info->UPTIME[0]);
               break;
            
            case 'IPS':
               $errors.=$this->importIps($child, $this->ptd->getValue('id'));
               break;
            
            default:
               $errors.=$LANG['plugin_fusioninventory']['errors'][22].' INFO : '.$child->getName()."\n";
               
         }
      }
      return $errors;      
   }


   
   /**
    * Import INFO:Printer
    * 
    * @param $p_info INFO code to import
    *
    * @return errors string to be alimented if import ko / '' if ok
    **/<commit_msg>add MANUFACTURER for Network devices too<commit_after><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2012 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author <NAME>
   @copyright Copyright (c) 2010-2012 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2010
 
   ------------------------------------------------------------------------
 */

if (!defined('GLPI_ROOT')) {
   die("Sorry. You can't access directly to this file");
}

class PluginFusinvsnmpCommunicationSNMPQuery {
//   private $sxml, $deviceId, $ptd, $type='', $logFile;
   private $sxml, $ptd, $agent, $unknownDeviceCDP;
   private $a_ports = array();

   /**
    * Import data
    *
    *@param $p_DEVICEID XML code to import
    *@param $p_CONTENT XML code to import
    *@return "" (import ok) / error string (import ko)
    **/
   function import($p_DEVICEID, $p_CONTENT, $p_xml) {

      //$_SESSION['SOURCEXML'] = $p_xml;

      $result = false;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->import().'
      );

      $pfAgent = new PluginFusioninventoryAgent();
      $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();

      $this->agent = $pfAgent->InfosByKey($p_DEVICEID);

      $this->sxml = $p_xml;
      $errors = '';

      $_SESSION['glpi_plugin_fusioninventory_processnumber'] = $p_CONTENT->PROCESSNUMBER;
      if ($pfTaskjobstate->getFromDB($p_CONTENT->PROCESSNUMBER)) {
         if ($pfTaskjobstate->fields['state'] != "3") {
            $pfTaskjobstate->changeStatus($p_CONTENT->PROCESSNUMBER, 2);
            if ((!isset($p_CONTENT->AGENT->START)) AND (!isset($p_CONTENT->AGENT->END))) {
               $nb_devices = 0;
               $segs=$p_CONTENT->xpath('//DEVICE');
               $nb_devices = count($segs);
                  
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = $p_CONTENT->PROCESSNUMBER;
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = $this->agent['id'];
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'PluginFusioninventoryAgent';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = '6';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = $nb_devices.' ==fusinvsnmp::1==';
               $this->addtaskjoblog();

            }
            $errors.=$this->importContent($p_CONTENT);
            $result=true;
            if ($errors != '') {
               if (isset($_SESSION['glpi_plugin_fusioninventory_processnumber'])) {
                  $result=true;
               } else {
                  // It's PROLOG
                  $result=false;
               }
            }
            if (isset($p_CONTENT->AGENT->END)) {
               $pfTaskjobstate->changeStatusFinish($p_CONTENT->PROCESSNUMBER,
                                                         $this->agent['id'],
                                                         'PluginFusioninventoryAgent');
            }
            if (isset($p_CONTENT->AGENT->START)) {
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['taskjobs_id'] = $p_CONTENT->PROCESSNUMBER;
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['items_id'] = $this->agent['id'];
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['itemtype'] = 'PluginFusioninventoryAgent';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['state'] = '6';
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '==fusinvsnmp::6==';
               $this->addtaskjoblog();
            }
         }
      } else {
         $errors.=$this->importContent($p_CONTENT);
      }
      return $errors;
   }


   
   /**
    * Import the content (where have all devices)
    *@param $p_content CONTENT code to import
    *
    *@return errors string to be alimented if import ko / '' if ok
    **/
   function importContent($p_content) {
      global $LANG;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importContent().'
      );
      $pfAgent = new PluginFusioninventoryAgent();
      
      $errors='';
      $nbDevices = 0;

      foreach ($p_content->children() as $child) {
         PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
            'fusioninventorycommunication',
            $child->getName()
         );
         switch ($child->getName()) {
            
            case 'DEVICE' :
               if (isset($child->ERROR)) {
                  $itemtype = "";
                  if ((string)$child->ERROR->TYPE == "NETWORKING") {
                     $itemtype = "NetworkEquipment";
                  } else if ((string)$child->ERROR->TYPE == "PRINTER") {
                     $itemtype = "Printer";
                  }
                  $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.(string)$child->ERROR->MESSAGE.' [['.$itemtype.'::'.$child->ERROR->ID.']]';
                  $this->addtaskjoblog();
               } else if (!isset($child->INFO->COMMENTS)
                       AND !isset($child->INFO->NAME)
                       AND !isset($child->INFO->SERIAL)) {
                  $itemtype = "";
                  if ((string)$child->TYPE == "NETWORKING") {
                     $itemtype = "NetworkEquipment";
                  } else if ((string)$child->TYPE == "PRINTER") {
                     $itemtype = "Printer";
                  }
                  $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] No informations [['.$itemtype.'::'.$child->ID.']]';
                  $this->addtaskjoblog();
               } else {
                  if (count($child) > 0) {
                     $errors .= $this->sendCriteria($this->sxml->DEVICEID, $child);
                     $nbDevices++;
                  }
               }
               break;

            case 'AGENT' :
               if (isset($this->sxml->CONTENT->AGENT->AGENTVERSION)) {
                  $agent = $pfAgent->InfosByKey($this->sxml->DEVICEID);
                  $agent['fusioninventory_agent_version'] = $this->sxml->CONTENT->AGENT->AGENTVERSION;
                  $agent['last_agent_update'] = date("Y-m-d H:i:s");
                  $pfAgent->update($agent);
               }
               break;

            case 'PROCESSNUMBER' :
               break;

            case 'MODULEVERSION' :
               break;
            
            default :
               $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.$LANG['plugin_fusioninventory']['errors'][22].' CONTENT : '.$child->getName();
               $this->addtaskjoblog();
         }
      }
      return $errors;
   }



   /**
    * Import one device
    *
    * @global type $LANG
    * @param type $itemtype
    * @param type $items_id
    * 
    * @return errors string to be alimented if import ko / '' if ok 
    */
   function importDevice($itemtype, $items_id) {
      global $LANG;

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importDevice().'
      );

      $p_xml = simplexml_load_string($_SESSION['SOURCE_XMLDEVICE'],'SimpleXMLElement', LIBXML_NOCDATA);

      // Write XML file
      if (isset($p_xml)) {
         $folder = substr($items_id,0,-1);
         if (empty($folder)) {
            $folder = '0';
         }
         if (!file_exists(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder)) {
            mkdir(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder, 0777, true);
         }
         $fileopen = fopen(GLPI_PLUGIN_DOC_DIR."/fusinvsnmp/".$itemtype."/".$folder."/".$items_id, 'w');
         fwrite($fileopen, $p_xml->asXML());
         fclose($fileopen);
       }

      $errors='';
      $this->deviceId=$items_id;
      switch ($itemtype) {
         
         case 'Printer':
            $this->type = 'Printer';
            break;
         
         case 'NetworkEquipment':
            $this->type = 'NetworkEquipment';
            break;
         
         default:
            $errors.=$LANG['plugin_fusioninventory']['errors'][22].' TYPE : '
                              .$p_xml->INFO->TYPE."\n";
      }
      if (!isset($p_xml->ERROR)) {
         $errors.=$this->importInfo($itemtype, $items_id);
         if ($this->deviceId!='') {
            foreach ($p_xml->children() as $child) {
               switch ($child->getName()) {
                  
                  case 'INFO': // already managed
                     break;
                  
                  case 'PORTS':
                     $errors.=$this->importPorts($child);
                     break;
                  
                  case 'CARTRIDGES':
                     if ($this->type == 'Printer') {
                        $errors.=$this->importCartridges($child);
                        break;
                     }
                  
                  case 'PAGECOUNTERS':
                     if ($this->type == 'Printer') {
                        $errors.=$this->importPageCounters($child);
                        break;
                     }
                  
                  default:
                     $errors.=$LANG['plugin_fusioninventory']['errors'][22].' DEVICE : '
                              .$child->getName()."\n";
               }
            }
            if ($errors=='') {
               $this->ptd->updateDB();
            }
         }
      }
      return $errors;
   }



   /**
    * Import INFO (Node info of the device
    *
    * @param type $itemtype
    * @param type $items_id
    * 
    * @return errors string to be alimented if import ko / '' if ok 
    */
   function importInfo($itemtype, $items_id) {

      PluginFusioninventoryLogger::logIfExtradebugAndDebugMode(
         'fusioninventorycommunication',
         'Function PluginFusinvsnmpCommunicationSNMPQuery->importInfo().'
      );
      $errors='';
      $xml = simplexml_load_string($_SESSION['SOURCE_XMLDEVICE'],'SimpleXMLElement', LIBXML_NOCDATA);
      if ($itemtype == 'NetworkEquipment') {
         $errors.=$this->importInfoNetworking($xml->INFO);
      } elseif ($itemtype == 'Printer') {
         $errors.=$this->importInfoPrinter($xml->INFO);
      }
      if (!empty($errors)) {
         $_SESSION['plugin_fusinvsnmp_taskjoblog']['comment'] = '[==fusinvsnmp::7==] '.$errors.' [['.$itemtype.'::'.$items_id.']]';
         $this->addtaskjoblog();
      }
      return $errors;
   }


   
   /**
    * Import INFO:Networking
    * 
    * @param $p_info INFO code to import
    *
    * @return errors string to be alimented if import ko / '' if ok
    **/
   function importInfoNetworking($p_info) {
      global $LANG;
      
      $errors='';
      $this->ptd = new PluginFusinvsnmpNetworkEquipment();
      $this->ptd->load($this->deviceId);

      $_SESSION["plugin_fusinvinventory_entity"] = $this->ptd->getValue('entities_id');
      if (!isset($_SESSION['glpiactiveentities_string'])) {
         $_SESSION['glpiactiveentities_string'] = "'".$this->ptd->getValue('entities_id')."'";
      }

      $a_lockable = PluginFusioninventoryLock::getLockFields('glpi_networkequipments', $this->ptd->getValue('id'));

      foreach ($p_info->children() as $child) {
         switch ($child->getName()) {
            
            case 'ID': // already managed
               break;
            
            case 'TYPE': // already managed
               break;
            
            case 'COMMENTS':
               $this->ptd->setValue('sysdescr', $p_info->COMMENTS[0]);
               break;
            
            case 'CPU':
               $this->ptd->setValue('cpu', $p_info->CPU[0]);
               break;
            
            case 'FIRMWARE':
               if (!in_array('networkequipmentfirmwares_id', $a_lockable)) {
                  $firmware = (string)$p_info->FIRMWARE;
                  if (strstr($firmware, "CW_VERSION")
                          OR strstr($firmware, "CW_INTERIM_VERSION")) {
                     $explode = explode("$", $firmware);
                     if (isset($explode[1])) {
                        $firmware = $explode[1];
                     }
                  }
                  $NetworkEquipmentFirmware = new NetworkEquipmentFirmware();
                  $this->ptd->setValue('networkequipmentfirmwares_id', $NetworkEquipmentFirmware->import(array('name' => $firmware)));
               }
               break;
               
            case 'MAC':
               if (!in_array('mac', $a_lockable)) {
                  $this->ptd->setValue('mac', $p_info->MAC[0]);
               }
               break;
            
            case 'MEMORY':
               if (!in_array('memory', $a_lockable)) {
                  $this->ptd->setValue('memory', $p_info->MEMORY[0]);
               }
               break;
               
            case 'MODEL':
               $NetworkEquipmentModel = new NetworkEquipmentModel();
               if (!in_array('networkequipmentmodels_id', $a_lockable)) {
                  $networkequipmentmodels_id = $NetworkEquipmentModel->import(array('name'=>(string)$p_info->MODEL));
                  $this->ptd->setValue('networkequipmentmodels_id', $networkequipmentmodels_id);
               }
               break;
               
            case 'LOCATION':
               if (!in_array('locations_id', $a_lockable)) {
                  $Location = new Location();
                  $this->ptd->setValue('locations_id', $Location->import(array('name' => (string)$p_info->LOCATION,
                                                                    'entities_id' => $this->ptd->getValue('entities_id'))));
               }
               break;
               
            case 'NAME':
               if (!in_array('name', $a_lockable)) {
                  $this->ptd->setValue('name', $p_info->NAME[0]);
               }
               break;
               
            case 'RAM':
               $this->ptd->setValue('ram', $p_info->RAM[0]);
               break;
            
            case 'SERIAL':
               if (!in_array('serial', $a_lockable)) {
                  $this->ptd->setValue('serial', $p_info->SERIAL[0]);
               }
               break;
               
            case 'UPTIME':
               $this->ptd->setValue('uptime', $p_info->UPTIME[0]);
               break;
            
            case 'IPS':
               $errors.=$this->importIps($child, $this->ptd->getValue('id'));
               break;

            case 'MANUFACTURER':
               if (!in_array('manufacturers_id', $a_lockable)) {
                  $Manufacturer = new Manufacturer();
                  $this->ptd->setValue('manufacturers_id', $Manufacturer->import(array('name' => (string)$p_info->MANUFACTURER)));
               }
               break;

            default:
               $errors.=$LANG['plugin_fusioninventory']['errors'][22].' INFO : '.$child->getName()."\n";
               
         }
      }
      return $errors;      
   }


   
   /**
    * Import INFO:Printer
    * 
    * @param $p_info INFO code to import
    *
    * @return errors string to be alimented if import ko / '' if ok
    **/
{'fraction_non_alphanumeric': 0.15621971899224807, 'fraction_numerical': 0.003815406976744186, 'mean_word_length': 1.6500561707591077, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 326, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 42}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623641', 'n_tokens_mistral': 9598, 'n_tokens_neox': 9115, 'n_words': 1561}