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 */ namespace MGProvision\Proxmox\v2; class Api extends AbstractApi { protected $version; /** * * @var Api */ private static $instance; private $responsePaser; const AUTH_COOKIE = 'PVEAuthCookie'; 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", '>='); } /** * @return array */ public function getAuthCookieAsArray(){ if(!$this->getLoginTicket()['ticket']){ throw new \InvalidArgumentException("Auth Cookie is empty"); } return [self::AUTH_COOKIE => $this->getLoginTicket()['ticket']]; } }