Api.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2015-07-24)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. /**
  20. * @author Pawel Kopec <pawelk@modulesgarden.com>
  21. */
  22. namespace MGProvision\Proxmox\v2;
  23. class Api extends AbstractApi
  24. {
  25. protected $version;
  26. /**
  27. *
  28. * @var Api
  29. */
  30. private static $instance;
  31. private $responsePaser;
  32. public function __construct($pve_hostname, $pve_username, $pve_realm = "pam", $pve_password, $login=true)
  33. {
  34. parent::__construct($pve_hostname, $pve_username, $pve_realm, $pve_password);
  35. $this->validate();
  36. if( $pve_realm != "PVEAPIToken" && $login){
  37. $this->login();
  38. }
  39. }
  40. public function setInstance()
  41. {
  42. $this->validate();
  43. self::$instance = $this;
  44. }
  45. /**
  46. *
  47. * @return Api
  48. */
  49. public static function whmcsFactory($params)
  50. {
  51. $host = $params['serverip'] ? $params['serverip'] : $params['serverhostname'];
  52. return self::$instance = new self($host, $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
  53. }
  54. public static function getInstance()
  55. {
  56. if (!empty(self::$instance))
  57. return self::$instance;
  58. throw new ProxmoxApiException("API instance is not defined.");
  59. }
  60. private function validate()
  61. {
  62. if (empty($this->pve_hostname))
  63. throw new ProxmoxApiException("Server fields 'Hostname' or 'IP Address' is empty");
  64. if (empty($this->pve_username))
  65. throw new ProxmoxApiException("Server field 'Username' is empty");
  66. if (empty($this->pve_password))
  67. throw new ProxmoxApiException("Server field 'Password' is empty");
  68. if (empty($this->pve_realm))
  69. throw new ProxmoxApiException("Server field 'Authentication' is empty");
  70. if (gethostbyname($this->pve_hostname) == $this->pve_hostname && !filter_var($this->pve_hostname, FILTER_VALIDATE_IP))
  71. {
  72. throw new ProxmoxApiException(sprintf("Server Hostname '%s' is invalid", $this->pve_hostname));
  73. }
  74. if (!is_numeric($this->port))
  75. throw new ProxmoxApiException(sprintf("Server port '%s' is not valid", $this->port));
  76. }
  77. /**
  78. * FUNCTION procesRequest
  79. * Catch bug form API
  80. * @param array $response
  81. * @return string|boolean
  82. * @throws Exception
  83. */
  84. public function processRequest($response)
  85. {
  86. if (is_string($response))
  87. {
  88. if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseResponse"))
  89. $this->responsePaser->parseResponse($response);
  90. return $response;
  91. } elseif (isset($response['errors']))
  92. {
  93. $err = null;
  94. foreach ($response['errors'] as $param => $msg)
  95. {
  96. $err .= (is_numeric($param) ? '' : $param . ' - ') . ucfirst(trim($msg)) . ' ';
  97. }
  98. if ($err)
  99. {
  100. $err = ucfirst($err);
  101. if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseError"))
  102. $this->responsePaser->parseError($err, $this->httpCode);
  103. throw new ProxmoxApiException($err, $this->httpCode);
  104. }
  105. return false;
  106. }
  107. else if (isset($response['data']['result']['error']['desc']) && $response['data']['result']['error']['desc']){
  108. throw new ProxmoxApiException($response['data']['result']['error']['desc'], 0);
  109. }
  110. else if ($response === null)
  111. {
  112. return true;
  113. }
  114. else
  115. {
  116. return $response;
  117. }
  118. }
  119. public function getLoginTicket()
  120. {
  121. return $this->pve_login_ticket;
  122. }
  123. function getResponsePaser()
  124. {
  125. return $this->responsePaser;
  126. }
  127. function setResponsePaser($responsePaser)
  128. {
  129. $this->responsePaser = $responsePaser;
  130. }
  131. public function getVersion()
  132. {
  133. if (empty($this->version))
  134. {
  135. $info = $this->get('/version');
  136. $this->version = $info ['version'];
  137. }
  138. return $this->version;
  139. }
  140. public function isProxmox4()
  141. {
  142. return version_compare($this->getVersion(), "4.0", '>=');
  143. }
  144. }