-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.php
483 lines (425 loc) · 14.9 KB
/
functions.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
// Check for server single sign-on in progress
if (!empty($_GET['CometSSO'])) {
performServerLogin($_GET['CometSSO']);
}
/**
* Obtain a cURL handle given a URL, POST data and optional extra options
*
* @param $URL
* @param $data
* @param array $extra_opts
* @return resource
*/
function getCurlHandle($URL, $data, $extra_opts = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 400); // Timeout in seconds
foreach ($extra_opts as $opt => $value) {
curl_setopt($ch, $opt, $value);
}
return $ch;
}
/**
* Send a request to the given API endpoint on the server.
*
* @param array $params
* @param array $data API parameters to supply
* @param string $endPoint Admin API to use
* @param bool $assoc (Optional) Return associative array instead of object
* @return array|object|string
*/
function performAPIRequest($params, $data, $endPoint, $assoc = true) {
$baseParams = [
'Username' => $params['serverusername'],
'AuthType' => 'Password',
'Password' => $params['serverpassword'],
];
$query = http_build_query($baseParams + $data);
$ch = getCurlHandle(
getHost($params) . '/api/v1/admin/' . $endPoint,
$query,
[CURLOPT_FOLLOWLOCATION => true]
);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return ['curlerror' => $error];
} else {
return json_decode($response, $assoc);
}
}
/**
* Stream a client software download.
*
* @param array $params
* @param array $data Request data
* @param string $action API endpoint
* @return string
*/
function softwareDownload($params, $data, $action) {
$baseParams = [
'Username' => $params['serverusername'],
'AuthType' => 'Password',
'Password' => $params['serverpassword'],
];
$query = http_build_query($baseParams + $data);
$ch = getCurlHandle(
getHost($params) . '/api/v1/admin/' . $action,
$query,
[CURLOPT_FOLLOWLOCATION => true]
);
// Streaming download
return curl_exec($ch);
}
/**
* Format bytes count into human-readable form.
*
* @param $bytes
* @return string
*/
function formatBytes($bytes) {
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} else if ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} else if ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} else if ($bytes > 1) {
return $bytes . ' bytes';
} else if ($bytes == 1) {
return $bytes . ' byte';
} else {
return '0 bytes';
}
}
/**
* Convert Comet job classification code to human readable string
*
* @param int $code
* @return string
*/
function formatJobType($code) {
switch ($code) {
case 4001:
return 'Backup';
case 4002:
return 'Restore';
case 4003:
return 'Cleanup';
case 4004:
return 'Unlock';
case 4005:
return 'Delete';
case 4006:
return 'Measure';
case 4007:
return 'Update';
case 4008:
return 'Import';
case 4009:
return 'Reindex';
case 4010:
return 'Verify';
default:
return 'Unknown';
}
}
/**
* Convert Comet status code to human readable form.
*
* @param $code
* @return string
*/
function formatStatusType($code) {
if ($code >= 5000 && $code <= 5999) {
return 'Success';
} else if ($code >= 6000 && $code <= 6999) {
return 'Running';
} else if ($code >= 7000 && $code <= 7999) {
return 'Error';
} else {
return 'Unknown';
}
}
/**
* Escape html entities (safe for use within `<tag properties="">' as well)
* n.b. ENT_SUBSTITUTE means PHP 5.4.0+
*
* @param string $str Raw text
* @return string HTML
*/
function hesc($str) {
return @htmlentities($str, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* Outputs API response message or a generic error message containing encoded response details if no API response message present
*
* @param array $response
* @param bool $noHTML
* @return string
*/
function handleErrorResponse($response, $noHTML = false) {
if (isset($response['Message'])) {
return $response['Message'];
} else {
return 'An error has occurred - please contact support. ' . ($noHTML ? ' Error detail: ' : '<br> <span style="color:darkred;"> Error detail:</span>') . base64_encode(var_export($response, true));
}
}
/**
* Sets the suspension state of an account to the provided value
*
* @param array $params
* @param bool $suspended
* @return string
*/
function modifyAccountSuspensionState($params, $suspended) {
$baseRequestData = [
'TargetUser' => getUsernameFromParams($params)
];
$profile = performAPIRequest(
$params,
$baseRequestData,
'get-user-profile-and-hash',
false
);
if (is_object($profile) && property_exists($profile, 'ProfileHash')) {
$profileData = $profile->Profile;
$profileData->IsSuspended = $suspended; // Modify suspension state value
$response = performAPIRequest(
$params,
$baseRequestData + [
'ProfileData' => json_encode($profileData),
'RequireHash' => $profile->ProfileHash
],
'set-user-profile-hash'
);
if (array_key_exists('Status', $response) && $response['Status'] === 200) {
return 'success';
} else {
return handleErrorResponse($response, true);
}
} else {
return "Error Fetching User.";
}
}
/**
* Checks for the existence of a policy group on the Comet server and creates it if necessary.
*
* @param array $params
* @param string $policyGroupGUID
*/
function maybeCreatePolicyGroup($params, $policyGroupGUID) {
$baseRequestData = [
'PolicyID' => $policyGroupGUID
];
$policy = performAPIRequest(
$params,
$baseRequestData,
'policies/get'
);
if (empty($policy['PolicyHash'])) {
performAPIRequest(
$params,
$baseRequestData + [
'Policy' => json_encode([
'Description' => $policyGroupGUID,
'Policy' => [
'PreventChangeAccountPassword' => true,
'ModeAdminResetPassword' => 3,
'PreventDeleteStorageVault' => true,
'PreventAddCustomStorageVault' => true,
'PreventRequestStorageVault' => true,
'StorageVaultProviders' => [
'AllowedProvidersWhenRestricted' => [1003],
'ShouldRestrictProviderList' => true
],
'ProtectedItemEngineTypes' => [
'AllowedEngineTypeWhenRestricted' => [],
'ShouldRestrictEngineTypeList' => false
],
]
])
],
'policies/set'
);
}
}
/**
* Retrieves a user profile and updates it to reflect the currently configured account restrictions in WHMCS.
*
* @param array $params
* @return string
*/
function applyRestrictions($params) {
// Prepare base API request params
$baseRequestData = [
'TargetUser' => getUsernameFromParams($params),
];
// Retrieve profile content for modification
$profile = performAPIRequest($params, $baseRequestData, 'get-user-profile-and-hash', false);
// Sanity check
if (is_object($profile) && property_exists($profile, 'ProfileHash')) {
$profileData = $profile->Profile;
// Apply storage vault quota if set
if (
!empty($params['configoptions']['storage_vault_quota_gb']) &&
!empty($profileData->Destinations)
) {
foreach (array_keys((array)$profileData->Destinations) as $destinationGUID) {
$profileData->Destinations->$destinationGUID->StorageLimitEnabled = true;
$profileData->Destinations->$destinationGUID->StorageLimitBytes = intval($params['configoptions']['storage_vault_quota_gb']) * pow(1024, 3);
}
} else if (
!empty($params['configoptions']['storage_vault_quota_tb']) &&
!empty($profileData->Destinations)
) {
foreach (array_keys((array)$profileData->Destinations) as $destinationGUID) {
$profileData->Destinations->$destinationGUID->StorageLimitEnabled = true;
$profileData->Destinations->$destinationGUID->StorageLimitBytes = intval($params['configoptions']['storage_vault_quota_tb']) * pow(1024, 4);
}
}
// Apply protected items quota if set
if (!empty($params['configoptions']['protected_item_quota_gb'])) {
$profileData->AllProtectedItemsQuotaEnabled = true;
$profileData->AllProtectedItemsQuotaBytes = intval($params['configoptions']['protected_item_quota_gb']) * pow(1024, 3);
} else if (!empty($params['configoptions']['protected_item_quota_tb'])) {
$profileData->AllProtectedItemsQuotaEnabled = true;
$profileData->AllProtectedItemsQuotaBytes = intval($params['configoptions']['protected_item_quota_tb']) * pow(1024, 4);
} else {
$profileData->AllProtectedItemsQuotaEnabled = false;
$profileData->AllProtectedItemsQuotaBytes = 0;
}
// Apply device quota if set
$profileData->MaximumDevices = intval((empty($params['configoptions']['number_of_devices']) ? 0 : intval($params['configoptions']['number_of_devices'])));
// Apply policy group if set
$profileData->PolicyID = (empty($params['configoption1']) ? '' : $params['configoption1']);
// Prepare request
$updateProfileRequestData = $baseRequestData + [
'ProfileData' => json_encode($profileData),
'RequireHash' => $profile->ProfileHash
];
// Update user
$response = performAPIRequest($params, $updateProfileRequestData, 'set-user-profile-hash');
if (isset($response['Status']) && $response['Status'] == 200) {
return 'success';
} else {
return 'Account creation succeeded, however an error occurred during configuration - please check to confirm whether account details are correct. Error detail: ' . base64_encode(var_export($response, true));
}
} else {
return 'Couldn\'t retrieve profile.';
}
}
/**
* Produce a usable server hostname
*
* @param array $params
* @return string
*/
function getHost($params) {
$hostname = preg_replace(["^http://^i", "^https://^i", "^/^"], "", $params['serverhostname']);
// Compatibility with servers configured under older versions of the module / WHMCS
$port = '';
$extractPort = [];
preg_match('/:([0-9]+)$/', $hostname, $extractPort);
// Use port from hostname if set (compatibility)
if (!empty($extractPort[1]) && is_numeric($extractPort[1])) {
$port = $extractPort[1];
$hostname = preg_replace(["/:[0-9]+/"], "", $hostname);
// Use port from serverport if set (standard)
} else if (!empty($params['serverport'])) {
$port = $params['serverport'];
}
return $params['serverhttpprefix'] . '://' . $hostname . (!empty($port) ? ':' . intval($port) : '');
}
/**
* Inject some javascript into the page to perform single sign-on and then die.
*
* @param string $requestData Base64-encoded JSON object
*/
function performServerLogin($requestData) {
$data = json_decode(base64_decode($requestData), true);
?>
<div style="height:100vh;width:100vw;position:fixed;top:0;left:0;background:#555;line-height:100vh;text-align:center;color:#FFF;font-family:Arial;">
<h1>Loading...</h1>
</div>
<script type="text/javascript">
(function() {
var TARGET_URI = <?= json_encode($data['Server']) ?>;
var SESSIONKEY = <?= json_encode($data['SessionKey']) ?>;
var USERNAME = <?= json_encode($data['TargetUser']) ?>;
window.addEventListener('message', function(msg) {
wnd.postMessage({
"msg": "session_login",
"username": USERNAME,
"sessionkey": SESSIONKEY
},
TARGET_URI
);
window.close();
}, TARGET_URI);
var wnd = window.open(TARGET_URI);
})();
</script>
<?php
die();
}
/**
* Retrieve the client's username, selecting from service params or product custom fields
* depending on whether a custom field is present.
*
* @param array $params
* @return string
*/
function getUsernameFromParams($params) {
$username = false;
// Check for a product custom field
if (getIsUsingCustomUsernameFromParams($params)) {
$username = $params['customfields']['Username'];
// If no custom field, use the service value
} else {
$username = $params['username'];
}
return $username;
}
/**
* Check whether a custom username is present.
*
* @param array $params
* @return bool
*/
function getIsUsingCustomUsernameFromParams($params) {
return !empty(trim($params['customfields']['Username']));
}
/**
* Retrieve the client's password, selecting from service params or product custom fields
* depending on whether a custom field is present.
*
* @param array $params
* @return string
*/
function getPasswordFromParams($params) {
$password = false;
// Check for a product custom field
if (getIsUsingCustomPasswordFromParams($params)) {
$password = $params['customfields']['Password'];
// If no custom field, use the service value
} else {
$password = $params['password'];
}
return $password;
}
/**
* Check whether a custom password is present.
*
* @param array $params
* @return bool
*/
function getIsUsingCustomPasswordFromParams($params) {
return !empty($params['customfields']['Password']);
}