EditSettingDataProvider.php 6.5 KB

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