AddEmailAliasDataProvider.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Providers;
  3. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  4. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\AccountAlias;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  7. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  8. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  9. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  10. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  11. /**
  12. *
  13. * Created by PhpStorm.
  14. * User: ThurData
  15. * Date: 18.09.19
  16. * Time: 11:50
  17. * Class AddEmailAliasDataProvider
  18. */
  19. class AddEmailAliasDataProvider extends BaseDataProvider
  20. {
  21. public function read()
  22. {
  23. /**
  24. * hosting id
  25. */
  26. $hid = $this->getRequestValue('id');
  27. /**
  28. * hosting model
  29. */
  30. $hosting = Hosting::where('id', $hid)->first();
  31. /**
  32. * hosting domain
  33. */
  34. $this->data['domain'] = $hosting->domain;
  35. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  36. try {
  37. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  38. $domains = $api->getDomains(['id','name']);
  39. } catch (KerioApiException $error) {
  40. logModuleCall(
  41. 'kerioEmail',
  42. __FUNCTION__,
  43. $error,
  44. 'Debug Error',
  45. $error->getMessage()
  46. );
  47. return ['error' => $error->getMessage()];
  48. }
  49. foreach($domains as $maildomain) {
  50. if(($maildomain['name']) === $this->data['domain']){
  51. $this->maildomainID = $maildomain['id'];
  52. $this->maildomain = $maildomain['name'];
  53. }
  54. }
  55. $productManager = new ProductManager();
  56. $productManager->loadByHostingId($hosting->id);
  57. $fields = array(
  58. "id",
  59. "loginName"
  60. );
  61. $accounts = $api->getUsers($fields,$this->maildomainID);
  62. $api->logout();
  63. /**
  64. * available accounts
  65. */
  66. foreach($accounts as $account)
  67. {
  68. $this->availableValues['mailbox'][$account['loginName']] = $account['loginName'] . '@' . $this->maildomain;
  69. }
  70. }
  71. public function create()
  72. {
  73. /**
  74. * hosting id
  75. */
  76. $hid = $this->request->get('id');
  77. /**
  78. * product manager allow to check product settings
  79. */
  80. $productManager = new ProductManager();
  81. $productManager->loadByHostingId($hid);
  82. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  83. try {
  84. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  85. $domains = $api->getDomains(['id','name']);
  86. } catch (KerioApiException $error) {
  87. logModuleCall(
  88. 'kerioEmail',
  89. __FUNCTION__,
  90. $error,
  91. 'Debug Error',
  92. $error->getMessage()
  93. );
  94. return ['error' => $error->getMessage()];
  95. }
  96. foreach($domains as $maildomain) {
  97. if(($maildomain['name']) === $this->formData['domain']){
  98. $this->maildomainID = $maildomain['id'];
  99. $this->maildomain = $maildomain['name'];
  100. }
  101. }
  102. try {
  103. $result = $api->createAlias($this->maildomainID, $this->formData['aliasName'], $this->formData['mailbox'] . '@' . $this->maildomain);
  104. } catch (KerioApiException $error) {
  105. logModuleCall(
  106. 'kerioEmail',
  107. __FUNCTION__,
  108. $error,
  109. 'Debug Error',
  110. $error->getMessage()
  111. );
  112. return ['error' => $error->getMessage()];
  113. }
  114. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAliasHasBeenCreated')->setStatusSuccess();
  115. }
  116. public function update()
  117. {
  118. // TODO: Implement update() method.
  119. }
  120. }