function replayRulesOnExistingDB($offset = 0, $maxtime = 0, $items = array(), $params = array())
 {
     global $DB;
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB started : " . date("r") . "\n";
     }
     $nb = 0;
     $i = $offset;
     //Select all the differents software
     $sql = "SELECT DISTINCT `glpi_printers`.`name`,\n                     `glpi_manufacturers`.`name` AS manufacturer,\n                     `glpi_printers`.`manufacturers_id` AS manufacturers_id,\n                     `glpi_printers`.`comment` AS comment\n              FROM `glpi_printers`\n              LEFT JOIN `glpi_manufacturers`\n                  ON (`glpi_manufacturers`.`id` = `glpi_printers`.`manufacturers_id`) ";
     // Do not replay on trash and templates
     $sql .= "WHERE `glpi_printers`.`is_deleted` = '0'\n                     AND `glpi_printers`.`is_template` = '0' ";
     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_array($res)) {
         if (!($i % $step)) {
             if (isCommandLine()) {
                 echo date("H:i:s") . " replayRulesOnExistingDB : {$i}/{$nb} (" . round(memory_get_usage() / (1024 * 1024), 2) . " Mo)\n";
             } else {
                 changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
             }
         }
         //Replay printer dictionnary rules
         $input = addslashes_deep($input);
         $res_rule = $this->processAllRules($input, array(), array());
         $res_rule = addslashes_deep($res_rule);
         foreach (array('manufacturer', 'is_global', 'name') as $attr) {
             if (isset($res_rule[$attr]) && $res_rule[$attr] == '') {
                 unset($res_rule[$attr]);
             }
         }
         //If the software's name or version has changed
         if (self::somethingHasChanged($res_rule, $input)) {
             $IDs = array();
             //Find all the printers in the database with the same name and manufacturer
             $sql = "SELECT `id`\n                    FROM `glpi_printers`\n                    WHERE `name` = '" . $input["name"] . "'\n                          AND `manufacturers_id` = '" . $input["manufacturers_id"] . "'";
             $res_printer = $DB->query($sql);
             if ($DB->numrows($res_printer) > 0) {
                 //Store all the software's IDs in an array
                 while ($result = $DB->fetch_array($res_printer)) {
                     $IDs[] = $result["id"];
                 }
                 //Replay dictionnary on all the softwares
                 $this->replayDirectionnaryOnPrintersByID($IDs, $res_rule);
             }
         }
         $i++;
         if ($maxtime) {
             $crt = explode(" ", microtime());
             if ($crt[0] + $crt[1] > $maxtime) {
                 break;
             }
         }
     }
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB : {$i}/{$nb}               \n";
     } else {
         changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     }
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB ended : " . date("r") . "\n";
     }
     return $i == $nb ? -1 : $i;
 }
 function ConvertField($force = 0)
 {
     include GLPI_ROOT . "/plugins/fusioninventory/inc_constants/snmp.mapping.constant.php";
     global $DB, $LANG;
     $constantsfield = array();
     foreach ($FUSIONINVENTORY_MAPPING[NETWORKING_TYPE] as $fieldtype => $array) {
         $constantsfield[$FUSIONINVENTORY_MAPPING[NETWORKING_TYPE][$fieldtype]['name']] = $fieldtype;
     }
     echo "<center><table align='center' width='500'>";
     echo "<tr>";
     echo "<td>";
     echo "Converting history port ...";
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     createProgressBar("Update Ports history");
     $query = "SELECT *\n                FROM " . $this->table . "\n                WHERE `Field` != '0';";
     if ($result = $DB->query($query)) {
         $nb = $DB->numrows($result);
         if ($nb > 300000 and $force == '0') {
             echo $LANG['plugin_fusioninventory']["update"][0] . "<br/>";
             echo "cd glpi/plugins/fusioninventory/front/ && php -f cli_update.php";
             echo "<br/>Waiting...";
             file_put_contents(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/cli-update.txt", "1");
             sleep(20);
             return;
         }
         $i = 0;
         while ($data = $DB->fetch_array($result)) {
             $i++;
             if (isset($constantsfield[$data['Field']])) {
                 $data['Field'] = $constantsfield[$data['Field']];
                 $query_update = "UPDATE `" . $this->table . "`\n                  SET `Field`='" . $data['Field'] . "'\n                  WHERE `ID`='" . $data['ID'] . "' ";
                 $DB->query($query_update);
                 if (preg_match("/000\$/", $i)) {
                     changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
                 }
             }
         }
     }
     changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     echo "</td>";
     echo "</tr>";
     echo "</table></center>";
     // Move connections from glpi_plugin_fusioninventory_snmphistories to glpi_plugin_fusioninventory_snmphistoryconnections
     $pfihc = new PluginFusioninventorySnmphistoryconnection();
     echo "<br/><center><table align='center' width='500'>";
     echo "<tr>";
     echo "<td>";
     echo "Moving creation connections history ...";
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     createProgressBar("Move create connections");
     $query = "SELECT *\n                FROM " . $this->table . "\n                WHERE `Field` = '0' \n                  AND ((`old_value` NOT LIKE '%:%')\n                        OR (`old_value` IS NULL))";
     if ($result = $DB->query($query)) {
         $nb = $DB->numrows($result);
         $i = 0;
         changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
         while ($data = $DB->fetch_array($result)) {
             $i++;
             // Search port from mac address
             $query_port = "SELECT * FROM `glpi_networking_ports`\n               WHERE `ifmac`='" . $data['new_value'] . "' ";
             if ($result_port = $DB->query($query_port)) {
                 if ($DB->numrows($result_port) == '1') {
                     $input = array();
                     $data_port = $DB->fetch_assoc($result_port);
                     $input['FK_port_source'] = $data_port['ID'];
                     $query_port2 = "SELECT * FROM `glpi_networking_ports`\n                     WHERE `on_device` = '" . $data['new_device_ID'] . "'\n                        AND `device_type` = '" . $data['new_device_type'] . "' ";
                     if ($result_port2 = $DB->query($query_port2)) {
                         if ($DB->numrows($result_port2) == '1') {
                             $data_port2 = $DB->fetch_assoc($result_port2);
                             $input['FK_port_destination'] = $data_port2['ID'];
                             $input['date'] = $data['date_mod'];
                             $input['creation'] = 1;
                             $input['process_number'] = $data['FK_process'];
                             $pfihc->add($input);
                         }
                     }
                 }
             }
             $query_delete = "DELETE FROM `" . $this->table . "`\n                  WHERE `ID`='" . $data['ID'] . "' ";
             $DB->query($query_delete);
             if (preg_match("/00\$/", $i)) {
                 changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
             }
         }
     }
     changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     echo "</td>";
     echo "</tr>";
     echo "</table></center>";
     echo "<br/><center><table align='center' width='500'>";
     echo "<tr>";
     echo "<td>";
     echo "Moving deleted connections history ...";
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<td>";
     createProgressBar("Move delete connections");
     $query = "SELECT *\n                FROM " . $this->table . "\n                WHERE `Field` = '0'\n                  AND ((`new_value` NOT LIKE '%:%')\n                        OR (`new_value` IS NULL))";
     if ($result = $DB->query($query)) {
         $nb = $DB->numrows($result);
         $i = 0;
         changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
         while ($data = $DB->fetch_array($result)) {
             $i++;
             // Search port from mac address
             $query_port = "SELECT * FROM `glpi_networking_ports`\n               WHERE `ifmac`='" . $data['old_value'] . "' ";
             if ($result_port = $DB->query($query_port)) {
                 if ($DB->numrows($result_port) == '1') {
                     $input = array();
                     $data_port = $DB->fetch_assoc($result_port);
                     $input['FK_port_source'] = $data_port['ID'];
                     $query_port2 = "SELECT * FROM `glpi_networking_ports`\n                     WHERE `on_device` = '" . $data['old_device_ID'] . "'\n                        AND `device_type` = '" . $data['old_device_type'] . "' ";
                     if ($result_port2 = $DB->query($query_port2)) {
                         if ($DB->numrows($result_port2) == '1') {
                             $data_port2 = $DB->fetch_assoc($result_port2);
                             $input['FK_port_destination'] = $data_port2['ID'];
                             $input['date'] = $data['date_mod'];
                             $input['creation'] = 1;
                             $input['process_number'] = $data['FK_process'];
                             if ($input['FK_port_source'] != $input['FK_port_destination']) {
                                 $pfihc->add($input);
                             }
                         }
                     }
                 }
             }
             $query_delete = "DELETE FROM `" . $this->table . "`\n                  WHERE `ID`='" . $data['ID'] . "' ";
             $DB->query($query_delete);
             if (preg_match("/00\$/", $i)) {
                 changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
             }
         }
     }
     changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     echo "</td>";
     echo "</tr>";
     echo "</table></center>";
 }
Пример #3
0
 /**
  * Merge softwares with current
  *
  * @param $item array of software ID to be merged
  *
  * @return boolean about success
  **/
 function merge($item)
 {
     global $DB, $LANG;
     $ID = $this->getField('id');
     echo "<div class='center'>";
     echo "<table class='tab_cadrehov'><tr><th>" . $LANG['software'][47] . "</th></tr>";
     echo "<tr class='tab_bg_2'><td>";
     createProgressBar($LANG['rulesengine'][90]);
     echo "</td></tr></table></div>\n";
     $item = array_keys($item);
     // Search for software version
     $req = $DB->request("glpi_softwareversions", array("softwares_id" => $item));
     $i = 0;
     if ($nb = $req->numrows()) {
         foreach ($req as $from) {
             $found = false;
             foreach ($DB->request("glpi_softwareversions", array("softwares_id" => $ID, "name" => $from["name"])) as $dest) {
                 // Update version ID on License
                 $sql = "UPDATE `glpi_softwarelicenses`\n                       SET `softwareversions_id_buy` = '" . $dest["id"] . "'\n                       WHERE `softwareversions_id_buy` = '" . $from["id"] . "'";
                 $DB->query($sql);
                 $sql = "UPDATE `glpi_softwarelicenses`\n                       SET `softwareversions_id_use` = '" . $dest["id"] . "'\n                       WHERE `softwareversions_id_use` = '" . $from["id"] . "'";
                 $DB->query($sql);
                 // Move installation to existing version in destination software
                 $sql = "UPDATE `glpi_computers_softwareversions`\n                       SET `softwareversions_id` = '" . $dest["id"] . "'\n                       WHERE `softwareversions_id` = '" . $from["id"] . "'";
                 $found = $DB->query($sql);
             }
             if ($found) {
                 // Installation has be moved, delete the source version
                 $sql = "DELETE\n                       FROM `glpi_softwareversions`\n                       WHERE `id` = '" . $from["id"] . "'";
             } else {
                 // Move version to destination software
                 $sql = "UPDATE `glpi_softwareversions`\n                       SET `softwares_id` = '{$ID}',\n                           `entities_id` = '" . $this->getField('entities_id') . "'\n                       WHERE `id` = '" . $from["id"] . "'";
             }
             if ($DB->query($sql)) {
                 $i++;
             }
             changeProgressBarPosition($i, $nb + 1);
         }
     }
     // Move software license
     $sql = "UPDATE `glpi_softwarelicenses`\n              SET `softwares_id` = '{$ID}'\n              WHERE `softwares_id` IN ('" . implode("','", $item) . "')";
     if ($DB->query($sql)) {
         $i++;
     }
     if ($i == $nb + 1) {
         //error_log ("All merge operations ok.");
         $soft = new self();
         foreach ($item as $old) {
             $soft->putInTrash($old, $LANG['software'][49]);
         }
     }
     changeProgressBarPosition($i, $nb + 1, $LANG['rulesengine'][91]);
     return $i == $nb + 1;
 }
 /**
  * Replay collection rules on an existing DB for model dropdowns
  *
  * @param $offset offset used to begin
  * @param $maxtime maximum time of process (reload at the end)
  *
  * @return -1 on completion else current offset
  **/
 function replayRulesOnExistingDBForModel($offset = 0, $maxtime = 0)
 {
     global $DB, $LANG;
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB started : " . date("r") . "\n";
     }
     // Model check : need to check using manufacturer extra data
     if (strpos($this->item_table, 'models') === false) {
         echo "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_array($result)) {
             if (!($i % $step)) {
                 if (isCommandLine()) {
                     echo "replayRulesOnExistingDB : {$i}/{$nb}\r";
                 } else {
                     changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
                 }
             }
             // Model case
             if (isset($data["manufacturer"])) {
                 $data["manufacturer"] = Manufacturer::processName($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_array($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
                         if (!class_exists('CartridgeItem')) {
                             include_once GLPI_ROOT . "/inc/cartridgeitem.function.php";
                         }
                         $ct = new CartridgeItem();
                         foreach ($carttype as $cartID) {
                             foreach ($tab as $model) {
                                 $ct->addCompatibleType($cartID, $model);
                             }
                         }
                     }
                 }
             }
         }
         // each tocheck
     }
     if (isCommandLine()) {
         echo "replayRulesOnExistingDB ended : " . date("r") . "\n";
     } else {
         changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
     }
     return $i == $nb ? -1 : $i;
 }