Improved BIMI detection code #115

This commit is contained in:
the-djmaze 2022-10-17 16:16:06 +02:00
parent 40e2375b3e
commit 69fb887e63
2 changed files with 45 additions and 8 deletions

View file

@ -260,14 +260,7 @@ class Email implements \JsonSerializable
/*
$BIMI = '';
if (Enumerations\DkimStatus::PASS == $this->GetDkimStatus()) {
if ($values = \dns_get_record($this->GetDomain(), \DNS_TXT)) {
foreach ($values as $value) {
if (\str_starts_with($value['txt'], 'v=BIMI1')) {
$BIMI = \preg_replace('/^.+l=([^;]+)(;.*)?$/', '$1', $value['txt']);
break;
}
}
}
$BIMI = \SnappyMail\DNS\BIMI($this->GetDomain());
}
*/
return array(

View file

@ -0,0 +1,44 @@
<?php
namespace SnappyMail;
abstract class DNS
{
public static function BIMI(string $domain) : string
{
$oCache = \RainLoop\Api::Actions()->Cacher();
$BIMI = $oCache->Get("dns-bimi-{$domain}") ?: null;
if ($BIMI) {
$BIMI = \json_decode($BIMI);
if ($BIMI[1] < \time()) {
$BIMI = null;
} else {
$BIMI = $BIMI[0];
}
}
if (null === $BIMI) {
$BIMI = '';
$values = \dns_get_record($domain, \DNS_TXT);
if ($values) {
foreach ($values as $value) {
if (\str_starts_with($value['txt'], 'v=BIMI1')) {
$BIMI = \preg_replace('/^.+l=([^;]+)(;.*)?$/D', '$1', $value['txt']);
$oCache->Set("dns-bimi-{$domain}", \json_encode([
$BIMI,
time() + $value['ttl']
]));
break;
}
}
}
if (!$BIMI) {
// Don't lookup for 24 hours
$oCache->Set("dns-bimi-{$domain}", \json_encode([
$BIMI,
time() + 86400
]));
}
}
return $BIMI;
}
}