| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \Exception;
- 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;
- use \SoapClient;
- use \stdClass;
- //TODO: wersja dla najnowszego whma
- class Nettica extends dns\SubmoduleAbstract implements interfaces\SubmoduleIPInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
-
- public $configFields = array(
- 'username' =>array (
- 'friendlyName' => 'Username',
- 'validators' => array(
- 'required' => 'required',
- ),
- ),
- 'password' =>array (
- 'friendlyName' => 'Password',
- 'type' => 'password',
- 'validators' => array(
- 'required' => 'required',
- ),
- ),
- 'default_ip' =>array (
- 'friendlyName' => 'Default IP',
- 'validators' => array(
- 'required' => 'required',
- 'pattern' => Patterns::IP4_OR_IP6,
- ),
- ),
- );
-
- public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');//, 'F');
-
- //TODO: idna decode
-
- private function get($function, $params = false) {
- $url = 'https://www.nettica.com/DNS/DnsApi.asmx?WSDL';
- $content = file_get_contents('https://www.nettica.com/DNS/DnsApi.asmx?WSDL');
- if(strpos($content, '<!DOCTYPE HTML') !== FALSE && strpos($content, 'Not Found') !== FALSE) {
- $url = __DIR__ . DIRECTORY_SEPARATOR . 'nettica' . DIRECTORY_SEPARATOR . 'nettica.wsdl';
- }
-
- $soap = new SoapClient($url, array('soap_version' => SOAP_1_2, 'trace' => 1, 'exception' => 1));
- if($params === false)
- $params = new stdClass();
- $params->UserName = $this->config['username'];
- $params->Password = base64_encode($this->config['password']);
- try {
- $result = $soap->$function($params);
- }catch(Exception $e){
- throw new exceptions\DNSSubmoduleException($e->faultstring, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- $res_str = $function . 'Result';
- if($result->$res_str->Result->Status != 200) {
- throw new exceptions\DNSSubmoduleException($result->$res_str->Result->Description?:"Unknown Error", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
-
- return $result;
- }
-
- public function testConnection(){
- $result = $this->get('ListZones');
- }
- public function zoneExists() {
- try {
- $result = $this->get('ListZones');
- return isset($result->ListZonesResult->Zone->string) && in_array($this->domain, (array)$result->ListZonesResult->Zone->string);
- } catch (exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
- }
- public function getRecords($recordType=false) {
- $input = new stdClass();
- $input->DomainName = $this->domain;
-
- $result = $this->get('ListDomain', $input);
- $out = array();
- if(!isset($result->ListDomainResult->Record->DomainRecord)){
- return $out;
- }
-
- if($result->ListDomainResult->Count == 1){
- $records[] = $result->ListDomainResult->Record->DomainRecord;
- } else {
- $records = $result->ListDomainResult->Record->DomainRecord;
- }
-
- foreach($records as $k => $r){
- if(in_array($r->RecordType, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $record = new dns\record\Record();
- $record->line = $k;
- $record->name = $r->HostName;
- $record->type = $r->RecordType;
- $record->ttl = $r->TTL;
- $record->createRDATAObject();
-
- switch ($r->RecordType) {
- case 'MX':
- $record->rdata->preference = $r->Priority;
- $record->rdata->exchange = $r->Data;
- break;
-
- default:
- $record->rdata->fromString($r->Data);
- break;
- }
-
-
- $out[] = $record;
- }
-
- }
-
- return $out;
- }
- private function recordToParams(dns\record\Record $record, $name = 'd') {
- $params = new stdClass();
-
- $params->$name->DomainName = (string)$this->$nameomain;
- $params->$name->HostName = (string)$record->nameToAbsolute($this->$nameomain);
- $params->$name->RecordType = (string)$record->type;
- $params->$name->Data = (string)$value;
- $params->$name->TTL = (int)$record->ttl;
- $params->$name->Priority = (int)$priority;
- switch ($record->type) {
- case 'MX':
- $params->$name->Priority = (string)$record->rdata->preference;
- $params->$name->Data = (string)$record->rdata->exchange;
- break;
- default:
- $params->$name->Priority = 0;
- $params->$name->Data = $record->rdata->toString();
- break;
- }
-
- return $params;
- }
-
- public function addRecord(dns\record\Record $record) {
- $this->get('AddRecord', $this->recordToParams($record));
- }
- public function editRecord(dns\record\Record $record) {
- $records = $this->getRecords();
-
- $params = array();
- foreach($records as $r) {
- if($r->line == $record->line) {
- $params = $this->recordToParams($r, 'Old');
- }
- }
-
- $params = (object) array_merge((array) $params, (array) $this->recordToParams($r, 'New'));
- $result = $this->get('UpdateRecord', $params);
- }
-
- public function deleteRecord(dns\record\Record $record) {
- $this->get('DeleteRecord', $this->recordToParams($record));
- }
- public function activateZone() {
- if($this->ip != '') {
- if(!filter_var($this->ip, FILTER_VALIDATE_IP)) {
- throw new exceptions\DNSSubmoduleException("IP is not valid!", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- } else {
- $this->ip = $this->config['default_ip'];
- }
-
- $input = new stdClass();
- $input->DomainName = $this->domain;
- $input->IpAddress = $this->ip;
-
- $this->get('CreateZone', $input);
- }
- public function terminateZone() {
- $input = new stdClass();
- $input->DomainName = $this->domain;
-
- $result = $this->get('DeleteZone', $input);
- }
- public function getZones() {
- $result = $this->get('ListZones');
- $out = array();
- foreach((array)$result->ListZonesResult->Zone->string as $domain) {
- $out[$domain] = '';
- }
-
- return $out;
- }
- }
|