MailConfigChecker/Application/Controller/Admin/SpfChecker.php

160 lines
5.1 KiB
PHP
Raw Normal View History

2023-12-11 15:48:31 +01:00
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\MailConfigChecker\Application\Controller\Admin;
use Assert\InvalidArgumentException;
2024-06-04 14:20:20 +02:00
use D3\MailAuthenticationCheck\DMARCCheck;
use D3\MailAuthenticationCheck\Model\DMARCResult;
2024-05-31 16:31:25 +02:00
use D3\MailConfigChecker\Application\Model\Constants;
2024-06-04 14:20:20 +02:00
use D3\MailConfigChecker\Application\Model\DmarcResult as OxDmarcResult;
2023-12-11 15:48:31 +01:00
use D3\MailConfigChecker\Application\Model\SpfResult;
2024-06-04 14:37:41 +02:00
use LogicException;
2023-12-11 15:48:31 +01:00
use Mika56\SPFCheck\DNS\DNSRecordGetter;
use Mika56\SPFCheck\Model\Query;
use Mika56\SPFCheck\Model\Result;
use Mika56\SPFCheck\SPFCheck;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
2023-12-12 11:43:46 +01:00
use OxidEsales\Eshop\Application\Model\Shop;
2023-12-11 15:48:31 +01:00
use OxidEsales\Eshop\Core\Registry;
class SpfChecker extends AdminDetailsController
{
2024-05-31 16:31:25 +02:00
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/spfCheck';
2023-12-11 15:48:31 +01:00
2024-06-04 14:37:41 +02:00
public function render(): ?string
2023-12-11 15:48:31 +01:00
{
$this->checkSpf();
2024-06-04 14:20:20 +02:00
$this->checkDmarc();
2023-12-11 15:48:31 +01:00
return parent::render();
}
2024-06-04 14:37:41 +02:00
protected function checkSpf(): void
2023-12-11 15:48:31 +01:00
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
2023-12-12 11:50:41 +01:00
function ($domain) use (&$result) {
2023-12-11 15:48:31 +01:00
$this->checkSpfByDomain($domain, $result);
}
);
2024-06-04 14:20:20 +02:00
$this->addTplParam('spf_result', $result);
2023-12-11 15:48:31 +01:00
}
2024-06-04 14:37:41 +02:00
protected function checkDmarc(): void
2024-06-04 14:20:20 +02:00
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function ($domain) use (&$result) {
$this->checkDmarcByDomain($domain, $result);
}
);
$this->addTplParam('dmarc_result', $result);
}
protected function getMailDomains(): array
2023-12-11 15:48:31 +01:00
{
2023-12-12 11:43:46 +01:00
/** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop();
2023-12-11 15:48:31 +01:00
return
array_filter(
array_unique(
array_map(
2023-12-12 11:50:41 +01:00
function ($mailAddress) {
2023-12-11 15:48:31 +01:00
$mailAddress = trim($mailAddress);
2023-12-12 11:28:18 +01:00
try {
2024-08-22 10:11:08 +02:00
if (! str_contains($mailAddress, '@')) {
2023-12-12 11:50:41 +01:00
throw oxNew(InvalidArgumentException::class);
2023-12-12 11:28:18 +01:00
}
2023-12-12 11:50:41 +01:00
$addressChunks = explode('@', $mailAddress);
2023-12-12 11:28:18 +01:00
2023-12-12 11:50:41 +01:00
return array_pop($addressChunks);
2024-06-04 14:37:41 +02:00
} catch (InvalidArgumentException) {
2023-12-12 11:28:18 +01:00
return '';
}
2023-12-11 15:48:31 +01:00
},
[
2023-12-12 11:43:46 +01:00
$shop->getFieldData('oxinfoemail'),
$shop->getFieldData('oxorderemail'),
$shop->getFieldData('oxowneremail'),
2023-12-11 15:48:31 +01:00
]
)
)
);
}
2024-06-04 14:37:41 +02:00
protected function checkSpfByDomain($domain, &$summarize): void
2023-12-11 15:48:31 +01:00
{
$checker = new SPFCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$result = $checker->getResult($query);
2024-08-22 10:11:08 +02:00
$status = match ($result->getResult()) {
2024-06-04 14:37:41 +02:00
Result::FAIL, Result::NEUTRAL, Result::PASS, Result::SOFTFAIL => SpfResult::SET,
Result::NONE => SpfResult::MISSING,
default => SpfResult::ERROR,
};
2023-12-11 15:48:31 +01:00
$rawRecord = ($record = $result->getRecord()) ?
$record->getRawRecord() :
null;
$summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord);
}
2024-06-04 14:37:41 +02:00
public function getSpfStatusColor(SpfResult $result): string
2023-12-11 15:48:31 +01:00
{
2024-08-22 10:11:08 +02:00
return match ($result->getStatus()) {
2024-06-04 14:37:41 +02:00
SpfResult::SET => 'success',
SpfResult::ERROR => 'warning',
default => 'danger',
};
2023-12-11 15:48:31 +01:00
}
2024-06-04 14:20:20 +02:00
2024-06-04 14:37:41 +02:00
public function checkDmarcByDomain($domain, &$summarize): void
2024-06-04 14:20:20 +02:00
{
try {
$check = new DMARCCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$record = $check->getResult($query)->getRecord();
2024-08-22 10:11:08 +02:00
$status = match ($record->getRejectPolicy()->getValue()) {
2024-06-04 14:37:41 +02:00
DMARCResult::REJECT_QUARANTINE, DMARCResult::REJECT_REJECT => OxDmarcResult::SET,
DMARCResult::REJECT_NONE => OxDmarcResult::MISSING,
default => OxDmarcResult::ERROR,
};
2024-06-04 14:20:20 +02:00
2024-08-22 10:11:08 +02:00
$summarize[$domain] = oxNew(OxDmarcResult::class, $status, $record->getRawRecord());
} catch (LogicException) {
$summarize[$domain] = oxNew(OxDmarcResult::class, OxDmarcResult::MISSING, '');
2024-06-04 14:20:20 +02:00
}
}
2024-06-04 14:37:41 +02:00
public function getDmarcStatusColor(OxDmarcResult $result): string
2024-06-04 14:20:20 +02:00
{
2024-08-22 10:11:08 +02:00
return match ($result->getStatus()) {
2024-06-04 14:37:41 +02:00
SpfResult::SET => 'success',
SpfResult::ERROR => 'warning',
default => 'danger',
};
2024-06-04 14:20:20 +02:00
}
2023-12-12 11:50:41 +01:00
}