snappymail/plugins/override-smtp-credentials/index.php

104 lines
4 KiB
PHP
Raw Normal View History

2014-02-01 04:32:42 +08:00
<?php
class OverrideSmtpCredentialsPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'Override SMTP Credentials',
2023-01-19 17:55:01 +08:00
VERSION = '2.4',
RELEASE = '2023-01-19',
REQUIRED = '2.23.0',
CATEGORY = 'Filters',
DESCRIPTION = 'Override SMTP credentials for specific users.';
2020-08-31 00:04:54 +08:00
public function Init() : void
2014-02-01 04:32:42 +08:00
{
2023-01-19 17:55:01 +08:00
$this->addHook('smtp.before-connect', 'FilterSmtpConnect');
2022-04-13 15:46:58 +08:00
$this->addHook('smtp.before-login', 'FilterSmtpCredentials');
2014-02-01 04:32:42 +08:00
}
/**
2014-11-15 04:23:46 +08:00
* @param \RainLoop\Model\Account $oAccount
2022-04-13 15:46:58 +08:00
* @param \MailSo\Smtp\SmtpClient $oSmtpClient
* @param \MailSo\Smtp\Settings $oSettings
2014-02-01 04:32:42 +08:00
*/
2023-01-19 17:55:01 +08:00
public function FilterSmtpConnect(\RainLoop\Model\Account $oAccount, \MailSo\Smtp\SmtpClient $oSmtpClient, \MailSo\Smtp\Settings $oSettings)
2014-02-01 04:32:42 +08:00
{
$sEmail = $oAccount->Email();
$sWhiteList = \trim($this->Config()->Get('plugin', 'override_users', ''));
$sFoundValue = '';
2023-01-19 17:55:01 +08:00
if (\strlen($sWhiteList) && \RainLoop\Plugins\Helper::ValidateWildcardValues($sEmail, $sWhiteList, $sFoundValue)) {
\SnappyMail\LOG::debug('SMTP Override', "{$sEmail} matched {$sFoundValue}");
2023-01-19 17:55:01 +08:00
$oSettings->usePhpMail = false;
$sHost = \trim($this->Config()->Get('plugin', 'smtp_host', ''));
if (\strlen($sHost)) {
$oSettings->host = $sHost;
$oSettings->port = (int) $this->Config()->Get('plugin', 'smtp_port', 25);
$sSecure = \trim($this->Config()->Get('plugin', 'smtp_secure', 'None'));
switch ($sSecure)
{
case 'SSL':
$oSettings->type = MailSo\Net\Enumerations\ConnectionSecurityType::SSL;
break;
case 'TLS':
case 'STARTTLS':
$oSettings->type = MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS;
break;
case 'Detect':
$oSettings->type = MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT;
break;
default:
$oSettings->type = MailSo\Net\Enumerations\ConnectionSecurityType::NONE;
break;
}
2022-06-08 19:12:33 +08:00
}
2023-01-19 17:55:01 +08:00
} else {
\SnappyMail\LOG::debug('SMTP Override', "{$sEmail} no match");
}
}
2023-01-19 17:55:01 +08:00
/**
* @param \RainLoop\Model\Account $oAccount
* @param \MailSo\Smtp\SmtpClient $oSmtpClient
* @param \MailSo\Smtp\Settings $oSettings
*/
public function FilterSmtpCredentials(\RainLoop\Model\Account $oAccount, \MailSo\Smtp\SmtpClient $oSmtpClient, \MailSo\Smtp\Settings $oSettings)
{
$sWhiteList = \trim($this->Config()->Get('plugin', 'override_users', ''));
$sFoundValue = '';
if (\strlen($sWhiteList) && \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $sWhiteList, $sFoundValue)) {
$oSettings->useAuth = (bool) $this->Config()->Get('plugin', 'smtp_auth', true);
$oSettings->Login = \trim($this->Config()->Get('plugin', 'smtp_user', ''));
$oSettings->Password = (string) $this->Config()->Get('plugin', 'smtp_password', '');
}
2014-02-01 04:32:42 +08:00
}
/**
* @return array
*/
2020-08-31 00:04:54 +08:00
protected function configMapping() : array
2014-02-01 04:32:42 +08:00
{
return array(
\RainLoop\Plugins\Property::NewInstance('smtp_host')->SetLabel('SMTP Host')
->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('smtp_port')->SetLabel('SMTP Port')
->SetType(\RainLoop\Enumerations\PluginPropertyType::INT)
->SetDefaultValue(25),
\RainLoop\Plugins\Property::NewInstance('smtp_secure')->SetLabel('SMTP Secure')
->SetType(\RainLoop\Enumerations\PluginPropertyType::SELECTION)
2023-01-19 17:55:01 +08:00
->SetDefaultValue(array('None', 'Detect', 'SSL', 'STARTTLS')),
2014-02-01 04:32:42 +08:00
\RainLoop\Plugins\Property::NewInstance('smtp_auth')->SetLabel('Use auth')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(true),
\RainLoop\Plugins\Property::NewInstance('smtp_user')->SetLabel('SMTP User')
->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('smtp_password')->SetLabel('SMTP Password')
->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD)
->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('override_users')->SetLabel('Override users')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('space as delimiter, wildcard supported.')
->SetDefaultValue('user@example.com *@example2.com')
);
}
}