EditSettingDataProvider.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $securitys = $api->getDomains(['id','name','isDkimEnabled','twoFAEnabled']);
  31. } catch (KerioApiException $error) {
  32. logModuleCall(
  33. 'kerioEmail',
  34. __FUNCTION__,
  35. $error,
  36. 'Debug Error',
  37. $error->getMessage()
  38. );
  39. return ['error' => $error->getMessage()];
  40. }
  41. $api->logout();
  42. // find own domain
  43. foreach($securitys as $security) {
  44. if($security['name'] === $maildomain){
  45. $domainID = $security['id'];
  46. $security['twoFAEnabled'] == true ? $tfaenabled = 'on' : $tfaenabled = 'off' ;
  47. $security['isDkimEnabled'] == true ? $dkimsigenabled = 'on' : $dkimsigenabled = 'off';
  48. }
  49. }
  50. // format dkim response
  51. $dkimArray = explode(PHP_EOL,$dkimSet['detail']);
  52. $dkimValue = explode(":",$dkimArray[1]);
  53. $this->domainKey = ltrim($dkimValue[1]);
  54. $dkimName = explode(":",$dkimArray[0]);
  55. $this->dkimName = ltrim($dkimName[1]);
  56. // get settings from product configuration
  57. $productManager = new ProductManager();
  58. $productManager->loadById($this->getWhmcsParamByKey('pid'));
  59. $this->spfConfig = $productManager->get('spf_string');
  60. $this->dmarcConfig = $productManager->get('dmarc_string');
  61. $clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->getWhmcsParamByKey('userid')));
  62. $dns = new DnsHelper();
  63. /**
  64. * format model to array
  65. */
  66. in_array($maildomain,$clientDomains) ? $selfdomain = 1 : $selfdomain = 0;
  67. $dnsRecords = $dns->getRecords($maildomain);
  68. $dnsRecords['selfdns'] ? $selfDns = $dnsRecords['selfdns'] : $selfDns = 0;
  69. $this->mxthurdata = $this->getWhmcsParamByKey('serverhostname');
  70. if(empty($dnsRecords['mx'])){
  71. $mx = 'unset';
  72. $mxmulti = false;
  73. } else {
  74. count($dnsRecords['mx']) > 1 ? $mxmulti = true : $mxmulti = false;
  75. $mx = implode(' ',$dnsRecords['mx']);
  76. }
  77. if(empty($dnsRecords['spf'])) {
  78. $spf = 'unset';
  79. $spfmulti = false;
  80. } else {
  81. count($dnsRecords['spf']) > 1 ? $spfmulti = true : $spfmulti = false;
  82. $spf = implode(' ',$dnsRecords['spf']);
  83. }
  84. if(empty($dnsRecords['dmarc'])) {
  85. $dmarc = 'unset';
  86. $dmarcmulti = false;
  87. } else {
  88. count($dnsRecords['dmarc']) > 1 ? $dmarcmulti = true : $dmarcmulti = false;
  89. $dmarc = implode(' ',$dnsRecords['dmarc']);
  90. }
  91. $dkim = 'unset';
  92. if(empty($dnsRecords['dkim'])) {
  93. $dkimmulti = false;
  94. } else {
  95. count($dnsRecords['dkim']) > 1 ? $dkimmulti = true : $dkimmulti = false;
  96. foreach($dnsRecords['dkim'] as $dkimRecord) {
  97. if($dkimRecord[0] == $this->dkimName) {
  98. $dkim = $dkimRecord[1];
  99. }
  100. }
  101. }
  102. if(in_array($this->mxthurdata, $dnsRecords['mx'])){
  103. if($this->spfConfig == $spf && $this->dmarcConfig == $dmarc && in_array($this->domainKey, $dnsRecords['dkim'])){
  104. $dnsok = 'success';
  105. } else {
  106. $dnsok = 'warning';
  107. }
  108. } else {
  109. $dnsok = 'danger';
  110. }
  111. $this->data = [
  112. 'editmaildomain' => $maildomain,
  113. 'editmxactive' => $mx,
  114. 'editspf' => $spf,
  115. 'editdmarc' => $dmarc,
  116. 'dkimname' => $this->dkimName,
  117. 'editdkim' => $dkim,
  118. 'editmxthurdata' => $this->mxthurdata,
  119. 'editspfthurdata' => $this->spfConfig,
  120. 'editdmarcthurdata' => $this->dmarcConfig,
  121. 'editdkimthurdata' => $this->domainKey,
  122. 'zoneid' => $selfDns,
  123. 'dnsok' => $dnsok,
  124. 'mxmulti' => $mxmulti,
  125. 'dkimmulti' => $dkimmulti,
  126. 'dmarcmulti' => $dmarcmulti,
  127. 'spfmulti' => $spfmulti,
  128. 'tfaenabled' => $tfaenabled,
  129. 'dkimsigenabled' => $dkimsigenabled,
  130. 'domainid' => $domainID
  131. ];
  132. }
  133. /**
  134. * @return HtmlDataJsonResponse
  135. */
  136. public function update()
  137. {
  138. $fieldToProtection = [
  139. 'editmaildomain',
  140. 'zoneid',
  141. 'editmxactive',
  142. 'editspf',
  143. 'editdmarc',
  144. 'editdkim',
  145. 'dkimname',
  146. 'tfaenabled',
  147. 'dkimsigenabled'
  148. ];
  149. foreach ($this->formData as $field => &$value)
  150. {
  151. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  152. }
  153. // $maildomain = $this->actionElementId;
  154. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  155. try {
  156. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  157. $result = $api->getDkimDnsRecord($maildomain);
  158. $securitys = $api->getDomains(['id','name','isDkimEnabled','twoFAEnabled']);
  159. } catch (KerioApiException $error) {
  160. logModuleCall(
  161. 'kerioEmail',
  162. __FUNCTION__,
  163. $error,
  164. 'Debug Error',
  165. $error->getMessage()
  166. );
  167. return ['error' => $error->getMessage()];
  168. }
  169. $api->logout();
  170. $dnsParams = array(
  171. 'maildomain' => $this->formData['editmaildomain'],
  172. 'mx' => $this->formData['editmxactive'],
  173. 'spf' => $this->formData['editspf'],
  174. 'dmarc' => $this->formData['editdmarc'],
  175. 'dkim' => $this->formData['editdkim'],
  176. 'dkimname' => $this->formData['dkimname']
  177. );
  178. $dns = new DnsHelper();
  179. $result = $dns->updateDNS($this->formData['zoneid'],$dnsParams);
  180. if($result != 'success') {
  181. logModuleCall(
  182. 'kerioEmail',
  183. __FUNCTION__,
  184. $result,
  185. 'Debug result updateDNS zoneID: ' . $this->formData['zoneid'],
  186. $dnsParams
  187. );
  188. return (new HtmlDataJsonResponse())->setMessageAndTranslate('updateHasBeenFailed')->setStatusError();
  189. }
  190. return (new HtmlDataJsonResponse())->setMessageAndTranslate('settingHasBeenUpdated')->setStatusSuccess();
  191. }
  192. /**
  193. * @return HtmlDataJsonResponse
  194. */
  195. public function updateStatus()
  196. {
  197. }
  198. /**
  199. * @return HtmlDataJsonResponse
  200. */
  201. public function changePassword()
  202. {
  203. }
  204. }