Settings.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. ->setOrderable()
  64. ->setSearchable(true))
  65. ;
  66. }
  67. /**
  68. * @param $key
  69. * @param $row
  70. * @return mixed
  71. */
  72. public function replaceFieldSelfdns($key, $row)
  73. {
  74. $status = self::STATUS_LABEL[$row[$key]] ? self::STATUS_LABEL[$row[$key]] : self::STATUS_LABEL['default'];
  75. $label = di('lang')->absoluteT('kerio','account','status',$row[$key]);
  76. $field = new EnabledField();
  77. $field->setRawType($status);
  78. $field->setRawTitle($label);
  79. return $field->getHtml();
  80. }
  81. /**
  82. * @param $key
  83. * @param $row
  84. * @return mixed
  85. */
  86. public function replaceFieldMxactive($key, $row)
  87. {
  88. $status = self::STATUS_LABEL[$row[$key]] ? self::STATUS_LABEL[$row[$key]] : self::STATUS_LABEL['default'];
  89. $label = di('lang')->absoluteT('kerio','account','status',$row[$key]);
  90. $field = new EnabledField();
  91. $field->setRawType($status);
  92. $field->setRawTitle($label);
  93. return $field->getHtml();
  94. }
  95. /**
  96. * load buttons
  97. */
  98. public function initContent()
  99. {
  100. $this->addActionButton(new InfoButton());
  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 = 'set' : $selfdomain = 'unset';
  153. $dnsRecords = $dns->getRecords($domain);
  154. $dnsRecords['selfdns'] ? $selfDns = 1 : $selfDns = 0;
  155. if(empty($dnsRecords['mx'])){
  156. $mx = 'unset';
  157. } else {
  158. in_array($params['serverhostname'],$dnsRecords['mx']) ? $mx = 'set' : $mx = 'wrong';
  159. }
  160. if(empty($dnsRecords['spf'])) {
  161. $spf = 'unset';
  162. } else {
  163. if (count($dnsRecords['spf']) > 1) {
  164. $spf = 'multi';
  165. } else {
  166. if($dnsRecords['spf'][0] === $this->spfConfig){
  167. $spf = 'set';
  168. } else {
  169. $spf = 'wrong';
  170. }
  171. }
  172. }
  173. if(empty($dnsRecords['dmarc'])) {
  174. $dmarc = 'unset';
  175. } else {
  176. if (count($dnsRecords['dmarc']) > 1) {
  177. $dmarc = 'multi';
  178. } else {
  179. if($dnsRecords['dmarc'][0] === $this->dmarcConfig){
  180. $dmarc = 'set';
  181. } else {
  182. $dmarc = 'wrong';
  183. }
  184. }
  185. }
  186. in_array($this->domainKey, $dnsRecords['dkim']) ? $dkim = 'set' : $dkim = 'unset';
  187. $tmp = [
  188. 'domain' => $domain,
  189. 'mxactive' => $mx,
  190. 'spf' => $spf,
  191. 'dmarc' => $dmarc,
  192. 'dkim' => $dkim,
  193. 'selfdns' => $selfDns,
  194. 'selfdomain' => $selfdomain
  195. ];
  196. $data[] = $tmp;
  197. }
  198. $dataProv = new ArrayDataProvider();
  199. $dataProv->setDefaultSorting('setting', 'ASC')->setData($data);
  200. $this->setDataProvider($dataProv);
  201. }
  202. }