StormOnDemand.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 StormOnDemand extends dns\SubmoduleAbstract implements interfaces\SubmoduleIPInterface, interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  8. public $configFields = array(
  9. 'username' => array(
  10. 'friendlyName' => 'Username',
  11. 'validators' => array(
  12. 'required' => 'required',
  13. )
  14. ),
  15. 'password' => array(
  16. 'friendlyName' => 'Password',
  17. 'type' => 'password',
  18. 'validators' => array(
  19. 'required' => 'required',
  20. )
  21. ),
  22. 'default_ip' => array(
  23. 'friendlyName' => 'Default IP',
  24. 'validators' => array(
  25. 'required' => 'required',
  26. 'pattern' => Patterns::IP4_OR_IP6,
  27. )
  28. ),
  29. );
  30. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'PTR');
  31. public function testConnection() {
  32. $info = $this->get('Utilities/Info/ping');
  33. return $info->ping ? true : false;
  34. }
  35. public function zoneExists() {
  36. try {
  37. $out = $this->get('Network/DNS/Zone/list', array(
  38. 'domain' => $this->domain,
  39. 'page_size' => 9999
  40. ));
  41. } catch (exceptions\DNSSubmoduleException $e) {
  42. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  43. return false;
  44. }
  45. throw $e;
  46. }
  47. foreach($out->items as $item) {
  48. if((string)$item->name == $this->domain) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. public function getRecords($recordType = false) {
  55. $out = $this->get('/Network/DNS/Record/list', array(
  56. 'zone' => $this->domain
  57. ));
  58. $return = array();
  59. foreach($out->items as $r) {
  60. $type = strtoupper((string)$r->type);
  61. if(in_array($type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  62. $record = dns\record\Record::tryToCreateFromArray((array)$r);
  63. $record->line = (string)$r->id;
  64. $record->rdata->fromString((string)$r->rdata);
  65. $return[] = $record;
  66. }
  67. }
  68. return $return;
  69. }
  70. public function addRecord(dns\record\Record $record) {
  71. $params = array(
  72. 'zone' => $this->domain,
  73. 'name' => $record->nameToAbsolute($this->domain, false),
  74. 'rdata' => $record->rdata->toString(),
  75. 'ttl' => $record->ttl,
  76. 'type' => $record->type,
  77. );
  78. $out = $this->get('Network/DNS/Record/create', $params);
  79. }
  80. public function editRecord(dns\record\Record $record) {
  81. $input = array( //TODO: sprawdzać czy należy do zona?
  82. 'id' => $record->line,
  83. 'name' => $record->nameToAbsolute($this->domain, false),
  84. 'ttl' => $record->ttl,
  85. 'rdata' => $record->rdata->toString(),
  86. );
  87. $this->get('Network/DNS/Record/update', $input);
  88. }
  89. public function deleteRecord(dns\record\Record $record) { //TODO: sprawdzanie przynależności
  90. $params = array('id' => $record->line);
  91. $this->get('Network/DNS/Record/delete', $params);
  92. }
  93. public function removeRDNS($ip) {
  94. $this->get('Network/DNS/Reverse/delete', array('ip' => $ip));
  95. }
  96. public function updateRDNS($ip, $ttl = false, $value = false) {
  97. $this->get('Network/DNS/Reverse/update', array('ip' => $ip, 'hostname' => $value?:$this->domain));
  98. }
  99. public function getRDNSRecord($ip) {
  100. $out = $this->get('Network/IP/list', array(
  101. 'page_size' => 9999
  102. ));
  103. foreach($out->items as $item) {
  104. if((string)$item->ip == $ip) {
  105. return dns\utils\ReverseDNSHelper::createPTRRecord($ip, 14400, $item->reverse_dns);
  106. }
  107. }
  108. return false;
  109. }
  110. public function activateZone() {
  111. if($this->ip != '') {
  112. if(!filter_var($this->ip, FILTER_VALIDATE_IP)) {
  113. throw new exceptions\DNSSubmoduleException('IP is not valid!', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
  114. }
  115. } else {
  116. $this->ip = $this->config['default_ip'];
  117. }
  118. $out = $this->get('Network/DNS/Zone/create ', array(
  119. 'name' => $this->domain,
  120. 'zone_data' => array(
  121. 'ip' => $this->ip,
  122. )
  123. ));
  124. if($out->active == '0') {
  125. throw new exceptions\DNSSubmoduleException('Error occured:'.$out->error, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  126. }
  127. }
  128. public function terminateZone() {
  129. $out = $this->get('/Network/DNS/Zone/delete', array(
  130. 'name' => $this->domain
  131. ));
  132. if(empty($out->deleted)) {
  133. throw new exceptions\DNSSubmoduleException('Error occured:'.$out->full_message, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  134. }
  135. }
  136. private function get($function, $params = []) {
  137. $query = "https://api.stormondemand.com/".$function;
  138. if(count($params)>0) {
  139. $params = array('params'=>$params);
  140. }
  141. $curl = curl_init();
  142. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
  143. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
  144. curl_setopt($curl, CURLOPT_HEADER,0);
  145. curl_setopt($curl, CURLOPT_TIMEOUT, 50);
  146. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  147. if(count($params)>0) {
  148. curl_setopt($curl,CURLOPT_POST,1);
  149. curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($params));
  150. }
  151. curl_setopt($curl, CURLOPT_USERPWD, $this->config['username'].':'.$this->config['password']);
  152. curl_setopt($curl, CURLOPT_URL, $query);
  153. $result = curl_exec($curl); //var_dump($result);
  154. if (curl_errno($curl)) {
  155. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($curl) . " - " . curl_error($curl), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  156. }
  157. if($result == 'Authorization denied' || $result == 'Authorization required') {
  158. throw new exceptions\DNSSubmoduleException($result, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  159. }
  160. $out = json_decode($result);
  161. if(empty($out)) {
  162. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  163. }
  164. $this->checkForErrors($out);
  165. curl_close($curl);
  166. return $out;
  167. }
  168. private function checkForErrors($obj) {
  169. if(isset($obj->error) || isset($obj->error_class)) {
  170. $message = isset($obj->full_message)?$obj->full_message:$obj->error;
  171. throw new exceptions\DNSSubmoduleException($message, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  172. }
  173. }
  174. public function getZones() {
  175. $out = $this->get('Network/DNS/Zone/list', array(
  176. 'page_size' => 9999
  177. ));
  178. $return = array();
  179. foreach($out->items as $item) {
  180. $return[(string)$item->name] = (string)$item->master;
  181. }
  182. return $return;
  183. }
  184. }