| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2015-07-24)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- /**
- * @author Pawel Kopec <pawelk@modulesgarden.com>
- */
- namespace MGProvision\Proxmox\v2;
- class Api extends AbstractApi
- {
- protected $version;
- /**
- *
- * @var Api
- */
- private static $instance;
- private $responsePaser;
- public function __construct($pve_hostname, $pve_username, $pve_realm = "pam", $pve_password, $login=true)
- {
- parent::__construct($pve_hostname, $pve_username, $pve_realm, $pve_password);
- $this->validate();
- if( $pve_realm != "PVEAPIToken" && $login){
- $this->login();
- }
- }
- public function setInstance()
- {
- $this->validate();
- self::$instance = $this;
- }
- /**
- *
- * @return Api
- */
- public static function whmcsFactory($params)
- {
- $host = $params['serverip'] ? $params['serverip'] : $params['serverhostname'];
- return self::$instance = new self($host, $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
- }
- public static function getInstance()
- {
- if (!empty(self::$instance))
- return self::$instance;
- throw new ProxmoxApiException("API instance is not defined.");
- }
- private function validate()
- {
- if (empty($this->pve_hostname))
- throw new ProxmoxApiException("Server fields 'Hostname' or 'IP Address' is empty");
- if (empty($this->pve_username))
- throw new ProxmoxApiException("Server field 'Username' is empty");
- if (empty($this->pve_password))
- throw new ProxmoxApiException("Server field 'Password' is empty");
- if (empty($this->pve_realm))
- throw new ProxmoxApiException("Server field 'Authentication' is empty");
- if (gethostbyname($this->pve_hostname) == $this->pve_hostname && !filter_var($this->pve_hostname, FILTER_VALIDATE_IP))
- {
- throw new ProxmoxApiException(sprintf("Server Hostname '%s' is invalid", $this->pve_hostname));
- }
- if (!is_numeric($this->port))
- throw new ProxmoxApiException(sprintf("Server port '%s' is not valid", $this->port));
- }
- /**
- * FUNCTION procesRequest
- * Catch bug form API
- * @param array $response
- * @return string|boolean
- * @throws Exception
- */
- public function processRequest($response)
- {
- if (is_string($response))
- {
- if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseResponse"))
- $this->responsePaser->parseResponse($response);
- return $response;
- } elseif (isset($response['errors']))
- {
- $err = null;
- foreach ($response['errors'] as $param => $msg)
- {
- $err .= (is_numeric($param) ? '' : $param . ' - ') . ucfirst(trim($msg)) . ' ';
- }
- if ($err)
- {
- $err = ucfirst($err);
- if (is_object($this->responsePaser) && method_exists($this->responsePaser, "parseError"))
- $this->responsePaser->parseError($err, $this->httpCode);
- throw new ProxmoxApiException($err, $this->httpCode);
- }
- return false;
- }
- else if (isset($response['data']['result']['error']['desc']) && $response['data']['result']['error']['desc']){
- throw new ProxmoxApiException($response['data']['result']['error']['desc'], 0);
- }
- else if ($response === null)
- {
- return true;
- }
- else
- {
- return $response;
- }
- }
- public function getLoginTicket()
- {
- return $this->pve_login_ticket;
- }
- function getResponsePaser()
- {
- return $this->responsePaser;
- }
- function setResponsePaser($responsePaser)
- {
- $this->responsePaser = $responsePaser;
- }
- public function getVersion()
- {
- if (empty($this->version))
- {
- $info = $this->get('/version');
- $this->version = $info ['version'];
- }
- return $this->version;
- }
- public function isProxmox4()
- {
- return version_compare($this->getVersion(), "4.0", '>=');
- }
- }
|