UnsuspendAccount.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Traits\ExtensionsCheckerTrait;
  6. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
  7. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  8. /**
  9. *
  10. * Created by PhpStorm.
  11. * User: ThurData
  12. * Date: 09.09.19
  13. * Time: 15:15
  14. * Class UnsuspendAccount
  15. */
  16. class UnsuspendAccount extends AddonController
  17. {
  18. use ExtensionsCheckerTrait;
  19. public function execute($params = null)
  20. {
  21. try{
  22. /**
  23. * check if extensions are installed
  24. */
  25. $this->checkExtensionOrThrowError();
  26. /**
  27. * run kerio service
  28. */
  29. $result = $this->kerioRunService($params);
  30. /**
  31. * return result
  32. */
  33. return $result;
  34. }catch (\Exception $ex)
  35. {
  36. /**
  37. * return some crit error
  38. */
  39. return $ex->getMessage();
  40. }
  41. }
  42. /**
  43. * @param null $params
  44. * @return mixed|string
  45. */
  46. protected function kerioRunService($params = null)
  47. {
  48. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  49. try {
  50. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  51. $domainID = $api->getDomainId($params['domain']);
  52. } catch (KerioApiException $error) {
  53. logModuleCall(
  54. 'kerioEmail',
  55. __FUNCTION__,
  56. $error,
  57. 'Debug Error',
  58. $error->getMessage()
  59. );
  60. return ['error' => $error->getMessage()];
  61. }
  62. if ($domainID === FALSE) {
  63. return "Error: Domain $domain not found";
  64. }
  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->modifyUser($user['id'], ['isEnabled' => TRUE]);
  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. $api->logout();
  92. /**
  93. * return success response
  94. */
  95. return Response::SUCCESS;
  96. }
  97. }