| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?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\interfaces;
- class AtomiaDNS extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
- public $configFields = array(
- 'hostname' =>array (
- 'friendlyName' => 'Hostname',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'username' =>array (
- 'friendlyName' => 'Username',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'password' =>array (
- 'friendlyName' => 'Password',
- 'type'=> 'password',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'ns_group' =>array (
- 'friendlyName' => 'Nameserver Group',
- ),
- 'c_ns1' =>array (
- 'friendlyName' => 'Nameserver 1',
- ),
- 'c_ns2' =>array (
- 'friendlyName' => 'Nameserver 2',
- ),
- 'c_ns3' =>array (
- 'friendlyName' => 'Nameserver 3',
- ),
- 'c_ns4' =>array (
- 'friendlyName' => 'Nameserver 4',
- ),
- );
-
- public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');
- public function testConnection() {
- $this->get('Noop');
- }
- public function zoneExists() {
- try {
- $this->get('GetZone', array($this->domain));
- } catch (exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
- return true;
- }
- public function getRecords($recordType = false) {
- $result = $this->get('GetZone', array($this->domain));
- $out = array();
- foreach($result as $tt) {
- foreach($tt->records as $data) {
- if(in_array((string)$data->type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $record = new dns\record\Record();
- $record->line = (string)$data->id;
- $record->name = (string)$data->label;
- $record->type = (string)$data->type;
- $record->ttl = (string)$data->ttl;
- $record->createRDATAObject();
- $record->rdata->fromString($data->rdata);
- $out[] = $record;
- }
- }
- }
- return $out;
- }
-
- public function addRecord(dns\record\Record $record) {
- $params = array(
- $this->domain,
- array(
- 'resourcerecord' => $this->recordToParamsArray($record),
- )
- );
-
- $this->get('AddDnsRecords', $params);
- }
- public function editRecord(dns\record\Record $record) {
-
- $r = array_merge(array('id' => $record->line), $this->recordToParamsArray($record));
-
- $params = array(
- $this->domain,
- array(
- 'resourcerecord' => $r,
- )
- );
-
- $this->get('EditDnsRecords', $params);
- }
-
- public function deleteRecord(dns\record\Record $record) {
- $r = array_merge(array('id' => $record->line), $this->recordToParamsArray($record));
- $params = array(
- $this->domain,
- array(
- 'resourcerecord' => $r,
- )
- );
-
- $this->get('DeleteDnsRecords', $params);
- }
- private function recordToParamsArray(dns\record\Record $record) {
- $name = $record->nameToRelative($this->domain);
- $params = array(
- 'label' => empty($name) ? '@' : $name,
- 'class' => $record->class,
- 'ttl' => $record->ttl,
- 'type' => $record->type,
- 'rdata' => $record->rdata->toString(),
- );
-
- return $params;
- }
- public function activateZone() {
- $ns = array();
- if(!empty($this->config['c_ns1']))
- $ns[] = $this->config['c_ns1'].'.';
- if(!empty($this->config['c_ns2']))
- $ns[] = $this->config['c_ns2'].'.';
- if(!empty($this->config['c_ns3']))
- $ns[] = $this->config['c_ns3'].'.';
- if(!empty($this->config['c_ns4']))
- $ns[] = $this->config['c_ns4'].'.';
-
- $params = array(
- $this->domain,
- 3600,
- $this->config['c_ns1'].'.',
- $this->config['c_ns2'].'.',
- 10800,
- 3600,
- 604800,
- 86400,
- $ns,
- $this->config['ns_group']? $this->config['ns_group'] : 'default'
- );
-
- $this->get('AddZone', $params);
- }
- public function terminateZone() {
- $this->get('DeleteZone', array($this->domain));
- }
- private function get($function, $params = false) {
- $header = array("X-Auth-Username: " . $this->config['username'], "X-Auth-Password: " . $this->config['password']);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'http://' . $this->config['hostname'] . '/pretty/atomiadns.json/' . $function);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if($params){
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
- }
- $result = curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- if (curl_errno($ch)) {
- throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- curl_close($ch);
-
- $data = json_decode($result);
- if(empty($data)) {
- throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- }
-
- if(isset($data->error_message) || $http_code != 200) {
- throw new exceptions\DNSSubmoduleException($data->error_message?:'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
-
- return $data;
- }
-
- private function array_values_recursive($array) {
- $out = array();
- foreach($array as $value) {
- if(is_array($value)) {
- $out[] = $this->array_values_recursive($value);
- } else {
- $out[] = $value;
- }
- }
- return $out;
- }
- public function getZones() {
- $result = $this->get('GetAllZones');
-
- $out = array();
- foreach($result as $zone) {
- $out[(string)$zone->name] = '';
- }
-
- return $out;
- }
- }
|