openstamanager/modules/aggiornamenti/database.php

412 lines
12 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-08-28 10:18:35 +02:00
$results_added = integrity_diff($info, $data);
2024-10-30 10:30:58 +01:00
2024-01-15 14:52:26 +01:00
$contents = file_get_contents(base_dir().'/settings.json');
2024-10-30 10:30:58 +01:00
$data_settings = json_decode($contents, true);
2024-01-15 14:52:26 +01:00
$settings = Update::getSettings();
2024-10-30 10:30:58 +01:00
$results_settings = settings_diff($data_settings, $settings);
$results_settings_added = settings_diff($settings, $data_settings);
2024-01-15 14:52:26 +01:00
// Schermata di visualizzazione degli errori
2024-08-28 10:18:35 +02:00
if (!empty($results) || !empty($results_added) || !empty($results_settings) || !empty($results_settings_added)) {
2024-11-05 16:54:48 +01:00
if ($results) {
echo '
2024-11-05 16:54:48 +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>
'.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-11-05 16:54:48 +01:00
</div>';
2024-01-17 18:06:37 +01:00
foreach ($results as $table => $errors) {
echo '
2024-11-05 16:54:48 +01:00
<h3>'.$table.'</h3>';
2024-01-17 18:06:37 +01:00
if (array_key_exists('current', $errors) && $errors['current'] == null) {
echo '
2024-11-05 16:54:48 +01:00
<div class="alert alert-danger" ><i class="fa fa-times"></i> '.tr('Tabella assente').'
</div>';
2024-01-17 18:06:37 +01:00
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 '
2024-11-05 16:54:48 +01:00
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Colonna').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
<tbody>';
2024-01-17 18:06:37 +01:00
foreach ($errors as $name => $diff) {
2024-10-30 10:30:58 +01:00
$query = '';
$null = '';
if (array_key_exists('key', $diff)) {
if ($diff['key']['expected'] == '') {
$query = 'Chiave non prevista';
} else {
$query = 'Chiave mancante';
}
} else {
$query .= 'ALTER TABLE `'.$table.'` CHANGE `'.$name.'` `'.$name.'` '.$data[$table][$name]['type'];
if ($data[$table][$name]['null'] == 'NO') {
$null = 'NOT NULL';
} else {
$null = 'NULL';
}
2024-10-31 10:10:02 +01:00
$query .= str_replace('DEFAULT_GENERATED', ' ', $data[$table][$name]['extra']).' '.$null;
if ($data[$table][$name]['default']) {
$query .= ' DEFAULT '.$data[$table][$name]['default'];
}
2024-10-30 10:30:58 +01:00
}
2024-01-17 18:06:37 +01:00
echo '
2024-11-05 16:54:48 +01:00
<tr class="bg-warning" >
<td>
'.$name.'
</td>
<td>
'.$query.';
</td>
</tr>';
2024-10-30 10:30:58 +01:00
2024-01-17 18:06:37 +01:00
}
2024-10-30 10:30:58 +01:00
2024-01-17 18:06:37 +01:00
echo '
2024-11-05 16:54:48 +01:00
</tbody>
</table>';
2024-01-17 18:06:37 +01:00
}
2024-01-17 18:06:37 +01:00
if (!empty($foreign_keys)) {
echo '
2024-11-05 16:54:48 +01:00
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Foreign keys').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
2024-01-17 18:06:37 +01:00
2024-11-05 16:54:48 +01:00
<tbody>';
2024-01-17 18:06:37 +01:00
2024-08-28 11:28:21 +02:00
foreach ($foreign_keys as $name => $diff) {
2024-01-17 18:06:37 +01:00
echo '
2024-11-05 16:54:48 +01:00
<tr class="bg-warning" >
<td>
'.($name ?: $diff['expected']['title']).'
</td>
<td>
ALTER TABLE '.$table.' ADD CONSTRAINT '.$name.' FOREIGN KEY ('.$diff['expected']['column'].') REFERENCES '.$diff['expected']['referenced_table'].'(`'.$diff['expected']['referenced_column'].'`) ON DELETE '.$diff['expected']['delete_rule'].' ON UPDATE '.$diff['expected']['update_rule'].';
</td>
</tr>';
2024-08-28 10:18:35 +02:00
}
echo '
2024-11-05 16:54:48 +01:00
</tbody>
</table>';
2024-08-28 10:18:35 +02:00
}
}
}
2024-11-05 16:54:48 +01:00
if ($results_added) {
2024-08-28 10:18:35 +02:00
foreach ($results_added as $table => $errors) {
2024-11-05 16:54:48 +01:00
if (($results[$table] && array_keys($results[$table]) != array_keys($errors)) || (empty($results[$table]) && !empty($errors))) {
2024-08-28 10:18:35 +02:00
echo '
2024-11-05 16:54:48 +01:00
<h3>'.$table.'</h3>';
2024-08-28 10:18:35 +02:00
2024-10-30 10:30:58 +01:00
if (array_key_exists('current', $errors) && $errors['current'] == null) {
echo '
2024-11-05 16:54:48 +01:00
<div class="alert alert-danger" ><i class="fa fa-times"></i> '.tr('Tabella assente').'
</div>';
2024-10-30 10:30:58 +01:00
continue;
}
2024-08-28 10:18:35 +02:00
2024-10-30 10:30:58 +01:00
$foreign_keys = $errors['foreign_keys'] ?: [];
unset($errors['foreign_keys']);
2024-08-28 10:18:35 +02:00
2024-10-30 10:30:58 +01:00
if (!empty($errors)) {
2024-08-28 10:18:35 +02:00
echo '
2024-11-05 16:54:48 +01:00
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Colonna').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
2024-10-30 10:30:58 +01:00
2024-11-05 16:54:48 +01:00
<tbody>';
2024-10-30 10:30:58 +01:00
foreach ($errors as $name => $diff) {
$query = '';
if ((!isset($results[$table][$name]))) {
if (isset($diff['key'])) {
if ($diff['key']['expected'] == '') {
$query = 'Chiave non prevista';
} else {
$query = 'Chiave mancante';
}
} else {
$query = 'Campo non previsto';
}
2024-01-17 18:06:37 +01:00
2024-10-30 10:30:58 +01:00
echo '
2024-11-05 16:54:48 +01:00
<tr class="bg-info" >
<td>
'.$name.'
</td>
<td>
'.$query.'
</td>
</tr>';
2024-10-30 10:30:58 +01:00
}
}
2024-10-30 10:30:58 +01:00
echo '
2024-11-05 16:54:48 +01:00
</tbody>
</table>';
2024-10-30 10:30:58 +01:00
}
2024-08-28 10:18:35 +02:00
2024-10-30 10:30:58 +01:00
if (!empty($foreign_keys)) {
echo '
2024-11-05 16:54:48 +01:00
<table class="table table-bordered">
<thead>
<tr>
<th>'.tr('Foreign keys').'</th>
<th>'.tr('Conflitto').'</th>
</tr>
</thead>
2024-10-30 10:30:58 +01:00
2024-11-05 16:54:48 +01:00
<tbody>';
2024-10-30 10:30:58 +01:00
foreach ($foreign_keys as $name => $diff) {
echo '
2024-11-05 16:54:48 +01:00
<tr class="bg-info" >
<td>
'.$name.'
</td>
<td>
Chiave esterna non prevista
</td>
</tr>';
2024-10-30 10:30:58 +01:00
}
2024-08-28 10:18:35 +02:00
echo '
2024-11-05 16:54:48 +01:00
</tbody>
</table>';
2024-08-28 10:18:35 +02:00
}
}
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-11-05 16:54:48 +01:00
if ($results_settings) {
2024-01-15 15:30:45 +01:00
echo '
2024-01-15 14:52:26 +01:00
<table class="table table-bordered">
2024-11-05 16:54:48 +01:00
<thead>
<h3>Problemi impostazioni</h3>
<tr>
<th>'.tr('Nome').'</th>
<th>'.tr('Valore attuale').'</th>
<th>'.tr('Valore atteso').'</th>
</tr>
</thead>
2024-01-15 14:52:26 +01:00
<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-11-05 16:54:48 +01:00
<tr class="bg-'.$class.'" >
<td>
'.$key.'
</td>
<td>
'.($setting['current'] ?: '⚠️ Impostazione mancante').'
</td>
<td>
'.$setting['expected'].'
</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-11-05 16:54:48 +01:00
if ($results_settings_added) {
2024-01-16 10:23:22 +01:00
echo '
<table class="table table-bordered">
2024-11-05 16:54:48 +01:00
<thead>
<h3>Impostazioni non previste</h3>
<tr>
<th>'.tr('Nome').'</th>
<th>'.tr('Valore attuale').'</th>
</tr>
</thead>
2024-01-16 10:23:22 +01:00
<tbody>';
foreach ($results_settings_added as $key => $setting) {
if ($setting['current'] == null) {
echo '
2024-11-05 16:54:48 +01:00
<tr class="bg-info" >
<td>
'.$key.'
</td>
<td>
'.$setting['expected'].'
</td>
</tr>';
2024-01-16 10:23:22 +01:00
}
}
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-11-05 16:54:48 +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
}