DNSMadeEasy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 DNSMadeEasy extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface, interfaces\SubmoduleRDNSInterface
  7. {
  8. public $configFields = [
  9. 'apikey' => [
  10. 'friendlyName' => 'API Key',
  11. 'validators' => [
  12. 'required' => 'required',
  13. ]
  14. ],
  15. 'api_secret' => [
  16. 'friendlyName' => 'API Secret',
  17. 'type' => 'password',
  18. 'validators' => [
  19. 'required' => 'required',
  20. ]
  21. ],
  22. 'test' => [
  23. 'friendlyName' => 'Test Mode',
  24. 'type' => 'yesno',
  25. ],
  26. ];
  27. public $availableTypes = ['A', 'AAAA', 'ANAME', 'NS', 'MX', 'CNAME', 'SPF', 'SRV', 'TXT', 'PTR','HTTPRED'];
  28. private $domain_id;
  29. const API_BASE_URL = 'http://api.dnsmadeeasy.com/V2.0/';
  30. const API_BASE_URL_TEST = 'http://api.sandbox.dnsmadeeasy.com/V2.0/';
  31. public function testConnection()
  32. {
  33. $info = $this->_get('dns/managed/');
  34. if( !isset($info['data']) )
  35. {
  36. throw new exceptions\DNSSubmoduleException('Unknown Error', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  37. }
  38. }
  39. public function zoneExists()
  40. {
  41. if( $this->domain_id() > 0 )
  42. {
  43. $result = $this->_get('dns/managed/' . $this->domain_id());
  44. if( $result['id'] > 0 )
  45. {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. public function getRecords( $recordType = false )
  52. {
  53. $result = $this->_get('dns/managed/' . $this->domain_id() . '/records');
  54. $out = [];
  55. foreach( $result['data'] as $data )
  56. {
  57. if( in_array((string)$data['type'], $recordType ? [strtoupper($recordType)] : $this->getAvailableRecordTypes(), true) )
  58. {
  59. $record = new dns\record\Record();
  60. $record->line = (string)$data['id'];
  61. $record->name = (string)$data['name'];
  62. $record->type = (string)$data['type'];
  63. $record->ttl = (string)$data['ttl'];
  64. $record->createRDATAObject();
  65. switch( (string)$data['type'] )
  66. {
  67. case 'MX':
  68. $record->rdata->preference = (string)$data['mxLevel'];
  69. $record->rdata->exchange = (string)$data['value'];
  70. break;
  71. case 'HTTPRED':
  72. $record->rdata->link = $data['value'];
  73. $record->rdata->title = $data['title'];
  74. $record->rdata->keywords = $data['keywords'];
  75. $record->rdata->redirecttype = $data['redirectType'];
  76. $record->rdata->hardlink = (int)$data['hardLink'];
  77. break;
  78. case 'SRV':
  79. $record->rdata->setDataFromArray($data);
  80. $record->rdata->target = (string)$data['value'];
  81. default:
  82. $record->rdata->setFirstProperty((string)$data['value']);
  83. break;
  84. }
  85. $out[] = $record;
  86. }
  87. }
  88. return $out;
  89. }
  90. private function recordToParamsArray( dns\record\Record $record )
  91. {
  92. $params = [
  93. 'name' => str_replace('@', '', $record->nameToRelative($this->domain)),
  94. 'ttl' => $record->ttl,
  95. 'type' => $record->type
  96. ];
  97. switch( $record->type )
  98. {
  99. case 'PTR':
  100. $params['value'] = trim($record->rdata->ptrdname, '.') . '.';
  101. break;
  102. case 'MX':
  103. $params['mxLevel'] = $record->rdata->preference;
  104. $params['value'] = $this->makeAbsolute($record->rdata->exchange);
  105. break;
  106. case 'HTTPRED':
  107. $params['title'] = $record->rdata->title;
  108. $params['keywords'] = $record->rdata->keywords;
  109. $params['redirectType'] = $record->rdata->redirecttype;
  110. $params['hardLink'] = (bool)$record->rdata->hardlink;
  111. $params['value'] = $record->rdata->link;
  112. break;
  113. case 'SRV':
  114. $params = array_merge($record->rdata->toArray());
  115. $params['value'] = $record->rdata->target;
  116. $params['ttl'] = $record->ttl;
  117. $params['type'] = $record->type;
  118. break;
  119. case 'NS':
  120. $params['value'] = $this->makeAbsolute($record->rdata->nsdname);
  121. break;
  122. case 'ANAME':
  123. $params['value'] = $this->makeAbsolute($record->rdata->aname);
  124. break;
  125. case 'CNAME':
  126. $params['value'] = $this->makeAbsolute($record->rdata->cname);
  127. break;
  128. default:
  129. $params['value'] = $record->rdata->toString();
  130. break;
  131. }
  132. return $params;
  133. }
  134. public function addRecord( dns\record\Record $record )
  135. {
  136. $params = $this->recordToParamsArray($record);
  137. $this->_post('dns/managed/' . $this->domain_id() . '/records/ ', $params);
  138. }
  139. public function editRecord( dns\record\Record $record )
  140. {
  141. $params = $this->recordToParamsArray($record);
  142. $params['gtdLocation'] = "DEFAULT";
  143. $params['id'] = $record->line;
  144. $this->_put('dns/managed/' . $this->domain_id() . '/records/' . $record->line . '/', $params);
  145. }
  146. public function deleteRecord( dns\record\Record $record )
  147. {
  148. $this->_delete('dns/managed/' . $this->domain_id() . '/records/' . $record->line);
  149. }
  150. public function activateZone()
  151. {
  152. $input = [
  153. 'names' => [$this->domain],
  154. ];
  155. $this->_post('dns/managed', $input);
  156. }
  157. public function terminateZone()
  158. {
  159. $this->_delete('dns/managed', [$this->domain_id()]);
  160. }
  161. private function _get( $operation )
  162. {
  163. return $this->_curl($operation, 'GET');
  164. }
  165. private function _delete( $operation, $data = [] )
  166. {
  167. return $this->_curl($operation, 'DELETE', $data);
  168. }
  169. private function _put( $operation, $data = [] )
  170. {
  171. return $this->_curl($operation, 'PUT', $data);
  172. }
  173. private function _post( $operation, $data = [] )
  174. {
  175. return $this->_curl($operation, 'POST', $data);
  176. }
  177. private function _curl( $operation, $method, $postData = null )
  178. {
  179. $url = ($this->config['test'] == 'on' ? self::API_BASE_URL_TEST : self::API_BASE_URL) . $operation;
  180. $ch = curl_init();
  181. curl_setopt($ch, CURLOPT_URL, $url);
  182. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  183. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  184. //curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_headerCallback'));
  185. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_requestHeaders());
  186. if( $method === 'POST' || $method === 'DELETE' )
  187. {
  188. foreach( $postData as $key => $value )
  189. {
  190. if( in_array($key, ['PRIORITY', 'WEIGHT', 'PORT', 'TARGET'], true) )
  191. {
  192. if( $key === 'TARGET' )
  193. {
  194. unset($postData['TARGET']);
  195. }
  196. else
  197. {
  198. $postData[strtolower($key)] = $value;
  199. }
  200. unset($postData[$key]);
  201. }
  202. }
  203. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  204. }
  205. elseif( $method === 'PUT' )
  206. {
  207. curl_setopt($ch, CURLOPT_HTTPHEADER, // Ask the remote end to PUT, curl won't PUT a raw body
  208. array_merge(['X-HTTP-Method-Override: PUT'], $this->_requestHeaders()));
  209. curl_setopt($ch, CURLOPT_POST, true);
  210. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  211. }
  212. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  213. // reset the header values
  214. $this->_headers = [];
  215. $apiResponse = curl_exec($ch);
  216. if( curl_errno($ch) )
  217. {
  218. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  219. }
  220. if( empty($apiResponse) )
  221. {
  222. $apiResponse = '[]';
  223. }
  224. $apiResponse = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/', '$1"$3":', $apiResponse); //key => "key"
  225. $result = json_decode($apiResponse, true);
  226. if( !is_array($result) )
  227. {
  228. throw new exceptions\DNSSubmoduleException('Unable to parse response (' . $apiResponse . ')', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  229. }
  230. $ci = curl_getinfo($ch);
  231. if( !empty($result['error']) || strpos($ci['http_code'], '2') !== 0 )
  232. {
  233. throw new exceptions\DNSSubmoduleException($result['error'][0] ? : 'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  234. }
  235. return $result;
  236. }
  237. private function _requestHeaders()
  238. {
  239. $requestDate = gmdate('r');
  240. return [
  241. "x-dnsme-apiKey: {$this->config['apikey']}",
  242. "x-dnsme-requestDate: $requestDate",
  243. "x-dnsme-hmac: " . $this->_hash($requestDate),
  244. 'content-type:application/json',
  245. 'accept:application/json',
  246. ];
  247. }
  248. private function _hash( $requestDate )
  249. {
  250. return hash_hmac('sha1', $requestDate, $this->config['api_secret']);
  251. }
  252. private function domain_id()
  253. {
  254. if( isset($this->domain_id) )
  255. {
  256. return $this->domain_id;
  257. }
  258. $info = $this->_get('dns/managed/');
  259. foreach( $info['data'] as $value )
  260. {
  261. if( $value['name'] === $this->domain )
  262. {
  263. $this->domain_id = $value['id'];
  264. return $value['id'];
  265. }
  266. }
  267. }
  268. public function getZones()
  269. {
  270. $out = [];
  271. $info = $this->_get('dns/managed/');
  272. foreach( $info['data'] as $value )
  273. {
  274. $out[$value['name']] = '';
  275. }
  276. return $out;
  277. }
  278. /**
  279. * @param $name
  280. *
  281. * @return string
  282. */
  283. private function makeAbsolute( $name )
  284. {
  285. return rtrim($name, '. ') . '.';
  286. }
  287. }