RRPproxy.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  7. class RRPproxy extends dns\SubmoduleAbstract
  8. implements interfaces\SubmoduleTTLInterface,
  9. interfaces\SubmoduleImportInterface,
  10. interfaces\SubmoduleRDNSInterface,
  11. interfaces\SubmoduleCustomParseEditZoneInput,
  12. interfaces\SubmoduleCustomEditRecords
  13. {
  14. CONST URL = 'https://api.rrpproxy.net:8082/soap';
  15. public $configFields = array (
  16. 'username' => array (
  17. 'friendlyName' => 'Username',
  18. 'validators' => array(
  19. 'required' => 'required',
  20. )
  21. ),
  22. 'password' => array (
  23. 'friendlyName' => 'User Password',
  24. 'type'=> 'password',
  25. 'validators' => array(
  26. 'required' => 'required',
  27. )
  28. ),
  29. 'default_ip' => array (
  30. 'friendlyName' => 'Default IP',
  31. 'validators' => array(
  32. 'pattern' => Patterns::IP4_OR_IP6,
  33. )
  34. ),
  35. );
  36. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV', 'PTR','NAPTR','X-SMTP','X-HTTP');
  37. private function verifySoap( )
  38. {
  39. if( !class_exists( 'SoapClient' ))
  40. {
  41. throw new exceptions\DNSSubmoduleException("SOAP Error: Soap not found" , dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  42. }
  43. }
  44. private function get($command, $params = array()) {
  45. $this->verifySoap();
  46. $params = array(array_merge($params, array(
  47. "s_login" => $this->config['username'],
  48. "s_pw" => $this->config['password'],
  49. "command" => $command,
  50. ))
  51. );
  52. $client = new \SoapClient(NULL, array(
  53. "location" => self::URL,
  54. "uri" => "urn:Api",
  55. "style" => SOAP_RPC,
  56. "use" => SOAP_ENCODED,
  57. "exceptions" => 0
  58. )
  59. );
  60. $result = $client->__call("xcall", $params,
  61. array("uri" => "urn:Api", "soapaction" => "urn:Api#xcall")
  62. );
  63. if (is_soap_fault($result)) {
  64. throw new exceptions\DNSSubmoduleException("SOAP Error: " . $result->faultcode . " - " . $result->faultstring, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  65. }
  66. if($result->CODE != 200) {
  67. throw new exceptions\DNSSubmoduleException($result->DESCRIPTION?:"Unknown Error", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  68. }
  69. return $result;
  70. }
  71. public function testConnection() {
  72. $this->get('QueryDNSZoneList', array('limit' => 1));
  73. }
  74. public function zoneExists() {
  75. try {
  76. $out = $this->get('QueryDNSZoneList', array('dnszone' => $this->domain, 'limit' => 1));
  77. } catch (exceptions\DNSSubmoduleException $e) {
  78. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  79. return false;
  80. }
  81. throw $e;
  82. }
  83. return !empty($out->PROPERTY->DNSZONE);
  84. }
  85. public function getRecords($recordType = false) {
  86. $result = $this->get('QueryDNSZoneRRList', array('dnszone' => $this->domain));
  87. $out = array();
  88. foreach($result->PROPERTY->RR as $line => $r) {
  89. $pad = array_pad(explode(' ',$r), 5, ''); //two line record parser ;)
  90. list($name, $ttl, $class, $type, $value) = array_merge(array_slice($pad,0, 4), array(implode(' ', array_slice($pad,4))));
  91. if(in_array($type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  92. $record = new dns\record\Record();
  93. $record->line = $line;
  94. $record->name = $name;
  95. $record->type = $type;
  96. $record->ttl = $ttl;
  97. $record->createRDATAObject();
  98. $record->rdata->fromString($value);
  99. $out[] = $record;
  100. }
  101. }
  102. return $out;
  103. }
  104. private function recordToString($record) {
  105. $name = $record->nameToAbsolute($this->domain, true);
  106. return $name . " " . $record->ttl . " " . strtoupper($record->type) . " " . $record->rdata->toString();
  107. }
  108. public function addRecord(dns\record\Record $record) {
  109. $params = array(
  110. 'dnszone' => $this->domain,
  111. 'addrr0' => $this->recordToString($record),
  112. );
  113. $this->get('ModifyDNSZone', $params);
  114. }
  115. public function editRecord(dns\record\Record $record) {
  116. $params = array(
  117. "dnszone" => $this->domain,
  118. 'rr' . $record->line => $this->recordToString($record), //TODO: string czy do arraya line dac?
  119. );
  120. $this->get('ModifyDNSZone', $params);
  121. }
  122. public function deleteRecord(dns\record\Record $record) {
  123. $params = array(
  124. "dnszone" => $this->domain,
  125. 'delrr' . $record->line => $this->recordToString($record),
  126. );
  127. $this->get('ModifyDNSZone', $params);
  128. }
  129. public function activateZone() {
  130. $params = array(
  131. 'dnszone' => $this->domain
  132. );
  133. $out = $this->get('AddDNSZone', $params);
  134. }
  135. public function terminateZone() {
  136. $params = array(
  137. 'dnszone' => $this->domain
  138. );
  139. $this->get('DeleteDNSZone', $params);
  140. }
  141. public function getZones() {
  142. $ret = $this->get('QueryDNSZoneList');
  143. $out = array();
  144. $dnsZones = $ret->dnszone ? $ret->dnszone : $ret->PROPERTY->DNSZONE;
  145. foreach($dnsZones as $zone) {
  146. $out[$zone] = '';
  147. }
  148. return $out;
  149. }
  150. public function convertInputFormData(&$input)
  151. {
  152. if(count($input['edit_record']) > 0)
  153. {
  154. $input['edit_record'] = array_merge($input['edit_record'], $input['record']);
  155. }
  156. }
  157. public function customEditRecords($input)
  158. {
  159. if(count($input['edit_record']) == 0)
  160. {
  161. return true;
  162. }
  163. $records = array();
  164. foreach($input['edit_record'] as $record_data)
  165. {
  166. $records[] = $this->createRecordFromInput($record_data);
  167. }
  168. $params = array(
  169. "dnszone" => $this->domain,
  170. //'rr' . $record->line => $this->recordToString($record), //TODO: string czy do arraya line dac?
  171. );
  172. foreach($records as $record)
  173. {
  174. $params['rr' . $record->line] = $this->recordToString($record);
  175. }
  176. $this->get('ModifyDNSZone', $params);
  177. return true;
  178. }
  179. private function createRecordFromInput($input)
  180. {
  181. $record = dns\record\Record::tryToCreateFromArray($input);
  182. $record->rdata->setDataFromArray($input['field']);
  183. $record->decode();
  184. return $record;
  185. }
  186. }