AccountDataProvider.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. try {
  59. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password'], $active)['result'][0]['id'];
  60. } catch (KerioApiException $error) {
  61. logModuleCall(
  62. 'kerioEmail',
  63. __FUNCTION__,
  64. $error,
  65. 'Debug Error',
  66. $error->getMessage()
  67. );
  68. return ['error' => $error->getMessage()];
  69. }
  70. $account['fullName'] = $this->formData['display_name'];
  71. if ($this->formData['quota'] > 0) {
  72. $account['diskSizeLimit']['isActive'] = TRUE;
  73. } else {
  74. $account['diskSizeLimit']['isActive'] = FALSE;
  75. }
  76. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  77. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  78. try {
  79. $result = $api->modifyUser($userID, $account);
  80. } catch (KerioApiException $error) {
  81. logModuleCall(
  82. 'kerioEmail',
  83. __FUNCTION__,
  84. $error,
  85. 'Debug Error',
  86. $error->getMessage()
  87. );
  88. return ['error' => $error->getMessage()];
  89. }
  90. $fields['firstName'] = $this->formData['firstname'];
  91. $fields['surName'] = $this->formData['lastname'];
  92. $fields['commonName'] = $this->formData['display_name'];
  93. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  94. $fields['titleBefore'] = $this->formData['title'];
  95. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  96. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  97. $fields['departmentName'] = $this->formData['department'];
  98. $fields['profession'] = $this->formData['profession'];
  99. try {
  100. $result = $api->setAddress($userID, $fields);
  101. } catch (KerioApiException $error) {
  102. logModuleCall(
  103. 'kerioEmail',
  104. __FUNCTION__,
  105. $error,
  106. 'Debug Error',
  107. $error->getMessage()
  108. );
  109. return ['error' => $error->getMessage()];
  110. }
  111. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  112. }
  113. public function updateStatus()
  114. {
  115. }
  116. public function update()
  117. {
  118. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  119. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  120. try {
  121. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  122. } catch (KerioApiException $error) {
  123. logModuleCall(
  124. 'kerioEmail',
  125. __FUNCTION__,
  126. $error,
  127. 'Debug Error',
  128. $error->getMessage()
  129. );
  130. return ['error' => $error->getMessage()];
  131. }
  132. /**
  133. * run service for each id
  134. */
  135. foreach($this->request->get('massActions') as $id)
  136. {
  137. try {
  138. $result = $api->modifyUser($id, $status);
  139. } catch (KerioApiException $error) {
  140. logModuleCall(
  141. 'kerioEmail',
  142. __FUNCTION__,
  143. $error,
  144. 'Debug Error',
  145. $error->getMessage()
  146. );
  147. return ['error' => $error->getMessage()];
  148. }
  149. }
  150. $api->logout();
  151. /**
  152. * return success
  153. */
  154. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  155. }
  156. }