Skip to content

Commit eb0ece3

Browse files
author
Nathan Sullivan
committed
revised optional port attribute patch - PR#10
1 parent e4d7297 commit eb0ece3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

pve2_api.class.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,30 @@ class PVE2_API {
3232
protected $username;
3333
protected $realm;
3434
protected $password;
35+
protected $port;
3536

3637
protected $login_ticket = null;
3738
protected $login_ticket_timestamp = null;
3839
protected $cluster_node_list = null;
3940

40-
public function __construct ($hostname, $username, $realm, $password) {
41-
if (empty($hostname) || empty($username) || empty($realm) || empty($password)) {
42-
throw new PVE2_Exception("Hostname/Username/Realm/Password required for PVE2_API object constructor.", 1);
41+
public function __construct ($hostname, $username, $realm, $password, $port = 8006) {
42+
if (empty($hostname) || empty($username) || empty($realm) || empty($password) || empty($port)) {
43+
throw new PVE2_Exception("Hostname/Username/Realm/Password/Port required for PVE2_API object constructor.", 1);
4344
}
4445
// Check hostname resolves.
4546
if (gethostbyname($hostname) == $hostname && !filter_var($hostname, FILTER_VALIDATE_IP)) {
4647
throw new PVE2_Exception("Cannot resolve {$hostname}.", 2);
4748
}
49+
// Check port is between 1 and 65535.
50+
if (!is_int($port) || $port < 1 || $port > 65535) {
51+
throw new PVE2_Exception("Port must be an integer between 1 and 65535.", 6);
52+
}
4853

4954
$this->hostname = $hostname;
5055
$this->username = $username;
51-
$this->realm = $realm;
56+
$this->realm = $realm;
5257
$this->password = $password;
58+
$this->port = $port;
5359
}
5460

5561
/*
@@ -68,7 +74,7 @@ public function login () {
6874

6975
// Perform login request.
7076
$prox_ch = curl_init();
71-
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:8006/api2/json/access/ticket");
77+
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:{$this->port}/api2/json/access/ticket");
7278
curl_setopt($prox_ch, CURLOPT_POST, true);
7379
curl_setopt($prox_ch, CURLOPT_RETURNTRANSFER, true);
7480
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $login_postfields_string);
@@ -140,7 +146,7 @@ private function action ($action_path, $http_method, $put_post_parameters = null
140146

141147
// Prepare cURL resource.
142148
$prox_ch = curl_init();
143-
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:8006/api2/json{$action_path}");
149+
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:{$this->port}/api2/json{$action_path}");
144150

145151
$put_post_http_headers = array();
146152
$put_post_http_headers[] = "CSRFPreventionToken: {$this->login_ticket['CSRFPreventionToken']}";

0 commit comments

Comments
 (0)