Settings.php 7.2 KB

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