EditSettingDataProvider.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. } else {
  64. $mx = implode('',$dnsRecords['mx']);
  65. }
  66. if(empty($dnsRecords['spf'])) {
  67. $spf = 'unset';
  68. } else {
  69. $spf = implode('',$dnsRecords['spf']);
  70. }
  71. if(empty($dnsRecords['dmarc'])) {
  72. $dmarc = 'unset';
  73. } else {
  74. $dmarc = implode('',$dnsRecords['dmarc']);
  75. }
  76. if(empty($dnsRecords['dkim'])) {
  77. $dkim = 'unset';
  78. } else {
  79. $dkim = implode('',$dnsRecords['dkim']);
  80. }
  81. if($this->mxthurdata == $mx){
  82. if($this->spfConfig == $spf && $this->dmarcConfig == $dmarc && in_array($this->domainKey, $dnsRecords['dkim'])){
  83. $dnsok = 'success';
  84. } else {
  85. $dnsok = 'warning';
  86. }
  87. } else {
  88. $dnsok = 'danger';
  89. }
  90. $this->data = [
  91. 'maildomain' => $maildomain,
  92. 'mxactive' => $mx,
  93. 'spf' => $spf,
  94. 'dmarc' => $dmarc,
  95. 'dkimname' => $this->dkimName,
  96. 'dkim' => $dkim,
  97. 'mxthurdata' => $this->mxthurdata,
  98. 'spfthurdata' => $this->spfConfig,
  99. 'dmarcthurdata' => $this->dmarcConfig,
  100. 'dkimthurdata' => $this->domainKey,
  101. 'zoneid' => $selfDns,
  102. 'dnsok' => $dnsok
  103. ];
  104. }
  105. /**
  106. * @return HtmlDataJsonResponse
  107. */
  108. public function update()
  109. {
  110. logModuleCall(
  111. 'kerioEmail',
  112. __FUNCTION__,
  113. $this->formData,
  114. 'Debug Formdata',
  115. $this->params
  116. );
  117. return (new HtmlDataJsonResponse())->setMessageAndTranslate('settingHasBeenUpdated')->setStatusSuccess();
  118. $fieldToProtection = [
  119. 'maildomain',
  120. 'mxthurdata',
  121. 'spfthurdata',
  122. 'dmarcthurdata',
  123. 'dkimthurdata',
  124. 'dkimname',
  125. 'zoneid'
  126. ];
  127. foreach ($this->formData as $field => &$value)
  128. {
  129. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  130. }
  131. $attr = array(
  132. 'description' => $this->formData['description'],
  133. 'type' => $this->formData['type'],
  134. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  135. 'manager' => array(
  136. 'id' => $this->formData['manager'],
  137. 'type' => 'UserPrincipal'
  138. )
  139. );
  140. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  141. try {
  142. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  143. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  144. } catch (KerioApiException $error) {
  145. logModuleCall(
  146. 'kerioEmail',
  147. __FUNCTION__,
  148. $error,
  149. 'Debug Error',
  150. $error->getMessage()
  151. );
  152. return ['error' => $error->getMessage()];
  153. }
  154. try {
  155. $result = $api->updateResouce($attr,$this->formData['id']);
  156. } catch (KerioApiException $error) {
  157. logModuleCall(
  158. 'kerioEmail',
  159. __FUNCTION__,
  160. $error,
  161. 'Debug Error',
  162. $error->getMessage()
  163. );
  164. return ['error' => $error->getMessage()];
  165. }
  166. $api->logout();
  167. return (new HtmlDataJsonResponse())->setMessageAndTranslate('settingHasBeenUpdated')->setStatusSuccess();
  168. }
  169. /**
  170. * @return HtmlDataJsonResponse
  171. */
  172. public function updateStatus()
  173. {
  174. }
  175. /**
  176. * @return HtmlDataJsonResponse
  177. */
  178. public function changePassword()
  179. {
  180. }
  181. }