EditSettingDataProvider.php 8.6 KB

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