Settings.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * @param $key
  82. * @param $row
  83. * @return mixed
  84. */
  85. public function replaceFieldSelfdns($key, $row)
  86. {
  87. return $row[$key] ? $row[$key] : '';
  88. }
  89. /**
  90. * load buttons
  91. */
  92. public function initContent()
  93. {
  94. $this->addActionButton(new InfoButton());
  95. $this->addActionButton(new EditSettingButton());
  96. }
  97. /**
  98. * load data
  99. */
  100. public function loadData()
  101. {
  102. $maildomain = $this->getWhmcsParamByKey('domain');
  103. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  104. try {
  105. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  106. $domains = $api->getDomains(['id','name','aliasList']);
  107. $dkimSet = $api->getDkimDnsRecord($maildomain);
  108. } catch (KerioApiException $error) {
  109. logModuleCall(
  110. 'kerioEmail',
  111. __FUNCTION__,
  112. $error,
  113. 'Debug Error',
  114. $error->getMessage()
  115. );
  116. return ['error' => $error->getMessage()];
  117. }
  118. foreach($domains as $domain) {
  119. if(($domain['name']) === $maildomain){
  120. $aliasList = $domain['aliasList'];
  121. }
  122. }
  123. $api->logout();
  124. $domains = [$maildomain];
  125. foreach($aliasList as $aliasDomain){
  126. array_push($domains,$aliasDomain);
  127. }
  128. // format dkim response
  129. $dkimArray = explode(PHP_EOL,$dkimSet['detail']);
  130. $dkimValue = explode(":",$dkimArray[1]);
  131. $this->domainKey = ltrim($dkimValue[1]);
  132. $dkimName = explode(":",$dkimArray[0]);
  133. $this->dkimName = ltrim($dkimName[1]);
  134. // get settings from product configuration
  135. $productManager = new ProductManager();
  136. $productManager->loadById($this->getWhmcsParamByKey('pid'));
  137. $this->spfConfig = $productManager->get('spf_string');
  138. $this->dmarcConfig = $productManager->get('dmarc_string');
  139. $clientDomains = localAPI('GetClientsDomains', array('clientid' => $params['userid']));
  140. $dns = new DnsHelper();
  141. /**
  142. * format model to array
  143. */
  144. $data = [];
  145. foreach($domains as $domain){
  146. in_array($domain,$clientDomains) ? $selfdomain = 1 : $selfdomain = 0;
  147. $dnsRecords = $dns->getRecords($domain);
  148. $dnsRecords['selfdns'] ? $selfDns = 1 : $selfDns = 0;
  149. if(empty($dnsRecords['mx'])){
  150. $mx = 'unset';
  151. } else {
  152. in_array($params['serverhostname'],$dnsRecords['mx']) ? $mx = 'set' : $mx = 'wrong';
  153. }
  154. if(empty($dnsRecords['spf'])) {
  155. $spf = 'unset';
  156. } else {
  157. if (count($dnsRecords['spf']) > 1) {
  158. $spf = 'multi';
  159. } else {
  160. if($dnsRecords['spf'][0] === $this->spfConfig){
  161. $spf = 'set';
  162. } else {
  163. $spf = 'wrong';
  164. }
  165. }
  166. }
  167. if(empty($dnsRecords['dmarc'])) {
  168. $dmarc = 'unset';
  169. } else {
  170. if (count($dnsRecords['dmarc']) > 1) {
  171. $dmarc = 'multi';
  172. } else {
  173. if($dnsRecords['dmarc'][0] === $this->dmarcConfig){
  174. $dmarc = 'set';
  175. } else {
  176. $dmarc = 'wrong';
  177. }
  178. }
  179. }
  180. in_array($this->domainKey, $dnsRecords['dkim']) ? $dkim = 'set' : $dkim = 'unset';
  181. $tmp = [
  182. 'domain' => $domain,
  183. 'mxactive' => $mx,
  184. 'spf' => $spf,
  185. 'dmarc' => $dmarc,
  186. 'dkim' => $dkim,
  187. 'selfdns' => $selfDns,
  188. 'selfdomain' => $selfdomain
  189. ];
  190. $data[] = $tmp;
  191. }
  192. $dataProv = new ArrayDataProvider();
  193. $dataProv->setDefaultSorting('setting', 'ASC')->setData($data);
  194. $this->setDataProvider($dataProv);
  195. }
  196. }