TerminateAccount.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Http\Actions;
  3. use ThurData\Servers\KerioEmail\App\Enums\Response;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
  7. use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
  8. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
  9. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  10. /**
  11. *
  12. * Created by PhpStorm.
  13. * User: ThurData
  14. * Date: 09.09.19
  15. * Time: 15:14
  16. * Class TerminateAccount
  17. */
  18. class TerminateAccount extends AddonController
  19. {
  20. use ExtensionsCheckerTrait;
  21. public function execute($params = null)
  22. {
  23. try{
  24. /**
  25. * check if extensions are installed
  26. */
  27. $this->checkExtensionOrThrowError();
  28. /**
  29. * run kerio service
  30. */
  31. $result = $this->kerioRunService($params);
  32. return $result;
  33. }catch (\Exception $ex)
  34. {
  35. /**
  36. * return some crit error
  37. */
  38. return $ex->getMessage();
  39. }
  40. }
  41. /**
  42. * @param null $params
  43. * @return mixed|string
  44. */
  45. protected function kerioRunService($params = null)
  46. {
  47. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  48. try {
  49. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  50. $domainID = $api->getDomainId($params['domain']);
  51. } catch (KerioApiException $error) {
  52. logModuleCall(
  53. 'kerioEmail',
  54. __FUNCTION__,
  55. $error,
  56. 'Debug Error',
  57. $error->getMessage()
  58. );
  59. return ['error' => $error->getMessage()];
  60. }
  61. if ($domainID === FALSE) {
  62. return "Error: Domain $domain not found";
  63. }
  64. // remove all users from domain
  65. try {
  66. $users = $api->getUsers(['id', 'loginName'], $domainID);
  67. } catch (KerioApiException $error) {
  68. logModuleCall(
  69. 'kerioEmail',
  70. __FUNCTION__,
  71. $error,
  72. 'Debug Error',
  73. $error->getMessage()
  74. );
  75. return ['error' => $error->getMessage()];
  76. }
  77. foreach($users as $user) {
  78. try {
  79. $api->deleteUser($user['id']);
  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. }
  91. // finally remove domain
  92. try {
  93. $result = $api->deleteDomain($domainID);
  94. } catch (KerioApiException $error) {
  95. logModuleCall(
  96. 'kerioEmail',
  97. __FUNCTION__,
  98. $error,
  99. 'Debug Error',
  100. $error->getMessage()
  101. );
  102. return ['error' => $error->getMessage()];
  103. }
  104. $api->logout();
  105. /**
  106. * return success response
  107. */
  108. return Response::SUCCESS;
  109. }
  110. }