DNScom.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \Exception;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  7. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  8. class DNScom extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleImportInterface {
  9. public $configFields = array(
  10. 'token' =>array (
  11. 'friendlyName' => 'Token',
  12. 'validators' => array(
  13. 'required' => 'required',
  14. )
  15. ),
  16. 'test' => array(
  17. 'friendlyName' => 'Sandbox',
  18. 'type' => 'yesno',
  19. ),
  20. 'default_ip' =>array (
  21. 'friendlyName' => 'Default IP',
  22. 'validators' => array(
  23. 'pattern' => Patterns::IP4_OR_IP6,
  24. )
  25. ),
  26. );
  27. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');
  28. private function makeQuery($arr, $key="", $recurs=1) {
  29. if ($recurs >= 10)
  30. throw Exception('Maximum Recursions');
  31. $ret = "";
  32. foreach ($arr as $k => $v) {
  33. $k = urlencode($k);
  34. if(substr($k,-2,1) == '_') $k = substr($k,0, strrpos($k, '_')); // poor fix, yet works so far...
  35. if (is_array($v)) {
  36. $ret .= $this->makeQuery($v, $k, ++ $recurs);
  37. continue;
  38. }
  39. $k = ($key) ? urlencode($key).'[]' : urlencode($k);
  40. $v = urlencode($v);
  41. $ret .= "$k=$v&";
  42. }
  43. return $ret;
  44. }
  45. private function get($function, $params = array()) {
  46. $token = strtoupper($this->config['token']);
  47. if($this->config['test'])
  48. $host = "http://sandbox.dns.com/api/";
  49. else
  50. $host = "http://dns.com/api/";
  51. $url = $host.$function.'/?';
  52. $params['AUTH_TOKEN'] = $token;
  53. $url .= $this->makeQuery($params);
  54. $ch = curl_init();
  55. $chOptions = array (
  56. CURLOPT_URL => trim ($url),
  57. CURLOPT_RETURNTRANSFER => true,
  58. CURLOPT_TIMEOUT => 90
  59. );
  60. curl_setopt_array($ch, $chOptions);
  61. $out = curl_exec($ch);
  62. if (curl_errno($ch) || $out == false) {
  63. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  64. }
  65. curl_close($ch);
  66. $response = json_decode($out);
  67. if(is_null($response)) {
  68. throw new exceptions\DNSSubmoduleException("Cannot parse response", dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  69. }
  70. if(isset($response->meta->error) && $response->meta->error != '') {
  71. throw new exceptions\DNSSubmoduleException($response->meta->error, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  72. }
  73. //$ret->meta->success if nie
  74. return $response;
  75. }
  76. public function testConnection() {
  77. $this->get('getDomains');
  78. }
  79. /**
  80. * Sprawdza czy domena istnieje
  81. * @return boolean
  82. */
  83. public function zoneExists() {
  84. try {
  85. $ret = $this->get('getDomains', array(
  86. 'search_term' => $this->domain
  87. ));
  88. return $ret->meta->success;
  89. } catch (exceptions\DNSSubmoduleException $e) {
  90. return false;
  91. }
  92. }
  93. /** NEED TESTING **/
  94. public function getRecords($recordType = false) {
  95. $ret = $this->get('getHostnamesForDomain',array(
  96. 'domain' => $this->domain
  97. ));
  98. $out = array();
  99. foreach($ret->data as $host) {
  100. $ret = $this->get('getRRSetForHostname', array(
  101. 'domain' => $this->domain,
  102. 'host' => $host->name
  103. ));
  104. foreach($ret->data as $r) {
  105. if(in_array($r->type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  106. $record = new dns\record\Record();
  107. $record->line = $r>id;
  108. $record->name = $host->name;
  109. $record->type = $r->type;
  110. $record->ttl = $r->ttl;
  111. $record->createRDATAObject();
  112. switch($r->type) {
  113. case 'MX':
  114. $record->rdata->preference = $r->priority;
  115. $record->rdata->exchange = $r->answer;
  116. break;
  117. case 'SRV':
  118. $record->rdata->setDataFromArray((array)$r);
  119. $record->rdata->target = $r->answer;
  120. break;
  121. default:
  122. $record->rdata->setFirstProperty($r->answer);
  123. break;
  124. }
  125. $out[] = $record;
  126. }
  127. }
  128. }
  129. return $out;
  130. }
  131. private function recordToParamsArray(dns\record\Record $record) {
  132. $params = array(
  133. 'host' => $record->nameToRelative($this->domain),
  134. 'type' => $record->type,
  135. 'ttl' => $record->ttl,
  136. );
  137. switch($record->type) {
  138. case 'MX':
  139. $params['priority'] = $record->rdata->preference;
  140. $params['rdata'] = $record->rdata->exchange;
  141. break;
  142. case 'SRV':
  143. $params['priority'] = $record->rdata->priority;
  144. $params['weight'] = $record->rdata->weight;
  145. $params['port'] = $record->rdata->port;
  146. $params['rdata'] = $record->rdata->target;
  147. break;
  148. default:
  149. $params['rdata'] = $record->rdata->toString();
  150. break;
  151. }
  152. return $params;
  153. }
  154. public function addRecord(dns\record\Record $record) {
  155. $this->get('createHostname', array(
  156. 'domain' => $this->domain,
  157. 'host' => $record->nameToRelative($this->domain)
  158. ));
  159. $params = $this->recordToParamsArray($record);
  160. try {
  161. $this->get('createRRData', $params);
  162. } catch (Exception $exc) {
  163. $this->get('removeHostname', array(
  164. 'domain' => $this->domain,
  165. 'host' => $record->nameToRelative($this->domain),
  166. 'confirm' => '1'
  167. ));
  168. throw $exc;
  169. }
  170. }
  171. public function editRecord(dns\record\Record $record) {
  172. $params = $this->recordToParamsArray($record);
  173. $params['rr_id'] = $record->line;
  174. $this->get('updateRRData', $params);
  175. }
  176. public function deleteRecord(dns\record\Record $record) {
  177. $records = $this->getRecords();
  178. $count = 0;
  179. foreach($records as $search) {
  180. if($search['name'] == $record->nameToRelative($this->domain)
  181. || $search['name'] == $record->nameToAbsolute($this->domain)) {
  182. $count++;
  183. if($count > 1) {
  184. break;
  185. }
  186. }
  187. }
  188. if($count == 1) {
  189. $ret = $this->get('removeHostname', array(
  190. 'domain' => $this->domain,
  191. 'host' => $record->nameToRelative($this->domain),
  192. 'confirm' => '1'
  193. ));
  194. } else {
  195. $ret = $this->get('removeRR', array(
  196. 'rr_id' => $record->line,
  197. 'confirm' => '1'
  198. ));
  199. }
  200. }
  201. public function activateZone() {
  202. $this->get('createDomain', array(
  203. 'domain' => $this->domain,
  204. 'mode' => 'advanced',
  205. ));
  206. }
  207. public function terminateZone() {
  208. $this->get('deleteDomain', array(
  209. 'domain' => $this->domain,
  210. 'confirm' => '1'
  211. ));
  212. }
  213. public function getZones() {
  214. $ret = $this->get('deleteDomain');
  215. $out = array();
  216. foreach($ret->data as $zone) {
  217. $out[$zone->name] = '';
  218. }
  219. }
  220. }