8
0
MailConfigChecker/Application/Controller/Admin/SpfChecker.php

119 Zeilen
3.6 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\MailConfigChecker\Application\Model\SpfResult;
use Mika56\SPFCheck\DNSRecordGetter;
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 = 'spfCheck.tpl';
public function render()
{
$this->checkSpf();
return parent::render();
}
protected function checkSpf()
{
$result = [];
$mailDomains = $this->getMailDomains();
array_walk(
$mailDomains,
function ($domain) use (&$result) {
$this->checkSpfByDomain($domain, $result);
}
);
$this->addTplParam('result', $result);
}
protected function getMailDomains()
{
/** @var Shop $shop */
$shop = Registry::getConfig()->getActiveShop();
return
array_filter(
array_unique(
array_map(
function ($mailAddress) {
$mailAddress = trim($mailAddress);
try {
if (! strstr($mailAddress, '@')) {
throw oxNew(InvalidArgumentException::class);
}
$addressChunks = explode('@', $mailAddress);
return array_pop($addressChunks);
} catch (InvalidArgumentException $e) {
return '';
}
},
[
$shop->getFieldData('oxinfoemail'),
$shop->getFieldData('oxorderemail'),
$shop->getFieldData('oxowneremail'),
]
)
)
);
}
protected function checkSpfByDomain($domain, &$summarize)
{
$checker = new SPFCheck(new DNSRecordGetter());
switch ($checker->isIPAllowed(gethostbyname($domain), $domain)) {
case SPFCheck::RESULT_FAIL:
case SPFCheck::RESULT_NEUTRAL:
case SPFCheck::RESULT_PASS:
case SPFCheck::RESULT_SOFTFAIL:
$status = SpfResult::SET;
break;
case SPFCheck::RESULT_NONE:
$status = SpfResult::MISSING;
break;
default:
$status = SpfResult::ERROR;
}
$rawRecord = ($record = (new DNSRecordGetter())->getSPFRecordForDomain($domain)) ?
implode(', ', $record) :
null;
$summarize[$domain] = oxNew(SpfResult::class, $status, $rawRecord);
}
public function getSpfStatusColor(SpfResult $result)
{
switch ($result->getStatus()) {
case SpfResult::SET:
return 'success';
case SpfResult::ERROR:
return 'warning';
default:
return 'danger';
}
}
}