From 53842a50d21f3e0a2a29156edbd7d0856ec37eab Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sun, 26 Jul 2026 01:52:25 -0400 Subject: [PATCH] Add URI Tabs to mail settings keeps the tab active upon saving --- admin/oauth_microsoft_mail_callback.php | 2 +- admin/post/settings_mail.php | 45 ++++++++++++--------- admin/settings_mail.php | 53 +++++++++++++++++++------ 3 files changed, 68 insertions(+), 32 deletions(-) diff --git a/admin/oauth_microsoft_mail_callback.php b/admin/oauth_microsoft_mail_callback.php index 691f8e7d..85ce5f26 100644 --- a/admin/oauth_microsoft_mail_callback.php +++ b/admin/oauth_microsoft_mail_callback.php @@ -4,7 +4,7 @@ require_once "../config.php"; require_once "../functions.php"; require_once "../includes/check_login.php"; -$settings_mail_path = '/admin/settings_mail.php'; +$settings_mail_path = '/admin/settings_mail.php?tab=oauth'; if (!isset($session_is_admin) || !$session_is_admin) { flashAlert("Admin access required.", 'error'); diff --git a/admin/post/settings_mail.php b/admin/post/settings_mail.php index 17e7b651..237b3ad4 100644 --- a/admin/post/settings_mail.php +++ b/admin/post/settings_mail.php @@ -2,6 +2,13 @@ defined('FROM_POST_HANDLER') || die("Direct file access is not allowed"); +// Saves and tests return to the tab they were submitted from (see settings_mail.php) +$mail_tabs = ['smtp', 'imap', 'oauth', 'from', 'tests']; +$mail_tab_redirect = 'settings_mail.php'; +if (isset($_POST['tab']) && in_array($_POST['tab'], $mail_tabs, true)) { + $mail_tab_redirect .= '?tab=' . $_POST['tab']; +} + if (!defined('MICROSOFT_OAUTH_BASE_URL')) { define('MICROSOFT_OAUTH_BASE_URL', 'https://login.microsoftonline.com/'); } @@ -30,12 +37,12 @@ if (isset($_POST['oauth_connect_microsoft_mail'])) { // the provider dropdowns live in different forms and are never posted here if ($config_imap_provider !== 'microsoft_oauth' && $config_smtp_provider !== 'microsoft_oauth') { flashAlert("Please set the SMTP or IMAP Provider to Microsoft 365 (OAuth) and save it before connecting.", 'error'); - redirect(); + redirect($mail_tab_redirect); } if (empty($config_mail_oauth_client_id) || empty($config_mail_oauth_client_secret) || empty($config_mail_oauth_tenant_id)) { flashAlert("Missing Microsoft OAuth settings. Please provide Client ID, Client Secret, and Tenant ID first.", 'error'); - redirect(); + redirect($mail_tab_redirect); } if (defined('BASE_URL') && !empty(BASE_URL)) { @@ -99,7 +106,7 @@ if (isset($_POST['edit_mail_smtp_settings'])) { flashAlert("SMTP Mail Settings updated"); - redirect(); + redirect($mail_tab_redirect); } @@ -129,7 +136,7 @@ if (isset($_POST['edit_mail_imap_settings'])) { flashAlert("IMAP Mail Settings updated"); - redirect(); + redirect($mail_tab_redirect); } @@ -154,7 +161,7 @@ if (isset($_POST['edit_mail_oauth_settings'])) { logAudit("Settings", "Edit", "$session_name edited mail OAuth settings"); flashAlert("Mail OAuth Settings updated"); - redirect(); + redirect($mail_tab_redirect); } if (isset($_POST['edit_mail_from_settings'])) { @@ -179,7 +186,7 @@ if (isset($_POST['edit_mail_from_settings'])) { flashAlert("Mail From Settings updated"); - redirect(); + redirect($mail_tab_redirect); } @@ -226,7 +233,7 @@ if (isset($_POST['test_email_smtp'])) { flashAlert("Failed to add test mail to queue", 'error'); } - redirect(); + redirect($mail_tab_redirect); } @@ -276,7 +283,7 @@ if (isset($_POST['test_email_imap'])) { if (empty($host) || empty($port) || empty($username)) { flashAlert("IMAP connection failed: Missing host, port, or username.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $token_is_expired = function (?string $expires_at): bool { @@ -320,7 +327,7 @@ if (isset($_POST['test_email_imap'])) { } else { if (empty($config_mail_oauth_client_id) || empty($config_mail_oauth_client_secret) || empty($config_mail_oauth_refresh_token)) { flashAlert("IMAP OAuth failed: Missing OAuth client credentials or refresh token.", 'error'); - redirect(); + redirect($mail_tab_redirect); } if ($provider === 'google_oauth') { @@ -333,7 +340,7 @@ if (isset($_POST['test_email_imap'])) { } else { if (empty($config_mail_oauth_tenant_id)) { flashAlert("IMAP OAuth failed: Microsoft tenant ID is required.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $token_url = MICROSOFT_OAUTH_BASE_URL . rawurlencode($config_mail_oauth_tenant_id) . "/oauth2/v2.0/token"; @@ -347,13 +354,13 @@ if (isset($_POST['test_email_imap'])) { if (!$response['ok']) { flashAlert("IMAP OAuth failed: Could not refresh access token.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $json = json_decode($response['body'], true); if (!is_array($json) || empty($json['access_token'])) { flashAlert("IMAP OAuth failed: Token response did not include an access token.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $password = $json['access_token']; @@ -405,7 +412,7 @@ if (isset($_POST['test_email_imap'])) { flashAlert("IMAP connection failed. Check the host, port, encryption, and credentials.", 'error'); } - redirect(); + redirect($mail_tab_redirect); } @@ -417,7 +424,7 @@ if (isset($_POST['test_oauth_token_refresh'])) { if ($provider !== 'google_oauth' && $provider !== 'microsoft_oauth') { flashAlert("OAuth token test failed: unsupported provider.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $oauth_client_id = escapeSql($config_mail_oauth_client_id ?? ''); @@ -427,12 +434,12 @@ if (isset($_POST['test_oauth_token_refresh'])) { if (empty($oauth_client_id) || empty($oauth_client_secret) || empty($oauth_refresh_token)) { flashAlert("OAuth token test failed: missing client ID, client secret, or refresh token.", 'error'); - redirect(); + redirect($mail_tab_redirect); } if ($provider === 'microsoft_oauth' && empty($oauth_tenant_id)) { flashAlert("OAuth token test failed: Microsoft tenant ID is required.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $token_url = 'https://oauth2.googleapis.com/token'; @@ -461,14 +468,14 @@ if (isset($_POST['test_oauth_token_refresh'])) { if ($raw_body === false || $http_code < 200 || $http_code >= 300) { $err_msg = !empty($curl_err) ? $curl_err : "HTTP $http_code"; flashAlert("OAuth token test failed: $err_msg", 'error'); - redirect(); + redirect($mail_tab_redirect); } $json = json_decode($raw_body, true); if (!is_array($json) || empty($json['access_token'])) { flashAlert("OAuth token test failed: access token missing in provider response.", 'error'); - redirect(); + redirect($mail_tab_redirect); } $new_access_token = escapeSql($json['access_token']); @@ -490,5 +497,5 @@ if (isset($_POST['test_oauth_token_refresh'])) { logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for $provider_label mail settings"); flashAlert("OAuth token refresh successful for $provider_label. Access token expires at $new_expires_at."); - redirect(); + redirect($mail_tab_redirect); } diff --git a/admin/settings_mail.php b/admin/settings_mail.php index d36d057d..05d521f5 100644 --- a/admin/settings_mail.php +++ b/admin/settings_mail.php @@ -13,6 +13,17 @@ $imap_on = !empty($config_imap_provider); $oauth_needed = in_array($config_smtp_provider, ['google_oauth', 'microsoft_oauth'], true) || in_array($config_imap_provider, ['google_oauth', 'microsoft_oauth'], true); +// ---- Active tab ------------------------------------------------------------- +// The tab lives in the URL (?tab=imap) so it can be linked, bookmarked, survives a +// reload, and lets the POST handlers send you back to the tab you saved from +$mail_tabs = ['smtp', 'imap', 'oauth', 'from', 'tests']; +$active_tab = isset($_GET['tab']) && in_array($_GET['tab'], $mail_tabs, true) ? $_GET['tab'] : 'smtp'; + +// A direct link to the OAuth tab reveals it even when no OAuth provider is selected yet +if ($active_tab === 'oauth') { + $oauth_needed = true; +} + // ---- OAuth callback URI (for Entra App Registration) ------------------------ if (defined('BASE_URL') && !empty(BASE_URL)) { $mail_oauth_callback_uri = rtrim((string) BASE_URL, '/') . '/admin/oauth_microsoft_mail_callback.php'; @@ -63,27 +74,27 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;