-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensrs.php
1906 lines (1677 loc) · 70.7 KB
/
opensrs.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
/**
* OpenSRS Module
*
* @package blesta
* @subpackage blesta.components.modules.opensrs
* @copyright Copyright (c) 2021, Phillips Data, Inc.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.blesta.com/ Blesta
*/
class Opensrs extends RegistrarModule
{
/**
* 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('opensrs', null, dirname(__FILE__) . DS . 'language' . DS);
Configure::load('opensrs', dirname(__FILE__) . DS . 'config' . DS);
}
/**
* Get a list of the TLD prices
*
* @param int $module_row_id The ID of the module row to fetch for the current module
* @return array A list of all TLDs and their pricing
* [tld => [currency => [year# => ['register' => price, 'transfer' => price, 'renew' => price]]]]
*/
public function getTldPricing($module_row_id = null)
{
return $this->getFilteredTldPricing($module_row_id);
}
/**
* Get a filtered list of the TLD prices
*
* @param int $module_row_id The ID of the module row to fetch for the current module
* @param array $filters A list of criteria by which to filter fetched pricings including but not limited to:
*
* - tlds A list of tlds for which to fetch pricings
* - currencies A list of currencies for which to fetch pricings
* - terms A list of terms for which to fetch pricings
* @return array A list of all TLDs and their pricing
* [tld => [currency => [year# => ['register' => price, 'transfer' => price, 'renew' => price]]]]
* @see https://api.enom.com/docs/pe_getproductprice
*/
public function getFilteredTldPricing($module_row_id = null, $filters = [])
{
Loader::loadModels($this, ['Currencies']);
// Get all TLDs
$tlds = $this->getTlds();
// Filter TLDs
if (!empty($filters['tlds']) && is_array($filters['tlds'])) {
$tlds = [];
foreach ($filters['tlds'] as $tld) {
$tlds[] = '.' . ltrim($tld, '.');
}
unset($tld);
}
// Fetch the TLDs results from the cache, if they exist
$cache = Cache::fetchCache(
'tlds_prices',
Configure::get('Blesta.company_id') . DS . 'modules' . DS . 'opensrs' . DS
);
if ($cache) {
$pricing = unserialize(base64_decode($cache));
} else {
$pricing = [];
}
// Fetch pricing from the registrar
$row = $this->getModuleRow($module_row_id);
$api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true');
$domains = new OpensrsDomains($api);
// Get pricing
foreach ($tlds as $tld) {
if (isset($pricing[$tld])) {
continue;
}
$pricing[$tld] = [];
$vars = [
'domain' => 'tldpricingtestdomainblesta' . $tld,
'period' => 1,
'all_periods' => 1
];
// Set the registration prices
$register_response = $domains->getPrice(array_merge($vars, ['reg_type' => 'new']));
$this->processResponse($api, $register_response);
if ($register_response->status() != 'OK') {
continue;
}
foreach (range(1, 10) as $years) {
$pricing[$tld]['register'] = $register_response->response();
}
// Set the renewal prices
$renew_response = $domains->getPrice(array_merge($vars, ['reg_type' => 'renewal']));
$this->processResponse($api, $renew_response);
if ($renew_response->status() != 'OK') {
continue;
}
foreach (range(1, 10) as $years) {
$pricing[$tld]['renew'] = $renew_response->response();
}
// Set the transfer prices
$transfer_response = $domains->getPrice(array_merge($vars, ['reg_type' => 'transfer']));
$this->processResponse($api, $transfer_response);
if ($transfer_response->status() != 'OK') {
continue;
}
foreach (range(1, 10) as $years) {
$pricing[$tld]['transfer'] = $transfer_response->response();
}
}
unset($tld);
// Save pricing in cache
if (Configure::get('Caching.on') && is_writable(CACHEDIR)) {
try {
Cache::writeCache(
'tlds_prices',
base64_encode(serialize($pricing)),
strtotime(Configure::get('Blesta.cache_length')) - time(),
Configure::get('Blesta.company_id') . DS . 'modules' . DS . 'opensrs' . DS
);
} catch (Exception $e) {
// Write to cache failed, so disable caching
Configure::set('Caching.on', false);
}
}
// Save the TLDs results to the cache
$tld_yearly_prices = [];
if (!empty($pricing)) {
$currencies = $this->Currencies->getAll(Configure::get('Blesta.company_id'));
foreach ($pricing as $tld => $tld_data) {
// Filter by 'tlds'
if (isset($filters['tlds']) && !in_array($tld, $filters['tlds'])) {
continue;
}
foreach ($currencies as $currency) {
// Filter by 'currencies'
if (isset($filters['currencies']) && !in_array($currency->code, $filters['currencies'])) {
continue;
}
foreach (range(1, 10) as $years) {
// Filter by 'terms'
if (isset($filters['terms']) && !in_array($years, $filters['terms'])) {
continue;
}
$register_price = $this->Currencies->convert(
($tld_data['register']->attributes['prices'][$years] ?? 0),
'USD',
$currency->code,
Configure::get('Blesta.company_id')
);
$transfer_price = $this->Currencies->convert(
($tld_data['transfer']->attributes['prices'][$years] ?? 0),
'USD',
$currency->code,
Configure::get('Blesta.company_id')
);
$renewal_price = $this->Currencies->convert(
($tld_data['renew']->attributes['prices'][$years] ?? 0),
'USD',
$currency->code,
Configure::get('Blesta.company_id')
);
$tld_yearly_prices[$tld][$currency->code][$years] = [
'register' => $register_price,
'transfer' => $transfer_price,
'renew' => $renewal_price,
];
}
}
}
}
return $tld_yearly_prices;
}
/**
* 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)
{
$this->Input->setRules($this->getServiceRules($vars));
return $this->Input->validates($vars);
}
/**
* Attempts to validate an existing service against a set of service info updates. Sets Input errors on failure.
*
* @param stdClass $service A stdClass object representing the service to validate for editing
* @param array $vars An array of user-supplied info to satisfy the request
* @return bool True if the service update validates or false otherwise. Sets Input errors when false.
*/
public function validateServiceEdit($service, array $vars = null)
{
$this->Input->setRules($this->getServiceRules($vars, true));
return $this->Input->validates($vars);
}
/**
* Returns the rule set for adding/editing a service
*
* @param array $vars A list of input vars
* @param bool $edit True to get the edit rules, false for the add rules
* @return array Service rules
*/
private function getServiceRules(array $vars = null, $edit = false)
{
// Validate .fr TLD rules
$fr_fields = Configure::get('Opensrs.domain_fields.fr');
$rules = [
'tld_data[registrant_extra_info][registrant_type]' => [
'format' => [
'if_set' => true,
'rule' => [
'in_array',
array_keys($fr_fields['tld_data[registrant_extra_info][registrant_type]']['options'])
],
'message' => Language::_('Opensrs.!error.registrant_type.format', true)
]
],
'tld_data[registrant_extra_info][registrant_vat_id]' => [
'format' => [
'if_set' => true,
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Opensrs.!error.registrant_vat_id.format', true)
]
],
'tld_data[registrant_extra_info][siren_siret]' => [
'format' => [
'if_set' => true,
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Opensrs.!error.siren_siret.format', true)
]
],
'tld_data[registrant_extra_info][trademark_number]' => [
'format' => [
'if_set' => true,
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('Opensrs.!error.trademark_number.format', true)
]
]
];
// Set required fields based on the selected .fr TLD legal type
if (isset($vars['tld_data']['registrant_extra_info']['registrant_type'])) {
$company_fields = [
'tld_data[registrant_extra_info][registrant_vat_id]',
'tld_data[registrant_extra_info][siren_siret]',
'tld_data[registrant_extra_info][trademark_number]'
];
if ($vars['tld_data']['registrant_extra_info']['registrant_type'] == 'organization') {
foreach ($company_fields as $field) {
unset($rules[$field]);
}
}
}
return $rules;
}
/**
* 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'
) {
$tld = null;
$input_fields = [];
// Set domain fields
if (isset($vars['domain'])) {
$tld = $this->getTld($vars['domain']);
}
$whois_fields = Configure::get('Opensrs.whois_fields');
if (array_key_exists('auth_info', $vars)) {
$input_fields = array_merge(
Configure::get('Opensrs.transfer_fields'),
Configure::get('Opensrs.nameserver_fields'),
$whois_fields,
(array)Configure::get('Opensrs.domain_fields' . $tld)
);
} else {
$input_fields = array_merge(
Configure::get('Opensrs.domain_fields'),
Configure::get('Opensrs.nameserver_fields'),
$whois_fields,
(array)Configure::get('Opensrs.domain_fields' . $tld)
);
}
if (isset($vars['use_module']) && $vars['use_module'] == 'true') {
// Set registration period
$vars['period'] = 1;
foreach ($package->pricing as $pricing) {
if ($pricing->id == $vars['pricing_id']) {
$vars['period'] = $pricing->term;
break;
}
}
// Set all whois info from client
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) {
if (str_contains($key, 'first_name')) {
$vars[$key] = $client->first_name;
} elseif (str_contains($key, 'last_name')) {
$vars[$key] = $client->last_name;
} elseif (str_contains($key, 'org_name')) {
$vars[$key] = $client->company;
} elseif (str_contains($key, 'address1')) {
$vars[$key] = $client->address1;
} elseif (str_contains($key, 'address2')) {
$vars[$key] = $client->address2;
} elseif (str_contains($key, 'city')) {
$vars[$key] = $client->city;
} elseif (str_contains($key, 'state')) {
$vars[$key] = $client->state;
} elseif (str_contains($key, 'postal_code')) {
$vars[$key] = $client->zip;
} elseif (str_contains($key, 'country')) {
$vars[$key] = $client->country;
} elseif (str_contains($key, 'phone')) {
$vars[$key] = trim($this->formatPhone(
isset($contact_numbers[0]) ? $contact_numbers[0]->number : '11111111111',
$client->country
));
} elseif (str_contains($key, 'email')) {
$vars[$key] = $client->email;
}
if (empty($vars[$key])) {
if (str_contains($key, 'postal_code')) {
$vars[$key] = '33064';
} else {
$vars[$key] = 'NA';
}
}
}
// Set country for .asia domains
if ($tld == '.asia') {
$vars['tld_data[ced_info][locality_country]'] = $client->country;
}
// Build contacts array
Loader::loadHelpers($this, ['DataStructure']);
$this->Array = $this->DataStructure->create('Array');
$fields = $this->Array->unflatten(
array_intersect_key($this->Array->flatten($vars), $input_fields)
);
$fields['client_id'] = $vars['client_id'];
// Register domain
$this->registerDomain($vars['domain'], $package->module_row, $fields);
if ($this->Input->errors()) {
return;
}
// Set nameservers
$this->setDomainNameservers($vars['domain'], $package->module_row, [
$fields['nameserver_list'][0]['name'] ?? '',
$fields['nameserver_list'][1]['name'] ?? '',
$fields['nameserver_list'][2]['name'] ?? '',
$fields['nameserver_list'][3]['name'] ?? '',
]);
// Ignore nameserver errors
$this->Input->setErrors([]);
return [['key' => 'domain', 'value' => $vars['domain'], 'encrypted' => 0]];
}
$meta = [];
$fields = array_intersect_key($vars, $input_fields);
foreach ($fields as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
return $meta;
}
/**
* 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)
{
$fields = $this->serviceFieldsToObject($service->fields);
// Set expiration year
$expiration_date = $this->getExpirationDate($service);
$vars = [
'currentexpirationyear' => date('Y', strtotime(
!empty($expiration_date) ? $expiration_date : $service->date_renews
)),
'period' => 1
];
// Set renew period
foreach ($package->pricing as $pricing) {
if ($pricing->id == $service->pricing_id) {
$vars['period'] = $pricing->term;
break;
}
}
// Renew domain
$this->renewDomain($fields->domain, $package->module_row, $vars);
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 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 . 'opensrs' . 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 . 'opensrs' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
// Set unspecified checkboxes
if (!empty($vars)) {
if (empty($vars['sandbox'])) {
$vars['sandbox'] = '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 . 'opensrs' . 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['sandbox'])) {
$vars['sandbox'] = '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)
{
$meta_fields = ['user', 'key', 'sandbox'];
$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)) {
$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.
*
* @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)
{
// Same as adding
return $this->addModuleRow($vars);
}
/**
* 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();
// Set all TLD checkboxes
$tld_options = $fields->label(Language::_('Opensrs.package_fields.tld_options', true));
$tlds = $this->getTlds();
sort($tlds);
foreach ($tlds as $tld) {
$tld_label = $fields->label($tld, 'tld_' . $tld);
$tld_options->attach(
$fields->fieldCheckbox(
'meta[tlds][]',
$tld,
(isset($vars->meta['tlds']) && in_array($tld, $vars->meta['tlds'])),
['id' => 'tld_' . $tld],
$tld_label
)
);
}
$fields->setField($tld_options);
// Set nameservers
for ($i = 1; $i <= 5; $i++) {
$type = $fields->label(Language::_('Opensrs.package_fields.ns' . $i, true), 'opensrs_ns' . $i);
$type->attach(
$fields->fieldText(
'meta[ns][]',
($vars->meta['ns'][$i - 1] ?? null),
['id' => 'opensrs_ns' . $i]
)
);
$fields->setField($type);
}
return $fields;
}
/**
* 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, ['Form', 'Html']);
// Set default name servers
if (!isset($vars->ns1) && isset($package->meta->ns)) {
$i = 0;
foreach ($package->meta->ns as $ns) {
$vars->{'nameserver_list[' . $i++ . '][name]'} = $ns;
}
}
// Handle transfer request
if (isset($vars->transfer) || isset($vars->auth_code)) {
return $this->arrayToModuleFields(Configure::get('Opensrs.transfer_fields'), null, $vars);
} else {
// Set domain fields
if (isset($vars->domain)) {
$tld = $this->getTld($vars->domain);
}
// Handle domain registration
$module_fields = $this->arrayToModuleFields(
array_merge(
Configure::get('Opensrs.domain_fields'),
Configure::get('Opensrs.nameserver_fields'),
isset($tld) ? (array)Configure::get('Opensrs.domain_fields' . $tld) : []
),
null,
$vars
);
// Build the domain fields
$fields = $this->buildDomainModuleFields($vars);
if ($fields) {
$module_fields = $fields;
}
}
return ($module_fields ?? new ModuleFields());
}
/**
* 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)
{
// Set default name servers
if (!isset($vars->ns) && isset($package->meta->ns)) {
$i = 0;
foreach ($package->meta->ns as $ns) {
$vars->{'nameserver_list[' . $i++ . '][name]'} = $ns;
}
}
// Handle transfer request
if (isset($vars->transfer) || isset($vars->auth_code)) {
$fields = Configure::get('Opensrs.transfer_fields');
// We should already have the domain name don't make editable
$fields['domain']['type'] = 'hidden';
$fields['domain']['label'] = null;
return $this->arrayToModuleFields($fields, null, $vars);
} else {
// Set domain fields
if (isset($vars->domain)) {
$tld = $this->getTld($vars->domain);
}
// Handle domain registration
$fields = array_merge(
Configure::get('Opensrs.nameserver_fields'),
Configure::get('Opensrs.domain_fields'),
isset($tld) ? (Configure::get('Opensrs.domain_fields' . $tld) ?? []) : []
);
// We should already have the domain name don't make editable
$fields['domain']['type'] = 'hidden';
$fields['domain']['label'] = null;
$module_fields = $this->arrayToModuleFields($fields, null, $vars);
// Build the domain fields
$domain_fields = $this->buildDomainModuleFields($vars, true);
if ($domain_fields) {
$module_fields = $domain_fields;
}
}
// Determine whether this is an AJAX request
return ($module_fields ?? new ModuleFields());
}
/**
* Builds and returns the module fields for domain registration
*
* @param stdClass $vars An stdClass object representing the input vars
* @param $client True if rendering the client view, or false for the admin (optional, default false)
* return mixed The module fields for this service, or false if none could be created
*/
private function buildDomainModuleFields($vars, $client = false)
{
if (isset($vars->domain)) {
$tld = $this->getTld($vars->domain);
$extension_fields = Configure::get('Opensrs.domain_fields' . $tld);
if ($extension_fields) {
// Set the fields
if ($client) {
$fields = array_merge(
Configure::get('Opensrs.nameserver_fields'),
Configure::get('Opensrs.domain_fields'),
$extension_fields
);
} else {
$fields = array_merge(
Configure::get('Opensrs.domain_fields'),
Configure::get('Opensrs.nameserver_fields'),
$extension_fields
);
}
if ($client) {
// We should already have the domain name don't make editable
$fields['domain']['type'] = 'hidden';
$fields['domain']['label'] = null;
}
// Build the module fields
$module_fields = new ModuleFields();
// Allow AJAX requests
$ajax = $module_fields->fieldHidden('allow_ajax', 'true', ['id' => 'opensrs_allow_ajax']);
$module_fields->setField($ajax);
$please_select = ['' => Language::_('AppController.select.please', true)];
foreach ($fields as $key => $field) {
// Build the field
$label = $module_fields->label((isset($field['label']) ? $field['label'] : ''), $key);
$type = null;
if ($field['type'] == 'text') {
$type = $module_fields->fieldText(
$key,
($vars->{$key} ?? ''),
['id' => $key]
);
} elseif ($field['type'] == 'select') {
$type = $module_fields->fieldSelect(
$key,
(isset($field['options']) ? $please_select + $field['options'] : $please_select),
($vars->{$key} ?? ''),
['id' => $key]
);
} elseif ($field['type'] == 'hidden') {
$type = $module_fields->fieldHidden(
$key,
($vars->{$key} ?? ''),
['id' => $key]
);
}
// Include a tooltip if set
if (!empty($field['tooltip'])) {
$label->attach($module_fields->tooltip($field['tooltip']));
}
if ($type) {
$label->attach($type);
$module_fields->setField($label);
}
}
}
}
return ($module_fields ?? false);
}
/**
* Returns all tabs to display to an admin when managing a service
*
* @param stdClass $service A stdClass object representing the service
* @return array An array of tabs in the format of method => title.
* Example: ['methodName' => "Title", 'methodName2' => "Title2"]
*/
public function getAdminServiceTabs($service)
{
Loader::loadModels($this, ['Packages']);
$tabs = [
'tabWhois' => Language::_('Opensrs.tab_whois.title', true),
'tabNameservers' => Language::_('Opensrs.tab_nameservers.title', true),
'tabSettings' => Language::_('Opensrs.tab_settings.title', true)
];
return $tabs;
}
/**
* Returns all tabs to display to a client when managing a service.
*
* @param stdClass $service A stdClass object representing the service
* @return array An array of tabs in the format of method => title, or method => array where array contains:
*
* - name (required) The name of the link
* - icon (optional) use to display a custom icon
* - href (optional) use to link to a different URL
* Example:
* ['methodName' => "Title", 'methodName2' => "Title2"]
* ['methodName' => ['name' => "Title", 'icon' => "icon"]]
*/
public function getClientServiceTabs($service)
{
Loader::loadModels($this, ['Packages']);
$tabs = [
'tabClientWhois' => [
'name' => Language::_('Opensrs.tab_whois.title', true),
'icon' => 'fas fa-users'
],
'tabClientNameservers' => [
'name' => Language::_('Opensrs.tab_nameservers.title', true),
'icon' => 'fas fa-server'
],
'tabClientSettings' => [
'name' => Language::_('Opensrs.tab_settings.title', true),
'icon' => 'fas fa-cog'
]
];