/**
  * @see RuleCollection::replayRulesOnExistingDB()
  **/
 function replayRulesOnExistingDB($offset = 0, $maxtime = 0, $items = array(), $params = array())
 {
     global $DB;
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB started : " . date("r") . "\n";
     }
     $nb = 0;
     $i = $offset;
     if (count($items) == 0) {
         //Select all the differents software
         $sql = "SELECT DISTINCT `glpi_softwares`.`name`,\n                        `glpi_manufacturers`.`name` AS manufacturer,\n                        `glpi_softwares`.`manufacturers_id` AS manufacturers_id,\n                        `glpi_softwares`.`entities_id` AS entities_id,\n                        `glpi_softwares`.`is_helpdesk_visible` AS helpdesk\n                 FROM `glpi_softwares`\n                 LEFT JOIN `glpi_manufacturers`\n                     ON (`glpi_manufacturers`.`id` = `glpi_softwares`.`manufacturers_id`)";
         // Do not replay on dustbin and templates
         $sql .= "WHERE `glpi_softwares`.`is_deleted` = '0'\n                        AND `glpi_softwares`.`is_template` = '0' ";
         if (isset($params['manufacturer']) && $params['manufacturer']) {
             $sql .= " AND `glpi_softwares`.`manufacturers_id` = '" . $params['manufacturer'] . "'";
         }
         if ($offset) {
             $sql .= " LIMIT " . intval($offset) . ",999999999";
         }
         $res = $DB->query($sql);
         $nb = $DB->numrows($res) + $offset;
         $step = $nb > 1000 ? 50 : ($nb > 20 ? floor($DB->numrows($res) / 20) : 1);
         while ($input = $DB->fetch_assoc($res)) {
             if (!($i % $step)) {
                 if (isCommandLine()) {
                     printf(__('%1$s - replay rules on existing database: %2$s/%3$s (%4$s Mio)') . "\n", date("H:i:s"), $i, $nb, round(memory_get_usage() / (1024 * 1024), 2));
                 } else {
                     Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
                 }
             }
             //If manufacturer is set, then first run the manufacturer's dictionnary
             if (isset($input["manufacturer"])) {
                 $input["manufacturer"] = Manufacturer::processName(addslashes($input["manufacturer"]));
             }
             //Replay software dictionnary rules
             $res_rule = $this->processAllRules($input, array(), array());
             if (isset($res_rule["name"]) && $res_rule["name"] != $input["name"] || isset($res_rule["version"]) && $res_rule["version"] != '' || isset($res_rule['new_entities_id']) && $res_rule['new_entities_id'] != $input['entities_id'] || isset($res_rule['is_helpdesk_visible']) && $res_rule['is_helpdesk_visible'] != $input['helpdesk'] || isset($res_rule['manufacturer']) && $res_rule['manufacturer'] != $input['manufacturer']) {
                 $IDs = array();
                 //Find all the softwares in the database with the same name and manufacturer
                 $sql = "SELECT `id`\n                       FROM `glpi_softwares`\n                       WHERE `name` = '" . $input["name"] . "'\n                             AND `manufacturers_id` = '" . $input["manufacturers_id"] . "'";
                 $res_soft = $DB->query($sql);
                 if ($DB->numrows($res_soft) > 0) {
                     //Store all the software's IDs in an array
                     while ($result = $DB->fetch_assoc($res_soft)) {
                         $IDs[] = $result["id"];
                     }
                     //Replay dictionnary on all the softwares
                     $this->replayDictionnaryOnSoftwaresByID($IDs, $res_rule);
                 }
             }
             $i++;
             if ($maxtime) {
                 $crt = explode(" ", microtime());
                 if ($crt[0] + $crt[1] > $maxtime) {
                     break;
                 }
             }
         }
         // each distinct software
         if (isCommandLine()) {
             printf(__('Replay rules on existing database: %1$s/%2$s') . "   \n", $i, $nb);
         } else {
             Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
         }
     } else {
         $this->replayDictionnaryOnSoftwaresByID($items);
         return true;
     }
     if (isCommandLine()) {
         printf(__('Replay rules on existing database ended on %s') . "\n", date("r"));
     }
     return $i == $nb ? -1 : $i;
 }
 /**
  * Replay collection rules on an existing DB for model dropdowns
  *
  * @param $offset    offset used to begin (default 0)
  * @param $maxtime   maximum time of process (reload at the end) (default 0)
  *
  * @return -1 on completion else current offset
  **/
 function replayRulesOnExistingDBForModel($offset = 0, $maxtime = 0)
 {
     global $DB;
     if (isCommandLine()) {
         printf(__('Replay rules on existing database started on %s') . "\n", date("r"));
     }
     // Model check : need to check using manufacturer extra data
     if (strpos($this->item_table, 'models') === false) {
         _e('Error replaying rules');
         return false;
     }
     $model_table = getPlural(str_replace('models', '', $this->item_table));
     $model_field = getForeignKeyFieldForTable($this->item_table);
     // Need to give manufacturer from item table
     $Sql = "SELECT DISTINCT `glpi_manufacturers`.`id` AS idmanu,\n                     `glpi_manufacturers`.`name` AS manufacturer,\n                     `" . $this->item_table . "`.`id`,\n                     `" . $this->item_table . "`.`name` AS name,\n                     `" . $this->item_table . "`.`comment`\n              FROM `" . $this->item_table . "`,\n                   `{$model_table}`\n              LEFT JOIN `glpi_manufacturers`\n                  ON (`{$model_table}`.`manufacturers_id` = `glpi_manufacturers`.`id`)\n              WHERE `{$model_table}`.`{$model_field}` = `" . $this->item_table . "`.`id`";
     if ($offset) {
         $Sql .= " LIMIT " . intval($offset) . ",999999999";
     }
     $result = $DB->query($Sql);
     $nb = $DB->numrows($result) + $offset;
     $i = $offset;
     if ($result && $nb > $offset) {
         // Step to refresh progressbar
         $step = $nb > 20 ? floor($nb / 20) : 1;
         $tocheck = array();
         while ($data = $DB->fetch_assoc($result)) {
             if (!($i % $step)) {
                 if (isCommandLine()) {
                     printf(__('Replay rules on existing database: %1$s/%2$s') . "\r", $i, $nb);
                 } else {
                     Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
                 }
             }
             // Model case
             if (isset($data["manufacturer"])) {
                 $data["manufacturer"] = Manufacturer::processName(addslashes($data["manufacturer"]));
             }
             //Replay Type dictionnary
             $ID = Dropdown::importExternal(getItemTypeForTable($this->item_table), addslashes($data["name"]), -1, $data, addslashes($data["comment"]));
             if ($data['id'] != $ID) {
                 $tocheck[$data["id"]][] = $ID;
                 $sql = "UPDATE `{$model_table}`\n                                          SET `{$model_field}` = '{$ID}'\n                                          WHERE `{$model_field}` = '" . $data['id'] . "'";
                 if (empty($data['idmanu'])) {
                     $sql .= " AND (`manufacturers_id` IS NULL\n                                 OR `manufacturers_id` = '0')";
                 } else {
                     $sql .= " AND `manufacturers_id` = '" . $data['idmanu'] . "'";
                 }
                 $DB->query($sql);
             }
             $i++;
             if ($maxtime) {
                 $crt = explode(" ", microtime());
                 if ($crt[0] + $crt[1] > $maxtime) {
                     break;
                 }
             }
         }
         foreach ($tocheck as $ID => $tab) {
             $sql = "SELECT COUNT(*)\n                                 FROM `{$model_table}`\n                                 WHERE `{$model_field}` = '{$ID}'";
             $result = $DB->query($sql);
             $deletecartmodel = false;
             // No item left : delete old item
             if ($result && $DB->result($result, 0, 0) == 0) {
                 $Sql = "DELETE\n                                    FROM `" . $this->item_table . "`\n                                    WHERE `id` = '{$ID}'";
                 $resdel = $DB->query($Sql);
                 $deletecartmodel = true;
             }
             // Manage cartridge assoc Update items
             if ($this->getRuleClassName() == 'RuleDictionnaryPrinterModel') {
                 $sql = "SELECT *\n                       FROM `glpi_cartridgeitems_printermodels`\n                       WHERE `printermodels_id` = '{$ID}'";
                 if ($result = $DB->query($sql)) {
                     if ($DB->numrows($result)) {
                         // Get compatible cartridge type
                         $carttype = array();
                         while ($data = $DB->fetch_assoc($result)) {
                             $carttype[] = $data['cartridgeitems_id'];
                         }
                         // Delete cartrodges_assoc
                         if ($deletecartmodel) {
                             $sql = "DELETE\n                                FROM `glpi_cartridgeitems_printermodels`\n                                WHERE `printermodels_id` = 'id'";
                             $DB->query($sql);
                         }
                         // Add new assoc
                         $ct = new CartridgeItem();
                         foreach ($carttype as $cartID) {
                             foreach ($tab as $model) {
                                 $ct->addCompatibleType($cartID, $model);
                             }
                         }
                     }
                 }
             }
         }
         // each tocheck
     }
     if (isCommandLine()) {
         printf(__('Replay rules on existing database ended on %s') . "\n", date("r"));
     } else {
         Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     }
     return $i == $nb ? -1 : $i;
 }
 function replaceids($array)
 {
     global $CFG_GLPI;
     foreach ($array as $key => $value) {
         if (!is_int($key) && ($key == "software" || $key == 'ipaddress' || $key == 'internalport')) {
             // do nothing
         } else {
             //if (is_array($value)) {
             if ((array) $value === $value) {
                 $array[$key] = $this->replaceids($value);
             } else {
                 if (!is_numeric($key) && ($key == "manufacturers_id" || $key == 'bios_manufacturers_id')) {
                     $manufacturer = new Manufacturer();
                     $array[$key] = $manufacturer->processName($value);
                     if ($key == 'bios_manufacturers_id') {
                         $this->foreignkey_itemtype[$key] = getItemTypeForTable(getTableNameForForeignKeyField('manufacturers_id'));
                     } else {
                         if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value])) {
                             $CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value] = $array[$key];
                         }
                     }
                 }
                 if (!is_numeric($key)) {
                     if ($key == "bios_manufacturers_id") {
                         $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype['manufacturers_id'], $value);
                     } else {
                         if ($key == "locations_id") {
                             $array[$key] = Dropdown::importExternal('Location', $value, $_SESSION["plugin_fusioninventory_entity"]);
                         } else {
                             if (isset($this->foreignkey_itemtype[$key])) {
                                 $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"]);
                             } else {
                                 if (isForeignKeyField($key) && $key != "users_id") {
                                     $this->foreignkey_itemtype[$key] = getItemTypeForTable(getTableNameForForeignKeyField($key));
                                     if ($key == 'computermodels_id') {
                                         if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'])) {
                                             $manufacturer = current($CFG_GLPI['plugin_fusioninventory_computermanufacturer']);
                                             $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"], array('manufacturer' => $manufacturer));
                                         } else {
                                             $array[$key] = 0;
                                         }
                                     } else {
                                         $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"]);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $array;
 }
Esempio n. 4
0
 /**
  * Update config of a new software
  *
  * This function create a new software in GLPI with some general data.
  *
  * @param $computers_id                         integer : glpi computer id.
  * @param $entity                               integer : entity of the computer
  * @param $ocsid                                integer : ocs computer id (ID).
  * @param $plugin_ocsinventoryng_ocsservers_id  integer : ocs server id
  * @param $cfg_ocs                              array   : ocs config
  * @param $import_software                      array   : already imported softwares
  * @param $dohistory                            boolean : log changes?
  *
  * @return Nothing (void).
  **/
 static function updateSoftware($computers_id, $entity, $ocsid, $plugin_ocsinventoryng_ocsservers_id, array $cfg_ocs, $dohistory)
 {
     global $DB, $PluginOcsinventoryngDBocs;
     $alread_processed = array();
     $is_utf8 = $cfg_ocs["ocs_db_utf8"];
     $computer_softwareversion = new Computer_SoftwareVersion();
     self::checkOCSconnection($plugin_ocsinventoryng_ocsservers_id);
     if ($cfg_ocs["import_software"]) {
         //---- Get all the softwares for this machine from OCS -----//
         if ($cfg_ocs["use_soft_dict"]) {
             $query2 = "SELECT `dico_soft`.`FORMATTED` AS NAME,\n                              `softwares`.`VERSION` AS VERSION,\n                              `softwares`.`PUBLISHER` AS PUBLISHER,\n                              `softwares`.`COMMENTS` AS COMMENTS\n                       FROM `softwares`\n                       INNER JOIN `dico_soft` ON (`softwares`.`NAME` = dico_soft.EXTRACTED)\n                       WHERE `softwares`.`HARDWARE_ID` = '{$ocsid}'";
         } else {
             $query2 = "SELECT `softwares`.`NAME` AS NAME,\n                              `softwares`.`VERSION` AS VERSION,\n                              `softwares`.`PUBLISHER` AS PUBLISHER,\n                              `softwares`.`COMMENTS` AS COMMENTS\n                       FROM `softwares`\n                       WHERE `softwares`.`HARDWARE_ID` = '{$ocsid}'";
         }
         $result2 = $PluginOcsinventoryngDBocs->query($query2);
         $soft = new Software();
         // Read imported software in last sync
         $query = "SELECT `glpi_computers_softwareversions`.`id` as id,\n                          `glpi_softwares`.`name` as sname,\n                          `glpi_softwareversions`.`name` as vname\n                   FROM `glpi_computers_softwareversions`\n                   INNER JOIN `glpi_softwareversions`\n                           ON `glpi_softwareversions`.`id`= `glpi_computers_softwareversions`.`softwareversions_id`\n                   INNER JOIN `glpi_softwares`\n                           ON `glpi_softwares`.`id`= `glpi_softwareversions`.`softwares_id`\n                   WHERE `glpi_computers_softwareversions`.`computers_id`='{$computers_id}'\n                         AND `is_dynamic`";
         $imported = array();
         foreach ($DB->request($query) as $data) {
             $imported[$data['id']] = strtolower($data['sname'] . self::FIELD_SEPARATOR . $data['vname']);
         }
         if ($PluginOcsinventoryngDBocs->numrows($result2) > 0) {
             while ($data2 = $PluginOcsinventoryngDBocs->fetch_array($result2)) {
                 $data2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($data2));
                 //As we cannot be sure that data coming from OCS are in utf8, let's try to encode them
                 //if possible
                 foreach (array('NAME', 'PUBLISHER', 'VERSION') as $field) {
                     $data2[$field] = self::encodeOcsDataInUtf8($is_utf8, $data2[$field]);
                 }
                 //Replay dictionnary on manufacturer
                 $manufacturer = Manufacturer::processName($data2["PUBLISHER"]);
                 $version = $data2['VERSION'];
                 $name = $data2['NAME'];
                 //Software might be created in another entity, depending on the entity's configuration
                 $target_entity = Entity::getUsedConfig('entities_id_software', $entity, '', true);
                 //Do not change software's entity except if the dictionnary explicity changes it
                 if ($target_entity < 0) {
                     $target_entity = $entity;
                 }
                 $modified_name = $name;
                 $modified_version = $version;
                 $is_helpdesk_visible = NULL;
                 if (!$cfg_ocs["use_soft_dict"]) {
                     //Software dictionnary
                     $params = array("name" => $name, "manufacturer" => $manufacturer, "old_version" => $version, "entities_id" => $entity);
                     $rulecollection = new RuleDictionnarySoftwareCollection();
                     $res_rule = $rulecollection->processAllRules(Toolbox::stripslashes_deep($params), array(), Toolbox::stripslashes_deep(array('version' => $version)));
                     if (isset($res_rule["name"]) && $res_rule["name"]) {
                         $modified_name = $res_rule["name"];
                     }
                     if (isset($res_rule["version"]) && $res_rule["version"]) {
                         $modified_version = $res_rule["version"];
                     }
                     if (isset($res_rule["is_helpdesk_visible"]) && strlen($res_rule["is_helpdesk_visible"])) {
                         $is_helpdesk_visible = $res_rule["is_helpdesk_visible"];
                     }
                     if (isset($res_rule['manufacturer']) && $res_rule['manufacturer']) {
                         $manufacturer = Dropdown::getDropdownName('glpi_manufacturers', $res_rule['manufacturer']);
                         $manufacturer = Toolbox::addslashes_deep($manufacturer);
                     }
                     //If software dictionnary returns an entity, it overrides the one that may have
                     //been defined in the entity's configuration
                     if (isset($res_rule["new_entities_id"]) && strlen($res_rule["new_entities_id"])) {
                         $target_entity = $res_rule["new_entities_id"];
                     }
                 }
                 //If software must be imported
                 if (!isset($res_rule["_ignore_import"]) || !$res_rule["_ignore_import"]) {
                     // Clean software object
                     $soft->reset();
                     // EXPLANATION About dictionnaries
                     // OCS dictionnary : if software name change, as we don't store INITNAME
                     //     GLPI will detect an uninstall (oldname) + install (newname)
                     // GLPI dictionnary : is rule have change
                     //     if rule have been replayed, modifiedname will be found => ok
                     //     if not, GLPI will detect an uninstall (oldname) + install (newname)
                     $id = array_search(strtolower(stripslashes($modified_name . self::FIELD_SEPARATOR . $version)), $imported);
                     if ($id) {
                         //-------------------------------------------------------------------------//
                         //---- The software exists in this version for this computer --------------//
                         //---------------------------------------------------- --------------------//
                         unset($imported[$id]);
                     } else {
                         //------------------------------------------------------------------------//
                         //---- The software doesn't exists in this version for this computer -----//
                         //------------------------------------------------------------------------//
                         $isNewSoft = $soft->addOrRestoreFromTrash($modified_name, $manufacturer, $target_entity, '', $entity != $target_entity, $is_helpdesk_visible);
                         //Import version for this software
                         $versionID = self::importVersion($isNewSoft, $modified_version);
                         //Install license for this machine
                         $instID = self::installSoftwareVersion($computers_id, $versionID, $dohistory);
                     }
                 }
             }
         }
         foreach ($imported as $id => $unused) {
             $computer_softwareversion->delete(array('id' => $id, '_no_history' => !$dohistory), true);
             // delete cause a getFromDB, so fields contains values
             $verid = $computer_softwareversion->getField('softwareversions_id');
             if (countElementsInTable('glpi_computers_softwareversions', "softwareversions_id = '{$verid}'") == 0 && countElementsInTable('glpi_softwarelicenses', "softwareversions_id_buy = '{$verid}'") == 0) {
                 $vers = new SoftwareVersion();
                 if ($vers->getFromDB($verid) && countElementsInTable('glpi_softwarelicenses', "softwares_id = '" . $vers->fields['softwares_id'] . "'") == 0 && countElementsInTable('glpi_softwareversions', "softwares_id = '" . $vers->fields['softwares_id'] . "'") == 1) {
                     // 1 is the current to be removed
                     $soft->putInTrash($vers->fields['softwares_id'], __('Software deleted by OCSNG synchronization'));
                 }
                 $vers->delete(array("id" => $verid));
             }
         }
     }
 }
 /**
  * Update config of a new software
  *
  * This function create a new software in GLPI with some general datas.
  *
  * @param $computers_id integer : glpi computer id.
  * @param $entity integer : entity of the computer
  * @param $ocsid integer : ocs computer id (ID).
  * @param $ocsservers_id integer : ocs server id
  * @param $cfg_ocs array : ocs config
  * @param $import_software array : already imported softwares
  * @param $dohistory boolean : log changes ?
  *
  * @return Nothing (void).
  **/
 static function updateSoftware($computers_id, $entity, $ocsid, $ocsservers_id, $cfg_ocs, $import_software, $dohistory)
 {
     global $DB, $DBocs, $LANG;
     self::checkOCSconnection($ocsservers_id);
     if ($cfg_ocs["import_software"]) {
         //------------------------------------------------------------------------------//
         //---- Import_software array is not in the new form ( ID => name+version) ------//
         //------------------------------------------------------------------------------//
         if (!in_array(self::IMPORT_TAG_070, $import_software)) {
             //Add the tag of the version at the beginning of the array
             $softs_array[0] = self::IMPORT_TAG_070;
             //For each element of the table, add instID=>name.version
             foreach ($import_software as $key => $value) {
                 $query_softs = "SELECT `glpi_softwareversions`.`name` AS version\n                               FROM `glpi_computers_softwareversions`,\n                                    `glpi_softwareversions`\n                               WHERE `glpi_computers_softwareversions`.`softwareversions_id`\n                                          =`glpi_softwareversions`.`id`\n                                     AND `glpi_computers_softwareversions`.`computers_id`\n                                          = '{$computers_id}'\n                                     AND `glpi_computers_softwareversions`.`id` = '{$key}'";
                 $result_softs = $DB->query($query_softs);
                 $softs = $DB->fetch_array($result_softs);
                 $softs_array[$key] = $value . self::FIELD_SEPARATOR . $softs["version"];
             }
             //Replace in GLPI database the import_software by the new one
             self::replaceOcsArray($computers_id, $softs_array, "import_software");
             // Get import_software from the GLPI db
             $query = "SELECT `import_software`\n                      FROM `glpi_ocslinks`\n                      WHERE `computers_id` = '{$computers_id}'";
             $result = $DB->query($query);
             //Reload import_software from DB
             if ($DB->numrows($result)) {
                 $tmp = $DB->fetch_array($result);
                 $import_software = importArrayFromDB($tmp["import_software"]);
             }
         }
         //---- Get all the softwares for this machine from OCS -----//
         if ($cfg_ocs["use_soft_dict"]) {
             $query2 = "SELECT `softwares`.`NAME` AS INITNAME,\n                              `dico_soft`.`FORMATTED` AS NAME,\n                              `softwares`.`VERSION` AS VERSION,\n                              `softwares`.`PUBLISHER` AS PUBLISHER,\n                              `softwares`.`COMMENTS` AS COMMENTS\n                       FROM `softwares`\n                       INNER JOIN `dico_soft` ON (`softwares`.`NAME` = dico_soft.EXTRACTED)\n                       WHERE `softwares`.`HARDWARE_ID` = '{$ocsid}'";
         } else {
             $query2 = "SELECT `softwares`.`NAME` AS INITNAME,\n                              `softwares`.`NAME` AS NAME,\n                              `softwares`.`VERSION` AS VERSION,\n                              `softwares`.`PUBLISHER` AS PUBLISHER,\n                              `softwares`.`COMMENTS` AS COMMENTS\n                       FROM `softwares`\n                       WHERE `softwares`.`HARDWARE_ID` = '{$ocsid}'";
         }
         $result2 = $DBocs->query($query2);
         $to_add_to_ocs_array = array();
         $soft = new Software();
         if ($DBocs->numrows($result2) > 0) {
             while ($data2 = $DBocs->fetch_array($result2)) {
                 $data2 = clean_cross_side_scripting_deep(addslashes_deep($data2));
                 $initname = $data2["INITNAME"];
                 // Hack for OCS encoding problems
                 if (!$cfg_ocs["ocs_db_utf8"] && !seems_utf8($initname)) {
                     $initname = encodeInUtf8($initname);
                 }
                 $name = $data2["NAME"];
                 // Hack for OCS encoding problems
                 if (!$cfg_ocs["ocs_db_utf8"] && !seems_utf8($name)) {
                     $name = encodeInUtf8($name);
                 }
                 // Hack for OCS encoding problems
                 if (!$cfg_ocs["ocs_db_utf8"] && !seems_utf8($data2["PUBLISHER"])) {
                     $data2["PUBLISHER"] = encodeInUtf8($data2["PUBLISHER"]);
                 }
                 $version = $data2["VERSION"];
                 $manufacturer = Manufacturer::processName($data2["PUBLISHER"]);
                 $use_glpi_dictionnary = false;
                 if (!$cfg_ocs["use_soft_dict"]) {
                     //Software dictionnary
                     $rulecollection = new RuleDictionnarySoftwareCollection();
                     $res_rule = $rulecollection->processAllRules(array("name" => $name, "manufacturer" => $manufacturer, "old_version" => $version), array(), array('version' => $version));
                     $res_rule = addslashes_deep($res_rule);
                     if (isset($res_rule["name"])) {
                         $modified_name = $res_rule["name"];
                     } else {
                         $modified_name = $name;
                     }
                     if (isset($res_rule["version"]) && $res_rule["version"] != '') {
                         $modified_version = $res_rule["version"];
                     } else {
                         $modified_version = $version;
                     }
                 } else {
                     $modified_name = $name;
                     $modified_version = $version;
                 }
                 //Ignore this software
                 if (!isset($res_rule["_ignore_ocs_import"]) || !$res_rule["_ignore_ocs_import"]) {
                     // Clean software object
                     $soft->reset();
                     //If name+version not in present for this computer in glpi, add it
                     if (!in_array(stripslashes($initname . self::FIELD_SEPARATOR . $version), $import_software)) {
                         //------------------------------------------------------------------------//
                         //---- The software doesn't exists in this version for this computer -----//
                         //------------------------------------------------------------------------//
                         $isNewSoft = $soft->addOrRestoreFromTrash($modified_name, $manufacturer, $entity);
                         //Import version for this software
                         $versionID = self::importVersion($isNewSoft, $modified_version);
                         //Install license for this machine
                         $instID = self::installSoftwareVersion($computers_id, $versionID, $dohistory);
                         //Add the software to the table of softwares for this computer to add in database
                         $to_add_to_ocs_array[$instID] = $initname . self::FIELD_SEPARATOR . $version;
                     } else {
                         $instID = -1;
                         //-------------------------------------------------------------------------//
                         //---- The software exists in this version for this computer --------------//
                         //---------------------------------------------------- --------------------//
                         //Get the name of the software in GLPI to know if the software's name
                         //have already been changed by the OCS dictionnary
                         $instID = array_search(stripslashes($initname . self::FIELD_SEPARATOR . $version), $import_software);
                         $query_soft = "SELECT `glpi_softwares`.`id`,\n                                           `glpi_softwares`.`name`,\n                                           `glpi_softwares`.`entities_id`\n                                    FROM `glpi_softwares`,\n                                         `glpi_computers_softwareversions`,\n                                         `glpi_softwareversions`\n                                    WHERE `glpi_computers_softwareversions`.`id` = '{$instID}'\n                                          AND `glpi_computers_softwareversions`.`softwareversions_id`\n                                                = `glpi_softwareversions`.`id`\n                                          AND `glpi_softwareversions`.`softwares_id`\n                                                = `glpi_softwares`.`id`";
                         $result_soft = $DB->query($query_soft);
                         $tmpsoft = $DB->fetch_array($result_soft);
                         $softName = $tmpsoft["name"];
                         $softID = $tmpsoft["id"];
                         $s = new Software();
                         $input["id"] = $softID;
                         $input["entities_id"] = $tmpsoft['entities_id'];
                         //First, get the name of the software into GLPI db IF dictionnary is used
                         if ($cfg_ocs["use_soft_dict"]) {
                             //First use of the OCS dictionnary OR name changed in the dictionnary
                             if ($softName != $name) {
                                 $input["name"] = $name;
                                 $s->update($input);
                             }
                         } else {
                             if ($softName != $modified_name) {
                                 // OCS Dictionnary not use anymore : revert to original name
                                 $input["name"] = $modified_name;
                                 $s->update($input);
                             }
                         }
                         unset($import_software[$instID]);
                     }
                 }
             }
         }
         //Remove the tag from the import_software array
         unset($import_software[0]);
         //Add all the new softwares
         if (count($to_add_to_ocs_array)) {
             self::addToOcsArray($computers_id, $to_add_to_ocs_array, "import_software");
         }
         // Remove softwares not present in OCS
         if (count($import_software)) {
             $inst = new Computer_SoftwareVersion();
             foreach ($import_software as $key => $val) {
                 $query = "SELECT *\n                         FROM `glpi_computers_softwareversions`\n                         WHERE `id` = '{$key}'";
                 $result = $DB->query($query);
                 if ($DB->numrows($result) > 0) {
                     if ($data = $DB->fetch_assoc($result)) {
                         $inst->delete(array('id' => $key, '_no_history' => !$dohistory));
                         if (countElementsInTable('glpi_computers_softwareversions', "softwareversions_id = '" . $data['softwareversions_id'] . "'") == 0 && countElementsInTable('glpi_softwarelicenses', "softwareversions_id_buy = '" . $data['softwareversions_id'] . "'") == 0) {
                             $vers = new SoftwareVersion();
                             if ($vers->getFromDB($data['softwareversions_id']) && countElementsInTable('glpi_softwarelicenses', "softwares_id = '" . $vers->fields['softwares_id'] . "'") == 0 && countElementsInTable('glpi_softwareversions', "softwares_id = '" . $vers->fields['softwares_id'] . "'") == 1) {
                                 // 1 is the current to be removed
                                 $soft->putInTrash($vers->fields['softwares_id'], $LANG['ocsng'][54]);
                             }
                             $vers->delete(array("id" => $data['softwareversions_id']));
                         }
                     }
                 }
                 self::deleteInOcsArray($computers_id, $key, "import_software");
             }
         }
     }
 }