*/ class DnsHelper { use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams; public function __construct() { $this->params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'domainid', 'serverid', 'pid']); $this->server = Server::select('id', 'nameserver1ip', 'nameserver2ip')->findOrFail($this->params['serverid']); // $this->nameserver = array(trim($this->server->nameserver1ip), trim($this->server->nameserver2ip)); $this->nameserver = array('127.0.0.1', '127.0.0.2'); //test $this->clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->params['userid'])); $this->productManager = new ProductManager(); $this->productManager->loadById($this->params['pid']); } public function KerioEmailCheckDNS() { $vars['maildomain'] = $this->params['domain']; $spfConfig = $this->productManager->get('spf_string'); $dmarcConfig = $this->productManager->get('dmarc_string'); if($this->clientDomains['totalresults'] == 0){ $vars['selfdomain'] = FALSE; $vars['dmarcconfig'] = $dmarcConfig; $vars['spfconfig'] = $spfConfig; return $vars; } else { $vars['selfdomain'] = TRUE; } $resolver = new \Net_DNS2_Resolver(array('nameservers' => $this->nameserver)); try { $result = $resolver->query($this->params['domain'], 'MX'); } catch(\Net_DNS2_Exception $e) { echo "::query() failed: ", $e->getMessage(), "\n"; } $domainMX = $result->answer; try { $result = $resolver->query($this->params['domain'], 'TXT'); } catch(\Net_DNS2_Exception $e) { echo "::query() failed: ", $e->getMessage(), "\n"; } $domainTXT = $result->answer; $domainSPF = array(); $domainDKIM = array(); $domainDMARC = array(); foreach($domainTXT as $txtRecord) { foreach($txtRecord->text as $txtData) { if(strstr($txtData,'v=spf')) { array_push($domainSPF,$txtData); } if(strstr($txtData,'v=DKIM')) { array_push($domainDKIM,$txtData); } if(strstr($txtData,'v=DMARC')) { array_push($domainDMARC,$txtData); } } } # self hosted DNS $vars['selfDNS'] = FALSE; for($i=$this->clientDomains['startnumber'];$i<=$this->clientDomains['numreturned'];$i++) { if($this->params['domain'] == $this->clientDomains['domains']['domain'][$i]['domainname']) { $vars['selfDNS'] = TRUE; $vars['domainId'] = $this->clientDomains['domains']['domain'][$i]['id']; } } # SPF if (count($domainSPF) > 1) { $vars['multiSPF'] = TRUE; } else { $vars['multiSPF'] = FALSE; } $vars['spf'] = 'wrong'; if (empty($domainSPF)) { $vars['spf'] = 'unset'; } else { foreach($domainSPF as $spf) { if($spf === $spfConfig) { $vars['spf'] = 'set'; } } } $vars['domainSPF'] = $domainSPF; # DKIM if (count($domainDKIM) > 1) { $vars['multiDKIM'] = TRUE; } else { $vars['multiDKIM'] = FALSE; } if (empty($domainDKIM)) { $vars['dkim'] = 'unset'; } else { $vars['dkim'] = 'set'; } $vars['domainDKIM'] = $domainDKIM; # DMARC if (count($domainDMARC) > 1) { $vars['multiDMARC'] = TRUE; } else { $vars['multiDMARC'] = FALSE; } $vars['dmarc'] = 'wrong'; if (empty($domainDMARC)) { $vars['dmarc'] = 'unset'; } else { foreach($domainDMARC as $dmarc) { if($dmarc === $dmarcConfig) { $vars['dmarc'] = 'set'; } } } $vars['domainDMARC'] = $domainDMARC; # MX if(count($domainMX) > 1) { $vars['multiMX'] = TRUE; } else { $vars['multiMX'] = FALSE; } if(empty($domainMX)){ $vars['mx'] = 'unset'; $vars['mxtarget'] = $params['serverhostname']; } else { $vars['domainMX'] = $domainMX; $domainMXrecord = array_shift($domainMX); $vars['mxtarget'] = $domainMXrecord->exchange; if($domainMXrecord->exchange == $params['serverhostname']) { $vars['mx'] = 'set'; } else { $var['mx'] = 'wrong'; } } $zoneIDcollection = Capsule::table('dns_manager2_zone') ->select('id') ->where('name', '=', $this->params['domain']) ->get(); logModuleCall( 'kerioEmail', __FUNCTION__, $this->params, 'Debug', $zoneIDcollection ); return $vars; } function KerioEmail_setMX() { $zoneIDcollection = Capsule::table('dns_manager2_zone') ->select('id') ->where('name', '=', $this->params['domain']) ->get(); $zoneIDobj = $zoneIDcollection[0]; $zoneID = $zoneIDobj->{'id'}; if(!isset($zoneID)) { return 'Error: zone ID not found for domain ' . $this->params['domain']; } $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID)); if($dnsZone['result'] != 'success') { return 'Error: cloud not fetch zone for ID ' . $zoneID; } $zoneRecords = array(); $mxRecord = array( 'line' => $this->params['domain'].'.|MX|0', 'name' => '@', 'type' => 'MX', 'class' => 'IN', 'data' => array( 'preference' => '10', 'exchange' => MX, ), ); array_push($zoneRecords, $mxRecord); foreach($dnsZone['data']->records as $record) { if($record->type != 'MX') { array_push($zoneRecords, $record); }; } $result = localAPI('dnsmanager' , array( 'dnsaction' => 'updateZone', 'zone_id' => $zoneID, 'records' => $zoneRecords, ) ); if($result['result'] != 'success') { return 'Error: cloud not update zone for ID ' . $zoneID; } return 'success'; } }