From 735d3b2bf924faec53122a8e967287673c01eba7 Mon Sep 17 00:00:00 2001 From: Dasc3er Date: Wed, 7 Jul 2021 11:27:46 +0200 Subject: [PATCH] Correzioni per esecuzione aggiornamenti --- assets/src/js/functions/functions.js | 2 +- src/Database.php | 8 +- src/Update.php | 147 +++++++++++++-------------- 3 files changed, 72 insertions(+), 85 deletions(-) diff --git a/assets/src/js/functions/functions.js b/assets/src/js/functions/functions.js index 12b440103..3d0a37504 100755 --- a/assets/src/js/functions/functions.js +++ b/assets/src/js/functions/functions.js @@ -613,7 +613,7 @@ function apriTab(link) { function ajaxError(xhr, error, thrown) { swal({ title: globals.translations.errorTitle, - html: globals.translations.errorMessage + (xhr.responseJSON ? ".
" + xhr.responseJSON.exception[0].message + "" : ''), + html: globals.translations.errorMessage + (xhr.responseJSON ? ".
" + xhr.responseJSON.exception + ": " + xhr.responseJSON.message + "" : ""), type: "error", }); } diff --git a/src/Database.php b/src/Database.php index 9e544e130..8e53ab428 100755 --- a/src/Database.php +++ b/src/Database.php @@ -27,12 +27,6 @@ use Illuminate\Support\Facades\Schema; */ class Database { - /** @var \Illuminate\Database\Capsule\Manager Gestore di connessione Laravel */ - protected $capsule; - - /** @var string Nome del database */ - protected $database_name; - /** @var bool Stato di connessione del database */ protected $is_connected; /** @var bool Stato di installazione del database */ @@ -122,7 +116,7 @@ class Database */ public function getDatabaseName() { - return $this->database_name; + return DB::connection()->getDatabaseName(); } /** diff --git a/src/Update.php b/src/Update.php index a70ec2bda..16d61f51d 100755 --- a/src/Update.php +++ b/src/Update.php @@ -241,94 +241,85 @@ class Update if (!self::isUpdateCompleted()) { $update = self::getCurrentUpdate(); - $file = base_dir().'/'.$update['directory'].$update['filename']; + $file = slashes(base_dir().substr($update['directory'], 1).$update['filename']); $database = database(); - try { - // Esecuzione delle query - if (!empty($update['sql']) && (!empty($update['done']) || is_null($update['done'])) && file_exists($file.'.sql')) { - $queries = readSQLFile($file.'.sql', ';'); - $count = count($queries); + // Esecuzione delle query + if (!empty($update['sql']) && (!empty($update['done']) || is_null($update['done'])) && file_exists($file.'.sql')) { + $queries = readSQLFile($file.'.sql', ';'); + $count = count($queries); - $start = empty($update['done']) ? 0 : $update['done'] - 2; - $end = ($start + $rate + 1) > $count ? $count : $start + $rate + 1; + $start = empty($update['done']) ? 0 : $update['done'] - 2; + $end = ($start + $rate + 1) > $count ? $count : $start + $rate + 1; - if ($start < $end) { - for ($i = $start; $i < $end; ++$i) { - try { - $database->query($queries[$i]); - } catch (\Exception $e) { - throw new PDOException(tr('Aggiornamento fallito').': '.$queries[$i]); - } - - $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ - ':done' => $i + 3, - ':id' => $update['id'], - ]); + if ($start < $end) { + for ($i = $start; $i < $end; ++$i) { + try { + $database->query($queries[$i]); + } catch (\Exception $e) { + throw new PDOException($queries[$i]); } - // Restituisce l'indice della prima e dell'ultima query eseguita, con la differenza relativa per l'avanzamento dell'aggiornamento - return [ - $start, - $end, - $count, + $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ + ':done' => $i + 3, + ':id' => $update['id'], + ]); + } + + // Restituisce l'indice della prima e dell'ultima query eseguita, con la differenza relativa per l'avanzamento dell'aggiornamento + return [ + $start, + $end, + $count, + ]; + } + } + + // Imposta l'aggiornamento nello stato di esecuzione dello script + $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ + ':done' => 0, + ':id' => $update['id'], + ]); + + // Permessi di default delle viste + if ($database->tableExists('zz_views')) { + $gruppi = $database->fetchArray('SELECT `id` FROM `zz_groups`'); + $viste = $database->fetchArray('SELECT `id` FROM `zz_views` WHERE `id` NOT IN (SELECT `id_vista` FROM `zz_group_view`)'); + + $array = []; + foreach ($viste as $vista) { + foreach ($gruppi as $gruppo) { + $array[] = [ + 'id_gruppo' => $gruppo['id'], + 'id_vista' => $vista['id'], ]; } } - - // Imposta l'aggiornamento nello stato di esecuzione dello script - $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ - ':done' => 0, - ':id' => $update['id'], - ]); - - // Permessi di default delle viste - if ($database->tableExists('zz_views')) { - $gruppi = $database->fetchArray('SELECT `id` FROM `zz_groups`'); - $viste = $database->fetchArray('SELECT `id` FROM `zz_views` WHERE `id` NOT IN (SELECT `id_vista` FROM `zz_group_view`)'); - - $array = []; - foreach ($viste as $vista) { - foreach ($gruppi as $gruppo) { - $array[] = [ - 'id_gruppo' => $gruppo['id'], - 'id_vista' => $vista['id'], - ]; - } - } - if (!empty($array)) { - $database->insert('zz_group_view', $array); - } + if (!empty($array)) { + $database->insert('zz_group_view', $array); } - - // Normalizzazione di charset e collation - self::normalizeDatabase($database->getDatabaseName()); - - // Normalizzazione dei campi per l'API - self::executeScript(base_dir().'/update/api.php'); - - // Esecuzione dello script - if (!empty($update['script']) && file_exists($file.'.php')) { - self::executeScript($file.'.php'); - } - - // Imposta l'aggiornamento come completato - $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ - ':done' => 1, - ':id' => $update['id'], - ]); - - // Normalizzazione di charset e collation - self::normalizeDatabase($database->getDatabaseName()); - - return true; - } catch (\Exception $e) { - $logger = logger(); - $logger->addRecord(\Monolog\Logger::EMERGENCY, $e->getMessage()); } - return false; + // Normalizzazione di charset e collation + self::normalizeDatabase($database->getDatabaseName()); + + // Normalizzazione dei campi per l'API + self::executeScript(base_dir().'/update/api.php'); + + // Esecuzione dello script + if (!empty($update['script']) && file_exists($file.'.php')) { + self::executeScript($file.'.php'); + } + + // Imposta l'aggiornamento come completato + $database->query('UPDATE `updates` SET `done` = :done WHERE id = :id', [ + ':done' => 1, + ':id' => $update['id'], + ]); + + // Normalizzazione di charset e collation + self::normalizeDatabase($database->getDatabaseName()); } return true; @@ -437,8 +428,8 @@ class Update // Inserimento degli aggiornamenti individuati foreach ($results as $result) { // Individuazione di script e sql - $sql = file_exists($result['path'].'.sql') ? 1 : 0; - $script = file_exists($result['path'].'.php') ? 1 : 0; + $sql = file_exists(base_dir().$result['path'].'.sql') ? 1 : 0; + $script = file_exists(base_dir().$result['path'].'.php') ? 1 : 0; // Reimpostazione degli stati per gli aggiornamenti precedentemente presenti $pos = array_search($result['path'], $versions); @@ -574,6 +565,8 @@ class Update */ protected static function normalizeDatabase($database_name) { + return; + set_time_limit(0); ignore_user_abort(true);