From c59c5efc1124b707f2bb2ac03e98f51f1c7637ad Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sun, 1 May 2022 16:48:43 +0200 Subject: [PATCH] migrate update/preconfig to a more OOP way and remove unnecessary file/dir complexity Signed-off-by: Michael Kaufmann --- admin_updates.php | 15 +-- install/updates/froxlor/0.11/index.html | 0 .../froxlor/{0.11 => }/update_0.11.inc.php | 5 + install/updates/preconfig.php | 80 ------------ install/updates/preconfig/0.11/index.html | 0 .../preconfig/0.11/preconfig_0.11.inc.php | 75 ------------ .../updates/preconfig/preconfig_0.11.inc.php | 76 ++++++++++++ install/updatesql.php | 4 +- lib/Froxlor/Install/Preconfig.php | 114 ++++++++++++++++++ lib/Froxlor/Install/Update.php | 10 ++ lng/de.lng.php | 2 +- lng/en.lng.php | 2 +- lng/it.lng.php | 2 +- lng/nl.lng.php | 2 +- 14 files changed, 219 insertions(+), 168 deletions(-) delete mode 100644 install/updates/froxlor/0.11/index.html rename install/updates/froxlor/{0.11 => }/update_0.11.inc.php (97%) delete mode 100644 install/updates/preconfig.php delete mode 100644 install/updates/preconfig/0.11/index.html delete mode 100644 install/updates/preconfig/0.11/preconfig_0.11.inc.php create mode 100644 install/updates/preconfig/preconfig_0.11.inc.php create mode 100644 lib/Froxlor/Install/Preconfig.php diff --git a/admin_updates.php b/admin_updates.php index 4b79a87b..5aa632dc 100644 --- a/admin_updates.php +++ b/admin_updates.php @@ -30,6 +30,7 @@ use Froxlor\Cron\TaskId; use Froxlor\Database\Database; use Froxlor\Froxlor; use Froxlor\FroxlorLogger; +use Froxlor\Install\Preconfig; use Froxlor\Settings; use Froxlor\System\Cronjob; use Froxlor\UI\Panel\UI; @@ -96,15 +97,15 @@ if ($page == 'overview') { $new_version = Froxlor::VERSION; $new_db_version = Froxlor::DBVERSION; - $ui_text = lng('update.update_information.part_a'); if (Froxlor::VERSION != $current_version) { - $ui_text = str_replace('%curversion', $current_version, $ui_text); - $ui_text = str_replace('%newversion', $new_version, $ui_text); + $replacer_currentversion = $current_version; + $replacer_newversion = $new_version; } else { // show db version - $ui_text = str_replace('%curversion', $current_db_version, $ui_text); - $ui_text = str_replace('%newversion', $new_db_version, $ui_text); + $replacer_currentversion = $current_db_version; + $replacer_newversion = $new_db_version; } + $ui_text = lng('update.update_information.part_a', [$replacer_newversion, $replacer_currentversion]); $ui_text .= lng('update.update_information.part_b'); $upd_formfield = [ @@ -121,8 +122,8 @@ if ($page == 'overview') { ] ]; - include_once Froxlor::getInstallDir() . '/install/updates/preconfig.php'; - $preconfig = getPreConfig($current_version, $current_db_version); + $preconfig = Preconfig::getPreConfig(); + if (!empty($preconfig)) { $upd_formfield['updates']['sections'] = $preconfig; } diff --git a/install/updates/froxlor/0.11/index.html b/install/updates/froxlor/0.11/index.html deleted file mode 100644 index e69de29b..00000000 diff --git a/install/updates/froxlor/0.11/update_0.11.inc.php b/install/updates/froxlor/update_0.11.inc.php similarity index 97% rename from install/updates/froxlor/0.11/update_0.11.inc.php rename to install/updates/froxlor/update_0.11.inc.php index ef7c5c0e..d968e7d2 100644 --- a/install/updates/froxlor/0.11/update_0.11.inc.php +++ b/install/updates/froxlor/update_0.11.inc.php @@ -87,6 +87,11 @@ if (Froxlor::isFroxlorVersion('0.10.99')) { $to_clean = array( "install/lib", "install/lng", + "install/updates/froxlor/0.9", + "install/updates/froxlor/0.10", + "install/updates/preconfig/0.9", + "install/updates/preconfig/0.10", + "install/updates/preconfig.php", "templates/Sparkle", "lib/version.inc.php", "lng/czech.lng.php", diff --git a/install/updates/preconfig.php b/install/updates/preconfig.php deleted file mode 100644 index e01c09fe..00000000 --- a/install/updates/preconfig.php +++ /dev/null @@ -1,80 +0,0 @@ - - * @license https://files.froxlor.org/misc/COPYING.txt GPLv2 - */ - -use Froxlor\Froxlor; -use Froxlor\FileDir; - -/** - * Function getPreConfig - * - * outputs various form-field-arrays before the update process - * can be continued (asks for agreement whatever is being asked) - * - * @param string $current_version - * @param int $current_db_version - * - * @return array - */ -function getPreConfig($current_version, $current_db_version): array -{ - $has_preconfig = false; - - include_once FileDir::makeCorrectFile(dirname(__FILE__) . '/preconfig/0.11/preconfig_0.11.inc.php'); - $return['section_011'] = [ - 'title' => '0.11.x updates', - 'fields' => [] - ]; - parseAndOutputPreconfig011($has_preconfig, $return['section_011']['fields'], $current_version, $current_db_version); - - if (empty($return['section_011']['fields'])) { - unset($return['section_011']); - } - - if (!empty($return)) { - $has_preconfig = true; - $return['section_agree'] = [ - 'title' => 'Check', - 'fields' => [ - 'update_changesagreed' => ['type' => 'checkbox', 'value' => 1, 'label' => 'I have read the update notifications above and I am aware of the changes made to my system.'], - 'update_preconfig' => ['type' => 'hidden', 'value' => 1] - ] - ]; - } - - if ($has_preconfig) { - return $return; - } else { - return []; - } -} - -function versionInUpdate($current_version, $version_to_check) -{ - if (!Froxlor::isFroxlor()) { - return true; - } - - return Froxlor::versionCompare2($current_version, $version_to_check) == -1; -} diff --git a/install/updates/preconfig/0.11/index.html b/install/updates/preconfig/0.11/index.html deleted file mode 100644 index e69de29b..00000000 diff --git a/install/updates/preconfig/0.11/preconfig_0.11.inc.php b/install/updates/preconfig/0.11/preconfig_0.11.inc.php deleted file mode 100644 index e9a7f959..00000000 --- a/install/updates/preconfig/0.11/preconfig_0.11.inc.php +++ /dev/null @@ -1,75 +0,0 @@ - (2010-) - * @license GPLv2 https://files.froxlor.org/misc/COPYING.txt - * @package Updater - * - */ - -use Froxlor\Froxlor; -use Froxlor\FileDir; -use Froxlor\Config\ConfigParser; - -/** - * checks if the new-version has some updating to do - * - * @param boolean $has_preconfig - * pointer to check if any preconfig has to be output - * @param string $return - * pointer to output string - * @param string $current_version - * current froxlor version - * - * @return void - */ -function parseAndOutputPreconfig011(&$has_preconfig, &$return, $current_version, $current_db_version) -{ - - if (versionInUpdate($current_version, '0.10.99')) { - $has_preconfig = true; - $description = 'We have rearranged the settings and split them into basic and advanced categories. This makes it easier for users who do not need all the detailed or very specific settings and options and gives a better overview of the basic/mostly used settings.'; - $return['panel_settings_mode_note'] = ['type' => 'infotext', 'value' => $description]; - $question = 'Chose settings mode (you can change that at any time)'; - $return['panel_settings_mode'] = [ - 'type' => 'select', - 'select_var' => [ - 0 => 'Basic', - 1 => 'Advanced' - ], - 'selected' => 1, - 'label' => $question - ]; - - $description = 'The configuration page now can preselect a distribution, please select your current distribution'; - $return['system_distribution_note'] = ['type' => 'infotext', 'value' => $description]; - $question = 'Select distribution'; - $config_dir = FileDir::makeCorrectDir(Froxlor::getInstallDir() . '/lib/configfiles/'); - // show list of available distro's - $distros = glob($config_dir . '*.xml'); - $distributions_select[''] = '-'; - // read in all the distros - foreach ($distros as $_distribution) { - // get configparser object - $dist = new ConfigParser($_distribution); - // store in tmp array - $distributions_select[str_replace(".xml", "", strtolower(basename($_distribution)))] = $dist->getCompleteDistroName(); - } - // sort by distribution name - asort($distributions_select); - $return['system_distribution'] = [ - 'type' => 'select', - 'select_var' => $distributions_select, - 'selected' => '', - 'label' => $question - ]; - } -} diff --git a/install/updates/preconfig/preconfig_0.11.inc.php b/install/updates/preconfig/preconfig_0.11.inc.php new file mode 100644 index 00000000..3db101ce --- /dev/null +++ b/install/updates/preconfig/preconfig_0.11.inc.php @@ -0,0 +1,76 @@ + + * @license https://files.froxlor.org/misc/COPYING.txt GPLv2 + */ + +use Froxlor\Froxlor; +use Froxlor\FileDir; +use Froxlor\Config\ConfigParser; +use Froxlor\Install\Update; + +$preconfig = [ + 'title' => '0.11.x updates', + 'fields' => [] +]; +$return = []; + +if (Update::versionInUpdate($current_version, '0.10.99')) { + $description = 'We have rearranged the settings and split them into basic and advanced categories. This makes it easier for users who do not need all the detailed or very specific settings and options and gives a better overview of the basic/mostly used settings.'; + $return['panel_settings_mode_note'] = ['type' => 'infotext', 'value' => $description]; + $question = 'Chose settings mode (you can change that at any time)'; + $return['panel_settings_mode'] = [ + 'type' => 'select', + 'select_var' => [ + 0 => 'Basic', + 1 => 'Advanced' + ], + 'selected' => 1, + 'label' => $question + ]; + + $description = 'The configuration page now can preselect a distribution, please select your current distribution'; + $return['system_distribution_note'] = ['type' => 'infotext', 'value' => $description]; + $question = 'Select distribution'; + $config_dir = FileDir::makeCorrectDir(Froxlor::getInstallDir() . '/lib/configfiles/'); + // show list of available distro's + $distros = glob($config_dir . '*.xml'); + $distributions_select[''] = '-'; + // read in all the distros + foreach ($distros as $_distribution) { + // get configparser object + $dist = new ConfigParser($_distribution); + // store in tmp array + $distributions_select[str_replace(".xml", "", strtolower(basename($_distribution)))] = $dist->getCompleteDistroName(); + } + // sort by distribution name + asort($distributions_select); + $return['system_distribution'] = [ + 'type' => 'select', + 'select_var' => $distributions_select, + 'selected' => '', + 'label' => $question + ]; +} + +$preconfig['fields'] = $return; +return $preconfig; diff --git a/install/updatesql.php b/install/updatesql.php index 41acf326..1fc9818e 100644 --- a/install/updatesql.php +++ b/install/updatesql.php @@ -24,7 +24,7 @@ */ use Froxlor\Froxlor; -use Froxlor\Filedir; +use Froxlor\FileDir; use Froxlor\FroxlorLogger; use Froxlor\UI\Response; use Froxlor\Database\IntegrityCheck; @@ -55,7 +55,7 @@ if (Froxlor::isFroxlor()) { $update_tasks = []; $task_counter = 0; - include_once(FileDir::makeCorrectFile(dirname(__FILE__) . '/updates/froxlor/0.11/update_0.11.inc.php')); + include_once(FileDir::makeCorrectFile(dirname(__FILE__) . '/updates/froxlor/update_0.11.inc.php')); // Check Froxlor - database integrity (only happens after all updates are done, so we know the db-layout is okay) Update::showUpdateStep("Checking database integrity"); diff --git a/lib/Froxlor/Install/Preconfig.php b/lib/Froxlor/Install/Preconfig.php new file mode 100644 index 00000000..e82315ea --- /dev/null +++ b/lib/Froxlor/Install/Preconfig.php @@ -0,0 +1,114 @@ + + * @license https://files.froxlor.org/misc/COPYING.txt GPLv2 + */ + +namespace Froxlor\Install; + +use Froxlor\Froxlor; +use Froxlor\Settings; + +class Preconfig +{ + private $preconfig_data = []; + + /** + * returns whether there are preconfig items in an update + * + * @return bool + */ + public function hasPreConfig(): bool + { + return count($this->preconfig_data) > 0; + } + + /** + * return all collected preconfig data + * + * @return array + */ + public function getData(): array + { + return $this->preconfig_data; + } + + /** + * adds an preconfig result-array to the preconfig-data + * + * @param array $array + * + * @return void + */ + public function addToPreConfig(array $array) + { + $this->preconfig_data = $this->preconfig_data + $array; + } + + /** + * read in all preconfig files and build up data-array for admin_updates + */ + public function __construct() + { + $preconfigs = glob(Froxlor::getInstallDir() . '/install/updates/preconfig/*.php'); + + if (!empty($preconfigs)) { + $current_version = Settings::Get('panel.version'); + $current_db_version = Settings::Get('panel.db_version'); + if (empty($current_db_version)) { + $current_db_version = "0"; + } + foreach (array_reverse($preconfigs) as $preconfig_file) { + $pconf = include $preconfig_file; + $this->addToPreConfig($pconf); + } + } + } + /** + * Function getPreConfig + * + * outputs various form-field-arrays before the update process + * can be continued (asks for agreement whatever is being asked) + * + * @return array + */ + public static function getPreConfig(): array + { + $preconfig = new self(); + + if ($preconfig->hasPreConfig()) { + $agree = [ + 'title' => 'Check', + 'fields' => [ + 'update_changesagreed' => ['type' => 'checkbox', 'value' => 1, 'label' => 'I have read the update notifications above and I am aware of the changes made to my system.'], + 'update_preconfig' => ['type' => 'hidden', 'value' => 1] + ] + ]; + $result = [ + $preconfig->getData(), + $agree + ]; + return $result; + } + return []; + } +} diff --git a/lib/Froxlor/Install/Update.php b/lib/Froxlor/Install/Update.php index 19ad9fb7..46b21fe4 100644 --- a/lib/Froxlor/Install/Update.php +++ b/lib/Froxlor/Install/Update.php @@ -25,6 +25,7 @@ namespace Froxlor\Install; +use Froxlor\Froxlor; use Froxlor\FroxlorLogger; class Update @@ -96,4 +97,13 @@ class Update FroxlorLogger::getInstanceOf()->logAction(FroxlorLogger::ADM_ACTION, LOG_WARNING, 'Success'); } } + + public static function versionInUpdate($current_version, $version_to_check) + { + if (!Froxlor::isFroxlor()) { + return true; + } + + return Froxlor::versionCompare2($current_version, $version_to_check) == -1; + } } diff --git a/lng/de.lng.php b/lng/de.lng.php index 993699d2..db4fd26d 100644 --- a/lng/de.lng.php +++ b/lng/de.lng.php @@ -2075,7 +2075,7 @@ Vielen Dank, Ihr Administrator', 'update' => 'Froxlor Aktualisierung', 'proceed' => 'Ausführen', 'update_information' => [ - 'part_a' => 'Die Froxlor-Dateien wurden aktualisiert. Neue Version ist %newversion. Die bisher installierte Version ist %curversion', + 'part_a' => 'Die Froxlor-Dateien wurden aktualisiert. Neue Version ist %s. Die bisher installierte Version ist %s', 'part_b' => '

Ein Kunden-Login ist vor Abschluss des Aktualisierungsvorganges nicht möglich.
Aktualisierung ausführen?', ], 'noupdatesavail' => 'Ihre Froxlor-Version ist aktuell.', diff --git a/lng/en.lng.php b/lng/en.lng.php index 3aee9789..3e606797 100644 --- a/lng/en.lng.php +++ b/lng/en.lng.php @@ -2433,7 +2433,7 @@ Yours sincerely, your administrator', 'update' => 'Froxlor update', 'proceed' => 'Proceed', 'update_information' => [ - 'part_a' => 'The Froxlor files have been updated to version %newversion. The installed version is %curversion.', + 'part_a' => 'The Froxlor files have been updated to version %s. The installed version is %s.', 'part_b' => '

Customers will not be able to log in until the update has been finished.
Proceed?', ], 'noupdatesavail' => 'You already have the latest Froxlor version.', diff --git a/lng/it.lng.php b/lng/it.lng.php index be4b1d24..7056f00e 100644 --- a/lng/it.lng.php +++ b/lng/it.lng.php @@ -1810,7 +1810,7 @@ Nota: Perfavore sii sicuro di usare lo stesso nome di file come per il cr 'update' => 'Aggiorna Froxlor', 'proceed' => 'Procedi', 'update_information' => [ - 'part_a' => 'I file di Froxlor sono stati aggiornati alla versione %newversion. La versione installata è %curversion.', + 'part_a' => 'I file di Froxlor sono stati aggiornati alla versione %s. La versione installata è %s.', 'part_b' => '

I clienti non potranno accedere fino a quando l\'aggiornamento non sarà completato.
Procedere?', ], 'noupdatesavail' => 'È già presente l\'ultima versione di Froxlor.', diff --git a/lng/nl.lng.php b/lng/nl.lng.php index 426b3d25..e82947ee 100644 --- a/lng/nl.lng.php +++ b/lng/nl.lng.php @@ -1150,7 +1150,7 @@ Met vriendelijke groet, uw beheerder', 'update' => 'Froxlor Update', 'proceed' => 'Verdergaan', 'update_information' => [ - 'part_a' => 'De bestanden van Froxlor zijn bijgewerkt naar versie %newversion. De geinstalleerde versie is %curversion.', + 'part_a' => 'De bestanden van Froxlor zijn bijgewerkt naar versie %s. De geinstalleerde versie is %s.', 'part_b' => '

Klanten kunnen niet inloggen totdat de update voltooid is.
Verdergaan?', ], 'noupdatesavail' => 'U gebruikt reeds de meest recente versie van Froxlor.',