Settings.php 7.3 KB

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