| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?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;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
- class Zonomi extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
- public $configFields = array(
- 'apikey' =>array (
- 'friendlyName' => 'API Key',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'default_ip' =>array (
- 'friendlyName' => 'Default IP',
- 'validators' => array(
- 'required' => 'required',
- 'pattern' => Patterns::IP4_OR_IP6,
- )
- ),
- );
-
- public $availableTypes = array('A', 'NS', 'MX', 'CNAME', 'TXT');
-
- private function get($params, $create = false) {
- if(empty(trim($this->config['apikey']))){
- throw new exceptions\DNSSubmoduleException("Authorization required", dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- if($create)
- $url = 'https://zonomi.com/app/dns/addzone.jsp?api_key='.trim($this->config['apikey']);
- else
- $url = 'https://zonomi.com/app/dns/dyndns.jsp?api_key='.trim($this->config['apikey']);
-
- if(!empty($params)) {
- foreach($params as $k => $v) {
- $url .= '&'.$k.'='.urlencode($v);
- }
- }
- $ch = curl_init();
- $chOptions = array (
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 90
- );
- curl_setopt_array($ch, $chOptions);
- $out = 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);
- @$parsed = simplexml_load_string($out);
- if($parsed===FALSE && !$create) {
- throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- }
-
- if((string)$parsed->is_ok == 'OK:')
- return $parsed;
- if(isset($parsed->error)) {
- throw new exceptions\DNSSubmoduleException((string)$parsed->error, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- throw new exceptions\DNSSubmoduleException((string)trim($parsed)?:'Unexpected error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
-
- public function testConnection()
- {
- $params = array(
- 'action' => 'QUERYZONES',
- // 'name' => $this->domain
- );
-
- $this->get($params);
- }
- public function zoneExists() {
- $params = array(
- 'action' => 'QUERY',
- 'name' => $this->domain
- );
-
- try {
- $this->get($params);
- } catch (exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
-
- return true;
- }
- public function getRecords($recordType = false) {
- $params = array(
- 'action' => 'QUERY',
- //'type' => $recordType,
- 'name' => '**.'.$this->domain
- );
- $out = array();
- $result = $this->get($params);
- foreach($result->actions->action->record as $data) {
- if(in_array((string)$data['type'], $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $record = new dns\record\Record(); //A gdzie jest id?
- $record->name = (string)$data['name'];
- $record->type = (string)$data['type'];
- $record->ttl = (string)$data['ttl'];
- $record->createRDATAObject();
- switch((string)$data['type']) {
- case 'MX':
- $record->rdata->preference = (string)$data['prio'];
- $record->rdata->exchange = (string)$data['content'];
- break;
- default:
- $record->rdata->setFirstProperty((string)$data['content']);
- break;
- }
- $out[] = $record;
- }
- }
-
- return $out;
- }
- private function recordToParamsArray($record) {
- $params = array(
- 'name' => $record->nameToAbsolute($this->domain, false),
- 'type' => $record->type,
- 'ttl' => $record->ttl,
- );
-
- switch($record->type) {
- case 'MX':
- $params['prio'] = $record->rdata->preference;
- $params['value'] = $record->rdata->exchange;
- break;
- default:
- $params['value'] = $record->rdata->toString();
- break;
- }
- return $params;
- }
-
- public function addRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record);
- $params['action'] = 'SET';
- $this->get($params);
- }
- public function editRecord(dns\record\Record $record) {
- $this->addRecord($record); //?!oneone TODO: sprawdzic
- }
- public function deleteRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record); //TODO: sprawdzić bo wcześniej było troszkę inaczej
- $params['action'] = 'DELETE';
- $result = $this->get($params);
-
- if((int)$result->result_counts['unchanged'] > 0)
- throw new exceptions\DNSSubmoduleException('You cannot delete NS records', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- public function activateZone() {
- $params = array(
- 'name' => $this->domain
- );
- $this->get($params, true);
- }
- public function terminateZone() {
- $params = array(
- 'name' => $this->domain,
- 'action' => 'DELETEZONE'
- );
- $this->get($params);
- }
- public function getZones() {
- $params = array(
- 'action' => 'QUERYZONES'
- );
- $ret = $this->get($params);
-
- $out = array();
- foreach($ret->actions->action->zone as $zone) {
- $attr=$zone->attributes();
- $out[(string)$attr['name']] = '';
- }
- return $out;
- }
-
- }
|