Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
Add VerifyCertificate
  • Loading branch information
franklupo committed Apr 2, 2024
1 parent cbf2ccc commit 0cdf09d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "corsinvest/cv4pve-api-php",
"description": "Corsinvest Proxmox VE Client API PHP",
"version": "8.1.2",
"version": "8.1.3",
"keywords": [
"Proxmox",
"Proxmox VE",
Expand Down
71 changes: 47 additions & 24 deletions src/PveClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class PveClientBase
*/
private $lastResult;

/**
* @ignore
*/
private $verifyCertificate = false;

/**
* Client constructor.
* @param string $hostname Host Proxmox VE
Expand All @@ -77,12 +82,14 @@ public function __construct($hostname, $port = 8006)
$this->hostname = $hostname;
$this->port = $port;
}

/**
* Set timeout in seconds
* @param int $timeout Timeout
* @return $this
* @return PveClientBase
*/
public function setTimeout($timeout) {
public function setTimeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
Expand All @@ -91,11 +98,11 @@ public function setTimeout($timeout) {
* Get timeout in seconds.
* @return int
*/
public function getTimeout() {
public function getTimeout()
{
return $this->timeout;
}


/**
* Return if result is object
* @return bool
Expand All @@ -108,7 +115,7 @@ public function isResultObject()
/**
* Set result is object
* @param bool $resultIsObject
* @return $this
* @return PveClientBase
*/
public function setResultIsObject($resultIsObject)
{
Expand All @@ -119,7 +126,7 @@ public function setResultIsObject($resultIsObject)
/**
* Gets the hostname configured.
*
* @return string The hostname.
* @return string
*/
public function getHostname()
{
Expand All @@ -129,7 +136,7 @@ public function getHostname()
/**
* Gets the port configured.
*
* @return int The port.
* @return int
*/
public function getPort()
{
Expand All @@ -139,8 +146,8 @@ public function getPort()
/**
* Sets the response type that is going to be returned when doing requests.
*
* @param string One of json, png.
* @return $this
* @param string
* @return PveClientBase
*/
public function setResponseType($type = 'json')
{
Expand All @@ -151,7 +158,7 @@ public function setResponseType($type = 'json')
/**
* Returns the response type that is being used by the Proxmox API client.
*
* @return string Response type being used.
* @return string
*/
public function getResponseType()
{
Expand All @@ -161,8 +168,8 @@ public function getResponseType()
/**
* Sets the debug level value 0 - nothing 1 - Url and method 2 - Url and method and result
*
* @param string $debugLevel One of json, png.
* @return $this
* @param int $debugLevel One of json, png.
* @return PveClientBase
*/
public function setDebugLevel($debugLevel)
{
Expand All @@ -173,13 +180,35 @@ public function setDebugLevel($debugLevel)
/**
* Returns debug level.
*
* @return string Response type being used.
* @return int debug level used.
*/
public function getDebugLevel()
{
return $this->debugLevel;
}

/**
* Sets the Verify Certificate
*
* @param bool $verifyCertificate
* @return PveClientBase
*/
public function setVerifyCertificate($verifyCertificate)
{
$this->verifyCertificate = $verifyCertificate;
return $this;
}

/**
* Returns verify Certificate.
*
* @return bool Verify Certificate.
*/
public function getVerifyCertificate()
{
return $this->verifyCertificate;
}

/**
* Return Api Token
*
Expand All @@ -194,7 +223,7 @@ public function getApiToken()
* Set Api Token format USER@REALM!TOKENID=UUID
*
* @param type string $apiToken
* @return $this
* @return PveClientBase
*/
public function setApiToken($apiToken)
{
Expand Down Expand Up @@ -315,9 +344,7 @@ private function executeAction($resource, $method, $parameters = [])

//fix bool value
$params = array_map(function ($value) {
return is_bool($value)
? ($value ? 1 : 0)
: $value;
return is_bool($value) ? ($value ? 1 : 0) : $value;
}, $params);

if ($this->getDebugLevel() >= 1) {
Expand Down Expand Up @@ -370,15 +397,11 @@ private function executeAction($resource, $method, $parameters = [])
curl_setopt($prox_ch, CURLOPT_HEADER, true);
curl_setopt($prox_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($prox_ch, CURLOPT_COOKIE, "PVEAuthCookie=" . $this->ticketPVEAuthCookie);
curl_setopt($prox_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($prox_ch, CURLOPT_SSL_VERIFYPEER, $this->verifyCertificate);
curl_setopt($prox_ch, CURLOPT_SSL_VERIFYHOST, false);

/**
* Set the Timeout for the Request in curl | CURLOPT_TIMEOUT sets the maximum execution time of the cUrl function.
*/
$timeout = $this->timeout;
if($timeout != 0) {
curl_setopt($prox_ch, CURLOPT_TIMEOUT, $timeout);
if ($this->timeout != 0) {
curl_setopt($prox_ch, CURLOPT_TIMEOUT, $this->timeout);
}

if (isset($this->ticketPVEAuthCookie)) {
Expand Down

0 comments on commit 0cdf09d

Please sign in to comment.