Settings.php 7.2 KB

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