Settings.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Setting\Pages;
  3. use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Fields\EnabledField;
  4. use ThurData\Servers\KerioEmail\App\UI\Client\Setting\Buttons\EditSettingButton;
  5. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  6. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\Column;
  7. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  8. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\ClientArea;
  9. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  10. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataTable;
  11. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  12. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  13. use ThurData\Servers\KerioEmail\App\UI\Client\Setting\Buttons\InfoButton;
  14. use ThurData\Servers\KerioEmail\Core\Helper\DnsHelper;
  15. /**
  16. *
  17. * Created by PhpStorm.
  18. * User: ThurData
  19. * Date: 10.09.19
  20. * Time: 10:51
  21. * Class Setting
  22. */
  23. class Settings extends DataTable implements ClientArea
  24. {
  25. use \ThurData\Servers\KerioEmail\Core\UI\Traits\DisableButtonByColumnValue;
  26. /**
  27. * labels for statuses
  28. */
  29. const STATUS_LABEL = [
  30. 'set' => 'success',
  31. 'unset' => 'warning',
  32. 'wrong' => 'danger',
  33. 'multi' => 'danger',
  34. 'default' => 'default'
  35. ];
  36. protected $id = 'Setting';
  37. protected $name = 'Setting';
  38. protected $title = null;
  39. /**
  40. * load columns
  41. */
  42. protected function loadHtml()
  43. {
  44. $this
  45. ->addColumn((new Column('domain'))
  46. ->setOrderable(DataProvider::SORT_ASC)
  47. ->setSearchable(true, Column::TYPE_STRING))
  48. ->addColumn((new Column('mxactive'))
  49. ->setOrderable()
  50. ->setSearchable(true, Column::TYPE_STRING))
  51. ->addColumn((new Column('spf'))
  52. ->setOrderable()
  53. ->setSearchable(true, Column::TYPE_STRING))
  54. ->addColumn((new Column('dmarc'))
  55. ->setOrderable()
  56. ->setSearchable(true, Column::TYPE_STRING))
  57. ->addColumn((new Column('dkim'))
  58. ->setOrderable()
  59. ->setSearchable(true))
  60. ->addColumn((new Column('selfdns'))
  61. ->setClass("hidden"))
  62. ->addColumn((new Column('selfdomain'))
  63. ->setClass("hidden"))
  64. ;
  65. }
  66. /**
  67. * @param $key
  68. * @param $row
  69. * @return mixed
  70. */
  71. public function replaceFieldMxactive($key, $row)
  72. {
  73. $status = self::STATUS_LABEL[$row[$key]] ? self::STATUS_LABEL[$row[$key]] : self::STATUS_LABEL['default'];
  74. $label = di('lang')->absoluteT('kerio','account','status',$row[$key]);
  75. $field = new EnabledField();
  76. $field->setRawType($status);
  77. $field->setRawTitle($label);
  78. return $field->getHtml();
  79. }
  80. /**
  81. * load buttons
  82. */
  83. public function initContent()
  84. {
  85. $this->addActionButton(new InfoButton());
  86. $this->addActionButton(new EditSettingButton());
  87. }
  88. /**
  89. * load data
  90. */
  91. public function loadData()
  92. {
  93. $maildomain = $this->getWhmcsParamByKey('domain');
  94. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  95. try {
  96. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  97. $domains = $api->getDomains(['id','name','aliasList']);
  98. $dkimSet = $api->getDkimDnsRecord($maildomain);
  99. } catch (KerioApiException $error) {
  100. logModuleCall(
  101. 'kerioEmail',
  102. __FUNCTION__,
  103. $error,
  104. 'Debug Error',
  105. $error->getMessage()
  106. );
  107. return ['error' => $error->getMessage()];
  108. }
  109. foreach($domains as $domain) {
  110. if(($domain['name']) === $maildomain){
  111. $aliasList = $domain['aliasList'];
  112. }
  113. }
  114. $api->logout();
  115. $domains = [$maildomain];
  116. foreach($aliasList as $aliasDomain){
  117. array_push($domains,$aliasDomain);
  118. }
  119. // format dkim response
  120. $dkimArray = explode(PHP_EOL,$dkimSet['detail']);
  121. $dkimValue = explode(":",$dkimArray[1]);
  122. $this->domainKey = ltrim($dkimValue[1]);
  123. $dkimName = explode(":",$dkimArray[0]);
  124. $this->dkimName = ltrim($dkimName[1]);
  125. // get settings from product configuration
  126. $productManager = new ProductManager();
  127. $productManager->loadById($this->getWhmcsParamByKey('pid'));
  128. $this->spfConfig = $productManager->get('spf_string');
  129. $this->dmarcConfig = $productManager->get('dmarc_string');
  130. $clientDomains = localAPI('GetClientsDomains', array('clientid' => $params['userid']));
  131. $dns = new DnsHelper();
  132. /**
  133. * format model to array
  134. */
  135. $data = [];
  136. foreach($domains as $domain){
  137. in_array($domain,$clientDomains) ? $selfdomain = 'set' : $selfdomain = 'unset';
  138. $dnsRecords = $dns->getRecords($domain);
  139. $dnsRecords['selfdns'] ? $selfDns = 1 : $selfDns = 0;
  140. if(empty($dnsRecords['mx'])){
  141. $mx = 'unset';
  142. } else {
  143. in_array($params['serverhostname'],$dnsRecords['mx']) ? $mx = 'set' : $mx = 'wrong';
  144. }
  145. if(empty($dnsRecords['spf'])) {
  146. $spf = 'unset';
  147. } else {
  148. if (count($dnsRecords['spf']) > 1) {
  149. $spf = 'multi';
  150. } else {
  151. if($dnsRecords['spf'][0] === $this->spfConfig){
  152. $spf = 'set';
  153. } else {
  154. $spf = 'wrong';
  155. }
  156. }
  157. }
  158. if(empty($dnsRecords['dmarc'])) {
  159. $dmarc = 'unset';
  160. } else {
  161. if (count($dnsRecords['dmarc']) > 1) {
  162. $dmarc = 'multi';
  163. } else {
  164. if($dnsRecords['dmarc'][0] === $this->dmarcConfig){
  165. $dmarc = 'set';
  166. } else {
  167. $dmarc = 'wrong';
  168. }
  169. }
  170. }
  171. in_array($this->domainKey, $dnsRecords['dkim']) ? $dkim = 'set' : $dkim = 'unset';
  172. $tmp = [
  173. 'domain' => $domain,
  174. 'mxactive' => $mx,
  175. 'spf' => $spf,
  176. 'dmarc' => $dmarc,
  177. 'dkim' => $dkim,
  178. 'selfdns' => $selfDns,
  179. 'selfdomain' => $selfdomain
  180. ];
  181. $data[] = $tmp;
  182. }
  183. $dataProv = new ArrayDataProvider();
  184. $dataProv->setDefaultSorting('setting', 'ASC')->setData($data);
  185. $this->setDataProvider($dataProv);
  186. }
  187. }