Api.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. const AUTH_COOKIE = 'PVEAuthCookie';
  33. public function __construct($pve_hostname, $pve_username, $pve_realm = "pam", $pve_password, $login=true)
  34. {
  35. parent::__construct($pve_hostname, $pve_username, $pve_realm, $pve_password);
  36. $this->validate();
  37. if( $pve_realm != "PVEAPIToken" && $login){
  38. $this->login();
  39. }
  40. }
  41. public function setInstance()
  42. {
  43. $this->validate();
  44. self::$instance = $this;
  45. }
  46. /**
  47. *
  48. * @return Api
  49. */
  50. public static function whmcsFactory($params)
  51. {
  52. $host = $params['serverip'] ? $params['serverip'] : $params['serverhostname'];
  53. return self::$instance = new self($host, $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
  54. }
  55. public static function getInstance()
  56. {
  57. if (!empty(self::$instance))
  58. return self::$instance;
  59. throw new ProxmoxApiException("API instance is not defined.");
  60. }
  61. private function validate()
  62. {
  63. if (empty($this->pve_hostname))
  64. throw new ProxmoxApiException("Server fields 'Hostname' or 'IP Address' is empty");
  65. if (empty($this->pve_username))
  66. throw new ProxmoxApiException("Server field 'Username' is empty");
  67. if (empty($this->pve_password))
  68. throw new ProxmoxApiException("Server field 'Password' is empty");
  69. if (empty($this->pve_realm))
  70. throw new ProxmoxApiException("Server field 'Authentication' is empty");
  71. if (gethostbyname($this->pve_hostname) == $this->pve_hostname && !filter_var($this->pve_hostname, FILTER_VALIDATE_IP))
  72. {
  73. throw new ProxmoxApiException(sprintf("Server Hostname '%s' is invalid", $this->pve_hostname));
  74. }
  75. if (!is_numeric($this->port))
  76. throw new ProxmoxApiException(sprintf("Server port '%s' is not valid", $this->port));
  77. }
  78. /**
  79. * FUNCTION procesRequest
  80. * Catch bug form API
  81. * @param array $response
  82. * @return string|boolean
  83. * @throws Exception
  84. */
  85. public function processRequest($response)
  86. {
  87. if (is_string($response))
  88. {
  89. if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseResponse"))
  90. $this->responsePaser->parseResponse($response);
  91. return $response;
  92. } elseif (isset($response['errors']))
  93. {
  94. $err = null;
  95. foreach ($response['errors'] as $param => $msg)
  96. {
  97. $err .= (is_numeric($param) ? '' : $param . ' - ') . ucfirst(trim($msg)) . ' ';
  98. }
  99. if ($err)
  100. {
  101. $err = ucfirst($err);
  102. if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseError"))
  103. $this->responsePaser->parseError($err, $this->httpCode);
  104. throw new ProxmoxApiException($err, $this->httpCode);
  105. }
  106. return false;
  107. }
  108. else if (isset($response['data']['result']['error']['desc']) && $response['data']['result']['error']['desc']){
  109. throw new ProxmoxApiException($response['data']['result']['error']['desc'], 0);
  110. }
  111. else if ($response === null)
  112. {
  113. return true;
  114. }
  115. else
  116. {
  117. return $response;
  118. }
  119. }
  120. public function getLoginTicket()
  121. {
  122. return $this->pve_login_ticket;
  123. }
  124. function getResponsePaser()
  125. {
  126. return $this->responsePaser;
  127. }
  128. function setResponsePaser($responsePaser)
  129. {
  130. $this->responsePaser = $responsePaser;
  131. }
  132. public function getVersion()
  133. {
  134. if (empty($this->version))
  135. {
  136. $info = $this->get('/version');
  137. $this->version = $info ['version'];
  138. }
  139. return $this->version;
  140. }
  141. public function isProxmox4()
  142. {
  143. return version_compare($this->getVersion(), "4.0", '>=');
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function getAuthCookieAsArray(){
  149. if(!$this->getLoginTicket()['ticket']){
  150. throw new \InvalidArgumentException("Auth Cookie is empty");
  151. }
  152. return [self::AUTH_COOKIE => $this->getLoginTicket()['ticket']];
  153. }
  154. }