/** * Play software dictionnary * * @param $values **/ function processDictionnariesIfNeeded(&$values) { $params['entities_id'] = $_SESSION['glpiactive_entity']; $params['name'] = $values['Software']['name']; if (isset($values['Software']['manufacturers_id'])) { $params['manufacturer'] = $values['Software']['manufacturers_id']; } else { $params['manufacturer'] = ''; } $rulecollection = new RuleDictionnarySoftwareCollection(); $res_rule = $rulecollection->processAllRules($params, array(), array()); if (!isset($res_rule['_no_rule_matches'])) { //Software dictionnary explicitly refuse import if (isset($res_rule['_ignore_import']) && $res_rule['_ignore_import']) { return false; } if (isset($res_rule['is_helpdesk_visible'])) { $values['Software']['is_helpdesk_visible'] = $res_rule['is_helpdesk_visible']; } if (isset($res_rule['version'])) { $values['SoftwareVersion']['name'] = $res_rule['version']; } if (isset($res_rule['name'])) { $values['Software']['name'] = $res_rule['name']; } if (isset($res_rule['supplier'])) { if (isset($values['supplier'])) { $values['Software']['manufacturers_id'] = Dropdown::getDropdownName('glpi_suppliers', $res_rule['supplier']); } } } return true; }
/** * @see CommonDBTM::doSpecificMassiveActions() **/ function doSpecificMassiveActions($input = array()) { $res = array('ok' => 0, 'ko' => 0, 'noright' => 0); switch ($input['action']) { case "mergesoftware": if (isset($input["id"]) && isset($input["item"]) && is_array($input["item"]) && count($input["item"])) { if ($this->can($_POST["id"], 'w')) { if ($this->merge($_POST["item"])) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } else { $res['ko']++; } break; case "compute_software_category": $softcatrule = new RuleSoftwareCategoryCollection(); foreach ($input["item"] as $key => $val) { if ($val == 1) { $params = array(); //Get software name and manufacturer if ($this->can($key, 'w')) { $params["name"] = $this->fields["name"]; $params["manufacturers_id"] = $this->fields["manufacturers_id"]; $params["comment"] = $this->fields["comment"]; $output = array(); $output = $softcatrule->processAllRules(null, $output, $params); //Process rules if (isset($output['softwarecategories_id']) && $this->update(array('id' => $key, 'softwarecategories_id' => $output['softwarecategories_id']))) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } } break; case "replay_dictionnary": $softdictionnayrule = new RuleDictionnarySoftwareCollection(); $ids = array(); foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->can($key, 'w')) { $ids[] = $key; } else { $res['noright']++; } } } if ($softdictionnayrule->replayRulesOnExistingDB(0, 0, $ids) > 0) { $res['ok'] += count($ids); } else { $res['ko'] += count($ids); } break; default: return parent::doSpecificMassiveActions($input); } return $res; }
/** * 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)); } } } }
/** * Import of Software (with Rule) * * From OcsServer class */ public function testSoftwareRule() { // Clean preload rules $tmp = SingletonRuleList::getInstance('RuleDictionnaryManufacturer'); $tmp->load = 0; $tmp = SingletonRuleList::getInstance('RuleDictionnarySoftware'); $tmp->load = 0; // Needed objetcs $rulem = new RuleDictionnaryDropdown('RuleDictionnaryManufacturer'); $rules = new RuleDictionnaryDropdown('RuleDictionnarySoftware'); $crit = new RuleCriteria(); $acte = new RuleAction(); // Rule for Manufacturer $idr[0] = $rulem->add(array('name' => 'test1', 'sub_type' => 'RuleDictionnaryManufacturer', 'match' => 'AND', 'is_active' => 1)); $this->assertGreaterThan(0, $idr[0], "Fail: can't create manufacturer rule"); $this->assertTrue($rulem->getFromDB($idr[0])); $this->assertEquals(1, $rulem->fields['ranking'], "Fail: ranking not set"); $idc[0] = $crit->add(array('rules_id' => $idr[0], 'criteria' => 'name', 'condition' => Rule::PATTERN_CONTAIN, 'pattern' => 'indepnet')); $this->assertGreaterThan(0, $idc[0], "Fail: can't create manufacturer rule criteria"); $ida[0] = $acte->add(array('rules_id' => $idr[0], 'action_type' => 'assign', 'field' => 'name', 'value' => $outm = 'Indepnet')); $this->assertGreaterThan(0, $ida[0], "Fail: can't create manufacturer rule action"); // Rule for Software $idr[1] = $rules->add(array('name' => 'test2', 'sub_type' => 'RuleDictionnarySoftware', 'match' => 'AND', 'is_active' => 1)); $this->assertGreaterThan(0, $idr[1], "Fail: can't create software rule"); $this->assertTrue($rules->getFromDB($idr[1])); $this->assertEquals(1, $rules->fields['ranking'], "Fail: ranking not set"); $idc[1] = $crit->add(array('rules_id' => $idr[1], 'criteria' => 'name', 'condition' => Rule::REGEX_MATCH, 'pattern' => '/^glpi (0\\.[0-9]+)/')); $this->assertGreaterThan(0, $idc[1], "Fail: can't create software rule criteria"); $ida[1] = $acte->add(array('rules_id' => $idr[1], 'action_type' => 'assign', 'field' => 'name', 'value' => $outs = 'GLPI')); $this->assertGreaterThan(0, $ida[1], "Fail: can't create software rule action"); $ida[2] = $acte->add(array('rules_id' => $idr[1], 'action_type' => 'regex_result', 'field' => 'version', 'value' => $outv = '#0')); $this->assertGreaterThan(0, $ida[2], "Fail: can't create software rule action"); // Apply Rule to manufacturer $manu = Dropdown::import('Manufacturer', 'the indepnet team'); $this->assertEquals('Indepnet', $manu, "Fail: manufacturer not altered"); // Apply Rule to software $rulecollection = new RuleDictionnarySoftwareCollection(); $res_rule = $rulecollection->processAllRules(array("name" => 'glpi 0.78', "manufacturer" => $manu, "old_version" => ''), array(), array()); $this->assertArrayHasKey('name', $res_rule, "Fail: name not altered"); $this->assertEquals('GLPI', $res_rule['name'], "Fail: name not correct"); $this->assertArrayHasKey('version', $res_rule, "Fail: name not altered"); $this->assertEquals('0.78', $res_rule['version'], "Fail: version not correct"); // Clean $this->assertTrue($rulem->delete(array('id' => $idr[0]))); $this->assertTrue($rules->delete(array('id' => $idr[1]))); }
function computerSoftwareTransformation($a_inventory, $entities_id) { /* * Sometimes we can have 2 same software, but one without manufacturer and * one with. So in this case, delete the software without manufacturer */ $softwareWithManufacturer = array(); $softwareWithoutManufacturer = array(); $entities_id_software = Entity::getUsedConfig('entities_id_software', $entities_id); $is_software_recursive = 0; $nb_RuleDictionnarySoftware = countElementsInTable("glpi_rules", "`sub_type`='RuleDictionnarySoftware'\n AND `is_active`='1'"); //Configuration says that software can be created in the computer's entity if ($entities_id_software < 0) { $entities_id_software = $entities_id; } else { //Software will be created in an entity which is not the computer's entity. //It should be set as recursive $is_software_recursive = 1; } $a_inventory['software'] = array(); $rulecollection = new RuleDictionnarySoftwareCollection(); foreach ($a_inventory['SOFTWARES'] as $a_softwares) { if (isset($a_softwares['PUBLISHER']) && gettype($a_softwares['PUBLISHER']) == 'array') { $a_softwares['PUBLISHER'] = current($a_softwares['PUBLISHER']); } $array_tmp = $this->addValues($a_softwares, array('PUBLISHER' => 'manufacturers_id', 'NAME' => 'name', 'VERSION' => 'version')); if (!isset($array_tmp['name']) || $array_tmp['name'] == '') { if (isset($a_softwares['GUID']) && $a_softwares['GUID'] != '') { $array_tmp['name'] = $a_softwares['GUID']; } } if (!(!isset($array_tmp['name']) || $array_tmp['name'] == '')) { if (count($array_tmp) > 0) { $res_rule = array(); if ($nb_RuleDictionnarySoftware > 0) { $res_rule = $rulecollection->processAllRules(array("name" => $array_tmp['name'], "manufacturer" => $array_tmp['manufacturers_id'], "old_version" => $array_tmp['version'], "entities_id" => $entities_id_software)); } if (isset($res_rule['_ignore_import']) && $res_rule['_ignore_import'] == 1) { } else { if (isset($res_rule["name"])) { $array_tmp['name'] = $res_rule["name"]; } if (isset($res_rule["version"])) { $array_tmp['version'] = $res_rule["version"]; } if (isset($res_rule["manufacturer"])) { $array_tmp['manufacturers_id'] = Dropdown::import("Manufacturer", array('name' => $res_rule["manufacturer"])); } else { if (isset($array_tmp['manufacturers_id']) && $array_tmp['manufacturers_id'] != '' && $array_tmp['manufacturers_id'] != '0') { if (!isset($this->manufacturer_cache[$array_tmp['manufacturers_id']])) { $new_value = Dropdown::importExternal('Manufacturer', $array_tmp['manufacturers_id']); $this->manufacturer_cache[$array_tmp['manufacturers_id']] = $new_value; } $array_tmp['manufacturers_id'] = $this->manufacturer_cache[$array_tmp['manufacturers_id']]; } else { $array_tmp['manufacturers_id'] = 0; } } if (isset($res_rule['new_entities_id'])) { $array_tmp['entities_id'] = $res_rule['new_entities_id']; $is_software_recursive = 1; } if (!isset($array_tmp['entities_id']) || $array_tmp['entities_id'] == '') { $array_tmp['entities_id'] = $entities_id_software; } if (!isset($array_tmp['version'])) { $array_tmp['version'] = ""; } $array_tmp['is_template_computer'] = 0; $array_tmp['is_deleted_computer'] = 0; $array_tmp['is_recursive'] = $is_software_recursive; $comp_key = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['manufacturers_id'] . "\$\$\$\$" . $array_tmp['entities_id']; $comp_key_simple = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['entities_id']; if ($array_tmp['manufacturers_id'] == 0) { $softwareWithoutManufacturer[$comp_key_simple] = $array_tmp; } else { if (!isset($a_inventory['software'][$comp_key])) { $softwareWithManufacturer[$comp_key_simple] = 1; $a_inventory['software'][$comp_key] = $array_tmp; } } } } } } foreach ($softwareWithoutManufacturer as $key => $array_tmp) { if (!isset($softwareWithManufacturer[$key])) { $comp_key = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['manufacturers_id'] . "\$\$\$\$" . $array_tmp['entities_id']; if (!isset($a_inventory['software'][$comp_key])) { $a_inventory['software'][$comp_key] = $array_tmp; } } } unset($a_inventory['SOFTWARES']); return $a_inventory; }
$soft = new Software(); foreach ($_POST["item"] as $key => $val) { if ($val == 1) { $params = array(); //Get software name and manufacturer $soft->getFromDB($key); $params["name"] = $soft->fields["name"]; $params["manufacturers_id"] = $soft->fields["manufacturers_id"]; $params["comment"] = $soft->fields["comment"]; //Process rules $soft->update($softcatrule->processAllRules(null, $soft->fields, $params)); } } break; case "replay_dictionnary": $softdictionnayrule = new RuleDictionnarySoftwareCollection(); $ids = array(); foreach ($_POST["item"] as $key => $val) { if ($val == 1) { $ids[] = $key; } } $softdictionnayrule->replayRulesOnExistingDB(0, 0, $ids); break; case "force_user_ldap_update": checkRight("user", "w"); $user = new User(); $ids = array(); foreach ($_POST["item"] as $key => $val) { if ($val == 1) { $user->getFromDB($key);
/** * @since version 0.85 * * @see CommonDBTM::processMassiveActionsForOneItemtype() **/ static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) { switch ($ma->getAction()) { case 'merge': $input = $ma->getInput(); if (isset($input['item_items_id'])) { $items = array(); foreach ($ids as $id) { $items[$id] = 1; } if ($item->can($input['item_items_id'], UPDATE)) { if ($item->merge($items)) { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } return; case 'compute_software_category': $softcatrule = new RuleSoftwareCategoryCollection(); foreach ($ids as $id) { $params = array(); //Get software name and manufacturer if ($item->can($id, UPDATE)) { $params["name"] = $item->fields["name"]; $params["manufacturers_id"] = $item->fields["manufacturers_id"]; $params["comment"] = $item->fields["comment"]; $output = array(); $output = $softcatrule->processAllRules(null, $output, $params); //Process rules if (isset($output['softwarecategories_id']) && $item->update(array('id' => $id, 'softwarecategories_id' => $output['softwarecategories_id']))) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } return; case 'replay_dictionnary': $softdictionnayrule = new RuleDictionnarySoftwareCollection(); $allowed_ids = array(); foreach ($ids as $id) { if ($item->can($id, UPDATE)) { $allowed_ids[] = $id; } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } if ($softdictionnayrule->replayRulesOnExistingDB(0, 0, $allowed_ids) > 0) { $ma->itemDone($item->getType(), $allowed_ids, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $allowed_ids, MassiveAction::ACTION_KO); } return; } parent::processMassiveActionsForOneItemtype($ma, $item, $ids); }
/** * 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"); } } } }