DnsHelper.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Helper;
  3. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Server;
  4. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  5. use \WHMCS\Database\Capsule;
  6. require_once '/usr/share/php/Net/DNS2.php';
  7. use \Net\DNS2\Net_DNS2_Resolver as Net_DNS2_Resolver;
  8. /**
  9. * Wrapper for WHMCS params passed to controler functions
  10. *
  11. * @autor ThurData <info@thurdata.ch>
  12. */
  13. class DnsHelper
  14. {
  15. use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams;
  16. public function __construct()
  17. {
  18. $this->params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'serverusername', 'serverpassword', 'domainid', 'serverid', 'pid']);
  19. $this->server = Server::select('id', 'nameserver1ip', 'nameserver2ip')->findOrFail($this->params['serverid']);
  20. $this->nameserver = array(trim($this->server->nameserver1ip), trim($this->server->nameserver2ip));
  21. // $this->nameserver = array('127.0.0.1', '127.0.0.2'); //test
  22. $this->resolver = new \Net_DNS2_Resolver(array('nameservers' => $this->nameserver));
  23. }
  24. public function getRecords($domain) {
  25. $zoneID = $this->selfDns($domain);
  26. if($zoneID) {
  27. return $this->getLocalRecords($zoneID);
  28. }
  29. return $this->getResolverRecords($domain);
  30. }
  31. private function getResolverRecords($domain)
  32. {
  33. $vars['mx'] = array();
  34. $vars['spf'] = array();
  35. $vars['dmarc'] = array();
  36. $vars['dkim'] = array();
  37. try {
  38. $responseMX = $this->resolver->query($domain, 'MX');
  39. $responseTXT = $this->resolver->query($domain, 'TXT');
  40. } catch(\Net_DNS2_Exception $e) {
  41. echo "::query() failed: ", $e->getMessage(), "\n";
  42. }
  43. $domainMX = $responseMX->answer;
  44. $domainTXT = $responseTXT->answer;
  45. foreach($domainMX as $mxRecord) {
  46. array_push($vars['mx'], $mxRecord->exchange);
  47. }
  48. foreach($domainTXT as $txtRecord) {
  49. foreach($txtRecord->text as $txtData) {
  50. if(strstr($txtData,'v=spf')) {
  51. array_push($vars['spf'],$txtData);
  52. }
  53. if(strstr($txtData,'v=DMARC')) {
  54. array_push($vars['dmarc'],$txtData);
  55. }
  56. if(strstr($txtData,'v=DKIM')) {
  57. array_push($vars['dkim'],$txtData);
  58. }
  59. }
  60. }
  61. return $vars;
  62. }
  63. private function selfDns($domain){
  64. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  65. ->select('id')
  66. ->where('name', '=', $domain)
  67. ->get();
  68. $zoneIDobj = $zoneIDcollection[0];
  69. if(!isset($zoneIDobj->{'id'})) {
  70. return false;
  71. }
  72. return $zoneIDobj->{'id'};
  73. }
  74. private function getLocalRecords($zoneID) {
  75. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  76. if($dnsZone['result'] != 'success') {
  77. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  78. }
  79. $zoneRecords = array();
  80. foreach($dnsZone['data']->records as $record) {
  81. if(in_array($record->type, ['MX', 'TXT'])){
  82. array_push($zoneRecords, $record);
  83. }
  84. }
  85. logModuleCall(
  86. 'kerioEmail',
  87. __FUNCTION__,
  88. $zoneRecords,
  89. 'DEbug',
  90. $dnsZone
  91. );
  92. return $zoneRecords;
  93. }
  94. function KerioEmailsetDNS()
  95. {
  96. return 'success';
  97. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  98. ->select('id')
  99. ->where('name', '=', $this->params['domain'])
  100. ->get();
  101. $zoneIDobj = $zoneIDcollection[0];
  102. $zoneID = $zoneIDobj->{'id'};
  103. if(!isset($zoneID)) {
  104. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  105. }
  106. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  107. logModuleCall(
  108. 'kerioEmail',
  109. __FUNCTION__,
  110. $this->params,
  111. 'DEbug',
  112. $dnsZone['result']
  113. );
  114. if($dnsZone['result'] != 'success') {
  115. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  116. }
  117. $zoneRecords = array();
  118. $mxRecord = array(
  119. 'line' => $this->params['domain'].'.|MX|0',
  120. 'name' => '@',
  121. 'type' => 'MX',
  122. 'class' => 'IN',
  123. 'data' => array(
  124. 'preference' => '10',
  125. 'exchange' => $this->params['serverhostname'],
  126. ),
  127. );
  128. array_push($zoneRecords, $mxRecord);
  129. $spfRecord = array(
  130. 'line' => $this->params['domain'].'.|TXT|0',
  131. 'name' => '@',
  132. 'type' => 'TXT',
  133. 'class' => 'IN',
  134. 'data' => $this->spfConfig
  135. );
  136. array_push($zoneRecords, $spfRecord);
  137. $dmarcRecord = array(
  138. 'line' => $this->params['domain'].'.|TXT|0',
  139. 'name' => '@',
  140. 'type' => 'TXT',
  141. 'class' => 'IN',
  142. 'data' => $this->dmarcConfig
  143. );
  144. array_push($zoneRecords, $dmarcRecord);
  145. foreach($dnsZone['data']->records as $record) {
  146. if($record->type == 'MX') continue;
  147. if(!$record->type === 'TXT') {
  148. // skip dmarc
  149. if(preg_match('/^v=DMARC1(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  150. // skip spf
  151. if(preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  152. // skip own dkim
  153. if(($this->dkimName == $record->name) && ($this->domainKey == trim($record->rdata->txtdata,'"'))) continue;
  154. };
  155. array_push($zoneRecords, $record);
  156. }
  157. logModuleCall(
  158. 'kerioEmail',
  159. __FUNCTION__,
  160. $this->params,
  161. 'DEbug',
  162. $zoneRecords
  163. );
  164. /* $result = localAPI('dnsmanager' ,
  165. array(
  166. 'dnsaction' => 'updateZone',
  167. 'zone_id' => $zoneID,
  168. 'records' => $zoneRecords,
  169. )
  170. );
  171. if($result['result'] != 'success') {
  172. return 'Error: cloud not update zone for ID ' . $zoneID;
  173. } */
  174. return 'success';
  175. }
  176. function KerioEmailunsetMX()
  177. {
  178. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  179. ->select('id')
  180. ->where('name', '=', $this->params['domain'])
  181. ->get();
  182. $zoneIDobj = $zoneIDcollection[0];
  183. $zoneID = $zoneIDobj->{'id'};
  184. if(!isset($zoneID)) {
  185. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  186. }
  187. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  188. if($dnsZone['result'] != 'success') {
  189. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  190. }
  191. $zoneRecords = array();
  192. foreach($dnsZone['data']->records as $record) {
  193. if($record->type == 'MX') continue;
  194. array_push($zoneRecords, $record);
  195. }
  196. logModuleCall(
  197. 'kerioEmail',
  198. __FUNCTION__,
  199. $this->params,
  200. 'DEbug',
  201. $zoneRecords
  202. );
  203. /* $result = localAPI('dnsmanager' ,
  204. array(
  205. 'dnsaction' => 'updateZone',
  206. 'zone_id' => $zoneID,
  207. 'records' => $zoneRecords,
  208. )
  209. );
  210. if($result['result'] != 'success') {
  211. return 'Error: cloud not update zone for ID ' . $zoneID;
  212. } */
  213. return 'success';
  214. }
  215. }