Whmcs.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\Api;
  3. use \ModulesGarden\Servers\ProxmoxVps\Core\Models\Whmcs\Admins;
  4. use \ModulesGarden\Servers\ProxmoxVps\Core\HandlerError\Exceptions\Exception;
  5. use \ModulesGarden\Servers\ProxmoxVps\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\Servers\ProxmoxVps\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. unset($result['result']);
  50. return $result;
  51. }
  52. public function getAdminDetails($adminId)
  53. {
  54. $data = $this->admins->where("id", "LIKE", $adminId)->first();
  55. if ($data === null)
  56. {
  57. throw new Exception(ErrorCodesLib::CORE_WAPI_000003, ['adminId' => $adminId], ['adminId' => $adminId]);
  58. }
  59. $result = localAPI("getadmindetails", [], $data->toArray()['username']);
  60. if ($result['result'] == 'error')
  61. {
  62. $exc = new Exception(ErrorCodesLib::CORE_WAPI_000004, ['command' => "getadmindetails", 'data' => [], 'result' => $result]);
  63. $exc->setCustomMessage($result['message']);
  64. throw $exc;
  65. }
  66. $result['allowedpermissions'] = explode(",", $result['allowedpermissions']);
  67. unset($result['result']);
  68. return $result;
  69. }
  70. }