kerioEmail_mx.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. use WHMCS\Database\Capsule;
  3. if (!defined("WHMCS"))
  4. {
  5. die("This file cannot be accessed directly");
  6. }
  7. require_once 'Net/DNS2.php';
  8. const NS1 = '185.163.51.130';
  9. const NS2 = '185.163.51.131';
  10. const MX = 'mail3.seecure.ch';
  11. const SPF = 'v=spf1 mx a:mail3.seecure.ch ip4:185.163.51.0/24 ip4:89.35.78.0/23 ~all';
  12. function KerioEmail_ClientAreaCustomButtonArray ($params)
  13. {
  14. return array(
  15. 'DNS-Settings' => 'DNS',
  16. );
  17. }
  18. function KerioEmail_ClientAreaAllowedFunctions ($params)
  19. {
  20. return array(
  21. 'MX konfigurieren' => 'setMX',
  22. 'SPF konfigurieren' => 'setSPF',
  23. );
  24. }
  25. function KerioEmail_DNS($params)
  26. {
  27. $vars = KerioEmailCheckDNS($params);
  28. return array(
  29. 'breadcrumb' => array(
  30. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=DNS' => 'DNS Mail Settings',
  31. ),
  32. 'templatefile' => 'kerioEmail_help_mx',
  33. 'vars' => $vars,
  34. );
  35. }
  36. function KerioEmail_setMX($params)
  37. {
  38. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  39. ->select('id')
  40. ->where('name', '=', $params['domain'])
  41. ->get();
  42. $zoneIDobj = $zoneIDcollection[0];
  43. $zoneID = $zoneIDobj->{'id'};
  44. if(!isset($zoneID)) {
  45. return 'Error: zone ID not found for domain ' . $params['domain'];
  46. }
  47. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  48. if($dnsZone['result'] != 'success') {
  49. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  50. }
  51. $zoneRecords = array();
  52. $mxRecord = array(
  53. 'line' => $params['domain'].'.|MX|0',
  54. 'name' => '@',
  55. 'type' => 'MX',
  56. 'class' => 'IN',
  57. 'data' => array(
  58. 'preference' => '10',
  59. 'exchange' => MX,
  60. ),
  61. );
  62. array_push($zoneRecords, $mxRecord);
  63. foreach($dnsZone['data']->records as $record) {
  64. if($record->type != 'MX') {
  65. array_push($zoneRecords, $record);
  66. };
  67. }
  68. $result = localAPI('dnsmanager' ,
  69. array(
  70. 'dnsaction' => 'updateZone',
  71. 'zone_id' => $zoneID,
  72. 'records' => $zoneRecords,
  73. )
  74. );
  75. if($result['result'] != 'success') {
  76. return 'Error: cloud not update zone for ID ' . $zoneID;
  77. }
  78. return 'success';
  79. }
  80. function KerioEmail_setSPF($params)
  81. {
  82. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  83. ->select('id')
  84. ->where('name', '=', $params['domain'])
  85. ->get();
  86. $zoneIDobj = $zoneIDcollection[0];
  87. $zoneID = $zoneIDobj->{'id'};
  88. if(!isset($zoneID)) {
  89. return 'Error: zone ID not found for domain ' . $params['domain'];
  90. }
  91. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  92. if($dnsZone['result'] != 'success') {
  93. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  94. }
  95. $zoneRecords = array();
  96. $spfRecord = array(
  97. 'line' => $params['domain'].'.|TXT|0',
  98. 'name' => '@',
  99. 'type' => 'TXT',
  100. 'class' => 'IN',
  101. 'data' => SPF
  102. );
  103. array_push($zoneRecords, $spfRecord);
  104. foreach($dnsZone['data']->records as $record) {
  105. if(!$record->type === 'TXT'){
  106. array_push($zoneRecords, $record);
  107. } elseif (!preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) {
  108. logModuleCall(
  109. 'kerioEmail',
  110. __FUNCTION__,
  111. $record,
  112. 'Debug Usage',
  113. trim($record->rdata->txtdata,'"')
  114. );
  115. array_push($zoneRecords, $record);
  116. };
  117. }
  118. $result = localAPI('dnsmanager' ,
  119. array(
  120. 'dnsaction' => 'updateZone',
  121. 'zone_id' => $zoneID,
  122. 'records' => $zoneRecords,
  123. )
  124. );
  125. if($result['result'] != 'success') {
  126. return 'Error: cloud not update zone for ID ' . $zoneID;
  127. }
  128. return 'success';
  129. }
  130. function KerioEmailCheckDNS($params)
  131. {
  132. unset($zoneID);
  133. unset($mxStatus);
  134. unset($dnsData);
  135. unset($vars);
  136. $vars['maildomain'] = $params['domain'];
  137. $clientDomains = localAPI('GetClientsDomains', array('clientid' => $params['userid']));
  138. $nameserver = array(
  139. NS1,
  140. NS2);
  141. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  142. try {
  143. $result = $resolver->query($params['domain'], 'MX');
  144. } catch(Net_DNS2_Exception $e) {
  145. echo "::query() failed: ", $e->getMessage(), "\n";
  146. }
  147. $domainMX = $result->answer;
  148. try {
  149. $result = $resolver->query($params['domain'], 'TXT');
  150. } catch(Net_DNS2_Exception $e) {
  151. echo "::query() failed: ", $e->getMessage(), "\n";
  152. }
  153. $domainTXT = $result->answer;
  154. $domainSPF = array();
  155. foreach($domainTXT as $txtRecord) {
  156. foreach($txtRecord->text as $txtData) {
  157. if(strstr($txtData,'v=spf')) {
  158. array_push($domainSPF,$txtData);
  159. }
  160. }
  161. }
  162. if (count($domainSPF) > 1) {
  163. $vars['multiSPF'] = TRUE;
  164. } else {
  165. $vars['multiSPF'] = FALSE;
  166. }
  167. $vars['spf'] = 'wrong';
  168. if (empty($domainSPF)) {
  169. $vars['spf'] = 'unset';
  170. } else {
  171. foreach($domainSPF as $spf) {
  172. if($spf === SPF) {
  173. $vars['spf'] = 'set';
  174. }
  175. }
  176. }
  177. $vars['domainSPF'] = $domainSPF;
  178. if(count($domainMX) > 1) {
  179. $vars['multiMX'] = TRUE;
  180. } else {
  181. $vars['multiMX'] = FALSE;
  182. }
  183. $vars['selfDNS'] = FALSE;
  184. for($i=$clientDomains['startnumber'];$i<=$clientDomains['numreturned'];$i++) {
  185. if($params['domain'] == $clientDomains['domains']['domain'][$i]['domainname']) {
  186. $vars['selfDNS'] = TRUE;
  187. $vars['domainId'] = $clientDomains['domains']['domain'][$i]['id'];
  188. }
  189. }
  190. if(empty($domainMX)){
  191. $vars['mx'] = 'unset';
  192. $vars['mxtarget'] = MX;
  193. } else {
  194. $vars['domainMX'] = $domainMX;
  195. $domainMXrecord = array_shift($domainMX);
  196. $vars['mxtarget'] = $domainMXrecord->exchange;
  197. if($domainMXrecord->exchange == MX) {
  198. $vars['mx'] = 'set';
  199. } else {
  200. $var['mx'] = 'wrong';
  201. }
  202. }
  203. return $vars;
  204. }