From 2755b2f04a6882f85ebc08f47678faec4fd5c589 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Thu, 26 Jun 2025 08:13:00 -0600 Subject: [PATCH] Update upgrade.php Add a command-line help menu --- core/upgrade/upgrade.php | 450 ++++++++++++++++++++++++++++++++------- 1 file changed, 371 insertions(+), 79 deletions(-) diff --git a/core/upgrade/upgrade.php b/core/upgrade/upgrade.php index 6869ef7f8f..41210d63b0 100644 --- a/core/upgrade/upgrade.php +++ b/core/upgrade/upgrade.php @@ -17,7 +17,7 @@ The Initial Developer of the Original Code is Mark J Crane - Portions created by the Initial Developer are Copyright (C) 2008-2024 + Portions created by the Initial Developer are Copyright (C) 2008-2025 the Initial Developer. All Rights Reserved. Contributor(s): @@ -136,13 +136,13 @@ //write the config file $file_handle = fopen($config_file,"w"); - if(!$file_handle){ return; } + if (!$file_handle){ return; } fwrite($file_handle, $conf); fclose($file_handle); } //check the permission - if(defined('STDIN')) { + if (defined('STDIN')) { $display_type = 'text'; //html, text } else { @@ -157,22 +157,19 @@ $display_type = 'html'; //html, text } -//set the default upgrade type - $upgrade_type = 'defaults'; +//set the default as an empty string + $upgrade_type = ''; //get the command line arguments - if(defined('STDIN')) { - //$application_name = $argv[0]; - if (isset($argv[1])) { + if (defined('STDIN')) { + if (!empty($argv[1])) { $upgrade_type = $argv[1]; } } -//check for the upgrade menu option first - if ($upgrade_type == 'menu') { - require __DIR__ . '/upgrade_menu.php'; - exit(); - } +//use upgrade language file + $language = new text; + $text = $language->get(null, 'core/upgrade'); //always update the now global autoload cache just-in-case the source files have updated $autoload->update(); @@ -182,100 +179,219 @@ $class::clear_cache(); } +//show the title + if ($display_type == 'text') { + //echo "\n"; + //echo $text['label-upgrade']."\n"; + echo "\n"; + } + +//show the help menu + if ($upgrade_type == 'help' or $upgrade_type == '-h' or $upgrade_type == '--help') { + + //send the help message to the console + echo "Usage:\n"; + echo " upgrade.php [-hvnostdmpgfui] [--menu [default|list]] [--services [update|restart]]\n"; + echo " A terminal based upgrade tool used to simplify or automate the upgrade.\n"; + echo "\n"; + echo "Options:\n"; + echo " -h --help Show this help message.\n"; + echo " -v --version Show the version.\n"; + echo " -n --main Update the main application.\n"; + echo " -o --optional Update the optional applications.\n"; + echo " -s --schemas Check the table and field structure.\n"; + echo " -t --types Updates field data types as needed.\n"; + echo " -d --defaults Restore application defaults.\n"; + echo " -m --menu [default|list] Restore menu default or show the menu list\n"; + echo " -p --permissions Restore default file and group permissions.\n"; + echo " -g --group Restore default group permissions.\n"; + echo " -f --file Update the file permissions.\n"; + echo " -u --service [update|restart] Update and restart services.\n"; + echo " -i --interactive Show the interactive menu.\n"; + echo "\n"; + + } + //get the version of the software - if ($upgrade_type == 'version') { + if ($upgrade_type == 'version' or $upgrade_type == '-v' or $upgrade_type == '--version') { echo software::version()."\n"; } -//run all app_defaults.php files - if ($upgrade_type == 'domains') { - $domain = new domains; - $domain->display_type = $display_type; - $domain->upgrade(); - } - //upgrade schema and/or data_types - if ($upgrade_type == 'schema') { + if ($upgrade_type == 'schema' or $upgrade_type == '-s' or $upgrade_type == '--schema') { + //send a message to the console + if ($display_type === 'text') { + echo "[ Update ] Table and field structure.\n"; + } + //get the database schema put it into an array then compare and update the database as needed. $obj = new schema; if (isset($argv[2]) && $argv[2] == 'data_types') { $obj->data_types = true; } - echo $obj->schema($format ?? ''); + $response = $obj->schema($format ?? ''); + if ($display_type === 'text') { + foreach(explode("\n", $response) as $row) { + echo " ".trim($row)."\n"; + } + } + } + +//upgrade schema and/or data_types + if ($upgrade_type == 'data_types' or $upgrade_type == '-t' or $upgrade_type == '--types') { + //send a message to the console + if ($display_type === 'text') { + echo "[ Update ] Table, field structure and data types.\n"; + } + + //get the database schema put it into an array then compare and update the database as needed. + $obj = new schema; + $obj->data_types = true; + $response = $obj->schema($format ?? ''); + if ($display_type === 'text') { + foreach(explode("\n", $response) as $row) { + echo " ".trim($row)."\n"; + } + } + } + +//run all app_defaults.php files + if ($upgrade_type == 'defaults' or $upgrade_type == '-d' or $upgrade_type == '--default') { + //send a message to the console + if ($display_type === 'text') { + echo "[ Update ] Restore application defaults.\n"; + } + + //upgrade application defaults + $domain = new domains; + $domain->display_type = $display_type; + $domain->upgrade(); } //restore the default menu - if ($upgrade_type == 'menus') { - //connect to the database - $database = new database; - + if ($upgrade_type == 'menu' or $upgrade_type == '-m' or $upgrade_type == '--menu') { //get the menu uuid and language - $sql = "select menu_uuid, menu_language from v_menus "; - $sql .= "where menu_name = :menu_name "; - $parameters['menu_name'] = 'default'; - $row = $database->select($sql, $parameters, 'row'); - if (is_array($row) && sizeof($row) != 0) { - $menu_uuid = $row["menu_uuid"]; - $menu_language = $row["menu_language"]; + $sql = "select menu_uuid, menu_name, menu_language "; + $sql .= "from v_menus "; + $menus = $database->select($sql, null, 'all'); + foreach ($menus as $row) { + if ($row == 'default') { + $menu_uuid = $row["menu_uuid"]; + $menu_language = $row["menu_language"]; + } } - unset($sql, $parameters, $row); + unset($sql, $row); //show the menu - if (isset($argv[2]) && $argv[2] == 'view') { - print_r($_SESSION["menu"]); + if (isset($argv[2]) && $argv[2] == 'list') { + echo "Menu List\n"; + foreach ($menus as $row) { + if (!empty($row) && sizeof($row) != 0) { + echo " ".$row["menu_name"]."\n"; + } + } + echo "\n"; } //set the menu back to default - if (!isset($argv[2]) || $argv[2] == 'default') { + if (empty($argv[2]) || $argv[2] == 'default') { + //send a message to the console + echo "[ Update ] Restore the default menu\n"; + //restore the menu $included = true; require_once("core/menu/menu_restore_default.php"); - unset($sel_menu); - - //use upgrade language file - $language = new text; - $text = $language->get(null, 'core/upgrade'); - - //send message to the console - echo $text['message-upgrade_menu']."\n"; + echo "\n"; } } //restore the default permissions - if ($upgrade_type == 'permissions') { - //default the groups in case they are missing - (new groups())->defaults(); + if ($upgrade_type == 'permissions' or $upgrade_type == '-p' or $upgrade_type == '--permissions') { + +if (empty($argv[2]) || $argv[2] == 'default') { + //send a message to the console + echo "[ Update ] Restore the file permissions\n"; + + //restore default file permissions. + update_file_permissions($text, $settings); + } + +if (empty($argv[2]) || $argv[2] == 'default') { + //send a message to the console + echo "[ Update ] Restore the group permissions\n"; + + //default the groups in case they are missing + $groups = new groups; + $groups->defaults(); + } //default the permissions $included = true; require_once("core/groups/permissions_default.php"); - //use upgrade language file - $language = new text; - $text = $language->get(null, 'core/upgrade'); + //add a line feed + echo "\n"; - //send message to the console - echo $text['message-upgrade_permissions']."\n"; + } + +//restore the file permissions + if ($upgrade_type == 'file' or $upgrade_type == '-f' or $upgrade_type == '--file') { + //send a message to the console + echo "[ Update ] Restore the file permissions\n"; + + //restore default file permissions. + update_file_permissions($text, $settings); + + //add a line feed + echo "\n"; + } + +//restore the group permissions + if ($upgrade_type == 'group' or $upgrade_type == '-g' or $upgrade_type == '--group') { + //send a message to the console + echo "[ Update ] Restore the group permissions\n"; + + //default the groups in case they are missing + $groups = new groups; + $groups->defaults(); + + //default the permissions + $included = true; + require_once("core/groups/permissions_default.php"); + + //add a line feed + echo "\n"; } //default upgrade schema and app defaults - if ($upgrade_type == 'defaults') { - //add multi-lingual support - $language = new text; - $text = $language->get(null, 'core/upgrade'); + if (empty($upgrade_type)) { - //show the title - if ($display_type == 'text') { - echo "\n"; - echo $text['label-upgrade']."\n"; - echo "-----------------------------------------\n"; - echo "\n"; - echo $text['label-database']."\n"; + //set the display type + if (defined('STDIN')) { + $display_type = 'text'; + } + else { + $display_type = 'html'; } - //make sure the database schema and installation have performed all necessary tasks + //send a message to the console + if ($display_type === 'text') { + echo "[ Update ] Table and field structure.\n"; + } + + //Update the table and field structure. $obj = new schema; - echo $obj->schema("text"); + $response = $obj->schema("text"); + if ($display_type === 'text') { + foreach(explode("\n", $response) as $row) { + echo " ".trim($row)."\n"; + } + } + + //send a message to the console + if ($display_type === 'text') { + echo "[ Update ] Application defaults.\n"; + } //run all app_defaults.php files $domain = new domains; @@ -312,22 +428,198 @@ } } -//upgrade optional apps - if ($upgrade_type == 'repos') { +//update main software source + if ($upgrade_type == 'services' or $upgrade_type == '-u' or $upgrade_type == '--service' or $upgrade_type == '--services') { - $app_list = git_find_repos($_SERVER["PROJECT_ROOT"]."/app"); + if (empty($argv[2]) || $argv[2] == 'update') { + //send a message to the console + echo "[ Update ] Update all default services\n"; - if (!is_array($app_list)) { - exit; + //add or update the services + upgrade_services($text, $settings); } - print_r($app_list);exit; - foreach ($app_list as $repo => $apps) { - $path = $repo; - $git_result = git_pull($path); - foreach ($git_result['message'] as $response_line) { - echo $repo . ": " . $response_line . "\n"; - } + + //send a message to the console + if (empty($argv[2]) || $argv[2] == 'restart') { + echo "[ Update ] Restart all services\n"; + + //restart the services + restart_services($text, $settings); } + } +//check for the upgrade menu option first + if ($upgrade_type == 'interactive' or $upgrade_type == '-i' or $upgrade_type == '--interactive') { + require __DIR__ . '/upgrade_menu.php'; + exit(); + } + + + +//update main software source + if ($upgrade_type == 'main' or $upgrade_type == '-n' or $upgrade_type == '--main') { + + //send a message to the console + echo "[ Update ] The main application\n"; + + $git_result = git_pull(dirname(__DIR__, 2)); + foreach ($git_result['message'] as $response_line) { + echo $repo . ": " . $response_line . "\n"; + } + + } + +//update optional applications + if ($upgrade_type == 'optional' or $upgrade_type == '-o' or $upgrade_type == '--optional') { + + //set the $text array as global + global $text; + + //get the list of updateable repos + $updateable_repos = git_find_repos(dirname(__DIR__, 2)."/app"); + + //send a message to the console + echo "[Update ] The optional applications\n"; + + //update the optional repos + $messages = []; + foreach ($updateable_repos as $repo => $row) { + + //set the application name + $application = $row[0]; + + // Set the project root + $project_root = dirname(__DIR__, 1); + + //show the response + echo " $application\n"; + + //pull the changes using git + $git_result = git_pull($repo); + + //set the response message array + if ($git_result['result']) { + $messages[$repo] = $text['message-optional_apps_upgrade_source_cli'] . (!empty($git_result['message']) && is_array($git_result['message']) ? ' - '.implode("\n", $git_result['message']) : ''); + } + else { + if (!empty($git_result['message']) && is_array($git_result['message'])) { + $message = "ERROR:\n" . implode("\n", $git_result['message']); + } else { + $message = $git_result['message']; + } + $messages[$repo] = $text['message-optional_apps_upgrade_source_failed_cli'] . " - " . $message; + } + } + foreach ($messages as $repo => $message) { + echo $repo.": ".$message."\n"; + } + + } + + +/** + * Update file system permissions + */ +function update_file_permissions($text, settings $settings) { + + if (is_root()) { + + $directories = []; + //get the fusionpbx folder + $project_root = dirname(__DIR__, 2); + //adjust the project root + $directories[] = $project_root; + //adjust the /etc/freeswitch + $directories[] = $settings->get('switch', 'conf', null); + $directories[] = $settings->get('switch', 'call_center', null); //normally in conf but can be different + $directories[] = $settings->get('switch', 'dialplan', null); //normally in conf but can be different + $directories[] = $settings->get('switch', 'directory', null); //normally in conf but can be different + $directories[] = $settings->get('switch', 'languages', null); //normally in conf but can be different + $directories[] = $settings->get('switch', 'sip_profiles', null); //normally in conf but can be different + //adjust the /usr/share/freeswitch/{scripts,sounds} + $directories[] = $settings->get('switch', 'scripts', null); + $directories[] = $settings->get('switch', 'sounds', null); + //adjust the /var/lib/freeswitch/{db,recordings,storage,voicemail} + $directories[] = $settings->get('switch', 'db', null); + $directories[] = $settings->get('switch', 'recordings', null); + $directories[] = $settings->get('switch', 'storage', null); + $directories[] = $settings->get('switch', 'voicemail', null); //normally included in storage but can be different + //only set the xml_cdr directory permissions + $log_directory = $settings->get('switch', 'log', null); + if ($log_directory !== null) { + $directories[] = $log_directory . '/xml_cdr'; + } + //update the auto_loader cache permissions file + $directories[] = sys_get_temp_dir() . '/' . auto_loader::CLASSES_FILE; + //execute chown command for each directory + foreach ($directories as $dir) { + //skip the empty directories + if (empty($dir)) { continue; } + + //execute + exec("chown -R www-data:www-data $dir"); + } + } else { + echo ($text['label-not_running_as_root'] ?? "Not root user - operation skipped")."\n"; + } +} + +/** + * Upgrade services + */ +function 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 = find_service_name($file); + echo " Name: ".$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)); + } +} + +/** + * Restart services + */ +function 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 = find_service_name($file); + echo " Name: ".$service_name."\n"; + system("systemctl restart ".$service_name); + } +} + +/** + * Get the service name + * @param string $file + */ +function find_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(): bool { + return posix_getuid() === 0; +} + ?>