| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- class Freenom extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
-
- public $configFields = array(
- 'email' => array (
- 'friendlyName' => 'Email',
- 'type' => 'email',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'password' => array (
- 'friendlyName' => 'Password',
- 'type' => 'password',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'api_url' => array (
- 'friendlyName' => 'Host',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- );
-
- public $availableTypes = array('A', 'AAAA', 'CNAME', 'LOC', 'MX', 'NAPTR', 'RP', 'TXT');
- public function testConnection() {
- try {
- $this->get('dnsrecord/list.xml', array());
- } catch(exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return true;
- }
- throw $e;
- }
- }
- public function zoneExists() {
- try {
- $xml = $this->get('dnsrecord/list.xml', array(
- 'domainname' => $this->domain
- ));
- return isset($xml->dnsrecord) || $xml->result == 'NO ENTRIES';
- } catch(exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
- }
-
- public function activateZone() {
- $this->post('domain/enable_fn_dns.xml', array(
- 'domainname' => $this->domain
- ));
- }
- public function terminateZone() {
- $this->post('domain/disable_fn_dns.xml', array(
- 'domainname' => $this->domain
- ));
- }
- public function getRecords($recordType = false) {
- $xml = $this->get('dnsrecord/list.xml', array(
- 'domainname' => $this->domain
- ));
-
- $out = array();
- $i = 0;
- foreach($xml->dnsrecord as $r) {
- if(in_array((string)$r->rrtype, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $record = new dns\record\Record();
- $record->line = $i;
- $record->name = (string)$r->name;
- $record->type = (string)$r->rrtype;
- $record->ttl = (string)$r->ttl;
- $record->createRDATAObject();
-
- switch((string)$r->rrtype) {
- case 'MX':
- $record->rdata->preference = (string)$r->priority;
- $record->rdata->exchange = (string)$r->value;
- break;
- case 'RP':
- $record->rdata->mbox = (string)$r->priority;
- $record->rdata->txtdname = (string)$r->value;
- break;
- default:
- $record->rdata->setFirstProperty((string)$r->value);
- break;
- }
- $i++;
- $out[] = $record;
- }
- }
- return $out;
- }
- private function recordToParamsArray(dns\record\Record $record) {
- $params = array(
- 'rrtype' => $record->type,
- 'ttl' => $record->ttl,
- 'name' => $record->nameToAbsolute($domain, false),
- );
-
- switch((string)$r->rrtype) {
- case 'MX':
- $params['value'] = $record->rdata->exchange;
- $params['priority'] = $record->rdata->preference;
- break;
- case 'RP':
- $params['value'] = $record->rdata->txtdname;
- $params['priority'] = $record->rdata->mbox;
- break;
- default:
- $params['value'] = $record->rdata->toString();
- break;
- }
-
- return $params;
- }
-
- public function addRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record);
- $params['domainname'] = $this->domain;
- $this->post('dnsrecord/register.xml', $params);
- }
- public function editRecord(dns\record\Record $record) {
- $records = $this->getRecords();
- foreach($records as $r) {
- if($r->line == $record->line) {
- $this->addRecord($record);
- $this->deleteRecord($r);
- break ;
- }
- }
- }
- public function deleteRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record);
- $params['domainname'] = $this->domain;
- $this->post('dnsrecord/delete.xml', $params);
- }
-
- private function get($function, $params = array()) {
- $url = trim($this->config['api_url'], '/').'/'.$function;
-
- if(is_array($params)) {
- $params['email'] = $this->config['email'];
- $params['password'] = $this->config['password'];
-
- $url .= '?';
-
- foreach($params as $key=>$value) {
- $value = urlencode($value);
- $key = urlencode($key);
- $url .= "{$key}={$value}&";
- }
- }
- $ch = curl_init();
- $chOptions = array (
- CURLOPT_URL => trim($url, '&'),
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 30
- );
- curl_setopt_array($ch, $chOptions);
-
- return $this->execCurl($ch);
- }
-
- private function post($function, $params = array()) {
- $url = trim($this->config['api_url'], '/').'/'.$function;
-
- $post_data = '';
-
- if(is_array($params)) {
- $params['email'] = $this->config['email'];
- $params['password'] = $this->config['password'];
- foreach($params as $key=>$value) {
- $value = urlencode($value);
- $key = urlencode($key);
- $post_data .= "{$key}={$value}&";
- }
- }
- $ch = curl_init();
- $chOptions = array (
- CURLOPT_URL => trim($url, '&'),
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => $post_data,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 30
- );
- curl_setopt_array($ch, $chOptions);
-
- return $this->execCurl($ch);
- }
-
- private function execCurl($ch) {
- $retval = curl_exec($ch);
- if (curl_errno($ch)) {
- throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- curl_close($ch);
-
- @$result = simplexml_load_string($retval);
- if($result === false) {
- throw new exceptions\DNSSubmoduleException('The requested URL was not found on this server.', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- }
-
- if((string)$result->status != 'OK') {
- throw new exceptions\DNSSubmoduleException((string)$result->error?:'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
-
- return $result;
- }
- public function getZones() {
- $xml = $this->get('dnsrecord/list.xml', array('results_per_page' => 9999));
- $out = array();
- foreach($xml->domain as $domain) {
- $out[(string)$domain->domainname] = '';
- }
- return $out;
- }
-
- }
|