mirror of
https://github.com/itflow-org/itflow
synced 2026-02-28 19:04:52 +00:00
Added Beta support for Microsoft IMAP OAUTH2 must use new mail parser for it to work cron/ticket_email_parser.php
This commit is contained in:
@@ -26,20 +26,91 @@ if (isset($_POST['edit_mail_imap_settings'])) {
|
||||
|
||||
validateCSRFToken($_POST['csrf_token']);
|
||||
|
||||
$config_imap_host = sanitizeInput($_POST['config_imap_host']);
|
||||
$config_imap_username = sanitizeInput($_POST['config_imap_username']);
|
||||
$config_imap_password = sanitizeInput($_POST['config_imap_password']);
|
||||
$config_imap_port = intval($_POST['config_imap_port']);
|
||||
$config_imap_encryption = sanitizeInput($_POST['config_imap_encryption']);
|
||||
// Provider ('' -> NULL allowed)
|
||||
$config_imap_provider = sanitizeInput($_POST['config_imap_provider']);
|
||||
$allowed_providers = ['standard_imap','google_oauth','microsoft_oauth'];
|
||||
if ($config_imap_provider !== '' && !in_array($config_imap_provider, $allowed_providers, true)) {
|
||||
$config_imap_provider = 'standard_imap'; // fallback
|
||||
}
|
||||
|
||||
mysqli_query($mysqli,"UPDATE settings SET config_imap_host = '$config_imap_host', config_imap_port = $config_imap_port, config_imap_encryption = '$config_imap_encryption', config_imap_username = '$config_imap_username', config_imap_password = '$config_imap_password' WHERE company_id = 1");
|
||||
// Standard IMAP fields (kept for all providers; OAuth still needs these endpoints)
|
||||
$config_imap_host = sanitizeInput($_POST['config_imap_host']);
|
||||
$config_imap_port = (int) sanitizeInput($_POST['config_imap_port']);
|
||||
$config_imap_encryption = sanitizeInput($_POST['config_imap_encryption']); // '', 'tls', 'ssl'
|
||||
$config_imap_username = sanitizeInput($_POST['config_imap_username']);
|
||||
$config_imap_password = sanitizeInput($_POST['config_imap_password']); // ignored if OAuth selected
|
||||
|
||||
logAction("Settings", "Edit", "$session_name edited IMAP mail settings");
|
||||
// Shared OAuth fields (may or may not be present in your form yet)
|
||||
$config_mail_oauth_client_id = sanitizeInput($_POST['config_mail_oauth_client_id']);
|
||||
$config_mail_oauth_client_secret = sanitizeInput($_POST['config_mail_oauth_client_secret']);
|
||||
$config_mail_oauth_tenant_id = sanitizeInput($_POST['config_mail_oauth_tenant_id']); // M365 only; harmless to keep when Google
|
||||
$config_mail_oauth_refresh_token = sanitizeInput($_POST['config_mail_oauth_refresh_token']);
|
||||
$config_mail_oauth_access_token = sanitizeInput($_POST['config_mail_oauth_access_token']); // optional manual paste
|
||||
$config_mail_oauth_access_token_expires_at = sanitizeInput($_POST['config_mail_oauth_access_token_expires_at']); // 'YYYY-mm-dd HH:ii:ss' optional
|
||||
|
||||
flash_alert("IMAP Mail Settings updated");
|
||||
// If provider is not OAuth, purge OAuth values on save
|
||||
$is_oauth = ($config_imap_provider === 'google_oauth' || $config_imap_provider === 'microsoft_oauth');
|
||||
|
||||
// Detect refresh token change to invalidate access token cache
|
||||
// (Relies on $config_mail_oauth_refresh_token loaded earlier with settings)
|
||||
$refresh_changed = false;
|
||||
if ($is_oauth) {
|
||||
$prev_refresh = isset($config_mail_oauth_refresh_token_current) ? $config_mail_oauth_refresh_token_current : ($config_mail_oauth_refresh_token ?? '');
|
||||
// If you already load settings into $config_mail_oauth_refresh_token, use that:
|
||||
if (isset($config_mail_oauth_refresh_token)) {
|
||||
$prev_refresh = $config_mail_oauth_refresh_token;
|
||||
}
|
||||
$refresh_changed = ($config_mail_oauth_refresh_token !== '' && $config_mail_oauth_refresh_token !== $prev_refresh)
|
||||
|| ($config_mail_oauth_refresh_token === '' && $prev_refresh !== '');
|
||||
}
|
||||
|
||||
// If OAuth refresh changed or provider just switched to non-OAuth, clear access token values
|
||||
if (!$is_oauth || $refresh_changed) {
|
||||
$config_mail_oauth_access_token = '';
|
||||
$config_mail_oauth_access_token_expires_at = '';
|
||||
}
|
||||
|
||||
// Helper for NULL / quoted values
|
||||
$q = fn($v) => ($v !== '' ? "'" . mysqli_real_escape_string($mysqli, $v) . "'" : "NULL");
|
||||
|
||||
// Build UPDATE with correct NULL handling
|
||||
$sql = "
|
||||
UPDATE settings SET
|
||||
config_imap_provider = " . ($config_imap_provider !== '' ? $q($config_imap_provider) : "NULL") . ",
|
||||
config_imap_host = " . $q($config_imap_host) . ",
|
||||
config_imap_port = " . (int)$config_imap_port . ",
|
||||
config_imap_encryption = " . $q($config_imap_encryption) . ",
|
||||
config_imap_username = " . $q($config_imap_username) . ",
|
||||
config_imap_password = " . ($is_oauth ? "NULL" : $q($config_imap_password)) . ",
|
||||
|
||||
-- Shared OAuth fields (kept even if provider is Google or Microsoft; NULL if not used)
|
||||
config_mail_oauth_client_id = " . ($is_oauth ? $q($config_mail_oauth_client_id) : "NULL") . ",
|
||||
config_mail_oauth_client_secret = " . ($is_oauth ? $q($config_mail_oauth_client_secret) : "NULL") . ",
|
||||
config_mail_oauth_tenant_id = " . ($is_oauth ? $q($config_mail_oauth_tenant_id) : "NULL") . ",
|
||||
config_mail_oauth_refresh_token = " . ($is_oauth ? $q($config_mail_oauth_refresh_token) : "NULL") . ",
|
||||
config_mail_oauth_access_token = " . ($is_oauth ? $q($config_mail_oauth_access_token) : "NULL") . ",
|
||||
config_mail_oauth_access_token_expires_at = " . ($is_oauth ? $q($config_mail_oauth_access_token_expires_at) : "NULL") . "
|
||||
WHERE company_id = 1
|
||||
";
|
||||
|
||||
mysqli_query($mysqli, $sql);
|
||||
|
||||
logAction("Settings", "Edit", "$session_name edited IMAP/OAuth mail settings");
|
||||
|
||||
// Friendly hint about what was saved
|
||||
if ($config_imap_provider === '') {
|
||||
flash_alert("IMAP monitoring disabled (provider not configured).");
|
||||
} elseif ($config_imap_provider === 'standard_imap') {
|
||||
flash_alert("IMAP settings updated (standard username/password).");
|
||||
} elseif ($config_imap_provider === 'google_oauth') {
|
||||
flash_alert("IMAP settings updated for Google Workspace (OAuth).");
|
||||
} elseif ($config_imap_provider === 'microsoft_oauth') {
|
||||
flash_alert("IMAP settings updated for Microsoft 365 (OAuth).");
|
||||
} else {
|
||||
flash_alert("IMAP settings updated.");
|
||||
}
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['edit_mail_from_settings'])) {
|
||||
|
||||
Reference in New Issue
Block a user