Settings.php 6.7 KB

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