array ( 'friendlyName' => 'Application Key', 'validators' => array( 'required' => 'required' ) ), 'application_secret' =>array ( 'friendlyName' => 'Application Secret', 'validators' => array( 'required' => 'required' ) ), 'consumer_key' =>array ( 'friendlyName' => 'Consumer Key', 'validators' => array( 'required' => 'required' ) ), 'api_endpoint' => array( 'friendlyName' => 'Account Is', 'type' => 'select', 'options' => array( 'ovh-eu' => 'OVH-EU', 'ovh-ca' => 'OVH-CA', 'kimsufi-eu' => 'KIMSUFI-EU', 'kimsufi-ca' => 'KIMSUFI-CA', 'soyoustart-eu' => 'SOYOUSTART-EU', 'soyoustart-ca' => 'SOYOUSTART-CA', 'runabove-ca' => 'RUNABOVE-CA') ), 'minimized' => array( 'friendlyName' => 'Minimized', 'type' => 'yesno', 'help' => "Create only mandatory records" ) ); public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV','SPF','CAA'); protected $disabledPopulateNs = true; /** * @var OVH\Ovh; */ private $api; public function loadApiInstance() { if(!$this->api) { $request = new OVH\Api( $this->config['application_key'], $this->config['application_secret'], $this->config['api_endpoint'], $this->config['consumer_key']); $this->api = new OVH\Ovh($request); return $this->api; } return $this->api; } public function getZones() { $out = array(); $this->loadApiInstance(); try { $zones = $this->api->listZones(); } catch (\Exception $ex) { } foreach($zones as $zone) { $out[$zone] = ''; } return $out; } public function activateZone() { $this->loadApiInstance(); try { $result = $this->api->createZone(array( 'minimized' => $this->hasMinimizedOption(), 'zoneName' => $this->domain, )); $orderId = $result['orderId']; $paymentMethod = 'fidelityAccount'; $this->api->payOrder($orderId, array( 'paymentMean' => $paymentMethod )); } catch (\Exception $ex) { $this->parseApiResponse($ex->getMessage()); throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR); } return true; } public function addRecord(dns\record\Record $record) { $record->name = $this->toApiRecordName($record); $this->loadApiInstance(); try { $this->api->addRecord($this->domain,array( 'fieldType' => $record->type, 'subDomain' => $record->name, 'target' => $record->rdata->toString(), 'ttl' => $record->ttl )); $this->api->refreshZone($this->domain); } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage()); } return true; } public function deleteRecord(dns\record\Record $record) { $record->name = $this->toApiRecordName($record); $this->loadApiInstance(); try { $this->api->deleteRecord($this->domain, $record->line); $this->api->refreshZone($this->domain); } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage()); } return true; } public function editRecord(dns\record\Record $record) { $record->name = $this->toApiRecordName($record); $this->loadApiInstance(); try { switch ($record->type) { case 'SOA': $this->editSOArecord($record); break; default : $this->api->updateRecord($this->domain, $record->line, array( 'target' => $record->rdata->toString(), 'ttl' => (int)$record->ttl, 'subDomain' => $record->name )); break; } } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage(),dns\SubmoduleExceptionCodes::INVALID_PARAMETERS); } return true; } public function getRecords($recordType = false) { $this->loadApiInstance(); try { $this->checkZone(); $recordsId = $this->api->getRecords($this->domain); $records = array(); foreach($recordsId as $recordId) { $records[] = $this->api->getRecordProperties($this->domain, $recordId); } $out = []; foreach($records as $r) { $record = new dns\record\Record(); $record->line = $r['id']; $record->name = $r['subDomain']; $record->type = $r['fieldType']; $record->ttl = $r['ttl']; $record->createRDATAObject(); $record->rdata->fromString($r['target']); $out[] = $record; } if($this->hasRecordSupport('SOA')) { $soa = $this->api->getSOA($this->domain); $soaRecord = array(); $soaRecord['mname'] = $soa['server']; $soaRecord['rname'] = $soa['email']; $soaRecord['minimum'] = $soa['ttl']; $soaRecord['retry'] = $soa['nxDomainTtl']; $soaRecord['serial'] = $soa['serial']; $soaRecord['expire'] = $soa['expire']; $soaRecord['refresh'] = $soa['refresh']; $record = new dns\record\Record(); $record->name = $this->domain; $record->type = 'SOA'; $record->ttl = $soaRecord['ttl']; $record->createRDATAObject(); $record->rdata->setDataFromArray($soaRecord); $out[] = $record; } } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage(),dns\SubmoduleExceptionCodes::COMMAND_ERROR); } return $out; } public function terminateZone() { $this->loadApiInstance(); try { $this->api->terminateZone($this->domain); } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR); } return true; } public function testConnection() { $this->loadApiInstance(); try { $this->api->listZones(); } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM); } return true; } public function zoneExists() { $this->loadApiInstance(); try { $this->checkZone(); $zones = $this->api->listZones(); } catch (\Exception $ex) { throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR); } foreach($zones as $zone) { if($zone == $this->domain) { return true; } } return false; } public function hasMinimizedOption() { if($this->config['minimized'] == 'on') { return true; } return false; } private function editSOArecord(dns\record\Record $record) { return $this->api->updateSOA($this->domain,array( 'email' => $record->rdata->rname, 'nxDomainTtl' => $record->rdata->retry, 'refresh' => $record->rdata->refresh, 'ttl' => $record->rdata->minimum, 'serial' => $record->rdata->serial, 'server' => $record->rdata->mname, 'expire' => $record->rdata->expire )); } private function editDSrecord(dns\record\Record $record) { return $this->api->updateDSRecord($this->domain, array( 'keys' => array( 'algorithm' => $record->rdata->algorithm, 'flags' => $record->rdata->digesttype, 'publicKey' => $record->rdata->digest, 'tag' => $record->rdata->keytag ) )); } private function checkZone() { try { $zone = \MGModule\DNSManager2\models\custom\zone\Repository::factory()->setFilter('name', $this->domain)->get()[0]; $check = $this->api->getZoneInformation(array( 'minimized' => 'false', 'zoneName' => $this->domain )); } catch (\Exception $ex) { try { $check2 = $this->api->getZoneInformation2($this->domain); } catch (\Exception $ex) { if((int)$zone->status == 1) { throw new exceptions\DNSSubmoduleException('The zone probably is while processing. Please refresh the page in a few minutes.'); } } return; } if($check['orderId'] == NULL && (int)$zone->status == 1) { throw new exceptions\DNSSubmoduleException('The zone probably is while processing. Please refresh the page in a few minutes.'); } } private function parseApiResponse($response) { if(strpos($response, 'ObjectAlreadyExists') !== false) { throw new exceptions\DNSSubmoduleException('Zone server returns : The selected zone already exists.'); } } /** * Returns valid api record name * @param dns\record\Record $record * @return string|string[] */ private function toApiRecordName( dns\record\Record $record ) { return str_replace('@', '', $record->nameToRelative($this->domain)); } }