Add URI Tabs to mail settings keeps the tab active upon saving

This commit is contained in:
johnnyq
2026-07-26 01:52:25 -04:00
parent 5fc2f93b38
commit 53842a50d2
3 changed files with 68 additions and 32 deletions

View File

@@ -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;
<ul class="nav nav-tabs" id="mailTabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#tab-smtp" data-target="#tab-smtp">
<a class="nav-link <?php if ($active_tab === 'smtp') { echo 'active'; } ?>" href="?tab=smtp" data-target="#tab-smtp">
<i class="fas fa-fw fa-paper-plane mr-1"></i>Sending<?php echo renderMailStatusDot($smtp_on); ?>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tab-imap" data-target="#tab-imap">
<a class="nav-link <?php if ($active_tab === 'imap') { echo 'active'; } ?>" href="?tab=imap" data-target="#tab-imap">
<i class="fas fa-fw fa-inbox mr-1"></i>Receiving<?php echo renderMailStatusDot($imap_on); ?>
</a>
</li>
<li class="nav-item" id="tabitem-oauth" style="<?php echo $oauth_needed ? '' : 'display:none;'; ?>">
<a class="nav-link" href="#tab-oauth" data-target="#tab-oauth">
<a class="nav-link <?php if ($active_tab === 'oauth') { echo 'active'; } ?>" href="?tab=oauth" data-target="#tab-oauth">
<i class="fas fa-fw fa-key mr-1"></i>OAuth
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tab-from" data-target="#tab-from">
<a class="nav-link <?php if ($active_tab === 'from') { echo 'active'; } ?>" href="?tab=from" data-target="#tab-from">
<i class="fas fa-fw fa-at mr-1"></i>From Addresses
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tab-tests" data-target="#tab-tests">
<a class="nav-link <?php if ($active_tab === 'tests') { echo 'active'; } ?>" href="?tab=tests" data-target="#tab-tests">
<i class="fas fa-fw fa-vial mr-1"></i>Tests
</a>
</li>
@@ -92,9 +103,10 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
<div class="tab-content pt-4">
<!-- ============================ SENDING / SMTP ============================ -->
<div class="tab-pane fade show active" id="tab-smtp" role="tabpanel">
<div class="tab-pane fade <?php if ($active_tab === 'smtp') { echo 'show active'; } ?>" id="tab-smtp" role="tabpanel">
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="smtp">
<div class="form-group">
<label>SMTP Provider <small class="text-muted">— outbound</small></label>
@@ -172,9 +184,10 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
</div>
<!-- ============================ RECEIVING / IMAP ============================ -->
<div class="tab-pane fade" id="tab-imap" role="tabpanel">
<div class="tab-pane fade <?php if ($active_tab === 'imap') { echo 'show active'; } ?>" id="tab-imap" role="tabpanel">
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="imap">
<div class="form-group">
<label>IMAP Provider <small class="text-muted">— inbound ticket inbox</small></label>
@@ -252,9 +265,10 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
</div>
<!-- ============================ OAUTH ============================ -->
<div class="tab-pane fade" id="tab-oauth" role="tabpanel">
<div class="tab-pane fade <?php if ($active_tab === 'oauth') { echo 'show active'; } ?>" id="tab-oauth" role="tabpanel">
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="oauth">
<div class="alert alert-secondary" id="oauth_hint">
<i class="fas fa-fw fa-info-circle mr-2"></i>These credentials are shared by any Sending or Receiving provider set to Google / Microsoft OAuth.
@@ -318,9 +332,10 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
</div>
<!-- ============================ FROM ADDRESSES ============================ -->
<div class="tab-pane fade" id="tab-from" role="tabpanel">
<div class="tab-pane fade <?php if ($active_tab === 'from') { echo 'show active'; } ?>" id="tab-from" role="tabpanel">
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="from">
<p class="text-muted">Each From address must be allowed to send on behalf of the SMTP user.</p>
@@ -361,7 +376,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
</div>
<!-- ============================ TESTS ============================ -->
<div class="tab-pane fade" id="tab-tests" role="tabpanel">
<div class="tab-pane fade <?php if ($active_tab === 'tests') { echo 'show active'; } ?>" id="tab-tests" role="tabpanel">
<?php if (!$send_ready && !$imap_ready && !$oauth_has_required_fields) { ?>
<div class="alert alert-secondary mb-0">
@@ -374,6 +389,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
<h6 class="text-bold"><i class="fas fa-fw fa-paper-plane mr-2"></i>Send a Test Email</h6>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="tests">
<div class="input-group">
<select class="form-control select2" name="test_email" required>
<option value="">- Select a From address -</option>
@@ -396,6 +412,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
<h6 class="text-bold"><i class="fas fa-fw fa-plug mr-2"></i>Test IMAP Connection</h6>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="tests">
<button type="submit" name="test_email_imap" class="btn btn-success"><i class="fas fa-fw fa-inbox mr-2"></i>Test IMAP</button>
</form>
</div>
@@ -406,6 +423,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
<h6 class="text-bold"><i class="fas fa-fw fa-sync-alt mr-2"></i>Test OAuth Token Refresh</h6>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="tab" value="tests">
<input type="hidden" name="oauth_provider" value="<?php echo htmlspecialchars($oauth_provider_for_test); ?>">
<p class="text-muted mb-2">Validates the refresh token and stores a new access token for <?php echo $oauth_provider_for_test === 'microsoft_oauth' ? 'Microsoft 365' : 'Google Workspace'; ?>.</p>
<button type="submit" name="test_oauth_token_refresh" class="btn btn-success"><i class="fas fa-fw fa-sync-alt mr-2"></i>Test OAuth Token Refresh</button>
@@ -434,11 +452,22 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
});
// ---- Self-contained tab controller (no dependency on the BS tab plugin) ----
// Set when the page was opened directly on the OAuth tab - stops the provider pass hiding it
const forcedOauthTab = <?php echo $active_tab === 'oauth' ? 'true' : 'false'; ?>;
const navLinks = Array.from(document.querySelectorAll('#mailTabs .nav-link'));
const panes = ['tab-smtp', 'tab-imap', 'tab-oauth', 'tab-from', 'tab-tests']
.map(id => document.getElementById(id)).filter(Boolean);
// Server rendered the initial tab; keep the URL honest as the user clicks around
function syncTabUrl(target) {
const tab = target.replace('#tab-', '');
const url = new URL(window.location.href);
url.searchParams.set('tab', tab);
history.replaceState(null, '', url);
}
function activateTab(target) {
syncTabUrl(target);
navLinks.forEach(l => l.classList.toggle('active', l.getAttribute('data-target') === target));
panes.forEach(p => {
const on = ('#' + p.id) === target;
@@ -508,7 +537,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
const anyOauth = isOauth(sv) || isOauth(iv);
const anyMs = sv === 'microsoft_oauth' || iv === 'microsoft_oauth';
show(oauthTabItem, anyOauth);
show(oauthTabItem, anyOauth || forcedOauthTab);
toggle(tenantRow, anyMs);
toggle(msConnect, anyMs);
if (oauthClientId) oauthClientId.placeholder = anyMs
@@ -520,7 +549,7 @@ $imap_ready = $imap_standard_ready || $imap_oauth_ready;
: anyOauth ? '<i class="fas fa-fw fa-info-circle mr-2"></i>Google Workspace: Client ID / Secret from Google Cloud; refresh token obtained via the consent flow.'
: '<i class="fas fa-fw fa-info-circle mr-2"></i>These credentials are shared by any Sending or Receiving provider set to Google / Microsoft OAuth.';
if (!anyOauth) {
if (!anyOauth && !forcedOauthTab) {
const oauthLink = document.querySelector('#mailTabs .nav-link[data-target="#tab-oauth"]');
if (oauthLink && oauthLink.classList.contains('active')) activateTab('#tab-smtp');
}