Skip to content

Commit 576e3d7

Browse files
authored
Merge pull request #38 from zeniar-au/main
Bug Fixes & Improvements
2 parents f6a2c91 + 8de8681 commit 576e3d7

File tree

3 files changed

+85
-59
lines changed

3 files changed

+85
-59
lines changed

modules/servers/AMP/hooks.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@
22

33
use WHMCS\Database\Capsule;
44

5-
add_hook('EmailPreSend', 1, function($vars) {
6-
if($vars['messageid'] == 'AMP Welcome Email' && $vars['relid'])
5+
add_hook('EmailPreSend', 2, function($vars) {
6+
if($vars['relid'])
77
{
88
$relid = $vars['relid'];
99

10-
$service = Capsule::table('ampServices')->where('serviceId', $relid)->first();
10+
$service = Capsule::table('ampServices')->where('serviceId', $relid)->first();
1111
$serverId = Capsule::table('tblhosting')->where('id', $relid)->value('server');
1212
$server = Capsule::table('tblservers')->where('id', $serverId)->first();
13-
13+
1414
$endpoint = (!empty($server->hostname) ? $server->hostname: $server->ipaddress);
1515
$endpoint = (!empty($server->secure) ? 'https://' : 'http://' ). $endpoint ;
16-
16+
1717
$endpoint = $endpoint . ((!empty($server->port) && $server->secure != true ) ? ':'.$params['serverport'] : '');
18-
18+
1919
$merge_fields = [];
2020
$merge_fields['ampEndpoints'] = json_decode($service->endpoints, 1);
2121
$merge_fields['ampApplicationUrl'] = $endpoint . '/?instance='. $service->instanceId;
2222
$merge_fields['ampInstanceId'] = $service->instanceId;
23-
23+
24+
// Fetch the instance ID from the database
25+
$instanceId = Capsule::table('ampServices')->where('serviceid', $vars['relid'])->value('instanceid');
26+
27+
// If instance ID exists, add it to the template variables
28+
if($instanceId) {
29+
$merge_fields['service_instance_id'] = $instanceId;
30+
}
31+
2432
return $merge_fields;
2533
}
2634
});
2735

36+
37+
2838
if($_REQUEST['ampCallback'] == 1)
2939
{
3040
$data = json_decode(file_get_contents('php://input'), 1);
@@ -34,33 +44,34 @@
3444
if($data['Action'] == 'Create')
3545
{
3646
Capsule::table('ampServices')->updateOrInsert(['secret' => $data['Secret'], 'instanceId' => ''], ['instanceId' => $data['InstanceId'], 'targetId' => $data['TargetId'], 'endpoints' => json_encode($data['Endpoints'])]);
37-
38-
$serviceId = Capsule::table('ampServices')->where('secret', $data['Secret'])->value('serviceId');
39-
47+
48+
$serviceId = Capsule::table('ampServices')->where('secret', $data['Secret'])->value('serviceId');
49+
4050
if(!empty($serviceId))
4151
{
4252
$command = 'SendEmail';
4353
$postData = array(
54+
//The below sets which template should be sent once the Instance is provisioned
4455
'messagename' => 'AMP Welcome Email',
4556
'id' => $serviceId
4657
);
4758
localAPI($command, $postData);
48-
59+
4960
Capsule::table('ampTasks')->where('serviceId', $serviceId)->delete();
5061
}
5162
}
5263
elseif($data['Action'] == 'Modify')
5364
{
5465
Capsule::table('ampServices')->where('secret', $data['Secret'])->where('instanceId', $data['InstanceId'])->update(
5566
['endpoints' => json_encode($data['Endpoints'])]
56-
);
57-
67+
);
68+
5869
}
59-
70+
6071

6172
}elseif($data['Success'] == false && $data['Secret'] && $data['Action'] == 'Create')
6273
{
63-
$serviceId = Capsule::table('ampServices')->where('secret', $data['Secret'])->value('serviceId');
74+
$serviceId = Capsule::table('ampServices')->where('secret', $data['Secret'])->value('serviceId');
6475
if(!empty($serviceId))
6576
{
6677
$task = Capsule::table('ampTasks')->where('serviceId', $serviceId)->first();
@@ -104,12 +115,12 @@
104115
<label for="website">Value</label>
105116
<input class="form-control" type="text" name="extraProvisionSettingsValue" id="extraProvisionSettingsValue" />
106117
</div>
107-
118+
108119
<button class="btn btn-success">Add</button>
109-
120+
110121
</div>
111-
112-
122+
123+
113124
</form>
114125
<table id="extraProvisionSettingsTable" class="datatable">
115126
<thead>
@@ -145,17 +156,17 @@
145156
clone = $('#extraProvisionSettingsMain').clone();
146157
$('#extraProvisionSettingsMain').remove();
147158
148-
function tableToJson(table) {
159+
function tableToJson(table) {
149160
var data = [];
150-
for (var i=1; i<table.rows.length; i++) {
151-
var tableRow = table.rows[i];
152-
var rowData = [];
153-
for (var j=0; j<tableRow.cells.length - 1; j++) {
161+
for (var i=1; i<table.rows.length; i++) {
162+
var tableRow = table.rows[i];
163+
var rowData = [];
164+
for (var j=0; j<tableRow.cells.length - 1; j++) {
154165
rowData.push(tableRow.cells[j].innerHTML);
155-
}
156-
data.push(rowData);
157-
}
158-
return data;
166+
}
167+
data.push(rowData);
168+
}
169+
return data;
159170
}
160171
161172
function onAddWebsite(e) {
@@ -186,4 +197,4 @@ function onDeleteRow(e) {
186197
}
187198
</script>
188199
HTML;
189-
});
200+
});

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+
}

modules/servers/AMP/templates/overview.tpl

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,50 @@
2929

3030

3131
<div class="panel panel-primary">
32-
<div class="panel-heading">Application URL</div>
32+
<div class="panel-heading"><h4>Game Server Control Panel</h4></div>
3333
<div class="panel-body">
34+
<p>You can access the link below to log in and manage your Game Server, which includes controlling settings, plugins, mods, backups and much more.</p>
35+
<p><b>Tip:</b> Make sure your Instance is Runnings before trying to access this link</p>
36+
<button style="margin-left: 16px; " onclick="window.open('{$appUrl}/?instance={$instanceId}', '_blank')" class="btn btn-default">Open Game Server Control Panel</button>
37+
<p>&nbsp;</p>
38+
<p>Alternatively you can Bookmark the below link for quick refference</p>
3439
<a href="{$appUrl}/?instance={$instanceId}" target="_blank">{$appUrl}/?instance={$instanceId}</a>
3540
</div>
3641
</div>
3742

38-
{if !empty($endpoints) }
43+
{if !empty($endpoints)}
3944
<div class="panel panel-primary">
40-
<div class="panel-heading">Endpoints</div>
45+
<div class="panel-heading"><h4>How to connect to your game</h4></div>
4146
<div class="panel-body" >
4247

4348
{foreach $endpoints as $e}
44-
<h4>{$e['DisplayName']}</h4>{if !empty($e['Uri'])}
45-
<a target="_blank" href="{$e['Uri']}" style="vertical-align: middle;" target="_blank">{$e['Endpoint']}</a>
46-
{else}{$e['Endpoint']}{/if}
47-
48-
<button style="margin-left: 16px;" onclick="copyToClipboard('{$e['Endpoint']}')" class="btn btn-default" >Copy to clipboard</button>
49-
50-
<hr>
51-
49+
<h6>{$e['DisplayName']}</h6>
50+
{assign var='cleanAppUrl' value=$appUrl|replace:'https://':''|replace:'http://':''}
51+
{if $e['Endpoint']}
52+
{assign var='parts' value=":"|explode:$e['Endpoint']}
53+
<a target="_blank" href="{$cleanAppUrl}:{$parts[1]}" style="vertical-align: middle;" target="_blank">{$cleanAppUrl}:{$parts[1]}</a>
54+
{else}
55+
{$e['Endpoint']}
56+
{/if}
57+
58+
<button style="margin-left: 16px;" onclick="copyToClipboard('{$cleanAppUrl}:{$parts[1]}')" class="btn btn-default">Copy to clipboard</button>
59+
60+
<hr>
5261
{/foreach}
62+
63+
</div>
5364
</div>
54-
55-
</div>
5665
{/if}
66+
67+
68+
69+
70+
5771
<div class="panel panel-default">
58-
<div class="panel-heading">Application Management</div>
72+
<div class="panel-heading"><h4>Application Management</h4></div>
5973
<div class="panel-body">
6074

61-
<h2>Status: <span id="status"></span></h2>
75+
<h5>Status: <span id="status"></span></h5>
6276
<button id="start" class="btn btn-default" data-toggle="modal" data-target="#confirmModal">Start Instance</button>
6377
<button id="stop" class="btn btn-default" data-toggle="modal" data-target="#confirmModal">Stop Instance</button>
6478
<button id="restart" class="btn btn-default" data-toggle="modal" data-target="#confirmModal">Restart Instance</button>
@@ -93,7 +107,7 @@ top:40px;
93107
right: 50px;
94108
display: inline-block;
95109
width: 50px;
96-
height: 50px;
110+
height: 20px;
97111
border: 3px solid rgba(255,255,255,.3);
98112
border-radius: 50%;
99113
border-top-color: rgb(0, 0, 0);

0 commit comments

Comments
 (0)