-
Notifications
You must be signed in to change notification settings - Fork 2
/
virtualmin_blesta.php
2664 lines (2175 loc) · 98.9 KB
/
virtualmin_blesta.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
/**
* Created by PhpStorm.
* User: Luke Hardiman
* Date: 30/08/2015
* Time: 12:51 PM
* copyright Luke Hardiman 2015
*/
class VirtualminBlesta extends module
{
/**
* @var string The version of this module
*/
private static $version = "0.1.2";
/**
* @var string The authors of this module
*/
private static $authors = array(
array('name' => "Luke Hardiman", 'url' => "https://github.com/lukesUbuntu")
);
/**
* Initializes the API and returns a Singleton instance of that object for api calls
* see ::Api()
* @return VirtualMIN API The
*/
private $_api = false;
/**
* Initializes the Virtualmin helper class and returns a Singleton instance of our helper
* see ::getVirtualMinHelper()
* @return VirtualMIN Helper Class
*/
private $_virtualmin_lib_helper = false;
/**
* Initializes the module
*/
public function __construct()
{
// Load components required by this module
Loader::loadComponents($this, array("Input"));
// Load the language required by this module
Language::loadLang("virtualmin_blesta", null, dirname(__FILE__) . DS . "language" . DS);
//added error reporting internally
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
/**
* Returns the name of this module
*
* @return string The common name of this module
*/
public function getName()
{
return Language::_("virtualmin.name", true);
}
/**
* Returns the version of this module
*
* @return string The current version of this module
*/
public function getVersion()
{
return self::$version;
}
/**
* Returns the name and URL for the authors of this module
*
* @return array A numerically indexed array that contains an array with key/value pairs for 'name' and 'url', representing the name and URL of the authors of this module
*/
public function getAuthors()
{
return self::$authors;
}
/**
* Returns the value used to identify a particular service
*
* @param stdClass $service A stdClass object representing the service
* @return string A value used to identify this service amongst other similar services
*/
public function getServiceName($service)
{
foreach ($service->fields as $field) {
if ($field->key == "virtualmin_domain")
return $field->value;
}
return null;
}
/**
* Returns a noun used to refer to a module row (e.g. "Server", "VPS", "Reseller Account", etc.)
*
* @return string The noun used to refer to a module row
*/
public function moduleRowName()
{
return Language::_("virtualmin.module_row", true);
}
/**
* Returns a noun used to refer to a module row in plural form (e.g. "Servers", "VPSs", "Reseller Accounts", etc.)
*
* @return string The noun used to refer to a module row in plural form
*/
public function moduleRowNamePlural()
{
return Language::_("virtualmin.module_row_plural", true);
}
/**
* Returns a noun used to refer to a module group (e.g. "Server Group", "Cloud", etc.)
*
* @return string The noun used to refer to a module group
*/
public function moduleGroupName()
{
return null;
}
/**
* Returns the key used to identify the primary field from the set of module row meta fields.
* This value can be any of the module row meta fields.
*
* @return string The key used to identify the primary field from the set of module row meta fields
*/
public function moduleRowMetaKey()
{
return "server_name";
}
/**
* Performs any necessary bootstraping actions. Sets Input errors on
* failure, preventing the module from being added.
*
* @return array A numerically indexed array of meta data 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 install()
{
}
/**
* Performs migration of data from $current_version (the current installed version)
* to the given file set version. Sets Input errors on failure, preventing
* the module from being upgraded.
*
* @param string $current_version The current installed version of this module
*/
public function upgrade($current_version)
{
}
/**
* Performs any necessary cleanup actions. Sets Input errors on failure
* after the module has been uninstalled.
*
* @param int $module_id The ID of the module being uninstalled
* @param boolean $last_instance True if $module_id is the last instance across all companies for this module, false otherwise
*/
public function uninstall($module_id, $last_instance)
{
}
/**
* Returns the value used to identify a particular package service which has
* not yet been made into a service. This may be used to uniquely identify
* an uncreated service of the same package (i.e. in an order form checkout)
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @return string The value used to identify this package service
* @see Module::getServiceName()
*/
public function getPackageServiceName($packages, array $vars = null)
{
if (isset($vars['virtualmin_domain']))
return $vars['virtualmin_domain'];
return null;
}
/**
* 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 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")
{
//going to get the fields we need but passing some pacakage fields
$params = $this->getFieldsFromInput((array)$vars, $package);
//validate
$this->validateService($package, $vars);
if ($this->Input->errors())
return;
// Only provision the service if module is enabled
//if $vars['virtualmin_domain_already']
if (isset($vars['use_module']) && $vars['use_module'] == "true" && !isset($vars['virtualmin_domain_already'])) {
//get row
$row = $this->getModuleRow();
//hide passwords from log
$masked_params = $params;
$masked_params['password'] = "****";
$this->log($row->meta->host_name . "|" . 'addService', serialize($masked_params), "input", true);
unset($masked_params);
//get current clients details we want username
Loader::loadModels($this, array("Clients"));
$client = $this->Clients->get($vars['client_id'], false);
//we are going to process the account under the blesta email as username
$api = $this->api($row);
//$result = $api->create($params['domain'],$params['passwd']);
$account = array(
'domain' => $params['domain'],
'email' => $client->email,
'user' => $params['domain'],
'pass' => $params['passwd'],
'plan' => $params['package'],
'features-from-plan' => ''
);
//lets add the domain
$result = $api->add_domain($account);
$this->parseResponse($result);
$this->log(
$row->meta->host_name . "|" . 'addService', serialize($result), "output", true
);
}
// Return service fields
return array(
array(
'key' => "virtualmin_domain",
'value' => $params['domain'],
'encrypted' => 0
),
array(
'key' => "virtualmin_username",
'value' => $params['domain'],
'encrypted' => 0
),
array(
'key' => "virtualmin_password",
'value' => $params['passwd'],
'encrypted' => 1
)
);
}
/**
* Returns an array of service field to set for the service using the given input
*
* @param array $vars An array of key/value input pairs
* @param stdClass $package A stdClass object representing the package for the service
* @return array An array of key/value pairs representing service fields
*/
private function getFieldsFromInput(array $vars, $package)
{
$fields = array(
'domain' => isset($vars['virtualmin_domain']) ? $vars['virtualmin_domain'] : null,
//'username' => isset($vars['virtualmin_domain']) ? $this->usernameFromDomain($vars['virtualmin_domain']): null,
'passwd' => isset($vars['virtualmin_password']) ? $vars['virtualmin_password'] : null,
'package' => isset($package->meta->package) ? $package->meta->package : null
);
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
* @return boolean True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null, $editService = false)
{
// Validate the service fields
$rules = array(
'virtualmin_domain' => array(
'format' => array(
'rule' => array(array($this, "validateHostName")),
'message' => Language::_("virtualmin.!error.virtualmin_domain.format", true)
)
),
'virtualmin_password' => array(
'format' => array(
'rule' => array("matches", "/^[(\x20-\x7F)]*$/"), // ASCII 32-127
'message' => Language::_("virtualmin.!error.virtualmin_password.format", true)
),
'length' => array(
'rule' => array("minLength", 5),
'message' => Language::_("virtualmin.!error.virtualmin_password.length", true)
)
)/*,
'virtualmin_edit_action' => array(
'format' => array(
'rule' => array(array($this, "validatePostActions")),
'message' => "Something is wrong with virtualmin_edit_action ;("
)
)*/
//if we are editing on form action make sure we are set
);
//edit_action
//if we are editing a service we do not need to verify all rules so we will unset ones we need
if ($editService) {
//add our rule for editing
//loop all arrays
foreach ($rules as $rule => $value) {
// || $vars[$rule] == ""
if (!array_key_exists($rule, $vars))
unset($rules[$rule]);
}
} else {
//we are adding a service remove the edit_action
//unset($rules["virtualmin_edit_action"]);
}
$this->Input->setRules($rules);
return $this->Input->validates($vars);
}
/**
*
* Returns a singleton of Virtualmin API
*
* see lib/virtualmin_api.php
*
* @param bool|false $module_row
* @return bool|VirtualMinApi
*/
private function api($module_row = false)
{
//load our api
if ($this->_api == false) {
if ($module_row == false)
$module_row = $this->getModuleRow();
if (!isset($module_row)) {
die("failed to get module row");
}
Loader::load(dirname(__FILE__) . DS . "lib" . DS . "virtualmin_api.php");
//$host, $username, $password, $port = "10000", $use_ssl = true
$this->_api = new VirtualMinApi(
$module_row->meta->host_name, //hostname
$module_row->meta->user_name, //username
$module_row->meta->password, //password
$module_row->meta->port_number, //port number
($module_row->meta->use_ssl == "true") //use secure
);
}
return $this->_api;
}
/**
* Parses the response from the API into a stdClass object
*
* @param array $response The response from the API
* @param boolean $return_response Whether to return the response, regardless of error
* @return stdClass A stdClass object representing the response, void if the response was an error
*/
private function parseResponse($response, $module_row = null, $ignore_error = false)
{
if (!$module_row)
$module_row = $this->getModuleRow();
$success = true;
// Set an internal error on no response or invalid response
if (empty($response)) {
$this->Input->setErrors(
array('errors' => Language::_("virtualmin.!error.api.internal", true))
);
$success = false;
}
// Set an error if given
if (isset($response->error) || $response->status != "success") {
$error = (isset($response->error) ? $response->error : Language::_("virtualmin.!error.api.internal", true));
$this->Input->setErrors(
array('errors' => $error)
);
$success = false;
}
//remove the full long error before logging
if (isset($response->full_error))
unset($response->full_error);
// Log the response
$this->log($module_row->meta->host_name, serialize($response), "output", $success);
// Return if any errors encountered
if (!$success && !$ignore_error)
return;
return $response;
}
/**
* 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 = array(), $parent_package = null, $parent_service = null)
{
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)
{
// TODO if service is canceled add an option to delete the package for now we will just disable the service on virtualmin
if (($row = $this->getModuleRow())) {
$service_fields = $this->serviceFieldsToObject($service->fields);
$api = $this->api($row);
$response = $api->suspend_domain(['domain' => $service_fields->virtualmin_domain]);
if (isset($response->output) && isset($response->error) && $response->status !== 'success') {
$this->log($row->meta->host_name . '| cancelService', serialize($response), 'output');
$this->Input->setErrors(array(array($response->output)));
} else {
$this->log($row->meta->host_name . '| cancelService', serialize($response), 'output', true);
}
}
return null;
}
/**
* 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)
{
// $service_reasone =
if (($row = $this->getModuleRow())) {
$service_fields = $this->serviceFieldsToObject($service->fields);
$api = $this->api($row);
$response = $api->suspend_domain(['domain' => $service_fields->virtualmin_domain]);
// $response = $api->suspend_domain(['domain' => $service_fields->virtualmin_domain]);
if (isset($response->output) && isset($response->error) && $response->status !== 'success') {
$this->log($row->meta->host_name . '| suspend_domain', serialize($response), 'output');
$this->Input->setErrors(array(array($response->output)));
} else {
$this->log($row->meta->host_name . '| suspend_domain', serialize($response), 'output', true);
}
}
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())) {
$service_fields = $this->serviceFieldsToObject($service->fields);
$api = $this->api($row);
$response = $api->unsuspend_domain(['domain' => $service_fields->virtualmin_domain]);
if (isset($response->output) && isset($response->error) && $response->status !== 'success') {
$this->log($row->meta->host_name . '| unsuspend_domain', serialize($response), 'output');
$this->Input->setErrors(array(array($response->output)));
} else {
$this->log($row->meta->host_name . '| unsuspend_domain', serialize($response), 'output', true);
}
}
return null;
}
/**
* Allows the module to perform an action when the service is ready to renew.
* Sets Input errors on failure, preventing the service from renewing.
*
* @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 renewed (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 renewService($package, $service, $parent_package = null, $parent_service = null)
{
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)
{
return null;
}
/**
* 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)
{
$meta = array();
if (isset($vars['meta']) && is_array($vars['meta'])) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = array(
'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)
{
$meta = array();
if (isset($vars['meta']) && is_array($vars['meta'])) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = array(
'key' => $key,
'value' => $value,
'encrypted' => 0
);
}
}
return $meta;
}
/**
* Deletes the package on the remote server. Sets Input errors on failure,
* preventing the package from being deleted.
*
* @param stdClass $package A stdClass object representing the selected package
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function deletePackage($package)
{
}
/**
* 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 manage 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 . "virtualmin_blesta" . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, array("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)
{
// ajax to manageAddRow
$allowedRequests = array("check_server");
$this->getVirtualMinHelper()->processAjax($this, $_GET, $_GET, $allowedRequests, $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 . 'virtualmin_blesta' . 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)
{
// ajax to manageAddRow
$allowedRequests = array("check_server");
$this->getVirtualMinHelper()->processAjax($this, $_GET, $_GET, $allowedRequests, $vars);
$this->view = new View("edit_row", "default");
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView("components" . DS . "modules" . DS . "virtualmin_blesta" . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, array("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.
*
* @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)
{
// ajax to manageAddRow
$allowedRequests = array("check_server");
$this->getVirtualMinHelper()->processAjax($this, $_GET, $_GET, $allowedRequests, $vars);
$vars['host_name'] = strtolower($vars['host_name']);
//our meta fields
$meta_fields = array(
"server_name",
"host_name",
"port_number",
"user_name",
"password",
"use_ssl",
"account_limit",
"account_count",
"name_servers"
);
//encrypted fields
$encrypted_fields = array("user_name", "password");
// Set use_ssl as false if not checked
if (empty($vars['use_ssl']))
$vars['use_ssl'] = "false";
// Set rules to validate against
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = array();
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = array(
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
);
}
}
return $meta;
}
}
/**
* Retrieves a list of rules for validating adding/editing a module row
*
* @param array $vars A list of input vars
* @return array A list of rules
*/
private function getRowRules($vars)
{
$rules = [
'server_name' => [
'valid' => [
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Virtualmin.!error.server_name_valid', true)
]
],
'host_name' => [
'valid' => [
'rule' => [[$this, 'validateHostName']],
'message' => Language::_('Virtualmin.!error.host_name_valid', true)
]
],
'user_name' => [
'valid' => [
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Virtualmin.!error.user_name_valid', true)
]
],
'port_number' => [
'valid' => [
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Virtualmin.!error.port_valid', true)
]
],
'password' => [
'valid' => [
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Virtualmin.!error.password_valid', true)
]
]
];
return $rules;
}
/**
* Validates connection by calling list_plans smallest transaction possible
*
*/
public function validateConnection($account)
{
// print_r($password . $hostname . $port . $user . $realm);
Loader::load(dirname(__FILE__) . DS . "lib" . DS . "virtualmin_api.php");
try {
$test = new VirtualMinApi(
$account['host_name'],
$account['username'], //username
$account['password'], //password
$account['port_number'], //port number
($account['use_ssl'] == "true") //use secure
);
$params['program'] = 'list-plans';
$params['json'] = 1;
$params[] = 'multiline';
$response = json_decode($test->callServer($params));
if ($response && $response->status == 'success') {
return $this->getVirtualMinHelper()->sendAjax("Connected Successfully");
}else{
//$this->log($account['host_name'], serialize($response), "connection error", $response);
$this->getVirtualMinHelper()->sendAjax($response, false);
}
} catch (Exception $e) {
// $errorMessage = $e->getMessage();
$this->getVirtualMinHelper()->sendAjax($e->getMessage(), false);
/*
$this->Input->setErrors(
array('errors' => $errorMessage)
);*/
$this->log($account['host_name'], serialize($errorMessage), "connection error", $errorMessage);
}
}
/**
* Edits the module row on the remote server. Sets Input errors on failure,
* preventing the row from being updated.
*
* @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)
{
// ajax to manageAddRow
$allowedRequests = array("check_server");
$this->getVirtualMinHelper()->processAjax($this, $_GET, $_GET, $allowedRequests, $vars);
//define our meta fields
$meta_fields = array(
"server_name",
"host_name",
"port_number",
"user_name",
"password",
"use_ssl",
"account_limit",
"account_count",
"name_servers"
);
//set encrypted fields
$encrypted_fields = array("user_name", "password");
// Set unspecified checkboxes
if (empty($vars['use_ssl']))
$vars['use_ssl'] = "false";
$this->Input->setRules($this->getRowRules($vars));
// Validate module row