-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenprovider_plesk_license.php
More file actions
735 lines (581 loc) · 23.5 KB
/
Copy pathopenprovider_plesk_license.php
File metadata and controls
735 lines (581 loc) · 23.5 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
<?php
use WHMCS\Module\Server\OpenproviderPleskLicense\Helper;
use WHMCS\Database\Capsule;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
function openprovider_plesk_license_MetaData()
{
return array(
'DisplayName' => 'OpenProvider Plesk License',
'APIVersion' => '1.0', // Use API Version 1.1
'RequiresServer' => true, // Set true if module requires a server to work
);
}
function openprovider_plesk_license_ConfigOptions(array $params)
{
global $whmcs;
$helper = new Helper();
$pid = $whmcs->get_req_var("id");
$product_data = Capsule::table('tblproducts')->select()->where('id', $pid)->first();
$plesk_type = $product_data->configoption1;
$customfieldarray = [
'key_id' =>
[
'type' => 'product',
'fieldname' => 'key_id|Key Id',
'relid' => $pid,
'fieldtype' => 'text',
'description' => '',
'adminonly' => 'on',
'sortorder' => '1',
],
'ip_address' => [
'type' => 'product',
'fieldname' => 'ip_address|IP Address',
'relid' => $pid,
'fieldtype' => 'text',
'description' => 'Enter the IP address associated with the license',
'required' => 'on', // Makes it a required field
'showorder' => 'on', // Shows the field on the order form
'sortorder' => '2',
],
'comments' => [
'type' => 'product',
'fieldname' => 'comments|Comments',
'relid' => $pid,
'fieldtype' => 'textarea',
'description' => 'Enter the comments',
'showorder' => 'on', // Shows the field on the order form
'sortorder' => '3',
],
'title' => [
'type' => 'product',
'fieldname' => 'title|Title',
'relid' => $pid,
'fieldtype' => 'text',
'description' => 'Enter the title',
'showorder' => 'on', // Shows the field on the order form
'sortorder' => '4',
],
];
/** create the custom fields */
$helper->createPleskCustomFields($customfieldarray);
/** Get the API to fetch the license items */
$getLicenseItems = $helper->getPleskLicenseItems();
if($getLicenseItems['httpcode'] == 200)
{
$productFile = __DIR__ . '/plesk-products.json';
$getJsondata = file_get_contents($productFile);
$getJsondata = json_decode($getJsondata,true);
$getPleskItems = array_keys($getJsondata['plesk_products']);
foreach ($getLicenseItems['result']->data->results as $key => $result) {
if (in_array($result->title, $getPleskItems)) {
$options[$result->item] = $result->title;
/** Code to create the config groups and there options/suboptions */
$group_name = "{$result->item}|{$result->title}";
foreach ($getJsondata['plesk_products'][$result->title] as $category => $productOptions) {
// Generate a clean group key
$groupKey = strtolower(str_replace([' ', '&', '/'], '_', $category));
$groupKey = preg_replace('/[^a-z0-9_]/', '', $groupKey);
// Match product options with API data (store key => value)
$filteredProductData = array_reduce($getLicenseItems['result']->data->results, function ($resultdatac, $result) use ($productOptions) {
if (in_array($result->title, $productOptions, true)) {
$key = $result->item;
$resultdatac[$key] = $result->title; // Store item as key and title as value
}
return $resultdatac;
}, []);
// Check if there are multiple or single options
if (!empty($filteredProductData)) {
if (count($filteredProductData) > 1) {
// Multiple options: Create dropdown
$optionType = "1"; // Dropdown in WHMCS
$filteredProductData = ["no_option" => "No Option"] + $filteredProductData;
$friendlyName = "$category Options";
} else {
// Single option: Create checkbox
$optionType = "3"; // Checkbox in WHMCS
foreach ($filteredProductData as $key => $value) {
$filteredProductData[$key] = "$value | $category";
}
$friendlyName = reset($filteredProductData); // Get the first value
}
// Create Configurable Option Array
$configurableOptions = [
[
"name" => "$category",
"friendlyName" => $friendlyName,
"description" => "",
"optiontype" => $optionType,
"optionvalue" => $filteredProductData // Key => Value pairs
]
];
// Call WHMCS function to create the configurable options
$response = $helper->create_configurable_options($group_name, $pid, $configurableOptions);
}
}
/** End */
}
}
if($plesk_type != '')
{
$helper->updateConfigGroup($plesk_type,$pid);
}
}
return array(
'product_items' => array(
'FriendlyName' => 'Product Items',
'Type' => 'dropdown',
'Size' => '25',
'Options' => $options,
'Description' => '',
),
'ip_binding' => array(
'FriendlyName' => 'IP Binding',
'Type' => 'yesno',
'Description' => 'Tick to enable',
'Size' => '25',
),
'installation_command' => array(
'FriendlyName' => 'Installation Command',
'Type' => 'textarea',
'Description' => 'Please enter the each command to the next line',
'Rows' => '8',
'Cols' => '60'
),
);
}
/**
* Test connection with the given server parameters.
*
* Allows an admin user to verify that an API connection can be
* successfully made with the given configuration parameters for a
* server.
*
* When defined in a module, a Test Connection button will appear
* alongside the Server Type dropdown when adding or editing an
* existing server.
*/
function openprovider_plesk_license_TestConnection(array $params)
{
try {
$helper = new Helper($params);
$response = $helper->getPleskApiToken($params);
if($response['httpcode'] == 200){
$success = true;
}
else{
$errorMsg = $response['result']->message;
}
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
$success = false;
$errorMsg = $e->getMessage();
}
return array(
'success' => $success,
'error' => $errorMsg,
);
}
function openprovider_plesk_license_CreateAccount(array $params)
{
try {
global $whmcs;
$helper = new Helper();
/** Api to create the plesk license */
$helper->create_PleskEmailTemplate();
$serviceId = $params['serviceid'];
$pid = $params['pid'];
$keyId = $params['customfields']['key_id'];
$configOptionSelect = $params['configoption1'];
$configOptions = $params['configoptions'];
// print_r($configOptions);
$getconfigItms = $helper->getCheckedConfigItemsCheckDrop($serviceId,$configOptionSelect,$configOptions);
$itemsData = [];
$atttachData = [];
foreach ($getconfigItms as $item) {
if (strpos($item, 'ADD-') === 0 || strpos($item, 'CLNX-') === 0 || strpos($item, 'SOPHOS-') === 0) {
$atttachData[] = $item;
} else {
$itemsData[] = $item;
}
}
$postData = [
'attached_keys' => $atttachData,
'comment' => $params['customfields']['comments'] ?? '',
"ip_address_binding" => $params['customfields']['ip_address'],
"items" => $itemsData,
"period" => 1,
"restrict_ip_binding" => !empty($params['configoption2']) ? true : false,
'title' => $params['customfields']['title'] ?? ''
];
if(isset($keyId) && $keyId != '')
{
$emailKeyId = $keyId;
$postData['keyId'] = $keyId;
$getResponse = $helper->updatePleskLicense($postData,$keyId);
if($getResponse['httpcode'] == 200)
{
}else{
return $getResponse['result']->desc;
}
}else{
$getResponse = $helper->createPleskLicense($postData);
if($getResponse['httpcode'] == 200)
{
if (isset($getResponse['result']->data->key_id)) {
$keyId = $getResponse['result']->data->key_id;
$fields = ["key_id" => $keyId];
$emailKeyId = $keyId;
$helper->insert_plesk_custom_fields_value($serviceId, $pid, $fields);
}
}else{
return $getResponse['result']->desc;
}
}
/** create the HTML email */
$getLicense = $helper->getPleskLicenseDetail($emailKeyId);
$htmlContent = $helper->generatePleskLicenseEmailContent($getLicense);
$emailData = [
'messagename' => 'OpenProvider Plesk License Welcome Email',
'id' => $serviceId,
'customvars' => base64_encode(serialize(["license_details" => $htmlContent]))
];
$results = $helper->sendEmail($emailData);
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
return $e->getMessage();
}
return 'success';
}
/**
* Suspend an instance of a product/service.
*
* Called when a suspension is requested. This is invoked automatically by WHMCS
* when a product becomes overdue on payment or can be called manually by admin
* user.
*/
function openprovider_plesk_license_SuspendAccount(array $params)
{
try {
$serviceId = $params['serviceid'];
$pid = $params['pid'];
$keyId = $params['customfields']['key_id'];
$postData = [
"ip_address_binding" => $params['customfields']['ip_address'],
'suspended' => true
];
$helper = new Helper();
$getResponse = $helper->suspendUnsuspendPleskLicense($postData,$keyId);
if($getResponse['httpcode'] == 200)
{
return 'success';
}else{
return $getResponse['result']->desc;
}
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
return $e->getMessage();
}
}
/**
* Un-suspend instance of a product/service.
*
* Called when an un-suspension is requested. This is invoked
* automatically upon payment of an overdue invoice for a product, or
* can be called manually by admin user.
*/
function openprovider_plesk_license_UnsuspendAccount(array $params)
{
try {
$serviceId = $params['serviceid'];
$pid = $params['pid'];
$keyId = $params['customfields']['key_id'];
$postData = [
"ip_address_binding" => $params['customfields']['ip_address'],
'suspended' => false
];
$helper = new Helper();
$getResponse = $helper->suspendUnsuspendPleskLicense($postData,$keyId);
if($getResponse['httpcode'] == 200)
{
return 'success';
}else{
return $getResponse['result']->desc;
}
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
return $e->getMessage();
}
}
/**
* Terminate instance of a product/service.
*
* Called when a termination is requested. This can be invoked automatically for
* overdue products if enabled, or requested manually by an admin user.
*/
function openprovider_plesk_license_TerminateAccount(array $params)
{
try {
$serviceId = $params['serviceid'];
$pid = $params['pid'];
$keyId = $params['customfields']['key_id'];
$helper = new Helper();
$getResponse = $helper->deleteLicensePlesk($keyId);
if ($getResponse["httpcode"] == 200)
{
$fields = ["key_id" => ""];
$helper->insert_plesk_custom_fields_value($serviceId, $pid, $fields);
return "success";
}else {
return $getResponse['result']->desc;
}
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
return $e->getMessage();
}
}
/**
* Upgrade or downgrade an instance of a product/service.
*
* Called to apply any change in product assignment or parameters. It
* is called to provision upgrade or downgrade orders, as well as being
* able to be invoked manually by an admin user.
*/
function openprovider_plesk_license_ChangePackage(array $params)
{
try {
global $whmcs;
$helper = new Helper();
/** Api to update the plesk license */
$serviceId = $params['serviceid'];
$pid = $params['pid'];
$keyId = $params['customfields']['key_id'];
$configOptionSelect = $params['configoption1'];
$configOptions = $params['configoptions'];
$getconfigItms = $helper->getCheckedConfigItemsCheckDrop($serviceId,$configOptionSelect,$configOptions);
$itemsData = [];
$atttachData = [];
foreach ($getconfigItms as $item) {
if (strpos($item, 'ADD-') === 0 || strpos($item, 'CLNX-') === 0 || strpos($item, 'SOPHOS-') === 0) {
$atttachData[] = $item;
} else {
$itemsData[] = $item;
}
}
$postData = [
'attached_keys' => $atttachData,
'comment' => $params['customfields']['comments'] ?? '',
"ip_address_binding" => $params['customfields']['ip_address'],
"items" => $itemsData,
"period" => 1,
"restrict_ip_binding" => !empty($params['configoption2']) ? true : false,
'title' => $params['customfields']['title'] ?? ''
];
if(isset($keyId) && $keyId != '')
{
$emailKeyId = $keyId;
$postData['keyId'] = $keyId;
$getResponse = $helper->updatePleskLicense($postData,$keyId);
if($getResponse['httpcode'] == 200)
return 'success';
else
return $getResponse['result']->desc;
}else {
return 'Keyid Does not exist';
}
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
return $e->getMessage();
}
return 'success';
}
/**
* Admin services tab additional fields.
*
* Define additional rows and fields to be displayed in the admin area service
* information and management page within the clients profile.
*/
function openprovider_plesk_license_AdminServicesTabFields(array $params)
{
try {
global $CONFIG;
global $whmcs;
$helper = new Helper();
$assets = $CONFIG['SystemURL'] . "/modules/servers/openprovider_plesk_license/assets";
$language = $CONFIG['Language'];
$langfilename = __DIR__ . '/lang/' . $language . '.php';
if (file_exists($langfilename)) {
require($langfilename);
} else {
require(__DIR__ . '/lang/english.php');
}
$key = $params['customfields']['key_id'];
$getLicense = $helper->getPleskLicenseDetail($key);
// Convert stdClass object to an array
$licenseData = json_decode(json_encode($getLicense['result']->data), true);
// Prepare license details safely
$licenseDetails = [];
if (!empty($licenseData['key_number'])) {
$licenseDetails = [
'product' => 'plesk',
'keyType' => $licenseData['key']['title'] ?? '',
'licenseNum' => $licenseData['key_number'] ?? '',
'activation_code' => $licenseData['activation_code'] ?? '',
'ip_address' => $licenseData['ip_address_binding'] ?? '',
'billing_type' => $licenseData['billing_type'] ?? '',
'period' => $licenseData['period'] ?? '',
'order_date' => !empty($licenseData['order_date']) ? date('Y-m-d', strtotime($licenseData['order_date'])) : '',
'expiry_date' => !empty($licenseData['expiration_date']) ? date('Y-m-d', strtotime($licenseData['expiration_date'])) : '',
'status' => (isset($licenseData['status']) && $licenseData['status'] == 'ACT') ? 'Active' : 'Inactive',
'features' => $licenseData['features'] ?? [],
'extensions' => $licenseData['extensions'] ?? []
];
}
// Initialize Smarty
$smarty = new \Smarty();
$smarty->assign('licenseDetails', $licenseDetails);
$smarty->assign('LANG', $_ADDONLANG);
$smarty->assign('assets', $assets);
// Render the TPL template
$licenseDetailsHtml = $smarty->fetch(__DIR__ . '/templates/admin/license_details.tpl');
return [
'Plesk License Details' => $licenseDetailsHtml
];
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
// In an error condition, simply return no additional fields to display.
}
return array();
}
/**
* Client area output logic handling.
*
* This function is used to define module specific client area output. It should
* return an array consisting of a template file and optional additional
* template variables to make available to that template.
*
*/
function openprovider_plesk_license_ClientArea(array $params)
{
try {
global $CONFIG;
global $whmcs;
$helper = new Helper();
if ($whmcs->get_req_var('customaction')){
if (file_exists(__DIR__ . '/lib/ajax.php'))
include_once __DIR__ . '/lib/ajax.php';
exit();
}
$assets = $CONFIG['SystemURL'] . "/modules/servers/openprovider_plesk_license/assets";
$language = $CONFIG['Language'];
$langfilename = __DIR__ . '/lang/' . $language . '.php';
if (file_exists($langfilename)) {
require($langfilename);
} else {
require(__DIR__ . '/lang/english.php');
}
$key = $params['customfields']['key_id'];
$commands = explode('<br />', nl2br($params['configoption3']));
$getLicense = $helper->getPleskLicenseDetail($key);
// echo '<pre>';
// print_r($getLicense); die;
if($getLicense)
{
$responseData = [
'product' => 'plesk',
'title' => $getLicense['result']->data->title,
'comment' => $getLicense['result']->data->comment,
'keyType' => $getLicense['result']->data->key->title,
'keyid' => $getLicense['result']->data->key_id,
'licenseNum' => $getLicense['result']->data->key_number,
'activation_code' => $getLicense['result']->data->activation_code,
'ip_address' => $getLicense['result']->data->ip_address_binding,
'billing_type' => $getLicense['result']->data->billing_type,
'period' => $getLicense['result']->data->period,
'order_date' => date('Y-m-d',strtotime($getLicense['result']->data->order_date)),
'expiry_date' => date('Y-m-d',strtotime($getLicense['result']->data->expiration_date)),
'features' => $getLicense['result']->data->features ?? [],
'extensions' => $getLicense['result']->data->extensions ?? [],
'status' => (isset($getLicense['result']->data->status) && $getLicense['result']->data->status == 'ACT') ? 'Active' : 'Inactive',
'keyId' =>$key,
'command'=>$commands
];
}
$templateFile = 'templates/manage.tpl';
return array(
'templatefile' => $templateFile,
'templateVariables' => array(
'responseData' => $responseData,
'assets' => $assets,
'LANG' => $_ADDONLANG
),
);
} catch (Exception $e) {
// Record the error in WHMCS's module log.
logModuleCall(
'openprovider_plesk_license',
__FUNCTION__,
$params,
$e->getMessage(),
$e->getTraceAsString()
);
// In an error condition, display an error page.
return array(
'tabOverviewReplacementTemplate' => 'error.tpl',
'templateVariables' => array(
'usefulErrorHelper' => $e->getMessage(),
),
);
}
}