Example #1
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));
             }
         }
     }
 }
 /**
  * @covers Computer_SoftwareVersion::updateDatasForComputer
  */
 public function testUpdateDatasFromComputer()
 {
     $c00 = 1566671;
     $computer1 = getItemByTypeName('Computer', '_test_pc01');
     $ver1 = getItemByTypeName('SoftwareVersion', '_test_softver_1', true);
     $ver2 = getItemByTypeName('SoftwareVersion', '_test_softver_2', true);
     // Do some installations
     $softver = new Computer_SoftwareVersion();
     $softver01 = $softver->add(['computers_id' => $computer1->getID(), 'softwareversions_id' => $ver1]);
     $this->assertGreaterThan(0, $softver01);
     $softver02 = $softver->add(['computers_id' => $computer1->getID(), 'softwareversions_id' => $ver2]);
     $this->assertGreaterThan(0, $softver02);
     foreach ([$softver01, $softver02] as $tsoftver) {
         $o = new Computer_SoftwareVersion();
         $o->getFromDb($tsoftver);
         $this->assertEquals('0', $o->getField('is_deleted_computer'));
     }
     //computer that does not exists
     $this->assertFalse($softver->updateDatasForComputer($c00));
     //update existing computer
     $input = $computer1->fields;
     $input['is_deleted'] = '1';
     $this->assertTrue($computer1->update($input));
     $this->assertEquals(2, $softver->updateDatasForComputer($computer1->getID()));
     //check if all has been updated
     foreach ([$softver01, $softver02] as $tsoftver) {
         $o = new Computer_SoftwareVersion();
         $o->getFromDb($tsoftver);
         $this->assertEquals('1', $o->getField('is_deleted_computer'));
     }
     //restore computer state
     $input['is_deleted'] = '0';
     $this->assertTrue($computer1->update($input));
 }