Settings.php 7.4 KB

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