8
0
MailConfigChecker/Application/Controller/Admin/SpfChecker.php
2024-06-04 14:37:41 +02:00

160 Zeilen
5.1 KiB
PHP

<?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;
use D3\MailAuthenticationCheck\DMARCCheck;
use D3\MailAuthenticationCheck\Model\DMARCResult;
use D3\MailConfigChecker\Application\Model\Constants;
use D3\MailConfigChecker\Application\Model\DmarcResult as OxDmarcResult;
use D3\MailConfigChecker\Application\Model\SpfResult;
use LogicException;
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;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Registry;
class SpfChecker extends AdminDetailsController
{
protected $_sThisTemplate = '@'.Constants::OXID_MODULE_ID.'/admin/spfCheck';
public function render(): ?string
{
$this->checkSpf();
$this->checkDmarc();
return parent::render();
}
protected function checkSpf(): void
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function ($domain) use (&$result) {
$this->checkSpfByDomain($domain, $result);
}
);
$this->addTplParam('spf_result', $result);
}
protected function checkDmarc(): void
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function ($domain) use (&$result) {
$this->checkDmarcByDomain($domain, $result);
}
);
$this->addTplParam('dmarc_result', $result);
}
protected function getMailDomains(): array
{
/** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop();
return
array_filter(
array_unique(
array_map(
function ($mailAddress) {
$mailAddress = trim($mailAddress);
try {
if ( ! str_contains( $mailAddress, '@' ) ) {
throw oxNew(InvalidArgumentException::class);
}
$addressChunks = explode('@', $mailAddress);
return array_pop($addressChunks);
} catch (InvalidArgumentException) {
return '';
}
},
[
$shop->getFieldData('oxinfoemail'),
$shop->getFieldData('oxorderemail'),
$shop->getFieldData('oxowneremail'),
]
)
)
);
}
protected function checkSpfByDomain($domain, &$summarize): void
{
$checker = new SPFCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$result = $checker->getResult($query);
$status = match ( $result->getResult() ) {
Result::FAIL, Result::NEUTRAL, Result::PASS, Result::SOFTFAIL => SpfResult::SET,
Result::NONE => SpfResult::MISSING,
default => SpfResult::ERROR,
};
$rawRecord = ($record = $result->getRecord()) ?
$record->getRawRecord() :
null;
$summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord);
}
public function getSpfStatusColor(SpfResult $result): string
{
return match ( $result->getStatus() ) {
SpfResult::SET => 'success',
SpfResult::ERROR => 'warning',
default => 'danger',
};
}
public function checkDmarcByDomain($domain, &$summarize): void
{
try {
$check = new DMARCCheck(new DNSRecordGetter());
$query = new Query('', $domain);
$record = $check->getResult($query)->getRecord();
$status = match ( $record->getRejectPolicy()->getValue() ) {
DMARCResult::REJECT_QUARANTINE, DMARCResult::REJECT_REJECT => OxDmarcResult::SET,
DMARCResult::REJECT_NONE => OxDmarcResult::MISSING,
default => OxDmarcResult::ERROR,
};
$summarize[$domain] = oxNew( OxDmarcResult::class, $status, $record->getRawRecord());
} catch ( LogicException) {
$summarize[$domain] = oxNew( OxDmarcResult::class, OxDmarcResult::MISSING, '');
}
}
public function getDmarcStatusColor(OxDmarcResult $result): string
{
return match ( $result->getStatus() ) {
SpfResult::SET => 'success',
SpfResult::ERROR => 'warning',
default => 'danger',
};
}
}