Skip to content

Commit 8de8681

Browse files
authored
Update Client.php
This resolved an issue where WHMCS hangs on 'Attempting to connect' when adding a server. This was fixed by adding in $this->params = new \stdClass;
1 parent 1b2e701 commit 8de8681

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

modules/servers/AMP/lib/Client.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ class Client
99
private $params;
1010
public $version;
1111
public function __construct(array $params)
12-
{
12+
{
1313
$this->version = '2.0.6.0';
14-
$endpoint = (!empty($params['serverhostname']) ? $params['serverhostname'] : $params['serverip']);
15-
$this->params->serverId = $params['serverid'];
14+
$this->params = new \stdClass;
15+
$endpoint = (!empty($params['serverhostname']) ? $params['serverhostname'] : $params['serverip']);
16+
$this->params->serverId = $params['serverid'];
1617
$this->params->endpoint = ( !empty($params['serversecure']) ? 'https' : 'http').'://'.$endpoint.( !empty($params['serverport'] && $params['serversecure'] != true) ? ':'.$params['serverport'] : '');
1718
$this->params->username = $params['serverusername'];
1819
$this->params->password = $params['serverpassword'];
1920

2021
if(!empty($params['serverid']))
2122
{
2223
$session = Capsule::table('ampSessions')->where('serverId', $params['serverid'])->value('sessionId');
23-
}
24+
}
2425
$this->params->sessionId = !empty($session) ? $session : $this->getSessionId();
2526
}
2627

@@ -35,7 +36,7 @@ private function getSessionId()
3536
curl_setopt($ch, CURLOPT_URL, $this->params->endpoint . '/API/Core/Login');
3637
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
3738
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
38-
39+
3940
$data = [
4041
'username' => $this->params->username,
4142
'password' => $this->params->password,
@@ -55,7 +56,7 @@ private function getSessionId()
5556

5657
$response = curl_exec($ch);
5758
$decoded = json_decode($response, 1);
58-
59+
5960
if(empty($decoded))
6061
{
6162
throw new \Exception('Unknown response');
@@ -66,19 +67,19 @@ private function getSessionId()
6667
throw new \Exception($decoded['Message']);
6768
}
6869

69-
70+
7071
if(!empty($decoded['result']['Reason']) && !$decoded['result']['Status'])
7172
{
7273
throw new \Exception($decoded['result']['Reason']);
7374
}
7475

7576
curl_close($ch);
76-
77+
7778
if(!empty($this->params->serverId) && !empty($decoded['sessionID']))
7879
{
7980
Capsule::table('ampSessions')->updateOrInsert(['serverId' => $this->params->serverId], ['sessionId' => $decoded['sessionID']]);
8081
}
81-
82+
8283
return $decoded['sessionID'];
8384
}
8485

@@ -90,7 +91,7 @@ public function call(string $query, array $data = [])
9091
curl_setopt($ch, CURLOPT_URL, $this->params->endpoint . '/API/'. $query);
9192
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
9293
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
93-
94+
9495
if (!empty($data)) {
9596
$post = json_encode($data);
9697
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
@@ -107,7 +108,7 @@ public function call(string $query, array $data = [])
107108
logModuleCall("AMP", $query, $post, $response);
108109
$response = curl_exec($ch);
109110
$decoded = json_decode($response, 1);
110-
111+
111112
if($decoded['Title'] == 'Unauthorized Access' || $decoded['result']['Status'] == false)
112113
{
113114
$data['SESSIONID'] = $this->getSessionId();
@@ -116,7 +117,7 @@ public function call(string $query, array $data = [])
116117
$headers[3] = 'Content-length:'.strlen($post);
117118
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
118119
$response = curl_exec($ch);
119-
$decoded = json_decode($response, 1);
120+
$decoded = json_decode($response, 1);
120121
}
121122
curl_close($ch);
122123

@@ -134,7 +135,7 @@ public function call(string $query, array $data = [])
134135
{
135136
throw new \Exception($decoded['result']['Reason']);
136137
}
137-
138+
138139
return $decoded;
139140
}
140-
}
141+
}

0 commit comments

Comments
 (0)