| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\submodules\OVH;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- class OVH extends dns\SubmoduleAbstract implements interfaces\SubmoduleImportInterface
- {
- public $configFields = array(
- 'application_key' =>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));
- }
- }
|