Zonomi.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  7. class Zonomi extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  8. public $configFields = array(
  9. 'apikey' =>array (
  10. 'friendlyName' => 'API Key',
  11. 'validators' => array(
  12. 'required' => 'required',
  13. )
  14. ),
  15. 'default_ip' =>array (
  16. 'friendlyName' => 'Default IP',
  17. 'validators' => array(
  18. 'required' => 'required',
  19. 'pattern' => Patterns::IP4_OR_IP6,
  20. )
  21. ),
  22. );
  23. public $availableTypes = array('A', 'NS', 'MX', 'CNAME', 'TXT');
  24. private function get($params, $create = false) {
  25. if(empty(trim($this->config['apikey']))){
  26. throw new exceptions\DNSSubmoduleException("Authorization required", dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  27. }
  28. if($create)
  29. $url = 'https://zonomi.com/app/dns/addzone.jsp?api_key='.trim($this->config['apikey']);
  30. else
  31. $url = 'https://zonomi.com/app/dns/dyndns.jsp?api_key='.trim($this->config['apikey']);
  32. if(!empty($params)) {
  33. foreach($params as $k => $v) {
  34. $url .= '&'.$k.'='.urlencode($v);
  35. }
  36. }
  37. $ch = curl_init();
  38. $chOptions = array (
  39. CURLOPT_URL => $url,
  40. CURLOPT_RETURNTRANSFER => true,
  41. CURLOPT_SSL_VERIFYPEER => false,
  42. CURLOPT_SSL_VERIFYHOST => false,
  43. CURLOPT_TIMEOUT => 90
  44. );
  45. curl_setopt_array($ch, $chOptions);
  46. $out = curl_exec($ch);
  47. if (curl_errno($ch)) {
  48. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  49. }
  50. curl_close($ch);
  51. @$parsed = simplexml_load_string($out);
  52. if($parsed===FALSE && !$create) {
  53. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  54. }
  55. if((string)$parsed->is_ok == 'OK:')
  56. return $parsed;
  57. if(isset($parsed->error)) {
  58. throw new exceptions\DNSSubmoduleException((string)$parsed->error, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  59. }
  60. throw new exceptions\DNSSubmoduleException((string)trim($parsed)?:'Unexpected error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  61. }
  62. public function testConnection()
  63. {
  64. $params = array(
  65. 'action' => 'QUERYZONES',
  66. // 'name' => $this->domain
  67. );
  68. $this->get($params);
  69. }
  70. public function zoneExists() {
  71. $params = array(
  72. 'action' => 'QUERY',
  73. 'name' => $this->domain
  74. );
  75. try {
  76. $this->get($params);
  77. } catch (exceptions\DNSSubmoduleException $e) {
  78. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  79. return false;
  80. }
  81. throw $e;
  82. }
  83. return true;
  84. }
  85. public function getRecords($recordType = false) {
  86. $params = array(
  87. 'action' => 'QUERY',
  88. //'type' => $recordType,
  89. 'name' => '**.'.$this->domain
  90. );
  91. $out = array();
  92. $result = $this->get($params);
  93. foreach($result->actions->action->record as $data) {
  94. if(in_array((string)$data['type'], $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  95. $record = new dns\record\Record(); //A gdzie jest id?
  96. $record->name = (string)$data['name'];
  97. $record->type = (string)$data['type'];
  98. $record->ttl = (string)$data['ttl'];
  99. $record->createRDATAObject();
  100. switch((string)$data['type']) {
  101. case 'MX':
  102. $record->rdata->preference = (string)$data['prio'];
  103. $record->rdata->exchange = (string)$data['content'];
  104. break;
  105. default:
  106. $record->rdata->setFirstProperty((string)$data['content']);
  107. break;
  108. }
  109. $out[] = $record;
  110. }
  111. }
  112. return $out;
  113. }
  114. private function recordToParamsArray($record) {
  115. $params = array(
  116. 'name' => $record->nameToAbsolute($this->domain, false),
  117. 'type' => $record->type,
  118. 'ttl' => $record->ttl,
  119. );
  120. switch($record->type) {
  121. case 'MX':
  122. $params['prio'] = $record->rdata->preference;
  123. $params['value'] = $record->rdata->exchange;
  124. break;
  125. default:
  126. $params['value'] = $record->rdata->toString();
  127. break;
  128. }
  129. return $params;
  130. }
  131. public function addRecord(dns\record\Record $record) {
  132. $params = $this->recordToParamsArray($record);
  133. $params['action'] = 'SET';
  134. $this->get($params);
  135. }
  136. public function editRecord(dns\record\Record $record) {
  137. $this->addRecord($record); //?!oneone TODO: sprawdzic
  138. }
  139. public function deleteRecord(dns\record\Record $record) {
  140. $params = $this->recordToParamsArray($record); //TODO: sprawdzić bo wcześniej było troszkę inaczej
  141. $params['action'] = 'DELETE';
  142. $result = $this->get($params);
  143. if((int)$result->result_counts['unchanged'] > 0)
  144. throw new exceptions\DNSSubmoduleException('You cannot delete NS records', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  145. }
  146. public function activateZone() {
  147. $params = array(
  148. 'name' => $this->domain
  149. );
  150. $this->get($params, true);
  151. }
  152. public function terminateZone() {
  153. $params = array(
  154. 'name' => $this->domain,
  155. 'action' => 'DELETEZONE'
  156. );
  157. $this->get($params);
  158. }
  159. public function getZones() {
  160. $params = array(
  161. 'action' => 'QUERYZONES'
  162. );
  163. $ret = $this->get($params);
  164. $out = array();
  165. foreach($ret->actions->action->zone as $zone) {
  166. $attr=$zone->attributes();
  167. $out[(string)$attr['name']] = '';
  168. }
  169. return $out;
  170. }
  171. }