diff --git a/opensrs/backwardcompatibility/dataconversion/domains/provisioning/SWRegister.php b/opensrs/backwardcompatibility/dataconversion/domains/provisioning/SWRegister.php index 74e01916..a3c04fe6 100644 --- a/opensrs/backwardcompatibility/dataconversion/domains/provisioning/SWRegister.php +++ b/opensrs/backwardcompatibility/dataconversion/domains/provisioning/SWRegister.php @@ -3,6 +3,7 @@ namespace opensrs\backwardcompatibility\dataconversion\domains\provisioning; use opensrs\backwardcompatibility\dataconversion\DataConversion; +use opensrs\Exception; class SWRegister extends DataConversion { @@ -52,10 +53,11 @@ class SWRegister extends DataConversion // if data->custom_nameservers == 1, nameserver_list // is populated with data->name1, data->name2 etc // up to max potential data->name10 - // 'nameserver_list' => 'data->nameserver_list', + 'nameserver_list' => 'data->nameserver_list', 'owner_confirm_address' => 'data->owner_confirm_address', 'period' => 'data->period', 'premium_price_to_verify' => 'data->premium_price_to_verify', + 'auth_info' => 'data->auth_info', 'reg_domain' => 'data->reg_domain', 'reg_username' => 'data->reg_username', 'reg_password' => 'data->reg_password', @@ -77,41 +79,74 @@ public function convertDataObject($dataObject, $newStructure = null) $newDataObject = $p->convertDataObject($dataObject, $newStructure); - // run customizations required by this particular class - - // set custom nameservers to nameserver_list + // Check if we need to handle custom nameservers if (isset($dataObject->data)) { - if ($dataObject->data->custom_nameservers == 1) { - $newDataObject->attributes->nameserver_list = array(); - - for ($j = 1; $j <= 10; ++$j) { - $tns = 'name'.$j; - $tso = 'sortorder'.$j; - - if ( - isset($dataObject->data->$tns) && - $dataObject->data->$tns != '' && - isset($dataObject->data->$tso) && - $dataObject->data->$tso - ) { - $nameserver = new \stdClass(); - $nameserver->name = $dataObject->data->{$tns}; - $nameserver->sortorder = $dataObject->data->{$tso}; - - $newDataObject->attributes->nameserver_list[] = $nameserver; + if ( + (isset($dataObject->data->custom_nameservers) && (int)$dataObject->data->custom_nameservers === 1) || + (isset($dataObject->data->custom_transfer_nameservers) && (int)$dataObject->data->custom_transfer_nameservers === 1) + ) { + // ✅ Normalize keys first + $dataObject->data->nameserver_list = array_values((array)$dataObject->data->nameserver_list); + + if (empty($dataObject->data->nameserver_list)) { + throw new Exception("nameserver_list is required when custom_nameservers or custom_transfer_nameservers is set to 1 "); + } + + // ✅ Set normalized list into attributes + $newDataObject->attributes->nameserver_list = $dataObject->data->nameserver_list; + + if (!empty($newDataObject->attributes->nameserver_list)) { + $normalizedList = []; + foreach ($dataObject->data->nameserver_list as $ns) { + $nsArray = (array) $ns; + if (!empty($nsArray['name'])) { + $normalizedList[] = [ + 'name' => $nsArray['name'], + 'sortorder' => (int) $nsArray['sortorder'] + ]; + } + } + $dataObject->data->nameserver_list = array_values($normalizedList); + + if (empty($normalizedList)) { + throw new Exception('nameserver_list is invalid — contains only empty names'); + } + + $newDataObject->attributes->nameserver_list = array_values($normalizedList); + } + + // ✅ Optional: legacy fallback logic if you still support name1..10 + if (empty($newDataObject->attributes->nameserver_list)) { + for ($j = 1; $j <= 10; ++$j) { + $tns = 'name' . $j; + $tso = 'sortorder' . $j; + + if ( + !empty($dataObject->data->$tns) && + !empty($dataObject->data->$tso) + ) { + $nameserver = new \stdClass(); + $nameserver->name = $dataObject->data->{$tns}; + $nameserver->sortorder = $dataObject->data->{$tso}; + + $newDataObject->attributes->nameserver_list[] = $nameserver; + } } } } } - if (isset($dataObject->personal)) { - $newDataObject->attributes->contact_set = new \stdClass(); - $newDataObject->attributes->contact_set->owner = $dataObject->personal; - $newDataObject->attributes->contact_set->admin = $dataObject->personal; - $newDataObject->attributes->contact_set->billing = $dataObject->personal; - $newDataObject->attributes->contact_set->tech = $dataObject->personal; + if (!isset($newDataObject->attributes->contact_set) || empty((array)$newDataObject->attributes->contact_set)) { + if (isset($dataObject->data->contact_set)) { + $newDataObject->attributes->contact_set = $dataObject->data->contact_set; + } elseif (isset($dataObject->personal)) { + $newDataObject->attributes->contact_set = new \stdClass(); + $newDataObject->attributes->contact_set->owner = $dataObject->personal; + $newDataObject->attributes->contact_set->admin = $dataObject->personal; + $newDataObject->attributes->contact_set->billing = $dataObject->personal; + $newDataObject->attributes->contact_set->tech = $dataObject->personal; + } } - // end customizations return $newDataObject; } diff --git a/opensrs/domains/provisioning/SWRegister.php b/opensrs/domains/provisioning/SWRegister.php index c3d891f2..a61289a7 100644 --- a/opensrs/domains/provisioning/SWRegister.php +++ b/opensrs/domains/provisioning/SWRegister.php @@ -3,6 +3,7 @@ namespace opensrs\domains\provisioning; use opensrs\Base; +use opensrs\Exception; class SWRegister extends Base { @@ -15,8 +16,9 @@ class SWRegister extends Base public $resultFullFormatted; public $resultFormatted; - public $requiredFields = array( - 'attributes' => array( + // Dynamic required fields per reg_type + public $requiredFieldsByType = array( + 'new' => array( 'domain', 'custom_nameservers', 'custom_tech_contact', @@ -24,8 +26,28 @@ class SWRegister extends Base 'reg_username', 'reg_password', 'reg_type', - ), - ); + ), + 'transfer' => array( + 'domain', + 'auth_info', + 'reg_username', + 'reg_password', + 'reg_type', + //'custom_transfer_nameservers' //is optional in API, enforced as domains existing if not set + ), + 'assign' => array( + 'domain', + 'reg_username', + 'reg_password', + 'reg_type', + ), + 'owner_change' => array( + 'domain', + 'reg_username', + 'reg_password', + 'reg_type', + ), + ); public function __construct($formatString, $dataObject, $returnFullResponse = true) { @@ -35,6 +57,8 @@ public function __construct($formatString, $dataObject, $returnFullResponse = tr $this->_validateObject($dataObject); + $this->validateAttributes((array)$dataObject->attributes); + $this->send($dataObject, $returnFullResponse); } @@ -43,13 +67,49 @@ public function __destruct() parent::__destruct(); } + private function validateAttributes($attributes) + { + if (!isset($attributes['reg_type'])) { + throw new Exception('SWRegister Error - Missing required field: reg_type'); + } + + $regType = strtolower($attributes['reg_type']); + + if (!isset($this->requiredFieldsByType[$regType])) { + throw new Exception("SWRegister Error - Unsupported reg_type: {$regType}"); + } + + foreach ($this->requiredFieldsByType[$regType] as $field) { + if (!isset($attributes[$field])) { + throw new Exception("SWRegister Error - Missing required field for '{$regType}': {$field}"); + } + } + + // Special handling for transfer - ensure custom_transfer_nameservers is always present as an array + if ($regType === 'transfer') { + if (!isset($attributes['custom_transfer_nameservers'])) { + $attributes['custom_transfer_nameservers'] = 0; + } + } + + // Example for .EU domain validation + if (isset($attributes['domain']) && preg_match('/\.eu$/i', $attributes['domain'])) { + if (empty($attributes['lang_pref'])) { + throw new Exception("SWRegister Error - .EU domains require 'lang_pref'."); + } + if (empty($attributes['eu_country_of_residence']) && empty($attributes['eu_country_of_citizenship'])) { + throw new Exception("SWRegister Error - .EU domains require 'eu_country_of_residence' or 'eu_country_of_citizenship'."); + } + } + + // You can extend here for other TLDs like .CA or .DE + } + public function customResponseHandling($arrayResult, $returnFullResponse = true) { - /* Added by BC : NG : 16-7-2014 : To set error message for Insufficient Funds */ - if (isset($arrayResult['attributes']['forced_pending']) and $arrayResult['attributes']['forced_pending'] != '' and $arrayResult['is_success'] == 1) { + if (isset($arrayResult['attributes']['forced_pending']) && $arrayResult['attributes']['forced_pending'] != '' && $arrayResult['is_success'] == 1) { $arrayResult['is_success'] = 0; - // Get Resonse Text 'Registration successful' when insufficient fund if ($arrayResult['response_text'] == 'Registration successful') { $arrayResult['response_text'] = 'Insufficient Funds'; }