Nettica.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \Exception;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  7. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  8. use \SoapClient;
  9. use \stdClass;
  10. //TODO: wersja dla najnowszego whma
  11. class Nettica extends dns\SubmoduleAbstract implements interfaces\SubmoduleIPInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  12. public $configFields = array(
  13. 'username' =>array (
  14. 'friendlyName' => 'Username',
  15. 'validators' => array(
  16. 'required' => 'required',
  17. ),
  18. ),
  19. 'password' =>array (
  20. 'friendlyName' => 'Password',
  21. 'type' => 'password',
  22. 'validators' => array(
  23. 'required' => 'required',
  24. ),
  25. ),
  26. 'default_ip' =>array (
  27. 'friendlyName' => 'Default IP',
  28. 'validators' => array(
  29. 'required' => 'required',
  30. 'pattern' => Patterns::IP4_OR_IP6,
  31. ),
  32. ),
  33. );
  34. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');//, 'F');
  35. //TODO: idna decode
  36. private function get($function, $params = false) {
  37. $url = 'https://www.nettica.com/DNS/DnsApi.asmx?WSDL';
  38. $content = file_get_contents('https://www.nettica.com/DNS/DnsApi.asmx?WSDL');
  39. if(strpos($content, '<!DOCTYPE HTML') !== FALSE && strpos($content, 'Not Found') !== FALSE) {
  40. $url = __DIR__ . DIRECTORY_SEPARATOR . 'nettica' . DIRECTORY_SEPARATOR . 'nettica.wsdl';
  41. }
  42. $soap = new SoapClient($url, array('soap_version' => SOAP_1_2, 'trace' => 1, 'exception' => 1));
  43. if($params === false)
  44. $params = new stdClass();
  45. $params->UserName = $this->config['username'];
  46. $params->Password = base64_encode($this->config['password']);
  47. try {
  48. $result = $soap->$function($params);
  49. }catch(Exception $e){
  50. throw new exceptions\DNSSubmoduleException($e->faultstring, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  51. }
  52. $res_str = $function . 'Result';
  53. if($result->$res_str->Result->Status != 200) {
  54. throw new exceptions\DNSSubmoduleException($result->$res_str->Result->Description?:"Unknown Error", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  55. }
  56. return $result;
  57. }
  58. public function testConnection(){
  59. $result = $this->get('ListZones');
  60. }
  61. public function zoneExists() {
  62. try {
  63. $result = $this->get('ListZones');
  64. return isset($result->ListZonesResult->Zone->string) && in_array($this->domain, (array)$result->ListZonesResult->Zone->string);
  65. } catch (exceptions\DNSSubmoduleException $e) {
  66. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  67. return false;
  68. }
  69. throw $e;
  70. }
  71. }
  72. public function getRecords($recordType=false) {
  73. $input = new stdClass();
  74. $input->DomainName = $this->domain;
  75. $result = $this->get('ListDomain', $input);
  76. $out = array();
  77. if(!isset($result->ListDomainResult->Record->DomainRecord)){
  78. return $out;
  79. }
  80. if($result->ListDomainResult->Count == 1){
  81. $records[] = $result->ListDomainResult->Record->DomainRecord;
  82. } else {
  83. $records = $result->ListDomainResult->Record->DomainRecord;
  84. }
  85. foreach($records as $k => $r){
  86. if(in_array($r->RecordType, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  87. $record = new dns\record\Record();
  88. $record->line = $k;
  89. $record->name = $r->HostName;
  90. $record->type = $r->RecordType;
  91. $record->ttl = $r->TTL;
  92. $record->createRDATAObject();
  93. switch ($r->RecordType) {
  94. case 'MX':
  95. $record->rdata->preference = $r->Priority;
  96. $record->rdata->exchange = $r->Data;
  97. break;
  98. default:
  99. $record->rdata->fromString($r->Data);
  100. break;
  101. }
  102. $out[] = $record;
  103. }
  104. }
  105. return $out;
  106. }
  107. private function recordToParams(dns\record\Record $record, $name = 'd') {
  108. $params = new stdClass();
  109. $params->$name->DomainName = (string)$this->$nameomain;
  110. $params->$name->HostName = (string)$record->nameToAbsolute($this->$nameomain);
  111. $params->$name->RecordType = (string)$record->type;
  112. $params->$name->Data = (string)$value;
  113. $params->$name->TTL = (int)$record->ttl;
  114. $params->$name->Priority = (int)$priority;
  115. switch ($record->type) {
  116. case 'MX':
  117. $params->$name->Priority = (string)$record->rdata->preference;
  118. $params->$name->Data = (string)$record->rdata->exchange;
  119. break;
  120. default:
  121. $params->$name->Priority = 0;
  122. $params->$name->Data = $record->rdata->toString();
  123. break;
  124. }
  125. return $params;
  126. }
  127. public function addRecord(dns\record\Record $record) {
  128. $this->get('AddRecord', $this->recordToParams($record));
  129. }
  130. public function editRecord(dns\record\Record $record) {
  131. $records = $this->getRecords();
  132. $params = array();
  133. foreach($records as $r) {
  134. if($r->line == $record->line) {
  135. $params = $this->recordToParams($r, 'Old');
  136. }
  137. }
  138. $params = (object) array_merge((array) $params, (array) $this->recordToParams($r, 'New'));
  139. $result = $this->get('UpdateRecord', $params);
  140. }
  141. public function deleteRecord(dns\record\Record $record) {
  142. $this->get('DeleteRecord', $this->recordToParams($record));
  143. }
  144. public function activateZone() {
  145. if($this->ip != '') {
  146. if(!filter_var($this->ip, FILTER_VALIDATE_IP)) {
  147. throw new exceptions\DNSSubmoduleException("IP is not valid!", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  148. }
  149. } else {
  150. $this->ip = $this->config['default_ip'];
  151. }
  152. $input = new stdClass();
  153. $input->DomainName = $this->domain;
  154. $input->IpAddress = $this->ip;
  155. $this->get('CreateZone', $input);
  156. }
  157. public function terminateZone() {
  158. $input = new stdClass();
  159. $input->DomainName = $this->domain;
  160. $result = $this->get('DeleteZone', $input);
  161. }
  162. public function getZones() {
  163. $result = $this->get('ListZones');
  164. $out = array();
  165. foreach((array)$result->ListZonesResult->Zone->string as $domain) {
  166. $out[$domain] = '';
  167. }
  168. return $out;
  169. }
  170. }