-
Notifications
You must be signed in to change notification settings - Fork 2
/
p360monitoring.php
232 lines (190 loc) · 7.45 KB
/
p360monitoring.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
// Copyright 2024. WebPros International GmbH. All rights reserved.
if (!defined('WHMCS')) {
die('This file cannot be accessed directly');
}
require __DIR__ . '/vendor/autoload.php';
use WHMCS\Module\Server\Plesk360Monitoring\KaApi;
use WHMCS\Module\Server\Plesk360Monitoring\Logger;
use WHMCS\Module\Server\Plesk360Monitoring\ServerOptions;
use WHMCS\Module\Server\Plesk360Monitoring\ServiceProperties;
use WHMCS\Module\Server\Plesk360Monitoring\PlanCollection;
use WHMCS\Module\Server\Plesk360Monitoring\Plans\ProPlan;
use WHMCS\Module\Server\Plesk360Monitoring\ProductOptions;
use WHMCS\Module\Server\Plesk360Monitoring\Translator;
use WHMCS\Module\Server\Plesk360Monitoring\UrlHelper;
function p360monitoring_getKaApiClient(array $params): KaApi
{
return new KaApi(
$params[ServerOptions::SERVER_SCHEME],
$params[ServerOptions::SERVER_HOST],
(int)$params[ServerOptions::SERVER_PORT],
$params[ServerOptions::SERVER_USERNAME],
$params[ServerOptions::SERVER_PASSWORD]
);
}
function p360monitoring_MetaData(): array
{
return [
'DisplayName' => '360 Monitoring',
'APIVersion' => '1.1',
'RequiresServer' => true,
'ServiceSingleSignOnLabel' => false,
];
}
function p360monitoring_ConfigOptions(): array
{
global $CONFIG;
$plans = new PlanCollection();
$planOptions = [];
foreach ($plans->getAll() as $plan) {
$planOptions[$plan->getId()] = $plan->getName();
}
$proPlan = new ProPlan();
$translator = Translator::getInstance($CONFIG);
return [
ProductOptions::PLAN_ID => [
'FriendlyName' => $translator->translate('p360monitoring_label_plan'),
'Type' => 'dropdown',
'Size' => '25',
'Options' => $planOptions,
'Default' => $proPlan->getId(),
'SimpleMode' => true,
],
ProductOptions::DOMAIN => [
'FriendlyName' => $translator->translate('p360monitoring_label_domain'),
'Type' => 'text',
'Size' => '25',
'Default' => '',
'SimpleMode' => true,
],
ProductOptions::SERVERS => [
'FriendlyName' => $translator->translate('p360monitoring_label_additional_servers'),
'Type' => 'text',
'Size' => '25',
'Default' => '',
'SimpleMode' => true,
],
ProductOptions::WEBSITES => [
'FriendlyName' => $translator->translate('p360monitoring_label_additional_websites'),
'Type' => 'text',
'Size' => '25',
'Default' => '',
'SimpleMode' => true,
],
];
}
function p360monitoring_ClientArea(array $params): string
{
global $CONFIG;
$kaApi = p360monitoring_getKaApiClient($params);
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$translator = Translator::getInstance($CONFIG);
try {
$license = $kaApi->retrieveLicense($keyId);
$domain = $params[ProductOptions::DOMAIN];
$activationUrl = UrlHelper::getActivationUrl($license, $domain);
$dashboardUrl = UrlHelper::getDashboardUrl($domain);
$html = '';
if ($license->getActivationInfo()->isActivated()) {
$html .= '<div class="tab-content"><div class="row"><div class="col-sm-3 text-left">' . $translator->translate('p360monitoring_button_license_activated') . '</div></div></div><br/>';
} elseif (!$license->isTerminated() && !$license->isSuspended()) {
$html .= '<div class="tab-content"><a class="btn btn-block btn-info" href="' . $activationUrl . '" target="_blank">' . $translator->translate('p360monitoring_button_activate_license') . '</a></div><br/>';
}
$html .= '<div class="tab-content"><a class="btn btn-block btn-default" href="' . $dashboardUrl . '" target="_blank">' . $translator->translate('p360monitoring_button_dashboard') . '</a></div><br/>';
return $html;
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_CreateAccount(array $params): string
{
try {
$servers = ProductOptions::serverAllowance($params);
$websites = ProductOptions::websiteAllowance($params);
$plans = new PlanCollection();
$plan = $plans->getPlanById($params[ProductOptions::PLAN_ID]);
$kaApi = p360monitoring_getKaApiClient($params);
$license = $kaApi->createLicense($plan, $servers, $websites);
$domain = $params[ProductOptions::DOMAIN];
$params['model']->serviceProperties->save([
ServiceProperties::KEY_ID => $license->getKeyIdentifiers()->getKeyId(),
ServiceProperties::ACTIVATION_CODE => $license->getKeyIdentifiers()->getActivationCode(),
ServiceProperties::ACTIVATION_URL => UrlHelper::getActivationUrl($license, $domain),
]);
return 'success';
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_SuspendAccount(array $params): string
{
try {
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);
$kaApi->suspendLicense($keyId);
return 'success';
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_UnsuspendAccount(array $params): string
{
try {
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);
$kaApi->resumeLicense($keyId);
return 'success';
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_TerminateAccount(array $params): string
{
try {
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);
$kaApi->terminateLicense($keyId);
return 'success';
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_ChangePackage(array $params): string
{
try {
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$servers = ProductOptions::serverAllowance($params);
$websites = ProductOptions::websiteAllowance($params);
$plans = new PlanCollection();
$plan = $plans->getPlanById($params[ProductOptions::PLAN_ID]);
$kaApi = p360monitoring_getKaApiClient($params);
$kaApi->modifyLicense($keyId, $plan, $servers, $websites);
return 'success';
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return $exception->getMessage();
}
}
function p360monitoring_TestConnection(array $params): array
{
try {
$kaApi = p360monitoring_getKaApiClient($params);
$kaApi->testConnection();
return [
'success' => true,
'error' => '',
];
} catch (Throwable $exception) {
Logger::error(__FUNCTION__, $params, $exception);
return [
'success' => false,
'error' => $exception->getMessage(),
];
}
}