EditSettingDataProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Setting\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  5. use ThurData\Servers\KerioEmail\Core\Helper\DnsHelper;
  6. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  7. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  8. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  9. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  10. /**
  11. *
  12. * Created by PhpStorm.
  13. * User: ThurData
  14. * Date: 18.09.19
  15. * Time: 09:35
  16. * Class EditSettingDataProvider
  17. */
  18. class EditSettingDataProvider extends BaseDataProvider
  19. {
  20. /**
  21. *
  22. */
  23. public function read()
  24. {
  25. $maildomain = $this->actionElementId;
  26. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  27. try {
  28. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  29. $dkimSet = $api->getDkimDnsRecord($maildomain);
  30. } catch (KerioApiException $error) {
  31. logModuleCall(
  32. 'kerioEmail',
  33. __FUNCTION__,
  34. $error,
  35. 'Debug Error',
  36. $error->getMessage()
  37. );
  38. return ['error' => $error->getMessage()];
  39. }
  40. $api->logout();
  41. // format dkim response
  42. $dkimArray = explode(PHP_EOL,$dkimSet['detail']);
  43. $dkimValue = explode(":",$dkimArray[1]);
  44. $this->domainKey = ltrim($dkimValue[1]);
  45. $dkimName = explode(":",$dkimArray[0]);
  46. $this->dkimName = ltrim($dkimName[1]);
  47. // get settings from product configuration
  48. $productManager = new ProductManager();
  49. $productManager->loadById($this->getWhmcsParamByKey('pid'));
  50. $this->spfConfig = $productManager->get('spf_string');
  51. $this->dmarcConfig = $productManager->get('dmarc_string');
  52. $clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->getWhmcsParamByKey('userid')));
  53. $dns = new DnsHelper();
  54. /**
  55. * format model to array
  56. */
  57. in_array($maildomain,$clientDomains) ? $selfdomain = 1 : $selfdomain = 0;
  58. $dnsRecords = $dns->getRecords($maildomain);
  59. $dnsRecords['selfdns'] ? $selfDns = $dnsRecords['selfdns'] : $selfDns = 0;
  60. $this->mxthurdata = $this->getWhmcsParamByKey('serverhostname');
  61. if(empty($dnsRecords['mx'])){
  62. $mx = 'unset';
  63. $mxmulti = false;
  64. } else {
  65. count($dnsRecords['mx']) > 1 ? $mxmulti = true : $mxmulti = false;
  66. $mx = implode(' ',$dnsRecords['mx']);
  67. }
  68. if(empty($dnsRecords['spf'])) {
  69. $spf = 'unset';
  70. $spfmulti = false;
  71. } else {
  72. count($dnsRecords['spf']) > 1 ? $spfmulti = true : $spfmulti = false;
  73. $spf = implode(' ',$dnsRecords['spf']);
  74. }
  75. if(empty($dnsRecords['dmarc'])) {
  76. $dmarc = 'unset';
  77. $dmarcmulti = false;
  78. } else {
  79. count($dnsRecords['dmarc']) > 1 ? $dmarcmulti = true : $dmarcmulti = false;
  80. $dmarc = implode(' ',$dnsRecords['dmarc']);
  81. }
  82. if(empty($dnsRecords['dkim'])) {
  83. $dkim = 'unset';
  84. $dkimmulti = false;
  85. } else {
  86. count($dnsRecords['dkim']) > 1 ? $dkimmulti = true : $dkimmulti = false;
  87. $dkim = implode(' ',$dnsRecords['dkim']);
  88. }
  89. if(in_array($this->mxthurdata, $dnsRecords['mx'])){
  90. if($this->spfConfig == $spf && $this->dmarcConfig == $dmarc && in_array($this->domainKey, $dnsRecords['dkim'])){
  91. $dnsok = 'success';
  92. } else {
  93. $dnsok = 'warning';
  94. }
  95. } else {
  96. $dnsok = 'danger';
  97. }
  98. $this->data = [
  99. 'editmaildomain' => $maildomain,
  100. 'editmxactive' => $mx,
  101. 'editspf' => $spf,
  102. 'editdmarc' => $dmarc,
  103. 'dkimname' => $this->dkimName,
  104. 'editdkim' => $dkim,
  105. 'editmxthurdata' => $this->mxthurdata,
  106. 'editspfthurdata' => $this->spfConfig,
  107. 'editdmarcthurdata' => $this->dmarcConfig,
  108. 'editdkimthurdata' => $this->domainKey,
  109. 'zoneid' => $selfDns,
  110. 'dnsok' => $dnsok,
  111. 'mxmulti' => $mxmulti,
  112. 'dkimmulti' => $dkimmulti,
  113. 'dmarcmulti' => $dmarcmulti,
  114. 'spfmulti' => $spfmulti
  115. ];
  116. }
  117. /**
  118. * @return HtmlDataJsonResponse
  119. */
  120. public function update()
  121. {
  122. $fieldToProtection = [
  123. 'editmaildomain',
  124. 'zoneid',
  125. 'editmxactive',
  126. 'editspf',
  127. 'editdmarc',
  128. 'editdkim',
  129. 'dkimname'
  130. ];
  131. foreach ($this->formData as $field => &$value)
  132. {
  133. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  134. }
  135. $dnsParams = array(
  136. 'maildomain' => $this->formData['editmaildomain'],
  137. 'mx' => $this->formData['editmxactive'],
  138. 'spf' => $this->formData['editspf'],
  139. 'dmarc' => $this->formData['editdmarc'],
  140. 'dkim' => $this->formData['editdkim'],
  141. 'dkimname' => $this->formData['dkimname']
  142. );
  143. $dns = new DnsHelper();
  144. $result = $dns->updateDNS($this->formData['zoneid'],$dnsParams);
  145. if($result != 'success') {
  146. logModuleCall(
  147. 'kerioEmail',
  148. __FUNCTION__,
  149. $result,
  150. 'Debug result updateDNS zoneID: ' . $this->formData['zoneid'],
  151. $dnsParams
  152. );
  153. return (new HtmlDataJsonResponse())->setMessageAndTranslate('updateHasBeenFailed')->setStatusError();
  154. }
  155. return (new HtmlDataJsonResponse())->setMessageAndTranslate('settingHasBeenUpdated')->setStatusSuccess();
  156. }
  157. /**
  158. * @return HtmlDataJsonResponse
  159. */
  160. public function updateStatus()
  161. {
  162. }
  163. /**
  164. * @return HtmlDataJsonResponse
  165. */
  166. public function changePassword()
  167. {
  168. }
  169. }