CreateAccount.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
  8. use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
  9. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
  10. use ThurData\Servers\KerioEmail\App\Traits\HostingService;
  11. use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams;
  12. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  13. /**
  14. *
  15. * Created by PhpStorm.
  16. * User: ThurData
  17. * Date: 09.09.19
  18. * Time: 15:14
  19. * Class CreateAccount
  20. */
  21. class CreateAccount extends AddonController
  22. {
  23. use ExtensionsCheckerTrait;
  24. use HostingService;
  25. use WhmcsParams;
  26. /**
  27. * create domain in kerio
  28. *
  29. * @param null $params
  30. * @return string
  31. */
  32. public function execute($params = null)
  33. {
  34. try{
  35. /**
  36. * check if extensions are installed
  37. */
  38. $this->checkExtensionOrThrowError();
  39. //update domain
  40. if($params['customfields']['maildomain']){
  41. $params['domain'] = $params['customfields']['maildomain'];
  42. $this->hosting()->domain = $params['domain'];
  43. $this->hosting()->save();
  44. }
  45. /**
  46. * run kerio service
  47. */
  48. $result = $this->kerioRunService($params);
  49. return $result;
  50. }catch (\Exception $ex)
  51. {
  52. /**
  53. * return some crit error
  54. */
  55. return $ex->getMessage();
  56. }
  57. }
  58. /**
  59. * @param null $params
  60. * @return mixed|string
  61. */
  62. protected function kerioRunService($params = null)
  63. {
  64. $productManager = new ProductManager();
  65. $productManager->loadById($params['pid']);
  66. $acc_limit = $productManager->get('acc_limit');
  67. $accounts = $productManager->get('acc_base') + $params['configoptions']['acc_add'] + $params['configoptions']['acc_new'];
  68. if ($acc_limit >= 0) {
  69. if ($accounts > $acc_limit) {
  70. $accounts = $acc_limit;
  71. }
  72. }
  73. $domainMaxSize = $productManager->get('domainMaxSize');
  74. $domainSize = $productManager->get('domainBaseSize') + $params['configoptions']['domainAddSize'] + $params['configoptions']['domainNewSize'];
  75. if ($domainMaxSize >= 0) {
  76. if ($domainSize > $domainMaxSize) {
  77. $domainSize = $domainMaxSize;
  78. }
  79. }
  80. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  81. try {
  82. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  83. $result = $api->createDomain($params['domain']);
  84. $domainID = $result['result'][0]['id'];
  85. $api->logout();
  86. logModuleCall(
  87. 'kerioEmail',
  88. __FUNCTION__,
  89. $accounts,
  90. 'Debug ID',
  91. $domainSize
  92. );
  93. } catch (KerioApiException $error) {
  94. logModuleCall(
  95. 'kerioEmail',
  96. __FUNCTION__,
  97. $error,
  98. 'Debug Error',
  99. $error->getMessage()
  100. );
  101. return ['error' => $error->getMessage()];
  102. }
  103. /**
  104. * return success response
  105. */
  106. return Response::SUCCESS;
  107. }
  108. }