Freenom.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 Freenom extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  7. public $configFields = array(
  8. 'email' => array (
  9. 'friendlyName' => 'Email',
  10. 'type' => 'email',
  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. 'api_url' => array (
  23. 'friendlyName' => 'Host',
  24. 'validators' => array(
  25. 'required' => 'required',
  26. )
  27. ),
  28. );
  29. public $availableTypes = array('A', 'AAAA', 'CNAME', 'LOC', 'MX', 'NAPTR', 'RP', 'TXT');
  30. public function testConnection() {
  31. try {
  32. $this->get('dnsrecord/list.xml', array());
  33. } catch(exceptions\DNSSubmoduleException $e) {
  34. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  35. return true;
  36. }
  37. throw $e;
  38. }
  39. }
  40. public function zoneExists() {
  41. try {
  42. $xml = $this->get('dnsrecord/list.xml', array(
  43. 'domainname' => $this->domain
  44. ));
  45. return isset($xml->dnsrecord) || $xml->result == 'NO ENTRIES';
  46. } catch(exceptions\DNSSubmoduleException $e) {
  47. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  48. return false;
  49. }
  50. throw $e;
  51. }
  52. }
  53. public function activateZone() {
  54. $this->post('domain/enable_fn_dns.xml', array(
  55. 'domainname' => $this->domain
  56. ));
  57. }
  58. public function terminateZone() {
  59. $this->post('domain/disable_fn_dns.xml', array(
  60. 'domainname' => $this->domain
  61. ));
  62. }
  63. public function getRecords($recordType = false) {
  64. $xml = $this->get('dnsrecord/list.xml', array(
  65. 'domainname' => $this->domain
  66. ));
  67. $out = array();
  68. $i = 0;
  69. foreach($xml->dnsrecord as $r) {
  70. if(in_array((string)$r->rrtype, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  71. $record = new dns\record\Record();
  72. $record->line = $i;
  73. $record->name = (string)$r->name;
  74. $record->type = (string)$r->rrtype;
  75. $record->ttl = (string)$r->ttl;
  76. $record->createRDATAObject();
  77. switch((string)$r->rrtype) {
  78. case 'MX':
  79. $record->rdata->preference = (string)$r->priority;
  80. $record->rdata->exchange = (string)$r->value;
  81. break;
  82. case 'RP':
  83. $record->rdata->mbox = (string)$r->priority;
  84. $record->rdata->txtdname = (string)$r->value;
  85. break;
  86. default:
  87. $record->rdata->setFirstProperty((string)$r->value);
  88. break;
  89. }
  90. $i++;
  91. $out[] = $record;
  92. }
  93. }
  94. return $out;
  95. }
  96. private function recordToParamsArray(dns\record\Record $record) {
  97. $params = array(
  98. 'rrtype' => $record->type,
  99. 'ttl' => $record->ttl,
  100. 'name' => $record->nameToAbsolute($domain, false),
  101. );
  102. switch((string)$r->rrtype) {
  103. case 'MX':
  104. $params['value'] = $record->rdata->exchange;
  105. $params['priority'] = $record->rdata->preference;
  106. break;
  107. case 'RP':
  108. $params['value'] = $record->rdata->txtdname;
  109. $params['priority'] = $record->rdata->mbox;
  110. break;
  111. default:
  112. $params['value'] = $record->rdata->toString();
  113. break;
  114. }
  115. return $params;
  116. }
  117. public function addRecord(dns\record\Record $record) {
  118. $params = $this->recordToParamsArray($record);
  119. $params['domainname'] = $this->domain;
  120. $this->post('dnsrecord/register.xml', $params);
  121. }
  122. public function editRecord(dns\record\Record $record) {
  123. $records = $this->getRecords();
  124. foreach($records as $r) {
  125. if($r->line == $record->line) {
  126. $this->addRecord($record);
  127. $this->deleteRecord($r);
  128. break ;
  129. }
  130. }
  131. }
  132. public function deleteRecord(dns\record\Record $record) {
  133. $params = $this->recordToParamsArray($record);
  134. $params['domainname'] = $this->domain;
  135. $this->post('dnsrecord/delete.xml', $params);
  136. }
  137. private function get($function, $params = array()) {
  138. $url = trim($this->config['api_url'], '/').'/'.$function;
  139. if(is_array($params)) {
  140. $params['email'] = $this->config['email'];
  141. $params['password'] = $this->config['password'];
  142. $url .= '?';
  143. foreach($params as $key=>$value) {
  144. $value = urlencode($value);
  145. $key = urlencode($key);
  146. $url .= "{$key}={$value}&";
  147. }
  148. }
  149. $ch = curl_init();
  150. $chOptions = array (
  151. CURLOPT_URL => trim($url, '&'),
  152. CURLOPT_RETURNTRANSFER => true,
  153. CURLOPT_SSL_VERIFYPEER => false,
  154. CURLOPT_SSL_VERIFYHOST => false,
  155. CURLOPT_TIMEOUT => 30
  156. );
  157. curl_setopt_array($ch, $chOptions);
  158. return $this->execCurl($ch);
  159. }
  160. private function post($function, $params = array()) {
  161. $url = trim($this->config['api_url'], '/').'/'.$function;
  162. $post_data = '';
  163. if(is_array($params)) {
  164. $params['email'] = $this->config['email'];
  165. $params['password'] = $this->config['password'];
  166. foreach($params as $key=>$value) {
  167. $value = urlencode($value);
  168. $key = urlencode($key);
  169. $post_data .= "{$key}={$value}&";
  170. }
  171. }
  172. $ch = curl_init();
  173. $chOptions = array (
  174. CURLOPT_URL => trim($url, '&'),
  175. CURLOPT_POST => 1,
  176. CURLOPT_POSTFIELDS => $post_data,
  177. CURLOPT_RETURNTRANSFER => true,
  178. CURLOPT_SSL_VERIFYPEER => false,
  179. CURLOPT_SSL_VERIFYHOST => false,
  180. CURLOPT_TIMEOUT => 30
  181. );
  182. curl_setopt_array($ch, $chOptions);
  183. return $this->execCurl($ch);
  184. }
  185. private function execCurl($ch) {
  186. $retval = curl_exec($ch);
  187. if (curl_errno($ch)) {
  188. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  189. }
  190. curl_close($ch);
  191. @$result = simplexml_load_string($retval);
  192. if($result === false) {
  193. throw new exceptions\DNSSubmoduleException('The requested URL was not found on this server.', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  194. }
  195. if((string)$result->status != 'OK') {
  196. throw new exceptions\DNSSubmoduleException((string)$result->error?:'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  197. }
  198. return $result;
  199. }
  200. public function getZones() {
  201. $xml = $this->get('dnsrecord/list.xml', array('results_per_page' => 9999));
  202. $out = array();
  203. foreach($xml->domain as $domain) {
  204. $out[(string)$domain->domainname] = '';
  205. }
  206. return $out;
  207. }
  208. }