-
Notifications
You must be signed in to change notification settings - Fork 0
/
ispmanager.php
1447 lines (1262 loc) · 53.5 KB
/
ispmanager.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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Blesta\Core\Util\Validate\Server;
/**
* ispmanager Module.
*
* @package blesta
* @subpackage blesta.components.modules.ispmanager
* @copyright Copyright (c) 2020, Phillips Data, Inc.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.blesta.com/ Blesta
*/
class Ispmanager extends Module
{
/**
* Initializes the module.
*/
public function __construct()
{
// Load configuration required by this module
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
// Load components required by this module
Loader::loadComponents($this, ['Input']);
// Load the language required by this module
Language::loadLang('ispmanager', null, dirname(__FILE__) . DS . 'language' . DS);
}
/**
* Returns all tabs to display to a client when managing a service whose
* package uses this module
*
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of tabs in the format of method => title.
* Example: array('methodName' => "Title", 'methodName2' => "Title2")
*/
public function getClientTabs($package)
{
return [
'tabClientActions' => Language::_('Ispmanager.tab_client_actions', true)
];
}
/**
* Returns an array of available service deligation order methods. The module
* will determine how each method is defined. For example, the method "first"
* may be implemented such that it returns the module row with the least number
* of services assigned to it.
*
* @return array An array of order methods in key/value paris where the key is
* the type to be stored for the group and value is the name for that option
* @see Module::selectModuleRow()
*/
public function getGroupOrderOptions()
{
return [
'roundrobin' => Language::_('Ispmanager.order_options.roundrobin', true),
'first' => Language::_('Ispmanager.order_options.first', true)
];
}
/**
* Returns all fields used when adding/editing a package, including any
* javascript to execute when the page is rendered with these fields.
*
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containing the fields to
* render as well as any additional HTML markup to include
*/
public function getPackageFields($vars = null)
{
Loader::loadHelpers($this, ['Html']);
$fields = new ModuleFields();
// Fetch all templates available for the given server or server group
$module_row = null;
if (isset($vars->module_group) && $vars->module_group == '') {
if (isset($vars->module_row) && $vars->module_row > 0) {
$module_row = $this->getModuleRow($vars->module_row);
} else {
$rows = $this->getModuleRows();
if (isset($rows[0])) {
$module_row = $rows[0];
}
unset($rows);
}
} else {
// Fetch the 1st server from the list of servers in the selected group
$rows = $this->getModuleRows($vars->module_group);
if (isset($rows[0])) {
$module_row = $rows[0];
}
unset($rows);
}
$templates = [];
if ($module_row) {
$templates = $this->getTemplates($module_row);
}
// Create template label
$template = $fields->label(Language::_('Ispmanager.package_fields.template', true), 'ispmanager_template');
// Create template field and attach to template label
$template->attach(
$fields->fieldSelect(
'meta[template]',
$templates,
(isset($vars->meta['template']) ? $vars->meta['template'] : null),
['id' => 'ispmanager_template']
)
);
// Set the label as a field
$fields->setField($template);
return $fields;
}
/**
* Validates input data when attempting to add a package, returns the meta
* data to save when adding a package. Performs any action required to add
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being added.
*
* @param array An array of key/value pairs used to add the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addPackage(array $vars = null)
{
// Set rules to validate input data
$this->Input->setRules($this->getPackageRules($vars));
// Build meta data to return
$meta = [];
if ($this->Input->validates($vars)) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
}
return $meta;
}
/**
* Validates input data when attempting to edit a package, returns the meta
* data to save when editing a package. Performs any action required to edit
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being edited.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array An array of key/value pairs used to edit the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editPackage($package, array $vars = null)
{
// Set rules to validate input data
$this->Input->setRules($this->getPackageRules($vars));
// Build meta data to return
$meta = [];
if ($this->Input->validates($vars)) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
}
return $meta;
}
/**
* Returns the rendered view of the manage module page.
*
* @param mixed $module A stdClass object representing the module and its rows
* @param array $vars An array of post data submitted to or on the manager module
* page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the manager module page
*/
public function manageModule($module, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('manage', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'ispmanager' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
$this->view->set('module', $module);
return $this->view->fetch();
}
/**
* Returns the rendered view of the add module row page.
*
* @param array $vars An array of post data submitted to or on the add module
* row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the add module row page
*/
public function manageAddRow(array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('add_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'ispmanager' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
// Set unspecified checkboxes
if (!empty($vars)) {
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
}
$this->view->set('vars', (object) $vars);
return $this->view->fetch();
}
/**
* Returns the rendered view of the edit module row page.
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of post data submitted to or on the edit
* module row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the edit module row page
*/
public function manageEditRow($module_row, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('edit_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'ispmanager' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
if (empty($vars)) {
$vars = $module_row->meta;
} else {
// Set unspecified checkboxes
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
}
$this->view->set('vars', (object) $vars);
return $this->view->fetch();
}
/**
* Adds the module row on the remote server. Sets Input errors on failure,
* preventing the row from being added. Returns a set of data, which may be
* a subset of $vars, that is stored for this module row.
*
* @param array $vars An array of module info to add
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function addModuleRow(array &$vars)
{
$meta_fields = ['server_name', 'host_name', 'user_name', 'password',
'use_ssl', 'account_limit', 'account_count', 'name_servers', 'notes'];
$encrypted_fields = ['password'];
// Set unspecified checkboxes
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
}
/**
* Edits the module row on the remote server. Sets Input errors on failure,
* preventing the row from being updated. Returns a set of data, which may be
* a subset of $vars, that is stored for this module row.
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of module info to update
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function editModuleRow($module_row, array &$vars)
{
$meta_fields = ['server_name', 'host_name', 'user_name', 'password',
'use_ssl', 'account_limit', 'account_count', 'name_servers', 'notes'];
$encrypted_fields = ['password'];
// Set unspecified checkboxes
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
}
/**
* Returns all fields to display to an admin attempting to add a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render
* as well as any additional HTML markup to include
*/
public function getAdminAddFields($package, $vars = null)
{
Loader::loadHelpers($this, ['Html']);
$fields = new ModuleFields();
// Create domain label
$domain = $fields->label(Language::_('Ispmanager.service_field.domain', true), 'ispmanager_domain');
// Create domain field and attach to domain label
$domain->attach(
$fields->fieldText(
'ispmanager_domain',
(isset($vars->ispmanager_domain) ? $vars->ispmanager_domain : null),
['id' => 'ispmanager_domain']
)
);
// Set the label as a field
$fields->setField($domain);
// Create username label
$username = $fields->label(
Language::_('Ispmanager.service_field.username', true),
'ispmanager_username'
);
// Create username field and attach to username label
$username->attach(
$fields->fieldText(
'ispmanager_username',
(isset($vars->ispmanager_username) ? $vars->ispmanager_username : null),
['id' => 'ispmanager_username']
)
);
// Add tooltip
$tooltip = $fields->tooltip(Language::_('Ispmanager.service_field.tooltip.username', true));
$username->attach($tooltip);
// Set the label as a field
$fields->setField($username);
// Create password label
$password = $fields->label(
Language::_('Ispmanager.service_field.password', true),
'ispmanager_password'
);
// Create password field and attach to password label
$password->attach(
$fields->fieldPassword(
'ispmanager_password',
['id' => 'ispmanager_password', 'value' => (isset($vars->ispmanager_password) ? $vars->ispmanager_password : null)]
)
);
// Add tooltip
$tooltip = $fields->tooltip(Language::_('Ispmanager.service_field.tooltip.password', true));
$password->attach($tooltip);
// Set the label as a field
$fields->setField($password);
return $fields;
}
/**
* Returns all fields to display to a client attempting to add a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render as well
* as any additional HTML markup to include
*/
public function getClientAddFields($package, $vars = null)
{
Loader::loadHelpers($this, ['Html']);
$fields = new ModuleFields();
// Create domain label
$domain = $fields->label(Language::_('Ispmanager.service_field.domain', true), 'ispmanager_domain');
// Create domain field and attach to domain label
$domain->attach(
$fields->fieldText(
'ispmanager_domain',
(isset($vars->ispmanager_domain) ? $vars->ispmanager_domain : ($vars->domain ?? null)),
['id' => 'ispmanager_domain']
)
);
// Set the label as a field
$fields->setField($domain);
return $fields;
}
/**
* Returns all fields to display to an admin attempting to edit a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render as
* well as any additional HTML markup to include
*/
public function getAdminEditFields($package, $vars = null)
{
Loader::loadHelpers($this, ['Html']);
$fields = new ModuleFields();
// Create password label
$password = $fields->label(
Language::_('Ispmanager.service_field.password', true),
'ispmanager_password'
);
// Create password field and attach to password label
$password->attach(
$fields->fieldPassword(
'ispmanager_password',
['id' => 'ispmanager_password', 'value' => (isset($vars->ispmanager_password) ? $vars->ispmanager_password : null)]
)
);
// Set the label as a field
$fields->setField($password);
return $fields;
}
/**
* Attempts to validate service info. This is the top-level error checking method. Sets Input errors on failure.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @param bool $edit True if this is an edit, false otherwise
* @return bool True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null, $edit = false)
{
$rules = [
'ispmanager_domain' => [
'format' => [
'rule' => [[$this, 'validateHostName']],
'message' => Language::_('Ispmanager.!error.ispmanager_domain.format', true)
]
],
'ispmanager_username' => [
'format' => [
'if_set' => true,
'rule' => ['matches', '/^([a-z0-9])+$/i'],
'message' => Language::_('Ispmanager.!error.ispmanager_username.format', true)
],
'length' => [
'if_set' => true,
'rule' => ['betweenLength', 1, 16],
'message' => Language::_('Ispmanager.!error.ispmanager_username.length', true)
]
],
'ispmanager_password' => [
'valid' => [
'if_set' => true,
'rule' => ['isPassword', 8],
'message' => Language::_('Ispmanager.!error.ispmanager_password.valid', true),
'last' => true
],
]
];
// Set the values that may be empty
$empty_values = ['ispmanager_username', 'ispmanager_password'];
if ($edit) {
// If this is an edit and no password given then don't evaluate password
// since it won't be updated
if (!array_key_exists('ispmanager_password', $vars) || $vars['ispmanager_password'] == '') {
unset($rules['ispmanager_password']);
}
unset($rules['ispmanager_domain']);
unset($rules['ispmanager_username']);
}
// Remove rules on empty fields
foreach ($empty_values as $value) {
if (empty($vars[$value])) {
unset($rules[$value]);
}
}
$this->Input->setRules($rules);
return $this->Input->validates($vars);
}
/**
* Adds the service to the remote server. Sets Input errors on failure,
* preventing the service from being added.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being added (if the current service is an addon service
* service and parent service has already been provisioned)
* @param string $status The status of the service being added. These include:
* - active
* - canceled
* - pending
* - suspended
* @return array A numerically indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addService(
$package,
array $vars = null,
$parent_package = null,
$parent_service = null,
$status = 'pending'
) {
$row = $this->getModuleRow();
if (!$row) {
$this->Input->setErrors(
['module_row' => ['missing' => Language::_('Ispmanager.!error.module_row.missing', true)]]
);
return;
}
$api = $this->getApi($row->meta->host_name, $row->meta->user_name, $row->meta->password, $row->meta->use_ssl);
// Generate username/password
if (array_key_exists('ispmanager_domain', $vars)) {
Loader::loadModels($this, ['Clients']);
// Force domain to lower case
$vars['ispmanager_domain'] = strtolower($vars['ispmanager_domain']);
// Generate a username
if (empty($vars['ispmanager_username'])) {
$vars['ispmanager_username'] = $this->generateUsername($vars['ispmanager_domain']);
}
// Generate a password
if (empty($vars['ispmanager_password'])) {
$vars['ispmanager_password'] = $this->generatePassword();
}
// Get client's contact email address
if (isset($vars['client_id']) && ($client = $this->Clients->get($vars['client_id'], false))) {
$vars['ispmanager_full_name'] = $client->first_name . ' ' . $client->last_name;
}
}
$params = $this->getFieldsFromInput((array) $vars, $package);
$this->validateService($package, $vars);
if ($this->Input->errors()) {
return;
}
// Only provision the service if 'use_module' is true
if ($vars['use_module'] == 'true') {
$masked_params = $params;
$masked_params['passwd'] = '***';
$this->log($row->meta->host_name . '|user.add', serialize($masked_params), 'input', true);
unset($masked_params);
// Create account
$response = $this->parseResponse($api->createAccount($params));
if ($this->Input->errors()) {
return;
}
// Update the number of accounts on the server
$this->updateAccountCount($row);
}
// Return service fields
return [
[
'key' => 'ispmanager_domain',
'value' => $vars['ispmanager_domain'],
'encrypted' => 0
],
[
'key' => 'ispmanager_username',
'value' => $vars['ispmanager_username'],
'encrypted' => 0
],
[
'key' => 'ispmanager_password',
'value' => $vars['ispmanager_password'],
'encrypted' => 1
]
];
}
/**
* Edits the service on the remote server. Sets Input errors on failure,
* preventing the service from being edited.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param array $vars An array of user supplied info to satisfy the request
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being edited (if the current service is an addon service)
* @return array A numerically indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editService($package, $service, array $vars = null, $parent_package = null, $parent_service = null)
{
$row = $this->getModuleRow();
$api = $this->getApi($row->meta->host_name, $row->meta->user_name, $row->meta->password, $row->meta->use_ssl);
$params = $this->getFieldsFromInput((array) $vars, $package, true);
$service_fields = $this->serviceFieldsToObject($service->fields);
// Default fields using service fields
if (!isset($params['passwd'])) {
$params['passwd'] = $service_fields->ispmanager_password;
}
// The domain and username can't be updated
$params['webdomain_name'] = $service_fields->ispmanager_domain;
$params['emaildomain_name'] = $service_fields->ispmanager_domain;
$params['name'] = $service_fields->ispmanager_username;
$this->validateService($package, $vars, true);
if ($this->Input->errors()) {
return;
}
// Remove password if not being updated
if (isset($params['passwd']) && $params['passwd'] == '') {
unset($params['passwd']);
}
// Only update the service if 'use_module' is true
if ($vars['use_module'] == 'true') {
// Update ispmanager account
$masked_params = $params;
$masked_params['passwd'] = '***';
// Edit account
$params['elid'] = $service_fields->ispmanager_username;
$this->log($row->meta->host_name . '|user.edit', serialize($masked_params), 'input', true);
$response = $this->parseResponse($api->updateAccount($params));
if ($this->Input->errors()) {
return;
}
}
// Set fields to update locally
$field_mappings = [
'passwd' => 'ispmanager_password'
];
foreach ($field_mappings as $field => $field_mapping) {
if (property_exists($service_fields, $field_mapping) && isset($params[$field])) {
$service_fields->{$field_mapping} = $params[$field];
}
}
// Return all the service fields
$fields = [];
$encrypted_fields = ['ispmanager_password'];
foreach ($service_fields as $key => $value) {
$fields[] = ['key' => $key, 'value' => $value, 'encrypted' => (in_array($key, $encrypted_fields) ? 1 : 0)];
}
return $fields;
}
/**
* Suspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being suspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being suspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function suspendService($package, $service, $parent_package = null, $parent_service = null)
{
if (($row = $this->getModuleRow())) {
$api = $this->getApi(
$row->meta->host_name,
$row->meta->user_name,
$row->meta->password,
$row->meta->use_ssl
);
$service_fields = $this->serviceFieldsToObject($service->fields);
// Suspend ispmanager account
$this->log(
$row->meta->host_name . '|user.suspend',
serialize($service_fields->ispmanager_username),
'input',
true
);
$response = $this->parseResponse($api->suspendAccount($service_fields->ispmanager_username));
if ($this->Input->errors()) {
return;
}
}
return null;
}
/**
* Unsuspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being unsuspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being unsuspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function unsuspendService($package, $service, $parent_package = null, $parent_service = null)
{
if (($row = $this->getModuleRow())) {
$api = $this->getApi(
$row->meta->host_name,
$row->meta->user_name,
$row->meta->password,
$row->meta->use_ssl
);
$service_fields = $this->serviceFieldsToObject($service->fields);
// Unsuspend ispmanager account
$this->log(
$row->meta->host_name . '|user.suspend',
serialize($service_fields->ispmanager_username),
'input',
true
);
$response = $this->parseResponse($api->unsuspendAccount($service_fields->ispmanager_username));
if ($this->Input->errors()) {
return;
}
}
return null;
}
/**
* Cancels the service on the remote server. Sets Input errors on failure,
* preventing the service from being canceled.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being canceled (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function cancelService($package, $service, $parent_package = null, $parent_service = null)
{
if (($row = $this->getModuleRow())) {
$api = $this->getApi(
$row->meta->host_name,
$row->meta->user_name,
$row->meta->password,
$row->meta->use_ssl
);
$service_fields = $this->serviceFieldsToObject($service->fields);
// Delete ispmanager account
$this->log(
$row->meta->host_name . '|user.delete',
serialize($service_fields->ispmanager_username),
'input',
true
);
$response = $this->parseResponse($api->removeAccount($service_fields->ispmanager_username));
if ($this->Input->errors()) {
return;
}
// Update the number of accounts on the server
$this->updateAccountCount($row, false);
}
return null;
}
/**
* Updates the package for the service on the remote server. Sets Input
* errors on failure, preventing the service's package from being changed.
*
* @param stdClass $package_from A stdClass object representing the current package
* @param stdClass $package_to A stdClass object representing the new package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being changed (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function changeServicePackage(
$package_from,
$package_to,
$service,
$parent_package = null,
$parent_service = null
) {
if (($row = $this->getModuleRow())) {
$api = $this->getApi(
$row->meta->host_name,
$row->meta->user_name,
$row->meta->password,
$row->meta->use_ssl
);
$service_fields = $this->serviceFieldsToObject($service->fields);
// Update account package
$this->log(
$row->meta->host_name . '|user.edit',
serialize($service_fields->ispmanager_username),
'input',
true
);
$params = [
'elid' => $service_fields->ispmanager_username,
'preset' => $package_to->meta->template
];
$response = $this->parseResponse($api->updateAccount($params));
if ($this->Input->errors()) {
return;
}
}
return null;
}
/**
* Fetches the HTML content to display when viewing the service info in the
* admin interface.
*
* @param stdClass $service A stdClass object representing the service
* @param stdClass $package A stdClass object representing the service's package
* @return string HTML content containing information to display when viewing the service info
*/
public function getAdminServiceInfo($service, $package)
{
$row = $this->getModuleRow();
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('admin_service_info', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'ispmanager' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
$this->view->set('module_row', $row);
$this->view->set('package', $package);
$this->view->set('service', $service);
$this->view->set('service_fields', $this->serviceFieldsToObject($service->fields));
return $this->view->fetch();
}
/**
* Fetches the HTML content to display when viewing the service info in the
* client interface.
*
* @param stdClass $service A stdClass object representing the service
* @param stdClass $package A stdClass object representing the service's package
* @return string HTML content containing information to display when viewing the service info
*/
public function getClientServiceInfo($service, $package)
{
$row = $this->getModuleRow();
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('client_service_info', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'ispmanager' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);