Settings.php 6.9 KB

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