<commit_before><?php

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

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

   LICENSE

   This file is part of FusionInventory.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   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 General Public License for more details.

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

   ------------------------------------------------------------------------
   Original Author of file: <NAME>
   Co-authors of file:
   Purpose of file:
   ----------------------------------------------------------------------
 */

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

class PluginFusinvinventoryLib extends CommonDBTM {

   var $table = "glpi_plugin_fusinvinventory_libserialization";


   /**
   * Sarting create or update computer and get Entity
   *
   * @param $simpleXMLObj simplexml xmlobject of the computer
   * @param $items_id interger id of the computer
   * @param $new bool if 1 create a new computer
   *
   * @return nothing
   *
   **/
   function startAction($simpleXMLObj, $items_id, $new=0) {
      global $DB;

      $_SESSION["plugin_fusinvinventory_entity"] = "0";
      $xml = simplexml_load_string($_SESSION['SOURCEXML'],'SimpleXMLElement', LIBXML_NOCDATA);
      // ** Get entity with rules
         $input_rules = array();
         if ((isset($xml->CONTENT->BIOS->SSN)) AND (!empty($xml->CONTENT->BIOS->SSN))) {
            $input_rules['serialnumber'] = $xml->CONTENT->BIOS->SSN;
         }
         if ((isset($xml->CONTENT->HARDWARE->NAME)) AND (!empty($xml->CONTENT->HARDWARE->NAME))) {
            $input_rules['name'] = $xml->CONTENT->HARDWARE->NAME;
         }
         if (isset($xml->CONTENT->NETWORKS)) {
            foreach($xml->CONTENT->NETWORKS as $network) {
               if ((isset($network->IPADDRESS)) AND (!empty($network->IPADDRESS))) {
                  $input_rules['ip'][] = $network->IPADDRESS;
               }
               if ((isset($network->IPSUBNET)) AND (!empty($network->IPSUBNET))) {
                  $input_rules['subnet'][] = $network->IPADDRESS;
               }
            }
         }
         if ((isset($xml->CONTENT->HARDWARE->USERDOMAIN)) AND (!empty($xml->CONTENT->HARDWARE->USERDOMAIN))) {
            $input_rules['domain'] = $xml->CONTENT->HARDWARE->USERDOMAIN;
         }
         if ((isset($xml->CONTENT->ACCOUNTINFO->KEYNAME)) AND ($xml->CONTENT->ACCOUNTINFO->KEYNAME == 'TAG')) {
            if (isset($xml->CONTENT->ACCOUNTINFO->KEYVALUE)) {
               $input_rules['tag'] = $xml->CONTENT->ACCOUNTINFO->KEYVALUE;
            }
         }

         $ruleEntity = new PluginFusinvinventoryRuleEntityCollection();
         $dataEntity = array ();
         $dataEntity = $ruleEntity->processAllRules($input_rules, array());
         if (isset($dataEntity['entities_id'])) {
            $_SESSION["plugin_fusinvinventory_entity"] = $dataEntity['entities_id'];
         } else {
            $_SESSION["plugin_fusinvinventory_entity"] = "0";
         }


      if ($new == "0") {
         // Transfer if entity is different
         $Computer = new Computer();
         $Computer->getFromDB($items_id);
         $PluginFusioninventoryConfig = new PluginFusioninventoryConfig();
         if ($Computer->getEntityID() != $_SESSION["plugin_fusinvinventory_entity"]) {
            $Transfer = new Transfer();
            // get value in Config ($config['transfers_id_auto'])
            $Transfer->getFromDB($PluginFusioninventoryConfig->getValue($_SESSION["plugin_fusinvinventory_moduleid"],
                    'transfers_id_auto'));

            $item_to_transfer = array("Computer" => array($items_id=>$items_id));

            $Transfer->moveItems($item_to_transfer, $_SESSION["plugin_fusinvinventory_entity"], $Transfer->fields);
         }
         // Transfer agent entity
         $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
         if ($agent_id = $PluginFusioninventoryAgent->getAgentWithComputerid($items_id)) {
            $PluginFusioninventoryAgent->getFromDB($agent_id);
            if ($PluginFusioninventoryAgent->getEntityID() != $_SESSION["plugin_fusinvinventory_entity"]) {
               $PluginFusioninventoryAgent->fields['entities_id'] = $_SESSION["plugin_fusinvinventory_entity"];
               $PluginFusioninventoryAgent->update($PluginFusioninventoryAgent->fields);
            }
         }

      //if ($internalId = $this->isMachineExist()) {
         // Get internal ID with $items_id
         $query = "SELECT * FROM `glpi_plugin_fusinvinventory_libserialization`
            WHERE `computers_id`='".$items_id."'
               LIMIT 1";
         if ($result = $DB->query($query)) {
            if ($DB->numrows($result) == '1') {
               $a_serialized = $DB->fetch_assoc($result);
            }
         }
         if (isset($a_serialized['internal_id'])) {
            $internalId = $a_serialized['internal_id'];
         } else {
            // Importer les donnes de GLPI dans le xml
            $internalId = uniqid();
            $PluginFusinvinventoryInventory = new PluginFusinvinventoryInventory();
            $PluginFusinvinventoryInventory->createMachineInLib($items_id, $internalId);
         }

         // Link computer to agent FusionInventory
         $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
         $PluginFusioninventoryAgent->setAgentWithComputerid($items_id, $xml->DEVICEID);


         //Sections update
         $xmlSections = $this->_getXMLSections($simpleXMLObj);
         $this->updateLibMachine($xmlSections, $internalId);
         
         $PluginFusinvinventoryLibhook = new PluginFusinvinventoryLibhook();<commit_msg>Fix entity rules. references #695 and references #696 for subnet problem<commit_after><?php

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

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

   LICENSE

   This file is part of FusionInventory.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   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 General Public License for more details.

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

   ------------------------------------------------------------------------
   Original Author of file: <NAME>
   Co-authors of file:
   Purpose of file:
   ----------------------------------------------------------------------
 */

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

class PluginFusinvinventoryLib extends CommonDBTM {

   var $table = "glpi_plugin_fusinvinventory_libserialization";


   /**
   * Sarting create or update computer and get Entity
   *
   * @param $simpleXMLObj simplexml xmlobject of the computer
   * @param $items_id interger id of the computer
   * @param $new bool if 1 create a new computer
   *
   * @return nothing
   *
   **/
   function startAction($simpleXMLObj, $items_id, $new=0) {
      global $DB;

      $_SESSION["plugin_fusinvinventory_entity"] = "0";
      $xml = simplexml_load_string($_SESSION['SOURCEXML'],'SimpleXMLElement', LIBXML_NOCDATA);
      // ** Get entity with rules
         $input_rules = array();
         if ((isset($xml->CONTENT->BIOS->SSN)) AND (!empty($xml->CONTENT->BIOS->SSN))) {
            $input_rules['serialnumber'] = (string)$xml->CONTENT->BIOS->SSN;
         }
         if ((isset($xml->CONTENT->HARDWARE->NAME)) AND (!empty($xml->CONTENT->HARDWARE->NAME))) {
            $input_rules['name'] = (string)$xml->CONTENT->HARDWARE->NAME;
         }
         if (isset($xml->CONTENT->NETWORKS)) {
            foreach($xml->CONTENT->NETWORKS as $network) {
               if ((isset($network->IPADDRESS)) AND (!empty($network->IPADDRESS))) {
                  if ((string)$network->IPADDRESS != '127.0.0.1') {
                     $input_rules['ip'][] = (string)$network->IPADDRESS;
                  }
               }
               if ((isset($network->IPSUBNET)) AND (!empty($network->IPSUBNET))) {
                  $input_rules['subnet'][] = (string)$network->IPSUBNET;
               }
            }
         }
         if ((isset($xml->CONTENT->HARDWARE->USERDOMAIN)) AND (!empty($xml->CONTENT->HARDWARE->USERDOMAIN))) {
            $input_rules['domain'] = (string)$xml->CONTENT->HARDWARE->USERDOMAIN;
         }
         if ((isset($xml->CONTENT->ACCOUNTINFO->KEYNAME)) AND ($xml->CONTENT->ACCOUNTINFO->KEYNAME == 'TAG')) {
            if (isset($xml->CONTENT->ACCOUNTINFO->KEYVALUE)) {
               $input_rules['tag'] = (string)$xml->CONTENT->ACCOUNTINFO->KEYVALUE;
            }
         }

         $ruleEntity = new PluginFusinvinventoryRuleEntityCollection();
         $dataEntity = array ();
         $dataEntity = $ruleEntity->processAllRules($input_rules, array());
         if (isset($dataEntity['entities_id'])) {
            $_SESSION["plugin_fusinvinventory_entity"] = $dataEntity['entities_id'];
         } else {
            $_SESSION["plugin_fusinvinventory_entity"] = "0";
         }


      if ($new == "0") {
         // Transfer if entity is different
         $Computer = new Computer();
         $Computer->getFromDB($items_id);
         $PluginFusioninventoryConfig = new PluginFusioninventoryConfig();
         if ($Computer->getEntityID() != $_SESSION["plugin_fusinvinventory_entity"]) {
            $Transfer = new Transfer();
            // get value in Config ($config['transfers_id_auto'])
            $Transfer->getFromDB($PluginFusioninventoryConfig->getValue($_SESSION["plugin_fusinvinventory_moduleid"],
                    'transfers_id_auto'));

            $item_to_transfer = array("Computer" => array($items_id=>$items_id));

            $Transfer->moveItems($item_to_transfer, $_SESSION["plugin_fusinvinventory_entity"], $Transfer->fields);
         }
         // Transfer agent entity
         $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
         if ($agent_id = $PluginFusioninventoryAgent->getAgentWithComputerid($items_id)) {
            $PluginFusioninventoryAgent->getFromDB($agent_id);
            if ($PluginFusioninventoryAgent->getEntityID() != $_SESSION["plugin_fusinvinventory_entity"]) {
               $PluginFusioninventoryAgent->fields['entities_id'] = $_SESSION["plugin_fusinvinventory_entity"];
               $PluginFusioninventoryAgent->update($PluginFusioninventoryAgent->fields);
            }
         }

      //if ($internalId = $this->isMachineExist()) {
         // Get internal ID with $items_id
         $query = "SELECT * FROM `glpi_plugin_fusinvinventory_libserialization`
            WHERE `computers_id`='".$items_id."'
               LIMIT 1";
         if ($result = $DB->query($query)) {
            if ($DB->numrows($result) == '1') {
               $a_serialized = $DB->fetch_assoc($result);
            }
         }
         if (isset($a_serialized['internal_id'])) {
            $internalId = $a_serialized['internal_id'];
         } else {
            // Importer les donnes de GLPI dans le xml
            $internalId = uniqid();
            $PluginFusinvinventoryInventory = new PluginFusinvinventoryInventory();
            $PluginFusinvinventoryInventory->createMachineInLib($items_id, $internalId);
         }

         // Link computer to agent FusionInventory
         $PluginFusioninventoryAgent = new PluginFusioninventoryAgent();
         $PluginFusioninventoryAgent->setAgentWithComputerid($items_id, $xml->DEVICEID);

         //Sections update
         $xmlSections = $this->_getXMLSections($simpleXMLObj);
         $this->updateLibMachine($xmlSections, $internalId);
         
         $PluginFusinvinventoryLibhook = new PluginFusinvinventoryLibhook();
{'fraction_non_alphanumeric': 0.16648673658235658, 'fraction_numerical': 0.003392967304133251, 'mean_word_length': 2.3459752321981426, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 154, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 46}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6566134', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3503, 'n_words': 736}