PowerDNSHosted.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. use \SoapClient;
  7. class PowerDNSHosted extends dns\SubmoduleAbstract implements
  8. interfaces\SubmoduleTTLInterface,
  9. interfaces\SubmoduleImportInterface
  10. {
  11. public $configFields = array(
  12. 'apikey' => array(
  13. 'friendlyName' => 'API Key',
  14. 'validators' => array(
  15. 'required' => 'required',
  16. )
  17. ),
  18. );
  19. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT');
  20. private $zoneID = false;
  21. private function get($function, $params = array())
  22. {
  23. $url = "https://www.powerdns.net/services/express.asmx?WSDL&apikey=" . $this->config['apikey'];
  24. $soap = new SoapClient($url, array("trace" => 1, "exceptions" => 0, "features" => SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS));
  25. $soap->__setLocation($url);
  26. $result = $soap->$function($params);
  27. if(is_a($result, 'SoapFault')) {
  28. throw new exceptions\DNSSubmoduleException('SOAP Error: ' . $result->getMessage(), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  29. }
  30. if($result == false) {
  31. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  32. }
  33. $n = $function . 'Result';
  34. if($result->$n->code != 100) {
  35. throw new exceptions\DNSSubmoduleException($result->$n->description? : 'Unknown error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  36. }
  37. return $result;
  38. }
  39. public function testConnection()
  40. {
  41. $this->get('listZones');
  42. }
  43. public function getZoneID()
  44. {
  45. if($this->zoneID !== false) {
  46. return $this->zoneID;
  47. }
  48. $out = $this->get('listZones', array());
  49. foreach($out->listZonesResult->Zones->Zone as $zone) {
  50. if((string)$zone->Name == $this->domain) {
  51. $this->zoneID = (int)$zone->Id;
  52. return (int)$zone->Id;
  53. break;
  54. }
  55. }
  56. throw new exceptions\DNSSubmoduleException('Zone does not exist', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  57. }
  58. public function zoneExists()
  59. {
  60. try {
  61. $this->getZoneID();
  62. return true;
  63. } catch(exceptions\DNSSubmoduleException $e) {
  64. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  65. return false;
  66. }
  67. throw $e;
  68. }
  69. }
  70. public function getRecords($recordType = false)
  71. {
  72. $ret = $this->get('listRecords', array('zoneId' => $this->getZoneID()));
  73. $out = array();
  74. foreach($ret->listRecordsResult->Records->Record as $r) {
  75. $type = strtoupper((string)$r->Type);
  76. if(in_array($type, $recordType !== false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  77. $new_record = new dns\record\Record();
  78. $new_record->line = (string)$r->Id;
  79. $new_record->name = (string)$r->Name;
  80. $new_record->type = $type;
  81. $new_record->ttl = (string)$r->TimeToLive;
  82. $new_record->createRDATAObject();
  83. switch($type) {
  84. case 'MX':
  85. $new_record->rdata->preference = (string)$r->Priority;
  86. $new_record->rdata->exchange = (string)$r->Content;
  87. break;
  88. default:
  89. $new_record->rdata->fromString((string)$r->Content);
  90. break;
  91. }
  92. $out[] = $new_record;
  93. }
  94. }
  95. return $out;
  96. }
  97. private function recordToParamsArray(dns\record\Record $record)
  98. {
  99. $params = array(
  100. 'zoneId' => $this->getZoneID(),
  101. 'Name' => $record->nameToAbsolute($this->domain, false),
  102. 'Type' => $record->type,
  103. 'Content' => $record->rdata->toString(),
  104. 'TimeToLive' => (int)$record->ttl,
  105. 'Priority' => (int)$record['priority']
  106. );
  107. switch($record->type) {
  108. case 'MX':
  109. $value = $record->rdata->exchange;
  110. $prio = $record->rdata->preference;
  111. break;
  112. default:
  113. $value = $record->rdata->toString();
  114. $prio = 0;
  115. break;
  116. }
  117. return $params;
  118. }
  119. public function addRecord(dns\record\Record $record)
  120. {
  121. $this->get('addRecordToZone', $this->recordToParamsArray($record));
  122. }
  123. public function editRecord(dns\record\Record $record)
  124. {
  125. $params = $this->recordToParamsArray($record);
  126. $params['recordID'] = (int)$record->line;
  127. $this->get('UpdateRecord', $params);
  128. }
  129. public function deleteRecord(dns\record\Record $record)
  130. {
  131. $params = array(
  132. 'recordId' => (int)$record->line,
  133. );
  134. $this->get('deleteRecordById', $params);
  135. }
  136. public function activateZone()
  137. {
  138. $this->get('addNativeDomain', array('domainName' => $this->domain));
  139. }
  140. public function terminateZone()
  141. {
  142. $this->get('deleteZoneByName', array('zoneName' => $this->domain));
  143. }
  144. public function getZones()
  145. {
  146. $ret = $this->get('listZones', array());
  147. $out = array();
  148. foreach($ret->listZonesResult->Zones->Zone as $zone) {
  149. $out[(string)$zone->Name] = (string)$zone->Master;
  150. }
  151. return $out;
  152. }
  153. }