From 868d07ae9674e6062ac01841e4cb613f559a4142 Mon Sep 17 00:00:00 2001 From: Faustin Date: Wed, 3 Aug 2022 20:01:13 +0200 Subject: [PATCH] New prop : select auth type with radio box --- htdocs/admin/mails.php | 81 +++++++++++++++------------ htdocs/core/class/CMailFile.class.php | 4 +- htdocs/core/class/smtps.class.php | 6 +- htdocs/langs/en_US/admin.lang | 8 ++- htdocs/langs/fr_FR/admin.lang | 6 +- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php index 31198471710..6b09c211414 100644 --- a/htdocs/admin/mails.php +++ b/htdocs/admin/mails.php @@ -91,10 +91,8 @@ if ($action == 'update' && !$cancel) { if (GETPOSTISSET("MAIN_MAIL_SMTPS_PW")) { dolibarr_set_const($db, "MAIN_MAIL_SMTPS_PW", GETPOST("MAIN_MAIL_SMTPS_PW", 'none'), 'chaine', 0, '', $conf->entity); } - if (GETPOSTISSET("MAIN_MAIL_SMTPS_USE_OAUTH")) { - dolibarr_set_const($db, "MAIN_MAIL_SMTPS_USE_OAUTH", GETPOST("MAIN_MAIL_SMTPS_USE_OAUTH", 'int'), 'chaine', 0, '', $conf->entity); - $method = GETPOST("MAIN_MAIL_SMTPS_USE_OAUTH", 'aZ09') == 1 ? 'XOAUTH2' : 'LOGIN'; - dolibarr_set_const($db, "MAIL_SMTP_AUTH_TYPE", $method, 'chaine', 0, '', $conf->entity); + if (GETPOSTISSET("MAIN_MAIL_SMTPS_AUTH_TYPE")) { + dolibarr_set_const($db, "MAIN_MAIL_SMTPS_AUTH_TYPE", GETPOST("MAIN_MAIL_SMTPS_AUTH_TYPE", 'chaine'), 'chaine', 0, '', $conf->entity); } if (GETPOSTISSET("MAIN_MAIL_SMTPS_OAUTH_SERVICE")) { dolibarr_set_const($db, "MAIN_MAIL_SMTPS_OAUTH_SERVICE", GETPOST("MAIN_MAIL_SMTPS_OAUTH_SERVICE", 'chaine'), 'chaine', 0, '', $conf->entity); @@ -222,8 +220,7 @@ if ($action == 'edit') { jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").prop("disabled", true); jQuery(".smtp_method").hide(); jQuery(".dkim").hide(); - jQuery(".smtp_oauth_service").hide(); - jQuery(".smtp_use_oauth").hide(); + jQuery(".smtp_auth_method").hide(); '; if ($linuxlike) { print ' @@ -266,8 +263,7 @@ if ($action == 'edit') { jQuery("#smtp_port_mess").hide(); jQuery(".smtp_method").show(); jQuery(".dkim").hide(); - jQuery(".smtp_oauth_service").show(); - jQuery(".smtp_use_oauth").show(); + jQuery(".smtp_auth_method").show(); } if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'swiftmailer\') { @@ -295,27 +291,30 @@ if ($action == 'edit') { jQuery("#smtp_port_mess").hide(); jQuery(".smtp_method").show(); jQuery(".dkim").show(); - jQuery(".smtp_oauth_service").hide(); - jQuery(".smtp_use_oauth").hide(); + jQuery(".smtp_auth_method").hide(); } } - function change_smtp_oauth_service() { - console.log(jQuery("#MAIN_MAIL_SMTPS_USE_OAUTH").val()); - if (jQuery("#MAIN_MAIL_SMTPS_USE_OAUTH").val() == 1) { + function change_smtp_auth_method() { + console.log(jQuery("#radio_pw").prop("checked")); + if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'smtps\' && jQuery("#radio_oauth").prop("checked")) { jQuery(".smtp_oauth_service").show(); jQuery(".smtp_pw").hide(); + } else if(jQuery("#MAIN_MAIL_SENDMODE").val()==\'mail\'){ + jQuery(".smtp_oauth_service").hide(); + jQuery(".smtp_pw").hide(); } else { jQuery(".smtp_oauth_service").hide(); jQuery(".smtp_pw").show(); } } initfields(); - change_smtp_oauth_service(); + change_smtp_auth_method(); jQuery("#MAIN_MAIL_SENDMODE").change(function() { initfields(); + change_smtp_auth_method(); }); - jQuery("#MAIN_MAIL_SMTPS_USE_OAUTH").change(function() { - change_smtp_oauth_service(); + jQuery("#radio_pw, #radio_oauth").change(function() { + change_smtp_auth_method(); }); jQuery("#MAIN_MAIL_EMAIL_TLS").change(function() { if (jQuery("#MAIN_MAIL_EMAIL_TLS").val() == 1) @@ -416,20 +415,6 @@ if ($action == 'edit') { } print ''; - // OAUTH - if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))) { - print ''.$form->textwithpicto($langs->trans("MAIN_MAIL_SMTPS_USE_OAUTH"), $langs->trans("OauthNotAvailableForAll")).''; - if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity)) { - print $form->selectyesno('MAIN_MAIL_SMTPS_USE_OAUTH', (!empty($conf->global->MAIN_MAIL_SMTPS_USE_OAUTH) ? $conf->global->MAIN_MAIL_SMTPS_USE_OAUTH : 0), 1); - } else { - $value = yn($conf->global->MAIN_MAIL_SMTPS_USE_OAUTH); - $htmltext = $langs->trans("ContactSuperAdminForChange"); - print $form->textwithpicto($text, $htmltext, 1, 'superadmin'); - print ''; - } - print ''; - } - // Port print ''; if (!$conf->use_javascript_ajax && $linuxlike && $conf->global->MAIN_MAIL_SENDMODE == 'mail') { @@ -474,6 +459,25 @@ if ($action == 'edit') { print ''; } + + // OAUTH + if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))) { + print ''.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").''; + if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity)) { + print 'global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'LOGIN' ? ' checked' : '').'> '; + print ''; + print '            '; + print 'global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'XOAUTH2' ? ' checked' : '').'> '; + print ''; + } else { + $value = $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE; + $htmltext = $langs->trans("ContactSuperAdminForChange"); + print $form->textwithpicto($langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE"), $htmltext, 1, 'superadmin'); + print ''; + } + print ''; + } + // PW if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))) { $mainsmtppw = (!empty($conf->global->MAIN_MAIL_SMTPS_PW) ? $conf->global->MAIN_MAIL_SMTPS_PW : ''); @@ -493,7 +497,7 @@ if ($action == 'edit') { // OAUTH service provider if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')))) { - print ''.$form->textwithpicto($langs->trans("MAIN_MAIL_SMTPS_OAUTH_SERVICE"), $langs->trans("DontForgetCreateTokenOauthMod")).''; + print ''.$langs->trans("MAIN_MAIL_SMTPS_OAUTH_SERVICE").''; // SuperAdministrator access only if ((empty($conf->global->MAIN_MODULE_MULTICOMPANY)) || ($user->admin && !$user->entity)) { @@ -680,10 +684,6 @@ if ($action == 'edit') { print ''.$langs->trans("MAIN_MAIL_SMTP_SERVER", ini_get('SMTP') ?ini_get('SMTP') : $langs->transnoentities("Undefined")).''.(!empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : '').''; } - // Use OAUTH - if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps'))) { - print ''.$langs->trans("MAIN_MAIL_SMTPS_USE_OAUTH").''.yn($conf->global->MAIN_MAIL_SMTPS_USE_OAUTH).''; - } // Port if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail')) { @@ -697,13 +697,20 @@ if ($action == 'edit') { print ''.$langs->trans("MAIN_MAIL_SMTPS_ID").''.$conf->global->MAIN_MAIL_SMTPS_ID.''; } + // AUTH method + if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps'))) { + $authtype = $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE; + $text = ($authtype === "LOGIN") ? $langs->trans("UsePassword") : ($authtype === "XOAUTH2" ? $langs->trans("UseOauth") : '') ; + print ''.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").''.$text.''; + } + // SMTPS PW - if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')) && (!(isset($conf->global->MAIL_SMTP_AUTH_TYPE) && $conf->global->MAIL_SMTP_AUTH_TYPE === "XOAUTH2"))) { + if (isset($conf->global->MAIN_MAIL_SENDMODE) && (in_array($conf->global->MAIN_MAIL_SENDMODE, array('swiftmailer')) || (in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')) && (isset($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE) && $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE === "LOGIN")))) { print ''.$langs->trans("MAIN_MAIL_SMTPS_PW").''.preg_replace('/./', '*', $conf->global->MAIN_MAIL_SMTPS_PW).''; } // SMTPS oauth service - if (isset($conf->global->MAIL_SMTP_AUTH_TYPE) && $conf->global->MAIL_SMTP_AUTH_TYPE === "XOAUTH2") { + if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')) && isset($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE) && $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE === "XOAUTH2") { $text = $oauthservices[$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE]; if (empty($text)) { $text = $langs->trans("Undefined").img_warning(); diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 24c27b47795..de2a44070fc 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -658,7 +658,7 @@ class CMailFile } $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER'; - $keyforsmtpuseoauth = "MAIN_MAIL_SMTPS_USE_OAUTH"; + $keyforsmtpauthtype = "MAIN_MAIL_SMTPS_AUTH_TYPE"; $keyforsmtpoauthservice = "MAIN_MAIL_SMTPS_OAUTH_SERVICE"; $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT'; $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID'; @@ -835,7 +835,7 @@ class CMailFile $this->smtps->setPW($loginpass); } - if (!empty($conf->global->$keyforsmtpuseoauth)) { + if (!empty($conf->global->$keyforsmtpauthtype) && $conf->global->$keyforsmtpauthtype === "XOAUTH2") { require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice; if (preg_match('/^.*-/', $keyforsupportedoauth2array)) { diff --git a/htdocs/core/class/smtps.class.php b/htdocs/core/class/smtps.class.php index 05248e89143..06ada5c4911 100644 --- a/htdocs/core/class/smtps.class.php +++ b/htdocs/core/class/smtps.class.php @@ -570,13 +570,13 @@ class SMTPs } // Default authentication method is LOGIN - if (empty($conf->global->MAIL_SMTP_AUTH_TYPE)) { - $conf->global->MAIL_SMTP_AUTH_TYPE = 'LOGIN'; + if (empty($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE)) { + $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE = 'LOGIN'; } // Send Authentication to Server // Check for errors along the way - switch ($conf->global->MAIL_SMTP_AUTH_TYPE) { + switch ($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE) { case 'NONE': // Do not send the 'AUTH type' message. For test purpose, if you don't need authentication, it is better to not enter login/pass into setup. $_retVal = true; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index e5b8fe597bc..d629bbc0fbf 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -2287,7 +2287,9 @@ NoName=No name ShowAdvancedOptions= Show advanced options HideAdvancedoptions= Hide advanced options CIDLookupURL=The module brings an URL that can be used by an external tool to get the name of a thirdparty or contact from its phone number. URL to use is: -MAIN_MAIL_SMTPS_USE_OAUTH=Use OAUTH2 authentication -OauthNotAvailableForAll=OAUTH2 authentication is not available for all hosts +OauthNotAvailableForAllAndHadToBeCreatedBefore=OAUTH2 authentication is not available for all hosts, and a token with the right permissions must have been created upstream with the OAUTH module MAIN_MAIL_SMTPS_OAUTH_SERVICE=OAUTH2 authentication service -DontForgetCreateTokenOauthMod=A token with the right permissions must have been created upstream with the OAUTH module \ No newline at end of file +DontForgetCreateTokenOauthMod=A token with the right permissions must have been created upstream with the OAUTH module +MAIN_MAIL_SMTPS_AUTH_TYPE=Authentification method +UsePassword=Use a password +UseOauth=Use a OAUTH token \ No newline at end of file diff --git a/htdocs/langs/fr_FR/admin.lang b/htdocs/langs/fr_FR/admin.lang index 6302377b449..42d973e8e06 100644 --- a/htdocs/langs/fr_FR/admin.lang +++ b/htdocs/langs/fr_FR/admin.lang @@ -2287,7 +2287,9 @@ ShowAdvancedOptions= Show advanced options HideAdvancedoptions= Hide advanced options CIDLookupURL=The module brings an URL that can be used by an external tool to get the name of a thirdparty or contact from its phone number. URL to use is: DoesNotWorkWithAllThemes=Ne fonctionne pas avec tous les thèmes -MAIN_MAIL_SMTPS_USE_OAUTH=Utilisation de l'authentification OAUTH2 -OauthNotAvailableForAll=L'authentification OAUTH2 n'est pas disponible pour tous les hôtes +OauthNotAvailableForAllAndHadToBeCreatedBefore=L'authentification OAUTH2 n'est pas disponible pour tous les hôtes, de plus, un jeton avec les bonnes permissions doit avoir été créé en amont avec le module OAUTH MAIN_MAIL_SMTPS_OAUTH_SERVICE=Service d'authentification OAUTH2 DontForgetCreateTokenOauthMod=Un jeton avec les bonnes permissions doit avoir été créé en amont avec le module OAUTH +MAIN_MAIL_SMTPS_AUTH_TYPE=Méthode d'authentication +UsePassword=Utiliser un mot de pass +UseOauth=Utiliser un jeton d'authentification OAUTH \ No newline at end of file