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; } }