2019-05-23 17:52:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MailPoet\Services;
|
|
|
|
|
2020-05-13 13:35:44 +02:00
|
|
|
use MailPoet\Entities\NewsletterEntity;
|
2020-03-25 12:34:57 +01:00
|
|
|
use MailPoet\Mailer\Mailer;
|
2019-05-27 13:49:51 +02:00
|
|
|
use MailPoet\Mailer\MailerError;
|
|
|
|
use MailPoet\Mailer\MailerLog;
|
2019-05-27 12:48:31 +02:00
|
|
|
use MailPoet\Models\Newsletter;
|
2020-03-25 12:34:57 +01:00
|
|
|
use MailPoet\Newsletter\NewslettersRepository;
|
2019-05-23 17:52:45 +02:00
|
|
|
use MailPoet\Settings\SettingsController;
|
|
|
|
|
|
|
|
class AuthorizedEmailsController {
|
|
|
|
const AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING = 'authorized_emails_addresses_check';
|
|
|
|
|
2022-07-01 14:59:02 +01:00
|
|
|
const AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED = 'authorized';
|
|
|
|
const AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_PENDING = 'pending';
|
|
|
|
const AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_ALL = 'all';
|
|
|
|
const AUTHORIZED_EMAIL_ERROR_ALREADY_AUTHORIZED = 'Email address is already authorized';
|
|
|
|
const AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION = 'Email address is pending confirmation';
|
|
|
|
|
2019-05-23 17:52:45 +02:00
|
|
|
/** @var Bridge */
|
2019-05-27 14:14:50 +02:00
|
|
|
private $bridge;
|
2019-05-23 17:52:45 +02:00
|
|
|
|
|
|
|
/** @var SettingsController */
|
|
|
|
private $settings;
|
|
|
|
|
2020-03-25 12:34:57 +01:00
|
|
|
/** @var NewslettersRepository */
|
|
|
|
private $newslettersRepository;
|
|
|
|
|
2019-05-27 14:14:50 +02:00
|
|
|
private $automaticEmailTypes = [
|
|
|
|
Newsletter::TYPE_WELCOME,
|
|
|
|
Newsletter::TYPE_NOTIFICATION,
|
|
|
|
Newsletter::TYPE_AUTOMATIC,
|
|
|
|
];
|
|
|
|
|
2020-03-25 12:34:57 +01:00
|
|
|
public function __construct(
|
|
|
|
SettingsController $settingsController,
|
|
|
|
Bridge $bridge,
|
|
|
|
NewslettersRepository $newslettersRepository
|
|
|
|
) {
|
2019-05-23 17:52:45 +02:00
|
|
|
$this->settings = $settingsController;
|
|
|
|
$this->bridge = $bridge;
|
2020-03-25 12:34:57 +01:00
|
|
|
$this->newslettersRepository = $newslettersRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFromEmailAddress(string $address) {
|
2022-07-06 19:22:04 +01:00
|
|
|
$authorizedEmails = $this->bridge->getAuthorizedEmailAddresses() ?: [];
|
2020-03-25 12:34:57 +01:00
|
|
|
$isAuthorized = $this->validateAuthorizedEmail($authorizedEmails, $address);
|
|
|
|
if (!$isAuthorized) {
|
|
|
|
throw new \InvalidArgumentException("Email address '$address' is not authorized");
|
|
|
|
}
|
|
|
|
|
|
|
|
// update FROM address in settings & all scheduled and active emails
|
|
|
|
$this->settings->set('sender.address', $address);
|
|
|
|
$result = $this->validateAddressesInScheduledAndAutomaticEmails($authorizedEmails);
|
|
|
|
foreach ($result['invalid_senders_in_newsletters'] ?? [] as $item) {
|
|
|
|
$newsletter = $this->newslettersRepository->findOneById((int)$item['newsletter_id']);
|
|
|
|
if ($newsletter) {
|
|
|
|
$newsletter->setSenderAddress($address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->newslettersRepository->flush();
|
|
|
|
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null);
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 09:38:47 +01:00
|
|
|
public function getAllAuthorizedEmailAddress(): array {
|
2022-07-01 14:59:02 +01:00
|
|
|
return $this->bridge->getAuthorizedEmailAddresses(self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_ALL);
|
|
|
|
}
|
|
|
|
|
2022-07-12 09:38:47 +01:00
|
|
|
public function createAuthorizedEmailAddress(string $email): array {
|
2022-07-01 14:59:02 +01:00
|
|
|
$allEmails = $this->getAllAuthorizedEmailAddress();
|
|
|
|
|
2022-07-06 19:22:04 +01:00
|
|
|
$authorizedEmails = isset($allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED]) ? $allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED] : [];
|
2022-07-01 14:59:02 +01:00
|
|
|
$isAuthorized = $this->validateAuthorizedEmail($authorizedEmails, $email);
|
|
|
|
|
|
|
|
if ($isAuthorized) {
|
|
|
|
throw new \InvalidArgumentException(self::AUTHORIZED_EMAIL_ERROR_ALREADY_AUTHORIZED);
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:22:04 +01:00
|
|
|
$pendingEmails = isset($allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_PENDING]) ? $allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_PENDING] : [];
|
2022-07-01 14:59:02 +01:00
|
|
|
$isPending = $this->validateAuthorizedEmail($pendingEmails, $email);
|
|
|
|
|
|
|
|
if ($isPending) {
|
|
|
|
throw new \InvalidArgumentException(self::AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalData = $this->bridge->createAuthorizedEmailAddress($email);
|
|
|
|
|
2022-07-12 09:38:47 +01:00
|
|
|
if ($finalData && isset($finalData['error'])) {
|
|
|
|
throw new \InvalidArgumentException($finalData['error']);
|
2022-07-06 15:44:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-01 14:59:02 +01:00
|
|
|
return $finalData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isEmailAddressAuthorized(string $email): bool {
|
2022-07-06 19:22:04 +01:00
|
|
|
$authorizedEmails = $this->bridge->getAuthorizedEmailAddresses() ?: [];
|
2022-07-01 14:59:02 +01:00
|
|
|
return $this->validateAuthorizedEmail($authorizedEmails, $email);
|
|
|
|
}
|
|
|
|
|
2019-12-26 12:56:49 +01:00
|
|
|
public function checkAuthorizedEmailAddresses() {
|
2019-09-16 00:19:10 +01:00
|
|
|
if (!Bridge::isMPSendingServiceEnabled()) {
|
2019-05-23 17:52:45 +02:00
|
|
|
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null);
|
2019-05-27 13:49:51 +02:00
|
|
|
$this->updateMailerLog();
|
2021-10-28 20:42:06 +02:00
|
|
|
return null;
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$authorizedEmails = $this->bridge->getAuthorizedEmailAddresses();
|
|
|
|
// Keep previous check result for an invalid response from API
|
2022-06-08 11:58:38 +03:00
|
|
|
if (!$authorizedEmails) {
|
2021-10-28 20:42:06 +02:00
|
|
|
return null;
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
2019-05-27 12:48:31 +02:00
|
|
|
$authorizedEmails = array_map('strtolower', $authorizedEmails);
|
2019-05-23 17:52:45 +02:00
|
|
|
|
2019-05-27 12:48:31 +02:00
|
|
|
$result = [];
|
|
|
|
$result = $this->validateAddressesInSettings($authorizedEmails, $result);
|
|
|
|
$result = $this->validateAddressesInScheduledAndAutomaticEmails($authorizedEmails, $result);
|
2019-05-23 17:52:45 +02:00
|
|
|
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, $result ?: null);
|
2019-05-27 13:49:51 +02:00
|
|
|
$this->updateMailerLog($result);
|
2021-10-28 20:42:06 +02:00
|
|
|
return $result;
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 20:42:06 +02:00
|
|
|
public function onSettingsSave($settings): ?array {
|
2019-05-23 17:52:45 +02:00
|
|
|
$senderAddressSet = !empty($settings['sender']['address']);
|
2020-03-25 12:34:16 +01:00
|
|
|
$mailpoetSendingMethodSet = ($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['method'] ?? null) === Mailer::METHOD_MAILPOET;
|
|
|
|
if ($senderAddressSet || $mailpoetSendingMethodSet) {
|
2021-10-28 20:42:06 +02:00
|
|
|
return $this->checkAuthorizedEmailAddresses();
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
2021-10-28 20:42:06 +02:00
|
|
|
return null;
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:35:44 +02:00
|
|
|
public function onNewsletterSenderAddressUpdate(NewsletterEntity $newsletter, string $oldSenderAddress = null) {
|
|
|
|
if ($newsletter->getSenderAddress() === $oldSenderAddress) {
|
2019-05-27 14:14:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-05-13 13:35:44 +02:00
|
|
|
if ($newsletter->getType() === NewsletterEntity::TYPE_STANDARD && $newsletter->getStatus() === NewsletterEntity::STATUS_SCHEDULED) {
|
2019-05-27 14:14:50 +02:00
|
|
|
$this->checkAuthorizedEmailAddresses();
|
|
|
|
}
|
2020-05-13 13:35:44 +02:00
|
|
|
if (in_array($newsletter->getType(), $this->automaticEmailTypes, true) && $newsletter->getStatus() === Newsletter::STATUS_ACTIVE) {
|
2019-05-27 14:14:50 +02:00
|
|
|
$this->checkAuthorizedEmailAddresses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 17:52:45 +02:00
|
|
|
private function validateAddressesInSettings($authorizedEmails, $result = []) {
|
|
|
|
$defaultSenderAddress = $this->settings->get('sender.address');
|
|
|
|
|
2019-05-27 12:48:31 +02:00
|
|
|
if (!$this->validateAuthorizedEmail($authorizedEmails, $defaultSenderAddress)) {
|
2019-05-23 17:52:45 +02:00
|
|
|
$result['invalid_sender_address'] = $defaultSenderAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-05-27 12:48:31 +02:00
|
|
|
|
|
|
|
private function validateAddressesInScheduledAndAutomaticEmails($authorizedEmails, $result = []) {
|
|
|
|
$condittion = sprintf(
|
|
|
|
"(`type` = '%s' AND `status` = '%s') OR (`type` IN ('%s') AND `status` = '%s')",
|
|
|
|
Newsletter::TYPE_STANDARD,
|
|
|
|
Newsletter::STATUS_SCHEDULED,
|
2019-05-27 14:14:50 +02:00
|
|
|
implode("', '", $this->automaticEmailTypes),
|
2019-05-27 12:48:31 +02:00
|
|
|
Newsletter::STATUS_ACTIVE
|
|
|
|
);
|
|
|
|
|
|
|
|
$newsletters = Newsletter::whereRaw($condittion)->findMany();
|
|
|
|
|
|
|
|
$invalidSendersInNewsletters = [];
|
|
|
|
foreach ($newsletters as $newsletter) {
|
|
|
|
if ($this->validateAuthorizedEmail($authorizedEmails, $newsletter->senderAddress)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$invalidSendersInNewsletters[] = [
|
|
|
|
'newsletter_id' => $newsletter->id,
|
|
|
|
'subject' => $newsletter->subject,
|
|
|
|
'sender_address' => $newsletter->senderAddress,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($invalidSendersInNewsletters)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result['invalid_senders_in_newsletters'] = $invalidSendersInNewsletters;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:49:51 +02:00
|
|
|
/**
|
|
|
|
* @param array|null $error
|
|
|
|
*/
|
|
|
|
private function updateMailerLog(array $error = null) {
|
|
|
|
if ($error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$mailerLogError = MailerLog::getError();
|
|
|
|
if ($mailerLogError && $mailerLogError['operation'] === MailerError::OPERATION_AUTHORIZATION) {
|
|
|
|
MailerLog::resumeSending();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:22:04 +01:00
|
|
|
private function validateAuthorizedEmail($authorizedEmails = [], $email = '') {
|
|
|
|
$lowercaseAuthorizedEmails = array_map('strtolower', $authorizedEmails);
|
|
|
|
return in_array(strtolower($email), $lowercaseAuthorizedEmails, true);
|
2019-05-27 12:48:31 +02:00
|
|
|
}
|
2019-05-23 17:52:45 +02:00
|
|
|
}
|