Whmcs.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Api;
  3. use \ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Admins;
  4. use \ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception;
  5. use \ModulesGarden\ProxmoxAddon\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  6. class Whmcs
  7. {
  8. /**
  9. * @var Admins
  10. */
  11. protected $admins;
  12. /**
  13. * @var string
  14. */
  15. protected $username;
  16. /**
  17. * @param Admins $admins
  18. * @throws \ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception
  19. */
  20. public function __construct(Admins $admins)
  21. {
  22. $this->admins = $admins;
  23. $this->getAdminUserName();
  24. if (function_exists('localAPI') === false)
  25. {
  26. throw new Exception(ErrorCodesLib::CORE_WAPI_000001);
  27. }
  28. }
  29. /**
  30. * @return string
  31. */
  32. protected function getAdminUserName()
  33. {
  34. if (isset($this->username) === false)
  35. {
  36. $this->username = $this->admins->first()->toArray()['username'];
  37. }
  38. return $this->username;
  39. }
  40. public function call($command, $config = [])
  41. {
  42. $result = localAPI($command, $config, $this->getAdminUserName());
  43. if ($result['result'] == 'error')
  44. {
  45. $exc = new Exception(ErrorCodesLib::CORE_WAPI_000002, ['command' => $command, 'data' => $config, 'result' => $result]);
  46. $exc->setCustomMessage($result['message']);
  47. throw $exc;
  48. }
  49. return $result;
  50. }
  51. public function getAdminDetails($adminId)
  52. {
  53. $data = $this->admins->where("id", "LIKE", $adminId)->first();
  54. if ($data === null)
  55. {
  56. throw new Exception(ErrorCodesLib::CORE_WAPI_000003, ['adminId' => $adminId], ['adminId' => $adminId]);
  57. }
  58. $result = localAPI("getadmindetails", [], $data->toArray()['username']);
  59. if ($result['result'] == 'error')
  60. {
  61. $exc = new Exception(ErrorCodesLib::CORE_WAPI_000004, ['command' => "getadmindetails", 'data' => [], 'result' => $result]);
  62. $exc->setCustomMessage($result['message']);
  63. throw $exc;
  64. }
  65. $result['allowedpermissions'] = explode(",", $result['allowedpermissions']);
  66. unset($result['result']);
  67. return $result;
  68. }
  69. }