EditSettingDataProvider.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. 'domainid'
  149. ];
  150. foreach ($this->formData as $field => &$value)
  151. {
  152. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  153. }
  154. // $maildomain = $this->actionElementId;
  155. $this->formData['dkimsigenabled'] == 'on' ? $attr['isDkimEnabled'] = true : $attr['isDkimEnabled'] = false;
  156. $this->formData['tfaenabled'] == 'on' ? $attr['twoFAEnabled'] = true : $attr['twoFAEnabled'] = false;
  157. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  158. try {
  159. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  160. $result = $api->modifyDomain($this->formData['domainid'], $attr);
  161. } catch (KerioApiException $error) {
  162. logModuleCall(
  163. 'kerioEmail',
  164. __FUNCTION__,
  165. $error,
  166. 'Debug Error',
  167. $error->getMessage()
  168. );
  169. return ['error' => $error->getMessage()];
  170. }
  171. $api->logout();
  172. logModuleCall(
  173. 'kerioEmail',
  174. __FUNCTION__,
  175. $result,
  176. 'Debug result update Kerio domainID: ' . $this->formData['domainid'],
  177. $this->actionElementId
  178. );
  179. $dnsParams = array(
  180. 'maildomain' => $this->formData['editmaildomain'],
  181. 'mx' => $this->formData['editmxactive'],
  182. 'spf' => $this->formData['editspf'],
  183. 'dmarc' => $this->formData['editdmarc'],
  184. 'dkim' => $this->formData['editdkim'],
  185. 'dkimname' => $this->formData['dkimname']
  186. );
  187. $dns = new DnsHelper();
  188. $result = $dns->updateDNS($this->formData['zoneid'],$dnsParams);
  189. if($result != 'success') {
  190. logModuleCall(
  191. 'kerioEmail',
  192. __FUNCTION__,
  193. $result,
  194. 'Debug result updateDNS zoneID: ' . $this->formData['zoneid'],
  195. $dnsParams
  196. );
  197. return (new HtmlDataJsonResponse())->setMessageAndTranslate('updateHasBeenFailed')->setStatusError();
  198. }
  199. return (new HtmlDataJsonResponse())->setMessageAndTranslate('settingHasBeenUpdated')->setStatusSuccess();
  200. }
  201. /**
  202. * @return HtmlDataJsonResponse
  203. */
  204. public function updateStatus()
  205. {
  206. }
  207. /**
  208. * @return HtmlDataJsonResponse
  209. */
  210. public function changePassword()
  211. {
  212. }
  213. }