AccountDataProvider.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  5. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  6. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  7. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  8. /**
  9. *
  10. * Created by PhpStorm.
  11. * User: ThurData
  12. * Date: 10.09.19
  13. * Time: 13:06
  14. * Class AccountDataProvider
  15. */
  16. class AccountDataProvider extends BaseDataProvider
  17. {
  18. public function read()
  19. {
  20. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  21. $this->data['quota'] = 0;
  22. $lang = di('lang');
  23. $this->availableValues['status'] = [
  24. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  25. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  26. ];
  27. $this->availableValues['unit'] = [
  28. 'MegaBytes' => 'MB',
  29. 'GigaBytes' => 'GB',
  30. ];
  31. }
  32. public function create()
  33. {
  34. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  35. foreach ($this->formData as $field => &$value)
  36. {
  37. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  38. }
  39. $maildomain = $this->getWhmcsParamByKey('domain');
  40. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  41. try {
  42. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  43. $domainID = $api->getDomainId($maildomain);
  44. } catch (KerioApiException $error) {
  45. logModuleCall(
  46. 'kerioEmail',
  47. __FUNCTION__,
  48. $error,
  49. 'Debug Error',
  50. $error->getMessage()
  51. );
  52. return ['error' => $error->getMessage()];
  53. }
  54. if ($domainID === FALSE) {
  55. return "Error: Domain $maildomain not found";
  56. }
  57. $active = $this->formData['status'] === 'active' ? TRUE : FALSE;
  58. $usercount = $api->countUser($domainId);
  59. logModuleCall(
  60. 'kerioEmail',
  61. __FUNCTION__,
  62. $usercount,
  63. 'Debug Error',
  64. $maildomain
  65. );
  66. try {
  67. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password'], $active)['result'][0]['id'];
  68. } catch (KerioApiException $error) {
  69. logModuleCall(
  70. 'kerioEmail',
  71. __FUNCTION__,
  72. $error,
  73. 'Debug Error',
  74. $error->getMessage()
  75. );
  76. return ['error' => $error->getMessage()];
  77. }
  78. $account['fullName'] = $this->formData['display_name'];
  79. if ($this->formData['quota'] > 0) {
  80. $account['diskSizeLimit']['isActive'] = TRUE;
  81. } else {
  82. $account['diskSizeLimit']['isActive'] = FALSE;
  83. }
  84. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  85. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  86. try {
  87. $result = $api->modifyUser($userID, $account);
  88. } catch (KerioApiException $error) {
  89. logModuleCall(
  90. 'kerioEmail',
  91. __FUNCTION__,
  92. $error,
  93. 'Debug Error',
  94. $error->getMessage()
  95. );
  96. return ['error' => $error->getMessage()];
  97. }
  98. $fields['firstName'] = $this->formData['firstname'];
  99. $fields['surName'] = $this->formData['lastname'];
  100. $fields['commonName'] = $this->formData['display_name'];
  101. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  102. $fields['titleBefore'] = $this->formData['title'];
  103. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  104. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  105. $fields['departmentName'] = $this->formData['department'];
  106. $fields['profession'] = $this->formData['profession'];
  107. try {
  108. $result = $api->setAddress($userID, $fields);
  109. } catch (KerioApiException $error) {
  110. logModuleCall(
  111. 'kerioEmail',
  112. __FUNCTION__,
  113. $error,
  114. 'Debug Error',
  115. $error->getMessage()
  116. );
  117. return ['error' => $error->getMessage()];
  118. }
  119. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  120. }
  121. public function updateStatus()
  122. {
  123. }
  124. public function update()
  125. {
  126. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  127. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  128. try {
  129. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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. /**
  141. * run service for each id
  142. */
  143. foreach($this->request->get('massActions') as $id)
  144. {
  145. try {
  146. $result = $api->modifyUser($id, $status);
  147. } catch (KerioApiException $error) {
  148. logModuleCall(
  149. 'kerioEmail',
  150. __FUNCTION__,
  151. $error,
  152. 'Debug Error',
  153. $error->getMessage()
  154. );
  155. return ['error' => $error->getMessage()];
  156. }
  157. }
  158. $api->logout();
  159. /**
  160. * return success
  161. */
  162. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  163. }
  164. }