forked from knownhost/Blesta-Namesilo
-
Notifications
You must be signed in to change notification settings - Fork 8
/
namesilo.php
3704 lines (3216 loc) · 141 KB
/
namesilo.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
/**
* Namesilo Module
*
* @package blesta
* @subpackage blesta.components.modules.namesilo
* @copyright Copyright (c) 2010, Phillips Data, Inc.
* @link http://www.blesta.com/ Blesta
* @copyright Copyright (c) 2015-2018, NETLINK IT SERVICES
* @link http://www.netlink.ie/ NETLINK
*/
class Namesilo extends RegistrarModule
{
/**
* @var array Namesilo response codes
*/
private static $codes;
/**
* @var string Default module view path
*/
private static $defaultModuleView;
private $api;
/**
* Initializes the module
*/
public function __construct()
{
// Load config.json
$this->loadConfig(__DIR__ . DS . 'config.json');
// Load models required by this module
Loader::loadModels($this, ['PluginManager']);
// Load components required by this module
Loader::loadComponents($this, ['Input', 'Record']);
// Load the language required by this module
Language::loadLang('namesilo', null, __DIR__ . DS . 'language' . DS);
// Load configuration
Configure::load('namesilo', __DIR__ . DS . 'config' . DS);
// Get Namesilo response codes
self::$codes = Configure::get('Namesilo.status.codes');
// Set default module view
self::$defaultModuleView = 'components' . DS . 'modules' . DS . 'namesilo' . DS;
}
/**
* 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)
{
// Upgrade if possible
if (version_compare($this->getVersion(), $current_version, '>')) {
// Handle the upgrade, set errors using $this->Input->setErrors() if any errors encountered
if (!isset($this->Record)) {
Loader::loadComponents($this, ['Record']);
}
// Upgrade to 2.0.0
if (version_compare($current_version, '2.0.0', '<')) {
$rows = $this->getRows();
foreach ($rows as $row) {
// Delete tld pricing
$this->Record->from('module_row_meta')
->where('module_row_meta.module_row_id', '=', $row->id)
->open()
->where('module_row_meta.key', 'LIKE', 'tld_%_pricing')
->orwhere('module_row_meta.key', '=', 'tld_packages_map')
->orwhere('module_row_meta.key', '=', 'tld_packages_settings')
->close()
->delete();
}
}
// Upgrade to 2.1.0
if (version_compare($current_version, '2.1.0', '<')) {
Cache::clearCache(
'tlds_prices',
Configure::get('Blesta.company_id') . DS . 'modules' . DS . 'namesilo' . DS
);
}
// Upgrade to 3.4.0
if (version_compare($current_version, '3.4.0', '<')) {
if (!isset($this->Record)) {
Loader::loadComponents($this, ['Record']);
}
$module_rows = $this->getRows();
foreach($module_rows as $module_row) {
$api = $this->getApi($module_row->meta->user, $module_row->meta->key, $module_row->meta->sandbox == 'true');
$domains = new NamesiloDomains($api);
$this->setContactsFromServices($domains, $module_row);
}
}
}
}
private function setContactsFromServices($domains, $module_row, $client_id = null)
{
$this->Record->from('services')->
select(['services.*', 'service_fields.value' => 'domain'])->
on('service_fields.key', '=', 'domain')->
innerJoin('service_fields', 'service_fields.service_id', '=', 'services.id', false)->
where('services.module_row_id', '=', $module_row->id)->
where('services.status', '=', 'active');
if ($client_id) {
$this->Record->where('services.client_id', '=', $client_id);
}
$services = $this->Record->fetchAll();
$contactsInfo = $domains->getContacts([]);
if ((self::$codes[$contactsInfo->status()][1] ?? 'fail') == 'fail') {
$this->processResponse($this->api, $contactsInfo);
}
$contacts = [];
$contact_response = $contactsInfo->response();
if (isset($contact_response->contact)) {
$namesilo_contacts = is_array($contact_response->contact)
? $contactsInfo->response()->contact
: [$contactsInfo->response()->contact];
foreach ($namesilo_contacts as $contact) {
$contacts[$contact->contact_id] = $contact->first_name . ' ' . $contact->last_name;
}
}
$client_contacts = [];
foreach ($services as $service) {
$domainInfo = $domains->getDomainInfo(['domain' => $service->domain]);
if ((self::$codes[$domainInfo->status()][1] ?? 'fail') == 'fail') {
$this->processResponse($this->api, $domainInfo);
continue;
}
$contact_ids = $domainInfo->response(true)['contact_ids'];
if (!isset($client_contacts[$service->client_id])) {
$client_contacts[$service->client_id] = [];
}
foreach ($contact_ids as $contact_id) {
$client_contacts[$service->client_id][$contact_id] = $contacts[$contact_id];
}
}
foreach ($client_contacts as $client_id => $contacts) {
$this->Record->duplicate('module_id', '=', $module_row->module_id)->
duplicate('module_row_id', '=', $module_row->id)->
duplicate('client_id', '=', $client_id)->
duplicate('key', '=', 'contacts')->
insert(
'module_client_meta',
[
'module_id' => $module_row->module_id,
'module_row_id' => $module_row->id,
'client_id' => $client_id,
'key' => 'contacts',
'value' => json_encode($contacts)
]
);
}
}
/**
* Gets a list of name server data associated with a domain
*
* @param string $domain The domain to lookup
* @param int $module_row_id The ID of the module row to fetch for the current module
* @return array A list of name servers, each with the following fields:
*
* - url The URL of the name server
* - ips A list of IPs for the name server
*/
public function getDomainNameServers($domain, $module_row_id = null)
{
$row = $this->getModuleRow($module_row_id);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
$dns = new NamesiloDomainsDns($api);
$response = $dns->getList(['domain' => $domain]);
$this->processResponse($api, $response);
$result = $response->response();
$nameservers = [];
if (isset($result->nameservers)) {
foreach ($result->nameservers->nameserver as $ns) {
$nameservers[] = [
'url' => trim($ns),
'ips' => [gethostbyname(trim($ns))]
];
}
}
return $nameservers;
}
/**
* Assign new name servers to a domain
*
* @param string $domain The domain for which to assign new name servers
* @param int|null $module_row_id The ID of the module row to fetch for the current module
* @param array $vars A list of name servers to assign (e.g. [ns1, ns2])
* @return bool True if the name servers were successfully updated, false otherwise
*/
public function setDomainNameservers($domain, $module_row_id = null, array $vars = [])
{
$row = $this->getModuleRow($module_row_id);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
$dns = new NamesiloDomainsDns($api);
$args = [];
$i = 1;
foreach ($vars as $ns) {
$args['ns' . $i] = $ns;
$i++;
}
$args['domain'] = $domain;
$response = $dns->setCustom($args);
$this->processResponse($api, $response);
return self::$codes[$response->status()][1] == 'success';
}
/**
* 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 bool True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null)
{
$rules = [];
// Transfers (EPP Code)
if (isset($vars['transfer']) && ($vars['transfer'] == '1' || $vars['transfer'] == true)) {
$rule = [
'auth' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.epp.empty', true),
'post_format' => 'trim'
]
],
];
$rules = array_merge($rules, $rule);
}
// .us fields
if (isset($vars['usnc']) || isset($vars['usap'])) {
$rule = [
'usnc' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.US.RegistrantNexus.empty', true),
'post_format' => 'trim',
'final' => true
],
'valid' => [
'rule' => ['array_key_exists', Configure::get('Namesilo.domain_fields.us')['usnc']['options']],
'message' => Language::_('Namesilo.!error.US.RegistrantNexus.invalid', true)
]
],
'usap' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.US.RegistrantPurpose.empty', true),
'post_format' => 'trim',
'final' => true
],
'valid' => [
'rule' => ['array_key_exists', Configure::get('Namesilo.domain_fields.us')['usap']['options']],
'message' => Language::_('Namesilo.!error.US.RegistrantPurpose.invalid', true)
]
],
];
$rules = array_merge($rules, $rule);
}
// .ca fields
if (isset($vars['calf']) || isset($vars['cawd']) || isset($vars['caln'])) {
$rule = [
'calf' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.CA.CIRALegalType.empty', true),
'post_format' => 'trim',
'final' => true
],
'valid' => [
'rule' => ['array_key_exists', Configure::get('Namesilo.domain_fields.ca')['calf']['options']],
'message' => Language::_('Namesilo.!error.CA.CIRALegalType.invalid', true)
],
'other' => [
'rule' => ['matches', '/^OTHER$/'],
'negate' => true,
'message' => Language::_('Namesilo.!error.CA.CIRALegalType.other', true)
]
],
'cawd' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.CA.CIRAWhoisDisplay.empty', true),
'post_format' => 'trim',
'final' => true
],
'valid' => [
'rule' => ['array_key_exists', Configure::get('Namesilo.domain_fields.ca')['cawd']['options']],
'message' => Language::_('Namesilo.!error.CA.CIRAWhoisDisplay.invalid', true)
]
],
'caln' => [
'empty' => [
'rule' => ['isEmpty'],
'negate' => true,
'message' => Language::_('Namesilo.!error.CA.CIRALanguage.empty', true),
'post_format' => 'trim',
'final' => true
],
'valid' => [
'rule' => ['array_key_exists', Configure::get('Namesilo.domain_fields.ca')['caln']['options']],
'message' => Language::_('Namesilo.!error.CA.CIRALanguage.invalid', true)
]
],
];
$rules = array_merge($rules, $rule);
}
if (isset($rules) && count($rules) > 0) {
$this->Input->setRules($rules);
return $this->Input->validates($vars);
}
return true;
}
/**
* 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'
)
{
$row = $this->getModuleRow($package->module_row);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
#
# TODO: Handle validation checks
# TODO: Fix nameservers
#
if (isset($vars['domain'])) {
$tld = $this->getTld($vars['domain'], $row);
$vars['domain'] = trim($vars['domain']);
}
$input_fields = array_merge(
Configure::get('Namesilo.domain_fields'),
(array) Configure::get('Namesilo.domain_fields' . $tld),
(array) Configure::get('Namesilo.nameserver_fields'),
(array) Configure::get('Namesilo.transfer_fields'),
['years' => true, 'transfer' => $vars['transfer'] ?? 1, 'private' => 0]
);
// Set the whois privacy field based on the config option
if (isset($vars['configoptions']['id_protection'])) {
$vars['private'] = $vars['configoptions']['id_protection'];
}
// .ca and .us domains can't have traditional whois privacy
if ($tld == '.ca' || $tld == '.us') {
unset($input_fields['private']);
}
if (isset($vars['use_module']) && $vars['use_module'] == 'true') {
if ($package->meta->type == 'domain') {
$vars['years'] = 1;
foreach ($package->pricing as $pricing) {
if ($pricing->id == $vars['pricing_id']) {
$vars['years'] = $pricing->term;
break;
}
}
if (!isset($this->ModuleClientMeta)) {
Loader::loadModels($this, ['ModuleClientMeta']);
}
$whois_fields = Configure::get('Namesilo.whois_fields');
// Set all whois info from client ($vars['client_id'])
if (!isset($this->Clients)) {
Loader::loadModels($this, ['Clients']);
}
if (!isset($this->Contacts)) {
Loader::loadModels($this, ['Contacts']);
}
$client = $this->Clients->get($vars['client_id']);
if ($client) {
$contact_numbers = $this->Contacts->getNumbers($client->contact_id);
}
foreach ($whois_fields as $key => $value) {
$input_fields[$value['rp']] = true;
if (strpos($key, 'phone') !== false) {
$vars[$value['rp']] = $this->formatPhone(
isset($contact_numbers[0]) ? $contact_numbers[0]->number : null,
$client->country
);
} else {
$vars[$value['rp']] =
(isset($value['lp']) && !empty($value['lp'])) ? $client->{$value['lp']} : 'NA';
}
}
$fields = array_intersect_key($vars, $input_fields);
$default_meta = $this->ModuleClientMeta->get(
$vars['client_id'],
'default_contact_id',
$row->module_id,
$row->id
);
if (isset($default_meta->value)) {
$fields['contact_id'] = $default_meta->value;
}
if (!empty($row->meta->portfolio)) {
$fields['portfolio'] = $row->meta->portfolio;
}
if (!empty($row->meta->payment_id)) {
$fields['payment_id'] = $row->meta->payment_id;
}
// for .ca domains we need to create a special contact to use
if ($tld == '.ca') {
$domains = new NamesiloDomains($api);
$response = $domains->addContacts($vars);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
return;
}
$fields['contact_id'] = $response->response()->contact_id;
}
// Validate if the provided term is valid
if (!$this->isValidTerm($tld, $fields['years'] ?? 1, !empty($vars['auth']))) {
$this->Input->setErrors(
['term' => ['invalid' => Language::_('Namesilo.!error.invalid_term', true)]]
);
return;
}
// Handle transfer
if (isset($vars['auth']) && $vars['auth']) {
$transfer = new NamesiloDomainsTransfer($api);
$response = $transfer->create($fields);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
if (isset($vars['contact_id'])) {
$domains->deleteContacts(['contact_id' => $vars['contact_id']]);
}
return;
}
} else {
// Handle registration
$domains = new NamesiloDomains($api);
$response = $domains->create($fields);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
// if namesilo is running a promotion on registrations we have to work around their system if
// we are doing a multi-year registration
$error = 'Invalid number of years, or no years provided.';
if (reset($this->Input->errors()['errors']) === $error) {
// unset the errors since we are working around it
$this->Input->setErrors([]);
// set the registration length to 1 year and save the remainder for an extension
$total_years = $fields['years'];
$fields['years'] = 1;
$response = $domains->create($fields);
$this->processResponse($api, $response);
// now extend the remainder of the years
$fields['years'] = $total_years - 1;
$response = $domains->renew($fields);
$this->processResponse($api, $response);
}
if (isset($vars['contact_id'])) {
$domains->deleteContacts(['contact_id' => $vars['contact_id']]);
}
return;
}
}
}
}
$meta = [];
$fields = array_intersect_key($vars, $input_fields);
foreach ($fields as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
return $meta;
}
/**
* 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 = [], $parent_package = null, $parent_service = null)
{
$row = $this->getModuleRow($service->module_row_id ?? $package->module_row);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
$domains = new NamesiloDomains($api);
// Manually renew the domain
$renew = isset($vars['renew']) ? (int) $vars['renew'] : 0;
if ($renew > 0 && $vars['use_module'] == 'true') {
$this->renewService($package, $service, $parent_package, $parent_service, $renew);
unset($vars['renew']);
}
// Handle whois privacy via config option
$id_protection = $this->featureServiceEnabled('id_protection', $service);
if (!$id_protection && isset($vars['configoptions']['id_protection'])) {
$response = $domains->addPrivacy(['domain' => $this->getServiceDomain($service)]);
$this->processResponse($api, $response);
} elseif ($id_protection && !isset($vars['configoptions']['id_protection'])) {
$response = $domains->removePrivacy(['domain' => $this->getServiceDomain($service)]);
$this->processResponse($api, $response);
}
return null; // All this handled by admin/client tabs instead
}
/**
* Cancels the service on the remote server. Sets Input errors on failure,
* preventing the service from being canceled.
*/
public function cancelService($package, $service, $parent_package = null, $parent_service = null)
{
$row = $this->getModuleRow($service->module_row_id ?? $package->module_row);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
if ($package->meta->type == 'domain') {
$fields = $this->serviceFieldsToObject($service->fields);
$domains = new NamesiloDomains($api);
$response = $domains->setAutoRenewal($fields->{'domain'}, false);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
return;
}
}
return;
}
/**
* Suspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being suspended.
*/
public function suspendService($package, $service, $parent_package = null, $parent_service = null)
{
$row = $this->getModuleRow($service->module_row_id ?? $package->module_row);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
if ($package->meta->type == 'domain') {
$fields = $this->serviceFieldsToObject($service->fields);
// Make sure auto renew is off
$domains = new NamesiloDomains($api);
$response = $domains->setAutoRenewal($fields->{'domain'}, false);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
return;
}
}
return;
}
/**
* 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, $years = null)
{
$row = $this->getModuleRow($service->module_row_id ?? $package->module_row);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
// Renew domain renewDomain?version=1&type=xml&key=12345&domain=namesilo.com&years=2
if ($package->meta->type == 'domain') {
$fields = $this->serviceFieldsToObject($service->fields);
$vars = [
'domain' => $fields->{'domain'},
'years' => 1
];
if (!empty($row->meta->payment_id)) {
$vars['payment_id'] = $row->meta->payment_id;
}
if (!$years) {
foreach ($package->pricing as $pricing) {
if ($pricing->id == $service->pricing_id) {
$vars['years'] = $pricing->term;
break;
}
}
} else {
$vars['years'] = $years;
}
$domains = new NamesiloDomains($api);
$response = $domains->renew($vars);
$this->processResponse($api, $response);
if ($this->Input->errors()) {
return;
}
}
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 = [];
if (isset($vars['meta']) && is_array($vars['meta'])) {
// 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)
{
$meta = [];
if (isset($vars['meta']) && is_array($vars['meta'])) {
// 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 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 required models
Loader::loadModels($this, ['Languages', 'Settings', 'Currencies', 'Packages']);
// 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(self::$defaultModuleView);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
#
#
# TODO: add tab to check status of all transfers: check if possible with Namesilo...
# ref: NamesiloDomainsTransfer->getList()
#
#
$link_buttons = [];
foreach ($module->rows as $row) {
if (isset($row->meta->key)) {
$link_buttons = [
[
'name' => Language::_('Namesilo.manage.audit_domains', true),
'attributes' => [
'href' => $this->base_uri . 'settings/company/modules/addrow/' . $module->id .
'?action=audit_domains'
]
]
];
break;
}
}
$this->view->set('module', $module);
$this->view->set('link_buttons', $link_buttons);
$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)
{
$action = isset($_GET['action']) ? $_GET['action'] : null;
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View((!empty($action) ? $action : 'add_row'), 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView(self::$defaultModuleView);
// Load the helpers and models required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
Loader::loadModels($this, ['Services', 'ModuleManager', 'Clients', 'ClientGroups']);
if ($action == 'audit_domains') {
$vars = [];
$module_row = $this->getRow();
$api = $this->getApi(
$module_row->meta->user,
$module_row->meta->key,
$module_row->meta->sandbox == 'true',
null,
true
);
$domains = new NamesiloDomains($api);
if ($module_row->meta->portfolio) {
$vars['portfolio'] = $module_row->meta->portfolio;
}
$response = $domains->getList($vars)->response();
$domain_list = (isset($response->domains->domain) ? $response->domains->domain : null);
$vars['domains'] = [];
if (!empty($domain_list)) {
if (!is_array($domain_list)) {
$domain_list = [$domain_list];
}
foreach ($domain_list as $domain) {
$record = $this->Record->select()->from('services')->leftJoin(
'service_fields',
'services.id',
'=',
'service_fields.service_id',
false
)->where('services.status', 'IN', ['active', 'suspended'])->where(
'service_fields.value',
'=',
$domain
)->where('services.module_row_id', '=', $module_row->id)->where(
'service_fields.key',
'=',
'domain'
)->numResults();
if (!$record) {
$vars['domains'][] = $domain;
}
}
}
// Set view
$this->view->set('vars', (object) $vars);
return $this->view->fetch();
} elseif ($action == 'get_renew_info') {
$service_id = isset($_GET['service_id']) ? $_GET['service_id'] : null;
if (is_null($service_id)) {
// exit() to prevent any output other than json from being rendered
exit();
}
// Load the API
$module_row = $this->getRow();
$api = $this->getApi(
$module_row->meta->user,
$module_row->meta->key,
$module_row->meta->sandbox == 'true',
null,
true
);
// Get the domain renewal details for the service
$domains = new NamesiloDomains($api);
$vars = $this->getRenewInfo($service_id, $domains);
exit(json_encode($vars));
} else {
// Set unspecified checkboxes
if (empty($vars['sandbox'])) {
$vars['sandbox'] = 'false';
}
// Set view
$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(self::$defaultModuleView);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
if (empty($vars)) {
$vars = (array) $module_row->meta;
}
$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)
{
$meta_fields = ['user', 'key', 'sandbox', 'portfolio', 'payment_id', 'namesilo_module'];
$encrypted_fields = ['key'];
// Set unspecified checkboxes
if (empty($vars['sandbox'])) {
$vars['sandbox'] = '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)) {