From d9d1649adcdebb7ea5310199da50e9392811c034 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Thu, 26 Jun 2025 08:14:32 -0600 Subject: [PATCH] Update upgrade_menu.php Refactor the interactive command line menu. --- core/upgrade/upgrade_menu.php | 175 ++++++++++++++++++++++++++-------- 1 file changed, 135 insertions(+), 40 deletions(-) diff --git a/core/upgrade/upgrade_menu.php b/core/upgrade/upgrade_menu.php index b00b534064..9ab625b18b 100644 --- a/core/upgrade/upgrade_menu.php +++ b/core/upgrade/upgrade_menu.php @@ -18,7 +18,7 @@ The Initial Developer of the Original Code is Mark J Crane - Portions created by the Initial Developer are Copyright (C) 2008-2023 + Portions created by the Initial Developer are Copyright (C) 2008-2025 the Initial Developer. All Rights Reserved. Contributor(s): @@ -55,7 +55,7 @@ $text = $language->get($language_code, 'core/upgrade'); //output to text type instead of html $display_type = 'text'; -//run +//run the upgrade menu show_upgrade_menu(); /** @@ -64,31 +64,55 @@ show_upgrade_menu(); * @global type $software_name */ function show_upgrade_menu() { + //set the global variables global $text, $software_name, $settings; + + //debug information //error_reporting(E_ALL); - $line = str_repeat('-', strlen($text['title-cli_upgrade']) + 2); + + //build the options array + $options = [ + ['1', $text['label-upgrade_source'], $text['description-update_all_source_files']], + ['1a', $text['label-main_software'], $text['description-main_software']], + ['1b', $text['label-optional_applications'], $text['description-optional_applications']], + ['2', $text['label-database'], $text['description-upgrade_schema']], + ['2a', $text['label-schema'], $text['description-upgrade_schema']], + ['2b', $text['label-upgrade_data_types'], $text['description-upgrade_data_types']], + ['3', $text['label-upgrade_apps'], $text['description-upgrade_apps']], + ['4', $text['label-upgrade_menu'], $text['description-upgrade_menu']], + ['5', $text['label-upgrade_permissions'], $text['description-upgrade_permissions']], + ['6', $text['label-update_file_permissions'], $text['description-update_file_permissions']], + ['7', $text['label-upgrade_services'], $text['description-upgrade_services']], + ['8', $text['label-restart_services'], $text['description-restart_services']] + ]; + + $line = str_repeat('-', strlen($text['title-cli_upgrade']) + 8); + + //show the menu while (true) { + //show the upgrade title + echo $text['title-cli_upgrade'] . "\n"; + echo " ".$software_name . " " . show_software_version(); echo "\n"; - echo "+{$line}+\n"; - echo "| {$text['title-cli_upgrade']} |\n"; - echo "+{$line}+\n"; - echo "version: "; show_software_version(); + + //show the command menu + echo "Options:\n"; + foreach ($options as list($key, $label, $description)) { + if (!is_numeric($key)) { echo " "; } + echo " $key) $label" . (($option == 'h') ? " - " . $description : "") . "\n"; + } echo "\n"; - echo "1) {$text['label-upgrade_source']} - {$text['description-update_all_source_files']}\n"; - echo " 1a) " . $software_name . " - Update Main Software Only \n"; - echo " 1b) {$text['label-update_external_repositories']} - {$text['description-repositories']}\n"; - echo "2) {$text['label-schema']} - {$text['description-upgrade_schema']}\n"; - echo " 2b) {$text['label-upgrade_data_types']} - {$text['description-upgrade_data_types']}\n"; - echo "3) {$text['label-upgrade_apps']} - {$text['description-upgrade_apps']}\n"; - echo "4) {$text['label-upgrade_menu']} - {$text['description-upgrade_menu']}\n"; - echo "5) {$text['label-upgrade_permissions']} - {$text['description-upgrade_permissions']}\n"; - echo "6) {$text['label-update_filesystem_permissions']} - {$text['description-update_filesystem_permissions']}\n"; - echo "7) {$text['label-all_of_the_above']} - {$text['description-all_of_the_above']}\n"; - echo "0) Exit\n"; + echo " a) All\n"; + echo " h) Help\n"; + echo " q) Quit\n"; + + //prompt for user input echo "\n"; - echo "Choice: "; - $choice = readline(); - switch ($choice) { + echo "Enter an Option: "; + + //read the input and then call the correction actions + $option = readline(); + switch ($option) { case 1: do_upgrade_code(); do_upgrade_code_submodules(); @@ -103,6 +127,9 @@ function show_upgrade_menu() { do_upgrade_auto_loader(); break; case 2: + do_upgrade_schema(true); + break; + case '2a': do_upgrade_schema(); break; case '2b': @@ -122,23 +149,32 @@ function show_upgrade_menu() { break; case 6: do_upgrade_auto_loader(); - do_filesystem_permissions($text, $settings); + do_file_permissions($text, $settings); break; case 7: + do_upgrade_auto_loader(); + do_upgrade_services($text, $settings); + break; + case 8: + do_restart_services($text, $settings); + break; + case 'a': do_upgrade_code(); do_upgrade_auto_loader(); - do_upgrade_schema(); + do_upgrade_schema(true); do_upgrade_domains(); do_upgrade_menu(); do_upgrade_permissions(); - do_filesystem_permissions($text, $settings); + do_file_permissions($text, $settings); + do_upgrade_services($text, $settings); + do_restart_services($text, $settings); break; - case 9: - break; - case 0: case 'q': exit(); } + + //add a few line feeds + echo "\n\n"; } } @@ -148,9 +184,11 @@ function show_upgrade_menu() { */ function do_upgrade_auto_loader() { global $text, $autoload; + //remove temp files unlink(sys_get_temp_dir() . '/' . auto_loader::CLASSES_FILE); unlink(sys_get_temp_dir() . '/' . auto_loader::INTERFACES_FILE); + //create a new instance of the autoloader $autoload->update(); echo "{$text['message-updated_autoloader']}\n"; @@ -159,7 +197,7 @@ function do_upgrade_auto_loader() { /** * Update file system permissions */ -function do_filesystem_permissions($text, settings $settings) { +function do_file_permissions($text, settings $settings) { echo ($text['label-header1'] ?? "Root account or sudo account must be used for this option") . "\n"; echo ($text['label-header2'] ?? "This option is used for resetting the permissions on the filesystem after executing commands using the root user account") . "\n"; @@ -193,29 +231,27 @@ function do_filesystem_permissions($text, settings $settings) { $directories[] = sys_get_temp_dir() . '/' . auto_loader::CLASSES_FILE; //execute chown command for each directory foreach ($directories as $dir) { - if ($dir !== null) { - //notify user - echo "chown -R www-data:www-data $dir\n"; - //execute - exec("chown -R www-data:www-data $dir"); - } + //skip the empty directories + if (empty($dir)) { continue; } + + //notify user + echo "chown -R www-data:www-data $dir\n"; + + //execute + exec("chown -R www-data:www-data $dir"); } } else { echo ($text['label-not_running_as_root'] ?? "Not root user - operation skipped")."\n"; } } -function is_root_user(): bool { - return posix_getuid() === 0; -} - function current_user(): ?string { return posix_getpwuid(posix_getuid())['name'] ?? null; } //show the upgrade type -function show_software_version() { - echo software::version() . "\n"; +function show_software_version(): ?string { + return software::version() . "\n"; } /** @@ -357,6 +393,65 @@ function do_upgrade_defaults() { echo "\n"; } +/** + * Upgrade services + */ +function do_upgrade_services($text, settings $settings) { + echo ($text['description-upgrade_services'] ?? "")."\n"; + $core_files = glob(dirname(__DIR__, 2) . "/core/*/resources/service/*.service"); + $app_files = glob(dirname(__DIR__, 2) . "/app/*/resources/service/*.service"); + $service_files = array_merge($core_files, $app_files); + foreach($service_files as $file) { + $service_name = get_service_name($file); + echo " ".$service_name."\n"; + system("cp " . escapeshellarg($file) . " /etc/systemd/system/" . escapeshellarg($service_name) . ".service"); + system("systemctl daemon-reload"); + system("systemctl enable --now " . escapeshellarg($service_name)); + } +} + +/** + * Get the service name + * @param string $file + */ +function get_service_name(string $file) { + $parsed = parse_ini_file($file); + $exec_cmd = $parsed['ExecStart']; + $parts = explode(' ', $exec_cmd); + $php_file = $parts[1] ?? ''; + if (!empty($php_file)) { + $path_info = pathinfo($php_file); + return $path_info['filename']; + } + return ''; +} + + +/** + * Checks if the current user has root privileges. + * + * @return bool Returns true if the current user is the root user, false otherwise. + */ +function is_root_user(): bool { + return posix_getuid() === 0; +} + + +/** + * Restart services + */ +function do_restart_services($text, settings $settings) { + echo ($text['description-restart_services'] ?? "")."\n"; + $core_files = glob(dirname(__DIR__, 2) . "/core/*/resources/service/*.service"); + $app_files = glob(dirname(__DIR__, 2) . "/app/*/resources/service/*.service"); + $service_files = array_merge($core_files, $app_files); + foreach($service_files as $file) { + $service_name = get_service_name($file); + echo " ".$service_name."\n"; + system("systemctl restart ".$service_name); + } +} + /** * Load the old config.php file * @return type