AtomiaDNS.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  6. class AtomiaDNS extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  7. public $configFields = array(
  8. 'hostname' =>array (
  9. 'friendlyName' => 'Hostname',
  10. 'validators' => array(
  11. 'required' => 'required',
  12. )
  13. ),
  14. 'username' =>array (
  15. 'friendlyName' => 'Username',
  16. 'validators' => array(
  17. 'required' => 'required',
  18. )
  19. ),
  20. 'password' =>array (
  21. 'friendlyName' => 'Password',
  22. 'type'=> 'password',
  23. 'validators' => array(
  24. 'required' => 'required',
  25. )
  26. ),
  27. 'ns_group' =>array (
  28. 'friendlyName' => 'Nameserver Group',
  29. ),
  30. 'c_ns1' =>array (
  31. 'friendlyName' => 'Nameserver 1',
  32. ),
  33. 'c_ns2' =>array (
  34. 'friendlyName' => 'Nameserver 2',
  35. ),
  36. 'c_ns3' =>array (
  37. 'friendlyName' => 'Nameserver 3',
  38. ),
  39. 'c_ns4' =>array (
  40. 'friendlyName' => 'Nameserver 4',
  41. ),
  42. );
  43. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');
  44. public function testConnection() {
  45. $this->get('Noop');
  46. }
  47. public function zoneExists() {
  48. try {
  49. $this->get('GetZone', array($this->domain));
  50. } catch (exceptions\DNSSubmoduleException $e) {
  51. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  52. return false;
  53. }
  54. throw $e;
  55. }
  56. return true;
  57. }
  58. public function getRecords($recordType = false) {
  59. $result = $this->get('GetZone', array($this->domain));
  60. $out = array();
  61. foreach($result as $tt) {
  62. foreach($tt->records as $data) {
  63. if(in_array((string)$data->type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  64. $record = new dns\record\Record();
  65. $record->line = (string)$data->id;
  66. $record->name = (string)$data->label;
  67. $record->type = (string)$data->type;
  68. $record->ttl = (string)$data->ttl;
  69. $record->createRDATAObject();
  70. $record->rdata->fromString($data->rdata);
  71. $out[] = $record;
  72. }
  73. }
  74. }
  75. return $out;
  76. }
  77. public function addRecord(dns\record\Record $record) {
  78. $params = array(
  79. $this->domain,
  80. array(
  81. 'resourcerecord' => $this->recordToParamsArray($record),
  82. )
  83. );
  84. $this->get('AddDnsRecords', $params);
  85. }
  86. public function editRecord(dns\record\Record $record) {
  87. $r = array_merge(array('id' => $record->line), $this->recordToParamsArray($record));
  88. $params = array(
  89. $this->domain,
  90. array(
  91. 'resourcerecord' => $r,
  92. )
  93. );
  94. $this->get('EditDnsRecords', $params);
  95. }
  96. public function deleteRecord(dns\record\Record $record) {
  97. $r = array_merge(array('id' => $record->line), $this->recordToParamsArray($record));
  98. $params = array(
  99. $this->domain,
  100. array(
  101. 'resourcerecord' => $r,
  102. )
  103. );
  104. $this->get('DeleteDnsRecords', $params);
  105. }
  106. private function recordToParamsArray(dns\record\Record $record) {
  107. $name = $record->nameToRelative($this->domain);
  108. $params = array(
  109. 'label' => empty($name) ? '@' : $name,
  110. 'class' => $record->class,
  111. 'ttl' => $record->ttl,
  112. 'type' => $record->type,
  113. 'rdata' => $record->rdata->toString(),
  114. );
  115. return $params;
  116. }
  117. public function activateZone() {
  118. $ns = array();
  119. if(!empty($this->config['c_ns1']))
  120. $ns[] = $this->config['c_ns1'].'.';
  121. if(!empty($this->config['c_ns2']))
  122. $ns[] = $this->config['c_ns2'].'.';
  123. if(!empty($this->config['c_ns3']))
  124. $ns[] = $this->config['c_ns3'].'.';
  125. if(!empty($this->config['c_ns4']))
  126. $ns[] = $this->config['c_ns4'].'.';
  127. $params = array(
  128. $this->domain,
  129. 3600,
  130. $this->config['c_ns1'].'.',
  131. $this->config['c_ns2'].'.',
  132. 10800,
  133. 3600,
  134. 604800,
  135. 86400,
  136. $ns,
  137. $this->config['ns_group']? $this->config['ns_group'] : 'default'
  138. );
  139. $this->get('AddZone', $params);
  140. }
  141. public function terminateZone() {
  142. $this->get('DeleteZone', array($this->domain));
  143. }
  144. private function get($function, $params = false) {
  145. $header = array("X-Auth-Username: " . $this->config['username'], "X-Auth-Password: " . $this->config['password']);
  146. $ch = curl_init();
  147. curl_setopt($ch, CURLOPT_URL, 'http://' . $this->config['hostname'] . '/pretty/atomiadns.json/' . $function);
  148. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  149. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  150. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  151. if($params){
  152. curl_setopt($ch, CURLOPT_POST, true);
  153. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  154. }
  155. $result = curl_exec($ch);
  156. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  157. if (curl_errno($ch)) {
  158. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  159. }
  160. curl_close($ch);
  161. $data = json_decode($result);
  162. if(empty($data)) {
  163. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  164. }
  165. if(isset($data->error_message) || $http_code != 200) {
  166. throw new exceptions\DNSSubmoduleException($data->error_message?:'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  167. }
  168. return $data;
  169. }
  170. private function array_values_recursive($array) {
  171. $out = array();
  172. foreach($array as $value) {
  173. if(is_array($value)) {
  174. $out[] = $this->array_values_recursive($value);
  175. } else {
  176. $out[] = $value;
  177. }
  178. }
  179. return $out;
  180. }
  181. public function getZones() {
  182. $result = $this->get('GetAllZones');
  183. $out = array();
  184. foreach($result as $zone) {
  185. $out[(string)$zone->name] = '';
  186. }
  187. return $out;
  188. }
  189. }