Settings.php 7.3 KB

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