openstamanager/modules/aggiornamenti/database.php

308 lines
9.1 KiB
PHP
Raw Normal View History

<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
include_once __DIR__.'/../../core.php';
function integrity_diff($expected, $current)
{
foreach ($expected as $key => $value) {
if (array_key_exists($key, $current) && is_array($value)) {
if (!is_array($current[$key])) {
$difference[$key] = $value;
} else {
$new_diff = integrity_diff($value, $current[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
} elseif (!array_key_exists($key, $current) || $current[$key] != $value) {
$difference[$key] = [
'current' => $current[$key],
'expected' => $value,
];
}
}
return !isset($difference) ? [] : $difference;
}
2024-01-16 10:23:22 +01:00
function settings_diff($expected, $current)
{
foreach ($expected as $key => $value) {
if (array_key_exists($key, $current)) {
if (!is_array($current[$key])) {
if ($current[$key] !== $value) {
$difference[$key] = [
'current' => $current[$key],
'expected' => $value,
];
}
} else {
$new_diff = integrity_diff($value, $current[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
} else {
$difference[$key] = [
'current' => null,
'expected' => $value,
];
}
}
2024-01-31 14:23:46 +01:00
2024-01-16 10:23:22 +01:00
return $difference;
}
$file = basename(__FILE__);
$effettua_controllo = filter('effettua_controllo');
// Schermata di caricamento delle informazioni
if (empty($effettua_controllo)) {
echo '
<div id="righe_controlli">
</div>
2024-05-16 18:02:50 +02:00
<div class="alert alert-info" id="card-loading">
<i class="fa fa-spinner fa-spin"></i> '.tr('Caricamento in corso').'...
</div>
<script>
var content = $("#righe_controlli");
2024-05-16 18:02:50 +02:00
var loader = $("#card-loading");
$(document).ready(function () {
loader.show();
content.html("");
content.load("'.$structure->fileurl($file).'?effettua_controllo=1", function() {
loader.hide();
});
})
</script>';
return;
}
switch ($database->getType()) {
case 'MariaDB':
$file_to_check_database = 'mariadb_10_x.json';
break;
case 'MySQL':
$mysql_min_version = '5.7.0';
$mysql_max_version = '5.7.99';
2024-03-22 15:52:24 +01:00
$file_to_check_database = ((version_compare($database->getMySQLVersion(), $mysql_min_version, '>=') && version_compare($database->getMySQLVersion(), $mysql_max_version, '<=')) ? 'mysql_5_7.json' : 'mysql.json');
break;
default:
$file_to_check_database = 'mysql.json';
break;
}
$contents = file_get_contents(base_dir().'/'.$file_to_check_database);
$data = json_decode($contents, true);
if (empty($data)) {
echo '
<div class="alert alert-warning">
<i class="fa fa-warning"></i> '.tr('Impossibile effettuare controlli di integrità in assenza del file _FILE_', [
2024-03-22 15:52:24 +01:00
'_FILE_' => '<b>'.$file_to_check_database.'</b>',
]).'.
</div>';
return;
}
// Controllo degli errori
$info = Update::getDatabaseStructure();
$results = integrity_diff($data, $info);
2024-01-15 14:52:26 +01:00
$contents = file_get_contents(base_dir().'/settings.json');
$data = json_decode($contents, true);
$settings = Update::getSettings();
2024-01-16 10:23:22 +01:00
$results_settings = settings_diff($data, $settings);
$results_settings_added = settings_diff($settings, $data);
2024-01-15 14:52:26 +01:00
// Schermata di visualizzazione degli errori
2024-01-17 18:06:37 +01:00
if (!empty($results) || !empty($results_settings) || !empty($results_settings_added)) {
if (!empty($results)) {
echo '
2024-01-17 18:06:37 +01:00
<p>'.tr("Segue l'elenco delle tabelle del database che presentano una struttura diversa rispetto a quella prevista nella versione ufficiale del gestionale").'.</p>
<div class="alert alert-warning">
<i class="fa fa-warning"></i>
2024-03-03 12:55:26 +01:00
'.tr('Attenzione: questa funzionalità può presentare dei risultati falsamente positivi, sulla base del contenuto del file _FILE_ e la versione di _MYSQL_VERSION_ di _DBMS_TYPE_ rilevata a sistema', [
2024-03-22 15:52:24 +01:00
'_FILE_' => '<b>'.$file_to_check_database.'</b>',
'_MYSQL_VERSION_' => '<b>'.$database->getMySQLVersion().'</b>',
'_DBMS_TYPE_' => '<b>'.$database->getType().'</b>',
]).'.
2024-01-17 18:06:37 +01:00
</div>';
foreach ($results as $table => $errors) {
echo '
2024-01-17 18:06:37 +01:00
<h3>'.$table.'</h3>';
2024-01-17 18:06:37 +01:00
if (array_key_exists('current', $errors) && $errors['current'] == null) {
echo '
2024-01-17 18:06:37 +01:00
<div class="alert alert-danger" ><i class="fa fa-times"></i> '.tr('Tabella assente').'</div>';
continue;
}
2024-01-17 18:06:37 +01:00
$foreign_keys = $errors['foreign_keys'] ?: [];
unset($errors['foreign_keys']);
2024-01-17 18:06:37 +01:00
if (!empty($errors)) {
echo '
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Colonna').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
<tbody>';
foreach ($errors as $name => $diff) {
if (count($diff) == 1 && array_key_exists('type', $diff) && string_contains($diff['type']['expected'], $diff['type']['current'])) {
$class = 'info';
} else {
$class = 'warning';
}
echo '
<tr class="bg-'.$class.'" >
<td>
'.$name.'
</td>
<td>
'.json_encode($diff).'
</td>
</tr>';
}
2024-01-17 18:06:37 +01:00
echo '
</tbody>
</table>';
}
2024-01-17 18:06:37 +01:00
if (!empty($foreign_keys)) {
echo '
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Foreign keys').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
<tbody>';
foreach ($foreign_keys as $name => $diff) {
if (count($diff) == 2 && array_key_exists('current', $diff) && $diff['current'] == null) {
$class = 'info';
} else {
$class = 'warning';
}
echo '
<tr class="bg-'.$class.'" >
<td>
'.$name.'
</td>
<td>
'.json_encode($diff).'
</td>
</tr>';
2021-01-14 02:24:28 +01:00
}
2024-01-17 18:06:37 +01:00
echo '
2024-01-17 18:06:37 +01:00
</tbody>
</table>';
}
2024-01-15 15:30:45 +01:00
}
2024-01-17 18:06:37 +01:00
}
2024-01-31 14:23:46 +01:00
2024-01-15 14:52:26 +01:00
if (!empty($results_settings)) {
2024-01-15 15:30:45 +01:00
echo '
2024-01-15 14:52:26 +01:00
<table class="table table-bordered">
<thead>
2024-01-16 10:23:22 +01:00
<h3>Problemi impostazioni</h3>
2024-01-15 14:52:26 +01:00
<tr>
<th>'.tr('Nome').'</th>
2024-01-16 10:23:22 +01:00
<th>'.tr('Valore attuale').'</th>
<th>'.tr('Valore atteso').'</th>
2024-01-15 14:52:26 +01:00
</tr>
</thead>
<tbody>';
foreach ($results_settings as $key => $setting) {
2024-01-31 14:23:46 +01:00
if (!$setting['current']) {
$class = 'danger';
} else {
$class = 'warning';
}
2024-01-15 15:30:45 +01:00
echo '
2024-01-16 10:23:22 +01:00
<tr class="bg-'.$class.'" >
2024-01-15 14:52:26 +01:00
<td>
'.$key.'
</td>
<td>
2024-04-08 15:44:33 +02:00
'.($setting['current'] ?: '⚠️ Impostazione mancante').'
2024-01-16 10:23:22 +01:00
</td>
<td>
'.$setting['expected'].'
2024-01-15 14:52:26 +01:00
</td>
</tr>';
}
2024-01-15 15:30:45 +01:00
echo '
2024-01-15 14:52:26 +01:00
</tbody>
2024-01-16 10:23:22 +01:00
</table>';
}
2024-01-31 14:32:05 +01:00
if (!empty($results_settings_added) && empty($results_settings)) {
2024-01-16 10:23:22 +01:00
echo '
<table class="table table-bordered">
<thead>
<h3>Impostazioni non previste</h3>
<tr>
<th>'.tr('Nome').'</th>
<th>'.tr('Valore attuale').'</th>
</tr>
</thead>
<tbody>';
foreach ($results_settings_added as $key => $setting) {
if ($setting['current'] == null) {
echo '
<tr class="bg-info" >
<td>
'.$key.'
</td>
<td>
'.$setting['expected'].'
</td>
</tr>';
}
}
echo '
</tbody>
2024-01-15 14:52:26 +01:00
</table>';
}
2024-01-15 15:30:45 +01:00
} else {
echo '
<div class="alert alert-info">
2024-01-15 14:52:26 +01:00
<i class="fa fa-info-circle"></i> '.tr('Il database non presenta problemi di integrità').'.
</div>';
2024-01-15 15:30:45 +01:00
}