EuroDNS.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 EuroDNS extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  8. public $configFields = array(
  9. 'username' =>array (
  10. 'friendlyName' => 'Username',
  11. 'validators' => array(
  12. 'required' => 'required',
  13. )
  14. ),
  15. 'password' =>array (
  16. 'friendlyName' => 'Password',
  17. 'type'=> 'password',
  18. 'validators' => array(
  19. 'required' => 'required',
  20. )
  21. ),
  22. 'default_ip' =>array (
  23. 'friendlyName' => 'Default IP',
  24. 'validators' => array(
  25. 'pattern' => Patterns::IP4_OR_IP6,
  26. )
  27. ),
  28. );
  29. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'PTR', 'TXT');
  30. private function get($request) {
  31. $ch = curl_init();
  32. curl_setopt ($ch, CURLOPT_USERPWD, $this->config['username'] . ":" . $this->config['password']);
  33. curl_setopt ($ch, CURLOPT_URL, "https://secure.api-eurodns.com:20015/v2/");
  34. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 1);
  35. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 1);
  36. curl_setopt ($ch, CURLOPT_VERBOSE, 0);
  37. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  38. curl_setopt ($ch, CURLOPT_POST, 1);
  39. curl_setopt ($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode (urlencode ($request)));
  40. $response = curl_exec ($ch);
  41. if (curl_errno($ch)) {
  42. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  43. }
  44. if (empty($response) || $response === false) {
  45. throw new exceptions\DNSSubmoduleException("Unexpected error(0)", dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  46. }
  47. curl_close($ch);
  48. @$ret = simplexml_load_string($response, null, LIBXML_NOCDATA);
  49. if($ret === false) {
  50. throw new exceptions\DNSSubmoduleException("Unexpected error(1)", dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  51. }
  52. $code = (int)current($ret->result->attributes()->code);
  53. if($code !== 1000) {
  54. throw new exceptions\DNSSubmoduleException((string)$response->result->msg?:"Unknown Error", dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  55. }
  56. return $ret;
  57. }
  58. public function testConnection(){
  59. $request = '<?xml version="1.0" encoding="UTF-8"?>
  60. <request xmlns:agent="http://www.eurodns.com/agent">
  61. <agent:balance />
  62. </request> ';
  63. $this->get($request);
  64. }
  65. public function zoneExists() {
  66. try {
  67. $request = '<?xml version="1.0" encoding="UTF-8"?>
  68. <request xmlns:zone="http://www.eurodns.com/zone">
  69. <zone:info>
  70. <zone:name>'.$this->domain.'</zone:name>
  71. </zone:info>
  72. </request>';
  73. $response = $this->get($request);
  74. $main = $response->resData->children('http://www.eurodns.com/zone');
  75. return current($main->name) == $this->domain;
  76. } catch (exceptions\DNSSubmoduleException $e) {
  77. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  78. return false;
  79. }
  80. throw $e;
  81. }
  82. }
  83. public function activateZone() {
  84. $request = '<?xml version="1.0" encoding="UTF-8"?>
  85. <request xmlns:zone="http://www.eurodns.com/zone" xmlns:record="http://www.eurodns.com/record">
  86. <zone:create>
  87. <zone:name>'.$this->domain.'</zone:name>
  88. </zone:create>
  89. </request>';
  90. $this->get($request);
  91. }
  92. public function terminateZone() {
  93. //nie ma
  94. }
  95. public function getRecords($recordType = false) {
  96. $request = '<?xml version="1.0" encoding="UTF-8"?>
  97. <request xmlns:zone="http://www.eurodns.com/zone">
  98. <zone:info>
  99. <zone:name>'.$this->domain.'</zone:name>
  100. </zone:info>
  101. </request>';
  102. $response = $this->get($request);
  103. $main = $response->resData->children('http://www.eurodns.com/zone');
  104. $out = array();
  105. foreach($main->records->record as $r) {
  106. $record = $r->children('http://www.eurodns.com/record');
  107. if(in_array((string)$record->type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  108. $line = (string)$r->attributes()->id[0];
  109. $new_record = new dns\record\Record();
  110. $new_record->line = $line;
  111. $new_record->name = (string)$record->host;
  112. $new_record->type = (string)$record->type;
  113. $new_record->ttl = (string)$record->ttl;
  114. $new_record->createRDATAObject();
  115. switch((string)$record->type) {
  116. case 'MX':
  117. $new_record->rdata->preference = (string)$record->priority;
  118. $new_record->rdata->exchange = (string)$record->data;
  119. break;
  120. default:
  121. $new_record->rdata->fromString((string)$record->data);
  122. break;
  123. }
  124. $out[] = $new_record;
  125. }
  126. }
  127. return $out;
  128. }
  129. private function recordToXML(dns\record\Record $record) {
  130. $out = '<record:type>'.$record->type.'</record:type>
  131. <record:host>'.$record->name.'</record:host>
  132. <record:ttl>'.$record->ttl.'</record:ttl>';
  133. switch($record->type) {
  134. case 'MX':
  135. $out .= '<record:data>' . $record->rdata->exchange . '</record:data>
  136. <record:mx_priority>'.$record->rdata->preference.'</record:mx_priority>';
  137. break;
  138. default:
  139. $out .= '<record:data>' . $record->rdata->exchange . '</record:data>';
  140. break;
  141. }
  142. return $out;
  143. }
  144. public function addRecord(dns\record\Record $record) {
  145. $request = '<?xml version="1.0" encoding="UTF-8"?>
  146. <request xmlns:zone="http://www.eurodns.com/zone" xmlns:record="http://www.eurodns.com/record">
  147. <zone:update>
  148. <zone:name>'.$this->domain.'</zone:name>
  149. <zone:records>
  150. <zone:add>
  151. <zone:record>'.$this->recordToXML($record).'</zone:record>
  152. </zone:add>
  153. </zone:records>
  154. </zone:update>
  155. </request> ';
  156. $this->get($request);
  157. }
  158. public function editRecord(dns\record\Record $record) {
  159. $request = '<?xml version="1.0" encoding="UTF-8"?>
  160. <request xmlns:zone="http://www.eurodns.com/zone" xmlns:record="http://www.eurodns.com/record">
  161. <zone:update>
  162. <zone:name>'.$this->domain.'</zone:name>
  163. <zone:records>
  164. <zone:change>
  165. <zone:record id="'.$record->line.'">'.$this->recordToXML($record).'</zone:record>
  166. </zone:change>
  167. </zone:records>
  168. </zone:update>
  169. </request>';
  170. $this->get($request);
  171. }
  172. public function deleteRecord(dns\record\Record $record) {
  173. $request = '<?xml version="1.0" encoding="UTF-8"?>
  174. <request xmlns:zone="http://www.eurodns.com/zone" xmlns:record="http://www.eurodns.com/record">
  175. <zone:update>
  176. <zone:name>'.$this->domain.'</zone:name>
  177. <zone:records>
  178. <zone:remove>
  179. <zone:record id="'.$record->line.'"></zone:record>
  180. </zone:remove>
  181. </zone:records>
  182. </zone:update>
  183. </request> ';
  184. $this->get($request);
  185. }
  186. public function getZones() {
  187. $request = '<?xml version="1.0" encoding="UTF-8"?>
  188. <request xmlns:zone="http://www.eurodns.com/zone">
  189. <zone:list>
  190. <zone:tld>#TLD#</zone:tld>
  191. </zone:list>
  192. </request>';
  193. $response = $this->get($request);
  194. $out = array();
  195. $main = $response->resData->children('http://www.eurodns.com/zone');
  196. foreach($main->list as $zone) {
  197. $out[(string)$zone->name] = '';
  198. }
  199. return $out;
  200. }
  201. }