Portions created by the Initial Developer are Copyright (C) 2008-2024 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ //set a timeout set_time_limit(15*60); //15 minutes //includes files require_once dirname(__DIR__, 2) . "/resources/require.php"; require_once "resources/check_auth.php"; //check the permission if ( !permission_exists('upgrade_source') && !permission_exists('upgrade_schema') && !permission_exists('upgrade_apps') && !permission_exists('menu_restore') && !permission_exists('group_edit') ) { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //connect to the database $database = database::new(); //set a default message_timeout $message_timeout = 4*1000; //find optional apps with repos $updateable_repos = git_find_repos($_SERVER["PROJECT_ROOT"]."/app"); if (!empty($updateable_repos) && is_array($updateable_repos) && @sizeof($updateable_repos) != 0) { foreach ($updateable_repos as $app_path => $repo) { $x = 0; include $app_path.'/app_config.php'; $updateable_repos[$app_path]['app'] = $repo[0]; $updateable_repos[$app_path]['name'] = $apps[$x]['name']; $updateable_repos[$app_path]['uuid'] = $apps[$x]['uuid']; $updateable_repos[$app_path]['version'] = $apps[$x]['version']; $updateable_repos[$app_path]['description'] = $apps[$x]['description'][$_SESSION['domain']['language']['code']]; unset($apps, $updateable_repos[$app_path][0]); } } //count upgradeable repos including + main repo $repos_count = 0; if (is_array($updateable_repos)) { $repos_count = @sizeof($updateable_repos); } $repos_count++; //process the http post if (!empty($_POST) && @sizeof($_POST) > 0) { //get the action options: source, schema, app_defaults, menu_defaults, permissions $action = $_POST['action'] ?? null; //run source update if (!empty($action["upgrade_source"]) && permission_exists("upgrade_source") && !is_dir("/usr/share/examples/fusionpbx")) { $project_update_status = git_pull($_SERVER["PROJECT_ROOT"]); $_SESSION["response"]["upgrade_source"] = $project_update_status['message']; if (!empty($project_update_status['result'])) { message::add($text['message-upgrade_source'], null, $message_timeout); } else { message::add($text['message-upgrade_source_failed'], 'negative', $message_timeout); } global $autoload; if ($autoload !== null && $autoload instanceof auto_loader) { $autoload->reload_classes(); $autoload->update_cache(); } } //run optional app source updates if (!empty($action["optional_apps"]) && permission_exists("upgrade_source")) { $apps_updated = $apps_failed = 0; if (is_array($action["optional_apps"])) { foreach ($updateable_repos as $app_path => $app_details) { if (array_search(basename($app_path), $action["optional_apps"]) !== false) { $git_result = git_pull($app_path); if (!empty($git_result['result'])) { $apps_updated++; } else { $apps_failed++; } $_SESSION["response"]["optional_apps"][$app_details['name']] = $git_result['message']; } } } if ($apps_updated != 0) { message::add($text['message-optional_apps_upgrade_source'], null, $message_timeout); } if ($apps_failed != 0) { message::add($text['message-optional_apps_upgrade_source_failed'], 'negative', $message_timeout); } //update the auto_loader cache just-in-case the source files have updated global $autoload; if ($autoload !== null && $autoload instanceof auto_loader) { $autoload->reload_classes(); $autoload->update_cache(); } } //load an array of the database schema and compare it with the active database if (!empty($action["upgrade_schema"]) && permission_exists("upgrade_schema")) { $obj = new schema(); if (isset($action["data_types"]) && $action["data_types"] == 'true') { $obj->data_types = true; } $_SESSION["response"]["schema"] = $obj->schema("html"); message::add($text['message-upgrade_schema'], null, $message_timeout); } //process the apps defaults if (!empty($action["app_defaults"]) && permission_exists("upgrade_apps")) { //update the auto_loader cache just-in-case the source files have updated global $autoload; if ($autoload !== null && $autoload instanceof auto_loader) { $autoload->reload_classes(); $autoload->update_cache(); } $domain = new domains; $domain->upgrade(); message::add($text['message-upgrade_apps'], null, $message_timeout); } //restore defaults of the selected menu if (!empty($action["menu_defaults"]) && permission_exists("menu_restore")) { global $autoload; if ($autoload !== null && $autoload instanceof auto_loader) { $autoload->reload_classes(); $autoload->update_cache(); } $sel_menu = explode('|', check_str($_POST["sel_menu"])); $menu_uuid = $sel_menu[0]; $menu_language = $sel_menu[1]; $included = true; require_once("core/menu/menu_restore_default.php"); unset($sel_menu); $text = $language->get(null, '/core/upgrade'); message::add($text['message-upgrade_menu'], null, $message_timeout); } //restore default permissions if (!empty($action["permission_defaults"]) && permission_exists("group_edit")) { global $autoload; if ($autoload !== null && $autoload instanceof auto_loader) { $autoload->reload_classes(); $autoload->update_cache(); } $included = true; require_once("core/groups/permissions_default.php"); $text = $language->get(null, '/core/upgrade'); message::add($text['message-upgrade_permissions'], null, $message_timeout); } //redirect the browser header("Location: ".PROJECT_PATH."/core/upgrade/index.php"); exit; } //process the http get (source preview) if (!empty($_GET['preview'])) { if (!empty($updateable_repos)) { foreach ($updateable_repos as $app_path => $app) { $repo_info = git_repo_info($app_path); if (empty($repo_info)) { continue; } $source_code[$app['app']] = $app_path; } } if ($_GET['preview'] == 'core') { $command = 'cd '.$_SERVER['PROJECT_ROOT'].' && git fetch && git diff --name-only @ @{u}'; } else if (array_key_exists($_GET['preview'], $source_code)) { $command = 'cd '.$source_code[$_GET['preview']].' && git fetch && git diff --name-only @ @{u}'; } if (!empty($command)) { $response = explode(PHP_EOL, shell_exec($command)); // simplify response if (!empty($response)) { foreach ($response as $l => $line) { if (empty($line)) { unset($response[$l]); } if (substr($line, 0, 8) == 'remote: ') { unset($response[$l]); } if (substr($line, 0, 8) == 'remote: ') { unset($response[$l]); } if (substr($line, 0, 19) == 'Unpacking objects: ') { unset($response[$l]); } if (substr($line, 0, 5) == 'From ') { unset($response[$l]); } if (substr($line, 0, 3) == ' ') { unset($response[$l]); } } } echo "\n"; echo "
".$text['header-source_code_upgrade_preview']."
\n"; echo "

\n"; if (!empty($response) && is_array($response)) { echo str_replace('APP_NAME', (!empty($_GET['title']) ? "".$_GET['title']."" : null), $text['description-source_code_changes_found']); echo "


\n"; echo "
\n"; if (!empty($response) && is_array($response)) { echo implode("
\n
\n", $response); } echo "
\n"; } else { echo str_replace('APP_NAME', (!empty($_GET['title']) ? "".$_GET['title']."" : null), $text['description-source_code_no_changes_found']); } echo "

\n"; echo "
\n"; echo " \n"; echo "
\n"; exit; } } //adjust color and initialize step counter $step = 1; $step_color = isset($_SESSION['theme']['upgrade_step_color']['text']) ? $_SESSION['theme']['upgrade_step_color']['text'] : color_adjust((!empty($_SESSION['theme']['form_table_label_background_color']['text']) ? $_SESSION['theme']['form_table_label_background_color']['text'] : '#e5e9f0'), -0.1); $step_container_style = "width: 30px; height: 30px; border: 2px solid ".$step_color."; border-radius: 50%; float: left; text-align: center; vertical-align: middle;"; $step_number_style = "font-size: 150%; font-weight: 600; color: ".$step_color.";"; //include the header and set the title $document['title'] = $text['title-upgrade']; require_once "resources/header.php"; //source preview layer echo "\n"; //show the content echo "
\n"; echo "
\n"; echo "
".$text['header-upgrade']."
\n"; echo "
\n"; echo button::create(['type'=>'submit','label'=>$text['button-upgrade_execute'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','collapse'=>'never']); echo "
\n"; echo "
\n"; echo "
\n"; echo $text['description-upgrade']; echo "

"; echo "
\n"; if (permission_exists("upgrade_source") && !is_dir("/usr/share/examples/fusionpbx") && is_writeable($_SERVER["PROJECT_ROOT"]."/.git")) { echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; echo "
\n"; echo "
".$step."
"; echo "
".$text['label-upgrade_source']."
\n"; echo "
\n"; echo "
\n"; echo " \n"; echo "
\n"; echo "
\n"; echo "   ".$text['description-update_all_source_files']." (".$repos_count.")\n"; echo "
\n"; echo "
\n"; echo "\n"; $step++; } if (permission_exists("upgrade_schema")) { echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; echo "
\n"; echo "
".$step."
"; echo "
".$text['label-upgrade_schema']."
\n"; echo "
\n"; echo "  ".$text['description-upgrade_schema']."\n"; echo "
\n"; echo "\n"; $step++; } if (permission_exists("upgrade_apps")) { echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; echo "
\n"; echo "
".$step."
"; echo "
".$text['label-upgrade_apps']."
\n"; echo "
\n"; echo "  ".$text['description-upgrade_apps']."\n"; echo "
\n"; $step++; } if (permission_exists("menu_restore")) { echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; echo "
\n"; echo "
".$step."
"; echo "
".$text['label-upgrade_menu']."
\n"; echo "
\n"; echo ""; echo ""; echo "  ".$text['description-upgrade_menu']; echo "
\n"; $step++; } if (permission_exists("group_edit")) { echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo "\n"; echo "
\n"; echo "
".$step."
"; echo "
".$text['label-upgrade_permissions']."
\n"; echo "
\n"; echo "  ".$text['description-upgrade_permissions']."\n"; echo "
\n"; $step++; } echo "
\n"; echo "
\n"; echo "
"; if (!empty($_SESSION["response"]) && is_array($_SESSION["response"])) { foreach($_SESSION["response"] as $part => $response){ echo "
\n"; echo "".$text["label-results"]." - ".$text["label-{$part}"]; echo "

"; $error_found = false; if ($part == "optional_apps") { foreach ($response as $app_name => $app_response) { echo "".$app_name."
\n"; $error_found = false; foreach ($app_response as $l => $response_line) { if (substr_count($response_line, 'error: ') != 0) { $error_found = true; $app_response[$l] = str_replace('error:', 'Error:', $response_line); } } if ($error_found) { $error_style = 'color: red;'; } echo "\n"; foreach ($app_response as $response_line) { echo htmlspecialchars($response_line) . "\n"; } echo "\n"; unset($error_found, $error_style); } } else if (is_array($response)) { foreach ($response as $l => $response_line) { if (substr_count($response_line, 'error: ') != 0) { $error_found = true; $response[$l] = str_replace('error:', 'Error:', $response_line); } } if ($error_found) { $error_style = 'color: red;'; } echo "\n"; echo implode("\n", $response); echo ""; unset($error_found, $error_style); } else { echo $response; } echo "
\n"; } unset($_SESSION["response"]); } //source preview script echo "\n"; //include the footer require_once "resources/footer.php"; ?>