Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
@@ -0,0 +1,203 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\SystemMailer\Service;
use Exception;
use Xentral\Components\Database\Database;
use Xentral\Components\Database\SqlQuery\SelectQuery;
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount;
use Xentral\Modules\SystemMailer\Exception\EmailBackupAccountException;
final class EmailAccountGateway
{
/** @var Database $db */
private $db;
/**
* @param Database $database
*/
public function __construct(Database $database)
{
$this->db = $database;
}
/**
* @param int $id
*
* @throws EmailBackupAccountException
*
* @return EmailBackupAccount
*/
public function getEmailAccountById(int $id): EmailBackupAccount
{
$query = $this->getBaseQuery()->where('id = :id');
$sql = $query->getStatement();
$row = $this->db->fetchRow($sql, ['id' => $id]);
if (empty($row)) {
throw new EmailBackupAccountException('Account not found.');
}
return EmailBackupAccount::fromDbState($row);
}
/**
* @param int $userId
*
* @throws EmailBackupAccountException
*
* @return EmailBackupAccount
*/
public function getAccountByUser(int $userId): EmailBackupAccount
{
$query = $this->getBaseQuery();
try {
$query->join('', 'user AS u', 'u.adresse = e.adresse AND u.adresse != 0');
} catch (Exception $e) {
throw new EmailBackupAccountException($e->getMessage(), $e->getCode(), $e);
}
$query->where('u.activ = 1')
->where('u.id = :user_id');
$sql = $query->getStatement();
$values = ['user_id' => $userId];
$row = $this->db->fetchRow($sql, $values);
if (empty($row)) {
throw new EmailBackupAccountException(sprintf('No email account found for user "%s"', $userId));
}
return EmailBackupAccount::fromDbState($row);
}
/**
* @param int $addressId
*
* @throws EmailBackupAccountException
*
* @return EmailBackupAccount
*/
public function getAccountByAddress(int $addressId): EmailBackupAccount
{
$query = $this->getBaseQuery()->where('e.adresse = :address_id');
$row = $this->db->fetchRow($query, ['address_id' => $addressId]);
if (empty($row)) {
throw new EmailBackupAccountException(sprintf('No email account found for address "%s"', $addressId));
}
return EmailBackupAccount::fromDbState($row);
}
/**
* @param string $email
*
* @throws EmailBackupAccountException
*
* @return EmailBackupAccount
*/
public function getAccountByEmail(string $email): EmailBackupAccount
{
$query = $this->getBaseQuery()->where('email LIKE :email')->orWhere('smtp_frommail LIKE :email');
$sql = $query->getStatement();
$row = $this->db->fetchRow($sql, ['email' => $email]);
if (empty($row)) {
throw new EmailBackupAccountException(sprintf('No email account found for %s', $email));
}
return EmailBackupAccount::fromDbState($row);
}
/**
* @deprecated 20.3
* @deprecated use getAccountByEmail instead
*
* @param string $email
*
* @return EmailBackupAccount|null
*/
public function tryGetEmailAccountByEmail(string $email): ?EmailBackupAccount
{
$query = $this->getBaseQuery()->where('email LIKE :email')->orWhere('smtp_frommail LIKE :email');
$sql = $query->getStatement();
$row = $this->db->fetchRow($sql, ['email' => $email]);
if (empty($row)) {
return null;
}
return EmailBackupAccount::fromDbState($row);
}
/**
* @return EmailBackupAccount[]
*/
public function getAccountsWithTicketActive(): array
{
$query = $this->getBaseQuery();
$query->where('e.ticket = 1');
$query->where('e.geloescht != 1');
$sql = $query->getStatement();
$rows = $this->db->fetchAll($sql);
$accounts = [];
foreach ($rows as $row) {
$accounts[] = EmailBackupAccount::fromDbState($row);
}
return $accounts;
}
/**
* @return SelectQuery
*/
private function getBaseQuery(): SelectQuery
{
$select = $this->db->select();
$select->cols(
[
'e.id',
'e.angezeigtername',
'e.internebeschreibung',
'e.benutzername',
'e.passwort',
'e.server',
'e.smtp',
'e.ticket',
'e.imap_sentfolder_aktiv',
'e.imap_sentfolder',
'e.imap_port',
'e.imap_type',
'e.autoresponder',
'e.geschaeftsbriefvorlage',
'e.autoresponderbetreff',
'e.autorespondertext',
'e.projekt',
'e.emailbackup',
'e.adresse',
'e.firma',
'e.loeschtage',
'e.geloescht',
'e.ticketloeschen',
'e.ticketabgeschlossen',
'e.ticketqueue',
'e.ticketprojekt',
'e.ticketemaileingehend',
'e.smtp_extra',
'e.smtp_ssl',
'e.smtp_port',
'e.smtp_frommail',
'e.smtp_fromname',
'e.autosresponder_blacklist as autoresponder_blacklist',
'e.eigenesignatur',
'e.signatur',
'e.mutex',
'e.abdatum',
'e.email',
'e.smtp_authtype',
'e.smtp_authparam',
'e.smtp_loglevel',
'e.client_alias',
]
);
$select->from('emailbackup as e');
return $select;
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\SystemMailer\Service;
use Xentral\Components\Mailer\Data\EmailMessage;
final class MailBodyCleaner
{
/**
* @param EmailMessage $email
*
* @return EmailMessage
*/
public function cleanEmailBody(EmailMessage $email): EmailMessage
{
return $email;
}
}
@@ -0,0 +1,107 @@
<?php
namespace Xentral\Modules\SystemMailer\Service;
use Xentral\Components\Database\Database;
use Xentral\Components\Mailer\Data\EmailMessage;
use Xentral\Modules\SystemMailer\Data\MailAccountInterface;
final class MailLogService
{
/** @var int LOGTYPE_DEFAULT */
const LOGTYPE_DEFAULT = 0;
/** @var int LOGTYPE_CC */
const LOGTYPE_CC = 1;
/** @var int LOGTYPE_BCC */
const LOGTYPE_BCC = 2;
/** @var Database $db */
private $db;
/**
* @param Database $database
*/
public function __construct(Database $database)
{
$this->db = $database;
}
/**
* @param EmailMessage $email
* @param MailAccountInterface $account
* @param string $status
*
* @return void
*/
public function logOutgoingMail(EmailMessage $email, MailAccountInterface $account, $status):void
{
$status .= sprintf(
' (Host:%s User: %s)',
$account->getServerAddress(),
$account->getUsername()
);
$insert = $this->db->insert();
$insert->into('mailausgang');
foreach ($email->getRecipients() as $recipient) {
$insert->addRow()
->cols(
[
'subject' => $email->getSubject(),
'body' => $email->getBody(),
'from' => $account->getSenderEmailAddress(),
'to' => (string)$recipient,
'status' => $status,
'art' => self::LOGTYPE_DEFAULT,
]
)
->set('zeit', 'NOW()');
}
foreach ($email->getCcRecipients() as $recipient) {
$insert->addRow()
->cols(
[
'subject' => $email->getSubject(),
'body' => $email->getBody(),
'from' => $account->getSenderEmailAddress(),
'to' => (string)$recipient,
'status' => $status,
'art' => self::LOGTYPE_CC,
]
)
->set('zeit', 'NOW()');
}
foreach ($email->getBccRecipients() as $recipient) {
$insert->addRow()
->cols(
[
'subject' => $email->getSubject(),
'body' => $email->getBody(),
'from' => $account->getSenderEmailAddress(),
'to' => (string)$recipient,
'status' => $status,
'art' => self::LOGTYPE_BCC,
]
)
->set('zeit', 'NOW()');
}
$sql = $insert->getStatement();
$values = $insert->getBindValues();
$this->db->perform($sql, $values);
}
/**
* @param int $days
*
* @return int
*/
public function deleteLogsOlderThan(int $days = 90):int
{
$sql = 'DELETE FROM `mailausgang` WHERE DATE_SUB(CURDATE(), INTERVAL :dayInterval DAY) >= zeit';
return $this->db->fetchAffected($sql, ['dayInterval' => $days]);
}
}
@@ -0,0 +1,187 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\SystemMailer\Service;
use PHPMailer\PHPMailer\PHPMailer;
use Xentral\Components\Logger\LoggerAwareTrait;
use Xentral\Components\Mailer\Config\OAuthMailerConfig;
use Xentral\Components\Mailer\Config\SmtpMailerConfig;
use Xentral\Components\Mailer\Transport\MailerTransportInterface;
use Xentral\Components\Mailer\Transport\PhpMailerOAuth;
use Xentral\Components\Mailer\Transport\PhpMailerTransport;
use Xentral\Core\DependencyInjection\ContainerInterface;
use Xentral\Modules\GoogleApi\Exception\GoogleAccountNotFoundException;
use Xentral\Modules\GoogleApi\Exception\GoogleCredentialsException;
use Xentral\Modules\GoogleApi\Service\GoogleAccountGateway;
use Xentral\Modules\GoogleApi\Service\GoogleAuthorizationService;
use Xentral\Modules\GoogleApi\Service\GoogleCredentialsService;
use Xentral\Modules\SystemMailer\Data\EmailBackupAccount;
use Xentral\Modules\SystemMailer\Exception\GmailOAuthException;
use Xentral\Modules\SystemMailer\Exception\InvalidArgumentException;
use Xentral\Modules\SystemMailer\Transport\PhpMailerGoogleAuthentification;
class MailerTransportFactory
{
use LoggerAwareTrait;
/** @var ContainerInterface $container */
private $container;
/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
/**
* @param EmailBackupAccount $account
*
* @throws InvalidArgumentException
*
* @return MailerTransportInterface
*/
public function createMailerTransport(EmailBackupAccount $account):MailerTransportInterface
{
switch ($account->getSmtpAuthType()) {
case EmailBackupAccount::AUTH_SMTP:
return $this->createSmtpTransport($account);
break;
case EmailBackupAccount::AUTH_GMAIL:
return $this->createGoogleOAuthTransport($account);
default:
throw new InvalidArgumentException('Only SMTP accounts are supported.');
}
}
/**
* @param EmailBackupAccount $account
*
* @return SmtpMailerConfig
*/
public function createSmtpMailerConfig(EmailBackupAccount $account):SmtpMailerConfig
{
if ($account->isSmtpEnabled() === false || $account->getSmtpAuthType() !== EmailBackupAccount::AUTH_SMTP) {
throw new InvalidArgumentException('Only SMTP accounts are supported.');
}
$email = $account->getSmtpSenderEmail();
if ($email === '') {
$email = $account->getEmailAddress();
}
$sender = $account->getSmtpSenderName();
if ($sender === '') {
$sender = $account->getDisplayName();
}
$debug = 0;
if ($account->isSmtpDebugEnabled()) {
$debug = 4;
}
return new SmtpMailerConfig([
'sender_email' => $email,
'sender_name' => $sender,
'host' => $account->getSmtpServer(),
'hostname' => $account->getClientAlias(),
'username' => $account->getUserName(),
'password' => $account->getPassword(),
'port' => $account->getSmtpPort(),
'smtp_security' => $account->getSmtpSecurity(),
'mailer' => 'smtp',
'smtp_debug' => $debug
]);
}
/**
* @param EmailBackupAccount $account
*
* @throws InvalidArgumentException
*
* @return OAuthMailerConfig
*/
public function createGmailMailerConfig(EmailBackupAccount $account):OAuthMailerConfig
{
if (
$account->isSmtpEnabled() === false
|| $account->getSmtpAuthType() !== EmailBackupAccount::AUTH_GMAIL
) {
throw new InvalidArgumentException('Only SMTP OAUTH accounts are supported.');
}
$email = $account->getSmtpSenderEmail();
if ($email === '') {
$email = $account->getEmailAddress();
}
$sender = $account->getSmtpSenderName();
if ($sender === '') {
$sender = $account->getDisplayName();
}
$debug = 0;
if ($account->isSmtpDebugEnabled()) {
$debug = 4;
}
$cfgValues = [
'sender_email' => $email,
'sender_name' => $sender,
'host' => $account->getSmtpServer(),
'hostname' => $account->getClientAlias(),
'port' => $account->getSmtpPort(),
'smtp_security' => $account->getSmtpSecurity(),
'mailer' => 'smtp',
'smtp_debug' => $debug
];
if ($cfgValues['host'] === '') {
$cfgValues['host'] = 'smtp.gmail.com';
}
return new OAuthMailerConfig($cfgValues);
}
/**
* @param EmailBackupAccount $account
*
* @return PhpMailerTransport
*/
public function createSmtpTransport(EmailBackupAccount $account):PhpMailerTransport
{
$config = $this->createSmtpMailerConfig($account);
$mailer = new PHPMailer(true);
return new PhpMailerTransport($mailer, $config, $this->logger);
}
/**
* @param EmailBackupAccount $account
*
* @throws GoogleCredentialsException
* @throws GmailOAuthException
* @throws GoogleAccountNotFoundException
*
* @return PhpMailerTransport
*/
public function createGoogleOAuthTransport(EmailBackupAccount $account):PhpMailerTransport
{
$config = $this->createGmailMailerConfig($account);
/** @var GoogleCredentialsService $credentialsService */
$credentialsService = $this->container->get('GoogleCredentialsService');
$credentialsService->getCredentials()->validate();
/** @var GoogleAccountGateway $googleApiGateway */
$googleApiGateway = $this->container->get('GoogleAccountGateway');
$googleAccount = $googleApiGateway->getAccountByGmailAddress($account->getSenderEmailAddress());
/** @var GoogleAuthorizationService $googleAuth */
$googleAuth = $this->container->get('GoogleAuthorizationService');
$oauth = new PhpMailerGoogleAuthentification(
$googleApiGateway,
$googleAuth,
$googleAccount
);
$mailer = new PhpMailerOAuth(true, $oauth);
return new PhpMailerTransport($mailer, $config, $this->logger);
}
}