| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?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\OpenProvider\OPRequest;
- use \MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPApi;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- use MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
- class OpenProvider extends dns\SubmoduleAbstract implements
- interfaces\SubmoduleTTLInterface,
- interfaces\SubmoduleImportInterface
- {
- public $configFields = array(
- 'username' =>array (
- 'friendlyName' => 'Username',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'password' =>array (
- 'friendlyName' => 'User Password',
- 'type'=> 'password',
- ),
- 'resellerAccount' =>array (
- 'friendlyName' => 'Reseller Account',
- 'type'=> 'yesno',
- ),
- 'apiURL' =>array (
- 'friendlyName' => 'API URL',
- ),
- 'default_ip' =>array (
- 'friendlyName' => 'Default IP',
- 'validators' => array(
- 'required' => 'required',
- 'pattern' => Patterns::IP4_OR_IP6,
- )
- ),
- 'debug' =>array (
- 'friendlyName' => 'Debugging',
- 'type'=> 'yesno',
- ),
- );
- public $availableTypes = array('A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'SOA', 'SPF', 'SRV', 'TXT');
- public function testConnection()
- {
- $this->executeCommand('searchZoneDnsRequest', array('limit' => 1));
- return true;
- }
- public function getRecords($recordType = false)
- {
- $result = $this->executeCommand('retrieveZoneDnsRequest',
- array(
- 'name' => $this->domain,
- 'withRecords' => 1,
- ));
- foreach($result->value['records'] as $r) {
- /* Adding dot for proper domain parsing */
- $r['name'] = $r['name'].'.';
- if(in_array((string)$r['type'], $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $recordAdapter = '\MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\Adapters\\'.$r['type'].'Adapter';
- if (class_exists($recordAdapter))
- {
- $return[] = $recordAdapter::toDnsManagerRecord($r);
- } else {
- $return[] = dns\record\Record::tryToCreateFromArray((array)$r);
- }
- }
- }
- foreach($return as $key => $record){
- $record->line = sha1(json_encode($record));
- }
- return $return;
- }
- public function addRecord(dns\record\Record $record)
- {
- $recordsData = $this->unsetDefaultRecords($this->getRecords());
- $adapterClassName = '\MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\Adapters\\'.$record->type.'Adapter';
- if (class_exists($adapterClassName))
- {
- /** @var dns\record\Record $record */
- $record = new $adapterClassName($record);
- }
- $recordsData[] = $record;
- $recordsToQuery = $this->groupRecords($recordsData);
- $domain = explode(".", $this->domain, 2);
- $this->executeCommand('modifyZoneDnsRequest',
- array('domain' => array(
- 'name' => $domain[0],
- 'extension' => $domain[1]),
- 'records' => $recordsToQuery,
- )
- );
- }
- public function editRecord(dns\record\Record $record)
- {
- $recordsData = $this->unsetDefaultRecords($this->getRecords());
- if (empty($recordsData))
- return false;
- $adapterClassName = '\MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\Adapters\\'.$record->type.'Adapter';
- if (class_exists($adapterClassName))
- {
- /** @var dns\record\Record $record */
- $record = new $adapterClassName($record);
- }
- foreach ($recordsData as $key => $recordData) {
- if ($recordData->line == $record->line) {
- $recordsData[$key] = $record;
- }
- }
- $recordsToQuery = $this->groupRecords($recordsData);
- $domain = explode(".", $this->domain, 2);
- $this->executeCommand('modifyZoneDnsRequest',
- array('domain' => array(
- 'name' => $domain[0],
- 'extension' => $domain[1]),
- 'records' => $recordsToQuery,
- )
- );
- }
- public function deleteRecord(dns\record\Record $record)
- {
- $recordsData = $this->unsetDefaultRecords($this->getRecords());
- if (empty($recordsData))
- return false;
- foreach ($recordsData as $key => $recordData) {
- if ($recordData->line == $record->line) {
- unset($recordsData[$key]);
- }
- }
- $recordsToQuery = $this->groupRecords($recordsData);
- if(empty($recordsToQuery))
- {
- return $this->reCreateZone();
- }
- $domain = explode(".", $this->domain, 2);
- $this->executeCommand('modifyZoneDnsRequest',
- array('domain' => array(
- 'name' => $domain[0],
- 'extension' => $domain[1]),
- 'records' => $recordsToQuery,
- )
- );
- }
- public function zoneExists()
- {
- $result = $this->executeCommand('searchZoneDnsRequest', array('namePattern' => '%'.$this->domain.'%'));
- if($result->value['total'] > 0)
- return true;
- else
- return false;
- }
- public function activateZone()
- {
- $domain = explode(".", $this->domain, 2);
- $this->executeCommand('createZoneDnsRequest',
- array('domain' => array(
- 'name' => $domain[0],
- 'extension' => $domain[1],
- ),
- 'type' => 'master',
- )
- );
- }
- public function terminateZone()
- {
- $domain = explode(".", $this->domain, 2);
- $this->executeCommand('deleteZoneDnsRequest',
- array('domain' => array(
- 'name' => $domain[0],
- 'extension' => $domain[1],
- ))
- );
- }
- public function getZones()
- {
- $out = [];
- $result = $this->executeCommand('searchZoneDnsRequest', ['namePattern' => '%']);
- foreach( $result->getValue()['results'] as $zone )
- {
- $out[$zone['name']] = '';
- }
- return $out;
- }
- private function groupRecords(array $recordsData)
- {
- $recordsToQuery = array();
- foreach($recordsData as $rdata) {
- /* Trimming last added dot for proper API usage */
- $rdata->name = rtrim($rdata->name, '.');
- /* For Root Records for proper API usage */
- if($rdata->name == $this->domain)
- {
- $rdata->name = '';
- }
- if($rdata->type == 'CAA')
- {
- $rdata->rdata->target = $this->addQuotes($rdata->rdata->target);
- }
- $rdataStringProperties = $this->parseOPDataToPrevious($rdata->rdata);
- $prio = null;
- if(isset($rdata->rdata->priority))
- {
- array_push($recordsToQuery, array('type' => $rdata->type, 'name' => str_replace('.'.$this->domain, '', $rdata->name), 'value' => $rdataStringProperties, 'ttl' => intval($rdata->ttl), 'prio' => intval($rdata->rdata->priority)));
- } elseif(isset($rdata->rdata->preference))
- {
- array_push($recordsToQuery, array('type' => $rdata->type, 'name' => str_replace('.'.$this->domain, '', $rdata->name), 'value' => $rdataStringProperties, 'ttl' => intval($rdata->ttl), 'prio' => intval($rdata->rdata->preference)));
- } else
- {
- array_push($recordsToQuery, array('type' => $rdata->type, 'name' => str_replace('.'.$this->domain, '', $rdata->name), 'value' => $rdataStringProperties, 'ttl' => intval($rdata->ttl)));
- }
- }
- return $recordsToQuery;
- }
- private function parseOPDataToPrevious( $opData)
- {
- $rdataStringProperties = '';
- foreach($opData as $key => $rdata)
- {
- if(!in_array($key, ['preference', 'priority']))
- {
- $rdataStringProperties .= $rdata . ' ';
- }
- }
- $rdataStringProperties = rtrim($rdataStringProperties);
- return $rdataStringProperties;
- }
- private function reCreateZone()
- {
- $this->terminateZone();
- $this->activateZone();
- }
- private function executeCommand( $command, array $args){
- $api = new OPApi ($this->config['apiURL']);
- $request = new OPRequest();
- $request->setCommand($command)
- ->setAuth(array('username' => $this->config['username'], 'password' => $this->config['password']))
- ->setArgs($args);
- if ($this->config['debug'] == 'on') {
- $result = $api->setDebug(1)->process($request);
- if ($result->getFaultCode() != 0)
- throw new exceptions\DNSSubmoduleException($result->getFaultString());
- return $result;
- } else {
- $result = $api->process($request);
- if ($result->getFaultCode() != 0)
- throw new exceptions\DNSSubmoduleException($result->getFaultString());
- return $result;
- }
- }
- private function addQuotes( $recordTarget)
- {
- return '"'.$recordTarget.'"';
- }
- /**
- * Unsetting default records - they are unmodifiable
- * @param array $records
- * @return array
- */
- private function unsetDefaultRecords(array $records)
- {
- $recordsCollection = collect($records);
- /* Getting default SOA Record array key */
- $soaRecord = $recordsCollection
- ->where('type', 'SOA')
- ->where('name', $this->domain.'.')
- ->where('rdata.rname', 'dns.openprovider.eu');
- if(count($soaRecord) < 0)
- {
- throw new exceptions\DNSSubmoduleException('Default SOA Record Error');
- }
- $soaRecordKey[] = $soaRecord->keys()->first();
- /* Getting default NS Records array keys */
- $nsRecords = $recordsCollection
- ->where('type', 'NS')
- ->where('name', $this->domain.'.')
- ->whereIn('rdata.nsdname', ['ns1.openprovider.nl', 'ns2.openprovider.be', 'ns3.openprovider.eu']);
- if(count($nsRecords) < 0)
- {
- throw new exceptions\DNSSubmoduleException('Default NS Records Error');
- }
- $nsRecordsKeys = $nsRecords->keys()->toArray();
- $defaultRecordsKeys = array_merge_recursive($soaRecordKey, $nsRecordsKeys);
- foreach($defaultRecordsKeys as $key)
- {
- unset($records[$key]);
- }
- return $records;
- }
- }
|